Skip to content

Commit

Permalink
Add methods to UsdStage for saving edited layers
Browse files Browse the repository at this point in the history
(Internal change: 1727839)
  • Loading branch information
sunyab authored and pixar-oss committed Mar 24, 2017
1 parent 4c742b8 commit 019a65f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pxr/usd/lib/usd/stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2766,6 +2766,62 @@ UsdStage::IsSupportedFile(const std::string& filePath)
UsdUsdFileFormatTokens->Target);
}

namespace {

void _SaveLayers(const SdfLayerHandleVector& layers)
{
for (const SdfLayerHandle& layer : layers) {
if (!layer->IsDirty()) {
continue;
}

if (layer->IsAnonymous()) {
TF_WARN("Not saving @%s@ because it is an anonymous layer",
layer->GetIdentifier().c_str());
continue;
}

// Sdf will emit errors if there are any problems with
// saving the layer.
layer->Save();
}
}

}

void
UsdStage::Save()
{
SdfLayerHandleVector layers = GetUsedLayers();

const PcpLayerStackPtr localLayerStack = _GetPcpCache()->GetLayerStack();
if (TF_VERIFY(localLayerStack)) {
const SdfLayerHandleVector sessionLayers =
localLayerStack->GetSessionLayers();
const auto isSessionLayer =
[&sessionLayers](const SdfLayerHandle& l) {
return std::find(
sessionLayers.begin(), sessionLayers.end(), l)
!= sessionLayers.end();
};

layers.erase(std::remove_if(layers.begin(), layers.end(),
isSessionLayer),
layers.end());
}

_SaveLayers(layers);
}

void
UsdStage::SaveSessionLayers()
{
const PcpLayerStackPtr localLayerStack = _GetPcpCache()->GetLayerStack();
if (TF_VERIFY(localLayerStack)) {
_SaveLayers(localLayerStack->GetSessionLayers());
}
}

static bool
_CheckAbsolutePrimPath(const SdfPath &path)
{
Expand Down
32 changes: 32 additions & 0 deletions pxr/usd/lib/usd/stage.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,38 @@ class UsdStage : public TfRefBase, public TfWeakBase {

/// @}

// --------------------------------------------------------------------- //
/// \anchor Usd_layerSerialization
/// \name Layer Serialization
///
/// Functions for saving changes to layers that contribute opinions to
/// this stage. Layers may also be saved by calling SdfLayer::Save or
/// exported to a new file by calling SdfLayer::Export.
///
/// @{

/// Calls SdfLayer::Save on all dirty layers contributing to this stage
/// except session layers and sublayers of session layers.
///
/// This function will emit a warning and skip each dirty anonymous
/// layer it encounters, since anonymous layers cannot be saved with
/// SdfLayer::Save. These layers must be manually exported by calling
/// SdfLayer::Export.
USD_API
void Save();

/// Calls SdfLayer::Save on all dirty session layers and sublayers of
/// session layers contributing to this stage.
///
/// This function will emit a warning and skip each dirty anonymous
/// layer it encounters, since anonymous layers cannot be saved with
/// SdfLayer::Save. These layers must be manually exported by calling
/// SdfLayer::Export.
USD_API
void SaveSessionLayers();

/// @}

// --------------------------------------------------------------------- //
/// \anchor Usd_variantManagement
/// \name Variant Management
Expand Down
3 changes: 3 additions & 0 deletions pxr/usd/lib/usd/wrapStage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ void wrapUsdStage()
.def("Close", &UsdStage::Close)
.def("Reload", &UsdStage::Reload)

.def("Save", &UsdStage::Save)
.def("SaveSessionLayers", &UsdStage::SaveSessionLayers)

.def("GetGlobalVariantFallbacks",
&UsdStage::GetGlobalVariantFallbacks,
return_value_policy<TfPyMapToDictionary>())
Expand Down

0 comments on commit 019a65f

Please sign in to comment.