Skip to content

Commit d687050

Browse files
authored
Merge pull request #1252 from johnhaddon/usdMoana
Small USD loading improvements
2 parents a11a8b7 + 4114bae commit d687050

File tree

5 files changed

+154
-68
lines changed

5 files changed

+154
-68
lines changed

contrib/IECoreUSD/src/IECoreUSD/DataAlgo.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ IECore::DataPtr dataFromArray( const pxr::VtValue &value, GeometricData::Interpr
139139
return d;
140140
}
141141

142+
IECore::DataPtr dataFromSdfAssetPath( const pxr::VtValue &value, GeometricData::Interpretation interpretation, bool arrayAccepted )
143+
{
144+
return new StringData( value.Get<SdfAssetPath>().GetResolvedPath() );
145+
}
146+
142147
static const std::map<pxr::TfType, IECore::DataPtr (*)( const pxr::VtValue &, GeometricData::Interpretation, bool )> g_fromVtValueConverters = {
143148

144149
// Numeric types
@@ -202,7 +207,8 @@ static const std::map<pxr::TfType, IECore::DataPtr (*)( const pxr::VtValue &, Ge
202207
{ TfType::Find<string>(), &dataFromValue<string> },
203208
{ TfType::Find<VtArray<string>>(), &dataFromArray<string> },
204209
{ TfType::Find<TfToken>(), &dataFromValue<TfToken> },
205-
{ TfType::Find<VtArray<TfToken>>(), &dataFromArray<TfToken> }
210+
{ TfType::Find<VtArray<TfToken>>(), &dataFromArray<TfToken> },
211+
{ TfType::Find<SdfAssetPath>(), &dataFromSdfAssetPath }
206212

207213
};
208214

contrib/IECoreUSD/src/IECoreUSD/ShaderAlgo.cpp

Lines changed: 79 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -46,95 +46,107 @@
4646

4747
namespace
4848
{
49-
pxr::TfToken g_adapterLabelToken( IECoreScene::ShaderNetworkAlgo::componentConnectionAdapterLabel().string() );
5049

51-
IECore::InternedString readShaderNetworkWalk( const pxr::SdfPath &anchorPath, const pxr::UsdShadeShader &usdShader, IECoreScene::ShaderNetwork &shaderNetwork )
52-
{
53-
IECore::InternedString handle( usdShader.GetPath().MakeRelativePath( anchorPath ).GetString() );
50+
pxr::TfToken g_adapterLabelToken( IECoreScene::ShaderNetworkAlgo::componentConnectionAdapterLabel().string() );
5451

55-
if( shaderNetwork.getShader( handle ) )
56-
{
57-
return handle;
58-
}
52+
IECore::InternedString readShaderNetworkWalk( const pxr::SdfPath &anchorPath, const pxr::UsdShadeShader &usdShader, IECoreScene::ShaderNetwork &shaderNetwork )
53+
{
54+
IECore::InternedString handle( usdShader.GetPath().MakeRelativePath( anchorPath ).GetString() );
55+
56+
if( shaderNetwork.getShader( handle ) )
57+
{
58+
return handle;
59+
}
5960

60-
pxr::TfToken id;
61-
std::string shaderName = "defaultsurface";
62-
std::string shaderType = "surface";
63-
if( usdShader.GetShaderId( &id ) )
61+
pxr::TfToken id;
62+
std::string shaderName = "defaultsurface";
63+
std::string shaderType = "surface";
64+
if( usdShader.GetShaderId( &id ) )
65+
{
66+
std::string name = id.GetString();
67+
size_t colonPos = name.find( ":" );
68+
if( colonPos != std::string::npos )
6469
{
65-
std::string name = id.GetString();
66-
size_t colonPos = name.find( ":" );
67-
if( colonPos != std::string::npos )
70+
std::string prefix = name.substr( 0, colonPos );
71+
name = name.substr( colonPos + 1 );
72+
if( prefix == "arnold" )
6873
{
69-
std::string prefix = name.substr( 0, colonPos );
70-
name = name.substr( colonPos + 1 );
71-
if( prefix == "arnold" )
72-
{
73-
prefix = "ai";
74-
}
75-
shaderType = prefix + ":shader";
74+
prefix = "ai";
7675
}
77-
shaderName = name;
76+
shaderType = prefix + ":shader";
7877
}
78+
shaderName = name;
79+
}
7980

80-
IECore::CompoundDataPtr parametersData = new IECore::CompoundData();
81-
IECore::CompoundDataMap &parameters = parametersData->writable();
82-
std::vector< std::tuple< IECore::InternedString, pxr::UsdShadeConnectableAPI, IECore::InternedString > > connections;
83-
std::vector< pxr::UsdShadeInput > inputs = usdShader.GetInputs();
84-
for( pxr::UsdShadeInput &i : usdShader.GetInputs() )
85-
{
86-
pxr::UsdShadeConnectableAPI usdSource;
87-
pxr::TfToken usdSourceName;
88-
pxr::UsdShadeAttributeType usdSourceType;
81+
IECore::CompoundDataPtr parametersData = new IECore::CompoundData();
82+
IECore::CompoundDataMap &parameters = parametersData->writable();
83+
std::vector< std::tuple< IECore::InternedString, pxr::UsdShadeConnectableAPI, IECore::InternedString > > connections;
84+
std::vector< pxr::UsdShadeInput > inputs = usdShader.GetInputs();
85+
for( pxr::UsdShadeInput &i : usdShader.GetInputs() )
86+
{
87+
pxr::UsdShadeConnectableAPI usdSource;
88+
pxr::TfToken usdSourceName;
89+
pxr::UsdShadeAttributeType usdSourceType;
8990

90-
if( IECore::DataPtr d = IECoreUSD::DataAlgo::fromUSD( pxr::UsdAttribute( i ) ) )
91+
pxr::UsdAttribute valueAttribute = i;
92+
if( i.GetConnectedSource( &usdSource, &usdSourceName, &usdSourceType ) )
93+
{
94+
if( !usdSource.IsContainer() )
9195
{
92-
parameters[ i.GetBaseName().GetString() ] = d;
96+
connections.push_back( { i.GetBaseName().GetString(), usdSource, usdSourceName.GetString() } );
9397
}
94-
95-
if( i.GetConnectedSource( &usdSource, &usdSourceName, &usdSourceType ) )
98+
else
9699
{
97-
connections.push_back( { i.GetBaseName().GetString(), usdSource, usdSourceName.GetString() } );
100+
// Connected to an exposed input on the material container. We don't
101+
// have an equivalent in IECoreScene::ShaderNetwork yet, so just take
102+
// the parameter value from the exposed input.
103+
valueAttribute = usdSource.GetInput( usdSourceName );
98104
}
99105
}
100106

101-
parametersData = boost::const_pointer_cast< IECore::CompoundData >( IECoreScene::ShaderNetworkAlgo::collapseSplineParameters( parametersData ) );
102-
103-
IECoreScene::ShaderPtr newShader = new IECoreScene::Shader( shaderName, shaderType, parametersData );
104-
pxr::VtValue metadataValue;
105-
if( usdShader.GetPrim().GetMetadata( g_adapterLabelToken, &metadataValue ) && metadataValue.Get<bool>() )
107+
if( IECore::DataPtr d = IECoreUSD::DataAlgo::fromUSD( pxr::UsdAttribute( valueAttribute ) ) )
106108
{
107-
newShader->blindData()->writable()[ IECoreScene::ShaderNetworkAlgo::componentConnectionAdapterLabel() ] = new IECore::BoolData( true );
109+
parameters[ i.GetBaseName().GetString() ] = d;
108110
}
109-
shaderNetwork.addShader( handle, std::move( newShader ) );
111+
}
110112

111-
for( const auto &c : connections )
112-
{
113-
IECore::InternedString attributeName;
114-
pxr::UsdShadeConnectableAPI usdSource;
115-
IECore::InternedString sourceAttributeName;
116-
std::tie( attributeName, usdSource, sourceAttributeName ) = c;
117-
IECore::InternedString sourceHandle = readShaderNetworkWalk( anchorPath, pxr::UsdShadeShader( usdSource.GetPrim() ), shaderNetwork );
113+
parametersData = boost::const_pointer_cast< IECore::CompoundData >( IECoreScene::ShaderNetworkAlgo::collapseSplineParameters( parametersData ) );
118114

119-
if( sourceAttributeName == "DEFAULT_OUTPUT" )
120-
{
121-
shaderNetwork.addConnection( IECoreScene::ShaderNetwork::Connection(
122-
{ sourceHandle, "" },
123-
{ handle, attributeName }
124-
) );
125-
}
126-
else
127-
{
128-
shaderNetwork.addConnection( IECoreScene::ShaderNetwork::Connection(
129-
{ sourceHandle, sourceAttributeName },
130-
{ handle, attributeName }
131-
) );
132-
}
133-
}
115+
IECoreScene::ShaderPtr newShader = new IECoreScene::Shader( shaderName, shaderType, parametersData );
116+
pxr::VtValue metadataValue;
117+
if( usdShader.GetPrim().GetMetadata( g_adapterLabelToken, &metadataValue ) && metadataValue.Get<bool>() )
118+
{
119+
newShader->blindData()->writable()[ IECoreScene::ShaderNetworkAlgo::componentConnectionAdapterLabel() ] = new IECore::BoolData( true );
120+
}
121+
shaderNetwork.addShader( handle, std::move( newShader ) );
134122

135-
return handle;
123+
for( const auto &c : connections )
124+
{
125+
IECore::InternedString attributeName;
126+
pxr::UsdShadeConnectableAPI usdSource;
127+
IECore::InternedString sourceAttributeName;
128+
std::tie( attributeName, usdSource, sourceAttributeName ) = c;
129+
IECore::InternedString sourceHandle = readShaderNetworkWalk( anchorPath, pxr::UsdShadeShader( usdSource.GetPrim() ), shaderNetwork );
130+
131+
if( sourceAttributeName == "DEFAULT_OUTPUT" )
132+
{
133+
shaderNetwork.addConnection( IECoreScene::ShaderNetwork::Connection(
134+
{ sourceHandle, "" },
135+
{ handle, attributeName }
136+
) );
137+
}
138+
else
139+
{
140+
shaderNetwork.addConnection( IECoreScene::ShaderNetwork::Connection(
141+
{ sourceHandle, sourceAttributeName },
142+
{ handle, attributeName }
143+
) );
144+
}
136145
}
137146

147+
return handle;
148+
}
149+
138150
} // namespace
139151

140152
pxr::UsdShadeOutput IECoreUSD::ShaderAlgo::writeShaderNetwork( const IECoreScene::ShaderNetwork *shaderNetwork, pxr::UsdPrim shaderContainer )

contrib/IECoreUSD/test/IECoreUSD/USDSceneTest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2938,5 +2938,34 @@ def testHoudiniVaryingLengthArrayPrimVar( self ) :
29382938
)
29392939
)
29402940

2941+
def testAssetAttributes( self ) :
2942+
2943+
root = IECoreScene.SceneInterface.create(
2944+
os.path.join( os.path.dirname( __file__ ), "data", "assetPathAttribute.usda" ),
2945+
IECore.IndexedIO.OpenMode.Read
2946+
)
2947+
xform = root.child( "xform" )
2948+
2949+
self.assertEqual( xform.attributeNames(), [ "render:testAsset" ] )
2950+
self.assertEqual(
2951+
os.path.normcase( os.path.normpath( xform.readAttribute( "render:testAsset", 0 ).value ) ),
2952+
os.path.normcase( os.path.join( os.path.dirname( __file__ ), "data", "cube.usda" ) )
2953+
)
2954+
2955+
def testExposedShaderInput( self ) :
2956+
2957+
root = IECoreScene.SceneInterface.create(
2958+
os.path.join( os.path.dirname( __file__ ), "data", "exposedShaderInput.usda" ),
2959+
IECore.IndexedIO.OpenMode.Read
2960+
)
2961+
sphere = root.child( "model" ).child( "sphere" )
2962+
2963+
self.assertEqual( sphere.attributeNames(), [ "surface" ] )
2964+
network = sphere.readAttribute( "surface", 0 )
2965+
2966+
self.assertEqual( network.size(), 1 )
2967+
self.assertEqual( network.getOutput(), "surface" )
2968+
self.assertEqual( network.getShader( "surface" ).parameters["diffuse_roughness"].value, 0.75 )
2969+
29412970
if __name__ == "__main__":
29422971
unittest.main()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#usda 1.0
2+
3+
def Xform "xform"
4+
{
5+
6+
asset primvars:testAsset = @./cube.usda@
7+
8+
}
9+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#usda 1.0
2+
3+
def "model"
4+
{
5+
6+
def Sphere "sphere"
7+
{
8+
rel material:binding = </model/materials/material1>
9+
}
10+
11+
def Scope "materials"
12+
{
13+
14+
def Material "material1"
15+
{
16+
17+
float inputs:exposedRoughness = 0.75
18+
token outputs:surface.connect = </model/materials/material1/surface.outputs:DEFAULT_OUTPUT>
19+
20+
def Shader "surface"
21+
{
22+
uniform token info:id = "arnold:standard_surface"
23+
float inputs:diffuse_roughness.connect = </model/materials/material1.inputs:exposedRoughness>
24+
token outputs:DEFAULT_OUTPUT
25+
}
26+
}
27+
28+
}
29+
30+
}

0 commit comments

Comments
 (0)