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..d305e00293 --- /dev/null +++ b/lib/mayaUsdAPI/utils.cpp @@ -0,0 +1,131 @@ +// +// 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. +// 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 +#ifdef UFE_V4_FEATURES_AVAILABLE +#include +#include +#endif +#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; +} + +#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)) { + return MayaUsd::ufe::UsdUndoAddNewMaterialCommand::create( + usdSceneItem, sdrShaderIdentifier); + } + return nullptr; +} + +Ufe::UndoableCommand::Ptr createMaterialsScopeCommand(const Ufe::SceneItem::Ptr& parentItem) +{ + if (auto usdSceneItem = std::dynamic_pointer_cast(parentItem)) { + return MayaUsd::ufe::UsdUndoCreateMaterialsScopeCommand::create(usdSceneItem); + } + return nullptr; +} + +Ufe::UndoableCommand::Ptr 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); +} + +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(); +} + +} // 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..cb9f5357a7 --- /dev/null +++ b/lib/mayaUsdAPI/utils.h @@ -0,0 +1,116 @@ +// +// 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. +// 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 + +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); + +#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::UndoableCommand::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. + * The returned command is not executed; it is up to the caller to call execute(). + */ +MAYAUSD_API_PUBLIC +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::UndoableCommand::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); + +//! 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(); + +} // namespace MAYAUSDAPI_NS_DEF + +#endif // MAYAUSDAPI_UTILS_H