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

Fix crashes when saving layers to USD files in specific edge cases. #4018

Merged
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
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