-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathtestComponentCreatorCreateMultiVariantsComponentFromUsdPrims.py
More file actions
210 lines (159 loc) · 9.97 KB
/
Copy pathtestComponentCreatorCreateMultiVariantsComponentFromUsdPrims.py
File metadata and controls
210 lines (159 loc) · 9.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import unittest
import fixturesUtils
import mayaUsd.ufe
import mayaUsd_createStageWithNewLayer
from pxr import Sdf
from testComponentCreatorBase import _ComponentCreatorTestBase
class CreateMultiVariantsComponentFromUsdPrimsTestCase(_ComponentCreatorTestBase, unittest.TestCase):
"""
Tests for create_multi_variants_component_from_usd_prims.
"""
@classmethod
def setUpClass(cls):
fixturesUtils.readOnlySetUpClass(__file__, initializeStandalone=False)
def setUp(self):
self._setUpCC()
def testEmptyNodesIsNoOp(self):
"""create_multi_variants_component_from_nodes([]) must not create any proxy shape."""
from usd_component_creator_plugin import create_multi_variants_component_from_usd_prims
before = self._snapshotProxyShapes()
create_multi_variants_component_from_usd_prims([])
self.assertEqual(self._snapshotProxyShapes(), before,
"No proxy shape should be created for an empty node list")
def testVariantCount(self):
"""Two input nodes should produce exactly two variants in the first variant set."""
from usd_component_creator_plugin import create_multi_variants_component_from_usd_prims
import AdskUsdComponentCreator
proxyShape = mayaUsd_createStageWithNewLayer.createStageWithNewLayer()
input_stage = mayaUsd.ufe.getStage(proxyShape)
cubePath = Sdf.Path('/pCube1')
input_stage.DefinePrim(cubePath, 'Cube')
conePath = Sdf.Path('/pCone1')
input_stage.DefinePrim(conePath, 'Cone')
paths = [f'{proxyShape},{str(cubePath)}', f'{proxyShape},{str(conePath)}']
before = self._snapshotProxyShapes()
create_multi_variants_component_from_usd_prims(paths)
proxy = self._findNewProxyShape(before)
self.assertIsNotNone(proxy)
stage = mayaUsd.ufe.getStage(proxy)
desc = self._getDescFromStage(stage)
self.assertIsNotNone(desc, 'Could not construct ComponentDescription from stage')
first_vs = self._findVariantSetByName(desc, 'variant_set_1')
self.assertEqual(len(first_vs.GetVariants()), 2,
"Two nodes must produce exactly two variants")
# Note: each prim is in a separate variant, so only one can be active at a time.
# The default is the alphabetically first variant, which corresponds to conePath,
# so we expect to see pCone1 in the stage and not pCube1.
self.assertFalse(stage.GetPrimAtPath('/root/geo/pCube1').IsValid())
self.assertTrue(stage.GetPrimAtPath('/root/geo/pCone1').IsValid())
AdskUsdComponentCreator.ComponentAPI.SetVariantTemporarySelection(desc, [first_vs, first_vs.GetVariantDescription('pCube1')], True)
self.assertTrue(stage.GetPrimAtPath('/root/geo/pCube1').IsValid())
self.assertFalse(stage.GetPrimAtPath('/root/geo/pCone1').IsValid())
def testTargetedVariantIsDefault(self):
"""After creation the targeted variant must be the default variant."""
import AdskUsdComponentCreator
from usd_component_creator_plugin import create_multi_variants_component_from_usd_prims
proxyShape = mayaUsd_createStageWithNewLayer.createStageWithNewLayer()
input_stage = mayaUsd.ufe.getStage(proxyShape)
cubePath = Sdf.Path('/pCube1')
input_stage.DefinePrim(cubePath, 'Cube')
conePath = Sdf.Path('/pCone1')
input_stage.DefinePrim(conePath, 'Cone')
paths = [f'{proxyShape},{str(cubePath)}', f'{proxyShape},{str(conePath)}']
before = self._snapshotProxyShapes()
create_multi_variants_component_from_usd_prims(paths)
proxy = self._findNewProxyShape(before)
self.assertIsNotNone(proxy)
desc = self._getActiveDesc()
if desc is None:
stage = mayaUsd.ufe.getStage(proxy)
desc = self._getDescFromStage(stage)
self.assertIsNotNone(desc, 'Could not obtain ComponentDescription')
first_vs = self._findVariantSetByName(desc, 'variant_set_1')
default_name = first_vs.default_variant
self.assertTrue(default_name)
default_var_desc = first_vs.GetVariantDescription(default_name)
self.assertIsNotNone(default_var_desc)
self.assertTrue(
AdskUsdComponentCreator.ComponentAPI.IsVariantTarget(
desc, [first_vs, default_var_desc]),
"Default variant must be targeted after create_multi_variants_component_from_nodes")
stage = mayaUsd.ufe.getStage(proxy)
# Note: each prim is in a separate variant, so only one can be active at a time.
# The default is the alphabetically first variant, which corresponds to conePath,
# so we expect to see pCone1 in the stage and not pCube1.
self.assertFalse(stage.GetPrimAtPath('/root/geo/pCube1').IsValid())
self.assertTrue(stage.GetPrimAtPath('/root/geo/pCone1').IsValid())
AdskUsdComponentCreator.ComponentAPI.SetVariantTemporarySelection(desc, [first_vs, first_vs.GetVariantDescription('pCube1')], True)
self.assertTrue(stage.GetPrimAtPath('/root/geo/pCube1').IsValid())
self.assertFalse(stage.GetPrimAtPath('/root/geo/pCone1').IsValid())
def testDefaultIsAlphabeticallyFirst(self):
"""With three nodes the alphabetically-first name should be the default variant."""
import AdskUsdComponentCreator
from usd_component_creator_plugin import create_multi_variants_component_from_usd_prims
proxyShape = mayaUsd_createStageWithNewLayer.createStageWithNewLayer()
input_stage = mayaUsd.ufe.getStage(proxyShape)
cubePath = Sdf.Path('/MangoNode')
input_stage.DefinePrim(cubePath, 'Cube')
conePath = Sdf.Path('/AvocadoNode')
input_stage.DefinePrim(conePath, 'Cone')
kiwiPath = Sdf.Path('/KiwiNode')
input_stage.DefinePrim(kiwiPath, 'Cube')
paths = [f'{proxyShape},{str(cubePath)}', f'{proxyShape},{str(conePath)}', f'{proxyShape},{str(kiwiPath)}']
before = self._snapshotProxyShapes()
create_multi_variants_component_from_usd_prims(paths)
proxy = self._findNewProxyShape(before)
self.assertIsNotNone(proxy)
stage = mayaUsd.ufe.getStage(proxy)
desc = self._getDescFromStage(stage)
self.assertIsNotNone(desc, 'Could not construct ComponentDescription from stage')
first_vs = self._findVariantSetByName(desc, 'variant_set_1')
self.assertEqual(len(first_vs.GetVariants()), 3, "Expected three variants from three nodes")
opts = AdskUsdComponentCreator.Options()
opts.Validate()
expected_name = AdskUsdComponentCreator.GenerateVariantNameFromObjectName(
opts, 'AvocadoNode', ['AvocadoNode'], [])
self.assertEqual(first_vs.default_variant, expected_name,
"AvocadoNode is alphabetically first and must be the default variant")
stage = mayaUsd.ufe.getStage(proxy)
# Note: each prim is in a separate variant, so only one can be active at a time.
self.assertTrue(stage.GetPrimAtPath('/root/geo/AvocadoNode').IsValid())
self.assertFalse(stage.GetPrimAtPath('/root/geo/KiwiNode').IsValid())
self.assertFalse(stage.GetPrimAtPath('/root/geo/MangoNode').IsValid())
AdskUsdComponentCreator.ComponentAPI.SetVariantTemporarySelection(desc, [first_vs, first_vs.GetVariantDescription('KiwiNode')], True)
self.assertTrue(stage.GetPrimAtPath('/root/geo/KiwiNode').IsValid())
self.assertFalse(stage.GetPrimAtPath('/root/geo/AvocadoNode').IsValid())
self.assertFalse(stage.GetPrimAtPath('/root/geo/MangoNode').IsValid())
AdskUsdComponentCreator.ComponentAPI.SetVariantTemporarySelection(desc, [first_vs, first_vs.GetVariantDescription('MangoNode')], True)
self.assertTrue(stage.GetPrimAtPath('/root/geo/MangoNode').IsValid())
self.assertFalse(stage.GetPrimAtPath('/root/geo/AvocadoNode').IsValid())
self.assertFalse(stage.GetPrimAtPath('/root/geo/KiwiNode').IsValid())
def testPrimsWithSameNameFromMultiStages(self):
"""Prims with the same name from multiple stages result in one of them being renamed."""
import AdskUsdComponentCreator
from usd_component_creator_plugin import create_multi_variants_component_from_usd_prims
cubePath = Sdf.Path('/pCube1')
proxyShape1 = mayaUsd_createStageWithNewLayer.createStageWithNewLayer()
input_stage1 = mayaUsd.ufe.getStage(proxyShape1)
input_stage1.DefinePrim(cubePath, 'Cube')
proxyShape2 = mayaUsd_createStageWithNewLayer.createStageWithNewLayer()
input_stage2 = mayaUsd.ufe.getStage(proxyShape2)
input_stage2.DefinePrim(cubePath, 'Cube')
paths = [f'{proxyShape1},{str(cubePath)}', f'{proxyShape2},{str(cubePath)}']
before = self._snapshotProxyShapes()
create_multi_variants_component_from_usd_prims(paths)
proxy = self._findNewProxyShape(before)
self.assertIsNotNone(proxy)
stage = mayaUsd.ufe.getStage(proxy)
desc = self._getDescFromStage(stage)
self.assertIsNotNone(desc, 'Could not obtain ComponentDescription')
self.assertGreaterEqual(len(desc.GetVariantSets()), 1)
first_vs = self._findVariantSetByName(desc, 'variant_set_1')
self.assertEqual(len(first_vs.GetVariants()), 2, "Expected two variants from two nodes")
stage = mayaUsd.ufe.getStage(proxy)
# Prim added second should be renamed to avoid name collision.
self.assertTrue(stage.GetPrimAtPath('/root/geo/pCube1').IsValid())
AdskUsdComponentCreator.ComponentAPI.SetVariantTemporarySelection(desc, [first_vs, first_vs.GetVariantDescription('pCube11')], True)
self.assertTrue(stage.GetPrimAtPath('/root/geo/pCube1').IsValid())
if __name__ == '__main__':
fixturesUtils.runTests(globals())