From 61644b337e39dbff953a7f62352cf3086c9902ed Mon Sep 17 00:00:00 2001 From: Vasyl Pidhirskyi <42581166+VascoPi@users.noreply.github.com> Date: Sat, 17 Sep 2022 02:12:53 +0300 Subject: [PATCH] BLEN-214: Material doesn't update on import from MatX library if render enabled. (#265) PURPOSE Material doesn't update on import from MatX library if render enabled. EFFECT OF CHANGE Material updates properly when import from MaterialX Library during viewport render. --- src/hdusd/export/material.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/hdusd/export/material.py b/src/hdusd/export/material.py index 9a4fd89b..fe13c298 100644 --- a/src/hdusd/export/material.py +++ b/src/hdusd/export/material.py @@ -92,7 +92,20 @@ def sync_update_all(root_prim, mat: bpy.types.Material): mx_file = utils.get_temp_file(".mtlx", f'{mat.name}{mat.hdusd.mx_node_tree.name if mat.hdusd.mx_node_tree else ""}', is_rand=True) mx.writeToXmlFile(doc, str(mx_file)) + surfacematerial = next(node for node in doc.getNodes() if node.getCategory() == 'surfacematerial') for mat_prim in mat_prims: mat_prim.GetReferences().ClearReferences() mat_prim.GetReferences().AddReference(f"./{mx_file.name}", "/MaterialX") + + # apply new bind if shader switched to MaterialX or vice versa + mesh_prim = next((prim for prim in mat_prim.GetParent().GetChildren() if prim.GetTypeName() == 'Mesh'), None) + if not mesh_prim: + return None + + usd_mat = UsdShade.Material.Define(root_prim.GetStage(), mat_prim.GetPath().AppendChild('Materials'). + AppendChild(surfacematerial.getName())) + + bindings = UsdShade.MaterialBindingAPI(mesh_prim) + bindings.UnbindAllBindings() + bindings.Bind(usd_mat)