Skip to content

Commit

Permalink
USD ShaderAlgo : Fix writing of SdfAssetPath and TfToken inputs
Browse files Browse the repository at this point in the history
These cases occur when writing to UsdLuxLight types where the inputs already exist, and don't need to be defined by us.
  • Loading branch information
johnhaddon committed Aug 3, 2023
1 parent f174f8d commit b6fa2b7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
27 changes: 23 additions & 4 deletions contrib/IECoreUSD/src/IECoreUSD/ShaderAlgo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,29 @@ void writeShaderParameterValues( const IECoreScene::Shader *shader, pxr::UsdShad
{
for( const auto &p : shader->parametersData()->readable() )
{
pxr::UsdShadeInput input = usdShader.CreateInput(
toUSDParameterName( p.first ),
IECoreUSD::DataAlgo::valueTypeName( p.second.get() )
);
const pxr::TfToken usdParameterName = toUSDParameterName( p.first );
pxr::UsdShadeInput input = usdShader.GetInput( usdParameterName );
if( !input )
{
input = usdShader.CreateInput(
toUSDParameterName( p.first ),
IECoreUSD::DataAlgo::valueTypeName( p.second.get() )
);
}
if( auto *s = IECore::runTimeCast<IECore::StringData>( p.second.get() ) )
{
// USD has several "stringy" types - convert if necessary.
if( input.GetTypeName() == pxr::SdfValueTypeNames->Token )
{
input.Set( pxr::TfToken( s->readable() ) );
continue;
}
else if( input.GetTypeName().GetType().IsA<pxr::SdfAssetPath>() )
{
input.Set( pxr::SdfAssetPath( s->readable() ) );
continue;
}
}
input.Set( IECoreUSD::DataAlgo::toUSD( p.second.get() ) );
}

Expand Down
26 changes: 26 additions & 0 deletions contrib/IECoreUSD/test/IECoreUSD/USDSceneTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3444,6 +3444,32 @@ def testWriteLightWithInputNetwork( self ) :

self.assertEqual( light.GetPrim().GetChildren(), [ source.GetPrim() ] )

def testWriteDomeLightFile( self ) :

# Write to USD

fileName = os.path.join( self.temporaryDirectory(), "pointInstancePrimvars.usda" )
scene = IECoreScene.SceneInterface.create( fileName, IECore.IndexedIO.OpenMode.Write )

lightNetwork = IECoreScene.ShaderNetwork(
shaders = {
"output" : IECoreScene.Shader( "DomeLight", "light", { "texture:file" : "test.exr", "texture:format" : "latlong" } ),
},
output = "output",
)

light = scene.createChild( "light" )
light.writeAttribute( "light", lightNetwork, 0 )

del light, scene

# Verify via USD API

stage = pxr.Usd.Stage.Open( fileName )
light = pxr.UsdLux.DomeLight( stage.GetPrimAtPath( "/light" ) )
self.assertEqual( light.GetTextureFileAttr().Get(), "test.exr" )
self.assertEqual( light.GetTextureFormatAttr().Get(), "latlong" )

def testPointInstancerPrimvars( self ) :

# Use the USD API to author a point instancer with primvars on it.
Expand Down

0 comments on commit b6fa2b7

Please sign in to comment.