Fix sublayers not persisted on save

- SaveStage: explicitly call rootLayer->Save() before m_stage->Save() to
  ensure root-layer metadata (subLayers field) is always written to disk
- SaveUsdFile: redirect to SaveAs when the root layer is anonymous so
  Ctrl+S on an in-memory stage no longer silently does nothing
- SaveUsdFileAs: call RefreshManagers() after SaveStageAs so LayerManager
  stays in sync with the newly opened file-backed stage; without this,
  sublayers added after a Save As went to the old stale stage and were lost
- LayerCreateCommand::Execute: use GetSublayers() (sublayer-local indices)
  instead of GetLayerStack() (full-stack indices) to record m_insertedIndex,
  fixing undo of add-sublayer removing the wrong layer

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-15 07:49:34 +08:00
parent 9816fb8759
commit e0b0fd8204
3 changed files with 40 additions and 13 deletions
+17 -3
View File
@@ -397,7 +397,15 @@ void Application::SaveUsdFile() {
if (!m_stageManager->HasStage()) {
return;
}
// If the current stage is in-memory (anonymous root layer), fall through to
// Save As — UsdStage::Save() cannot write anonymous layers to disk.
auto rootLayer = m_stageManager->GetRootLayer();
if (!rootLayer || rootLayer->IsAnonymous()) {
SaveUsdFileAs();
return;
}
if (!m_stageManager->SaveStage()) {
LOG_ERROR("Failed to save USD file: " + m_stageManager->GetLastError());
}
@@ -407,17 +415,23 @@ void Application::SaveUsdFileAs() {
if (!m_stageManager->HasStage()) {
return;
}
std::string filePath = FileDialog::SaveFile(
"USD Files (*.usd;*.usda;*.usdc)\0*.usd;*.usda;*.usdc\0All Files (*.*)\0*.*\0",
"Save USD File As",
"usd",
m_imguiContext->GetWindowHandle()
);
if (!filePath.empty()) {
if (!m_stageManager->SaveStageAs(filePath)) {
LOG_ERROR("Failed to save USD file: " + m_stageManager->GetLastError());
} else {
// SaveStageAs reopens m_stageManager's stage from the new file path.
// RefreshManagers syncs m_layerManager (and others) to that new stage;
// without this, subsequent sublayer edits go to the old (now stale) stage
// and are silently lost on the next save.
RefreshManagers();
}
}
}