-
Notifications
You must be signed in to change notification settings - Fork 202
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 6 commits
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 |
---|---|---|
|
@@ -26,6 +26,8 @@ | |
|
||
#include <mayaUsd/ufe/Utils.h> | ||
|
||
#include <mayaUsdUtils/util.h> | ||
|
||
#include "private/InPathChange.h" | ||
|
||
#ifdef UFE_V2_FEATURES_AVAILABLE | ||
|
@@ -42,18 +44,44 @@ UsdUndoRenameCommand::UsdUndoRenameCommand(const UsdSceneItem::Ptr& srcItem, con | |
: Ufe::UndoableCommand() | ||
{ | ||
const UsdPrim& prim = srcItem->prim(); | ||
fStage = prim.GetStage(); | ||
fUfeSrcItem = srcItem; | ||
fUsdSrcPath = prim.GetPath(); | ||
_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. | ||
fUsdDstPath = prim.GetParent().GetPath().AppendChild(TfToken(newName.string())); | ||
fLayer = defPrimSpecLayer(prim); | ||
if (!fLayer) { | ||
_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)) { | ||
auto possibleTargetLayer = MayaUsdUtils::strongestLayerWithPrimSpec(prim); | ||
std::string err = TfStringPrintf("Cannot rename [%s] defined on another layer. " | ||
"Please set [%s] as the target layer to proceed", | ||
prim.GetName().GetString(), | ||
possibleTargetLayer->GetDisplayName()); | ||
throw std::runtime_error(err.c_str()); | ||
} | ||
else | ||
{ | ||
auto layers = MayaUsdUtils::layersWithPrimSpec(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(), layerDisplayNames); | ||
throw std::runtime_error(err.c_str()); | ||
} | ||
} | ||
} | ||
|
||
UsdUndoRenameCommand::~UsdUndoRenameCommand() | ||
|
@@ -68,7 +96,7 @@ UsdUndoRenameCommand::Ptr UsdUndoRenameCommand::create(const UsdSceneItem::Ptr& | |
|
||
UsdSceneItem::Ptr UsdUndoRenameCommand::renamedItem() const | ||
{ | ||
return fUfeDstItem; | ||
return _ufeDstItem; | ||
} | ||
|
||
bool UsdUndoRenameCommand::renameRedo() | ||
|
@@ -78,10 +106,10 @@ bool UsdUndoRenameCommand::renameRedo() | |
// 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); | ||
bool status = SdfCopySpec(_layer, _usdSrcPath, _layer, _usdDstPath); | ||
if (status) | ||
{ | ||
auto srcPrim = fStage->GetPrimAtPath(fUsdSrcPath); | ||
auto srcPrim = _stage->GetPrimAtPath(_usdSrcPath); | ||
#ifdef UFE_V2_FEATURES_AVAILABLE | ||
UFE_ASSERT_MSG(srcPrim, "Invalid prim cannot be inactivated."); | ||
#else | ||
|
@@ -91,17 +119,17 @@ bool UsdUndoRenameCommand::renameRedo() | |
|
||
if (status) { | ||
// The renamed scene item is a "sibling" of its original name. | ||
auto ufeSrcPath = fUfeSrcItem->path(); | ||
fUfeDstItem = createSiblingSceneItem( | ||
ufeSrcPath, fUsdDstPath.GetElementString()); | ||
auto ufeSrcPath = _ufeSrcItem->path(); | ||
_ufeDstItem = createSiblingSceneItem( | ||
ufeSrcPath, _usdDstPath.GetElementString()); | ||
|
||
Ufe::ObjectRename notification(fUfeDstItem, ufeSrcPath); | ||
Ufe::ObjectRename notification(_ufeDstItem, ufeSrcPath); | ||
Ufe::Scene::notifyObjectPathChange(notification); | ||
} | ||
} | ||
else { | ||
UFE_LOG(std::string("Warning: SdfCopySpec(") + | ||
fUsdSrcPath.GetString() + std::string(") failed.")); | ||
_usdSrcPath.GetString() + std::string(") failed.")); | ||
} | ||
|
||
return status; | ||
|
@@ -114,11 +142,11 @@ bool UsdUndoRenameCommand::renameUndo() | |
// 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); | ||
UsdEditContext ctx(_stage, _layer); | ||
status = _stage->RemovePrim(_usdDstPath); | ||
} | ||
if (status) { | ||
auto srcPrim = fStage->GetPrimAtPath(fUsdSrcPath); | ||
auto srcPrim = _stage->GetPrimAtPath(_usdSrcPath); | ||
#ifdef UFE_V2_FEATURES_AVAILABLE | ||
UFE_ASSERT_MSG(srcPrim, "Invalid prim cannot be activated."); | ||
#else | ||
|
@@ -127,14 +155,14 @@ bool UsdUndoRenameCommand::renameUndo() | |
status = srcPrim.SetActive(true); | ||
|
||
if (status) { | ||
Ufe::ObjectRename notification(fUfeSrcItem, fUfeDstItem->path()); | ||
Ufe::ObjectRename notification(_ufeSrcItem, _ufeDstItem->path()); | ||
Ufe::Scene::notifyObjectPathChange(notification); | ||
fUfeDstItem = nullptr; | ||
_ufeDstItem = nullptr; | ||
} | ||
} | ||
else { | ||
UFE_LOG(std::string("Warning: RemovePrim(") + | ||
fUsdDstPath.GetString() + std::string(") failed.")); | ||
_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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,12 +59,14 @@ class MAYAUSD_CORE_PUBLIC UsdUndoRenameCommand : public Ufe::UndoableCommand | |
bool renameRedo(); | ||
bool renameUndo(); | ||
|
||
UsdStageWeakPtr fStage; | ||
SdfLayerHandle fLayer; | ||
UsdSceneItem::Ptr fUfeSrcItem; | ||
SdfPath fUsdSrcPath; | ||
UsdSceneItem::Ptr fUfeDstItem; | ||
SdfPath fUsdDstPath; | ||
UsdStageWeakPtr _stage; | ||
SdfLayerHandle _layer; | ||
|
||
UsdSceneItem::Ptr _ufeSrcItem; | ||
SdfPath _usdSrcPath; | ||
|
||
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 |
---|---|---|
|
@@ -101,29 +101,6 @@ bool isRootChild(const Ufe::Path& path) | |
return(segments[1].size() == 1); | ||
} | ||
|
||
SdfLayerHandle defPrimSpecLayer(const UsdPrim& prim) | ||
{ | ||
// Iterate over the layer stack, starting at the highest-priority layer. | ||
// The source layer is the one in which there exists a def primSpec, not | ||
// an over. | ||
|
||
SdfLayerHandle defLayer; | ||
auto layerStack = prim.GetStage()->GetLayerStack(); | ||
auto stage = prim.GetStage(); | ||
auto primFromPath = stage->GetPrimAtPath(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. remove unused variables. |
||
|
||
for (auto layer : layerStack) | ||
{ | ||
auto primSpec = layer->GetPrimAtPath(prim.GetPath()); | ||
if (primSpec && (primSpec->GetSpecifier() == SdfSpecifierDef)) | ||
{ | ||
defLayer = layer; | ||
break; | ||
} | ||
} | ||
return defLayer; | ||
} | ||
|
||
UsdSceneItem::Ptr createSiblingSceneItem(const Ufe::Path& ufeSrcPath, const std::string& siblingName) | ||
{ | ||
auto ufeSiblingPath = ufeSrcPath.sibling(Ufe::PathComponent(siblingName)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// | ||
// Copyright 2020 Autodesk | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#include "util.h" | ||
|
||
#include <pxr/usd/sdf/layer.h> | ||
#include <pxr/usd/usd/prim.h> | ||
#include <pxr/usd/usd/stage.h> | ||
|
||
#include <vector> | ||
|
||
PXR_NAMESPACE_USING_DIRECTIVE | ||
|
||
namespace MayaUsdUtils { | ||
|
||
SdfLayerHandle | ||
defPrimSpecLayer(const UsdPrim& prim) | ||
{ | ||
// Iterate over the layer stack, starting at the highest-priority layer. | ||
// The source layer is the one in which there exists a def primSpec, not | ||
// an over. | ||
|
||
SdfLayerHandle defLayer; | ||
auto layerStack = prim.GetStage()->GetLayerStack(); | ||
|
||
for (auto layer : layerStack) { | ||
auto primSpec = layer->GetPrimAtPath(prim.GetPath()); | ||
if (primSpec && (primSpec->GetSpecifier() == SdfSpecifierDef)) { | ||
defLayer = layer; | ||
break; | ||
} | ||
} | ||
return defLayer; | ||
} | ||
|
||
std::vector<SdfLayerHandle> | ||
layersWithPrimSpec(const UsdPrim& prim) | ||
{ | ||
// get the list of PrimSpecs that provide opinions for this prim | ||
// ordered from strongest to weakest. | ||
const auto& primStack = prim.GetPrimStack(); | ||
|
||
std::vector<SdfLayerHandle> layersWithPrimSpec; | ||
for (auto primSpec : primStack) { | ||
layersWithPrimSpec.emplace_back(primSpec->GetLayer()); | ||
} | ||
|
||
return layersWithPrimSpec; | ||
} | ||
|
||
bool | ||
doesEditTargetLayerHavePrimSpec(const UsdPrim& prim) | ||
{ | ||
auto editTarget = prim.GetStage()->GetEditTarget(); | ||
auto layer = editTarget.GetLayer(); | ||
auto primSpec = layer->GetPrimAtPath(prim.GetPath()); | ||
|
||
// to know whether the target layer contains any opinions that | ||
// affect a particular prim, there must be a primSpec for that prim | ||
if (!primSpec) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
SdfLayerHandle | ||
strongestLayerWithPrimSpec(const UsdPrim& prim) | ||
{ | ||
SdfLayerHandle targetLayer; | ||
auto layerStack = prim.GetStage()->GetLayerStack(); | ||
for (auto layer : layerStack) | ||
{ | ||
// to know whether the target layer contains any opinions that | ||
// affect a particular prim, there must be a primSpec for that prim | ||
auto primSpec = layer->GetPrimAtPath(prim.GetPath()); | ||
if (primSpec) { | ||
targetLayer = layer; | ||
break; | ||
} | ||
} | ||
return targetLayer; | ||
} | ||
|
||
} // MayaUsdUtils |
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.
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.