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

MAYA-111902 Fix MaterialX import errors found by testing #1553

Merged
merged 5 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 1 deletion lib/usd/translators/shading/mtlxPreviewSurfaceReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ TfToken MtlxUsd_PreviewSurfaceReader::GetMayaNameForUsdAttrName(const TfToken& u

if (attrType == UsdShadeAttributeType::Input) {
return baseName;
} else if (attrType == UsdShadeAttributeType::Output && baseName == TrMtlxTokens->out) {
} else if (attrType == UsdShadeAttributeType::Output && baseName == UsdShadeTokens->surface) {
return TrMayaTokens->outColor;
}
return TfToken();
Expand Down
8 changes: 5 additions & 3 deletions lib/usd/translators/shading/mtlxPreviewSurfaceWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ MtlxUsd_PreviewSurfaceWriter::MtlxUsd_PreviewSurfaceWriter(
MPlug attrPlug = depNodeFn.findPlug(mayaAttrName.GetText(), true);

// Keep our authoring sparse by ignoring attributes with no values set
// and no connections. We know that the default value of base and base
// color diverged between Maya and MaterialX in version 1.38.
if (!UsdMayaUtil::IsAuthored(attrPlug) && !attrPlug.isConnected()) {
// and no connections. We know that the default value of roughness is
// wrong in MaterialX 1.38.
if (!(UsdMayaUtil::IsAuthored(attrPlug)
|| mayaAttrName == PxrMayaUsdPreviewSurfaceTokens->RoughnessAttrName)
&& !attrPlug.isConnected()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from maya import standalone

import fixturesUtils
import mayaUtils

class testUsdExportImportRoundtripPreviewSurface(unittest.TestCase):

Expand All @@ -51,7 +52,13 @@ def testUsdPreviewSurfaceRoundtripSpecular(self):
def testUsdPreviewSurfaceRoundtripMetallic(self):
self.__testUsdPreviewSurfaceRoundtrip(metallic=True)

def __testUsdPreviewSurfaceRoundtrip(self, metallic=True):
@unittest.skipUnless(mayaUtils.previewReleaseVersion() >= 126 and Usd.GetVersion() > (0, 21, 5), 'Requires MaterialX support.')
def testUsdPreviewSurfaceRoundtripMaterialX(self):
self.__testUsdPreviewSurfaceRoundtrip(metallic=True, convertTo="MaterialX")

def __testUsdPreviewSurfaceRoundtrip(self,
metallic=True,
convertTo="UsdPreviewSurface"):
"""
Tests that a usdPreviewSurface exports and imports correctly.
"""
Expand Down Expand Up @@ -105,18 +112,25 @@ def __testUsdPreviewSurfaceRoundtrip(self, metallic=True):
original_path = cmds.getAttr(file_node+".fileTextureName")

# Export to USD:
usd_path = os.path.abspath('UsdPreviewSurfaceRoundtripTest{}.usda'.format('Metallic' if metallic else 'Specular'))
file_suffix = "_{}_{}".format(convertTo, 'Metallic' if metallic else 'Specular')
usd_path = os.path.abspath('UsdPreviewSurfaceRoundtripTest{}.usda'.format(file_suffix))

export_options = [
"shadingMode=useRegistry",
"convertMaterialsTo={}".format(convertTo),
"mergeTransformAndShape=1"
]

cmds.file(usd_path, force=True,
options="shadingMode=useRegistry;mergeTransformAndShape=1",
options=";".join(export_options),
typ="USD Export", pr=True, ea=True)

cmds.file(defaultExtensions=default_ext_setting)

cmds.file(newFile=True, force=True)

# Import back:
import_options = ("shadingMode=[[useRegistry,UsdPreviewSurface]]",
import_options = ("shadingMode=[[useRegistry,{}]]".format(convertTo),
"preferredMaterial=none",
"primPath=/")
cmds.file(usd_path, i=True, type="USD Import",
Expand Down Expand Up @@ -170,9 +184,12 @@ def __testUsdPreviewSurfaceRoundtrip(self, metallic=True):
# Make sure paths are relative in the USD file. Joining the directory
# that the USD file lives in with the texture path should point us at
# a file that exists.
file_template = "/{}/Looks/{}/{}"
if convertTo == "MaterialX":
file_template = "/{0}/Looks/{1}/MayaNG_{1}/{2}"
stage = Usd.Stage.Open(usd_path)
texture_prim = stage.GetPrimAtPath(
"/%s/Looks/%s/%s" % (sphere_xform, material_sg, file_node))
file_template.format(sphere_xform, material_sg, file_node))
rel_texture_path = texture_prim.GetAttribute('inputs:file').Get().path

usd_dir = os.path.dirname(usd_path)
Expand Down