Skip to content

Commit

Permalink
Merge branch 'RB-10.3' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhaddon committed Mar 24, 2022
2 parents 52c07c7 + d687050 commit df5c687
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 74 deletions.
8 changes: 7 additions & 1 deletion contrib/IECoreUSD/src/IECoreUSD/DataAlgo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ IECore::DataPtr dataFromArray( const pxr::VtValue &value, GeometricData::Interpr
return d;
}

IECore::DataPtr dataFromSdfAssetPath( const pxr::VtValue &value, GeometricData::Interpretation interpretation, bool arrayAccepted )
{
return new StringData( value.Get<SdfAssetPath>().GetResolvedPath() );
}

static const std::map<pxr::TfType, IECore::DataPtr (*)( const pxr::VtValue &, GeometricData::Interpretation, bool )> g_fromVtValueConverters = {

// Numeric types
Expand Down Expand Up @@ -202,7 +207,8 @@ static const std::map<pxr::TfType, IECore::DataPtr (*)( const pxr::VtValue &, Ge
{ TfType::Find<string>(), &dataFromValue<string> },
{ TfType::Find<VtArray<string>>(), &dataFromArray<string> },
{ TfType::Find<TfToken>(), &dataFromValue<TfToken> },
{ TfType::Find<VtArray<TfToken>>(), &dataFromArray<TfToken> }
{ TfType::Find<VtArray<TfToken>>(), &dataFromArray<TfToken> },
{ TfType::Find<SdfAssetPath>(), &dataFromSdfAssetPath }

};

Expand Down
146 changes: 79 additions & 67 deletions contrib/IECoreUSD/src/IECoreUSD/ShaderAlgo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,95 +46,107 @@

namespace
{
pxr::TfToken g_adapterLabelToken( IECoreScene::ShaderNetworkAlgo::componentConnectionAdapterLabel().string() );

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

if( shaderNetwork.getShader( handle ) )
{
return handle;
}
IECore::InternedString readShaderNetworkWalk( const pxr::SdfPath &anchorPath, const pxr::UsdShadeShader &usdShader, IECoreScene::ShaderNetwork &shaderNetwork )
{
IECore::InternedString handle( usdShader.GetPath().MakeRelativePath( anchorPath ).GetString() );

if( shaderNetwork.getShader( handle ) )
{
return handle;
}

pxr::TfToken id;
std::string shaderName = "defaultsurface";
std::string shaderType = "surface";
if( usdShader.GetShaderId( &id ) )
pxr::TfToken id;
std::string shaderName = "defaultsurface";
std::string shaderType = "surface";
if( usdShader.GetShaderId( &id ) )
{
std::string name = id.GetString();
size_t colonPos = name.find( ":" );
if( colonPos != std::string::npos )
{
std::string name = id.GetString();
size_t colonPos = name.find( ":" );
if( colonPos != std::string::npos )
std::string prefix = name.substr( 0, colonPos );
name = name.substr( colonPos + 1 );
if( prefix == "arnold" )
{
std::string prefix = name.substr( 0, colonPos );
name = name.substr( colonPos + 1 );
if( prefix == "arnold" )
{
prefix = "ai";
}
shaderType = prefix + ":shader";
prefix = "ai";
}
shaderName = name;
shaderType = prefix + ":shader";
}
shaderName = name;
}

IECore::CompoundDataPtr parametersData = new IECore::CompoundData();
IECore::CompoundDataMap &parameters = parametersData->writable();
std::vector< std::tuple< IECore::InternedString, pxr::UsdShadeConnectableAPI, IECore::InternedString > > connections;
std::vector< pxr::UsdShadeInput > inputs = usdShader.GetInputs();
for( pxr::UsdShadeInput &i : usdShader.GetInputs() )
{
pxr::UsdShadeConnectableAPI usdSource;
pxr::TfToken usdSourceName;
pxr::UsdShadeAttributeType usdSourceType;
IECore::CompoundDataPtr parametersData = new IECore::CompoundData();
IECore::CompoundDataMap &parameters = parametersData->writable();
std::vector< std::tuple< IECore::InternedString, pxr::UsdShadeConnectableAPI, IECore::InternedString > > connections;
std::vector< pxr::UsdShadeInput > inputs = usdShader.GetInputs();
for( pxr::UsdShadeInput &i : usdShader.GetInputs() )
{
pxr::UsdShadeConnectableAPI usdSource;
pxr::TfToken usdSourceName;
pxr::UsdShadeAttributeType usdSourceType;

if( IECore::DataPtr d = IECoreUSD::DataAlgo::fromUSD( pxr::UsdAttribute( i ) ) )
pxr::UsdAttribute valueAttribute = i;
if( i.GetConnectedSource( &usdSource, &usdSourceName, &usdSourceType ) )
{
if( !usdSource.IsContainer() )
{
parameters[ i.GetBaseName().GetString() ] = d;
connections.push_back( { i.GetBaseName().GetString(), usdSource, usdSourceName.GetString() } );
}

if( i.GetConnectedSource( &usdSource, &usdSourceName, &usdSourceType ) )
else
{
connections.push_back( { i.GetBaseName().GetString(), usdSource, usdSourceName.GetString() } );
// Connected to an exposed input on the material container. We don't
// have an equivalent in IECoreScene::ShaderNetwork yet, so just take
// the parameter value from the exposed input.
valueAttribute = usdSource.GetInput( usdSourceName );
}
}

parametersData = boost::const_pointer_cast< IECore::CompoundData >( IECoreScene::ShaderNetworkAlgo::collapseSplineParameters( parametersData ) );

IECoreScene::ShaderPtr newShader = new IECoreScene::Shader( shaderName, shaderType, parametersData );
pxr::VtValue metadataValue;
if( usdShader.GetPrim().GetMetadata( g_adapterLabelToken, &metadataValue ) && metadataValue.Get<bool>() )
if( IECore::DataPtr d = IECoreUSD::DataAlgo::fromUSD( pxr::UsdAttribute( valueAttribute ) ) )
{
newShader->blindData()->writable()[ IECoreScene::ShaderNetworkAlgo::componentConnectionAdapterLabel() ] = new IECore::BoolData( true );
parameters[ i.GetBaseName().GetString() ] = d;
}
shaderNetwork.addShader( handle, std::move( newShader ) );
}

for( const auto &c : connections )
{
IECore::InternedString attributeName;
pxr::UsdShadeConnectableAPI usdSource;
IECore::InternedString sourceAttributeName;
std::tie( attributeName, usdSource, sourceAttributeName ) = c;
IECore::InternedString sourceHandle = readShaderNetworkWalk( anchorPath, pxr::UsdShadeShader( usdSource.GetPrim() ), shaderNetwork );
parametersData = boost::const_pointer_cast< IECore::CompoundData >( IECoreScene::ShaderNetworkAlgo::collapseSplineParameters( parametersData ) );

if( sourceAttributeName == "DEFAULT_OUTPUT" )
{
shaderNetwork.addConnection( IECoreScene::ShaderNetwork::Connection(
{ sourceHandle, "" },
{ handle, attributeName }
) );
}
else
{
shaderNetwork.addConnection( IECoreScene::ShaderNetwork::Connection(
{ sourceHandle, sourceAttributeName },
{ handle, attributeName }
) );
}
}
IECoreScene::ShaderPtr newShader = new IECoreScene::Shader( shaderName, shaderType, parametersData );
pxr::VtValue metadataValue;
if( usdShader.GetPrim().GetMetadata( g_adapterLabelToken, &metadataValue ) && metadataValue.Get<bool>() )
{
newShader->blindData()->writable()[ IECoreScene::ShaderNetworkAlgo::componentConnectionAdapterLabel() ] = new IECore::BoolData( true );
}
shaderNetwork.addShader( handle, std::move( newShader ) );

return handle;
for( const auto &c : connections )
{
IECore::InternedString attributeName;
pxr::UsdShadeConnectableAPI usdSource;
IECore::InternedString sourceAttributeName;
std::tie( attributeName, usdSource, sourceAttributeName ) = c;
IECore::InternedString sourceHandle = readShaderNetworkWalk( anchorPath, pxr::UsdShadeShader( usdSource.GetPrim() ), shaderNetwork );

if( sourceAttributeName == "DEFAULT_OUTPUT" )
{
shaderNetwork.addConnection( IECoreScene::ShaderNetwork::Connection(
{ sourceHandle, "" },
{ handle, attributeName }
) );
}
else
{
shaderNetwork.addConnection( IECoreScene::ShaderNetwork::Connection(
{ sourceHandle, sourceAttributeName },
{ handle, attributeName }
) );
}
}

return handle;
}

} // namespace

pxr::UsdShadeOutput IECoreUSD::ShaderAlgo::writeShaderNetwork( const IECoreScene::ShaderNetwork *shaderNetwork, pxr::UsdPrim shaderContainer )
Expand Down
29 changes: 29 additions & 0 deletions contrib/IECoreUSD/test/IECoreUSD/USDSceneTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2938,5 +2938,34 @@ def testHoudiniVaryingLengthArrayPrimVar( self ) :
)
)

def testAssetAttributes( self ) :

root = IECoreScene.SceneInterface.create(
os.path.join( os.path.dirname( __file__ ), "data", "assetPathAttribute.usda" ),
IECore.IndexedIO.OpenMode.Read
)
xform = root.child( "xform" )

self.assertEqual( xform.attributeNames(), [ "render:testAsset" ] )
self.assertEqual(
os.path.normcase( os.path.normpath( xform.readAttribute( "render:testAsset", 0 ).value ) ),
os.path.normcase( os.path.join( os.path.dirname( __file__ ), "data", "cube.usda" ) )
)

def testExposedShaderInput( self ) :

root = IECoreScene.SceneInterface.create(
os.path.join( os.path.dirname( __file__ ), "data", "exposedShaderInput.usda" ),
IECore.IndexedIO.OpenMode.Read
)
sphere = root.child( "model" ).child( "sphere" )

self.assertEqual( sphere.attributeNames(), [ "surface" ] )
network = sphere.readAttribute( "surface", 0 )

self.assertEqual( network.size(), 1 )
self.assertEqual( network.getOutput(), "surface" )
self.assertEqual( network.getShader( "surface" ).parameters["diffuse_roughness"].value, 0.75 )

if __name__ == "__main__":
unittest.main()
9 changes: 9 additions & 0 deletions contrib/IECoreUSD/test/IECoreUSD/data/assetPathAttribute.usda
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#usda 1.0

def Xform "xform"
{

asset primvars:testAsset = @./cube.usda@

}

30 changes: 30 additions & 0 deletions contrib/IECoreUSD/test/IECoreUSD/data/exposedShaderInput.usda
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#usda 1.0

def "model"
{

def Sphere "sphere"
{
rel material:binding = </model/materials/material1>
}

def Scope "materials"
{

def Material "material1"
{

float inputs:exposedRoughness = 0.75
token outputs:surface.connect = </model/materials/material1/surface.outputs:DEFAULT_OUTPUT>

def Shader "surface"
{
uniform token info:id = "arnold:standard_surface"
float inputs:diffuse_roughness.connect = </model/materials/material1.inputs:exposedRoughness>
token outputs:DEFAULT_OUTPUT
}
}

}

}
19 changes: 13 additions & 6 deletions src/IECoreScene/CurvesAlgo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ namespace


// PrimitiveEvaluator only supports certain types
template< typename T > struct IsPrimitiveEvaluatableTypedData : boost::mpl::and_<
template< typename T >
struct IsPrimitiveEvaluatableTypedData : boost::mpl::and_<
TypeTraits::IsNumericBasedVectorTypedData<T>,
boost::mpl::or_<
TypeTraits::IsVec3<typename TypeTraits::VectorValueType<T>::type >,
boost::is_same< typename TypeTraits::VectorValueType<T>::type, float >,
boost::is_same< typename TypeTraits::VectorValueType<T>::type, int >,
TypeTraits::IsColor3<typename TypeTraits::VectorValueType<T>::type >
>
TypeTraits::IsVec2<typename TypeTraits::VectorValueType<T>::type>,
TypeTraits::IsVec3<typename TypeTraits::VectorValueType<T>::type>,
boost::is_same<typename TypeTraits::VectorValueType<T>::type, float>,
boost::is_same<typename TypeTraits::VectorValueType<T>::type, int>,
TypeTraits::IsColor3<typename TypeTraits::VectorValueType<T>::type>
>
> {};


Expand All @@ -69,6 +71,11 @@ static T evalPrimVar( PrimitiveEvaluator::Result *result, const PrimitiveVariabl
throw Exception( "PrimvarResamplerCache : This should never be called because of IsPrimitiveEvaluatableTypedData" );
}

template<>
Imath::V2f evalPrimVar<Imath::V2f>( PrimitiveEvaluator::Result *result, const PrimitiveVariable &primVar )
{
return result->vec2PrimVar( primVar );
}

template<>
Imath::V3f evalPrimVar<Imath::V3f>( PrimitiveEvaluator::Result *result, const PrimitiveVariable &primVar )
Expand Down
18 changes: 18 additions & 0 deletions test/IECoreScene/CurvesAlgoTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,24 @@ def testBezierCurvesFaceVaryingToVarying( self ) :

# endregion

def testV2fVaryingToVertex( self ) :

curves = IECoreScene.CurvesPrimitive(
IECore.IntVectorData( [ 4 ] ), IECore.CubicBasisf.catmullRom(), False,
IECore.V3fVectorData( [ imath.V3f( i ) for i in range( 0, 4 ) ] )
)

uv = IECoreScene.PrimitiveVariable(
IECoreScene.PrimitiveVariable.Interpolation.Varying,
IECore.V2fVectorData( [ imath.V2f( 0 ), imath.V2f( 1 ) ] )
)
curves["uv"] = uv

IECoreScene.CurvesAlgo.resamplePrimitiveVariable( curves, uv, IECoreScene.PrimitiveVariable.Interpolation.Vertex )
self.assertTrue( curves.arePrimitiveVariablesValid() )
self.assertEqual( uv.interpolation, IECoreScene.PrimitiveVariable.Interpolation.Vertex )
self.assertEqual( len( uv.data ), 4 )

class CurvesAlgoDeleteCurvesTest ( unittest.TestCase ):

def createLinearCurves(self):
Expand Down

0 comments on commit df5c687

Please sign in to comment.