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

EMSUSD-1098 Fixed saving vs locked layers #3669

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 7 additions & 15 deletions lib/mayaUsd/nodes/layerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,19 @@ void convertAnonymousLayersRecursive(
{
auto currentTarget = stage->GetEditTarget().GetLayer();

MayaUsd::utils::LayerParent parentPtr;
if (stage->GetRootLayer() == layer) {
parentPtr._layerParent = nullptr;
parentPtr._proxyPath = basename;
} else if (stage->GetSessionLayer() == layer) {
parentPtr._layerParent = nullptr;
parentPtr._proxyPath = basename;
} else {
parentPtr._layerParent = layer;
parentPtr._proxyPath = basename;
}

pierrebai-adsk marked this conversation as resolved.
Show resolved Hide resolved
std::vector<std::string> sublayers = layer->GetSubLayerPaths();
for (size_t i = 0, n = sublayers.size(); i < n; ++i) {
auto subL = layer->Find(sublayers[i]);
SdfLayerRefPtr subL = layer->Find(sublayers[i]);
if (subL) {
convertAnonymousLayersRecursive(subL, basename, stage);

if (subL->IsAnonymous()) {
auto newLayer
= MayaUsd::utils::saveAnonymousLayer(stage, subL, parentPtr, basename);
MayaUsd::utils::LayerParent subLayerParent;
subLayerParent._layerParent = layer;
subLayerParent._proxyPath = basename;

SdfLayerRefPtr newLayer
= MayaUsd::utils::saveAnonymousLayer(stage, subL, subLayerParent, basename);
if (subL == currentTarget) {
stage->SetEditTarget(newLayer);
}
Expand Down
1 change: 1 addition & 0 deletions lib/mayaUsd/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ target_sources(${PYTHON_TARGET_NAME}
wrapConverter.cpp
wrapCopyLayerPrims.cpp
wrapDiagnosticDelegate.cpp
wrapLayerLocking.cpp
wrapLoadRules.cpp
wrapMeshWriteUtils.cpp
wrapOpUndoItem.cpp
Expand Down
1 change: 1 addition & 0 deletions lib/mayaUsd/python/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ TF_WRAP_MODULE
TF_WRAP(ConverterArgs);
TF_WRAP(CopyLayerPrims);
TF_WRAP(DiagnosticDelegate);
TF_WRAP(LayerLocking);
TF_WRAP(LoadRules);
TF_WRAP(MeshWriteUtils);
#ifdef UFE_V3_FEATURES_AVAILABLE
Expand Down
65 changes: 65 additions & 0 deletions lib/mayaUsd/python/wrapLayerLocking.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// Copyright 2023 Autodesk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include <mayaUsd/utils/layerLocking.h>

#include <pxr/base/tf/pyResultConversions.h>

#include <boost/python/def.hpp>

using namespace boost::python;

namespace {

void _lockLayer(const std::string& shapeName, PXR_NS::SdfLayer& layer)
{
PXR_NS::SdfLayerRefPtr layerPtr(&layer);
MayaUsd::lockLayer(shapeName, layerPtr, MayaUsd::LayerLock_Locked);
}

void _systemLockLayer(const std::string& shapeName, PXR_NS::SdfLayer& layer)
{
PXR_NS::SdfLayerRefPtr layerPtr(&layer);
MayaUsd::lockLayer(shapeName, layerPtr, MayaUsd::LayerLock_SystemLocked);
}

void _unlockLayer(const std::string& shapeName, PXR_NS::SdfLayer& layer)
{
PXR_NS::SdfLayerRefPtr layerPtr(&layer);
MayaUsd::lockLayer(shapeName, layerPtr, MayaUsd::LayerLock_Unlocked);
}

bool _isLayerLocked(PXR_NS::SdfLayer& layer)
{
PXR_NS::SdfLayerRefPtr layerPtr(&layer);
return MayaUsd::isLayerLocked(layerPtr);
}

bool _isLayerSystemLocked(PXR_NS::SdfLayer& layer)
{
PXR_NS::SdfLayerRefPtr layerPtr(&layer);
return MayaUsd::isLayerSystemLocked(layerPtr);
}

} // namespace

void wrapLayerLocking()
{
def("lockLayer", _lockLayer);
def("systemLockLayer", _systemLockLayer);
def("unlockLayer", _unlockLayer);
def("isLayerLocked", _isLayerLocked);
def("isLayerSystemLocked", _isLayerSystemLocked);
}
3 changes: 2 additions & 1 deletion lib/mayaUsd/utils/layerLocking.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ enum LayerLockType
LayerLock_SystemLocked
};

/*! \brief Sets the lock status on a layer
/*! \brief Sets the lock status on a layer.
* Automatically calls addLockedLayer, addSystemLockedLayer or their remove counterparts.
*/
MAYAUSD_CORE_PUBLIC
void lockLayer(
Expand Down
53 changes: 53 additions & 0 deletions lib/mayaUsd/utils/utilFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,3 +750,56 @@ std::string UsdMayaUtilFileSystem::pathFindExtension(std::string& filePath)
ghc::filesystem::path ext = p.extension();
return ext.string();
}

UsdMayaUtilFileSystem::FileBackup::FileBackup(const std::string& filename)
pierrebai-adsk marked this conversation as resolved.
Show resolved Hide resolved
: _filename(filename)
{
backup();
}

UsdMayaUtilFileSystem::FileBackup::~FileBackup()
{
// If commited, we don't restore the old file.
if (_commited)
return;

try {
restore();
} catch (...) {
// Don't allow exceptions out of a destructor.
}
}

std::string UsdMayaUtilFileSystem::FileBackup::getBackupFilename() const
{
return _filename + ".backup";
}

void UsdMayaUtilFileSystem::FileBackup::backup()
{
if (!ghc::filesystem::exists(ghc::filesystem::path(_filename)))
return;

const std::string backupFileName = getBackupFilename();
remove(backupFileName.c_str());
if (rename(_filename.c_str(), backupFileName.c_str()) != 0)
return;

_backed = true;
}

void UsdMayaUtilFileSystem::FileBackup::commit()
{
// Once commited, the backup will not be put back into the original file.
_commited = true;
}

void UsdMayaUtilFileSystem::FileBackup::restore()
{
if (!_backed)
return;

remove(_filename.c_str());
const std::string backupFileName = getBackupFilename();
rename(backupFileName.c_str(), _filename.c_str());
}
24 changes: 24 additions & 0 deletions lib/mayaUsd/utils/utilFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,30 @@ void pathRemoveExtension(std::string& filePath);
MAYAUSD_CORE_PUBLIC
std::string pathFindExtension(std::string& filePath);

// Backup a file and restore it if not commited.
class FileBackup
{
public:
FileBackup(const std::string& filename);
~FileBackup();

// Once commited, the backup will not be put back into the original file.
void commit();

// Force restoration of the original file if successfully backed-up, even if commited.
void restore();

// Return the backup file name.
std::string getBackupFilename() const;

public:
void backup();

std::string _filename;
bool _backed = false;
bool _commited = false;
};

} // namespace UsdMayaUtilFileSystem

PXR_NAMESPACE_CLOSE_SCOPE
Loading
Loading