def testExportPxrRisShading(self): """ Tests that exporting a Maya mesh with a simple Maya shading setup results in the correct shading on the USD mesh. """ cubePrim = self._stage.GetPrimAtPath('/MarbleCube/Geom/Cube') self.assertTrue(cubePrim) # Validate the Material prim bound to the Mesh prim. material = UsdShade.Material.GetBoundMaterial(cubePrim) self.assertTrue(material) materialPath = material.GetPath().pathString self.assertEqual(materialPath, '/MarbleCube/Looks/MarbleCubeSG') # Validate the surface shader that is connected to the material. # XXX: Note that the expected number of outputs here is two rather than # one, since we are still authoring the UsdRi Bxdf source in addition # to the surface terminal for backwards compatibility. When consumers # are updated to use the surface terminal instead, this test will have # to be updated. materialOutputs = material.GetOutputs() self.assertEqual(len(materialOutputs), 2) materialOutput = material.GetOutput('surface') (connectableAPI, outputName, outputType) = materialOutput.GetConnectedSource() self.assertEqual(outputName, 'out') shader = UsdShade.Shader(connectableAPI) self.assertTrue(shader) # XXX: Validate the UsdRi Bxdf. This must also be removed when we no # longer author it. from pxr import UsdRi usdRiMaterialAPI = UsdRi.MaterialAPI(material.GetPrim()) self.assertTrue(usdRiMaterialAPI) bxdf = usdRiMaterialAPI.GetBxdf() self.assertEqual(bxdf.GetPrim(), shader.GetPrim()) shaderId = shader.GetIdAttr().Get() self.assertEqual(shaderId, 'PxrMayaMarble') # Validate the connected input on the surface shader. shaderInput = shader.GetInput('placementMatrix') self.assertTrue(shaderInput) (connectableAPI, outputName, outputType) = shaderInput.GetConnectedSource() self.assertEqual(outputName, 'worldInverseMatrix') shader = UsdShade.Shader(connectableAPI) self.assertTrue(shader) shaderId = shader.GetIdAttr().Get() self.assertEqual(shaderId, 'PxrMayaPlacement3d')
def test_Basic(self): l = Sdf.Layer.CreateAnonymous() stage = Usd.Stage.Open(l.identifier) world = stage.DefinePrim("/World", "Xform") assert world world.SetMetadata('kind', 'group') assert world.IsModel() assert world.IsGroup() group = stage.DefinePrim("/World/Group", "Xform") assert group group.SetMetadata('kind', 'group') assert group.IsModel() assert group.IsGroup() model = stage.DefinePrim("/World/Group/Model", "Xform") assert model model.SetMetadata('kind', 'component') assert model.IsModel() p = stage.DefinePrim("/World/Group/Model/Mesh", "Scope") assert p print("Test Material") material = UsdShade.Material.Define(stage, "/World/Group/Model/Material") assert material assert material.GetPrim() material.Bind(p) print("Test shader") shader = UsdRi.RslShader.Define(stage, '/World/Group/Model/Shader') assert shader assert shader.GetPrim() assert not UsdRi.StatementsAPI.IsRiAttribute(shader.GetSloPathAttr()) shader.GetSloPathAttr().Set('foo') print("Test RiMaterialAPI") riMaterial = UsdRi.MaterialAPI(material) assert riMaterial assert riMaterial.GetPrim() # Test surface output self._TestOutput(riMaterial, UsdRi.MaterialAPI.GetSurfaceOutput, UsdRi.MaterialAPI.SetSurfaceSource, UsdRi.MaterialAPI.GetSurface, shader.GetPath()) # Test displacement output self._TestOutput(riMaterial, UsdRi.MaterialAPI.GetDisplacementOutput, UsdRi.MaterialAPI.SetDisplacementSource, UsdRi.MaterialAPI.GetDisplacement, shader.GetPath()) # Test volume output self._TestOutput(riMaterial, UsdRi.MaterialAPI.GetVolumeOutput, UsdRi.MaterialAPI.SetVolumeSource, UsdRi.MaterialAPI.GetVolume, shader.GetPath()) print("Test pattern") pattern = UsdRi.RisPattern.Define(stage, '/World/Group/Model/Pattern') assert pattern assert pattern.GetPrim() pattern.GetFilePathAttr().Set('foo') self.assertEqual(pattern.GetFilePathAttr().Get(), 'foo') pattern.GetArgsPathAttr().Set('argspath') self.assertEqual(pattern.GetArgsPathAttr().Get(), 'argspath') print("Test oslPattern") oslPattern = UsdRi.RisOslPattern.Define( stage, '/World/Group/Model/OslPattern') assert oslPattern assert oslPattern.GetPrim() self.assertEqual(oslPattern.GetFilePathAttr().Get(), 'PxrOSL') print("Test bxdf") bxdf = UsdRi.RisBxdf.Define(stage, '/World/Group/Model/Bxdf') assert bxdf assert bxdf.GetPrim() bxdf.GetFilePathAttr().Set('foo') bxdf.GetArgsPathAttr().Set('argspath') print("Test RIS Material") risMaterial = UsdRi.MaterialAPI(material.GetPrim()) assert risMaterial assert risMaterial.GetPrim() assert not risMaterial.GetBxdf() # Test the bxdf output self._TestOutput(risMaterial, UsdRi.MaterialAPI.GetBxdfOutput, UsdRi.MaterialAPI.SetBxdfSource, UsdRi.MaterialAPI.GetBxdf, bxdf.GetPath()) print("Test riStatements") riStatements = UsdRi.StatementsAPI(shader.GetPrim()) assert riStatements assert riStatements.GetPrim() attr = riStatements.CreateRiAttribute("ModelName", "string").\ Set('someModelName') assert attr props = riStatements.GetRiAttributes() assert props # this is so convoluted attr = riStatements.GetPrim().GetAttribute(props[0].GetName()) assert attr self.assertEqual(attr.GetName(), 'ri:attributes:user:ModelName') self.assertEqual(attr.Get(), 'someModelName') self.assertEqual(UsdRi.StatementsAPI.GetRiAttributeName(attr), 'ModelName') self.assertEqual(UsdRi.StatementsAPI.GetRiAttributeNameSpace(attr), 'user') assert UsdRi.StatementsAPI.IsRiAttribute(attr) self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName('myattr'), 'ri:attributes:user:myattr') self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName('dice:myattr'), 'ri:attributes:dice:myattr') self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName('dice.myattr'), 'ri:attributes:dice:myattr') self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName('dice_myattr'), 'ri:attributes:dice:myattr') # period is stronger separator than underscore, when both are present self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName('dice_my.attr'), 'ri:attributes:dice_my:attr') # multiple tokens concatted with underscores self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName( 'dice:my1:long:attr'), 'ri:attributes:dice:my1_long_attr') self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName( 'dice.my2.long.attr'), 'ri:attributes:dice:my2_long_attr') self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName( 'dice_my3_long_attr'), 'ri:attributes:dice:my3_long_attr') self.assertEqual(riStatements.GetCoordinateSystem(), '') self.assertEqual( UsdRi.StatementsAPI(model).GetModelCoordinateSystems(), []) self.assertEqual( UsdRi.StatementsAPI(model).GetModelScopedCoordinateSystems(), []) riStatements.SetCoordinateSystem('LEyeSpace') self.assertEqual(riStatements.GetCoordinateSystem(), 'LEyeSpace') self.assertEqual( UsdRi.StatementsAPI(model).GetModelCoordinateSystems(), [Sdf.Path('/World/Group/Model/Shader')]) riStatements.SetScopedCoordinateSystem('ScopedLEyeSpace') self.assertEqual(riStatements.GetScopedCoordinateSystem(), 'ScopedLEyeSpace') self.assertEqual( UsdRi.StatementsAPI(model).GetModelScopedCoordinateSystems(), [Sdf.Path('/World/Group/Model/Shader')]) self.assertEqual( UsdRi.StatementsAPI(group).GetModelCoordinateSystems(), []) self.assertEqual( UsdRi.StatementsAPI(group).GetModelScopedCoordinateSystems(), []) self.assertEqual( UsdRi.StatementsAPI(world).GetModelCoordinateSystems(), []) self.assertEqual( UsdRi.StatementsAPI(world).GetModelScopedCoordinateSystems(), []) self.assertFalse(riStatements.GetFocusRegionAttr().IsValid()) assert (riStatements.CreateFocusRegionAttr() is not None) assert (riStatements.GetFocusRegionAttr() is not None) self.assertTrue(riStatements.GetFocusRegionAttr().IsValid()) self.assertEqual(riStatements.GetFocusRegionAttr().Get(), None) riStatements.CreateFocusRegionAttr(9.0, True) self.assertEqual(riStatements.GetFocusRegionAttr().Get(), 9.0)
def test_Basic(self): l = Sdf.Layer.CreateAnonymous() stage = Usd.Stage.Open(l.identifier) world = stage.DefinePrim("/World", "Xform") assert world world.SetMetadata('kind', 'group') assert world.IsModel() assert world.IsGroup() group = stage.DefinePrim("/World/Group", "Xform") assert group group.SetMetadata('kind', 'group') assert group.IsModel() assert group.IsGroup() model = stage.DefinePrim("/World/Group/Model", "Xform") assert model model.SetMetadata('kind', 'component') assert model.IsModel() p = stage.DefinePrim("/World/Group/Model/Mesh", "Scope") assert p print("Test Material") material = UsdShade.Material.Define(stage, "/World/Group/Model/Material") assert material assert material.GetPrim() UsdShade.MaterialBindingAPI(p).Bind(material) print("Test shader") shader = UsdRi.RslShader.Define(stage, '/World/Group/Model/Shader') assert shader assert shader.GetPrim() assert not UsdRi.StatementsAPI.IsRiAttribute(shader.GetSloPathAttr()) shader.GetSloPathAttr().Set('foo') print("Test RiMaterialAPI") riMaterial = UsdRi.MaterialAPI.Apply(material.GetPrim()) assert riMaterial assert riMaterial.GetPrim() # Test surface output self._TestOutput(riMaterial, UsdRi.MaterialAPI.GetSurfaceOutput, UsdRi.MaterialAPI.SetSurfaceSource, UsdRi.MaterialAPI.GetSurface, shader.GetPath()) # Test displacement output self._TestOutput(riMaterial, UsdRi.MaterialAPI.GetDisplacementOutput, UsdRi.MaterialAPI.SetDisplacementSource, UsdRi.MaterialAPI.GetDisplacement, shader.GetPath()) # Test volume output self._TestOutput(riMaterial, UsdRi.MaterialAPI.GetVolumeOutput, UsdRi.MaterialAPI.SetVolumeSource, UsdRi.MaterialAPI.GetVolume, shader.GetPath()) print("Test pattern") pattern = UsdRi.RisPattern.Define(stage, '/World/Group/Model/Pattern') assert pattern assert pattern.GetPrim() pattern.GetFilePathAttr().Set('foo') self.assertEqual(pattern.GetFilePathAttr().Get(), 'foo') pattern.GetArgsPathAttr().Set('argspath') self.assertEqual(pattern.GetArgsPathAttr().Get(), 'argspath') print("Test oslPattern") oslPattern = UsdRi.RisOslPattern.Define( stage, '/World/Group/Model/OslPattern') assert oslPattern assert oslPattern.GetPrim() self.assertEqual(oslPattern.GetFilePathAttr().Get(), 'PxrOSL') print("Test bxdf") bxdf = UsdRi.RisBxdf.Define(stage, '/World/Group/Model/Bxdf') assert bxdf assert bxdf.GetPrim() bxdf.GetFilePathAttr().Set('foo') bxdf.GetArgsPathAttr().Set('argspath') print("Test RIS Material") risMaterial = UsdRi.MaterialAPI(material.GetPrim()) assert risMaterial assert risMaterial.GetPrim() print("Test riStatements") riStatements = UsdRi.StatementsAPI.Apply(shader.GetPrim()) assert riStatements assert riStatements.GetPrim() attr = riStatements.CreateRiAttribute("ModelName", "string").\ Set('someModelName') assert attr props = riStatements.GetRiAttributes() assert props # this is so convoluted attr = riStatements.GetPrim().GetAttribute(props[0].GetName()) assert attr prefix = ('primvars:' if Tf.GetEnvSetting('USDRI_STATEMENTS_WRITE_NEW_ATTR_ENCODING') else '') self.assertEqual(attr.GetName(), prefix + 'ri:attributes:user:ModelName') self.assertEqual(attr.Get(), 'someModelName') self.assertEqual(UsdRi.StatementsAPI.GetRiAttributeName(attr), 'ModelName') self.assertEqual(UsdRi.StatementsAPI.GetRiAttributeNameSpace(attr), 'user') assert UsdRi.StatementsAPI.IsRiAttribute(attr) self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName('myattr'), prefix + 'ri:attributes:user:myattr') self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName('dice:myattr'), prefix + 'ri:attributes:dice:myattr') self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName('dice.myattr'), prefix + 'ri:attributes:dice:myattr') self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName('dice_myattr'), prefix + 'ri:attributes:dice:myattr') # period is stronger separator than underscore, when both are present self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName('dice_my.attr'), prefix + 'ri:attributes:dice_my:attr') # multiple tokens concatted with underscores self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName( 'dice:my1:long:attr'), prefix + 'ri:attributes:dice:my1_long_attr') self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName( 'dice.my2.long.attr'), prefix + 'ri:attributes:dice:my2_long_attr') self.assertEqual( UsdRi.StatementsAPI.MakeRiAttributePropertyName( 'dice_my3_long_attr'), prefix + 'ri:attributes:dice:my3_long_attr') self.assertEqual(riStatements.GetCoordinateSystem(), '') self.assertEqual( UsdRi.StatementsAPI(model).GetModelCoordinateSystems(), []) self.assertEqual( UsdRi.StatementsAPI(model).GetModelScopedCoordinateSystems(), []) riStatements.SetCoordinateSystem('LEyeSpace') self.assertEqual(riStatements.GetCoordinateSystem(), 'LEyeSpace') self.assertEqual( UsdRi.StatementsAPI(model).GetModelCoordinateSystems(), [Sdf.Path('/World/Group/Model/Shader')]) riStatements.SetScopedCoordinateSystem('ScopedLEyeSpace') self.assertEqual(riStatements.GetScopedCoordinateSystem(), 'ScopedLEyeSpace') self.assertEqual( UsdRi.StatementsAPI(model).GetModelScopedCoordinateSystems(), [Sdf.Path('/World/Group/Model/Shader')]) self.assertEqual( UsdRi.StatementsAPI(group).GetModelCoordinateSystems(), []) self.assertEqual( UsdRi.StatementsAPI(group).GetModelScopedCoordinateSystems(), []) self.assertEqual( UsdRi.StatementsAPI(world).GetModelCoordinateSystems(), []) self.assertEqual( UsdRi.StatementsAPI(world).GetModelScopedCoordinateSystems(), []) # Test mixed old & new style encodings if (Tf.GetEnvSetting('USDRI_STATEMENTS_WRITE_NEW_ATTR_ENCODING') and Tf.GetEnvSetting('USDRI_STATEMENTS_READ_OLD_ATTR_ENCODING')): prim = stage.DefinePrim("/prim") riStatements = UsdRi.StatementsAPI.Apply(prim) self.assertEqual(len(riStatements.GetRiAttributes()), 0) # Add new-style newStyleAttr = riStatements.CreateRiAttribute('newStyle', 'string') newStyleAttr.Set('new') self.assertEqual(len(riStatements.GetRiAttributes()), 1) # Add old-style (note that we can't use UsdRi API for this, # since it doesn't let the caller choose the encoding) oldStyleAttr = prim.CreateAttribute('ri:attributes:user:oldStyle', Sdf.ValueTypeNames.String) oldStyleAttr.Set('old') self.assertEqual(len(riStatements.GetRiAttributes()), 2) # Exercise the case of an Ri attribute encoded in both # old and new styles. ignoredAttr = prim.CreateAttribute('ri:attributes:user:newStyle', Sdf.ValueTypeNames.String) self.assertEqual(len(riStatements.GetRiAttributes()), 2) self.assertFalse(ignoredAttr in riStatements.GetRiAttributes())
def testExportDisplayColorShading(self): """ Tests that exporting a Maya mesh with a simple Maya shading setup results in the correct shading on the USD mesh. """ # Validate the displayColor on the mesh prim. cubePrim = self._stage.GetPrimAtPath('/RedCube/Geom/Cube') self.assertTrue(cubePrim) cubeMesh = UsdGeom.Mesh(cubePrim) self.assertTrue(cubeMesh) meshDisplayColors = cubeMesh.GetDisplayColorPrimvar().Get() self.assertEqual(len(meshDisplayColors), 1) self.assertTrue(Gf.IsClose(meshDisplayColors[0], self.RED_COLOR, 1e-6)) # Validate the Material prim bound to the Mesh prim. material = UsdShade.Material.GetBoundMaterial(cubePrim) self.assertTrue(material) materialPath = material.GetPath().pathString self.assertEqual(materialPath, '/RedCube/Looks/RedLambertSG') materialInputs = material.GetInputs() self.assertEqual(len(materialInputs), 3) materialInput = material.GetInput('displayColor') matDisplayColor = materialInput.Get() self.assertTrue(Gf.IsClose(matDisplayColor, self.RED_COLOR, 1e-6)) # Just verify that displayOpacity and transparency exist. materialInput = material.GetInput('displayOpacity') self.assertTrue(materialInput) materialInput = material.GetInput('transparency') self.assertTrue(materialInput) # Validate the surface shader that is connected to the material. # XXX: Note that the expected number of outputs here is two rather than # one, since we are still authoring the UsdRi Bxdf source in addition # to the surface terminal for backwards compatibility. When consumers # are updated to use the surface terminal instead, this test will have # to be updated. materialOutputs = material.GetOutputs() self.assertEqual(len(materialOutputs), 2) materialOutput = material.GetOutput('surface') (connectableAPI, outputName, outputType) = materialOutput.GetConnectedSource() self.assertEqual(outputName, 'out') shader = UsdShade.Shader(connectableAPI) self.assertTrue(shader) self.assertEqual(shader.GetPrim().GetName(), 'RedLambertSG_lambert') # XXX: Validate the UsdRi Bxdf. This must also be removed when we no # longer author it. from pxr import UsdRi usdRiMaterialAPI = UsdRi.MaterialAPI(material.GetPrim()) self.assertTrue(usdRiMaterialAPI) bxdf = usdRiMaterialAPI.GetBxdf() self.assertEqual(bxdf.GetPrim(), shader.GetPrim()) shaderId = shader.GetIdAttr().Get() self.assertEqual(shaderId, 'PxrDiffuse') shaderInputs = shader.GetInputs() self.assertEqual(len(shaderInputs), 2) diffuseInput = shader.GetInput('diffuseColor') self.assertTrue(diffuseInput) (connectableAPI, outputName, outputType) = diffuseInput.GetConnectedSource() self.assertEqual(outputName, 'displayColor') self.assertTrue(connectableAPI) self.assertEqual(connectableAPI.GetPath().pathString, materialPath) transmissionInput = shader.GetInput('transmissionColor') self.assertTrue(transmissionInput) (connectableAPI, outputName, outputType) = transmissionInput.GetConnectedSource() self.assertEqual(outputName, 'transparency') self.assertTrue(connectableAPI) self.assertEqual(connectableAPI.GetPath().pathString, materialPath)