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-112372: Add restriction for grouping. #1550

Merged
merged 3 commits into from
Jul 7, 2021
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
3 changes: 2 additions & 1 deletion lib/mayaUsd/ufe/UsdUndoAddNewPrimCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ UsdUndoAddNewPrimCommand::UsdUndoAddNewPrimCommand(
_newUfePath = appendToPath(ufePath, newPrimName);
}

ufe::applyCommandRestriction(usdSceneItem->prim(), "add new [" + _newUfePath.back().string() + "] under ");
ufe::applyCommandRestriction(
usdSceneItem->prim(), "add new [" + _newUfePath.back().string() + "] under ");

// Build (and store) the usd path for the new prim with the unique name.
PXR_NS::SdfPath usdItemPath = usdSceneItem->prim().GetPath();
Expand Down
42 changes: 27 additions & 15 deletions lib/mayaUsd/ufe/UsdUndoCreateGroupCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,36 @@ void UsdUndoCreateGroupCommand::execute()
setKindCmd->execute();
}

auto newParentHierarchy = Ufe::Hierarchy::hierarchy(_group);
if (newParentHierarchy) {
for (auto child : _selection) {
auto parentCmd = newParentHierarchy->appendChildCmd(child);
parentCmd->execute();
append(parentCmd);
// Make sure to handle the exception if the parenting operation fails.
// This scenario happens if a user tries to group prim(s) in a layer
// other than the one where they were defined. In this case, the group creation itself
// will succeed, however the re-parenting is expected to throw an exception. We also need to
// make sure to undo the previous command ( AddNewPrimCommand ) when this happens.
try {
auto newParentHierarchy = Ufe::Hierarchy::hierarchy(_group);
if (newParentHierarchy) {
for (auto child : _selection) {
auto parentCmd = newParentHierarchy->appendChildCmd(child);
parentCmd->execute();
append(parentCmd);
}
}
}

// Make sure to add the newly created _group (a.k.a parent) to selection. This matches native
// Maya behavior and also prevents the crash on grouping a prim twice.
Ufe::Selection groupSelect;
groupSelect.append(_group);
Ufe::GlobalSelection::get()->replaceWith(groupSelect);
// Make sure to add the newly created _group (a.k.a parent) to selection. This matches
// native Maya behavior and also prevents the crash on grouping a prim twice.
Ufe::Selection groupSelect;
groupSelect.append(_group);
Ufe::GlobalSelection::get()->replaceWith(groupSelect);

TF_VERIFY(
Ufe::GlobalSelection::get()->size() == 1,
"_group node should be in the global selection now. \n");
} catch (...) {
// undo previous AddNewPrimCommand
undo();

TF_VERIFY(
Ufe::GlobalSelection::get()->size() == 1,
"_group node should be in the global selection now. \n");
throw; // re-throw the same exception
}
}

Comment on lines +87 to 113
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@seando-adsk
Grouping in a two tier operation:

1- create a group node
2- insert selected item(s) under the group node.

There could be a case where creating a group node would be Ok by the restriction rules but not the insertion part.
For example, this scenario could happen if a user tries to group prim(s) in a layer other than the one where they were defined. In this case, the group creation itself will succeed but not the insertion part. In this case, we should throw an exception and undo the previous group command.

Also keep in mind that the way we are performing the insertion here makes it impossible to support grouping flags (e.g absolute, relative, etc...) . Eventually, the insertion logics needs to be moved in Maya so we can support those flags.

} // namespace ufe
Expand Down
35 changes: 35 additions & 0 deletions test/lib/ufe/testGroupCmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,41 @@ def testGroupRestirction(self):
with self.assertRaises(RuntimeError):
cmds.group(name="newGroup")

# set the edit target to the root layer
stage.SetEditTarget(stage.GetRootLayer())

# create a sphere
stage.DefinePrim('/Sphere1', 'Sphere')

# select the Sphere
spherePath = ufe.PathString.path("{},/Sphere1".format("|transform1|proxyShape1"))
sphereItem = ufe.Hierarchy.createItem(spherePath)
ufeSelection = ufe.GlobalSelection.get()
ufeSelection.clear()
ufeSelection.append(sphereItem)

# set the edit target to the session layer
stage.SetEditTarget(stage.GetSessionLayer())

# expect the exception happens.
with self.assertRaises(RuntimeError):
cmds.group(name="newGroup")

# undo
cmds.undo()

# verify that group1 doesn't exist after the undo
self.assertEqual([item for item in stage.Traverse()],
[stage.GetPrimAtPath("/Ball_set"),
stage.GetPrimAtPath("/Ball_set/Props"),
stage.GetPrimAtPath("/Ball_set/Props/Ball_1"),
stage.GetPrimAtPath("/Ball_set/Props/Ball_2"),
stage.GetPrimAtPath("/Ball_set/Props/Ball_3"),
stage.GetPrimAtPath("/Ball_set/Props/Ball_4"),
stage.GetPrimAtPath("/Ball_set/Props/Ball_5"),
stage.GetPrimAtPath("/Ball_set/Props/Ball_6"),
stage.GetPrimAtPath("/Sphere1")])


if __name__ == '__main__':
unittest.main(verbosity=2)