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-106310 - Crash when querying target layer and nothing is assigne… #1015

Merged
merged 3 commits into from
Dec 23, 2020
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
47 changes: 44 additions & 3 deletions lib/mayaUsd/commands/layerEditorCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class InsertRemoveSubPathBase : public BaseCmd
public:
int _index = -1;
std::string _subPath;
std::string _proxyShapePath;

InsertRemoveSubPathBase(CmdId id)
: BaseCmd(id)
Expand All @@ -136,6 +137,18 @@ class InsertRemoveSubPathBase : public BaseCmd
}
_subPath = layer->GetSubLayerPaths()[_index];
holdOnPathIfDirty(layer, _subPath);

// if the layer to remove is the current edit target,
// set the root layer as the current edit target
auto subLayerHandle = SdfLayer::FindRelativeToLayer(layer, _subPath);
auto stage = getStage();
auto currentTarget = stage->GetEditTarget().GetLayer();
if (currentTarget
&& currentTarget->GetIdentifier() == subLayerHandle->GetIdentifier()) {
_isEditTarget = true;
stage->SetEditTarget(stage->GetRootLayer());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be worth considering matching the logic with base proxy shape, ie fallback to session layer of root layer can’t be edited or we forced session layer with option var.

}

layer->RemoveSubLayerPath(_index);
}
return true;
Expand All @@ -157,6 +170,14 @@ class InsertRemoveSubPathBase : public BaseCmd
TF_VERIFY(_index != -1);
if (validateUndoIndex(layer, _index)) {
layer->InsertSubLayerPath(_subPath, _index);

// if the removed layer was the edit target,
// set it back to the current edit target
if (_isEditTarget) {
auto stage = getStage();
auto subLayerHandle = SdfLayer::FindRelativeToLayer(layer, _subPath);
stage->SetEditTarget(subLayerHandle);
}
} else {
return false;
}
Expand All @@ -179,6 +200,16 @@ class InsertRemoveSubPathBase : public BaseCmd
return true;
}
}

protected:
bool _isEditTarget = false;

UsdStageWeakPtr getStage()
{
auto prim = UsdMayaQuery::GetPrim(_proxyShapePath.c_str());
auto stage = prim.GetStage();
return stage;
}
};

class InsertSubPath : public InsertRemoveSubPathBase
Expand Down Expand Up @@ -397,7 +428,7 @@ MSyntax LayerEditorCommand::createSyntax()

syntax.addFlag(kInsertSubPathFlag, kInsertSubPathFlagL, MSyntax::kLong, MSyntax::kString);
syntax.makeFlagMultiUse(kInsertSubPathFlag);
syntax.addFlag(kRemoveSubPathFlag, kRemoveSubPathFlagL, MSyntax::kLong);
syntax.addFlag(kRemoveSubPathFlag, kRemoveSubPathFlagL, MSyntax::kLong, MSyntax::kString);
syntax.makeFlagMultiUse(kRemoveSubPathFlag);
syntax.addFlag(kReplaceSubPathFlag, kReplaceSubPathFlagL, MSyntax::kString, MSyntax::kString);
syntax.makeFlagMultiUse(kReplaceSubPathFlag);
Expand Down Expand Up @@ -451,11 +482,21 @@ MStatus LayerEditorCommand::parseArgs(const MArgList& argList)
if (argParser.isFlagSet(kRemoveSubPathFlag)) {
auto count = argParser.numberOfFlagUses(kRemoveSubPathFlag);
for (unsigned i = 0; i < count; i++) {
auto cmd = std::make_shared<Impl::RemoveSubPath>();

MArgList listOfArgs;
argParser.getFlagArgumentList(kRemoveSubPathFlag, i, listOfArgs);
cmd->_index = listOfArgs.asInt(0);

auto index = listOfArgs.asInt(0);
auto shapePath = listOfArgs.asString(1);
auto prim = UsdMayaQuery::GetPrim(shapePath.asChar());
if (prim == UsdPrim()) {
displayError(MString("Invalid proxy shape \"") + shapePath.asChar() + "\"");
return MS::kInvalidParameter;
}

auto cmd = std::make_shared<Impl::RemoveSubPath>();
cmd->_index = index;
cmd->_proxyShapePath = shapePath.asChar();
_subCommands.push_back(std::move(cmd));
}
}
Expand Down
3 changes: 0 additions & 3 deletions lib/usd/ui/layerEditor/layerTreeItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,6 @@ void LayerTreeItem::getActionButton(int index, LayerActionInfo* out_info) const
void LayerTreeItem::removeSubLayer()
{
if (isSublayer()) { // can't remove session or root layer
if (_isTargetLayer) {
commandHook()->setEditTarget(stage()->GetRootLayer());
}
commandHook()->removeSubLayerPath(parentLayerItem()->layer(), subLayerPath());
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/usd/ui/layerEditor/mayaCommandHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ void MayaCommandHook::removeSubLayerPath(UsdLayer usdLayer, Path path)
std::string cmd;
cmd = "mayaUsdLayerEditor -edit -removeSubPath ";
cmd += std::to_string(index);
cmd += quote(proxyShapePath());
cmd += quote(usdLayer->GetIdentifier());
executeMel(cmd);
}
Expand Down
23 changes: 19 additions & 4 deletions test/lib/testMayaUsdLayerEditorCommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ def testEditTarget(self):
cmds.undo()
self.assertEqual(stage.GetEditTarget().GetLayer().identifier, undoStepTwo)

# test removing the edit target
self.assertEqual(stage.GetEditTarget().GetLayer().identifier, greenLayerId)
cmds.mayaUsdLayerEditor(rootLayer.identifier, edit=True, removeSubPath=[0,shapePath])
self.assertEqual(stage.GetEditTarget().GetLayer().identifier, rootLayerID)
cmds.undo()
self.assertEqual(stage.GetEditTarget().GetLayer().identifier, greenLayerId)

cmds.mayaUsdEditTarget(shapePath, edit=True, editTarget=redLayerId)

self.assertEqual(stage.GetEditTarget().GetLayer().identifier, redLayerId)
cmds.mayaUsdLayerEditor(rootLayer.identifier, edit=True, removeSubPath=[1,shapePath])
self.assertEqual(stage.GetEditTarget().GetLayer().identifier, rootLayerID)
cmds.undo()
self.assertEqual(stage.GetEditTarget().GetLayer().identifier, redLayerId)

# test bad input
with self.assertRaises(RuntimeError):
cmds.mayaUsdEditTarget("bogusShape", query=True, editTarget=True)
Expand Down Expand Up @@ -141,18 +156,18 @@ def testSubLayerEditing(self):

# -removeSubPath
# remove second sublayer
cmds.mayaUsdLayerEditor(rootLayer.identifier, edit=True, removeSubPath=1)
cmds.mayaUsdLayerEditor(rootLayer.identifier, edit=True, removeSubPath=[1,_shapePath])
afterDeletion = [layer1Id, layer3Id]
self.assertEqual(rootLayer.subLayerPaths, afterDeletion)

# remove second sublayer again to leave only one
afterDeletion = [layer1Id, layer3Id]
cmds.mayaUsdLayerEditor(rootLayer.identifier, edit=True, removeSubPath=1)
cmds.mayaUsdLayerEditor(rootLayer.identifier, edit=True, removeSubPath=[1,_shapePath])
self.assertEqual(rootLayer.subLayerPaths, [layer1Id])

# remove second sublayer -- this time it's out of bounds
with self.assertRaises(RuntimeError):
cmds.mayaUsdLayerEditor(rootLayer.identifier, edit=True, removeSubPath=1)
cmds.mayaUsdLayerEditor(rootLayer.identifier, edit=True, removeSubPath=[1,_shapePath])

# undo twice to get back to three layers
cmds.undo()
Expand All @@ -168,7 +183,7 @@ def testSubLayerEditing(self):
grandChildLayerId = cmds.mayaUsdLayerEditor(childLayerId, edit=True, addAnonymous="GrandChild")[0]

# delete the top layer. See if it comes back after redo.
cmds.mayaUsdLayerEditor(rootLayer.identifier, edit=True, removeSubPath=0)
cmds.mayaUsdLayerEditor(rootLayer.identifier, edit=True, removeSubPath=[0,_shapePath])
# check layer1 was deleted
self.assertEqual(rootLayer.subLayerPaths, [layer2Id, layer3Id])
# bring it back
Expand Down