def test_AssetInfo(self): for fmt in allFormats: s = Usd.Stage.CreateInMemory('TestAssetInfo.' + fmt) p = s.DefinePrim('/World', 'Xform') model = Usd.ModelAPI(p) self.assertEqual(model.GetAssetInfo(), {}) model.SetAssetName('PaperCup') self.assertEqual(model.GetAssetName(), 'PaperCup') model.SetAssetVersion('10a') self.assertEqual(model.GetAssetVersion(), '10a') model.SetAssetIdentifier('PaperCup/usd/PaperCup.usd') self.assertEqual(model.GetAssetIdentifier(), 'PaperCup/usd/PaperCup.usd') pad = Sdf.AssetPathArray([ Sdf.AssetPath('Paper/usd/Paper.usd'), Sdf.AssetPath('Cup/usd/Cup.usd') ]) model.SetPayloadAssetDependencies(pad) self.assertEqual(model.GetPayloadAssetDependencies(), pad) expectedAssetInfo = { 'identifier': Sdf.AssetPath('PaperCup/usd/PaperCup.usd'), 'name': 'PaperCup', 'version': '10a', 'payloadAssetDependencies': Sdf.AssetPathArray([ Sdf.AssetPath('Paper/usd/Paper.usd'), Sdf.AssetPath('Cup/usd/Cup.usd') ]) } self.assertEqual(model.GetAssetInfo(), expectedAssetInfo) stageContents = s.ExportToString() self.assertTrue('string name = "PaperCup"' in stageContents) self.assertTrue('asset identifier = @PaperCup/usd/PaperCup.usd@' in stageContents) self.assertTrue( 'asset[] payloadAssetDependencies = ' '[@Paper/usd/Paper.usd@, @Cup/usd/Cup.usd@]' in stageContents) self.assertTrue('string version = "10a"' in stageContents)
def test_SetArraysWithLists(self): from pxr import Vt, Sdf for fmt in allFormats: s = Usd.Stage.CreateInMemory('SetArraysWithListsTest.' + fmt) prim = s.OverridePrim('/test') strs = prim.CreateAttribute('strs', Sdf.ValueTypeNames.StringArray) toks = prim.CreateAttribute('toks', Sdf.ValueTypeNames.TokenArray) asst = prim.CreateAttribute('asst', Sdf.ValueTypeNames.AssetArray) assert strs.IsDefined() assert toks.IsDefined() assert asst.IsDefined() # Set with python list. strs.Set(['hello'] * 3) assert strs.Get() == Vt.StringArray(3, ['hello']) toks.Set(['bye'] * 3) assert toks.Get() == Vt.TokenArray(3, ['bye']) asst.Set([Sdf.AssetPath('/path')] * 3) assert asst.Get() == Sdf.AssetPathArray(3, [Sdf.AssetPath('/path')]) # Should fail with incompatible types. with self.assertRaises(Tf.ErrorException): strs.Set([1234] * 3) with self.assertRaises(Tf.ErrorException): toks.Set([1234] * 3) with self.assertRaises(Tf.ErrorException): asst.Set([1234] * 3)
def main(): stage = Usd.Stage.CreateInMemory() # Method A: Set using methods some_sphere = UsdGeom.Sphere.Define(stage, "/SomeSphere") model = Usd.ModelAPI(some_sphere.GetPrim()) model.SetAssetName("some_asset") model.SetAssetVersion("v1") model.SetAssetIdentifier("some/path/to/file.usda") model.SetPayloadAssetDependencies( Sdf.AssetPathArray( [Sdf.AssetPath("something.usd"), Sdf.AssetPath("another/thing.usd")] ) ) # Method B: Set-by-key another_sphere = UsdGeom.Sphere.Define(stage, "/AnotherSphere") another_prim = another_sphere.GetPrim() another_prim.SetAssetInfoByKey("version", "v1") another_prim.SetAssetInfoByKey("name", "some_asset") another_prim.SetAssetInfoByKey("identifier", "some/path/to/file.usda") another_prim.SetAssetInfoByKey( "payloadAssetDependencies", Sdf.AssetPathArray( [Sdf.AssetPath("something.usd"), Sdf.AssetPath("another/thing.usd")] ), ) # Method C: Set-by-dict last_sphere = UsdGeom.Sphere.Define(stage, "/LastSphere") last_sphere.GetPrim().SetAssetInfo( { "identifier": "some/path/to/file.usda", "name": "some_asset", "version": "v1", "payloadAssetDependencies": Sdf.AssetPathArray( [Sdf.AssetPath("something.usd"), Sdf.AssetPath("another/thing.usd")] ), } ) print(stage.GetRootLayer().ExportToString())
def test_CustomSetName(self): """ Test authoring with a custom set name 'bob' """ # template case resultLayer = Sdf.Layer.CreateNew('customSetName.usd') topLayer = Sdf.Layer.CreateNew('customSetName.topology.usd') UsdUtils.StitchClipsTemplate(resultLayer, topLayer, self.clipPath, 'asset.#.usd', 101, 120, 1, 0.3, 'bob') self.assertEqual(list(resultLayer.subLayerPaths), ['./customSetName.topology.usd']) self.assertEqual(resultLayer.endTimeCode, 120) self.assertEqual(resultLayer.startTimeCode, 101) prim = resultLayer.GetPrimAtPath('/World/fx/Particles_Splash') self.assertTrue('clips' in prim.GetMetaDataInfoKeys()) expectedValues = { 'templateStartTime': 101.0, 'templateStride': 1.0, 'templateActiveOffset': 0.3, 'primPath': '/World/fx/Particles_Splash', 'templateAssetPath': 'asset.#.usd', 'templateEndTime': 120.0 } # Ensure that our custom set name applied actualValues = prim.GetInfo('clips')['bob'] self.assertTrue(actualValues is not None) # Ensure all of our values were written out for k in [ 'templateStartTime', 'templateStride', 'templateEndTime', 'primPath', 'templateAssetPath' ]: self.assertEqual(expectedValues[k], actualValues[k]) # non template case resultLayer = Sdf.Layer.CreateNew('customSetNameNonTemplate.usd') UsdUtils.StitchClips(resultLayer, self.layerFileNames[:7], self.clipPath, clipSet='bob') self.assertEqual(resultLayer.startTimeCode, 101) self.assertEqual(resultLayer.endTimeCode, 107) self.assertEqual(list(resultLayer.subLayerPaths), ['./customSetNameNonTemplate.topology.usd']) prim = resultLayer.GetPrimAtPath('/World/fx/Particles_Splash') expectedValues = { 'active': Vt.Vec2dArray([(101.0, 0.0), (102.0, 1.0), (103.0, 2.0), (104.0, 3.0), (105.0, 4.0), (106.0, 5.0), (107.0, 6.0)]), 'times': Vt.Vec2dArray([(101.0, 101.0), (102.0, 102.0), (103.0, 103.0), (104.0, 104.0), (105.0, 105.0), (106.0, 106.0), (107.0, 107.0)]), 'manifestAssetPath': Sdf.AssetPath('./customSetNameNonTemplate.topology.usd'), 'assetPaths': Sdf.AssetPathArray([ Sdf.AssetPath('./src/Particles_Splash.101.usd'), Sdf.AssetPath('./src/Particles_Splash.102.usd'), Sdf.AssetPath('./src/Particles_Splash.103.usd'), Sdf.AssetPath('./src/Particles_Splash.104.usd'), Sdf.AssetPath('./src/Particles_Splash.105.usd'), Sdf.AssetPath('./src/Particles_Splash.106.usd'), Sdf.AssetPath('./src/Particles_Splash.107.usd') ]), 'primPath': '/World/fx/Particles_Splash' } # Ensure that our custom set name applied actualValues = prim.GetInfo('clips')['bob'] self.assertTrue(actualValues is not None) # Ensure all of our values were written out for k in [ 'active', 'times', 'manifestAssetPath', 'primPath', 'assetPaths' ]: self.assertEqual(expectedValues[k], actualValues[k])
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_ClipAuthoring(self): """Tests clip authoring API on Usd.ClipsAPI""" allFormats = ['usd' + x for x in 'ac'] for fmt in allFormats: stage = Usd.Stage.CreateInMemory('TestClipAuthoring.' + fmt) prim = stage.DefinePrim('/Model') model = Usd.ClipsAPI(prim) prim2 = stage.DefinePrim('/Model2') model2 = Usd.ClipsAPI(prim2) # Clip authoring API supports the use of lists as well as Vt arrays. clipAssetPaths = [ Sdf.AssetPath('clip1.usda'), Sdf.AssetPath('clip2.usda') ] model.SetClipAssetPaths(clipAssetPaths) self.assertEqual(model.GetClipAssetPaths(), clipAssetPaths) model2.SetClipAssetPaths( Sdf.AssetPathArray( [Sdf.AssetPath('clip1.usda'), Sdf.AssetPath('clip2.usda')])) self.assertEqual(model2.GetClipAssetPaths(), clipAssetPaths) clipPrimPath = "/Clip" model.SetClipPrimPath(clipPrimPath) self.assertEqual(model.GetClipPrimPath(), clipPrimPath) clipTimes = Vt.Vec2dArray([(0.0, 0.0), (10.0, 10.0), (20.0, 20.0)]) model.SetClipTimes(clipTimes) self.assertEqual(model.GetClipTimes(), clipTimes) model2.SetClipTimes( Vt.Vec2dArray([ Gf.Vec2d(0.0, 0.0), Gf.Vec2d(10.0, 10.0), Gf.Vec2d(20.0, 20.0) ])) self.assertEqual(model2.GetClipTimes(), clipTimes) clipActive = [(0.0, 0.0), (10.0, 1.0), (20.0, 0.0)] model.SetClipActive(clipActive) self.assertEqual(model.GetClipActive(), Vt.Vec2dArray(clipActive)) model2.SetClipActive( Vt.Vec2dArray([ Gf.Vec2d(0.0, 0.0), Gf.Vec2d(10.0, 1.0), Gf.Vec2d(20.0, 0.0) ])) self.assertEqual(model2.GetClipActive(), Vt.Vec2dArray(clipActive)) clipManifestAssetPath = Sdf.AssetPath('clip_manifest.usda') model.SetClipManifestAssetPath(clipManifestAssetPath) self.assertEqual(model.GetClipManifestAssetPath(), clipManifestAssetPath) # Test authoring of template clip metadata model.SetClipTemplateAssetPath('clip.###.usda') self.assertEqual(model.GetClipTemplateAssetPath(), 'clip.###.usda') model.SetClipTemplateStride(4.5) self.assertEqual(model.GetClipTemplateStride(), 4.5) model.SetClipTemplateStartTime(1) self.assertEqual(model.GetClipTemplateStartTime(), 1) model.SetClipTemplateEndTime(5) self.assertEqual(model.GetClipTemplateEndTime(), 5) # Ensure we can't set the clipTemplateStride to 0 with self.assertRaises(Tf.ErrorException) as e: model.SetClipTemplateStride(0)
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]))