Skip to content

Commit 652667a

Browse files
authored
Merge pull request #4018 from jufrantz/fix_potential_crash_on_save_to_usd_files
Fix crashes when saving layers to USD files in specific edge cases.
2 parents 2abaed5 + 1072bf6 commit 652667a

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

lib/mayaUsd/nodes/layerManager.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ bool LayerDatabase::getProxiesToSave(bool isExport, bool* hasAnyProxy)
606606
if (!pShape->isShareableStage() || !pShape->isStageIncoming()) {
607607
SdfLayerHandleVector allLayers = stage->GetUsedLayers(true);
608608
for (auto layer : allLayers) {
609-
if (layer->IsDirty()) {
609+
if (TF_VERIFY(layer) && layer->IsDirty()) {
610610
StageSavingInfo info;
611611
MDagPath::getAPathTo(mobj, info.dagPath);
612612
info.stage = stage;
@@ -893,7 +893,8 @@ SaveStageToMayaResult saveStageToMayaFile(
893893
saveLayersToMayaFile(
894894
stage->GetUsedLayers(true),
895895
[&localLayerIds](const auto& layer) {
896-
return localLayerIds.find(layer->GetIdentifier()) != localLayerIds.cend();
896+
return TF_VERIFY(layer)
897+
&& localLayerIds.find(layer->GetIdentifier()) != localLayerIds.cend();
897898
},
898899
lm,
899900
builder,
@@ -1020,12 +1021,14 @@ BatchSaveResult LayerDatabase::saveUsdToUsdFiles()
10201021
const auto& sessionLayer = info.stage->GetSessionLayer();
10211022
const auto& allLayers = info.stage->GetUsedLayers(true);
10221023
for (auto layer : allLayers) {
1023-
if (layer != sessionLayer && layer->PermissionToSave()) {
1024-
if (!MayaUsd::utils::saveLayerWithFormat(layer)) {
1025-
MString errMsg;
1026-
MString layerName(layer->GetDisplayName().c_str());
1027-
errMsg.format("Could not save layer ^1s.", layerName);
1028-
MGlobal::displayError(errMsg);
1024+
if (TF_VERIFY(layer)) {
1025+
if (layer != sessionLayer && layer->PermissionToSave()) {
1026+
if (!MayaUsd::utils::saveLayerWithFormat(layer)) {
1027+
MString errMsg;
1028+
MString layerName(layer->GetDisplayName().c_str());
1029+
errMsg.format("Could not save layer ^1s.", layerName);
1030+
MGlobal::displayError(errMsg);
1031+
}
10291032
}
10301033
}
10311034
}

lib/mayaUsd/utils/utilSerialization.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,12 @@ bool saveLayerWithFormat(
407407
}
408408
}
409409

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

412417
return true;
413418
}

lib/usdUfe/utils/layers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ StageDirtyState isStageDirty(const PXR_NS::UsdStage& stage)
9595

9696
SdfLayerHandleVector allLayers = stage.GetUsedLayers(true);
9797
for (auto layer : allLayers) {
98-
if (!layer->IsDirty())
98+
if (!TF_VERIFY(layer) || !layer->IsDirty())
9999
continue;
100100

101101
if (rootLayers.count(layer))

0 commit comments

Comments
 (0)