Skip to content

Commit

Permalink
Merge pull request #4106 from jufrantz/fix_crash_exporting_collection…
Browse files Browse the repository at this point in the history
…_based_bindings

ShadingModeExporter: Fix potential crash when exporting collection-based material bindings.
  • Loading branch information
seando-adsk authored Feb 21, 2025
2 parents ca79971 + 1d31090 commit dc8857b
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/mayaUsd/fileio/shading/shadingModeExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void UsdMayaShadingModeExporter::DoExport(
using MaterialAssignments = std::vector<std::pair<TfToken, SdfPathSet>>;
MaterialAssignments matAssignments;

std::vector<UsdShadeMaterial> exportedMaterials;
std::vector<UsdShadeMaterial> materialsToBind;

std::vector<MObject> shadingEngines;
MItDependencyNodes shadingEngineIter(MFn::kShadingEngine);
Expand All @@ -130,8 +130,8 @@ void UsdMayaShadingModeExporter::DoExport(

if (mat) {
writeJobContext.AddMaterialPath(mat.GetPath());
exportedMaterials.push_back(mat);
if (!boundPrimPaths.empty()) {
materialsToBind.push_back(mat);
matAssignments.push_back(std::make_pair(_GetCollectionName(mat), boundPrimPaths));
}
}
Expand All @@ -151,7 +151,7 @@ void UsdMayaShadingModeExporter::DoExport(
//
// This computes the first root prim below which a material has
// been exported.
SdfPath rootPrimPath = exportedMaterials[0].GetPath().GetPrefixes()[0];
SdfPath rootPrimPath = materialsToBind.at(0).GetPath().GetPrefixes()[0];
materialCollectionsPrim = stage->GetPrimAtPath(rootPrimPath);
TF_VERIFY(
materialCollectionsPrim,
Expand All @@ -164,9 +164,9 @@ void UsdMayaShadingModeExporter::DoExport(
= UsdUtilsCreateCollections(matAssignments, materialCollectionsPrim);

if (exportArgs.exportCollectionBasedBindings) {
for (size_t i = 0u; i < exportedMaterials.size(); ++i) {
const UsdShadeMaterial& mat = exportedMaterials[i];
const UsdCollectionAPI& coll = collections[i];
for (size_t i = 0u; i < materialsToBind.size(); ++i) {
const UsdShadeMaterial& mat = materialsToBind[i];
const UsdCollectionAPI& coll = collections.at(i);

// If the all the paths are under the prim with the materialBind
// collections, export the binding on the prim.
Expand Down

0 comments on commit dc8857b

Please sign in to comment.