Skip to content

Commit

Permalink
Merge pull request #475 from Autodesk/sabrih/MAYA-104215/improve_usd_…
Browse files Browse the repository at this point in the history
…rename_operation

MAYA-104215: improve renaming restriction on prim objects
  • Loading branch information
Krystian Ligenza authored May 20, 2020
2 parents b0a4fd4 + ade83fa commit 2ae67f5
Show file tree
Hide file tree
Showing 20 changed files with 1,019 additions and 180 deletions.
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());
}
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());
}

//------------------------------------------------------------------------------
// 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

0 comments on commit 2ae67f5

Please sign in to comment.