Skip to content

Commit

Permalink
Added PrimvarReader_float3 translation for displayColor
Browse files Browse the repository at this point in the history
  • Loading branch information
jonjondev committed Jan 27, 2022
1 parent 98790a4 commit 351dd61
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/usd/translators/shading/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ target_sources(${TARGET_NAME}
usdPhongEWriter.cpp
usdReflectWriter.cpp
usdUVTextureReader.cpp
usdPrimvarReaderFloat3Reader.cpp
)

if (BUILD_RFM_TRANSLATORS)
Expand Down
2 changes: 1 addition & 1 deletion lib/usd/translators/shading/shadingTokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ TF_DEFINE_PUBLIC_TOKENS(TrUsdTokens, TR_USD_COMMON TR_USD_TEXTURE TR_USD_PRIMVAR

TF_DEFINE_PUBLIC_TOKENS(
TrMayaTokens,
TR_MAYA_MATERIALS TR_MAYA_STANDARD_SURFACE TR_MAYA_FILE TR_MAYA_UV);
TR_MAYA_MATERIALS TR_MAYA_STANDARD_SURFACE TR_MAYA_FILE TR_MAYA_UV TR_MAYA_PRIMVAR);

#ifdef WANT_MATERIALX_TRANSLATORS
TF_DEFINE_PUBLIC_TOKENS(
Expand Down
6 changes: 5 additions & 1 deletion lib/usd/translators/shading/shadingTokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ PXR_NAMESPACE_OPEN_SCOPE

#define TR_USD_PRIMVAR \
(UsdPrimvarReader_float2) \
(displayColor) \
(result)

#define TR_USD_XFORM \
Expand Down Expand Up @@ -173,12 +174,15 @@ TF_DECLARE_PUBLIC_TOKENS(TrUsdTokens, , TR_USD_COMMON TR_USD_TEXTURE TR_USD_PRIM
(outUV) \
(uvCoord)

#define TR_MAYA_PRIMVAR \
(cpvColor)

// clang-format on

TF_DECLARE_PUBLIC_TOKENS(
TrMayaTokens,
,
TR_MAYA_MATERIALS TR_MAYA_STANDARD_SURFACE TR_MAYA_FILE TR_MAYA_UV);
TR_MAYA_MATERIALS TR_MAYA_STANDARD_SURFACE TR_MAYA_FILE TR_MAYA_UV TR_MAYA_PRIMVAR);

#ifdef WANT_MATERIALX_TRANSLATORS

Expand Down
97 changes: 97 additions & 0 deletions lib/usd/translators/shading/usdPrimvarReaderFloat3Reader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//
// Copyright 2021 Animal Logic
//
// 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 "shadingTokens.h"

#include <mayaUsd/fileio/shaderReader.h>
#include <mayaUsd/fileio/shaderReaderRegistry.h>

#include <pxr/usd/usdShade/shader.h>

#include <maya/MFnDependencyNode.h>

PXR_NAMESPACE_OPEN_SCOPE

class PxrMayaUsdPrimvarReader_float3_Reader : public UsdMayaShaderReader
{
public:
PxrMayaUsdPrimvarReader_float3_Reader(const UsdMayaPrimReaderArgs& readArgs);

bool Read(UsdMayaPrimReaderContext& context) override;

TfToken GetMayaNameForUsdAttrName(const TfToken& usdAttrName) const override;
};

PXRUSDMAYA_REGISTER_SHADER_READER(UsdPrimvarReader_float3, PxrMayaUsdPrimvarReader_float3_Reader)

PxrMayaUsdPrimvarReader_float3_Reader::PxrMayaUsdPrimvarReader_float3_Reader(
const UsdMayaPrimReaderArgs& readArgs)
: UsdMayaShaderReader(readArgs)
{
}

/* virtual */
bool PxrMayaUsdPrimvarReader_float3_Reader::Read(UsdMayaPrimReaderContext& context)
{
const UsdPrim& prim = _GetArgs().GetUsdPrim();
UsdShadeShader shaderSchema = UsdShadeShader(prim);
if (!shaderSchema) {
return false;
}

MObject obj;
VtValue val;
UsdShadeInput usdInput = shaderSchema.GetInput(TrUsdTokens->varname);
if (usdInput && usdInput.Get(&val) && val.IsHolding<std::string>()
&& val.UncheckedGet<std::string>() == TrUsdTokens->displayColor.GetString()) {
// Create a CPV color node in place of USD's displayColor primvar reader
MStatus status;
MFnDependencyNode depFn;
obj = depFn.create(TrMayaTokens->cpvColor.GetText(), &status);
if (status != MS::kSuccess) {
TF_RUNTIME_ERROR(
"Could not create node of type %s for prim '%s'. ",
TrMayaTokens->cpvColor.GetText(),
prim.GetPath().GetText());
return false;
}
}

if (obj.isNull()) {
return false;
}

context.RegisterNewMayaNode(prim.GetPath().GetString(), obj);
return true;
}

/* virtual */
TfToken
PxrMayaUsdPrimvarReader_float3_Reader::GetMayaNameForUsdAttrName(const TfToken& usdAttrName) const
{
TfToken baseName;
UsdShadeAttributeType attrType;
std::tie(baseName, attrType) = UsdShadeUtils::GetBaseNameAndType(usdAttrName);

if (attrType == UsdShadeAttributeType::Output) {
if (baseName == TrUsdTokens->result) {
return TrMayaTokens->outColor;
}
}
return TfToken();
}

PXR_NAMESPACE_CLOSE_SCOPE

0 comments on commit 351dd61

Please sign in to comment.