glTF exporter: Fix exporter both VertexColor & colorFactor in the same time

This commit is contained in:
Julien Duroure
2024-05-24 09:32:42 +02:00
parent f8b8a1a5b6
commit d80a1dd5f8
3 changed files with 63 additions and 5 deletions

View File

@@ -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',

View File

@@ -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

View File

@@ -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