Skip to content

Commit

Permalink
Address feedbacks:
Browse files Browse the repository at this point in the history
- renamed usd utility functions related to "usd" and "sdf" operations
- moved usd utility functions from mayaUsd\ufe\Utils.h to usd\utils\util.h under MayaUsdUtils namespace.
- cleaned up variable names according to c++ guideline.
  • Loading branch information
Hamed Sabri committed Apr 30, 2020
1 parent c9100f7 commit 54166d2
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 94 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
48 changes: 25 additions & 23 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,23 +44,23 @@ UsdUndoRenameCommand::UsdUndoRenameCommand(const UsdSceneItem::Ptr& srcItem, con
: Ufe::UndoableCommand()
{
const UsdPrim& prim = srcItem->prim();
m_fStage = prim.GetStage();
m_fUfeSrcItem = srcItem;
m_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.
m_fUsdDstPath = prim.GetParent().GetPath().AppendChild(TfToken(newName.string()));
m_fLayer = defPrimSpecLayer(prim);
if (!m_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());
}

// check if the target layer has any opinions that affects selected prim
if (!isTargetLayerHaveOpinion(prim)) {
auto possibleTargetLayer = targetLayerWithOpion(prim);
// check if a layer has any opinions that affects selected prim
if (!MayaUsdUtils::doesLayerHavePrimSpec(prim)) {
auto possibleTargetLayer = MayaUsdUtils::strongestLayerWithPrimSpec(prim);
std::string err = TfStringPrintf("Unable to rename [%s] on current target layer, "
"Please set [%s] as the target layer to proceed",
prim.GetName().GetString(),
Expand All @@ -79,7 +81,7 @@ UsdUndoRenameCommand::Ptr UsdUndoRenameCommand::create(const UsdSceneItem::Ptr&

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

bool UsdUndoRenameCommand::renameRedo()
Expand All @@ -89,10 +91,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(m_fLayer, m_fUsdSrcPath, m_fLayer, m_fUsdDstPath);
bool status = SdfCopySpec(_layer, _usdSrcPath, _layer, _usdDstPath);
if (status)
{
auto srcPrim = m_fStage->GetPrimAtPath(m_fUsdSrcPath);
auto srcPrim = _stage->GetPrimAtPath(_usdSrcPath);
#ifdef UFE_V2_FEATURES_AVAILABLE
UFE_ASSERT_MSG(srcPrim, "Invalid prim cannot be inactivated.");
#else
Expand All @@ -102,17 +104,17 @@ bool UsdUndoRenameCommand::renameRedo()

if (status) {
// The renamed scene item is a "sibling" of its original name.
auto ufeSrcPath = m_fUfeSrcItem->path();
m_fUfeDstItem = createSiblingSceneItem(
ufeSrcPath, m_fUsdDstPath.GetElementString());
auto ufeSrcPath = _ufeSrcItem->path();
_ufeDstItem = createSiblingSceneItem(
ufeSrcPath, _usdDstPath.GetElementString());

Ufe::ObjectRename notification(m_fUfeDstItem, ufeSrcPath);
Ufe::ObjectRename notification(_ufeDstItem, ufeSrcPath);
Ufe::Scene::notifyObjectPathChange(notification);
}
}
else {
UFE_LOG(std::string("Warning: SdfCopySpec(") +
m_fUsdSrcPath.GetString() + std::string(") failed."));
_usdSrcPath.GetString() + std::string(") failed."));
}

return status;
Expand All @@ -125,11 +127,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(m_fStage, m_fLayer);
status = m_fStage->RemovePrim(m_fUsdDstPath);
UsdEditContext ctx(_stage, _layer);
status = _stage->RemovePrim(_usdDstPath);
}
if (status) {
auto srcPrim = m_fStage->GetPrimAtPath(m_fUsdSrcPath);
auto srcPrim = _stage->GetPrimAtPath(_usdSrcPath);
#ifdef UFE_V2_FEATURES_AVAILABLE
UFE_ASSERT_MSG(srcPrim, "Invalid prim cannot be activated.");
#else
Expand All @@ -138,14 +140,14 @@ bool UsdUndoRenameCommand::renameUndo()
status = srcPrim.SetActive(true);

if (status) {
Ufe::ObjectRename notification(m_fUfeSrcItem, m_fUfeDstItem->path());
Ufe::ObjectRename notification(_ufeSrcItem, _ufeDstItem->path());
Ufe::Scene::notifyObjectPathChange(notification);
m_fUfeDstItem = nullptr;
_ufeDstItem = nullptr;
}
}
else {
UFE_LOG(std::string("Warning: RemovePrim(") +
m_fUsdDstPath.GetString() + std::string(") failed."));
_usdDstPath.GetString() + std::string(") failed."));
}

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 m_fStage;
SdfLayerHandle m_fLayer;
UsdSceneItem::Ptr m_fUfeSrcItem;
SdfPath m_fUsdSrcPath;
UsdSceneItem::Ptr m_fUfeDstItem;
SdfPath m_fUsdDstPath;
UsdStageWeakPtr _stage;
SdfLayerHandle _layer;

UsdSceneItem::Ptr _ufeSrcItem;
SdfPath _usdSrcPath;

UsdSceneItem::Ptr _ufeDstItem;
SdfPath _usdDstPath;

}; // UsdUndoRenameCommand

Expand Down
53 changes: 0 additions & 53 deletions lib/mayaUsd/ufe/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,59 +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();

for (auto layer : layerStack)
{
auto primSpec = layer->GetPrimAtPath(prim.GetPath());
if (primSpec && (primSpec->GetSpecifier() == SdfSpecifierDef))
{
defLayer = layer;
break;
}
}
return defLayer;
}

bool isTargetLayerHaveOpinion(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 targetLayerWithOpion(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;
}

UsdSceneItem::Ptr createSiblingSceneItem(const Ufe::Path& ufeSrcPath, const std::string& siblingName)
{
auto ufeSiblingPath = ufeSrcPath.sibling(Ufe::PathComponent(siblingName));
Expand Down
12 changes: 0 additions & 12 deletions lib/mayaUsd/ufe/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +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);

//! Check if the target layer has any opinions that affects a particular prim
MAYAUSD_CORE_PUBLIC
bool isTargetLayerHaveOpinion(const UsdPrim& prim);

//! Return the target layer that has any opinions on a particular prim
MAYAUSD_CORE_PUBLIC
SdfLayerHandle targetLayerWithOpion(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
81 changes: 81 additions & 0 deletions lib/usd/utils/util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// 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>

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;
}

bool
MayaUsdUtils::doesLayerHavePrimSpec(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
MayaUsdUtils::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
42 changes: 42 additions & 0 deletions lib/usd/utils/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// 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.
//

#ifndef MAYAUSDUTILS_UTIL_H
#define MAYAUSDUTILS_UTIL_H

#include <mayaUsdUtils/Api.h>

#include <mayaUsdUtils/ForwardDeclares.h>

PXR_NAMESPACE_USING_DIRECTIVE

namespace MayaUsdUtils {

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

//! Check if a layer has any opinions that affects a particular prim
MAYA_USD_UTILS_PUBLIC
bool doesLayerHavePrimSpec(const UsdPrim&);

//! Return the layer that has any opinions on a particular prim
MAYA_USD_UTILS_PUBLIC
SdfLayerHandle strongestLayerWithPrimSpec(const UsdPrim&);

} // namespace MayaUsdUtils

#endif // MAYAUSDUTILS_UTIL_H

0 comments on commit 54166d2

Please sign in to comment.