Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update exporters for string varnames #2601

Merged
merged 5 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/mayaUsd/fileio/shading/shadingModeExporterContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <pxr/base/tf/token.h>
#include <pxr/base/vt/types.h>
#include <pxr/usd/sdf/path.h>
#include <pxr/usd/sdf/types.h>
#include <pxr/usd/usd/prim.h>
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usdGeom/scope.h>
Expand Down Expand Up @@ -627,13 +628,18 @@ class _UVMappingManager
TfStringPrintf("%s:%s", itNode->GetText(), _tokens->varname.GetText()));
UsdShadeInput materialInput = material.GetInput(TfToken(inputName.c_str()));
if (materialInput) {
materialInput.Set(*itName);
// varname becomes a std::string in USD 20.11
if (materialInput.GetTypeName() == SdfValueTypeNames->Token) {
materialInput.Set(*itName);
} else {
materialInput.Set(itName->GetString());
}
}
inputName
= TfStringPrintf("%s:%s", itNode->GetText(), _tokens->varnameStr.GetText());
materialInput = material.GetInput(TfToken(inputName.c_str()));
if (materialInput) {
materialInput.Set((*itName).GetString());
materialInput.Set(itName->GetString());
}
}
_uvNamesToMaterial[largestSet] = material;
Expand Down Expand Up @@ -677,12 +683,17 @@ class _UVMappingManager
TfStringPrintf("%s:%s", itNode->GetText(), _tokens->varname.GetText()));
UsdShadeInput materialInput = newMaterial.GetInput(TfToken(inputName.c_str()));
if (materialInput) {
materialInput.Set(*itName);
// varname becomes a std::string in USD 20.11
if (materialInput.GetTypeName() == SdfValueTypeNames->Token) {
materialInput.Set(*itName);
} else {
materialInput.Set(itName->GetString());
}
}
inputName = TfStringPrintf("%s:%s", itNode->GetText(), _tokens->varnameStr.GetText());
materialInput = newMaterial.GetInput(TfToken(inputName.c_str()));
if (materialInput) {
materialInput.Set((*itName).GetString());
materialInput.Set(itName->GetString());
}
}
auto insertResult
Expand Down
8 changes: 6 additions & 2 deletions lib/mayaUsd/fileio/translators/translatorMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ _GetUVBindingsFromMaterial(const UsdShadeMaterial& material, UsdMayaPrimReaderCo
}
VtValue val;
usdAttr.Get(&val);
if (!val.IsHolding<TfToken>()) {
if (!val.IsHolding<TfToken>() && !val.IsHolding<std::string>()) {
continue;
}
SdfPath nodePath = isMergeable
Expand All @@ -200,7 +200,11 @@ _GetUVBindingsFromMaterial(const UsdShadeMaterial& material, UsdMayaPrimReaderCo
if (!status) {
continue;
}
retVal[val.UncheckedGet<TfToken>()] = TfToken(depFn.name().asChar());
if (val.IsHolding<TfToken>()) {
retVal[val.UncheckedGet<TfToken>()] = TfToken(depFn.name().asChar());
} else {
retVal[TfToken(val.UncheckedGet<std::string>())] = TfToken(depFn.name().asChar());
}
}

return retVal;
Expand Down
17 changes: 16 additions & 1 deletion lib/mayaUsd/fileio/utils/shadingUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <pxr/base/tf/token.h>
#include <pxr/pxr.h>
#include <pxr/usd/sdf/valueTypeName.h>
#include <pxr/usd/sdr/registry.h>
#include <pxr/usd/sdr/shaderNode.h>
#include <pxr/usd/usdShade/input.h>
#include <pxr/usd/usdShade/material.h>
#include <pxr/usd/usdShade/output.h>
Expand Down Expand Up @@ -147,7 +149,20 @@ UsdShadeOutput UsdMayaShadingUtil::CreateShaderOutputAndConnectMaterial(
return UsdShadeOutput();
}

UsdShadeOutput shaderOutput = shader.CreateOutput(terminalName, materialOutput.GetTypeName());
// Make sure the shading node has a registered output by that name.
SdrRegistry& registry = SdrRegistry::GetInstance();
TfToken nodeID;
shader.GetIdAttr().Get(&nodeID);
TfToken outputName = terminalName;
SdrShaderNodeConstPtr shaderNodeDef = registry.GetShaderNodeByIdentifier(nodeID);
if (shaderNodeDef) {
const NdrTokenVec& outputNames = shaderNodeDef->GetOutputNames();
if (std::find(outputNames.cbegin(), outputNames.cend(), terminalName) == outputNames.cend()
&& outputNames.size() == 1) {
outputName = outputNames.front();
}
}
UsdShadeOutput shaderOutput = shader.CreateOutput(outputName, materialOutput.GetTypeName());

UsdPrim parentPrim = shader.GetPrim().GetParent();
if (parentPrim == material.GetPrim()) {
Expand Down
65 changes: 51 additions & 14 deletions lib/usd/translators/shading/mtlxBaseWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#include <pxr/usd/sdf/path.h>
#include <pxr/usd/sdf/types.h>
#include <pxr/usd/sdf/valueTypeName.h>
#include <pxr/usd/sdr/registry.h>
#include <pxr/usd/sdr/shaderNode.h>
#include <pxr/usd/sdr/shaderProperty.h>
#include <pxr/usd/usd/timeCode.h>
#include <pxr/usd/usdShade/input.h>
#include <pxr/usd/usdShade/output.h>
Expand Down Expand Up @@ -163,12 +166,12 @@ UsdAttribute MtlxUsd_BaseWriter::ExtractChannel(size_t channelIndex, UsdAttribut
swizzleSchema.CreateInput(TrMtlxTokens->channels, SdfValueTypeNames->String)
.Set(channel, UsdTimeCode::Default());

swizzleOutput = swizzleSchema.CreateOutput(TrMtlxTokens->out, SdfValueTypeNames->Float);

TfToken swizzleID(TfStringPrintf("ND_swizzle_%s_float", materialXType.c_str()));

swizzleSchema.CreateIdAttr(VtValue(swizzleID));

swizzleOutput = swizzleSchema.CreateOutput(_GetOutputName(swizzleID), SdfValueTypeNames->Float);

return swizzleOutput;
}

Expand Down Expand Up @@ -208,6 +211,8 @@ MtlxUsd_BaseWriter::AddConstructor(UsdAttribute nodeInput, size_t channelIndex,
channelIndex = maxChannels - 1;
}

TfToken ctorID(TfStringPrintf("ND_combine%zu_%s", maxChannels, materialXType.c_str()));

UsdShadeNodeGraph nodegraphSchema(GetNodeGraph());
SdfPath nodegraphPath = nodegraphSchema.GetPath();

Expand All @@ -233,10 +238,10 @@ MtlxUsd_BaseWriter::AddConstructor(UsdAttribute nodeInput, size_t channelIndex,
// Here we must initialize the CTOR from the provided plug:
if (outputPath == nodegraphPath) {
UsdShadeOutput(nodeInput).ConnectToSource(
ctorSchema.CreateOutput(TrMtlxTokens->out, sourceType));
ctorSchema.CreateOutput(_GetOutputName(ctorID), sourceType));
} else {
UsdShadeInput(nodeInput).ConnectToSource(
ctorSchema.CreateOutput(TrMtlxTokens->out, sourceType));
ctorSchema.CreateOutput(_GetOutputName(ctorID), sourceType));
}

for (size_t i = 0; i < maxChannels; ++i) {
Expand All @@ -254,8 +259,6 @@ MtlxUsd_BaseWriter::AddConstructor(UsdAttribute nodeInput, size_t channelIndex,
}
}

TfToken ctorID(TfStringPrintf("ND_combine%zu_%s", maxChannels, materialXType.c_str()));

ctorSchema.CreateIdAttr(VtValue(ctorID));

return ctorInput;
Expand Down Expand Up @@ -405,15 +408,15 @@ MtlxUsd_BaseWriter::AddConversion(const SdfValueTypeName& destType, UsdAttribute
.Set(*channels, UsdTimeCode::Default());
}

swizzleOutput = swizzleSchema.CreateOutput(TrMtlxTokens->out, destType);

TfToken swizzleID(TfStringPrintf(
(channels->empty() ? "ND_convert_%s_%s" : "ND_swizzle_%s_%s"),
srcType->c_str(),
dstType->c_str()));

swizzleSchema.CreateIdAttr(VtValue(swizzleID));

swizzleOutput = swizzleSchema.CreateOutput(_GetOutputName(swizzleID), destType);

return swizzleOutput;
}

Expand Down Expand Up @@ -451,15 +454,15 @@ UsdAttribute MtlxUsd_BaseWriter::AddLuminance(int numChannels, UsdAttribute node
luminanceSchema.CreateIdAttr(VtValue(TrMtlxTokens->ND_luminance_color3));
luminanceSchema.CreateInput(TrMtlxTokens->in, SdfValueTypeNames->Color3f)
.ConnectToSource(UsdShadeOutput(nodeOutput));
luminanceOutput
= luminanceSchema.CreateOutput(TrMtlxTokens->out, SdfValueTypeNames->Color3f);
luminanceOutput = luminanceSchema.CreateOutput(
_GetOutputName(TrMtlxTokens->ND_luminance_color3), SdfValueTypeNames->Color3f);
break;
case 4:
luminanceSchema.CreateIdAttr(VtValue(TrMtlxTokens->ND_luminance_color4));
luminanceSchema.CreateInput(TrMtlxTokens->in, SdfValueTypeNames->Color4f)
.ConnectToSource(UsdShadeOutput(nodeOutput));
luminanceOutput
= luminanceSchema.CreateOutput(TrMtlxTokens->out, SdfValueTypeNames->Color4f);
luminanceOutput = luminanceSchema.CreateOutput(
_GetOutputName(TrMtlxTokens->ND_luminance_color4), SdfValueTypeNames->Color4f);
break;
default: TF_CODING_ERROR("Unsupported format for luminance"); return UsdAttribute();
}
Expand Down Expand Up @@ -492,8 +495,8 @@ UsdAttribute MtlxUsd_BaseWriter::AddNormalMapping(UsdAttribute normalInput)
UsdShadeShader nodeSchema = UsdShadeShader::Define(GetUsdStage(), nodePath);
nodeSchema.CreateIdAttr(VtValue(TrMtlxTokens->ND_normalmap));
UsdShadeInput mapInput = nodeSchema.CreateInput(TrMtlxTokens->in, SdfValueTypeNames->Float3);
UsdShadeOutput mapOutput
= nodeSchema.CreateOutput(TrMtlxTokens->out, SdfValueTypeNames->Float3);
UsdShadeOutput mapOutput = nodeSchema.CreateOutput(
_GetOutputName(TrMtlxTokens->ND_normalmap), SdfValueTypeNames->Float3);
UsdShadeOutput(normalInput).ConnectToSource(UsdShadeOutput(mapOutput));

return mapInput;
Expand All @@ -517,6 +520,40 @@ UsdAttribute MtlxUsd_BaseWriter::PreserveNodegraphBoundaries(UsdAttribute input)
return input;
}

TfToken MtlxUsd_BaseWriter::_GetOutputName(const TfToken& nodeID)
{
SdrRegistry& registry = SdrRegistry::GetInstance();
SdrShaderNodeConstPtr shaderNodeDef = registry.GetShaderNodeByIdentifier(nodeID);
if (shaderNodeDef) {
const NdrTokenVec& outputNames = shaderNodeDef->GetOutputNames();
if (!outputNames.empty()) {
return outputNames.front();
}
}
// If MaterialX nodes are not registered, return "out" as default. Correct 99% of the time.
return TrMtlxTokens->out;
}

TfToken MtlxUsd_BaseWriter::_GetVarnameName()
{
static TfToken _varnameName;
if (_varnameName.IsEmpty()) {
// UsdPrimvarReaders varname input went from TfToken to std::string in USD 20.11. Fetch the
// type directly from the registry:
SdrRegistry& registry = SdrRegistry::GetInstance();
SdrShaderNodeConstPtr shaderNodeDef
= registry.GetShaderNodeByIdentifier(TrUsdTokens->UsdPrimvarReader_float2);
SdfValueTypeName varnameType = shaderNodeDef
? shaderNodeDef->GetShaderInput(TrUsdTokens->varname)->GetTypeAsSdfType().first
: SdfValueTypeNames->Token;

// If UsdPrimvarReaders use string varnames, then we do not need to use varnameStr anymore.
_varnameName = varnameType == SdfValueTypeNames->String ? TrUsdTokens->varname
: TrMtlxTokens->varnameStr;
}
return _varnameName;
}

bool MtlxUsd_BaseWriter::AuthorShaderInputFromShadingNodeAttr(
const MFnDependencyNode& depNodeFn,
const TfToken& shadingNodeAttrName,
Expand Down
6 changes: 6 additions & 0 deletions lib/usd/translators/shading/mtlxBaseWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ class MtlxUsd_BaseWriter : public UsdMayaShaderWriter
// to subgraph nodes:
UsdAttribute PreserveNodegraphBoundaries(UsdAttribute input);

// Get the official output name of a MaterialX node using Sdr information.
static TfToken _GetOutputName(const TfToken& nodeID);

// Get the global MaterialX name we use for primvar name inputs. Depends on UsdPrimvarReader.
static TfToken _GetVarnameName();

/// Adds a schema attribute to the schema \p shaderSchema if the Maya attribute \p
/// shadingNodeAttrName in dependency node \p depNodeFn has been modified or has an incoming
/// connection at \p usdTime.
Expand Down
9 changes: 5 additions & 4 deletions lib/usd/translators/shading/mtlxFileTextureWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ MtlxUsd_FileWriter::MtlxUsd_FileWriter(
default: TF_CODING_ERROR("Unsupported format"); return;
}
texSchema.CreateInput(TrMtlxTokens->filtertype, SdfValueTypeNames->String).Set("cubic");
UsdShadeOutput colorOutput = texSchema.CreateOutput(TrMtlxTokens->out, _outputDataType);
UsdShadeOutput colorOutput
= texSchema.CreateOutput(_GetOutputName(TrMtlxTokens->ND_image_color3), _outputDataType);

// The color correction section of a fileTexture node exists on a separate node that post
// processes the values of the MaterialX image node, which is kept visible to allow DCC to
Expand Down Expand Up @@ -218,7 +219,7 @@ MtlxUsd_FileWriter::MtlxUsd_FileWriter(
= primvarReaderSchema.CreateInput(TrMtlxTokens->geomprop, SdfValueTypeNames->String);

TfToken inputName(
TfStringPrintf("%s:%s", depNodeFn.name().asChar(), TrMtlxTokens->varnameStr.GetText()));
TfStringPrintf("%s:%s", depNodeFn.name().asChar(), _GetVarnameName().GetText()));

// We expose the primvar reader varnameStr attribute to the material to allow
// easy specialization based on UV mappings to geometries:
Expand Down Expand Up @@ -246,8 +247,8 @@ MtlxUsd_FileWriter::MtlxUsd_FileWriter(
varnameInput.Set(UsdUtilsGetPrimaryUVSetName());
}

primvarReaderOutput
= primvarReaderSchema.CreateOutput(TrMtlxTokens->out, SdfValueTypeNames->Float2);
primvarReaderOutput = primvarReaderSchema.CreateOutput(
_GetOutputName(TrMtlxTokens->ND_geompropvalue_vector2), SdfValueTypeNames->Float2);
} else {
// Re-using an existing primvar reader:
UsdShadeShader primvarReaderShaderSchema(GetUsdStage()->GetPrimAtPath(primvarReaderPath));
Expand Down
12 changes: 6 additions & 6 deletions lib/usd/translators/shading/mtlxPlace2dTextureWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ MtlxUsd_Place2dTextureWriter::MtlxUsd_Place2dTextureWriter(
} else {
// Just install a reader to save space.
p2dTexSchema.CreateIdAttr(VtValue(TrMtlxTokens->ND_geompropvalue_vector2));
p2dTexSchema.CreateOutput(TrMtlxTokens->out, SdfValueTypeNames->Float2);
p2dTexSchema.CreateOutput(
_GetOutputName(TrMtlxTokens->ND_geompropvalue_vector2), SdfValueTypeNames->Float2);
_ConnectVarnameInput(p2dTexSchema);
return;
}
Expand All @@ -158,8 +159,8 @@ MtlxUsd_Place2dTextureWriter::MtlxUsd_Place2dTextureWriter(

_ConnectVarnameInput(primvarReaderSchema);

UsdShadeOutput primvarReaderOutput
= primvarReaderSchema.CreateOutput(TrMtlxTokens->out, SdfValueTypeNames->Float2);
UsdShadeOutput primvarReaderOutput = primvarReaderSchema.CreateOutput(
_GetOutputName(TrMtlxTokens->ND_geompropvalue_vector2), SdfValueTypeNames->Float2);

// Connect the output of the primvar reader to the texture coordinate
// input of the UV texture.
Expand Down Expand Up @@ -198,10 +199,9 @@ void MtlxUsd_Place2dTextureWriter::_ConnectVarnameInput(UsdShadeShader& primvarR

UsdShadeInput varnameInput
= primvarReaderSchema.CreateInput(TrMtlxTokens->geomprop, SdfValueTypeNames->String);
TfToken inputName(
TfStringPrintf("%s:%s", fileNodeName.c_str(), TrMtlxTokens->varnameStr.GetText()));
TfToken inputName(TfStringPrintf("%s:%s", fileNodeName.c_str(), _GetVarnameName().GetText()));

// We expose the primvar reader varnameStr attribute to the material to allow
// We expose the primvar reader varname attribute to the material to allow
// easy specialization based on UV mappings to geometries:
UsdPrim materialPrim = primvarReaderSchema.GetPrim().GetParent();
UsdShadeMaterial materialSchema(materialPrim);
Expand Down
6 changes: 4 additions & 2 deletions lib/usd/translators/shading/mtlxPreviewSurfaceWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ MtlxUsd_PreviewSurfaceWriter::MtlxUsd_PreviewSurfaceWriter(
}

// Surface Output
shaderSchema.CreateOutput(UsdShadeTokens->surface, SdfValueTypeNames->Token);
shaderSchema.CreateOutput(
_GetOutputName(TrMtlxTokens->ND_UsdPreviewSurface_surfaceshader), SdfValueTypeNames->Token);
}

/* override */
Expand Down Expand Up @@ -192,7 +193,8 @@ UsdAttribute MtlxUsd_PreviewSurfaceWriter::GetShadingAttributeForMayaAttrName(
}

// Surface output is on the shader itself
return surfaceSchema.GetOutput(UsdShadeTokens->surface);
return surfaceSchema.GetOutput(
_GetOutputName(TrMtlxTokens->ND_UsdPreviewSurface_surfaceshader));
}

// All other are outputs of the NodeGraph:
Expand Down
6 changes: 4 additions & 2 deletions lib/usd/translators/shading/mtlxStandardSurfaceWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ MaterialXTranslators_StandardSurfaceWriter::MaterialXTranslators_StandardSurface
}

// Surface Output
shaderSchema.CreateOutput(UsdShadeTokens->surface, SdfValueTypeNames->Token);
shaderSchema.CreateOutput(
_GetOutputName(TrMtlxTokens->ND_standard_surface_surfaceshader), SdfValueTypeNames->Token);
}

/* override */
Expand Down Expand Up @@ -245,7 +246,8 @@ UsdAttribute MaterialXTranslators_StandardSurfaceWriter::GetShadingAttributeForM
}

// Surface output is on the shader itself
return surfaceSchema.GetOutput(UsdShadeTokens->surface);
return surfaceSchema.GetOutput(
_GetOutputName(TrMtlxTokens->ND_standard_surface_surfaceshader));
}

// All other are outputs of the NodeGraph:
Expand Down
Loading