From d80a1dd5f80e5c017392c4e5e5fbe3d71687bf45 Mon Sep 17 00:00:00 2001 From: Julien Duroure Date: Fri, 24 May 2024 09:32:42 +0200 Subject: [PATCH] glTF exporter: Fix exporter both VertexColor & colorFactor in the same time --- .../addons_core/io_scene_gltf2/__init__.py | 2 +- ...gather_materials_pbr_metallic_roughness.py | 15 ++++-- .../gltf2_blender_search_node_tree.py | 51 +++++++++++++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/scripts/addons_core/io_scene_gltf2/__init__.py b/scripts/addons_core/io_scene_gltf2/__init__.py index cd3c6014526..de00fd85743 100755 --- a/scripts/addons_core/io_scene_gltf2/__init__.py +++ b/scripts/addons_core/io_scene_gltf2/__init__.py @@ -5,7 +5,7 @@ bl_info = { 'name': 'glTF 2.0 format', 'author': 'Julien Duroure, Scurest, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors', - "version": (4, 2, 36), + "version": (4, 2, 37), 'blender': (4, 2, 0), 'location': 'File > Import-Export', 'description': 'Import-Export as glTF 2.0', diff --git a/scripts/addons_core/io_scene_gltf2/blender/exp/material/gltf2_blender_gather_materials_pbr_metallic_roughness.py b/scripts/addons_core/io_scene_gltf2/blender/exp/material/gltf2_blender_gather_materials_pbr_metallic_roughness.py index 5c7ed35a3ba..8af1df6f790 100644 --- a/scripts/addons_core/io_scene_gltf2/blender/exp/material/gltf2_blender_gather_materials_pbr_metallic_roughness.py +++ b/scripts/addons_core/io_scene_gltf2/blender/exp/material/gltf2_blender_gather_materials_pbr_metallic_roughness.py @@ -15,7 +15,8 @@ from .gltf2_blender_search_node_tree import \ get_const_from_default_value_socket, \ get_socket, \ get_factor_from_socket, \ - gather_alpha_info + gather_alpha_info, \ + gather_color_info @cached @@ -66,12 +67,16 @@ def __gather_base_color_factor(blender_material, export_settings): "alpha": None, "color_type": None, "alpha_type": None} rgb, alpha = None, None + vc_info = {"color": None, "alpha": None, "color_type": None, "alpha_type": None, "alpha_mode": "OPAQUE"} path_alpha = None path = None alpha_socket = get_socket(blender_material.node_tree, blender_material.use_nodes, "Alpha") if alpha_socket.socket is not None and isinstance(alpha_socket.socket, bpy.types.NodeSocket): alpha_info = gather_alpha_info(alpha_socket.to_node_nav()) + vc_info['alpha'] = alpha_info['alphaColorAttrib'] + vc_info['alpha_type'] = alpha_info['alphaColorAttribType'] + vc_info['alpha_mode'] = alpha_info['alphaMode'] alpha = alpha_info['alphaFactor'] path_alpha = alpha_info['alphaPath'] @@ -85,7 +90,11 @@ def __gather_base_color_factor(blender_material, export_settings): blender_material.node_tree, blender_material.use_nodes, "BaseColorFactor") if base_color_socket.socket is not None and isinstance(base_color_socket.socket, bpy.types.NodeSocket): if export_settings['gltf_image_format'] != "NONE": - rgb, path = get_factor_from_socket(base_color_socket, kind='RGB') + rgb_vc_info = gather_color_info(base_color_socket.to_node_nav()) + vc_info['color'] = rgb_vc_info['colorAttrib'] + vc_info['color_type'] = rgb_vc_info['colorAttribType'] + rgb = rgb_vc_info['colorFactor'] + path = rgb_vc_info['colorPath'] else: rgb, path = get_const_from_default_value_socket(base_color_socket, kind='RGB') @@ -115,8 +124,6 @@ def __gather_base_color_factor(blender_material, export_settings): rgba = [*rgb, alpha] - vc_info = get_vertex_color_info(base_color_socket, alpha_socket, export_settings) - if rgba == [1, 1, 1, 1]: return None, vc_info return rgba, vc_info diff --git a/scripts/addons_core/io_scene_gltf2/blender/exp/material/gltf2_blender_search_node_tree.py b/scripts/addons_core/io_scene_gltf2/blender/exp/material/gltf2_blender_search_node_tree.py index 882ecd1ce6a..a69dfc8d157 100644 --- a/scripts/addons_core/io_scene_gltf2/blender/exp/material/gltf2_blender_search_node_tree.py +++ b/scripts/addons_core/io_scene_gltf2/blender/exp/material/gltf2_blender_search_node_tree.py @@ -390,6 +390,54 @@ class NodeNav: return None, None +# Gather information about factor and vertex color from the Color socket +# The general form for color is +# color = factor * color attribute * texture +def gather_color_info(color_nav): + info = { + 'colorFactor': None, + 'colorAttrib': None, + 'colorAttribType': None, + 'colorPath': None, + } + + # Reads the factor and color attribute by checking for variations on + # -> [Multiply by Factor] -> [Multiply by Color Attrib] -> + + for _ in range(2): # Twice, to handle both factor and attrib + # No factor found yet? + if info['colorFactor'] is None: + a, color_path = color_nav.get_constant() + if a is not None: + info['colorFactor'] = a[:3] + info['colorPath'] = color_path + break + + a, color_path = detect_multiply_by_constant(color_nav) + if a is not None: + info['colorFactor'] = a[:3] + info['colorPath'] = color_path + continue + + # No color attrib found yet? + if info['colorAttrib'] is None: + attr = get_color_attrib(color_nav) + if attr is not None: + info['colorAttrib'] = attr + info['colorAttribType'] = 'active' if attr == "" else 'name' + break + + attr = detect_multiply_by_color_attrib(color_nav) + if attr is not None: + info['colorAttrib'] = attr + info['colorAttribType'] = 'active' if attr == "" else 'name' + continue + + break + + return info + + # Gather information about alpha from the Alpha socket. Alpha has the # general form # @@ -403,6 +451,7 @@ def gather_alpha_info(alpha_nav): 'alphaCutoff': None, 'alphaFactor': None, 'alphaColorAttrib': None, + 'alphaColorAttribType': None, 'alphaPath': None, } if not alpha_nav: @@ -443,11 +492,13 @@ def gather_alpha_info(alpha_nav): attr = get_color_attrib(alpha_nav) if attr is not None: info['alphaColorAttrib'] = attr + info['alphaColorAttribType'] = 'active' if attr == "" else 'name' break attr = detect_multiply_by_color_attrib(alpha_nav) if attr is not None: info['alphaColorAttrib'] = attr + info['alphaColorAttribType'] = 'active' if attr == "" else 'name' continue break