-
Notifications
You must be signed in to change notification settings - Fork 201
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
Changes from 1 commit
c9100f7
54166d2
8f8295f
e21a130
4c67b69
97996bb
581932b
761143d
d58a028
20be9ac
ae2a7e0
bb1da2e
c4f247a
a45779c
b207895
890372d
a305af9
610f2ce
80aa35e
3cf2acc
7bb77b3
24001e3
7e66d41
22d6fc9
df3ff44
67fd431
7e8bef6
d84d82f
5d81f6c
ade83fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
||
|
@@ -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 | ||
// 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)) { | ||
|
@@ -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()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Redo operation: |
||
|
||
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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.")); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
return status; | ||
return status; | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,9 +19,6 @@ | |
#include <ufe/pathComponent.h> | ||
#include <ufe/undoableCommand.h> | ||
|
||
#include <pxr/usd/sdf/path.h> | ||
#include <pxr/usd/usd/prim.h> | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
||
|
@@ -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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit-pick: indentation changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see 4c67b69