Skip to content

Commit

Permalink
Merge pull request #3608 from Autodesk/gz-adsk/EMSUSD-1016/mayausdapi…
Browse files Browse the repository at this point in the history
…-for-lookdevx

EMSUSD-1066 - Minimal API needed by LookdevX to decouple from mayaUsd
  • Loading branch information
seando-adsk authored Feb 20, 2024
2 parents 490ab8b + de2ce9a commit ccfefed
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/mayaUsdAPI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

#------------------------------------------------------------------------------
Expand Down
131 changes: 131 additions & 0 deletions lib/mayaUsdAPI/utils.cpp
Original file line number Diff line number Diff line change
@@ -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 <mayaUsd/ufe/Global.h>
#ifdef UFE_V4_FEATURES_AVAILABLE
#include <mayaUsd/ufe/UsdUndoCreateStageWithNewLayerCommand.h>
#include <mayaUsd/ufe/UsdUndoMaterialCommands.h>
#endif
#include <mayaUsd/ufe/Utils.h>
#include <mayaUsd/utils/utilFileSystem.h>
#include <mayaUsdUtils/MergePrims.h>

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<MayaUsd::ufe::UsdAttribute>(attribute)) {
return usdAttribute->usdPrim().GetStage();
}
return nullptr;
}

PXR_NS::SdfValueTypeName usdAttributeType(const Ufe::Attribute::Ptr& attribute)
{
if (auto usdAttribute = std::dynamic_pointer_cast<MayaUsd::ufe::UsdAttribute>(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<MayaUsd::ufe::UsdAttribute>(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<UsdUfe::UsdSceneItem>(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<UsdUfe::UsdSceneItem>(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<std::string, bool>
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
116 changes: 116 additions & 0 deletions lib/mayaUsdAPI/utils.h
Original file line number Diff line number Diff line change
@@ -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 <mayaUsdAPI/api.h>

#include <pxr/usd/usd/attribute.h>

#include <ufe/attribute.h>
#include <ufe/rtid.h>

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<std::string, bool>
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

0 comments on commit ccfefed

Please sign in to comment.