|
| 1 | +import unittest |
| 2 | + |
| 3 | +import os.path |
| 4 | +import tempfile |
| 5 | +from distutils.dir_util import copy_tree |
| 6 | + |
| 7 | +import fixturesUtils |
| 8 | +import testUtils |
| 9 | +import mayaUsd.lib |
| 10 | +import mayaUsd.ufe |
| 11 | +from maya import cmds |
| 12 | +from pxr import Sdf, Usd |
| 13 | + |
| 14 | +from testComponentCreatorBase import _ComponentCreatorTestBase |
| 15 | + |
| 16 | +class SaveToComponentTestCase(_ComponentCreatorTestBase, unittest.TestCase): |
| 17 | + """ |
| 18 | + Tests for usd_component_creator_plugin.create_component.add_to_component_from_nodes |
| 19 | + and then saving and reloading. |
| 20 | +
|
| 21 | + The reloading part is tested after in the test SaveToComponentFollowupTestCase. |
| 22 | + """ |
| 23 | + |
| 24 | + _tempFolder = None |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def setUpClass(cls): |
| 28 | + fixturesUtils.readOnlySetUpClass(__file__, initializeStandalone=False) |
| 29 | + |
| 30 | + testFolderName = "component3Variants" |
| 31 | + fromDirectory = testUtils.getTestScene(testFolderName) |
| 32 | + toDirectory = os.path.join(tempfile.gettempdir(), 'SaveToComponentTestCase') |
| 33 | + copy_tree(fromDirectory, toDirectory) |
| 34 | + SaveToComponentTestCase._tempFolder = toDirectory |
| 35 | + |
| 36 | + @classmethod |
| 37 | + def tearDownClass(cls): |
| 38 | + if SaveToComponentTestCase._tempFolder: |
| 39 | + if os.path.exists(SaveToComponentTestCase._tempFolder): |
| 40 | + import shutil |
| 41 | + shutil.rmtree(SaveToComponentTestCase._tempFolder) |
| 42 | + cls._resetDefaultTemplate() |
| 43 | + return super().tearDownClass() |
| 44 | + |
| 45 | + def _findVariantSet(self, desc, withName): |
| 46 | + variantSetMap = desc.GetVariantSets() |
| 47 | + self.assertGreaterEqual(len(variantSetMap), 1, "There must be at least one variant set") |
| 48 | + for variantSetName, variantSet in variantSetMap.items(): |
| 49 | + if withName not in variantSetName: |
| 50 | + continue |
| 51 | + return variantSet |
| 52 | + return None |
| 53 | + |
| 54 | + def setUp(self): |
| 55 | + self._setUpCC() |
| 56 | + # Clear the variant editor state so there is no lingering component from a |
| 57 | + # previous test. |
| 58 | + self._resetDefaultTemplate() |
| 59 | + return super().setUp() |
| 60 | + |
| 61 | + def testSaveAndReload(self): |
| 62 | + """ |
| 63 | + Open a Maya scene containing a USD stage of a component. |
| 64 | + Modify all variants of the component by replacing its data with a new node. |
| 65 | + Save the scene. Re-open the scene and verify that the component is still |
| 66 | + valid and has the new data. |
| 67 | + """ |
| 68 | + before = self._snapshotProxyShapes() |
| 69 | + |
| 70 | + mayaSceneFilePath = os.path.join(SaveToComponentTestCase._tempFolder, "repro-3530.ma") |
| 71 | + cmds.file(mayaSceneFilePath, open=True) |
| 72 | + |
| 73 | + proxy = self._findNewProxyShape(before) |
| 74 | + self.assertIsNotNone(proxy) |
| 75 | + stage = mayaUsd.ufe.getStage(proxy) |
| 76 | + self.assertIsNotNone(stage) |
| 77 | + desc = self._getDescFromStage(stage) |
| 78 | + self.assertIsNotNone(desc, 'Could not get ComponentDescription from the Maya scene') |
| 79 | + |
| 80 | + first_vs = self._findVariantSet(desc, 'variant_set_1') |
| 81 | + self.assertTrue(first_vs) |
| 82 | + self.assertGreaterEqual(len(first_vs.GetVariants()), 3, "There must be at least three variants in the set {}".format(first_vs.GetName())) |
| 83 | + |
| 84 | + variantsMap = first_vs.GetVariants() |
| 85 | + self.assertGreaterEqual(len(variantsMap), 3, "There must be at least three variants") |
| 86 | + |
| 87 | + for variantName in variantsMap.keys(): |
| 88 | + polyCubeName = cmds.polyCube(name='pCubeExtra')[0] |
| 89 | + self.assertIn('pCubeExtra', polyCubeName) |
| 90 | + |
| 91 | + # The following code is equivalent to add_to_component_from_nodes, |
| 92 | + # but that function does not work in maya batch mode and we need to run this test in batch mode |
| 93 | + # because the Maya save would pop-up a dialog in interactive mode. |
| 94 | + # |
| 95 | + # from usd_component_creator_plugin import add_to_component_from_nodes |
| 96 | + # result = add_to_component_from_nodes( |
| 97 | + # [polyCubeName], |
| 98 | + # [(first_vs.name, variantName)], |
| 99 | + # is_replacing=True, |
| 100 | + # component_desc=desc) |
| 101 | + # self.assertTrue(result) |
| 102 | + |
| 103 | + from AdskUsdComponentCreator import CreateFromFileCommand |
| 104 | + from usd_component_creator_plugin import ExportNodesCommand, execute_ufe_command, AddComponentToManagerCommand, UfeCommandWrapper |
| 105 | + from usd_component_creator_plugin.create_component import _generate_unique_temp_filename, _update_options_for_node, _update_options_for_purpose_from_nodes |
| 106 | + |
| 107 | + component_desc = desc |
| 108 | + nodes = [polyCubeName] |
| 109 | + variant_selections = [(first_vs.name, variantName)] |
| 110 | + is_replacing = True |
| 111 | + export_options = None |
| 112 | + purpose = None |
| 113 | + |
| 114 | + options = component_desc.GetOptions().Clone() |
| 115 | + _update_options_for_node(options, nodes[0], nodes) |
| 116 | + options.component_variants = variant_selections if variant_selections else [] |
| 117 | + options.replace_variant_content = is_replacing |
| 118 | + # Never change the default variant when appending to an existing component. |
| 119 | + options.is_default_variant = False |
| 120 | + |
| 121 | + input_usd_filename = _generate_unique_temp_filename(options) |
| 122 | + export_cmd = ExportNodesCommand(nodes, input_usd_filename, export_options) |
| 123 | + execute_ufe_command(export_cmd) |
| 124 | + |
| 125 | + creation_options = _update_options_for_purpose_from_nodes(options, purpose, input_usd_filename) |
| 126 | + creation_options.Validate() |
| 127 | + |
| 128 | + delete_input_file = False |
| 129 | + create_cmd = CreateFromFileCommand(component_desc, input_usd_filename, creation_options, delete_input_file) |
| 130 | + self.assertTrue(UfeCommandWrapper.executeWithUndo(create_cmd)) |
| 131 | + |
| 132 | + add_cmd = AddComponentToManagerCommand(create_cmd) |
| 133 | + execute_ufe_command(add_cmd) |
| 134 | + |
| 135 | + |
| 136 | + updated_desc = self._getDescFromStage(stage) |
| 137 | + self.assertIsNotNone(updated_desc, 'Could not get updated ComponentDescription') |
| 138 | + |
| 139 | + # Save the file. Make sure the USD edits will go to a USD file. |
| 140 | + cmds.optionVar(intValue=('mayaUsd_ConfirmExistingFileSave', 0)) |
| 141 | + saveLocation = 1 |
| 142 | + cmds.optionVar(intValue=(mayaUsd.lib.OptionVarTokens.SerializedUsdEditsLocation, saveLocation)) |
| 143 | + cmds.file(save=True, force=True) |
| 144 | + |
| 145 | + cmds.file(new=True, force=True) |
| 146 | + cmds.file(mayaSceneFilePath, open=True) |
| 147 | + |
| 148 | + proxy = self._findNewProxyShape(before) |
| 149 | + self.assertIsNotNone(proxy) |
| 150 | + stage = mayaUsd.ufe.getStage(proxy) |
| 151 | + self.assertIsNotNone(stage) |
| 152 | + desc = self._getDescFromStage(stage) |
| 153 | + self.assertIsNotNone(desc, 'Could not get ComponentDescription from the Maya scene') |
| 154 | + |
| 155 | + first_vs = self._findVariantSet(desc, 'variant_set_1') |
| 156 | + self.assertTrue(first_vs) |
| 157 | + |
| 158 | + variantsMap = first_vs.GetVariants() |
| 159 | + self.assertGreaterEqual(len(variantsMap), 3, "There must be at least three variants") |
| 160 | + |
| 161 | + variant_to_cube_map = { |
| 162 | + 'pPlane1': 'pCubeExtra', |
| 163 | + 'pPlane2': 'pCubeExtra1', |
| 164 | + 'pPlane3': 'pCubeExtra2', |
| 165 | + } |
| 166 | + |
| 167 | + found_cubes = set() |
| 168 | + |
| 169 | + for variant in variantsMap.values(): |
| 170 | + primPath = desc.root_prim_path |
| 171 | + prim = stage.GetPrimAtPath(primPath) |
| 172 | + self.assertTrue(prim) |
| 173 | + prim.GetVariantSet(first_vs.GetName()).SetVariantSelection(variant.GetName()) |
| 174 | + |
| 175 | + stage.Reload() |
| 176 | + |
| 177 | + expected_cube_name = variant_to_cube_map.get(variant.GetName()) |
| 178 | + self.assertIsNotNone(expected_cube_name, "No expected cube name for variant {}".format(variant.GetName())) |
| 179 | + |
| 180 | + geoPrim = stage.GetPrimAtPath(Sdf.Path('/root/geo')) |
| 181 | + for child in geoPrim.GetChildren(): |
| 182 | + print(f'Child: {child.GetName()}') |
| 183 | + if child.GetTypeName() == 'Mesh': |
| 184 | + self.assertEqual(expected_cube_name, child.GetName(), f"The variant {variant.GetName()} should have a cube named {expected_cube_name}") |
| 185 | + found_cubes.add(child.GetName()) |
| 186 | + |
| 187 | + self.assertEqual(len(found_cubes), len(variantsMap), "There should be one cube for each variant") |
| 188 | + |
| 189 | + |
| 190 | +if __name__ == '__main__': |
| 191 | + fixturesUtils.runTests(globals()) |
| 192 | + |
0 commit comments