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-104215: improve renaming restriction on prim objects #475

Merged
merged 30 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c9100f7
MAYA-104215: Trying to rename an object that was not defined on the t…
Apr 29, 2020
54166d2
Address feedbacks:
Apr 30, 2020
8f8295f
First attempt to handle following scenario:
May 1, 2020
e21a130
Fix compiler errors.
May 4, 2020
4c67b69
Address Feedbacks: rename functions and variables to read better.
May 4, 2020
97996bb
- Added two new test cases for rename restriction operations.
May 5, 2020
581932b
Address feedback: rename test case names to something more meaningful.
May 5, 2020
761143d
Fix MacOS build and make sure to pass the error messages by char*
May 5, 2020
d58a028
Fix debug build:
May 5, 2020
20be9ac
MAYA-104215: fix the "cruft" in USD files as objects are named and re…
May 6, 2020
ae2a7e0
Improve rename logics:
May 6, 2020
bb1da2e
- Added early checks to make sure we are in a valid state when doing …
May 6, 2020
c4f247a
- Added tree.ma and tree.usda to the test-samples. This is a useful u…
May 6, 2020
a45779c
Updated test prim. tesRename does pass but there are still some issue…
May 7, 2020
b207895
- Add new logics for handling internal vs external references.
May 11, 2020
890372d
Eliminate the need to stores _prim and _oldName member variables.
May 12, 2020
a305af9
Address feedback: Re-factored the logics for detecting internal vs ex…
May 12, 2020
610f2ce
Address feedback: break out of the loop once the condition is met.
May 12, 2020
80aa35e
Address feedback: Don't return if we have an internal reference since…
May 12, 2020
3cf2acc
- change the wording form opinion to contribute/contribution to make …
May 13, 2020
7bb77b3
Merge pull request #483 from Autodesk/sabrih/MAYA-104215/improve_rena…
HamedSabri-adsk May 13, 2020
24001e3
Merge branch 'dev' into sabrih/MAYA-104215/improve_usd_rename_operation
HamedSabri-adsk May 13, 2020
7e66d41
Fix build after merging dev branch into this PR.
May 13, 2020
22d6fc9
Fix reference file path.
May 13, 2020
df3ff44
Make sure maya scene examples are using relative path.
May 13, 2020
67fd431
Nit-pick: No need to pass stage as an argument since it can be retrie…
May 14, 2020
7e8bef6
Added more logics for handling defaultprim rename.
May 14, 2020
d84d82f
Nit-Pick: do an object comparison instead of string.
May 14, 2020
5d81f6c
For future reference, added the feedback I received from Spiff under …
May 15, 2020
ade83fa
Remove out of date comment. MAYA-92264 is now internally closed.
May 15, 2020
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
150 changes: 76 additions & 74 deletions lib/mayaUsd/ufe/UsdUndoRenameCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
//
#include "UsdUndoRenameCommand.h"

#include <ufe/scene.h>
#include <ufe/sceneNotification.h>
#include <ufe/log.h>

#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usd/editContext.h>
#include <pxr/usd/sdf/copyUtils.h>
#include <pxr/base/tf/token.h>
#include <pxr/usd/sdf/copyUtils.h>
#include <pxr/usd/sdf/path.h>
#include <pxr/usd/usd/editContext.h>
#include <pxr/usd/usd/prim.h>
#include <pxr/usd/usd/stage.h>

#include <mayaUsd/ufe/Utils.h>

Expand All @@ -41,22 +41,23 @@ MAYAUSD_NS_DEF {
namespace ufe {

UsdUndoRenameCommand::UsdUndoRenameCommand(const UsdSceneItem::Ptr& srcItem, const Ufe::PathComponent& newName)
: Ufe::UndoableCommand()
: Ufe::UndoableCommand()
{
const UsdPrim& prim = srcItem->prim();
_stage = prim.GetStage();
_ufeSrcItem = srcItem;
_usdSrcPath = prim.GetPath();

// Every call to rename() (through execute(), undo() or redo()) removes
// a prim, which becomes expired. Since USD UFE scene items contain a
// prim, we must recreate them after every call to rename.
_usdDstPath = prim.GetParent().GetPath().AppendChild(TfToken(newName.string()));
_layer = MayaUsdUtils::defPrimSpecLayer(prim);
if (!_layer) {
std::string err = TfStringPrintf("No prim found at %s", prim.GetPath().GetString().c_str());
throw std::runtime_error(err.c_str());
}
const UsdPrim& prim = srcItem->prim();
_stage = prim.GetStage();
_ufeSrcItem = srcItem;
_usdSrcPath = prim.GetPath();

// Every call to rename() (through execute(), undo() or redo()) removes
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit-pick: indentation changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

see 4c67b69

// a prim, which becomes expired. Since USD UFE scene items contain a
// prim, we must recreate them after every call to rename.
_usdDstPath = prim.GetParent().GetPath().AppendChild(TfToken(newName.string()));

_layer = MayaUsdUtils::defPrimSpecLayer(prim);
if (!_layer) {
std::string err = TfStringPrintf("No prim found at %s", prim.GetPath().GetString().c_str());
throw std::runtime_error(err.c_str());
}

// if the current layer doesn't have any opinions that affects selected prim
if (!MayaUsdUtils::doesEditTargetLayerHavePrimSpec(prim)) {
Expand Down Expand Up @@ -88,84 +89,85 @@ UsdUndoRenameCommand::~UsdUndoRenameCommand()
{
}

/*static*/
UsdUndoRenameCommand::Ptr UsdUndoRenameCommand::create(const UsdSceneItem::Ptr& srcItem, const Ufe::PathComponent& newName)
{
return std::make_shared<UsdUndoRenameCommand>(srcItem, newName);
return std::make_shared<UsdUndoRenameCommand>(srcItem, newName);
}

UsdSceneItem::Ptr UsdUndoRenameCommand::renamedItem() const
{
return _ufeDstItem;
return _ufeDstItem;
}

bool UsdUndoRenameCommand::renameRedo()
{
// Copy the source path using CopySpec, and inactivate the source.

// We use the source layer as the destination. An alternate workflow
// would be the edit target layer be the destination:
// layer = self.fStage.GetEditTarget().GetLayer()
bool status = SdfCopySpec(_layer, _usdSrcPath, _layer, _usdDstPath);
if (status)
{
auto srcPrim = _stage->GetPrimAtPath(_usdSrcPath);
#ifdef UFE_V2_FEATURES_AVAILABLE
UFE_ASSERT_MSG(srcPrim, "Invalid prim cannot be inactivated.");
#else
assert(srcPrim);
#endif
status = srcPrim.SetActive(false);
// Copy the source path using CopySpec, and remove the source.

// We use the source layer as the destination. An alternate workflow
// would be the edit target layer be the destination:
// _layer = _stage->GetEditTarget().GetLayer()
bool status = SdfCopySpec(_layer, _usdSrcPath, _layer, _usdDstPath);
if (status) {
// remove all scene description for the given path and
// its subtree in the current UsdEditTarget
{
UsdEditContext ctx(_stage, _layer);
status = _stage->RemovePrim(_ufeSrcItem->prim().GetPath());
}
Copy link
Contributor Author

@HamedSabri-adsk HamedSabri-adsk May 6, 2020

Choose a reason for hiding this comment

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

With the new restriction in place it is now safe to remove the prim from the scene graph rather than deactivating it.When RemovePrim is called, I simply pass the _ufeSrcItem->prim().GetPath() rather than creating a new srcPrim which would be really unnecessary.

Redo operation:
1- Call SdfCopySpec to copy ALL the child specs from _usdSrcPath to _usdDstPath. This is a crucial call.
2- Remove the prim at _ufeSrcItem USD path.
3- set _ufeDstItem by calling createSiblingSceneItem
4- send rename notification


if (status) {
// The renamed scene item is a "sibling" of its original name.
auto ufeSrcPath = _ufeSrcItem->path();
_ufeDstItem = createSiblingSceneItem(
ufeSrcPath, _usdDstPath.GetElementString());
_ufeDstItem = createSiblingSceneItem(_ufeSrcItem->path(), _usdDstPath.GetElementString());

Ufe::ObjectRename notification(_ufeDstItem, ufeSrcPath);
Ufe::Scene::notifyObjectPathChange(notification);
sendRenameNotification(_ufeDstItem, _ufeSrcItem->path());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

minor refactoring here.

}
}
else {
UFE_LOG(std::string("Warning: SdfCopySpec(") +
_usdSrcPath.GetString() + std::string(") failed."));
}

return status;
return status;
}

bool UsdUndoRenameCommand::renameUndo()
{
bool status{false};
{
// Regardless of where the edit target is currently set, switch to the
// layer where we copied the source prim into the destination, then
// restore the edit target.
UsdEditContext ctx(_stage, _layer);
status = _stage->RemovePrim(_usdDstPath);
}
// Copy the source path using CopySpec, and remove the source.
bool status = SdfCopySpec(_layer, _usdDstPath, _layer, _usdSrcPath);

if (status) {
auto srcPrim = _stage->GetPrimAtPath(_usdSrcPath);
#ifdef UFE_V2_FEATURES_AVAILABLE
UFE_ASSERT_MSG(srcPrim, "Invalid prim cannot be activated.");
#else
assert(srcPrim);
#endif
status = srcPrim.SetActive(true);
// remove all scene description for the given path and
// its subtree in the current UsdEditTarget
{
UsdEditContext ctx(_stage, _layer);
status = _stage->RemovePrim(_usdDstPath);
}

if (status) {
Ufe::ObjectRename notification(_ufeSrcItem, _ufeDstItem->path());
Ufe::Scene::notifyObjectPathChange(notification);
// create a new prim at _usdSrcPath
auto newPrim = _stage->DefinePrim(_usdSrcPath);

#ifdef UFE_V2_FEATURES_AVAILABLE
UFE_ASSERT_MSG(newPrim, "Invalid prim cannot be inactivated.");
#else
assert(newPrim);
#endif

// I shouldn't have to again create a sibling sceneItem here since we already have a valid _ufeSrcItem
// however, I get random crashes if I don't which needs furthur investigation. HS, 6-May-2020.
_ufeSrcItem = createSiblingSceneItem(_ufeDstItem->path(), _usdSrcPath.GetElementString());

sendRenameNotification(_ufeSrcItem, _ufeDstItem->path());

_ufeDstItem = nullptr;
}
}
}
else {
UFE_LOG(std::string("Warning: RemovePrim(") +
UFE_LOG(std::string("Warning: SdfCopySpec(") +
_usdDstPath.GetString() + std::string(") failed."));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

On Undo, we do an opposite of Redo:

1- Call SdfCopySpec to copy ALL the child specs from _usdDstPath to _usdSrcPath. This is a crucial call.
2- Remove the prim at the _usdDstPath.
3- Create a new prim from _usdSrcPath and check if have a valid object to continue.
4- set _ufeSrcItem by calling createSiblingSceneItem ( read comments in code )
5- send rename notification
6- set _ufeDstItem to nullptr


return status;
return status;
}

//------------------------------------------------------------------------------
Expand All @@ -174,24 +176,24 @@ bool UsdUndoRenameCommand::renameUndo()

void UsdUndoRenameCommand::undo()
{
// MAYA-92264: Pixar bug prevents undo from working. Try again with USD
// version 0.8.5 or later. PPT, 7-Jul-2018.
try {
// MAYA-92264: Pixar bug prevents undo from working. Try again with USD
Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment is no longer valid, right?

// version 0.8.5 or later. PPT, 7-Jul-2018.
try {
InPathChange pc;
if (!renameUndo()) {
if (!renameUndo()) {
UFE_LOG("rename undo failed");
}
}
catch (const std::exception& e) {
UFE_LOG(e.what());
throw; // re-throw the same exception
}
}
catch (const std::exception& e) {
UFE_LOG(e.what());
throw; // re-throw the same exception
}
}

void UsdUndoRenameCommand::redo()
{
InPathChange pc;
if (!renameRedo()) {
if (!renameRedo()) {
UFE_LOG("rename redo failed");
}
}
Expand Down
45 changes: 21 additions & 24 deletions lib/mayaUsd/ufe/UsdUndoRenameCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
#include <ufe/pathComponent.h>
#include <ufe/undoableCommand.h>

#include <pxr/usd/sdf/path.h>
#include <pxr/usd/usd/prim.h>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These headers are not needed here.

#include <mayaUsd/base/api.h>
#include <mayaUsd/ufe/UsdSceneItem.h>

Expand All @@ -34,39 +31,39 @@ namespace ufe {
class MAYAUSD_CORE_PUBLIC UsdUndoRenameCommand : public Ufe::UndoableCommand
{
public:
typedef std::shared_ptr<UsdUndoRenameCommand> Ptr;
typedef std::shared_ptr<UsdUndoRenameCommand> Ptr;

UsdUndoRenameCommand(const UsdSceneItem::Ptr& srcItem, const Ufe::PathComponent& newName);
~UsdUndoRenameCommand() override;
UsdUndoRenameCommand(const UsdSceneItem::Ptr& srcItem, const Ufe::PathComponent& newName);
~UsdUndoRenameCommand() override;

// Delete the copy/move constructors assignment operators.
UsdUndoRenameCommand(const UsdUndoRenameCommand&) = delete;
UsdUndoRenameCommand& operator=(const UsdUndoRenameCommand&) = delete;
UsdUndoRenameCommand(UsdUndoRenameCommand&&) = delete;
UsdUndoRenameCommand& operator=(UsdUndoRenameCommand&&) = delete;
// Delete the copy/move constructors assignment operators.
UsdUndoRenameCommand(const UsdUndoRenameCommand&) = delete;
UsdUndoRenameCommand& operator=(const UsdUndoRenameCommand&) = delete;
UsdUndoRenameCommand(UsdUndoRenameCommand&&) = delete;
UsdUndoRenameCommand& operator=(UsdUndoRenameCommand&&) = delete;

//! Create a UsdUndoRenameCommand from a USD scene item and UFE pathcomponent.
static UsdUndoRenameCommand::Ptr create(const UsdSceneItem::Ptr& srcItem, const Ufe::PathComponent& newName);
//! Create a UsdUndoRenameCommand from a USD scene item and UFE pathcomponent.
static UsdUndoRenameCommand::Ptr create(const UsdSceneItem::Ptr& srcItem, const Ufe::PathComponent& newName);

UsdSceneItem::Ptr renamedItem() const;
UsdSceneItem::Ptr renamedItem() const;

private:

// UsdUndoRenameCommand overrides
void undo() override;
void redo() override;
// UsdUndoRenameCommand overrides
void undo() override;
void redo() override;

bool renameRedo();
bool renameUndo();
bool renameRedo();
bool renameUndo();

UsdStageWeakPtr _stage;
SdfLayerHandle _layer;
UsdStageWeakPtr _stage;
SdfLayerHandle _layer;

UsdSceneItem::Ptr _ufeSrcItem;
SdfPath _usdSrcPath;
SdfPath _usdSrcPath;

UsdSceneItem::Ptr _ufeDstItem;
SdfPath _usdDstPath;
UsdSceneItem::Ptr _ufeDstItem;
SdfPath _usdDstPath;

}; // UsdUndoRenameCommand

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have converted tabs to dots. If you find them distracting, Github allow hiding whitespace changes during review:

Please see
https://medium.com/@kevinpmcc/hide-whitespace-for-easier-github-code-reviews-d3f476d03863

Expand Down
9 changes: 9 additions & 0 deletions lib/mayaUsd/ufe/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include <string>
#include <unordered_map>

#include <ufe/scene.h>
#include <ufe/sceneNotification.h>

#include <maya/MGlobal.h>
#include <maya/MSelectionList.h>
#include <maya/MObjectHandle.h>
Expand Down Expand Up @@ -242,5 +245,11 @@ UsdTimeCode getTime(const Ufe::Path& path)
return proxyShape->getTime();
}

void sendRenameNotification(const Ufe::SceneItem::Ptr& item, const Ufe::Path& previousPath)
{
Ufe::ObjectRename notification(item, previousPath);
Ufe::Scene::notifyObjectPathChange(notification);
}

} // namespace ufe
} // namespace MayaUsd
4 changes: 4 additions & 0 deletions lib/mayaUsd/ufe/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,9 @@ MDagPath nameToDagPath(const std::string& name);
MAYAUSD_CORE_PUBLIC
UsdTimeCode getTime(const Ufe::Path& path);

//! Object renamed scene notification
MAYAUSD_CORE_PUBLIC
void sendRenameNotification(const Ufe::SceneItem::Ptr& item, const Ufe::Path& previousPath);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created a utility function for sending UFE scene rename notification.

} // namespace ufe
} // namespace MayaUsd