Skip to content

Commit

Permalink
Merge pull request #4018 from jufrantz/fix_potential_crash_on_save_to…
Browse files Browse the repository at this point in the history
…_usd_files

Fix crashes when saving layers to USD files in specific edge cases.
  • Loading branch information
seando-adsk authored Dec 3, 2024
2 parents 2abaed5 + 1072bf6 commit 652667a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
19 changes: 11 additions & 8 deletions lib/mayaUsd/nodes/layerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ bool LayerDatabase::getProxiesToSave(bool isExport, bool* hasAnyProxy)
if (!pShape->isShareableStage() || !pShape->isStageIncoming()) {
SdfLayerHandleVector allLayers = stage->GetUsedLayers(true);
for (auto layer : allLayers) {
if (layer->IsDirty()) {
if (TF_VERIFY(layer) && layer->IsDirty()) {
StageSavingInfo info;
MDagPath::getAPathTo(mobj, info.dagPath);
info.stage = stage;
Expand Down Expand Up @@ -893,7 +893,8 @@ SaveStageToMayaResult saveStageToMayaFile(
saveLayersToMayaFile(
stage->GetUsedLayers(true),
[&localLayerIds](const auto& layer) {
return localLayerIds.find(layer->GetIdentifier()) != localLayerIds.cend();
return TF_VERIFY(layer)
&& localLayerIds.find(layer->GetIdentifier()) != localLayerIds.cend();
},
lm,
builder,
Expand Down Expand Up @@ -1020,12 +1021,14 @@ BatchSaveResult LayerDatabase::saveUsdToUsdFiles()
const auto& sessionLayer = info.stage->GetSessionLayer();
const auto& allLayers = info.stage->GetUsedLayers(true);
for (auto layer : allLayers) {
if (layer != sessionLayer && layer->PermissionToSave()) {
if (!MayaUsd::utils::saveLayerWithFormat(layer)) {
MString errMsg;
MString layerName(layer->GetDisplayName().c_str());
errMsg.format("Could not save layer ^1s.", layerName);
MGlobal::displayError(errMsg);
if (TF_VERIFY(layer)) {
if (layer != sessionLayer && layer->PermissionToSave()) {
if (!MayaUsd::utils::saveLayerWithFormat(layer)) {
MString errMsg;
MString layerName(layer->GetDisplayName().c_str());
errMsg.format("Could not save layer ^1s.", layerName);
MGlobal::displayError(errMsg);
}
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion lib/mayaUsd/utils/utilSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,12 @@ bool saveLayerWithFormat(
}
}

updateAllCachedStageWithLayer(layer, filePath);
// Update all known stage caches if the layer was saved to a new file path.
// Skip this step when the layer's file path hasn't changed to avoid unnecessary stage
// recompositions.
if (!requestedFilePath.empty()) {
updateAllCachedStageWithLayer(layer, filePath);
}

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/usdUfe/utils/layers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ StageDirtyState isStageDirty(const PXR_NS::UsdStage& stage)

SdfLayerHandleVector allLayers = stage.GetUsedLayers(true);
for (auto layer : allLayers) {
if (!layer->IsDirty())
if (!TF_VERIFY(layer) || !layer->IsDirty())
continue;

if (rootLayers.count(layer))
Expand Down

0 comments on commit 652667a

Please sign in to comment.