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 all commits
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
1 change: 1 addition & 0 deletions lib/mayaUsd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ target_link_libraries(${PROJECT_NAME}
vt
$<$<BOOL:${UFE_FOUND}>:${UFE_LIBRARY}>
${MAYA_LIBRARIES}
mayaUsdUtils
PRIVATE
Boost::filesystem
Boost::system
Expand Down
4 changes: 3 additions & 1 deletion lib/mayaUsd/ufe/UsdHierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include <mayaUsd/ufe/Utils.h>

#include <mayaUsdUtils/util.h>

#include "private/InPathChange.h"
#include "private/Utils.h"

Expand Down Expand Up @@ -137,7 +139,7 @@ Ufe::AppendedChild UsdHierarchy::appendChild(const Ufe::SceneItem::Ptr& child)
auto usdSrcPath = prim.GetPath();
auto ufeDstPath = fItem->path() + childName;
auto usdDstPath = fPrim.GetPath().AppendChild(TfToken(childName));
SdfLayerHandle layer = defPrimSpecLayer(prim);
SdfLayerHandle layer = MayaUsdUtils::defPrimSpecLayer(prim);
if (!layer) {
std::string err = TfStringPrintf("No prim found at %s", usdSrcPath.GetString().c_str());
throw std::runtime_error(err.c_str());
Expand Down
4 changes: 3 additions & 1 deletion lib/mayaUsd/ufe/UsdUndoDuplicateCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include <mayaUsd/ufe/Utils.h>

#include <mayaUsdUtils/util.h>

MAYAUSD_NS_DEF {
namespace ufe {

Expand Down Expand Up @@ -73,7 +75,7 @@ void UsdUndoDuplicateCommand::primInfo(const UsdPrim& srcPrim, SdfPath& usdDstPa
// each layer in which there is an over or a def, until we reach the
// layer with a def primSpec. This would preserve the visual appearance
// of the duplicate. PPT, 12-Jun-2018.
srcLayer = defPrimSpecLayer(srcPrim);
srcLayer = MayaUsdUtils::defPrimSpecLayer(srcPrim);
if (!srcLayer) {
std::string err = TfStringPrintf("No prim found at %s", srcPrim.GetPath().GetString().c_str());
throw std::runtime_error(err.c_str());
Expand Down
4 changes: 3 additions & 1 deletion lib/mayaUsd/ufe/UsdUndoInsertChildCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <cassert>
#endif

#include <mayaUsdUtils/util.h>

#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usd/editContext.h>
#include <pxr/usd/sdf/copyUtils.h>
Expand Down Expand Up @@ -62,7 +64,7 @@ UsdUndoInsertChildCommand::UsdUndoInsertChildCommand(
}
fUsdDstPath = parent->prim().GetPath().AppendChild(TfToken(childName));

fLayer = defPrimSpecLayer(childPrim);
fLayer = MayaUsdUtils::defPrimSpecLayer(childPrim);
if (!fLayer) {
std::string err = TfStringPrintf("No prim found at %s", childPrim.GetPath().GetString().c_str());
throw std::runtime_error(err.c_str());
Expand Down
216 changes: 128 additions & 88 deletions lib/mayaUsd/ufe/UsdUndoRenameCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@
//
#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>

#include <mayaUsdUtils/util.h>

#include "private/InPathChange.h"

#ifdef UFE_V2_FEATURES_AVAILABLE
Expand All @@ -38,132 +40,170 @@
MAYAUSD_NS_DEF {
namespace ufe {

/*
HS, May 15, 2020

See usd-interest: Question around SdfPrimSepc's SetName routine

SdfPrimSpec::SetName() will rename any prim in the layer, but it does not allow you to reparent the prim,
nor will it update any relationship or connection targets in the layer that targeted the prim or any of its
decendants (they will all break unless you fix them up yourself.Renaming and reparenting prims destructively
in composed scenes is pretty tricky stuff that cannot really practically be done with 100% guarantees.
*/

UsdUndoRenameCommand::UsdUndoRenameCommand(const UsdSceneItem::Ptr& srcItem, const Ufe::PathComponent& newName)
: Ufe::UndoableCommand()
: Ufe::UndoableCommand()
, _ufeSrcItem(srcItem)
, _ufeDstItem(nullptr)
, _stage(_ufeSrcItem->prim().GetStage())
, _newName(newName.string())
{
const UsdPrim& prim = srcItem->prim();
fStage = prim.GetStage();
fUfeSrcItem = srcItem;
fUsdSrcPath = 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.
fUsdDstPath = prim.GetParent().GetPath().AppendChild(TfToken(newName.string()));
fLayer = defPrimSpecLayer(prim);
if (!fLayer) {
std::string err = TfStringPrintf("No prim found at %s", prim.GetPath().GetString().c_str());
throw std::runtime_error(err.c_str());
}
const UsdPrim& prim = _stage->GetPrimAtPath(_ufeSrcItem->prim().GetPath());

// if the current layer doesn't have any contributions
if (!MayaUsdUtils::doesEditTargetLayerContribute(prim)) {
auto strongestContributingLayer = MayaUsdUtils::strongestContributingLayer(prim);
std::string err = TfStringPrintf("Cannot rename [%s] defined on another layer. "
"Please set [%s] as the target layer to proceed",
prim.GetName().GetString().c_str(),
strongestContributingLayer->GetDisplayName().c_str());
throw std::runtime_error(err.c_str());
}
Copy link
Contributor Author

@HamedSabri-adsk HamedSabri-adsk Apr 29, 2020

Choose a reason for hiding this comment

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

We check here if the target layer has any opinions that affects selected prim. If it doesn't pass the check, we then find the possible target layer and append it's display name to the error message. Finally we throw a runtime_error message.

else
{
// account for internal vs external references
// internal references (references without a file path specified) from the same file
// should be renamable.
if (prim.HasAuthoredReferences()) {
auto primSpec = MayaUsdUtils::getPrimSpecAtEditTarget(prim);

if(!MayaUsdUtils::isInternalReference(primSpec)) {
std::string err = TfStringPrintf("Unable to rename referenced object [%s]",
prim.GetName().GetString().c_str());
throw std::runtime_error(err.c_str());
}
}

auto layers = MayaUsdUtils::layersWithContribution(prim);
// if we have more than 2 layers that contributes to the final composed prim
if (layers.size() > 1) {
std::string layerDisplayNames;
for (auto layer : layers) {
layerDisplayNames.append("[" + layer->GetDisplayName() + "]" + ",");
}
layerDisplayNames.pop_back();
std::string err = TfStringPrintf("Cannot rename [%s] with definitions or opinions on other layers. "
"Opinions exist in %s", prim.GetName().GetString().c_str(), layerDisplayNames.c_str());
throw std::runtime_error(err.c_str());
}
}
}

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 fUfeDstItem;
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(fLayer, fUsdSrcPath, fLayer, fUsdDstPath);
if (status)
{
auto srcPrim = fStage->GetPrimAtPath(fUsdSrcPath);
#ifdef UFE_V2_FEATURES_AVAILABLE
UFE_ASSERT_MSG(srcPrim, "Invalid prim cannot be inactivated.");
#else
assert(srcPrim);
#endif
status = srcPrim.SetActive(false);
const UsdPrim& prim = _stage->GetPrimAtPath(_ufeSrcItem->prim().GetPath());

if (status) {
// The renamed scene item is a "sibling" of its original name.
auto ufeSrcPath = fUfeSrcItem->path();
fUfeDstItem = createSiblingSceneItem(
ufeSrcPath, fUsdDstPath.GetElementString());
auto primSpec = MayaUsdUtils::getPrimSpecAtEditTarget(prim);
if(!primSpec) {
return false;
}

Ufe::ObjectRename notification(fUfeDstItem, ufeSrcPath);
Ufe::Scene::notifyObjectPathChange(notification);
}
// these two lines MUST be called before the set name
// _stage->GetDefaultPrim() and prim after the rename can be invalid.
auto primPath = prim.GetPath();
auto defaultPrimPath = _stage->GetDefaultPrim().GetPath();

// set prim's name
// XXX: SetName successfuly returns true but when you examine the _prim.GetName()
// after the rename, the prim name shows the original name HS, 6-May-2020.
bool status = primSpec->SetName(_newName);
if (!status) {
return false;
}
else {
UFE_LOG(std::string("Warning: SdfCopySpec(") +
fUsdSrcPath.GetString() + std::string(") failed."));

// the renamed scene item is a "sibling" of its original name.
_ufeDstItem = createSiblingSceneItem(_ufeSrcItem->path(), _newName);
sendRenameNotification(_ufeDstItem, _ufeSrcItem->path());

// SdfLayer is a "simple" container, and all it knows about defaultPrim is that it is a piece of token-valued layer metadata.
// It is only the higher-level Usd and Pcp modules that know that it is identifying a prim on the stage.
// One must use the SdfLayer API for setting the defaultPrim when you rename the prim it identifies.
if(primPath == defaultPrimPath){
_stage->SetDefaultPrim(_ufeDstItem->prim());
}

return status;
return true;
}

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(fStage, fLayer);
status = fStage->RemovePrim(fUsdDstPath);
const UsdPrim& prim = _stage->GetPrimAtPath(_ufeDstItem->prim().GetPath());

auto primSpec = MayaUsdUtils::getPrimSpecAtEditTarget(prim);
if(!primSpec) {
return false;
}
if (status) {
auto srcPrim = fStage->GetPrimAtPath(fUsdSrcPath);
#ifdef UFE_V2_FEATURES_AVAILABLE
UFE_ASSERT_MSG(srcPrim, "Invalid prim cannot be activated.");
#else
assert(srcPrim);
#endif
status = srcPrim.SetActive(true);

if (status) {
Ufe::ObjectRename notification(fUfeSrcItem, fUfeDstItem->path());
Ufe::Scene::notifyObjectPathChange(notification);
fUfeDstItem = nullptr;
}
}
else {
UFE_LOG(std::string("Warning: RemovePrim(") +
fUsdDstPath.GetString() + std::string(") failed."));
// these two lines MUST be called before the set name
// _stage->GetDefaultPrim() and prim after the rename can be invalid.
auto primPath = prim.GetPath();
auto defaultPrimPath = _stage->GetDefaultPrim().GetPath();

// set prim's name
bool status = primSpec->SetName(_ufeSrcItem->prim().GetName());
if (!status) {
return false;
}

return status;
}
// 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(), _ufeSrcItem->prim().GetName());
sendRenameNotification(_ufeSrcItem, _ufeDstItem->path());

// SdfLayer is a "simple" container, and all it knows about defaultPrim is that it is a piece of token-valued layer metadata.
// It is only the higher-level Usd and Pcp modules that know that it is identifying a prim on the stage.
// One must use the SdfLayer API for setting the defaultPrim when you rename the prim it identifies.
if (primPath == defaultPrimPath) {
_stage->SetDefaultPrim(_ufeSrcItem->prim());
}
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


//------------------------------------------------------------------------------
// UsdUndoRenameCommand overrides
//------------------------------------------------------------------------------
_ufeDstItem = nullptr;

return true;
}

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 {
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
Loading