Skip to content

Commit

Permalink
Merge pull request #2743 from Autodesk/bailp/MAYA-126367/invalid-prim…
Browse files Browse the repository at this point in the history
…-variant-selection

MAYA-126367 issue error message for invalid variant selection
  • Loading branch information
neilh-adsk authored Nov 29, 2022
2 parents 7860750 + 3d5100e commit 7c4c775
Showing 1 changed file with 55 additions and 6 deletions.
61 changes: 55 additions & 6 deletions lib/mayaUsd/fileio/jobs/readJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,54 @@ struct AutoTimelineRestore
const MTime originalMaxTime;
};

bool IsValidVariant(const UsdVariantSet& varSet)
{
if (!varSet.IsValid())
return false;

if (varSet.GetVariantNames().size() == 0)
return false;

return true;
}

bool IsValidVariantSelection(const UsdVariantSet& varSet, const std::string& selection)
{
const std::vector<std::string> names = varSet.GetVariantNames();
if (std::find(names.begin(), names.end(), selection) == names.end())
return false;

return true;
}

void ApplyVariantSelections(const UsdPrim& prim, const SdfVariantSelectionMap& selections)
{
for (auto& variant : selections) {
const std::string& varSetName = variant.first;
const std::string& varSelection = variant.second;
UsdVariantSet varSet = prim.GetVariantSet(varSetName);

if (!IsValidVariant(varSet)) {
TF_WARN(
"Invalid variant (%s) for prim (%s).",
varSetName.c_str(),
prim.GetName().GetText());
continue;
}

if (!IsValidVariantSelection(varSet, varSelection)) {
TF_WARN(
"Invalid variant selection (%s) in variant (%s) for prim (%s).",
varSelection.c_str(),
varSetName.c_str(),
prim.GetName().GetText());
continue;
}

varSet.SetVariantSelection(varSelection);
}
}

} // namespace

UsdMaya_ReadJob::UsdMaya_ReadJob(
Expand Down Expand Up @@ -281,17 +329,18 @@ bool UsdMaya_ReadJob::Read(std::vector<MDagPath>* addedDagPaths)
}

// Set the variants on the usdRootPrim
for (auto& variant : mImportData.rootVariantSelections()) {
usdRootPrim.GetVariantSet(variant.first).SetVariantSelection(variant.second);
}
ApplyVariantSelections(usdRootPrim, mImportData.rootVariantSelections());
progressBar.advance();

// Set the variants on all the import data prims.
for (auto& varPrim : mImportData.primVariantSelections()) {
for (auto& variant : varPrim.second) {
UsdPrim usdVarPrim = stage->GetPrimAtPath(varPrim.first);
usdVarPrim.GetVariantSet(variant.first).SetVariantSelection(variant.second);
const SdfPath& primName = varPrim.first;
UsdPrim usdVarPrim = stage->GetPrimAtPath(primName);
if (!usdVarPrim.IsValid()) {
TF_WARN("Invalid prim specified (%s) for variant selection.", primName.GetText());
continue;
}
ApplyVariantSelections(usdVarPrim, varPrim.second);
}
progressBar.advance();

Expand Down

0 comments on commit 7c4c775

Please sign in to comment.