Skip to content

Commit 153ee11

Browse files
authored
Fix enumeration interfacename bug (#2423)
* Fix case where an enumerated input is driven by an interfacename would cause an invalid type error * Add a unit test to catch any future regression
1 parent 65871aa commit 153ee11

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0"?>
2+
<materialx version="1.39">
3+
4+
<!-- create a custom node that exposes an enumeration input via "interfacename" -->
5+
<nodedef name="ND_positionwrapper" node="positionwrapper">
6+
<input name="wrap_space" type="string" value="object" enum="model, object, world" uniform="true"/>
7+
<output name="out" type="vector3"/>
8+
</nodedef>
9+
<nodegraph name="NG_positionwrapper" nodedef="ND_positionwrapper">
10+
<position name="myPos" type="vector3">
11+
<input name="space" type="string" interfacename="wrap_space"/>
12+
</position>
13+
<output name="out" type="vector3" nodename="myPos"/>
14+
</nodegraph>
15+
16+
<positionwrapper name="myPosWrapper" type="vector3">
17+
<input name="wrap_space" type="string" value="object"/>
18+
</positionwrapper>
19+
20+
<extract name="extract" type="float">
21+
<input name="in" type="vector3" nodename="myPosWrapper"/>
22+
<input name="index" type="integer" value="1"/>
23+
</extract>
24+
<surface_unlit name="Srf" type="surfaceshader">
25+
<input name="emission" type="float" nodename="extract" />
26+
</surface_unlit>
27+
<surfacematerial name="Mtl" type="material">
28+
<input name="surfaceshader" type="surfaceshader" nodename="Srf" />
29+
</surfacematerial>
30+
</materialx>

source/MaterialXGenShader/ShaderNode.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,16 +353,21 @@ void ShaderNode::initialize(const Node& node, const NodeDef& nodeDef, GenContext
353353
}
354354
}
355355
const string& valueString = portValue ? portValue->getValueString() : EMPTY_STRING;
356-
std::pair<TypeDesc, ValuePtr> enumResult;
357-
const string& enumNames = nodeDefInput->getAttribute(ValueElement::ENUM_ATTRIBUTE);
358-
const TypeDesc type = context.getTypeDesc(nodeDefInput->getType());
359-
if (context.getShaderGenerator().getSyntax().remapEnumeration(valueString, type, enumNames, enumResult))
360-
{
361-
input->setValue(enumResult.second);
362-
}
363-
else if (!valueString.empty())
356+
if (!valueString.empty())
364357
{
365-
input->setValue(portValue);
358+
// We explicitly check the valueString is not empty before checking the enumeration,
359+
// because otherwise the enumeration value would always return nullptr
360+
std::pair<TypeDesc, ValuePtr> enumResult;
361+
const string& enumNames = nodeDefInput->getAttribute(ValueElement::ENUM_ATTRIBUTE);
362+
const TypeDesc type = context.getTypeDesc(nodeDefInput->getType());
363+
if (context.getShaderGenerator().getSyntax().remapEnumeration(valueString, type, enumNames, enumResult))
364+
{
365+
input->setValue(enumResult.second);
366+
}
367+
else
368+
{
369+
input->setValue(portValue);
370+
}
366371
}
367372
}
368373
}

0 commit comments

Comments
 (0)