From 12afbe90439f87bc6aceff488b2addbb6ffe4840 Mon Sep 17 00:00:00 2001 From: Jerry Gamache Date: Tue, 6 Jul 2021 12:53:56 -0400 Subject: [PATCH 1/5] MAYA-111902 Fix errors found by testing And add rountrip unit test. --- .../shading/mtlxPreviewSurfaceReader.cpp | 2 +- .../shading/mtlxPreviewSurfaceWriter.cpp | 8 +++--- ...tUsdExportImportRoundtripPreviewSurface.py | 27 +++++++++++++++---- 3 files changed, 28 insertions(+), 9 deletions(-) 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..35d9950609 100644 --- a/test/lib/usd/translators/testUsdExportImportRoundtripPreviewSurface.py +++ b/test/lib/usd/translators/testUsdExportImportRoundtripPreviewSurface.py @@ -28,6 +28,7 @@ from maya import standalone import fixturesUtils +import mayaUtils class testUsdExportImportRoundtripPreviewSurface(unittest.TestCase): @@ -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. """ @@ -105,10 +112,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 +130,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 +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) From 380593b6f3babe57b62c5cedfffa34018c3ddb8f Mon Sep 17 00:00:00 2001 From: Jerry Gamache Date: Tue, 6 Jul 2021 14:12:52 -0400 Subject: [PATCH 2/5] Fix roundtrip test when there is no UFE. --- .../testUsdExportImportRoundtripPreviewSurface.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/lib/usd/translators/testUsdExportImportRoundtripPreviewSurface.py b/test/lib/usd/translators/testUsdExportImportRoundtripPreviewSurface.py index 35d9950609..d58a2de364 100644 --- a/test/lib/usd/translators/testUsdExportImportRoundtripPreviewSurface.py +++ b/test/lib/usd/translators/testUsdExportImportRoundtripPreviewSurface.py @@ -28,7 +28,10 @@ from maya import standalone import fixturesUtils -import mayaUtils +try: + import mayaUtils +except ImportError: + pass class testUsdExportImportRoundtripPreviewSurface(unittest.TestCase): @@ -52,7 +55,7 @@ def testUsdPreviewSurfaceRoundtripSpecular(self): def testUsdPreviewSurfaceRoundtripMetallic(self): self.__testUsdPreviewSurfaceRoundtrip(metallic=True) - @unittest.skipUnless(mayaUtils.previewReleaseVersion() >= 126 and Usd.GetVersion() > (0, 21, 5), 'Requires MaterialX support.') + @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") From 24ee60f1116e3480d80ec7d0772b2bb62f08c790 Mon Sep 17 00:00:00 2001 From: Jerry Gamache Date: Tue, 6 Jul 2021 17:50:14 -0400 Subject: [PATCH 3/5] Fix build issues with default material API. --- cmake/modules/FindMaya.cmake | 26 ++++++++++++++++++++++++++ lib/mayaUsd/CMakeLists.txt | 9 +-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cmake/modules/FindMaya.cmake b/cmake/modules/FindMaya.cmake index e1ec449330..129363b32f 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_DEFAULT_MATERIAL_API Presence of a default material API on MRenderItem. # #============================================================================= @@ -331,6 +332,30 @@ if (MAYA_OGSDEVICES_LIBRARY) endif() message(STATUS "Using Maya Light API Version ${MAYA_LIGHTAPI_VERSION}") +set(MAYA_DEFAULT_MATERIAL_API 0) +# Grep for the new API in OpenMayaRender. Might be quicker than asking MayaPy. +find_file(MAYA_OPENMAYARENDER_LIBRARY + "${MAYA_DSO_PREFIX}OpenMayaRender${MAYA_DSO_SUFFIX}" + HINTS + "${MAYA_LIBRARY_DIR}" + "${MAYA_LOCATION}" + PATH_SUFFIXES + lib/ + bin/ + DOC + "Maya's ${MAYA_LIB} library path" + # NO_CMAKE_SYSTEM_PATH needed to avoid conflicts between + # Maya's Foundation library and OSX's framework. + NO_CMAKE_SYSTEM_PATH +) +if (MAYA_OPENMAYARENDER_LIBRARY) + file(STRINGS ${MAYA_OPENMAYARENDER_LIBRARY} HAS_DEFAULT_MATERIAL_API REGEX "setDefaultMaterialHandling") + if (HAS_DEFAULT_MATERIAL_API) + set(MAYA_DEFAULT_MATERIAL_API 1) + message(STATUS "This version of Maya has the default material API") + endif() +endif() + # handle the QUIETLY and REQUIRED arguments and set MAYA_FOUND to TRUE if # all listed variables are TRUE include(FindPackageHandleStandardArgs) @@ -345,6 +370,7 @@ find_package_handle_standard_args(Maya MAYA_API_VERSION MAYA_APP_VERSION MAYA_LIGHTAPI_VERSION + MAYA_DEFAULT_MATERIAL_API VERSION_VAR MAYA_APP_VERSION ) diff --git a/lib/mayaUsd/CMakeLists.txt b/lib/mayaUsd/CMakeLists.txt index 25c0ea5686..50b76c0827 100644 --- a/lib/mayaUsd/CMakeLists.txt +++ b/lib/mayaUsd/CMakeLists.txt @@ -62,14 +62,7 @@ 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) +if (MAYA_DEFAULT_MATERIAL_API EQUAL 1) target_compile_definitions(${PROJECT_NAME} PRIVATE HAS_DEFAULT_MATERIAL_SUPPORT_API=1 From 42e1d9bc0b8602c633ea18fc4ea27ad19794afc2 Mon Sep 17 00:00:00 2001 From: Jerry Gamache Date: Wed, 7 Jul 2021 09:54:39 -0400 Subject: [PATCH 4/5] Fix detection of default material API. --- cmake/modules/FindMaya.cmake | 27 +++++++++++---------------- lib/mayaUsd/CMakeLists.txt | 3 ++- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/cmake/modules/FindMaya.cmake b/cmake/modules/FindMaya.cmake index 129363b32f..353ec91593 100644 --- a/cmake/modules/FindMaya.cmake +++ b/cmake/modules/FindMaya.cmake @@ -12,7 +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_DEFAULT_MATERIAL_API Presence of a default material API on MRenderItem. +# MAYA_HAS_DEFAULT_MATERIAL_API Presence of a default material API on MRenderItem. # #============================================================================= @@ -332,27 +332,23 @@ if (MAYA_OGSDEVICES_LIBRARY) endif() message(STATUS "Using Maya Light API Version ${MAYA_LIGHTAPI_VERSION}") -set(MAYA_DEFAULT_MATERIAL_API 0) -# Grep for the new API in OpenMayaRender. Might be quicker than asking MayaPy. -find_file(MAYA_OPENMAYARENDER_LIBRARY - "${MAYA_DSO_PREFIX}OpenMayaRender${MAYA_DSO_SUFFIX}" +set(MAYA_HAS_DEFAULT_MATERIAL_API FALSE CACHE INTERNAL "setDefaultMaterialHandling") +# Grep for the new API in include/maya/MHWGeometry.h +find_file(MAYA_MHWGeometry_HEADER + "MHWGeometry.h" HINTS - "${MAYA_LIBRARY_DIR}" - "${MAYA_LOCATION}" + "${MAYA_INCLUDE_DIRS}" PATH_SUFFIXES - lib/ - bin/ - DOC - "Maya's ${MAYA_LIB} library path" + maya # NO_CMAKE_SYSTEM_PATH needed to avoid conflicts between # Maya's Foundation library and OSX's framework. NO_CMAKE_SYSTEM_PATH ) -if (MAYA_OPENMAYARENDER_LIBRARY) - file(STRINGS ${MAYA_OPENMAYARENDER_LIBRARY} HAS_DEFAULT_MATERIAL_API REGEX "setDefaultMaterialHandling") +if (MAYA_MHWGeometry_HEADER) + file(STRINGS ${MAYA_MHWGeometry_HEADER} HAS_DEFAULT_MATERIAL_API REGEX "setDefaultMaterialHandling") if (HAS_DEFAULT_MATERIAL_API) - set(MAYA_DEFAULT_MATERIAL_API 1) - message(STATUS "This version of Maya has the default material API") + set(MAYA_HAS_DEFAULT_MATERIAL_API TRUE CACHE INTERNAL "setDefaultMaterialHandling") + message(STATUS "Maya has setDefaultMaterialHandling API") endif() endif() @@ -370,7 +366,6 @@ find_package_handle_standard_args(Maya MAYA_API_VERSION MAYA_APP_VERSION MAYA_LIGHTAPI_VERSION - MAYA_DEFAULT_MATERIAL_API VERSION_VAR MAYA_APP_VERSION ) diff --git a/lib/mayaUsd/CMakeLists.txt b/lib/mayaUsd/CMakeLists.txt index 50b76c0827..9ae0d2d062 100644 --- a/lib/mayaUsd/CMakeLists.txt +++ b/lib/mayaUsd/CMakeLists.txt @@ -62,7 +62,8 @@ if(DEFINED UFE_PREVIEW_VERSION_NUM) ) endif() -if (MAYA_DEFAULT_MATERIAL_API EQUAL 1) +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 From 0e53b7f5c5c8115d13a783d2ecd1f0c0b48bcf8e Mon Sep 17 00:00:00 2001 From: Jerry Gamache Date: Wed, 7 Jul 2021 10:53:10 -0400 Subject: [PATCH 5/5] Simplified check since we know exactly which file to probe. --- cmake/modules/FindMaya.cmake | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/cmake/modules/FindMaya.cmake b/cmake/modules/FindMaya.cmake index 353ec91593..f65bacab25 100644 --- a/cmake/modules/FindMaya.cmake +++ b/cmake/modules/FindMaya.cmake @@ -333,20 +333,9 @@ endif() message(STATUS "Using Maya Light API Version ${MAYA_LIGHTAPI_VERSION}") set(MAYA_HAS_DEFAULT_MATERIAL_API FALSE CACHE INTERNAL "setDefaultMaterialHandling") -# Grep for the new API in include/maya/MHWGeometry.h -find_file(MAYA_MHWGeometry_HEADER - "MHWGeometry.h" - HINTS - "${MAYA_INCLUDE_DIRS}" - PATH_SUFFIXES - maya - # NO_CMAKE_SYSTEM_PATH needed to avoid conflicts between - # Maya's Foundation library and OSX's framework. - NO_CMAKE_SYSTEM_PATH -) -if (MAYA_MHWGeometry_HEADER) - file(STRINGS ${MAYA_MHWGeometry_HEADER} HAS_DEFAULT_MATERIAL_API REGEX "setDefaultMaterialHandling") - if (HAS_DEFAULT_MATERIAL_API) +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()