Esempio n. 1
0
    def test_invalidCollections(self):
        invalidCollectionNames = [
            "invalidExpansionRule", "invalidExcludesExplicitOnly",
            "invalidExcludesExpandPrims", "invalidTopLevelRules"
        ]

        for collName in invalidCollectionNames:
            coll = Usd.CollectionAPI(testPrim, collName)
            (valid, reason) = coll.Validate()
            self.assertFalse(valid)
            self.assertTrue(len(reason) > 0)
Esempio n. 2
0
    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)
        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)
Esempio n. 3
0
    def test_AppliedSchemas(self):
        self.assertTrue(Usd.ModelAPI().IsAPISchema())
        self.assertTrue(Usd.ClipsAPI().IsAPISchema())
        self.assertTrue(Usd.CollectionAPI().IsAPISchema())

        self.assertFalse(Usd.ModelAPI().IsAppliedAPISchema())
        self.assertFalse(Usd.ClipsAPI().IsAppliedAPISchema())
        self.assertTrue(Usd.CollectionAPI().IsAppliedAPISchema())

        self.assertTrue(Usd.CollectionAPI().IsMultipleApplyAPISchema())

        self.assertTrue(Usd.CollectionAPI().GetSchemaType() ==
                        Usd.SchemaType.MultipleApplyAPI)
        self.assertTrue(Usd.CollectionAPI().GetSchemaType() !=
                        Usd.SchemaType.SingleApplyAPI)
        self.assertTrue(
            Usd.ModelAPI().GetSchemaType() == Usd.SchemaType.NonAppliedAPI)
        self.assertTrue(
            Usd.ClipsAPI().GetSchemaType() == Usd.SchemaType.NonAppliedAPI)

        # Verify that we an exception but don't crash when applying to the
        # null prim.
        with self.assertRaises(Tf.ErrorException):
            self.assertFalse(Usd.CollectionAPI.Apply(Usd.Prim(), "root"))

        for fmt in allFormats:
            sessionLayer = Sdf.Layer.CreateNew("SessionLayer.%s" % fmt)
            s = Usd.Stage.CreateInMemory('AppliedSchemas.%s' % fmt,
                                         sessionLayer)

            s.SetEditTarget(Usd.EditTarget(s.GetRootLayer()))

            world = s.OverridePrim('/world')
            self.assertEqual([], world.GetAppliedSchemas())

            rootCollAPI = Usd.CollectionAPI.Apply(world, "root")
            self.assertTrue(rootCollAPI)

            world = rootCollAPI.GetPrim()
            self.assertTrue(world)

            self.assertTrue(world.HasAPI(Usd.CollectionAPI))

            # The schemaType that's passed into HasAPI must derive from
            # UsdAPISchemaBase and must not be UsdAPISchemaBase.
            with self.assertRaises(RuntimeError):
                world.HasAPI(Usd.Typed)
            with self.assertRaises(RuntimeError):
                world.HasAPI(Usd.APISchemaBase)
            with self.assertRaises(RuntimeError):
                world.HasAPI(Usd.ModelAPI)

            # Try calling HasAPI a random TfType that isn't a derivative of
            # SchemaBase.
            with self.assertRaises(RuntimeError):
                world.HasAPI(Sdf.ListOpType)

            self.assertEqual(['CollectionAPI:root'], world.GetAppliedSchemas())

            # Switch the edit target to the session layer and test bug 156929
            s.SetEditTarget(Usd.EditTarget(s.GetSessionLayer()))
            sessionCollAPI = Usd.CollectionAPI.Apply(world, "session")
            self.assertTrue(sessionCollAPI)
            self.assertEqual(['CollectionAPI:session', 'CollectionAPI:root'],
                             world.GetAppliedSchemas())

            self.assertTrue(world.HasAPI(Usd.CollectionAPI))

            # Ensure duplicates aren't picked up
            anotherSessionCollAPI = Usd.CollectionAPI.Apply(world, "session")
            self.assertTrue(anotherSessionCollAPI)
            self.assertEqual(['CollectionAPI:session', 'CollectionAPI:root'],
                             world.GetAppliedSchemas())

            # Add a duplicate in the root layer and ensure that there are no
            # duplicates in the composed result.
            s.SetEditTarget(Usd.EditTarget(s.GetRootLayer()))
            rootLayerSessionCollAPI = Usd.CollectionAPI.Apply(world, "session")
            self.assertTrue(rootLayerSessionCollAPI)
            self.assertEqual(['CollectionAPI:session', 'CollectionAPI:root'],
                             world.GetAppliedSchemas())