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

Dev issue950 #956

Merged
merged 2 commits into from
Sep 20, 2019
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
2 changes: 2 additions & 0 deletions pxr/usd/plugin/usdMtlx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pxr_plugin(usdMtlx
${MATERIALX_INCLUDE_DIRS}

CPPFILES
debugCodes.cpp
discovery.cpp
parser.cpp

Expand All @@ -35,6 +36,7 @@ pxr_plugin(usdMtlx

PRIVATE_HEADERS
api.h
debugCodes.h

PYTHON_CPPFILES
moduleDeps.cpp
Expand Down
39 changes: 39 additions & 0 deletions pxr/usd/plugin/usdMtlx/debugCodes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Copyright 2019 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#include "pxr/pxr.h"
#include "pxr/usd/usdMtlx/debugCodes.h"

#include "pxr/base/tf/debug.h"
#include "pxr/base/tf/registryManager.h"

PXR_NAMESPACE_OPEN_SCOPE

TF_REGISTRY_FUNCTION(TfDebug)
{
TF_DEBUG_ENVIRONMENT_SYMBOL(USDMTLX_READER,
"UsdMtlx reader details");
}

PXR_NAMESPACE_CLOSE_SCOPE

40 changes: 40 additions & 0 deletions pxr/usd/plugin/usdMtlx/debugCodes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Copyright 2019 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef USDMTLX_DEBUGCODES_H
#define USDMTLX_DEBUGCODES_H

#include "pxr/pxr.h"
#include "pxr/base/tf/debug.h"

PXR_NAMESPACE_OPEN_SCOPE


TF_DEBUG_CODES(
USDMTLX_READER
);


PXR_NAMESPACE_CLOSE_SCOPE

#endif // USDMTLX_DEBUGCODES_H
43 changes: 38 additions & 5 deletions pxr/usd/plugin/usdMtlx/reader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2018 Pixar
// Copyright 2018-2019 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
Expand All @@ -22,6 +22,7 @@
// language governing permissions and limitations under the Apache License.
//
#include "pxr/pxr.h"
#include "pxr/usd/usdMtlx/debugCodes.h"
#include "pxr/usd/usdMtlx/reader.h"
#include "pxr/usd/usdMtlx/utils.h"

Expand Down Expand Up @@ -1066,9 +1067,14 @@ _NodeGraph::AddReference(const SdfPath& referencingPath) const
}

// Something other than a node graph already exists.
TF_WARN("Can't create node graph at <%s>; a '%s' already exists",
referencingPath.GetText(), prim.GetTypeName().GetText());
return _NodeGraph();
// If it has a type, ignore it.
// Otherwise still add the reference for the implicit
// node graph case
if (prim.GetTypeName() != "") {
TF_WARN("Can't create node graph at <%s>; a '%s' already exists",
referencingPath.GetText(), prim.GetTypeName().GetText());
return _NodeGraph();
}
}

// Create a new prim referencing the node graph.
Expand Down Expand Up @@ -1247,10 +1253,15 @@ _Context::_AddNodeGraph(
// getting nodes and outputs at the document scope and we
// don't make a USD nodegraph.
if (mtlxNodeGraph) {
TF_DEBUG(USDMTLX_READER).Msg("Add node graph: %s at path %s\n",
mtlxNodeGraph->getName().c_str(),
_nodeGraphsPath.GetString().c_str());
builder.SetContainer(mtlxNodeGraph);
builder.SetTarget(_stage, _nodeGraphsPath, mtlxNodeGraph);
}
else {
TF_DEBUG(USDMTLX_READER).Msg("Add implicit node graph at path %s\n",
_nodeGraphsPath.GetString().c_str());
builder.SetContainer(mtlxDocument);
builder.SetTarget(_stage, _nodeGraphsPath);
}
Expand All @@ -1266,6 +1277,8 @@ _Context::AddNodeGraphWithDef(const mx::ConstNodeGraphPtr& mtlxNodeGraph)
auto& nodeGraph = _nodeGraphs[mtlxNodeGraph];
if (!nodeGraph && mtlxNodeGraph) {
if (auto mtlxNodeDef = mtlxNodeGraph->getNodeDef()) {
TF_DEBUG(USDMTLX_READER).Msg("Add mtlxNodeDef %s\n",
mtlxNodeDef->getName().c_str());
_NodeGraphBuilder builder;
builder.SetInterface(mtlxNodeDef);
builder.SetContainer(mtlxNodeGraph);
Expand Down Expand Up @@ -1365,7 +1378,10 @@ _Context::AddShaderRef(const mx::ConstShaderRefPtr& mtlxShaderRef)
if (auto usdShaderImpl = UsdShadeShader::Get(_stage, shaderImplPath)) {
// Do nothing
}
else if ((usdShaderImpl = UsdShadeShader::Define(_stage, shaderImplPath))) {
else if (usdShaderImpl = UsdShadeShader::Define(_stage, shaderImplPath)) {
TF_DEBUG(USDMTLX_READER).Msg("Created shader mtlx %s, as usd %s\n",
mtlxNodeDef->getName().c_str(),
name.GetString().c_str());
usdShaderImpl.CreateIdAttr(VtValue(TfToken(shaderId)));
auto connectable = usdShaderImpl.ConnectableAPI();
_SetCoreUIAttributes(usdShaderImpl.GetPrim(), mtlxShaderRef);
Expand Down Expand Up @@ -1557,6 +1573,7 @@ _Context::AddMaterialVariant(
usdVariantSet.ClearVariantSelection();
}


UsdCollectionAPI
_Context::AddCollection(const mx::ConstCollectionPtr& mtlxCollection)
{
Expand Down Expand Up @@ -1772,6 +1789,9 @@ _Context::_BindNodeGraph(
auto referencingPath =
referencingPathParent.AppendChild(
usdNodeGraph.GetOwnerPrim().GetPath().GetNameToken());
TF_DEBUG(USDMTLX_READER).Msg("_BindNodeGraph %s %s\n",
mtlxBindInput->getName().c_str(),
referencingPath.GetString().c_str());
auto refNodeGraph = usdNodeGraph.AddReference(referencingPath);
if (!refNodeGraph) {
return;
Expand Down Expand Up @@ -1830,6 +1850,9 @@ _Context::_AddShaderOutput(
}
}
}
TF_DEBUG(USDMTLX_READER).Msg("Add shader output %s of type %s\n",
mtlxTyped->getName().c_str(),
type.c_str());
if (context == "surface" || type == mx::SURFACE_SHADER_TYPE_STRING) {
return connectable.CreateOutput(UsdShadeTokens->surface,
SdfValueTypeNames->Token);
Expand Down Expand Up @@ -2270,6 +2293,8 @@ ReadNodeGraphsWithDefs(mx::ConstDocumentPtr mtlx, _Context& context)
{
// Translate nodegraphs with nodedefs.
for (auto& mtlxNodeGraph: mtlx->getNodeGraphs()) {
TF_DEBUG(USDMTLX_READER).Msg("Read node graph %s\n",
mtlxNodeGraph->getName().c_str() );
context.AddNodeGraphWithDef(mtlxNodeGraph);
}
}
Expand Down Expand Up @@ -2307,10 +2332,14 @@ ReadMaterials(mx::ConstDocumentPtr mtlx, _Context& context)
{
for (auto& mtlxMaterial: mtlx->getMaterials()) {
// Translate material.
TF_DEBUG(USDMTLX_READER).Msg("Adding mtlxMaterial '%s'\n",
_Name(mtlxMaterial).c_str());
if (auto usdMaterial = context.BeginMaterial(mtlxMaterial)) {
// Translate all shader references.
for (auto mtlxShaderRef: mtlxMaterial->getShaderRefs()) {
// Translate shader reference.
TF_DEBUG(USDMTLX_READER).Msg("Adding shaderref '%s'\n",
_Name(mtlxShaderRef).c_str());
if (auto usdShader = context.AddShaderRef(mtlxShaderRef)) {
// Do nothing.
}
Expand Down Expand Up @@ -2348,6 +2377,10 @@ ReadMaterials(mx::ConstDocumentPtr mtlx, _Context& context)
if (auto usdInherited = context.GetMaterial(name)) {
usdMaterial.GetPrim().GetSpecializes()
.AddSpecialize(usdInherited.GetPath());
TF_DEBUG(USDMTLX_READER).Msg("Material '%s' inherit from "
" '%s'\n",
_Name(mtlxMaterial).c_str(),
name.c_str());
}
else {
TF_WARN("Material '%s' attempted to inherit from "
Expand Down
29 changes: 28 additions & 1 deletion pxr/usd/plugin/usdMtlx/testenv/testUsdMtlxFileFormat.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# KIND, either express or implied. See the Apache License for the specific
# language governing permissions and limitations under the Apache License.

from pxr import Tf, Usd, UsdMtlx
from pxr import Tf, Sdf, Usd, UsdMtlx, UsdShade
import unittest

def _EmptyLayer():
Expand Down Expand Up @@ -117,6 +117,33 @@ def test_NodeGraphs(self):
with open('NodeGraphs.usda', 'w') as f:
print >>f, stage.GetRootLayer().ExportToString()

def test_MultiBindInputs(self):
"""
Test MaterialX conversion with mutliple bind inputs.
"""

stage = UsdMtlx._TestFile('MultiBindInputs.mtlx')

# Get the node graph and make sure there are exactly 3 inputs
nodeGraph = UsdShade.NodeGraph.Get(stage,
Sdf.Path('/MaterialX/Materials/layered/ND_layerShader'))
inputs = nodeGraph.GetInputs()
self.assertEqual(len(inputs), 3)

# Make sure each input is connected as expected
inputToSource = {
'weight_1':
'/MaterialX/Materials/layered/NodeGraphs/layered_layer1_gradient',
'weight_2':
'/MaterialX/Materials/layered/NodeGraphs/layered_layer2_gradient',
'weight_3':
'/MaterialX/Materials/layered/NodeGraphs/layered_layer3_gradient'
}
for inputName, source in inputToSource.iteritems():
input = nodeGraph.GetInput(inputName)
self.assertEqual(input.HasConnectedSource(), True)
self.assertEqual(input.GetConnectedSource()[0].GetPath(), source)

def test_Looks(self):
"""
Test general MaterialX look conversions.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<materialx version="1.36">
<nodedef name="ND_layerShader" type="surfaceshader" node="layerShader">
<input name="weight_1" type="color3"/>
<input name="weight_2" type="color3"/>
<input name="weight_3" type="color3"/>
</nodedef>
<Gradient name="layered_layer1_gradient" type="color3">
</Gradient>
<Gradient name="layered_layer2_gradient" type="color3">
</Gradient>
<Gradient name="layered_layer3_gradient" type="color3">
</Gradient>
<material name="layered">
<shaderref name="layered_sr" node="layerShader">
<bindinput name="weight_1" type="color3" output="o_layered_layer1_gradient" />
<bindinput name="weight_2" type="color3" output="o_layered_layer2_gradient" />
<bindinput name="weight_3" type="color3" output="o_layered_layer3_gradient" />
</shaderref>
</material>
<output name="o_layered_layer1_gradient" type="color3" nodename="layered_layer1_gradient" />
<output name="o_layered_layer2_gradient" type="color3" nodename="layered_layer2_gradient" />
<output name="o_layered_layer3_gradient" type="color3" nodename="layered_layer3_gradient" />
</materialx>