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 6 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 @@ -134,7 +136,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
68 changes: 48 additions & 20 deletions lib/mayaUsd/ufe/UsdUndoRenameCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include <mayaUsd/ufe/Utils.h>

#include <mayaUsdUtils/util.h>

#include "private/InPathChange.h"

#ifdef UFE_V2_FEATURES_AVAILABLE
Expand All @@ -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());
}
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
{
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()
Expand All @@ -68,7 +96,7 @@ UsdUndoRenameCommand::Ptr UsdUndoRenameCommand::create(const UsdSceneItem::Ptr&

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

bool UsdUndoRenameCommand::renameRedo()
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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."));
}
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;
Expand Down
14 changes: 8 additions & 6 deletions lib/mayaUsd/ufe/UsdUndoRenameCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

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
23 changes: 0 additions & 23 deletions lib/mayaUsd/ufe/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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));
Expand Down
4 changes: 0 additions & 4 deletions lib/mayaUsd/ufe/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ UsdPrim ufePathToPrim(const Ufe::Path& path);
MAYAUSD_CORE_PUBLIC
bool isRootChild(const Ufe::Path& path);

//! Return the highest-priority layer where the prim has a def primSpec.
MAYAUSD_CORE_PUBLIC
SdfLayerHandle defPrimSpecLayer(const UsdPrim& prim);

MAYAUSD_CORE_PUBLIC
UsdSceneItem::Ptr createSiblingSceneItem(const Ufe::Path& ufeSrcPath, const std::string& siblingName);

Expand Down
3 changes: 3 additions & 0 deletions lib/usd/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ target_sources(${TARGET_NAME}
PRIVATE
DebugCodes.cpp
DiffCore.cpp
util.cpp
)

# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -38,6 +39,7 @@ target_link_libraries(${TARGET_NAME}
PUBLIC
gf
usd
sdf
)

# -----------------------------------------------------------------------------
Expand All @@ -50,6 +52,7 @@ set(headers
DiffCore.h
ForwardDeclares.h
SIMD.h
util.h
)

mayaUsd_promoteHeaderList(
Expand Down
98 changes: 98 additions & 0 deletions lib/usd/utils/util.cpp
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
Loading