From 79c318f6927524fb5adf3678ee98ed9ff4ae09f5 Mon Sep 17 00:00:00 2001 From: Michael B Johnson Date: Wed, 30 Apr 2025 19:54:17 +0200 Subject: [PATCH] Fix #137973: Add MaterialX version info on USD export This change adds MaterialX version information to the exported MaterialX USD materials. Details: In USD 25.02, the MaterialXConfigAPI schema was introduced to allow recording the MaterialX version used to author a material. When loading MaterialX documents into USD, this schema is automatically applied and the associated version attribute is created on the material. Due to how the MaterialX export works (via copying the composed MaterialX spec from a temporary stage), the MaterialXConfigAPI needs to be explicitly applied to the final material being exported. This change applies this MaterialXConfigAPI schema and copies the version attribute from the temporary MaterialX stage to the final stage. Authored by Apple: Dan Knowlton Co-authored-by: Dan Knowlton Pull Request: https://projects.blender.org/blender/blender/pulls/137974 --- source/blender/io/usd/intern/usd_writer_material.cc | 13 +++++++++++++ tests/python/bl_usd_export_test.py | 7 ++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/source/blender/io/usd/intern/usd_writer_material.cc b/source/blender/io/usd/intern/usd_writer_material.cc index 49bb42129e9..d045f4d1606 100644 --- a/source/blender/io/usd/intern/usd_writer_material.cc +++ b/source/blender/io/usd/intern/usd_writer_material.cc @@ -42,6 +42,7 @@ #ifdef WITH_MATERIALX # include "shader/materialx/material.h" # include +# include # include #endif @@ -1467,6 +1468,18 @@ static void create_usd_materialx_material(const USDExporterContext &usd_export_c return; } + /* Copy over the MateralXConfigAPI schema and associated attribute. */ + pxr::UsdMtlxMaterialXConfigAPI temp_config_api{temp_material_prim}; + if (temp_config_api) { + pxr::UsdMtlxMaterialXConfigAPI materialx_config_api = pxr::UsdMtlxMaterialXConfigAPI::Apply( + material_prim); + pxr::UsdAttribute temp_mtlx_version_attr = temp_config_api.GetConfigMtlxVersionAttr(); + pxr::VtValue mtlx_version; + if (temp_mtlx_version_attr && temp_mtlx_version_attr.Get(&mtlx_version)) { + materialx_config_api.CreateConfigMtlxVersionAttr(mtlx_version); + } + } + /* Once we have the material, we need to prepare for renaming any conflicts. * However, we must make sure any new names don't conflict with names in the temp stage either */ Set temp_used_names; diff --git a/tests/python/bl_usd_export_test.py b/tests/python/bl_usd_export_test.py index a246475d485..0a66aad072e 100644 --- a/tests/python/bl_usd_export_test.py +++ b/tests/python/bl_usd_export_test.py @@ -9,7 +9,7 @@ import pprint import sys import tempfile import unittest -from pxr import Gf, Sdf, Usd, UsdGeom, UsdShade, UsdSkel, UsdUtils, UsdVol +from pxr import Gf, Sdf, Usd, UsdGeom, UsdMtlx, UsdShade, UsdSkel, UsdUtils, UsdVol import bpy @@ -1132,6 +1132,11 @@ class USDExportTest(AbstractUSDTest): material_prim = stage.GetPrimAtPath("/root/_materials/Material") self.assertTrue(material_prim, "Could not find Material prim") + self.assertTrue(material_prim.HasAPI(UsdMtlx.MaterialXConfigAPI)) + mtlx_config_api = UsdMtlx.MaterialXConfigAPI(material_prim) + mtlx_version_attr = mtlx_config_api.GetConfigMtlxVersionAttr() + self.assertTrue(mtlx_version_attr, "Could not find mtlx config version attribute") + material = UsdShade.Material(material_prim) mtlx_output = material.GetOutput("mtlx:surface") self.assertTrue(mtlx_output, "Could not find mtlx output")