Skip to content

Commit fb419e4

Browse files
Fix MDL generation for empty materials (#2817)
Repro case: M_EmptySurface in surfacematerial_with_graph.mtlx The test driver skips such materials, but they can be explicitly requested via the API and caused invalid MDL code to be generated.
1 parent 033004e commit fb419e4

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

source/MaterialXGenMdl/MdlShaderGenerator.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ ShaderPtr MdlShaderGenerator::generate(const string& name, ElementPtr element, G
237237

238238
// Emit function calls for "root" closure/shader nodes.
239239
// These will internally emit function calls for any dependent closure nodes upstream.
240+
bool rootFunctionCallEmitted = false;
240241
for (ShaderGraphOutputSocket* socket : graph.getOutputSockets())
241242
{
242243
if (socket->getConnection())
@@ -247,6 +248,7 @@ ShaderPtr MdlShaderGenerator::generate(const string& name, ElementPtr element, G
247248
upstream->hasClassification(ShaderNode::Classification::SHADER)))
248249
{
249250
emitFunctionCall(*upstream, context, stage);
251+
rootFunctionCallEmitted = true;
250252
}
251253
}
252254
}
@@ -314,7 +316,15 @@ ShaderPtr MdlShaderGenerator::generate(const string& name, ElementPtr element, G
314316
}
315317
else
316318
{
317-
emitLine(_syntax->getTypeSyntax(outputType).getName() + " finalOutput__ = " + result, stage);
319+
if (rootFunctionCallEmitted)
320+
{
321+
emitLine(_syntax->getTypeSyntax(outputType).getName() + " finalOutput__ = " + result, stage);
322+
}
323+
else
324+
{
325+
// No code has been emitted for "result". Use default material as fallback.
326+
emitLine(_syntax->getTypeSyntax(outputType).getName() + " finalOutput__ = material()", stage);
327+
}
318328

319329
// End shader body
320330
emitScopeEnd(stage);

0 commit comments

Comments
 (0)