Skip to content

Commit 0577ecc

Browse files
IECoreUSD::ShaderAlgo : Don't use shader type prefix for Rman
1 parent d445260 commit 0577ecc

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

contrib/IECoreUSD/include/IECoreUSD/ShaderAlgo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ namespace ShaderAlgo
5454
{
5555

5656
/// Write ShaderNetwork to USD, placing the shaders under the Prim `shaderContainer`
57+
IECOREUSD_API pxr::UsdShadeOutput writeShaderNetwork( const IECoreScene::ShaderNetwork *shaderNetwork, pxr::UsdPrim shaderContainer, const std::string &shaderNetworkType );
58+
59+
// \todo : When we can break binary compatibility, this should replaced by providing a default empty value to the last argument of the function above.
5760
IECOREUSD_API pxr::UsdShadeOutput writeShaderNetwork( const IECoreScene::ShaderNetwork *shaderNetwork, pxr::UsdPrim shaderContainer );
5861

5962
/// Reads a ShaderNetwork from a material output, typically obtained from `UsdShadeMaterial::GetOutput()`.
@@ -65,6 +68,8 @@ bool canReadShaderNetwork( const pxr::UsdShadeOutput &output );
6568

6669
#if PXR_VERSION >= 2111
6770
/// Writes a UsdLuxLight from a shader network.
71+
// TODO - if adding shaderNetworkType to writeShaderNetwork is actually the right approach, then it should
72+
// probably be done here too.
6873
IECOREUSD_API void writeLight( const IECoreScene::ShaderNetwork *shaderNetwork, pxr::UsdPrim prim );
6974
/// Reads a ShaderNetwork from a light.
7075
IECOREUSD_API IECoreScene::ShaderNetworkPtr readLight( const pxr::UsdLuxLightAPI &light );

contrib/IECoreUSD/src/IECoreUSD/ShaderAlgo.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,22 +302,40 @@ IECoreScene::ConstShaderNetworkPtr adaptShaderNetworkForWriting( const IECoreSce
302302
return result;
303303
}
304304

305-
pxr::UsdShadeConnectableAPI createShaderPrim( const IECoreScene::Shader *shader, const pxr::UsdStagePtr &stage, const pxr::SdfPath &path )
305+
pxr::UsdShadeConnectableAPI createShaderPrim( const IECoreScene::Shader *shader, const pxr::UsdStagePtr &stage, const pxr::SdfPath &path, const std::string &shaderNetworkType )
306306
{
307307
pxr::UsdShadeShader usdShader = pxr::UsdShadeShader::Define( stage, path );
308308
if( !usdShader )
309309
{
310310
throw IECore::Exception( "Could not create shader at " + path.GetAsString() );
311311
}
312+
312313
const std::string type = shader->getType();
314+
315+
bool exportTypePrefixes = false;
316+
317+
if( shaderNetworkType == "arnold" )
318+
{
319+
exportTypePrefixes = true;
320+
}
321+
else if( shaderNetworkType != "ri" )
322+
{
323+
// \todo : This is currently here just for backwards compatibility. In the long run
324+
// we should probably remove this, and only use shader type prefixes for Arnold, which requires them.
325+
exportTypePrefixes = true;
326+
}
327+
313328
std::string typePrefix;
314-
size_t typeColonPos = type.find( ":" );
315-
if( typeColonPos != std::string::npos )
329+
if( exportTypePrefixes )
316330
{
317-
typePrefix = type.substr( 0, typeColonPos ) + ":";
318-
if( typePrefix == "ai:" )
331+
size_t typeColonPos = type.find( ":" );
332+
if( typeColonPos != std::string::npos )
319333
{
320-
typePrefix = "arnold:";
334+
typePrefix = type.substr( 0, typeColonPos ) + ":";
335+
if( typePrefix == "ai:" )
336+
{
337+
typePrefix = "arnold:";
338+
}
321339
}
322340
}
323341
usdShader.SetShaderId( pxr::TfToken( typePrefix + shader->getName() ) );
@@ -404,6 +422,11 @@ void writeShaderConnections( const IECoreScene::ShaderNetwork *shaderNetwork, co
404422
} // namespace
405423

406424
pxr::UsdShadeOutput IECoreUSD::ShaderAlgo::writeShaderNetwork( const IECoreScene::ShaderNetwork *shaderNetwork, pxr::UsdPrim shaderContainer )
425+
{
426+
return writeShaderNetwork( shaderNetwork, shaderContainer, "" );
427+
}
428+
429+
pxr::UsdShadeOutput IECoreUSD::ShaderAlgo::writeShaderNetwork( const IECoreScene::ShaderNetwork *shaderNetwork, pxr::UsdPrim shaderContainer, const std::string &shaderNetworkType )
407430
{
408431
IECoreScene::ConstShaderNetworkPtr adaptedNetwork = adaptShaderNetworkForWriting( shaderNetwork );
409432
shaderNetwork = adaptedNetwork.get();
@@ -423,7 +446,7 @@ pxr::UsdShadeOutput IECoreUSD::ShaderAlgo::writeShaderNetwork( const IECoreScene
423446
for( const auto &shader : shaderNetwork->shaders() )
424447
{
425448
const pxr::SdfPath usdShaderPath = shaderContainer.GetPath().AppendChild( pxr::TfToken( pxr::TfMakeValidIdentifier( shader.first.string() ) ) );
426-
pxr::UsdShadeConnectableAPI usdShader = createShaderPrim( shader.second.get(), shaderContainer.GetStage(), usdShaderPath );
449+
pxr::UsdShadeConnectableAPI usdShader = createShaderPrim( shader.second.get(), shaderContainer.GetStage(), usdShaderPath, shaderNetworkType );
427450
writeShaderParameterValues( shader.second.get(), usdShader );
428451
usdShaders[shader.first] = usdShader;
429452

@@ -559,7 +582,7 @@ void IECoreUSD::ShaderAlgo::writeLight( const IECoreScene::ShaderNetwork *shader
559582
continue;
560583
}
561584
const pxr::SdfPath usdShaderPath = prim.GetPath().AppendChild( pxr::TfToken( pxr::TfMakeValidIdentifier( shader.first.string() ) ) );
562-
pxr::UsdShadeConnectableAPI usdShader = createShaderPrim( shader.second.get(), prim.GetStage(), usdShaderPath );
585+
pxr::UsdShadeConnectableAPI usdShader = createShaderPrim( shader.second.get(), prim.GetStage(), usdShaderPath, "" );
563586
writeShaderParameterValues( shader.second.get(), usdShader );
564587
usdShaders[shader.first] = usdShader;
565588
}

contrib/IECoreUSD/src/IECoreUSD/USDScene.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,16 @@ void populateMaterial( pxr::UsdShadeMaterial &mat, const boost::container::flat_
500500

501501
std::string shaderContainerName = boost::replace_all_copy( output.GetString(), ":", "_" ) + "_shaders";
502502
pxr::UsdGeomScope shaderContainer = pxr::UsdGeomScope::Define( mat.GetPrim().GetStage(), mat.GetPath().AppendChild( pxr::TfToken( shaderContainerName ) ) );
503-
pxr::UsdShadeOutput networkOut = ShaderAlgo::writeShaderNetwork( shaderNetwork.get(), shaderContainer.GetPrim() );
503+
504+
std::string outputString = output.GetString();
505+
size_t outputColonPos = outputString.find( ":" );
506+
std::string shaderNetworkType = "";
507+
if( outputColonPos != std::string::npos )
508+
{
509+
shaderNetworkType = outputString.substr( 0, outputColonPos );
510+
}
511+
512+
pxr::UsdShadeOutput networkOut = ShaderAlgo::writeShaderNetwork( shaderNetwork.get(), shaderContainer.GetPrim(), shaderNetworkType );
504513

505514
if( networkOut.GetPrim().IsValid() )
506515
{

0 commit comments

Comments
 (0)