Skip to content

Commit b6fa2b7

Browse files
committed
USD ShaderAlgo : Fix writing of SdfAssetPath and TfToken inputs
These cases occur when writing to UsdLuxLight types where the inputs already exist, and don't need to be defined by us.
1 parent f174f8d commit b6fa2b7

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

contrib/IECoreUSD/src/IECoreUSD/ShaderAlgo.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,29 @@ void writeShaderParameterValues( const IECoreScene::Shader *shader, pxr::UsdShad
255255
{
256256
for( const auto &p : shader->parametersData()->readable() )
257257
{
258-
pxr::UsdShadeInput input = usdShader.CreateInput(
259-
toUSDParameterName( p.first ),
260-
IECoreUSD::DataAlgo::valueTypeName( p.second.get() )
261-
);
258+
const pxr::TfToken usdParameterName = toUSDParameterName( p.first );
259+
pxr::UsdShadeInput input = usdShader.GetInput( usdParameterName );
260+
if( !input )
261+
{
262+
input = usdShader.CreateInput(
263+
toUSDParameterName( p.first ),
264+
IECoreUSD::DataAlgo::valueTypeName( p.second.get() )
265+
);
266+
}
267+
if( auto *s = IECore::runTimeCast<IECore::StringData>( p.second.get() ) )
268+
{
269+
// USD has several "stringy" types - convert if necessary.
270+
if( input.GetTypeName() == pxr::SdfValueTypeNames->Token )
271+
{
272+
input.Set( pxr::TfToken( s->readable() ) );
273+
continue;
274+
}
275+
else if( input.GetTypeName().GetType().IsA<pxr::SdfAssetPath>() )
276+
{
277+
input.Set( pxr::SdfAssetPath( s->readable() ) );
278+
continue;
279+
}
280+
}
262281
input.Set( IECoreUSD::DataAlgo::toUSD( p.second.get() ) );
263282
}
264283

contrib/IECoreUSD/test/IECoreUSD/USDSceneTest.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3444,6 +3444,32 @@ def testWriteLightWithInputNetwork( self ) :
34443444

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

3447+
def testWriteDomeLightFile( self ) :
3448+
3449+
# Write to USD
3450+
3451+
fileName = os.path.join( self.temporaryDirectory(), "pointInstancePrimvars.usda" )
3452+
scene = IECoreScene.SceneInterface.create( fileName, IECore.IndexedIO.OpenMode.Write )
3453+
3454+
lightNetwork = IECoreScene.ShaderNetwork(
3455+
shaders = {
3456+
"output" : IECoreScene.Shader( "DomeLight", "light", { "texture:file" : "test.exr", "texture:format" : "latlong" } ),
3457+
},
3458+
output = "output",
3459+
)
3460+
3461+
light = scene.createChild( "light" )
3462+
light.writeAttribute( "light", lightNetwork, 0 )
3463+
3464+
del light, scene
3465+
3466+
# Verify via USD API
3467+
3468+
stage = pxr.Usd.Stage.Open( fileName )
3469+
light = pxr.UsdLux.DomeLight( stage.GetPrimAtPath( "/light" ) )
3470+
self.assertEqual( light.GetTextureFileAttr().Get(), "test.exr" )
3471+
self.assertEqual( light.GetTextureFormatAttr().Get(), "latlong" )
3472+
34473473
def testPointInstancerPrimvars( self ) :
34483474

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

0 commit comments

Comments
 (0)