def test_GetUnionedTimeSamples(self): s = Usd.Stage.CreateInMemory() foo = s.DefinePrim('/foo') attr1 = foo.CreateAttribute('attr1', Sdf.ValueTypeNames.Bool) self.assertEqual([], attr1.GetTimeSamples()) attr1.Set(True, 1.0) attr1.Set(False, 3.0) attr2 = foo.CreateAttribute('attr2', Sdf.ValueTypeNames.Float) attr2.Set(100.0, 2.0) attr2.Set(200.0, 4.0) self.assertEqual(Usd.Attribute.GetUnionedTimeSamples([attr1, attr2]), [1.0, 2.0, 3.0, 4.0]) self.assertEqual( Usd.Attribute.GetUnionedTimeSamplesInInterval([attr1, attr2], Gf.Interval( 1.5, 3.5)), [2.0, 3.0]) attrQueries = [Usd.AttributeQuery(attr1), Usd.AttributeQuery(attr2)] self.assertEqual(Usd.AttributeQuery.GetUnionedTimeSamples(attrQueries), [1.0, 2.0, 3.0, 4.0]) self.assertEqual( Usd.AttributeQuery.GetUnionedTimeSamplesInInterval( attrQueries, Gf.Interval(1.5, 3.5)), [2.0, 3.0])
def _Check(assertFn, attr, expected, time=None, query=True): if time is not None: assertFn(attr.Get(time), expected) if query: assertFn(Usd.AttributeQuery(attr).Get(time), expected) else: assertFn(attr.Get(), expected) if query: assertFn(Usd.AttributeQuery(attr).Get(), expected)
def main(): print("Simple Stage:") stage = _create_basic_scene() sphere = UsdGeom.Sphere(stage.GetPrimAtPath("/Some/Prim")) radius = sphere.GetRadiusAttr() print("Testing Get(), normally") timeit(radius.Get, REPEATS) query = Usd.AttributeQuery(radius) print(query.Get()) radius.Set(100) print(query.Get()) print("Testing Get(), using UsdAttributeQuery") timeit(query.Get, REPEATS) print() print("Testing GetTimeSamples(), normally") timeit(radius.GetTimeSamples, REPEATS) function = functools.partial(query.GetUnionedTimeSamples, [query]) function.__name__ = "GetUnionedTimeSamples" print("Testing GetTimeSamples(), using a union") timeit(function, REPEATS) print() visibility = sphere.GetVisibilityAttr() function = functools.partial(_get_time_samples, (radius, visibility)) function.__name__ = "_get_time_samples - with radius and visibility" print("Testing GetTimeSamples() for multiple attributes, normally") timeit(function, REPEATS) function = functools.partial(query.GetUnionedTimeSamples, [query, Usd.AttributeQuery(visibility)]) function.__name__ = "GetUnionedTimeSamples - with radius and visibility" print("Testing GetTimeSamples() for multiple attributes, using a union") timeit(function, REPEATS) print() print("Heavy Stage:") heavier_stage = _create_basic_scene_with_more_values() sphere = UsdGeom.Sphere(heavier_stage.GetPrimAtPath("/Some/Prim")) radius = sphere.GetRadiusAttr() visibility = sphere.GetVisibilityAttr() query = Usd.AttributeQuery(radius) function = functools.partial(_get_time_samples, (radius, visibility)) function.__name__ = "_get_time_samples - with radius and visibility" print("Testing GetTimeSamples() for multiple attributes, normally") timeit(function, REPEATS) function = functools.partial(query.GetUnionedTimeSamples, [query, Usd.AttributeQuery(visibility)]) function.__name__ = "GetUnionedTimeSamples - with radius and visibility" print("Testing GetTimeSamples() for multiple attributes, using a union") timeit(function, REPEATS)
def test_NoInvalidationForInsignificantChange(self): """Test that insignificant layer stack changes do not invalidate an attribute query.""" sublayer = Sdf.Layer.CreateAnonymous("source.usda") sublayer.ImportFromString(''' #usda 1.0 def "Prim" { double attr = 1.0 double attr.timeSamples = { 0.0: 2.0 } } '''.strip()) emptySublayerBefore = Sdf.Layer.CreateAnonymous("empty_before.usda") emptySublayerAfter = Sdf.Layer.CreateAnonymous("empty_after.usda") rootLayer = Sdf.Layer.CreateAnonymous("root.usda") rootLayer.subLayerPaths.insert(0, sublayer.identifier) stage = Usd.Stage.Open(rootLayer) query = Usd.AttributeQuery(stage.GetAttributeAtPath("/Prim.attr")) self.assertTrue(query) self.assertEqual(query.Get(), 1.0) self.assertEqual(query.Get(0), 2.0) # Test that adding and removing empty sublayers before and after the # sublayer with attribute values does not invalidate the attribute # query. @contextlib.contextmanager def _Validate(): with StageChangeListener(stage) as l: yield self.assertEqual(l.resyncedPrimPaths, []) self.assertTrue(query) self.assertEqual(query.Get(), 1.0) self.assertEqual(query.Get(0), 2.0) with _Validate(): rootLayer.subLayerPaths.insert(0, emptySublayerBefore.identifier) with _Validate(): rootLayer.subLayerPaths.append(emptySublayerAfter.identifier) with _Validate(): rootLayer.subLayerPaths.remove(emptySublayerBefore.identifier) with _Validate(): rootLayer.subLayerPaths.remove(emptySublayerAfter.identifier)
def _SetDefaultWithEditTarget(editTarget, attr, resolvedValue, expectedAuthoredValue): # Set the edit target on the stage. stage.SetEditTarget(editTarget) # Set the value at time to the resolved value and verify we get the # same resolved value back from both the attr and a # UsdAttributeQuery attr.Set(resolvedValue) self.assertEqual(attr.Get(), resolvedValue) # Note that we create the attribute query in this function for the # same attribute because we're making changes that affect value # resolution attrQuery = Usd.AttributeQuery(attr) self.assertEqual(attrQuery.Get(), resolvedValue) # Verify that the default value authored on the edit target's layer # matches the expected authored value. self._VerifyAuthoredValue(editTarget, attr.GetPath(), "default", expectedAuthoredValue)
def test_ResolvedAssetPaths(self): print 'Testing that asset-path-valued attributes give resolved values' import os for fmt in allFormats: with Tf.NamedTemporaryFile(suffix='.'+fmt) as rootFile, \ Tf.NamedTemporaryFile(suffix='.'+fmt) as targetFile: self.assertEqual( os.path.split(rootFile.name)[0], os.path.split(targetFile.name)[0]) print rootFile.name, targetFile.name s = Usd.Stage.CreateNew(rootFile.name) foo = s.DefinePrim('/foo') singleAsset = foo.CreateAttribute('singleAsset', Sdf.ValueTypeNames.Asset) arrayAsset = foo.CreateAttribute('arrayAsset', Sdf.ValueTypeNames.AssetArray) singleAssetQuery = Usd.AttributeQuery(foo, 'singleAsset') arrayAssetQuery = Usd.AttributeQuery(foo, 'arrayAsset') relPath = './' + os.path.split(targetFile.name)[1] relPathArray = Sdf.AssetPathArray(42, [relPath]) self.assertNotEqual(relPath, targetFile.name) singleAsset.Set(relPath) arrayAsset.Set(relPathArray) singleAssetValue = singleAsset.Get() arrayAssetValue = arrayAsset.Get() singleAssetQueryValue = singleAssetQuery.Get() arrayAssetQueryValue = arrayAssetQuery.Get() self.assertEqual(singleAssetValue.path, relPath) self.assertTrue( all([ap.path == relPath for ap in arrayAssetValue])) # Ensure that attribute query also resolves paths self.assertEqual(singleAssetValue.path, singleAssetQueryValue.path) self.assertEqual([ap.path for ap in arrayAssetValue], [ap.path for ap in arrayAssetQueryValue]) # NOTE: We use os.path.abspath() to ensure the paths can be # accurately compared. On Windows this will change # forward slash directory separators to backslashes. self.assertEqual( os.path.abspath(singleAssetValue.resolvedPath), targetFile.name) self.assertTrue( all([ os.path.abspath(ap.resolvedPath) == targetFile.name for ap in arrayAssetValue ])) self.assertEqual(singleAssetValue.resolvedPath, singleAssetQueryValue.resolvedPath) self.assertEqual( [ap.resolvedPath for ap in arrayAssetValue], [ap.resolvedPath for ap in arrayAssetQueryValue]) # Test a case where the file does not exist, which the default # resolver doesn't resolve relPath = './nonexistent_file_for_testUsdCreateProperties.xxx' relPathArray = Sdf.AssetPathArray(42, [relPath]) singleAsset.Set(relPath) arrayAsset.Set(relPathArray) singleAssetValue = singleAsset.Get() arrayAssetValue = arrayAsset.Get() self.assertEqual(singleAssetValue.path, relPath) self.assertTrue( all([ap.path == relPath for ap in arrayAssetValue])) self.assertEqual(singleAssetValue.resolvedPath, '') self.assertTrue( all([ap.resolvedPath == '' for ap in arrayAssetValue])) # NOTE: rootFile will want to delete the underlying file # on __exit__ from the context manager. But stage s # may have the file open. If so the deletion will # fail on Windows. Explicitly release our reference # to the stage to close the file. del s
def test_Fallbacks(self): # Author Scene and Compose Stage stage = Usd.Stage.CreateInMemory() # Xformable Tests identity = Gf.Matrix4d(1) origin = Gf.Vec3f(0, 0, 0) xform = UsdGeom.Xform.Define(stage, "/Xform") # direct subclass xformOpOrder = xform.GetXformOpOrderAttr() self.assertFalse(xformOpOrder.HasAuthoredValueOpinion()) # xformOpOrder has no fallback value self.assertEqual(xformOpOrder.Get(), None) self.assertFalse(xformOpOrder.HasFallbackValue()) # Try authoring and reverting... xformOpOrderAttr = xform.GetPrim().GetAttribute( UsdGeom.Tokens.xformOpOrder) self.assertTrue(xformOpOrderAttr) self.assertEqual(xformOpOrderAttr.Get(), None) opOrderVal = ["xformOp:transform"] self.assertTrue(xformOpOrderAttr.Set(opOrderVal)) self.assertTrue(xformOpOrderAttr.HasAuthoredValueOpinion()) self.assertNotEqual(xformOpOrderAttr.Get(), None) self.assertTrue(xformOpOrderAttr.Clear()) self.assertFalse(xformOpOrderAttr.HasAuthoredValueOpinion()) self.assertEqual(xformOpOrderAttr.Get(), None) self.assertFalse(xformOpOrder.HasFallbackValue()) mesh = UsdGeom.Mesh.Define(stage, "/Mesh") # multiple ancestor hops # PointBased and Curves curves = UsdGeom.BasisCurves.Define(stage, "/Curves") self.assertEqual(curves.GetNormalsInterpolation(), UsdGeom.Tokens.varying) self.assertEqual(curves.GetWidthsInterpolation(), UsdGeom.Tokens.varying) # Before we go, test that CreateXXXAttr performs as we expect in various # scenarios # Number 1: Sparse and non-sparse authoring on def'd prim mesh.CreateDoubleSidedAttr(False, True) self.assertFalse(mesh.GetDoubleSidedAttr().HasAuthoredValueOpinion()) mesh.CreateDoubleSidedAttr(False, False) self.assertTrue(mesh.GetDoubleSidedAttr().HasAuthoredValueOpinion()) # Number 2: Sparse authoring demotes to dense for non-defed prim overMesh = UsdGeom.Mesh(stage.OverridePrim('/overMesh')) overMesh.CreateDoubleSidedAttr(False, True) self.assertTrue( overMesh.GetDoubleSidedAttr().HasAuthoredValueOpinion()) self.assertEqual(overMesh.GetDoubleSidedAttr().Get(), False) overMesh.CreateDoubleSidedAttr(True, True) self.assertEqual(overMesh.GetDoubleSidedAttr().Get(), True) # make it a defined mesh, and sanity check it still evals the same mesh2 = UsdGeom.Mesh.Define(stage, "/overMesh") self.assertEqual(overMesh.GetDoubleSidedAttr().Get(), True) # Check querying of fallback values. sphere = UsdGeom.Sphere.Define(stage, "/Sphere") radius = sphere.GetRadiusAttr() self.assertTrue(radius.HasFallbackValue()) radiusQuery = Usd.AttributeQuery(radius) self.assertTrue(radiusQuery.HasFallbackValue())
def test_ResolvedAssetPaths(self): print 'Testing that asset-path-valued attributes give resolved values' import os for fmt in allFormats: with Tf.NamedTemporaryFile(suffix='.'+fmt) as rootFile, \ Tf.NamedTemporaryFile(suffix='.'+fmt) as targetFile: self.assertEqual(os.path.split(rootFile.name)[0], os.path.split(targetFile.name)[0]) print rootFile.name, targetFile.name s = Usd.Stage.CreateNew(rootFile.name) foo = s.DefinePrim('/foo') singleAsset = foo.CreateAttribute('singleAsset', Sdf.ValueTypeNames.Asset) arrayAsset = foo.CreateAttribute('arrayAsset', Sdf.ValueTypeNames.AssetArray) singleAssetQuery = Usd.AttributeQuery(foo, 'singleAsset') arrayAssetQuery = Usd.AttributeQuery(foo, 'arrayAsset') relPath = './' + os.path.split(targetFile.name)[1] relPathArray = Sdf.AssetPathArray(42, [relPath]) self.assertNotEqual(relPath, targetFile.name) singleAsset.Set(relPath) arrayAsset.Set(relPathArray) singleAssetValue = singleAsset.Get() arrayAssetValue = arrayAsset.Get() singleAssetQueryValue = singleAssetQuery.Get() arrayAssetQueryValue = arrayAssetQuery.Get() self.assertEqual(singleAssetValue.path, relPath) self.assertTrue(all([ap.path == relPath for ap in arrayAssetValue])) # Ensure that attribute query also resolves paths self.assertEqual(singleAssetValue.path, singleAssetQueryValue.path) self.assertEqual([ap.path for ap in arrayAssetValue], [ap.path for ap in arrayAssetQueryValue]) self.assertEqual(singleAssetValue.resolvedPath, targetFile.name) self.assertTrue(all([ap.resolvedPath == targetFile.name for ap in arrayAssetValue])) self.assertEqual(singleAssetValue.resolvedPath, singleAssetQueryValue.resolvedPath) self.assertEqual([ap.resolvedPath for ap in arrayAssetValue], [ap.resolvedPath for ap in arrayAssetQueryValue]) # Test a case where the file does not exist, which the default # resolver doesn't resolve relPath = './nonexistent_file_for_testUsdCreateProperties.xxx' relPathArray = Sdf.AssetPathArray(42, [relPath]) singleAsset.Set(relPath) arrayAsset.Set(relPathArray) singleAssetValue = singleAsset.Get() arrayAssetValue = arrayAsset.Get() self.assertEqual(singleAssetValue.path, relPath) self.assertTrue(all([ap.path == relPath for ap in arrayAssetValue])) self.assertEqual(singleAssetValue.resolvedPath, '') self.assertTrue(all([ap.resolvedPath == '' for ap in arrayAssetValue]))