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

MAYA-123013 obey file-format choice in layer editor #2334

Merged
merged 4 commits into from
May 13, 2022
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
5 changes: 2 additions & 3 deletions lib/mayaUsd/nodes/layerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ BatchSaveResult LayerDatabase::saveUsdToUsdFiles()
SdfLayerHandleVector allLayers = stage->GetLayerStack(false);
for (auto layer : allLayers) {
if (layer->PermissionToSave()) {
layer->Save();
MayaUsd::utils::saveLayerWithFormat(layer);
}
}
}
Expand All @@ -719,9 +719,8 @@ void LayerDatabase::convertAnonymousLayers(MayaUsdProxyShapeBase* pShape, UsdSta

if (root->IsAnonymous()) {
PXR_NS::SdfFileFormat::FileFormatArguments args;
args["format"] = MayaUsd::utils::usdFormatArgOption();
std::string newFileName = MayaUsd::utils::generateUniqueFileName(proxyName);
root->Export(newFileName, "", args);
MayaUsd::utils::saveLayerWithFormat(root, newFileName);

MayaUsd::utils::setNewProxyPath(pShape->name(), UsdMayaUtil::convert(newFileName));
}
Expand Down
58 changes: 50 additions & 8 deletions lib/mayaUsd/utils/utilSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,55 @@ void setNewProxyPath(const MString& proxyNodeName, const MString& newValue)
/*undo*/ false);
}

static bool isCompatibleWithSave(
SdfLayerRefPtr layer,
const std::string& filePath,
const std::string& formatArg)
{
if (!layer)
return false;

// Save cannot specify the filename, so the file name must match to use save.
if (layer->GetRealPath() != filePath)
return false;

const TfToken underlyingFormat = UsdUsdFileFormat::GetUnderlyingFormatForLayer(*layer);
if (underlyingFormat.size()) {
return underlyingFormat == formatArg;
} else {
const SdfFileFormat::FileFormatArguments currentFormatArgs
= layer->GetFileFormatArguments();

// If we cannot find the format argument then we cannot validate that the file format match
// so we err to the side of safety and claim they don't match.
const auto keyAndValue = currentFormatArgs.find("format");
if (keyAndValue == currentFormatArgs.end())
return false;

return keyAndValue->second == formatArg;
}
}

bool saveLayerWithFormat(
SdfLayerRefPtr layer,
const std::string& requestedFilePath,
const std::string& requestedFormatArg)
{
const std::string& filePath
= requestedFilePath.empty() ? layer->GetRealPath() : requestedFilePath;

const std::string& formatArg
= requestedFormatArg.empty() ? usdFormatArgOption() : requestedFormatArg;

if (isCompatibleWithSave(layer, filePath, formatArg)) {
return layer->Save();
} else {
PXR_NS::SdfFileFormat::FileFormatArguments args;
args["format"] = formatArg;
return layer->Export(filePath, "", args);
}
}

SdfLayerRefPtr saveAnonymousLayer(
SdfLayerRefPtr anonLayer,
LayerParent parent,
Expand All @@ -204,14 +253,7 @@ SdfLayerRefPtr saveAnonymousLayer(
return nullptr;
}

PXR_NS::SdfFileFormat::FileFormatArguments args;
if (!formatArg.empty()) {
args["format"] = formatArg;
} else {
args["format"] = usdFormatArgOption();
}

anonLayer->Export(path, "", args);
saveLayerWithFormat(anonLayer, path, formatArg);

SdfLayerRefPtr newLayer = SdfLayer::FindOrOpen(path);
if (newLayer) {
Expand Down
11 changes: 11 additions & 0 deletions lib/mayaUsd/utils/utilSerialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ struct stageLayersToSave
std::vector<SdfLayerRefPtr> _dirtyFileBackedLayers;
};

/*! \brief Save an layer to disk to the given file path and using the given format.
If the file path is empty then use the current file path of the layer.
If the format is empty then use the current user-selected USD format option
as defined by the usdFormatArgOption() function. (See above.)
*/
MAYAUSD_CORE_PUBLIC
bool saveLayerWithFormat(
SdfLayerRefPtr layer,
const std::string& requestedFilePath = "",
const std::string& requestedFormatArg = "");

/*! \brief Save an anonymous layer to disk and update the sublayer path array
in the parent layer.
*/
Expand Down
1 change: 1 addition & 0 deletions lib/usd/translators/mayaReferenceUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <mayaUsd/undo/OpUndoItems.h>
#include <mayaUsd/utils/editRouter.h>
#include <mayaUsd/utils/util.h>
#include <mayaUsd/utils/utilSerialization.h>
#include <mayaUsdUtils/MergePrims.h>
#include <mayaUsd_Schemas/ALMayaReference.h>
#include <mayaUsd_Schemas/MayaReference.h>
Expand Down
2 changes: 1 addition & 1 deletion lib/usd/ui/layerEditor/layerTreeItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ void LayerTreeItem::saveEditsNoPrompt()
if (!isSessionLayer())
saveAnonymousLayer();
} else {
layer()->Save();
MayaUsd::utils::saveLayerWithFormat(layer());
}
}

Expand Down
7 changes: 3 additions & 4 deletions lib/usd/ui/layerEditor/pathChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "stringResources.h"
#include "warningDialogs.h"

#include <mayaUsd/utils/utilSerialization.h>

#include <pxr/usd/ar/resolver.h>
#include <pxr/usd/sdf/fileFormat.h>
#include <pxr/usd/sdf/layerUtils.h>
Expand Down Expand Up @@ -181,10 +183,7 @@ bool saveSubLayer(
}

if (!fileError) {
PXR_NS::SdfFileFormat::FileFormatArguments formatArgs;
formatArgs["format"] = in_formatTag;

if (!in_layer->Export(in_absolutePath, "", formatArgs)) {
if (!MayaUsd::utils::saveLayerWithFormat(in_layer, in_absolutePath, in_formatTag)) {
fileError = true;
} else {
// parent item is null if we're saving the root later
Expand Down
2 changes: 2 additions & 0 deletions plugin/al/lib/AL_USDMaya/AL/usdmaya/cmds/LayerCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "AL/usdmaya/nodes/ProxyShape.h"
#include "AL/usdmaya/nodes/Transform.h"

#include <mayaUsd/utils/utilSerialization.h>

#include <pxr/usd/sdf/listOp.h>
#include <pxr/usd/usd/stage.h>

Expand Down