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-127954 - As a user, I'd like to drag and drop a sublayer that has a relative path #3002

Merged
merged 7 commits into from
Apr 4, 2023
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
40 changes: 37 additions & 3 deletions lib/mayaUsd/commands/layerEditorCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "layerEditorCommand.h"

#include <mayaUsd/utils/query.h>
#include <mayaUsd/utils/utilFileSystem.h>

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

Expand All @@ -33,6 +34,8 @@
#include <ufe/observableSelection.h>
#endif

#include <ghc/filesystem.hpp>

#include <cstddef>
#include <string>

Expand Down Expand Up @@ -333,6 +336,7 @@ class MoveSubPath : public BaseCmd
_oldIndex = subPathIndex; // save for undo

SdfLayerHandle newParentLayer;
std::string newPath = _path;

if (layer->GetIdentifier() == _newParentLayer) {

Expand All @@ -359,11 +363,41 @@ class MoveSubPath : public BaseCmd
return false;
}

// See if the path should be reparented
ghc::filesystem::path filePath(_path);
bool needsRepathing = !SdfLayer::IsAnonymousLayerIdentifier(_path);
needsRepathing &= filePath.is_relative();
needsRepathing &= !layer->GetRealPath().empty();
needsRepathing &= !newParentLayer->GetRealPath().empty();

// Reparent the path if needed
if (needsRepathing) {
auto oldLayerDir = ghc::filesystem::path(layer->GetRealPath()).remove_filename();
auto newLayerDir
= ghc::filesystem::path(newParentLayer->GetRealPath()).remove_filename();

std::string absolutePath
= (oldLayerDir / filePath).lexically_normal().generic_string();
auto result = UsdMayaUtilFileSystem::makePathRelativeTo(
absolutePath, newLayerDir.lexically_normal().generic_string());

if (result.second) {
newPath = result.first;
} else {
newPath = absolutePath;
TF_WARN(
"File name (%s) cannot be resolved as relative to the layer %s, "
"using the absolute path.",
absolutePath.c_str(),
newParentLayer->GetIdentifier().c_str());
}
}

// make sure the subpath is not already in the new parent layer.
// Otherwise, the SdfLayer::InsertSubLayerPath() below will do nothing
// and the subpath will be removed from it's current parent.
if (newParentLayer->GetSubLayerPaths().Find(_path) != size_t(-1)) {
std::string message = std::string("SubPath ") + _path
if (newParentLayer->GetSubLayerPaths().Find(newPath) != size_t(-1)) {
std::string message = std::string("SubPath ") + newPath
+ std::string(" already exist in layer ") + newParentLayer->GetIdentifier();
MPxCommand::displayError(message.c_str());
return false;
Expand All @@ -376,7 +410,7 @@ class MoveSubPath : public BaseCmd
// oterwise InsertSubLayerPath() will fail because the subLayer
// already exists.
layer->RemoveSubLayerPath(subPathIndex);
newParentLayer->InsertSubLayerPath(_path, _newIndex);
newParentLayer->InsertSubLayerPath(newPath, _newIndex);

return true;
}
Expand Down
40 changes: 40 additions & 0 deletions lib/usd/ui/layerEditor/layerTreeModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,46 @@ QMimeData* LayerTreeModel::mimeData(const QModelIndexList& indexes) const
return mimeData;
}

bool LayerTreeModel::canDropMimeData(
const QMimeData* in_mimeData,
Qt::DropAction action,
int row,
int column,
const QModelIndex& parentIndex) const
{
if (!in_mimeData || (action != Qt::MoveAction)) {
return false;
}
if (!in_mimeData->hasFormat(LAYER_EDITOR_MIME_TYPE)) {
return false;
}

auto parentItem = layerItemFromIndex(parentIndex);
if (!parentItem || parentItem->isReadOnly()) {
return false;
}

// Only anonymous layers have additional restrictions
if (!parentItem->layer()->IsAnonymous()) {
return true;
}

// Layer with its path relative cannot be dropped to an anonymous layer
QStringList identifiers = in_mimeData->text().split(LAYED_EDITOR_MIME_SEP);
for (QString const& ident : identifiers) {
if (auto layer = SdfLayer::FindOrOpen(ident.toStdString())) {
if (auto layerItem = findUSDLayerItem(layer)) {
if (!layer->IsAnonymous()
&& QDir::isRelativePath(layerItem->subLayerPath().c_str())) {
return false;
}
}
}
}

return true;
}

bool LayerTreeModel::dropMimeData(
const QMimeData* in_mimeData,
Qt::DropAction action,
Expand Down
6 changes: 6 additions & 0 deletions lib/usd/ui/layerEditor/layerTreeModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ class LayerTreeModel
int row,
int column,
const QModelIndex& parent) override;
bool canDropMimeData(
const QMimeData* data,
Qt::DropAction action,
int row,
int column,
const QModelIndex& parent) const override;

// for debugging
void forceRefresh() { rebuildModelOnIdle(); }
Expand Down
41 changes: 41 additions & 0 deletions test/lib/testMayaUsdLayerEditorCommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,47 @@ def moveElement(list, item, index):
self.assertEqual(rootLayer.subLayerPaths, originalSubLayerPaths)
self.assertEqual(layer1.subLayerPaths, [layerId3])

def testMoveRelativeSubPath(self):
""" test 'mayaUsdLayerEditor' command 'moveSubPath' paramater for relative subpaths """

shapePath, stage = getCleanMayaStage()
rootLayer = stage.GetRootLayer()

# Create a temporary directory in the current diretory
myDir = testUtils.TemporaryDirectory(suffix="testMoveRelativeSubPathDir")
myDir_path, myDir_name = path.split(myDir.name)

# Create a new usda file in the current diretory and add it to the root layer through absolute path
absLayer1File = tempfile.NamedTemporaryFile(suffix=".usda", prefix="absLayer1", dir=myDir_path, delete=False, mode="w")
absLayer1File.write("#usda 1.0")
absLayer1File.close()
absLayer1Id = path.normpath(absLayer1File.name)
cmds.mayaUsdLayerEditor(rootLayer.identifier, edit=True, insertSubPath=[0, absLayer1Id])
absLayer1 = Sdf.Layer.Find(absLayer1Id)

# Create a new usda file in the new temporary diretory and add it to the root layer through absolute path
absLayer2File = tempfile.NamedTemporaryFile(suffix=".usda", prefix="absLayer2", dir=myDir.name, delete=False, mode="w")
absLayer2File.write("#usda 1.0")
absLayer2File.close()
absLayer2Id = path.normpath(absLayer2File.name)
cmds.mayaUsdLayerEditor(rootLayer.identifier, edit=True, insertSubPath=[1, absLayer2Id])
absLayer2 = Sdf.Layer.Find(absLayer2Id)

# Create a new usda file in the new temporary diretory and add it to absLayer2 through its relative path
relLayerFile = tempfile.NamedTemporaryFile(suffix=".usda", prefix="relLayer", dir=myDir.name, delete=False, mode="w")
relLayerFile.write("#usda 1.0")
relLayerFile.close()
relLayerFileId = path.basename(relLayerFile.name) # relative path
cmds.mayaUsdLayerEditor(absLayer2.identifier, edit=True, insertSubPath=[0, relLayerFileId])

# Now move the relative sublayer from absLayer2 to absLayer1
cmds.mayaUsdLayerEditor(absLayer2.identifier, edit=True, moveSubPath=[relLayerFileId, absLayer1.identifier, 0])

# The relative sublayer's path should change now to include the directory name
relLayerNewFileId = myDir_name + "/" + relLayerFileId
self.assertTrue(len(absLayer1.subLayerPaths) == 1)
self.assertEqual(absLayer1.subLayerPaths[0], relLayerNewFileId)

def testMuteLayer(self):
""" test 'mayaUsdLayerEditor' command 'muteLayer' paramater """

Expand Down