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

Ufe 0.4.8: Add classifications and metadata to def #2315

Merged
merged 6 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions lib/mayaUsd/ufe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ if(CMAKE_UFE_V4_FEATURES_AVAILABLE)
UsdShaderNodeDefHandler.cpp
)
endif()

if (${UFE_PREVIEW_VERSION_NUM} GREATER_EQUAL 4010)
target_sources(${PROJECT_NAME}
PRIVATE
UsdShaderAttributeDef.cpp
UsdUndoCreateFromNodeDefCommand.cpp
)
endif()
endif()

set(HEADERS
Expand Down Expand Up @@ -186,6 +194,13 @@ if(CMAKE_UFE_V4_FEATURES_AVAILABLE)
UsdShaderNodeDefHandler.h
)
endif()

if (${UFE_PREVIEW_VERSION_NUM} GREATER_EQUAL 4010)
list(APPEND HEADERS
UsdShaderAttributeDef.h
UsdUndoCreateFromNodeDefCommand.h
)
endif()
endif()

# -----------------------------------------------------------------------------
Expand Down
96 changes: 96 additions & 0 deletions lib/mayaUsd/ufe/UsdShaderAttributeDef.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// Copyright 2022 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 "UsdShaderAttributeDef.h"

#include "Global.h"
#include "Utils.h"

#include <pxr/base/tf/token.h>
#include <pxr/usd/ndr/declare.h>
#include <pxr/usd/sdr/shaderProperty.h>

#include <map>
#include <vector>

namespace MAYAUSD_NS_DEF {
namespace ufe {

PXR_NAMESPACE_USING_DIRECTIVE

UsdShaderAttributeDef::UsdShaderAttributeDef(
const PXR_NS::SdrShaderPropertyConstPtr& shaderAttributeDef)
: Ufe::AttributeDef()
, fShaderAttributeDef(shaderAttributeDef)
{
if (!TF_VERIFY(fShaderAttributeDef)) {
throw std::runtime_error("Invalid shader attribute definition");
}
}

UsdShaderAttributeDef::~UsdShaderAttributeDef() { }

std::string UsdShaderAttributeDef::name() const
{
TF_AXIOM(fShaderAttributeDef);
return fShaderAttributeDef->GetName().GetString();
}

std::string UsdShaderAttributeDef::type() const
{
TF_AXIOM(fShaderAttributeDef);
const PXR_NS::SdfValueTypeName typeName = fShaderAttributeDef->GetTypeAsSdfType().first;
return usdTypeToUfe(typeName);
}

std::string UsdShaderAttributeDef::defaultValue() const
{
TF_AXIOM(fShaderAttributeDef);
std::ostringstream defaultValue;
defaultValue << fShaderAttributeDef->GetDefaultValue();
return defaultValue.str();
}

Ufe::AttributeDef::IOType UsdShaderAttributeDef::ioType() const
{
TF_AXIOM(fShaderAttributeDef);
return fShaderAttributeDef->IsOutput() ? Ufe::AttributeDef::OUTPUT_ATTR
: Ufe::AttributeDef::INPUT_ATTR;
}

Ufe::Value UsdShaderAttributeDef::getMetadata(const std::string& key) const
{
TF_AXIOM(fShaderAttributeDef);
const PXR_NS::NdrTokenMap& metadata = fShaderAttributeDef->GetMetadata();
auto it = metadata.find(PXR_NS::TfToken(key));
if (it != metadata.cend()) {
return Ufe::Value(it->second);
}
// TODO: Adapt UI metadata information found in SdrShaderProperty to Ufe standards
// TODO: Fix Mtlx parser in USD to populate UI metadata in SdrShaderProperty
return {};
}

bool UsdShaderAttributeDef::hasMetadata(const std::string& key) const
{
TF_AXIOM(fShaderAttributeDef);
const PXR_NS::NdrTokenMap& metadata = fShaderAttributeDef->GetMetadata();
auto it = metadata.find(PXR_NS::TfToken(key));
return (it != metadata.cend());
}

} // namespace ufe
} // namespace MAYAUSD_NS_DEF
70 changes: 70 additions & 0 deletions lib/mayaUsd/ufe/UsdShaderAttributeDef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// Copyright 2022 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.
//
#pragma once

#include <mayaUsd/base/api.h>

#include <pxr/usd/sdr/shaderproperty.h>

#include <ufe/attributeDef.h>

namespace MAYAUSD_NS_DEF {
namespace ufe {

//! \brief UsdShaderAttributeDef interface.
class MAYAUSD_CORE_PUBLIC UsdShaderAttributeDef : public Ufe::AttributeDef
{
public:
typedef std::shared_ptr<UsdShaderAttributeDef> Ptr;

UsdShaderAttributeDef(const PXR_NS::SdrShaderPropertyConstPtr& shaderAttributeDef);
~UsdShaderAttributeDef();

// Delete the copy/move constructors assignment operators.
UsdShaderAttributeDef(const UsdShaderAttributeDef&) = delete;
UsdShaderAttributeDef& operator=(const UsdShaderAttributeDef&) = delete;
UsdShaderAttributeDef(UsdShaderAttributeDef&&) = delete;
UsdShaderAttributeDef& operator=(UsdShaderAttributeDef&&) = delete;

//! \return The attributeDef's name.
std::string name() const override;

//! \return The attributeDef's type.
std::string type() const override;

//! \return The string representation of the attributeDef's value.
std::string defaultValue() const override;

//! \return The attribute input/output type.
IOType ioType() const override;

/*!
Get the value of the metadata named key.
\param[in] key The metadata key to query.
\return The value of the metadata key. If the key does not exist an empty Value is returned.
*/
Ufe::Value getMetadata(const std::string& key) const override;

//! Returns true if metadata key has a non-empty value.
bool hasMetadata(const std::string& key) const override;

private:
const PXR_NS::SdrShaderPropertyConstPtr fShaderAttributeDef;

}; // UsdShaderAttributeDef

} // namespace ufe
} // namespace MAYAUSD_NS_DEF
Loading