def test_AuthorCollections(self): # ---------------------------------------------------------- # Test an explicitOnly collection. explicitColl = Usd.CollectionAPI.ApplyCollection(testPrim, "test:Explicit:Collection", Usd.Tokens.explicitOnly) # The collection is initially empty. self.assertTrue(explicitColl.HasNoIncludedPaths()) self.assertTrue('CollectionAPI:test:Explicit:Collection' in testPrim.GetAppliedSchemas()) self.assertTrue(testPrim.HasAPI(Usd.CollectionAPI)) self.assertTrue(testPrim.HasAPI(Usd.CollectionAPI, instanceName="test:Explicit:Collection")) self.assertTrue(not testPrim.HasAPI(Usd.CollectionAPI, instanceName="unknown")) self.assertTrue(not testPrim.HasAPI(Usd.CollectionAPI, instanceName="test")) self.assertEqual(explicitColl.GetCollectionPath(), Usd.CollectionAPI.GetNamedCollectionPath(testPrim, "test:Explicit:Collection")) explicitColl.CreateIncludesRel().AddTarget(sphere.GetPath()) self.assertFalse(explicitColl.HasNoIncludedPaths()) explicitColl.GetIncludesRel().AddTarget(cube.GetPath()) explicitColl.GetIncludesRel().AddTarget(cylinder.GetPath()) explicitColl.GetIncludesRel().AddTarget(cone.GetPath()) explicitCollMquery = explicitColl.ComputeMembershipQuery() explicitCollIncObjects = Usd.CollectionAPI.ComputeIncludedObjects( explicitCollMquery, stage) self.assertEqual(len(explicitCollIncObjects), 4) self.assertTrue(len(explicitCollMquery.GetAsPathExpansionRuleMap()) > 0) for obj in explicitCollIncObjects: self.assertTrue(explicitCollMquery.IsPathIncluded(obj.GetPath())) # Ensure that descendants of explicitly included objects aren't # included in the collection. self.assertFalse(explicitCollMquery.IsPathIncluded(hemiSphere1.GetPath())) self.assertFalse(explicitCollMquery.IsPathIncluded(hemiSphere2.GetPath())) # An explicitly included object can be explicitly excluded from the # collection. i.e. excludes is stronger than includes. explicitColl.CreateExcludesRel().AddTarget(cone.GetPath()) # We have to recompute the membership map if we add or remove # includes/excludes targets. explicitCollMquery = explicitColl.ComputeMembershipQuery() # Ensure that the cone is excluded. self.assertFalse(explicitCollMquery.IsPathIncluded(cone.GetPath())) # ---------------------------------------------------------- # Test an expandPrims collection. expandPrimsColl = Usd.CollectionAPI.ApplyCollection(testPrim, "testExpandPrimsColl", Usd.Tokens.expandPrims) expandPrimsColl.CreateIncludesRel().AddTarget(geom.GetPath()) expandPrimsCollMquery = expandPrimsColl.ComputeMembershipQuery() expandPrimCollIncObjects = Usd.CollectionAPI.ComputeIncludedObjects( expandPrimsCollMquery, stage) self.assertEqual(len(expandPrimCollIncObjects), 9) for obj in expandPrimCollIncObjects: self.assertTrue(expandPrimsCollMquery.IsPathIncluded(obj.GetPath())) # Exclude all shapes from the collection. This leaves just the instanced # box behind. expandPrimsColl.CreateExcludesRel().AddTarget(shapes.GetPath()) # Verify that there's no harm in excluding a path that isn't # included. expandPrimsColl.GetExcludesRel().AddTarget( Sdf.Path("/Collection/Materials/Plastic")) expandPrimsCollMquery = expandPrimsColl.ComputeMembershipQuery() expandPrimCollIncObjects = Usd.CollectionAPI.ComputeIncludedObjects( expandPrimsCollMquery, stage, Usd.TraverseInstanceProxies()) self.assertEqual(len(expandPrimCollIncObjects), 4) # ---------------------------------------------------------- # Test an expandPrimsAndProperties collection. expandPrimsAndPropertiesColl = Usd.CollectionAPI.ApplyCollection( testPrim, "testExpandPrimsAndPropertiesColl", Usd.Tokens.expandPrimsAndProperties) expandPrimsAndPropertiesColl.CreateIncludesRel().AddTarget( shapes.GetPath()) expandPnPCollMquery = expandPrimsAndPropertiesColl.ComputeMembershipQuery() expandPnPCollObjects = Usd.CollectionAPI.ComputeIncludedObjects( expandPnPCollMquery, stage) self.assertEqual(len(expandPnPCollObjects), 18) for obj in expandPnPCollObjects: self.assertTrue(expandPnPCollMquery.IsPathIncluded(obj.GetPath())) # ---------------------------------------------------------- # Test a collection that includes other collections. # # Create a collection that combines the explicit collection and # the expandPrimsAndProperties collection. combinedColl = Usd.CollectionAPI.ApplyCollection(testPrim, "combined", Usd.Tokens.explicitOnly) combinedColl.CreateIncludesRel().AddTarget( expandPrimsAndPropertiesColl.GetCollectionPath()) combinedColl.CreateIncludesRel().AddTarget( explicitColl.GetCollectionPath()) combinedMquery = combinedColl.ComputeMembershipQuery() combinedCollIncObjects = Usd.CollectionAPI.ComputeIncludedObjects( combinedMquery, stage) for obj in combinedCollIncObjects: self.assertTrue(combinedMquery.IsPathIncluded(obj.GetPath())) self.assertEqual(len(combinedCollIncObjects), 15) # now add the collection "expandPrimsColl", which includes "Geom" and # exludes "Shapes", but is weaker than the "expandPrimsAndProperties" # collection. combinedColl.CreateIncludesRel().AddTarget( expandPrimsColl.GetCollectionPath(), position=Usd.ListPositionBackOfAppendList) combinedMquery = combinedColl.ComputeMembershipQuery() combinedCollIncObjects = Usd.CollectionAPI.ComputeIncludedObjects( combinedMquery, stage) for obj in combinedCollIncObjects: self.assertTrue(combinedMquery.IsPathIncluded(obj.GetPath())) self.assertEqual(len(combinedCollIncObjects), 5) expandPrimsColl.ResetCollection() self.assertTrue(expandPrimsColl.HasNoIncludedPaths()) explicitColl.BlockCollection() self.assertTrue(explicitColl.HasNoIncludedPaths())
def test_testReadCollection(self): leafGeom = Usd.CollectionAPI(testPrim, "leafGeom") (valid, reason) = leafGeom.Validate() self.assertTrue(valid) # Test the other overload of GetCollection. leafGeomPath = leafGeom.GetCollectionPath() leafGeom = Usd.CollectionAPI.Get(stage, leafGeomPath) self.assertEqual(leafGeom.GetCollectionPath(), leafGeomPath) (valid, reason) = leafGeom.Validate() self.assertTrue(valid) # Test GetName() API. self.assertEqual(leafGeom.GetName(), 'leafGeom') self.assertFalse(Usd.CollectionAPI.CanContainPropertyName( leafGeom.GetName())) self.assertTrue(leafGeom.GetCollectionPath().name) # Test Get/IsCollectionAPIPath API. self.assertTrue(Usd.CollectionAPI.IsCollectionAPIPath( leafGeom.GetCollectionPath())) # Ensure that paths of collection schema properties aren't valid # collection paths. self.assertFalse(Usd.CollectionAPI.IsCollectionAPIPath( leafGeom.GetExpansionRuleAttr().GetPath())) self.assertFalse(Usd.CollectionAPI.IsCollectionAPIPath( leafGeom.GetIncludesRel().GetPath())) leafGeomMquery = leafGeom.ComputeMembershipQuery() self.assertEqual(leafGeomMquery.GetIncludedCollections(), []) self.assertEqual( len(Usd.CollectionAPI.ComputeIncludedObjects(leafGeomMquery, stage)), 2) # Calling Apply on an already existing collection will not update # the expansionRule. self.assertEqual(leafGeom.GetExpansionRuleAttr().Get(), Usd.Tokens.explicitOnly) self.assertTrue(Usd.CollectionAPI.CanApply(testPrim, "leafGeom")) leafGeom = Usd.CollectionAPI.Apply(testPrim, "leafGeom") self.assertEqual(leafGeom.GetExpansionRuleAttr().Get(), Usd.Tokens.explicitOnly) allGeom = Usd.CollectionAPI(testPrim, "allGeom") (valid, reason) = allGeom.Validate() allGeomMquery = allGeom.ComputeMembershipQuery() self.assertEqual(allGeomMquery.GetIncludedCollections(), []) self.assertEqual(len(Usd.CollectionAPI.ComputeIncludedObjects( allGeomMquery,stage)), 9) # included object count increases when we count instance proxies. self.assertEqual(len(Usd.CollectionAPI.ComputeIncludedObjects( allGeomMquery,stage, predicate=Usd.TraverseInstanceProxies())), 11) allGeomProperties = Usd.CollectionAPI(testPrim, "allGeomProperties") (valid, reason) = allGeomProperties.Validate() allGeomPropertiesMquery = allGeomProperties.ComputeMembershipQuery() self.assertEqual(allGeomPropertiesMquery.GetIncludedCollections(), []) self.assertEqual(len(Usd.CollectionAPI.ComputeIncludedObjects( allGeomPropertiesMquery, stage)), 29) hasRels = Usd.CollectionAPI(testPrim, "hasRelationships") (valid, reason) = hasRels.Validate() self.assertTrue(valid) hasRelsMquery = hasRels.ComputeMembershipQuery() self.assertEqual(hasRelsMquery.GetIncludedCollections(), []) incObjects = Usd.CollectionAPI.ComputeIncludedObjects(hasRelsMquery, stage) for obj in incObjects: self.assertTrue(isinstance(obj, Usd.Property)) hasInstanceProxy = Usd.CollectionAPI(testPrim, "hasInstanceProxy") (valid, reason) = hasInstanceProxy.Validate() self.assertTrue(valid) hasInstanceProxyMquery = hasInstanceProxy.ComputeMembershipQuery() self.assertEqual(hasInstanceProxyMquery.GetIncludedCollections(), []) incObjects = Usd.CollectionAPI.ComputeIncludedObjects( hasInstanceProxyMquery, stage) self.assertEqual(len(incObjects), 2) for obj in incObjects: self.assertTrue(obj.IsInstanceProxy()) self.assertFalse(obj.IsInPrototype()) coneProperties = Usd.CollectionAPI(testPrim, "coneProperties") (valid, reason) = coneProperties.Validate() self.assertTrue(valid) conePropertiesMquery = coneProperties.ComputeMembershipQuery() self.assertEqual(conePropertiesMquery.GetIncludedCollections(), []) incObjects = Usd.CollectionAPI.ComputeIncludedObjects( conePropertiesMquery, stage) self.assertEqual(len(incObjects), 2) for obj in incObjects: self.assertTrue(isinstance(obj, Usd.Property)) includesCollection = Usd.CollectionAPI(testPrim, "includesCollection") (valid, reason) = includesCollection.Validate() self.assertTrue(valid) includesCollectionMquery = includesCollection.ComputeMembershipQuery() self.assertEqual( set(includesCollectionMquery.GetIncludedCollections()), set([Sdf.Path("/CollectionTest/Geom/Shapes.collection:allShapes")])) incObjects = Usd.CollectionAPI.ComputeIncludedObjects( includesCollectionMquery, stage) self.assertTrue(hemiSphere2 in incObjects) self.assertTrue(hemiSphere1 not in incObjects) includesNestedCollection = Usd.CollectionAPI( testPrim, "includesNestedCollection") (valid, reason) = includesNestedCollection.Validate() self.assertTrue(valid) includesNestedCollectionMquery = \ includesNestedCollection.ComputeMembershipQuery() self.assertEqual( set(includesNestedCollectionMquery.GetIncludedCollections()), set([Sdf.Path("/CollectionTest/Geom/Shapes.collection:allShapes"), Sdf.Path("/CollectionTest/Geom.collection:allGeom")])) excludeInstanceGeom = Usd.CollectionAPI(testPrim, "excludeInstanceGeom") (valid, reason) = excludeInstanceGeom.Validate() self.assertTrue(valid) excludeInstanceGeomMquery = excludeInstanceGeom.ComputeMembershipQuery() self.assertEqual(excludeInstanceGeomMquery.GetIncludedCollections(), []) incObjects = Usd.CollectionAPI.ComputeIncludedObjects( excludeInstanceGeomMquery, stage) self.assertEqual(len(incObjects), 1) allIncObjects = Usd.CollectionAPI.ComputeIncludedObjects( excludeInstanceGeomMquery, stage, predicate=Usd.TraverseInstanceProxies()) self.assertEqual(len(allIncObjects), 2)
def BuildTree(self, startPrim): startItem = self._primPathToItemMap.get(startPrim.GetPath()) if not startItem: return # Add traversal of instances to predicate predicate = Usd.TraverseInstanceProxies(Usd.PrimIsActive & Usd.PrimIsDefined & ~Usd.PrimIsAbstract) childCount = len(startPrim.GetFilteredChildren(predicate)) first = startItem.childCount() last = first + childCount - 1 self.beginInsertRows(self.indexFromItem(startItem), first, last) prims = list(Usd.PrimRange(startPrim, predicate)) # Iterate through prims, but skip the startPrim for prim in prims[1:]: if not self.IsPrimBoundable(prim, predicate): continue parentPrim = prim.GetParent() parentItem = self._primPathToItemMap.get(parentPrim.GetPath()) if parentItem: primName = prim.GetName() primTypeName = prim.GetTypeName() primPath = prim.GetPath() hasUnloadedPayload = prim.HasAuthoredPayloads() # Use parentItem's import state to determine its child's # import state. (Note it is intentional that the parentItem's # data is tested to be equal to Checked or PartiallyChecked, # instead of just testing that it's *not* equal to Unchecked. # This is because when parentItem is the top-most root item, # its data is a header string instead of a CheckState). importState = Qt.Unchecked if parentItem.data(COL_IMPORT) == Qt.Checked or\ parentItem.data(COL_IMPORT) == Qt.PartiallyChecked: importState = Qt.PartiallyChecked # Retrieve variants from the prim. variants = [] variantSets = prim.GetVariantSets() for name in variantSets.GetNames(): variantSet = variantSets.GetVariantSet(name) variants.append(VariantInfo(\ name = name,\ choices = variantSet.GetVariantNames(),\ initialSelection = variantSet.GetVariantSelection(),\ enabled = False)) data = [primName, importState, primTypeName, variants] childItem = TreeItem(data, parentItem, primPath, hasUnloadedPayload) self._primPathToItemMap[primPath] = childItem self.endInsertRows()
def test_PrimRange(self): s = Usd.Stage.Open('nested/root.usda') # Test iterating over all prims on the stage expectedPrims = [ s.GetPrimAtPath(p) for p in [ '/World', '/World/sets', '/World/sets/Set_1', '/World/sets/Set_1/Prop_1', '/World/sets/Set_1/Prop_1/geom', '/World/sets/Set_1/Prop_1/anim', '/World/sets/Set_1/Prop_2', '/World/sets/Set_1/Prop_2/geom', '/World/sets/Set_1/Prop_2/anim', '/World/sets/Set_2', '/World/sets/Set_2/Prop_1', '/World/sets/Set_2/Prop_1/geom', '/World/sets/Set_2/Prop_1/anim', '/World/sets/Set_2/Prop_2', '/World/sets/Set_2/Prop_2/geom', '/World/sets/Set_2/Prop_2/anim' ] ] gotPrims = list(Usd.PrimRange.Stage(s, Usd.TraverseInstanceProxies())) self.assertEqual(expectedPrims, gotPrims) # Test getting instance proxy descendants from each instance. def _ValidateInstanceDescendants(parentPath, expectedDescendantPaths): parent = s.GetPrimAtPath(parentPath) descendants = \ list(Usd.PrimRange(parent, Usd.TraverseInstanceProxies())) expectedDescendants = \ [s.GetPrimAtPath(p) for p in expectedDescendantPaths] self.assertEqual(expectedDescendants, descendants) _ValidateInstanceDescendants( parentPath='/World/sets/Set_1', expectedDescendantPaths=[ '/World/sets/Set_1', '/World/sets/Set_1/Prop_1', '/World/sets/Set_1/Prop_1/geom', '/World/sets/Set_1/Prop_1/anim', '/World/sets/Set_1/Prop_2', '/World/sets/Set_1/Prop_2/geom', '/World/sets/Set_1/Prop_2/anim' ]) _ValidateInstanceDescendants(parentPath='/World/sets/Set_1/Prop_1', expectedDescendantPaths=[ '/World/sets/Set_1/Prop_1', '/World/sets/Set_1/Prop_1/geom', '/World/sets/Set_1/Prop_1/anim' ]) _ValidateInstanceDescendants(parentPath='/World/sets/Set_1/Prop_2', expectedDescendantPaths=[ '/World/sets/Set_1/Prop_2', '/World/sets/Set_1/Prop_2/geom', '/World/sets/Set_1/Prop_2/anim' ]) _ValidateInstanceDescendants( parentPath='/World/sets/Set_2', expectedDescendantPaths=[ '/World/sets/Set_2', '/World/sets/Set_2/Prop_1', '/World/sets/Set_2/Prop_1/geom', '/World/sets/Set_2/Prop_1/anim', '/World/sets/Set_2/Prop_2', '/World/sets/Set_2/Prop_2/geom', '/World/sets/Set_2/Prop_2/anim' ]) _ValidateInstanceDescendants(parentPath='/World/sets/Set_2/Prop_1', expectedDescendantPaths=[ '/World/sets/Set_2/Prop_1', '/World/sets/Set_2/Prop_1/geom', '/World/sets/Set_2/Prop_1/anim' ]) _ValidateInstanceDescendants(parentPath='/World/sets/Set_2/Prop_2', expectedDescendantPaths=[ '/World/sets/Set_2/Prop_2', '/World/sets/Set_2/Prop_2/geom', '/World/sets/Set_2/Prop_2/anim' ]) # Test iterating starting from a master prim master = s.GetPrimAtPath('/World/sets/Set_1').GetMaster() _ValidateInstanceDescendants( parentPath=master.GetPath(), expectedDescendantPaths=[ master.GetPath(), master.GetPath().AppendPath('Prop_1'), master.GetPath().AppendPath('Prop_1/geom'), master.GetPath().AppendPath('Prop_1/anim'), master.GetPath().AppendPath('Prop_2'), master.GetPath().AppendPath('Prop_2/geom'), master.GetPath().AppendPath('Prop_2/anim') ]) master = s.GetPrimAtPath('/World/sets/Set_1/Prop_1').GetMaster() _ValidateInstanceDescendants(parentPath=master.GetPath(), expectedDescendantPaths=[ master.GetPath(), master.GetPath().AppendPath('geom'), master.GetPath().AppendPath('anim') ])
def test_CreateCollections(self): carPaths = [ Sdf.Path('/World/City_set/Vehicles_grp/Cars_grp/CarA'), Sdf.Path('/World/City_set/Vehicles_grp/Cars_grp/CarB'), Sdf.Path('/World/City_set/Vehicles_grp/Cars_grp/CarC'), ] bikePaths = [ Sdf.Path('/World/City_set/Vehicles_grp/Bikes_grp/BikeA'), Sdf.Path('/World/City_set/Vehicles_grp/Bikes_grp/BikeB'), Sdf.Path('/World/City_set/Vehicles_grp/Bikes_grp/BikeC'), Sdf.Path('/World/City_set/Vehicles_grp/Bikes_grp/BikeD') ] otherVehiclePaths = [ Sdf.Path('/World/City_set/Misc_grp/TruckA'), Sdf.Path('/World/City_set/Misc_grp/BicycleA') ] stage = Usd.Stage.Open("collections.usda") self.assertTrue(stage) city_set = stage.GetPrimAtPath("/World/City_set") self.assertTrue(city_set) vehicles_grp = stage.GetPrimAtPath("/World/City_set/Vehicles_grp") self.assertTrue(vehicles_grp) assignments = [('vehicles:cars', carPaths), ('vehicles:bikes', bikePaths), ('vehicles:other', otherVehiclePaths)] collections = UsdUtils.CreateCollections(assignments, vehicles_grp) # Verify that the collections that were created have the appropriate # sizes. for collection in collections: query = collection.ComputeMembershipQuery() includedObjects = Usd.CollectionAPI.ComputeIncludedObjects( query, stage) includes = collection.GetIncludesRel().GetTargets() excludes = collection.GetExcludesRel().GetTargets() if collection.GetName() == "vehicles:cars": # Ensure that the common ancestor is included. self.assertEqual( includes, [Sdf.Path("/World/City_set/Vehicles_grp/Cars_grp")]) self.assertEqual(excludes, []) self.assertEqual(len(includedObjects), 7) elif collection.GetName() == "vehicles:bikes": self.assertEqual( includes, [Sdf.Path("/World/City_set/Vehicles_grp/Bikes_grp")]) self.assertEqual(excludes, []) self.assertEqual(len(includedObjects), 9) elif collection.GetName() == "vehicles:other": self.assertEqual(set(includes), set(otherVehiclePaths)) self.assertEqual(len(includedObjects), 4) carPaths.append(Sdf.Path('/World/City_set/Misc_grp/CarD')) bikePaths.extend([ Sdf.Path('/World/City_set/Misc_grp/BikeE'), Sdf.Path('/World/City_set/Misc_grp/BikeF') ]) # Test case with overlapping paths and collections that # have excludes. newAssignments = [('vehicles:cars', carPaths), ('vehicles:bikes', bikePaths), ('vehicles:carsOrBikes', carPaths + bikePaths), ('vehicles:others', otherVehiclePaths)] newCollections = UsdUtils.CreateCollections(newAssignments, city_set) vehicleCarsIncludesAndExcludes = \ UsdUtils.ComputeCollectionIncludesAndExcludes(carPaths, stage) vehicleBikesIncludesAndExcludes = \ UsdUtils.ComputeCollectionIncludesAndExcludes(bikePaths, stage) vehicleCarsOrBikesIncludesAndExcludes = \ UsdUtils.ComputeCollectionIncludesAndExcludes( carPaths + bikePaths, stage) vehicleOthersIncludesAndExcludes = \ UsdUtils.ComputeCollectionIncludesAndExcludes(otherVehiclePaths, stage) # Verify that the collections that were created have the appropriate # sizes. for collection in newCollections: query = collection.ComputeMembershipQuery() includedObjects = Usd.CollectionAPI.ComputeIncludedObjects( query, stage) includes = collection.GetIncludesRel().GetTargets() excludes = collection.GetExcludesRel().GetTargets() if collection.GetName() == "vehicles:cars": self.assertEqual(vehicleCarsIncludesAndExcludes[0], includes) self.assertEqual(vehicleCarsIncludesAndExcludes[1], excludes) self.assertTrue( Sdf.Path("/World/City_set/Vehicles_grp") in includes) self.assertTrue( Sdf.Path("/World/City_set/Vehicles_grp/Bikes_grp") in excludes) self.assertEqual(len(includedObjects), 10) elif collection.GetName() == "vehicles:bikes": self.assertEqual(vehicleBikesIncludesAndExcludes[0], includes) self.assertEqual(vehicleBikesIncludesAndExcludes[1], excludes) self.assertTrue( Sdf.Path("/World/City_set/Vehicles_grp") in includes) self.assertTrue( Sdf.Path("/World/City_set/Misc_grp") not in includes) self.assertTrue( Sdf.Path("/World/City_set/Vehicles_grp/Cars_grp") in excludes) self.assertEqual(len(includedObjects), 14) elif collection.GetName() == "vehicles:carsOrBikes": self.assertEqual(vehicleCarsOrBikesIncludesAndExcludes[0], includes) self.assertEqual(vehicleCarsOrBikesIncludesAndExcludes[1], excludes) self.assertTrue(Sdf.Path("/World/City_set") in includes) self.assertTrue( Sdf.Path("/World/City_set/Misc_grp/BicycleA") in excludes) self.assertTrue( Sdf.Path("/World/City_set/Misc_grp/TruckA") in excludes) elif collection.GetName() == "vehicles:other": self.assertEqual(vehicleOthersIncludesAndExcludes[0], includes) self.assertEqual(vehicleOthersIncludesAndExcludes[1], excludes) self.assertEqual(len(includedObjects), 4) furniturePaths = [ Sdf.Path('/World/Room_set/Table_grp/Table'), Sdf.Path('/World/Room_set/Chairs_grp/ChairA'), Sdf.Path('/World/Room_set/Chairs_grp/ChairB') ] penOrPencilPaths = [ Sdf.Path('/World/Room_set/Table_grp/Pencils_grp/PencilA'), Sdf.Path('/World/Room_set/Table_grp/Pencils_grp/PencilB'), Sdf.Path('/World/Room_set/Table_grp/Pencils_grp/PencilC'), Sdf.Path('/World/Room_set/Table_grp/Pens_grp/PenA'), Sdf.Path('/World/Room_set/Table_grp/Pens_grp/PenB') ] assignments = [('furniture', furniturePaths), ('pensOrPencils', penOrPencilPaths), ('emptyCollection', [])] rootCollections = UsdUtils.CreateCollections( assignments, stage.GetPrimAtPath("/World")) # 'emptyCollection' is still created but has no includes or excludes. self.assertEqual(len(rootCollections), 3) for collection in rootCollections: query = collection.ComputeMembershipQuery() includedObjects = Usd.CollectionAPI.ComputeIncludedObjects( query, stage) includes = collection.GetIncludesRel().GetTargets() excludes = collection.GetExcludesRel().GetTargets() if collection.GetName() == 'pensOrPencils': self.assertTrue( Sdf.Path('/World/Room_set/Table_grp/Pencils_grp') in includes) self.assertTrue( Sdf.Path('/World/Room_set/Table_grp/Pens_grp') in includes) self.assertTrue( Sdf.Path('/World/Room_set/Table_grp/Pencils_grp/EraserA') in excludes) elif collection.GetName() == "furniture": self.assertTrue( Sdf.Path('/World/Room_set/Chairs_grp') in includes) self.assertTrue( Sdf.Path('/World/Room_set/Table_grp/Table') in includes) self.assertEqual(len(excludes), 0) elif collection.GetName() == "emptyCollection": self.assertEqual(len(includes), 0) self.assertEqual(len(excludes), 0) # Test creation of collections with instance paths. lampBasePaths = [ Sdf.Path('/World/Room_set/Table_grp/LampA/Geom/Base'), Sdf.Path('/World/Room_set/Table_grp/LampB/Geom/Base'), Sdf.Path('/World/Room_set/Table_grp/LampC/Geom/Base') ] lampShadePaths = [ Sdf.Path('/World/Room_set/Table_grp/LampA/Geom/Shade'), Sdf.Path('/World/Room_set/Table_grp/LampB/Geom/Shade'), Sdf.Path('/World/Room_set/Table_grp/LampC/Geom/Shade') ] assignments = [('lampBases', lampBasePaths), ('lampShades', lampShadePaths)] lampCollections = UsdUtils.CreateCollections( assignments, stage.GetPrimAtPath("/World/Room_set")) for collection in lampCollections: query = collection.ComputeMembershipQuery() includedPaths = Usd.CollectionAPI.ComputeIncludedPaths( query, stage, Usd.TraverseInstanceProxies()) if collection.GetName() == 'lampBases': for p in lampBasePaths: self.assertTrue(p in includedPaths) elif collection.GetName() == "lampShades": for p in lampShadePaths: self.assertTrue(p in includedPaths) lampAPaths = [ Sdf.Path('/World/Room_set/Table_grp/LampA/Geom/Base'), Sdf.Path('/World/Room_set/Table_grp/LampA/Geom/Shade') ] lampBPaths = [ Sdf.Path('/World/Room_set/Table_grp/LampB/Geom/Base'), Sdf.Path('/World/Room_set/Table_grp/LampB/Geom/Shade') ] lampCPaths = [ Sdf.Path('/World/Room_set/Table_grp/LampC/Geom/Base'), Sdf.Path('/World/Room_set/Table_grp/LampC/Geom/Shade') ] assignments = [('lampA', lampAPaths), ('lampB', lampBPaths), ('lampC', lampCPaths)] lampCollections = UsdUtils.CreateCollections( assignments, stage.GetPrimAtPath("/World/Room_set/Table_grp"), minIncludeExcludeCollectionSize=2) for collection in lampCollections: query = collection.ComputeMembershipQuery() includedPaths = Usd.CollectionAPI.ComputeIncludedPaths( query, stage, Usd.TraverseInstanceProxies()) if collection.GetName() == 'lampA': for p in lampAPaths: self.assertTrue(p in includedPaths) elif collection.GetName() == "lampB": for p in lampBPaths: self.assertTrue(p in includedPaths) elif collection.GetName() == "lampC": for p in lampCPaths: self.assertTrue(p in includedPaths)
def test_InstancedSkeletonBinding(self): """Tests for correctness in the interpretation of the inherited skel:skeleton binding with instancing.""" testFile = "populate.usda" stage = Usd.Stage.Open(testFile) cache = UsdSkel.Cache() root = UsdSkel.Root(stage.GetPrimAtPath("/SkelBinding")) self.assertTrue(cache.Populate(root, Usd.TraverseInstanceProxies())) skel1 = UsdSkel.Skeleton.Get(stage, "/Skel1") binding1 = cache.ComputeSkelBinding(root, skel1, Usd.TraverseInstanceProxies()) self.assertEqual(binding1.GetSkeleton().GetPrim(), skel1.GetPrim()) self.assertEqual(len(binding1.GetSkinningTargets()), 2) skinningQuery1 = binding1.GetSkinningTargets()[1] self.assertEqual(skinningQuery1.GetPrim().GetPath(), Sdf.Path("/SkelBinding/Instance/Inherit")) # Inherited skinning properties. self.assertEqual( skinningQuery1.GetJointIndicesPrimvar().GetAttr().GetPath(). GetPrimPath(), Sdf.Path("/SkelBinding/Instance")) self.assertEqual( skinningQuery1.GetJointWeightsPrimvar().GetAttr().GetPath(). GetPrimPath(), Sdf.Path("/SkelBinding/Instance")) self.assertEqual(skinningQuery1.GetJointOrder(), Vt.TokenArray(["instance"])) # Non-inherited skinning properties. self.assertFalse(skinningQuery1.GetBlendShapesAttr()) self.assertFalse(skinningQuery1.GetBlendShapeTargetsRel()) allBindings = cache.ComputeSkelBindings(root, Usd.TraverseInstanceProxies()) # Expecting three resolved bindings. This should *not* include bindings # for any inactive skels, but does include instances self.assertEqual(len(allBindings), 3) skel2 = UsdSkel.Skeleton.Get(stage, "/SkelBinding/Instance/Skel") binding2 = cache.ComputeSkelBinding(root, skel2, Usd.TraverseInstanceProxies()) self.assertEqual(binding2.GetSkeleton().GetPrim(), allBindings[2].GetSkeleton().GetPrim()) self.assertEqual( [t.GetPrim() for t in binding2.GetSkinningTargets()], [t.GetPrim() for t in allBindings[2].GetSkinningTargets()]) inheritBindingMesh = stage.GetPrimAtPath( "/SkelBinding/Instance/Inherit") overrideBindingMesh = stage.GetPrimAtPath( "/SkelBinding/Instance/Override") # Instances should not be discoverable with a default predicate. cache = UsdSkel.Cache() cache.Populate(root, Usd.PrimDefaultPredicate) self.assertFalse(cache.GetSkinningQuery(inheritBindingMesh)) self.assertFalse(cache.GetSkinningQuery(overrideBindingMesh)) # Need to explicitly traverse instance prototypes to see these bindings. cache.Populate(root, Usd.TraverseInstanceProxies()) query = cache.GetSkinningQuery(inheritBindingMesh) self.assertTrue(query) self.assertEqual(list(query.GetJointOrder()), ["instance"]) query = cache.GetSkinningQuery(overrideBindingMesh) self.assertTrue(query) self.assertEqual(list(query.GetJointOrder()), ["override"]) self.assertEqual(list(query.GetBlendShapeOrder()), ["override"])
def test_testReadCollection(self): leafGeom = Usd.CollectionAPI.GetCollection(testPrim, "leafGeom") (valid, reason) = leafGeom.Validate() self.assertTrue(valid) # Test GetName() API. self.assertEqual(leafGeom.GetName(), 'leafGeom') # Test Get/IsCollectionPath API. self.assertTrue( Usd.CollectionAPI.IsCollectionPath(leafGeom.GetCollectionPath())) # Ensure that paths of collection schema properties aren't valid # collection paths. self.assertFalse( Usd.CollectionAPI.IsCollectionPath( leafGeom.GetExpansionRuleAttr().GetPath())) self.assertFalse( Usd.CollectionAPI.IsCollectionPath( leafGeom.GetIncludesRel().GetPath())) leafGeomMquery = leafGeom.ComputeMembershipQuery() self.assertEqual( len(Usd.CollectionAPI.ComputeIncludedObjects( leafGeomMquery, stage)), 2) # Calling AddCollection on an already existing collection will update # the expansionRule. self.assertEqual(leafGeom.GetExpansionRuleAttr().Get(), Usd.Tokens.explicitOnly) leafGeom = Usd.CollectionAPI.AddCollection(testPrim, "leafGrom", Usd.Tokens.expandPrims) self.assertEqual(leafGeom.GetExpansionRuleAttr().Get(), Usd.Tokens.expandPrims) allGeom = Usd.CollectionAPI.GetCollection(testPrim, "allGeom") (valid, reason) = allGeom.Validate() allGeomMquery = allGeom.ComputeMembershipQuery() self.assertEqual( len(Usd.CollectionAPI.ComputeIncludedObjects(allGeomMquery, stage)), 9) # included object count increases when we count instance proxies. self.assertEqual( len( Usd.CollectionAPI.ComputeIncludedObjects( allGeomMquery, stage, predicate=Usd.TraverseInstanceProxies())), 11) allGeomProperties = Usd.CollectionAPI.GetCollection( testPrim, "allGeomProperties") (valid, reason) = allGeomProperties.Validate() allGeomPropertiesMquery = allGeomProperties.ComputeMembershipQuery() self.assertEqual( len( Usd.CollectionAPI.ComputeIncludedObjects( allGeomPropertiesMquery, stage)), 24) hasRels = Usd.CollectionAPI.GetCollection(testPrim, "hasRelationships") (valid, reason) = hasRels.Validate() self.assertTrue(valid) hasRelsMquery = hasRels.ComputeMembershipQuery() incObjects = Usd.CollectionAPI.ComputeIncludedObjects( hasRelsMquery, stage) for obj in incObjects: self.assertTrue(isinstance(obj, Usd.Property)) hasInstanceProxy = Usd.CollectionAPI.GetCollection( testPrim, "hasInstanceProxy") (valid, reason) = hasInstanceProxy.Validate() self.assertTrue(valid) hasInstanceProxyMquery = hasInstanceProxy.ComputeMembershipQuery() incObjects = Usd.CollectionAPI.ComputeIncludedObjects( hasInstanceProxyMquery, stage) self.assertEqual(len(incObjects), 2) for obj in incObjects: self.assertTrue(obj.IsInstanceProxy()) self.assertFalse(obj.IsInMaster()) coneProperties = Usd.CollectionAPI.GetCollection( testPrim, "coneProperties") (valid, reason) = coneProperties.Validate() self.assertTrue(valid) conePropertiesMquery = coneProperties.ComputeMembershipQuery() incObjects = Usd.CollectionAPI.ComputeIncludedObjects( conePropertiesMquery, stage) self.assertEqual(len(incObjects), 2) for obj in incObjects: self.assertTrue(isinstance(obj, Usd.Property)) includesCollection = Usd.CollectionAPI.GetCollection( testPrim, "includesCollection") (valid, reason) = includesCollection.Validate() self.assertTrue(valid) includesCollectionMquery = includesCollection.ComputeMembershipQuery() incObjects = Usd.CollectionAPI.ComputeIncludedObjects( includesCollectionMquery, stage) self.assertTrue(hemiSphere2 in incObjects) self.assertTrue(hemiSphere1 not in incObjects) excludeInstanceGeom = Usd.CollectionAPI.GetCollection( testPrim, "excludeInstanceGeom") (valid, reason) = excludeInstanceGeom.Validate() self.assertTrue(valid) excludeInstanceGeomMquery = excludeInstanceGeom.ComputeMembershipQuery( ) incObjects = Usd.CollectionAPI.ComputeIncludedObjects( excludeInstanceGeomMquery, stage) self.assertEqual(len(incObjects), 1) allIncObjects = Usd.CollectionAPI.ComputeIncludedObjects( excludeInstanceGeomMquery, stage, predicate=Usd.TraverseInstanceProxies()) self.assertEqual(len(allIncObjects), 2)