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

EMSUSD-1896 avoid crash when shader has no uvLink #4136

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 31 additions & 8 deletions lib/mayaUsd/fileio/shading/shadingModeExporterContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,19 +612,26 @@ class _UVMappingManager
// See lib\usd\translators\shading\usdFileTextureWriter.cpp for an example of an exporter
// declaring UV inputs that require linking.
for (const UsdShadeInput& input : material.GetInputs()) {
// Split the input name along ':' (that is what SplitName does)
// and if the last element is "varname" or "varnameStr" then
// the Maya node name is added to _nodesWithUVInput. (The Maya
// node name is the middle part of the split name.)
const UsdAttribute& usdAttr = input.GetAttr();
std::vector<std::string> splitName = usdAttr.SplitName();
if (splitName.back() != _tokens->varname.GetString()
&& splitName.back() != _tokens->varnameStr.GetString()) {
continue;
}

// Maya node names can be inside a Maya namespace. The Maya namespace
// separator is *also* ':'. So we need to check if the split name
// has more than 3 parts, and if so, we need to concatenate the parts.
switch (splitName.size()) {
case 3: _nodesWithUVInput.push_back(TfToken(splitName[1])); break;
default
: // NOTE: (yliangsiew) Means that we have a Maya node with a namespace/multiple
// namespaces.
{
case 3: {
_nodesWithUVInput.push_back(TfToken(splitName[1]));
break;
}
default: {
std::string mayaNodeName = splitName[1];
for (size_t i = 2; i < splitName.size() - 1; ++i) {
mayaNodeName += ":" + splitName[i];
Expand All @@ -645,12 +652,26 @@ class _UVMappingManager
}

// Ask Maya about UV linkage:
for (const TfToken& nodeName : _nodesWithUVInput) {
MString uvLinkCmd;

for (size_t i = 0; i < _nodesWithUVInput.size(); ++i) {
const TfToken& nodeName = _nodesWithUVInput[i];
MString uvLinkCmd;
uvLinkCmd.format(
"stringArrayToString(`uvLink -q -t \"^1s\"`, \" \");", nodeName.GetText());
std::string uvLinkResult = MGlobal::executeCommandStringResult(uvLinkCmd).asChar();
for (std::string uvSetRef : TfStringTokenize(uvLinkResult)) {
const std::vector<std::string> uvSets = TfStringTokenize(uvLinkResult);
// If there are not UV sets, then the node has no valid UV input we must remove it.
// Otherwise, assumptions in the code below won't be satisfied.
if (uvSets.empty()) {
TF_WARN(
"Could not determine the UV link of the UV input of the node \"%s\".",
nodeName.GetText());
_nodesWithUVInput.erase(_nodesWithUVInput.begin() + i);
--i;
continue;
}

for (std::string uvSetRef : uvSets) {
// NOTE: If the mesh shape has the same name as the transform, then we will get a
// complete path like
// |mesh|mesh.uvSet[0].uvSetName
Expand Down Expand Up @@ -706,6 +727,8 @@ class _UVMappingManager

// Update the original material with the most common mapping:
if (largestSize) {
// TODO: why is it assumed that the largest set has the same size as the nodes?
// Could the UV sets not be disjoint?
TfTokenVector::const_iterator itNode = _nodesWithUVInput.cbegin();
TfTokenVector::const_iterator itName = largestSet.cbegin();
for (; itNode != _nodesWithUVInput.cend(); ++itNode, ++itName) {
Expand Down
1 change: 1 addition & 0 deletions test/lib/usd/translators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(TEST_SCRIPT_FILES
testUsdExportTypes.py
testUsdExportUnits.py
testUsdExportUpAxis.py
testUsdExportUVSetsSwitch.py

# To investigate: following test asserts in MFnParticleSystem, but passes.
# PPT, 17-Jun-20.
Expand Down
Loading
Loading