diff --git a/cmake/modules/FindMaya.cmake b/cmake/modules/FindMaya.cmake index e1ec449330..f65bacab25 100644 --- a/cmake/modules/FindMaya.cmake +++ b/cmake/modules/FindMaya.cmake @@ -12,6 +12,7 @@ # MAYA_API_VERSION Maya version (6-8 digits) # MAYA_APP_VERSION Maya app version (4 digits) # MAYA_LIGHTAPI_VERSION Maya light API version (1 or 2) +# MAYA_HAS_DEFAULT_MATERIAL_API Presence of a default material API on MRenderItem. # #============================================================================= @@ -331,6 +332,15 @@ if (MAYA_OGSDEVICES_LIBRARY) endif() message(STATUS "Using Maya Light API Version ${MAYA_LIGHTAPI_VERSION}") +set(MAYA_HAS_DEFAULT_MATERIAL_API FALSE CACHE INTERNAL "setDefaultMaterialHandling") +if(MAYA_INCLUDE_DIRS AND EXISTS "${MAYA_INCLUDE_DIR}/maya/MHWGeometry.h") + file(STRINGS ${MAYA_INCLUDE_DIR}/maya/MHWGeometry.h MAYA_HAS_API REGEX "setDefaultMaterialHandling") + if(MAYA_HAS_API) + set(MAYA_HAS_DEFAULT_MATERIAL_API TRUE CACHE INTERNAL "setDefaultMaterialHandling") + message(STATUS "Maya has setDefaultMaterialHandling API") + endif() +endif() + # handle the QUIETLY and REQUIRED arguments and set MAYA_FOUND to TRUE if # all listed variables are TRUE include(FindPackageHandleStandardArgs) diff --git a/lib/mayaUsd/CMakeLists.txt b/lib/mayaUsd/CMakeLists.txt index 25c0ea5686..9ae0d2d062 100644 --- a/lib/mayaUsd/CMakeLists.txt +++ b/lib/mayaUsd/CMakeLists.txt @@ -62,14 +62,8 @@ if(DEFINED UFE_PREVIEW_VERSION_NUM) ) endif() -if (MAYA_API_VERSION VERSION_GREATER_EQUAL 20220100 AND MAYA_API_VERSION VERSION_LESS 20230000) - target_compile_definitions(${PROJECT_NAME} - PRIVATE - HAS_DEFAULT_MATERIAL_SUPPORT_API=1 - ) -endif() - -if (MAYA_API_VERSION VERSION_GREATER_EQUAL 20230000 AND UFE_VERSION VERSION_GREATER_EQUAL 2.0.3) +message(STATUS "MAYA_HAS_DEFAULT_MATERIAL_API is ${MAYA_HAS_DEFAULT_MATERIAL_API}") +if (MAYA_HAS_DEFAULT_MATERIAL_API) target_compile_definitions(${PROJECT_NAME} PRIVATE HAS_DEFAULT_MATERIAL_SUPPORT_API=1 diff --git a/lib/usd/translators/shading/mtlxPreviewSurfaceReader.cpp b/lib/usd/translators/shading/mtlxPreviewSurfaceReader.cpp index baaf05ef66..d0ce23d7f4 100644 --- a/lib/usd/translators/shading/mtlxPreviewSurfaceReader.cpp +++ b/lib/usd/translators/shading/mtlxPreviewSurfaceReader.cpp @@ -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(); diff --git a/lib/usd/translators/shading/mtlxPreviewSurfaceWriter.cpp b/lib/usd/translators/shading/mtlxPreviewSurfaceWriter.cpp index b57858940a..b2acdfc0aa 100644 --- a/lib/usd/translators/shading/mtlxPreviewSurfaceWriter.cpp +++ b/lib/usd/translators/shading/mtlxPreviewSurfaceWriter.cpp @@ -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; } diff --git a/test/lib/usd/translators/testUsdExportImportRoundtripPreviewSurface.py b/test/lib/usd/translators/testUsdExportImportRoundtripPreviewSurface.py index 56f2348425..d58a2de364 100644 --- a/test/lib/usd/translators/testUsdExportImportRoundtripPreviewSurface.py +++ b/test/lib/usd/translators/testUsdExportImportRoundtripPreviewSurface.py @@ -28,6 +28,10 @@ from maya import standalone import fixturesUtils +try: + import mayaUtils +except ImportError: + pass class testUsdExportImportRoundtripPreviewSurface(unittest.TestCase): @@ -51,7 +55,13 @@ def testUsdPreviewSurfaceRoundtripSpecular(self): def testUsdPreviewSurfaceRoundtripMetallic(self): self.__testUsdPreviewSurfaceRoundtrip(metallic=True) - def __testUsdPreviewSurfaceRoundtrip(self, metallic=True): + @unittest.skipUnless("mayaUtils" in globals() and mayaUtils.previewReleaseVersion() >= 126 and Usd.GetVersion() > (0, 21, 2), '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. """ @@ -105,10 +115,17 @@ 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) @@ -116,7 +133,7 @@ def __testUsdPreviewSurfaceRoundtrip(self, metallic=True): 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", @@ -170,9 +187,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)