Skip to content

Commit

Permalink
Merge pull request #1790 from seando-adsk/adsk/fix_materialx_vector3_…
Browse files Browse the repository at this point in the history
…inputs

Improve discovery of MaterialX vector3 inputs

(Internal change: 2218521)
  • Loading branch information
pixar-oss committed Mar 4, 2022
2 parents def802b + b598c7e commit 20a9fe4
Showing 1 changed file with 18 additions and 54 deletions.
72 changes: 18 additions & 54 deletions pxr/imaging/hdMtlx/hdMtlx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,6 @@ _GetMxNodeType(mx::DocumentPtr const& mxDoc, TfToken const& hdNodeType)
}
}

// Determine if the given mxInputName is of type mx::Vector3
// Hd stores both mx::Vector3 and mx::Color3 as a GlfVec3f
static bool
_IsInputVector3(std::string const& mxInputName)
{
// mxInputs from UsdPreviewSurface and standard_surface nodes that are
// Vector3 types
static const mx::StringSet Vector3Inputs = {"normal",
"coat_normal",
"tangent"};
return Vector3Inputs.count(mxInputName) > 0;
}

// Add the mxNode to the mxNodeGraph, or get the mxNode from the NodeGraph
static mx::NodePtr
_AddNodeToNodeGraph(
Expand Down Expand Up @@ -224,7 +211,7 @@ _AddMaterialXNode(
// Get the MaterialX Parameter info
const std::string &mxInputName = paramName.GetString();
std::string mxInputType;
mx::InputPtr mxInput = mxNodeDef->getInput(mxInputName);
mx::InputPtr mxInput = mxNodeDef->getActiveInput(mxInputName);
if (mxInput) {
mxInputType = mxInput->getType();
}
Expand Down Expand Up @@ -427,48 +414,25 @@ _AddParameterInputsToTerminalNode(
{
TfTokenVector paramNames =
netInterface->GetAuthoredNodeParameterNames(terminalNodeName);


mx::NodeDefPtr mxNodeDef = mxShaderNode->getNodeDef();
if (!mxNodeDef){
TF_WARN("NodeDef not found for Node '%s'", mxType.GetText());
return;
}

for (TfToken const &paramName : paramNames) {
const std::string & mxInputName = paramName.GetString();
mx::InputPtr mxInput = mxShaderNode->addInput(mxInputName);

// Convert the parameter to the appropriate MaterialX input format
VtValue hdParamValue = netInterface->GetNodeParameterValue(
terminalNodeName, paramName);

if (hdParamValue.IsHolding<bool>()) {
bool value = hdParamValue.UncheckedGet<bool>();
mxInput->setValue(value);
}
else if (hdParamValue.IsHolding<int>()) {
int value = hdParamValue.UncheckedGet<int>();
mxInput->setValue(value);
}
else if (hdParamValue.IsHolding<float>()) {
float value = hdParamValue.UncheckedGet<float>();
mxInput->setValue(value);
}
else if (hdParamValue.IsHolding<GfVec3f>()) {

const GfVec3f & value = hdParamValue.UncheckedGet<GfVec3f>();
// Check if the parameter is a mx::vector3 or mx::color3
if (_IsInputVector3(mxInputName)) {
mxInput->setValue(mx::Vector3(value.data()[0],
value.data()[1],
value.data()[2]));
}
else {
mxInput->setValue(mx::Color3(value.data()[0],
value.data()[1],
value.data()[2]));
}
}
else {
mxShaderNode->removeInput(mxInputName);
TF_WARN("Unsupported Input Type '%s' for mxNode '%s' of type '%s'",
hdParamValue.GetTypeName().c_str(), mxInputName.c_str(),
mxType.GetText());
// Get the MaterialX Parameter info
const std::string &mxInputName = paramName.GetString();
std::string mxInputType;
mx::InputPtr mxInput = mxNodeDef->getActiveInput(mxInputName);
if (mxInput) {
mxInputType = mxInput->getType();
}
std::string mxInputValue = HdMtlxConvertToString(
netInterface->GetNodeParameterValue(terminalNodeName, paramName));

mxShaderNode->setInputValue(mxInputName, mxInputValue, mxInputType);
}
}

Expand Down

0 comments on commit 20a9fe4

Please sign in to comment.