From 4f201d3cff8053ccd0061da9a9548c450d489119 Mon Sep 17 00:00:00 2001 From: gz-adsk Date: Fri, 9 Feb 2024 17:11:12 +0000 Subject: [PATCH 1/4] EMSUSD-1066 - Minimal API needed by LookdevX to decouple from mayaUsd --- lib/mayaUsdAPI/CMakeLists.txt | 2 + lib/mayaUsdAPI/utils.cpp | 157 ++++++++++++++++++++++++++++++++++ lib/mayaUsdAPI/utils.h | 135 +++++++++++++++++++++++++++++ 3 files changed, 294 insertions(+) create mode 100644 lib/mayaUsdAPI/utils.cpp create mode 100644 lib/mayaUsdAPI/utils.h diff --git a/lib/mayaUsdAPI/CMakeLists.txt b/lib/mayaUsdAPI/CMakeLists.txt index ee852a32b8..80c476f530 100644 --- a/lib/mayaUsdAPI/CMakeLists.txt +++ b/lib/mayaUsdAPI/CMakeLists.txt @@ -24,12 +24,14 @@ target_sources(${PROJECT_NAME} PRIVATE proxyStage.cpp proxyShapeNotice.cpp + utils.cpp ) set(HEADERS api.h proxyStage.h proxyShapeNotice.h + utils.h ) #------------------------------------------------------------------------------ diff --git a/lib/mayaUsdAPI/utils.cpp b/lib/mayaUsdAPI/utils.cpp new file mode 100644 index 0000000000..aa6d998e6a --- /dev/null +++ b/lib/mayaUsdAPI/utils.cpp @@ -0,0 +1,157 @@ +// +// Copyright 2023 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 "utils.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace MAYAUSDAPI_NS_DEF { + +Ufe::Rtid getMayaRunTimeId() { return MayaUsd::ufe::getMayaRunTimeId(); } + +bool isConnected(const PXR_NS::UsdAttribute& srcUsdAttr, const PXR_NS::UsdAttribute& dstUsdAttr) +{ + return MayaUsd::ufe::isConnected(srcUsdAttr, dstUsdAttr); +} + +PXR_NS::UsdStageWeakPtr usdStage(const Ufe::Attribute::Ptr& attribute) +{ + if (auto usdAttribute = std::dynamic_pointer_cast(attribute)) { + return usdAttribute->usdPrim().GetStage(); + } + return nullptr; +} + +PXR_NS::SdfValueTypeName usdAttributeType(const Ufe::Attribute::Ptr& attribute) +{ + if (auto usdAttribute = std::dynamic_pointer_cast(attribute)) { + return usdAttribute->usdAttributeType(); + } + return PXR_NS::SdfValueTypeName(); +} + +bool getUsdValue( + const Ufe::Attribute::Ptr& attribute, + PXR_NS::VtValue& value, + PXR_NS::UsdTimeCode time) +{ + if (auto usdAttribute = std::dynamic_pointer_cast(attribute)) { + return usdAttribute->get(value, time); + } + return false; +} + +Ufe::InsertChildCommand::Ptr +addNewMaterialCommand(const Ufe::SceneItem::Ptr& parentItem, const std::string& sdrShaderIdentifier) +{ + if (auto usdSceneItem = std::dynamic_pointer_cast(parentItem)) { + return MayaUsd::ufe::UsdUndoAddNewMaterialCommand::create( + usdSceneItem, sdrShaderIdentifier); + } + return nullptr; +} + +Ufe::SceneItemResultUndoableCommand::Ptr +createMaterialsScopeCommand(const Ufe::SceneItem::Ptr& parentItem) +{ + if (auto usdSceneItem = std::dynamic_pointer_cast(parentItem)) { + return MayaUsd::ufe::UsdUndoCreateMaterialsScopeCommand::create(usdSceneItem); + } + return nullptr; +} + +Ufe::SceneItemResultUndoableCommand::Ptr +createStageWithNewLayerCommand(const Ufe::SceneItem::Ptr& parentItem) +{ + return MayaUsd::ufe::UsdUndoCreateStageWithNewLayerCommand::create(parentItem); +} + +bool isMaterialsScope(const Ufe::SceneItem::Ptr& item) +{ + return MayaUsd::ufe::isMaterialsScope(item); +} + +bool isAGatewayType(const std::string& mayaNodeType) +{ + return MayaUsd::ufe::isAGatewayType(mayaNodeType); +} + +bool mergePrims( + const PXR_NS::UsdStageRefPtr& srcStage, + const PXR_NS::SdfLayerRefPtr& srcLayer, + const PXR_NS::SdfPath& srcPath, + const PXR_NS::UsdStageRefPtr& dstStage, + const PXR_NS::SdfLayerRefPtr& dstLayer, + const PXR_NS::SdfPath& dstPath) +{ + MayaUsdUtils::MergePrimsOptions options; + options.verbosity = MayaUsdUtils::MergeVerbosity::None; + return MayaUsdUtils::mergePrims( + srcStage, srcLayer, srcPath, dstStage, dstLayer, dstPath, options); +} + +std::string getDir(const std::string& fullFilePath) +{ + return PXR_NS::UsdMayaUtilFileSystem::getDir(fullFilePath); +} + +std::pair +makePathRelativeTo(const std::string& fileName, const std::string& relativeToDir) +{ + return PXR_NS::UsdMayaUtilFileSystem::makePathRelativeTo(fileName, relativeToDir); +} + +bool requireUsdPathsRelativeToEditTargetLayer() +{ + return PXR_NS::UsdMayaUtilFileSystem::requireUsdPathsRelativeToEditTargetLayer(); +} + +Ufe::AttributesHandler::Ptr createAttributesHandler() +{ + return MayaUsd::ufe::UsdAttributesHandler::create(); +} + +Ufe::NodeDefHandler::Ptr createNodeDefHandler() +{ + return MayaUsd::ufe::UsdShaderNodeDefHandler::create(); +} + +Ufe::ConnectionHandler::Ptr createConnectionHandler() +{ + return MayaUsd::ufe::UsdConnectionHandler::create(); +} + +Ufe::SceneItemOpsHandler::Ptr createSceneItemOpsHandler() +{ + return MayaUsd::ufe::UsdSceneItemOpsHandler::create(); +} + +Ufe::UINodeGraphNodeHandler::Ptr createUINodeGraphNodeHandler() +{ + return MayaUsd::ufe::UsdUINodeGraphNodeHandler::create(); +} + +} // End of namespace MAYAUSDAPI_NS_DEF \ No newline at end of file diff --git a/lib/mayaUsdAPI/utils.h b/lib/mayaUsdAPI/utils.h new file mode 100644 index 0000000000..443096431b --- /dev/null +++ b/lib/mayaUsdAPI/utils.h @@ -0,0 +1,135 @@ +// +// Copyright 2023 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 MAYAUSDAPI_UTILS_H +#define MAYAUSDAPI_UTILS_H + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace MAYAUSDAPI_NS_DEF { + +//! Returns the currently registered Ufe runtime id for Maya. +MAYAUSD_API_PUBLIC +Ufe::Rtid getMayaRunTimeId(); + +//! Returns whether or not the two src and dst Usd attributes are connected. +MAYAUSD_API_PUBLIC +bool isConnected(const PXR_NS::UsdAttribute& srcUsdAttr, const PXR_NS::UsdAttribute& dstUsdAttr); + +//! Returns the Usd stage this attribute belongs to. +MAYAUSD_API_PUBLIC +PXR_NS::UsdStageWeakPtr usdStage(const Ufe::Attribute::Ptr& attribute); + +//! Returns the native Usd attribute type of this attribute. +MAYAUSD_API_PUBLIC +PXR_NS::SdfValueTypeName usdAttributeType(const Ufe::Attribute::Ptr& attribute); + +/*! Populates the passed value argument with the Usd value stored in the attribute at the given + * time, returning true if it succeeds. + */ +MAYAUSD_API_PUBLIC +bool getUsdValue( + const Ufe::Attribute::Ptr& attribute, + PXR_NS::VtValue& value, + PXR_NS::UsdTimeCode time); + +/*! Returns a Ufe command that can create a new material based on the given shader identifier or + * nullptr if the parent item is not a valid Usd item. + */ +MAYAUSD_API_PUBLIC +Ufe::InsertChildCommand::Ptr addNewMaterialCommand( + const Ufe::SceneItem::Ptr& parentItem, + const std::string& sdrShaderIdentifier); + +/*! Returns a Ufe command that can create a material scope or nullptr if the parent item is not a + * valid Usd item. + */ +MAYAUSD_API_PUBLIC +Ufe::SceneItemResultUndoableCommand::Ptr +createMaterialsScopeCommand(const Ufe::SceneItem::Ptr& parentItem); + +//! Returns a Ufe command that can create a new Usd stage with a new layer. +MAYAUSD_API_PUBLIC +Ufe::SceneItemResultUndoableCommand::Ptr +createStageWithNewLayerCommand(const Ufe::SceneItem::Ptr& parentItem); + +//! Returns whether or not the given item is a materials scope. +MAYAUSD_API_PUBLIC +bool isMaterialsScope(const Ufe::SceneItem::Ptr& item); + +//! Returns whether or not the given Ufe node type corresponds to a gateway Maya node. +MAYAUSD_API_PUBLIC +bool isAGatewayType(const std::string& mayaNodeType); + +/*! Merges prims starting at a source path from a source layer and stage to a destination, returning + * true if it succeeds. + */ +MAYAUSD_API_PUBLIC +bool mergePrims( + const PXR_NS::UsdStageRefPtr& srcStage, + const PXR_NS::SdfLayerRefPtr& srcLayer, + const PXR_NS::SdfPath& srcPath, + const PXR_NS::UsdStageRefPtr& dstStage, + const PXR_NS::SdfLayerRefPtr& dstLayer, + const PXR_NS::SdfPath& dstPath); + +//! Returns the directory part of the given file path. +MAYAUSD_API_PUBLIC +std::string getDir(const std::string& fullFilePath); + +//! Takes in two absolute file paths and computes a relative path of the first one to second one. +MAYAUSD_API_PUBLIC +std::pair +makePathRelativeTo(const std::string& fileName, const std::string& relativeToDir); + +/*! Returns the flag specifying whether Usd file paths should be saved as relative to the current + * edit target layer. + */ +MAYAUSD_API_PUBLIC +bool requireUsdPathsRelativeToEditTargetLayer(); + +//! Creates an Attributes Handler. +MAYAUSD_API_PUBLIC +Ufe::AttributesHandler::Ptr createAttributesHandler(); + +//! Creates a NodeDef Handler. +MAYAUSD_API_PUBLIC +Ufe::NodeDefHandler::Ptr createNodeDefHandler(); + +//! Creates a Connection Handler. +MAYAUSD_API_PUBLIC +Ufe::ConnectionHandler::Ptr createConnectionHandler(); + +//! Creates a SceneItemOps Handler. +MAYAUSD_API_PUBLIC +Ufe::SceneItemOpsHandler::Ptr createSceneItemOpsHandler(); + +//! Creates a UINodeGraphNode Handler. +MAYAUSD_API_PUBLIC +Ufe::UINodeGraphNodeHandler::Ptr createUINodeGraphNodeHandler(); + +} // namespace MAYAUSDAPI_NS_DEF + +#endif // MAYAUSDAPI_UTILS_H From 01f1f60f8696c1c5124391dd76cd55af83c21459 Mon Sep 17 00:00:00 2001 From: gz-adsk Date: Tue, 13 Feb 2024 14:24:09 +0000 Subject: [PATCH 2/4] EMSUSD-1016 - Code Review: * Date fixes. * Removal of handler functions as their use is temporary and only for tests * Clarification for commands that they are not executed. --- lib/mayaUsdAPI/utils.cpp | 32 +------------------------------- lib/mayaUsdAPI/utils.h | 33 ++++++--------------------------- 2 files changed, 7 insertions(+), 58 deletions(-) diff --git a/lib/mayaUsdAPI/utils.cpp b/lib/mayaUsdAPI/utils.cpp index aa6d998e6a..1edfdf3669 100644 --- a/lib/mayaUsdAPI/utils.cpp +++ b/lib/mayaUsdAPI/utils.cpp @@ -1,5 +1,5 @@ // -// Copyright 2023 Autodesk +// Copyright 2024 Autodesk // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -17,11 +17,6 @@ #include "utils.h" #include -#include -#include -#include -#include -#include #include #include #include @@ -129,29 +124,4 @@ bool requireUsdPathsRelativeToEditTargetLayer() return PXR_NS::UsdMayaUtilFileSystem::requireUsdPathsRelativeToEditTargetLayer(); } -Ufe::AttributesHandler::Ptr createAttributesHandler() -{ - return MayaUsd::ufe::UsdAttributesHandler::create(); -} - -Ufe::NodeDefHandler::Ptr createNodeDefHandler() -{ - return MayaUsd::ufe::UsdShaderNodeDefHandler::create(); -} - -Ufe::ConnectionHandler::Ptr createConnectionHandler() -{ - return MayaUsd::ufe::UsdConnectionHandler::create(); -} - -Ufe::SceneItemOpsHandler::Ptr createSceneItemOpsHandler() -{ - return MayaUsd::ufe::UsdSceneItemOpsHandler::create(); -} - -Ufe::UINodeGraphNodeHandler::Ptr createUINodeGraphNodeHandler() -{ - return MayaUsd::ufe::UsdUINodeGraphNodeHandler::create(); -} - } // End of namespace MAYAUSDAPI_NS_DEF \ No newline at end of file diff --git a/lib/mayaUsdAPI/utils.h b/lib/mayaUsdAPI/utils.h index 443096431b..12da1ae879 100644 --- a/lib/mayaUsdAPI/utils.h +++ b/lib/mayaUsdAPI/utils.h @@ -1,5 +1,5 @@ // -// Copyright 2023 Autodesk +// Copyright 2024 Autodesk // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -21,12 +21,7 @@ #include #include -#include -#include -#include #include -#include -#include namespace MAYAUSDAPI_NS_DEF { @@ -57,6 +52,7 @@ bool getUsdValue( /*! Returns a Ufe command that can create a new material based on the given shader identifier or * nullptr if the parent item is not a valid Usd item. + * The returned command is not executed; it is up to the caller to call execute(). */ MAYAUSD_API_PUBLIC Ufe::InsertChildCommand::Ptr addNewMaterialCommand( @@ -65,12 +61,15 @@ Ufe::InsertChildCommand::Ptr addNewMaterialCommand( /*! Returns a Ufe command that can create a material scope or nullptr if the parent item is not a * valid Usd item. + * The returned command is not executed; it is up to the caller to call execute(). */ MAYAUSD_API_PUBLIC Ufe::SceneItemResultUndoableCommand::Ptr createMaterialsScopeCommand(const Ufe::SceneItem::Ptr& parentItem); -//! Returns a Ufe command that can create a new Usd stage with a new layer. +/*! Returns a Ufe command that can create a new Usd stage with a new layer. + * The returned command is not executed; it is up to the caller to call execute(). + */ MAYAUSD_API_PUBLIC Ufe::SceneItemResultUndoableCommand::Ptr createStageWithNewLayerCommand(const Ufe::SceneItem::Ptr& parentItem); @@ -110,26 +109,6 @@ makePathRelativeTo(const std::string& fileName, const std::string& relativeToDir MAYAUSD_API_PUBLIC bool requireUsdPathsRelativeToEditTargetLayer(); -//! Creates an Attributes Handler. -MAYAUSD_API_PUBLIC -Ufe::AttributesHandler::Ptr createAttributesHandler(); - -//! Creates a NodeDef Handler. -MAYAUSD_API_PUBLIC -Ufe::NodeDefHandler::Ptr createNodeDefHandler(); - -//! Creates a Connection Handler. -MAYAUSD_API_PUBLIC -Ufe::ConnectionHandler::Ptr createConnectionHandler(); - -//! Creates a SceneItemOps Handler. -MAYAUSD_API_PUBLIC -Ufe::SceneItemOpsHandler::Ptr createSceneItemOpsHandler(); - -//! Creates a UINodeGraphNode Handler. -MAYAUSD_API_PUBLIC -Ufe::UINodeGraphNodeHandler::Ptr createUINodeGraphNodeHandler(); - } // namespace MAYAUSDAPI_NS_DEF #endif // MAYAUSDAPI_UTILS_H From 58b61e9f16b77fee65f20535f8ce2a780facaa6c Mon Sep 17 00:00:00 2001 From: gz-adsk Date: Tue, 13 Feb 2024 16:30:53 +0000 Subject: [PATCH 3/4] EMSUSD-1016 - Added Ufe v4 guards. --- lib/mayaUsdAPI/utils.cpp | 6 ++++++ lib/mayaUsdAPI/utils.h | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/lib/mayaUsdAPI/utils.cpp b/lib/mayaUsdAPI/utils.cpp index 1edfdf3669..2a634ce53e 100644 --- a/lib/mayaUsdAPI/utils.cpp +++ b/lib/mayaUsdAPI/utils.cpp @@ -17,7 +17,9 @@ #include "utils.h" #include +#ifdef UFE_V4_FEATURES_AVAILABLE #include +#endif #include #include #include @@ -69,6 +71,8 @@ addNewMaterialCommand(const Ufe::SceneItem::Ptr& parentItem, const std::string& return nullptr; } +#ifdef UFE_V4_FEATURES_AVAILABLE + Ufe::SceneItemResultUndoableCommand::Ptr createMaterialsScopeCommand(const Ufe::SceneItem::Ptr& parentItem) { @@ -84,6 +88,8 @@ createStageWithNewLayerCommand(const Ufe::SceneItem::Ptr& parentItem) return MayaUsd::ufe::UsdUndoCreateStageWithNewLayerCommand::create(parentItem); } +#endif + bool isMaterialsScope(const Ufe::SceneItem::Ptr& item) { return MayaUsd::ufe::isMaterialsScope(item); diff --git a/lib/mayaUsdAPI/utils.h b/lib/mayaUsdAPI/utils.h index 12da1ae879..efa0181d10 100644 --- a/lib/mayaUsdAPI/utils.h +++ b/lib/mayaUsdAPI/utils.h @@ -59,6 +59,8 @@ Ufe::InsertChildCommand::Ptr addNewMaterialCommand( const Ufe::SceneItem::Ptr& parentItem, const std::string& sdrShaderIdentifier); +#ifdef UFE_V4_FEATURES_AVAILABLE + /*! Returns a Ufe command that can create a material scope or nullptr if the parent item is not a * valid Usd item. * The returned command is not executed; it is up to the caller to call execute(). @@ -74,6 +76,8 @@ MAYAUSD_API_PUBLIC Ufe::SceneItemResultUndoableCommand::Ptr createStageWithNewLayerCommand(const Ufe::SceneItem::Ptr& parentItem); +#endif + //! Returns whether or not the given item is a materials scope. MAYAUSD_API_PUBLIC bool isMaterialsScope(const Ufe::SceneItem::Ptr& item); From de2ce9a38e220efb69cb9097b9fa5bdb78d18d53 Mon Sep 17 00:00:00 2001 From: gz-adsk Date: Tue, 13 Feb 2024 17:34:36 +0000 Subject: [PATCH 4/4] EMSUSD-1016 - Using more generic Ufe::UndoableCommand as return type. --- lib/mayaUsdAPI/utils.cpp | 14 ++++++-------- lib/mayaUsdAPI/utils.h | 12 +++++------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/lib/mayaUsdAPI/utils.cpp b/lib/mayaUsdAPI/utils.cpp index 2a634ce53e..d305e00293 100644 --- a/lib/mayaUsdAPI/utils.cpp +++ b/lib/mayaUsdAPI/utils.cpp @@ -19,8 +19,8 @@ #include #ifdef UFE_V4_FEATURES_AVAILABLE #include -#endif #include +#endif #include #include #include @@ -61,7 +61,9 @@ bool getUsdValue( return false; } -Ufe::InsertChildCommand::Ptr +#ifdef UFE_V4_FEATURES_AVAILABLE + +Ufe::UndoableCommand::Ptr addNewMaterialCommand(const Ufe::SceneItem::Ptr& parentItem, const std::string& sdrShaderIdentifier) { if (auto usdSceneItem = std::dynamic_pointer_cast(parentItem)) { @@ -71,10 +73,7 @@ addNewMaterialCommand(const Ufe::SceneItem::Ptr& parentItem, const std::string& return nullptr; } -#ifdef UFE_V4_FEATURES_AVAILABLE - -Ufe::SceneItemResultUndoableCommand::Ptr -createMaterialsScopeCommand(const Ufe::SceneItem::Ptr& parentItem) +Ufe::UndoableCommand::Ptr createMaterialsScopeCommand(const Ufe::SceneItem::Ptr& parentItem) { if (auto usdSceneItem = std::dynamic_pointer_cast(parentItem)) { return MayaUsd::ufe::UsdUndoCreateMaterialsScopeCommand::create(usdSceneItem); @@ -82,8 +81,7 @@ createMaterialsScopeCommand(const Ufe::SceneItem::Ptr& parentItem) return nullptr; } -Ufe::SceneItemResultUndoableCommand::Ptr -createStageWithNewLayerCommand(const Ufe::SceneItem::Ptr& parentItem) +Ufe::UndoableCommand::Ptr createStageWithNewLayerCommand(const Ufe::SceneItem::Ptr& parentItem) { return MayaUsd::ufe::UsdUndoCreateStageWithNewLayerCommand::create(parentItem); } diff --git a/lib/mayaUsdAPI/utils.h b/lib/mayaUsdAPI/utils.h index efa0181d10..cb9f5357a7 100644 --- a/lib/mayaUsdAPI/utils.h +++ b/lib/mayaUsdAPI/utils.h @@ -50,31 +50,29 @@ bool getUsdValue( PXR_NS::VtValue& value, PXR_NS::UsdTimeCode time); +#ifdef UFE_V4_FEATURES_AVAILABLE + /*! Returns a Ufe command that can create a new material based on the given shader identifier or * nullptr if the parent item is not a valid Usd item. * The returned command is not executed; it is up to the caller to call execute(). */ MAYAUSD_API_PUBLIC -Ufe::InsertChildCommand::Ptr addNewMaterialCommand( +Ufe::UndoableCommand::Ptr addNewMaterialCommand( const Ufe::SceneItem::Ptr& parentItem, const std::string& sdrShaderIdentifier); -#ifdef UFE_V4_FEATURES_AVAILABLE - /*! Returns a Ufe command that can create a material scope or nullptr if the parent item is not a * valid Usd item. * The returned command is not executed; it is up to the caller to call execute(). */ MAYAUSD_API_PUBLIC -Ufe::SceneItemResultUndoableCommand::Ptr -createMaterialsScopeCommand(const Ufe::SceneItem::Ptr& parentItem); +Ufe::UndoableCommand::Ptr createMaterialsScopeCommand(const Ufe::SceneItem::Ptr& parentItem); /*! Returns a Ufe command that can create a new Usd stage with a new layer. * The returned command is not executed; it is up to the caller to call execute(). */ MAYAUSD_API_PUBLIC -Ufe::SceneItemResultUndoableCommand::Ptr -createStageWithNewLayerCommand(const Ufe::SceneItem::Ptr& parentItem); +Ufe::UndoableCommand::Ptr createStageWithNewLayerCommand(const Ufe::SceneItem::Ptr& parentItem); #endif