glTF: Fix factors export after Mix Node change in Blender

This commit is contained in:
Julien Duroure 2022-12-10 00:32:10 +01:00
parent 1168a4fd9b
commit 3fbcd35feb
Notes: blender-bot 2023-02-14 18:10:38 +01:00
Referenced by issue blender/blender#102967: 3.4: Potential candidates for corrective releases
Referenced by issue blender/blender#102967, 3.4: Potential candidates for corrective releases
Referenced by issue #103220, No baseColorFactor in glTF with new Mix node
7 changed files with 43 additions and 37 deletions

View File

@ -4,8 +4,8 @@
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": (3, 5, 5),
'blender': (3, 3, 0),
"version": (3, 5, 6),
'blender': (3, 4, 0),
'location': 'File > Import-Export',
'description': 'Import-Export as glTF 2.0',
'warning': '',

View File

@ -123,14 +123,14 @@ def __gather_occlusion_strength(primary_socket, export_settings):
# Look for a MixRGB node that mixes with pure white in front of
# primary_socket. The mix factor gives the occlusion strength.
node = gltf2_blender_get.previous_node(primary_socket)
if node and node.type == 'MIX_RGB' and node.blend_type == 'MIX':
fac = gltf2_blender_get.get_const_from_socket(node.inputs['Fac'], kind='VALUE')
col1 = gltf2_blender_get.get_const_from_socket(node.inputs['Color1'], kind='RGB')
col2 = gltf2_blender_get.get_const_from_socket(node.inputs['Color2'], kind='RGB')
if node and node.type == 'MIX' and node.blend_type == 'MIX':
fac = gltf2_blender_get.get_const_from_socket(node.inputs['Factor'], kind='VALUE')
col1 = gltf2_blender_get.get_const_from_socket(node.inputs[6], kind='RGB')
col2 = gltf2_blender_get.get_const_from_socket(node.inputs[7], kind='RGB')
if fac is not None:
if col1 == [1, 1, 1] and col2 is None:
if col1 == [1.0, 1.0, 1.0] and col2 is None:
return fac
if col1 is None and col2 == [1, 1, 1]:
if col1 is None and col2 == [1.0, 1.0, 1.0]:
return 1.0 - fac # reversed for reversed inputs
return None

View File

@ -244,14 +244,14 @@ def get_factor_from_socket(socket, kind):
if node is not None:
x1, x2 = None, None
if kind == 'RGB':
if node.type == 'MIX_RGB' and node.blend_type == 'MULTIPLY':
if node.type in 'MIX' and node.data_type == "RGBA" and node.blend_type == 'MULTIPLY':
# TODO: handle factor in inputs[0]?
x1 = get_const_from_socket(node.inputs[1], kind)
x2 = get_const_from_socket(node.inputs[2], kind)
x1 = get_const_from_socket(node.inputs[6], kind)
x2 = get_const_from_socket(node.inputs[7], kind)
if kind == 'VALUE':
if node.type == 'MATH' and node.operation == 'MULTIPLY':
x1 = get_const_from_socket(node.inputs[0], kind)
x2 = get_const_from_socket(node.inputs[1], kind)
x2 = get_const_from_socket(node.inputs[1], kind)
if x1 is not None and x2 is None: return x1
if x2 is not None and x1 is None: return x2

View File

@ -117,16 +117,17 @@ def specular_glossiness(mh, location, specular_socket, roughness_socket):
# Mix in spec/gloss factor
if spec_factor != [1, 1, 1] or gloss_factor != 1:
if spec_factor != [1, 1, 1]:
node = mh.node_tree.nodes.new('ShaderNodeMixRGB')
node = mh.node_tree.nodes.new('ShaderNodeMix')
node.data_type = 'RGBA'
node.label = 'Specular Factor'
node.location = x - 140, y
node.blend_type = 'MULTIPLY'
# Outputs
mh.node_tree.links.new(specular_socket, node.outputs[0])
# Inputs
node.inputs['Fac'].default_value = 1.0
specular_socket = node.inputs['Color1']
node.inputs['Color2'].default_value = spec_factor + [1]
node.inputs['Factor'].default_value = 1.0
specular_socket = node.inputs[6]
node.inputs[7].default_value = spec_factor + [1]
if gloss_factor != 1:
node = mh.node_tree.nodes.new('ShaderNodeMath')

View File

@ -40,16 +40,17 @@ def sheen( mh,
# Mix sheenColor factor
sheenColorFactor = sheenColorFactor + [1.0]
if sheenColorFactor != [1.0, 1.0, 1.0, 1.0]:
node = mh.node_tree.nodes.new('ShaderNodeMixRGB')
node = mh.node_tree.nodes.new('ShaderNodeMix')
node.label = 'sheenColor Factor'
node.data_type = 'RGBA'
node.location = x_sheenColor - 140, y_sheenColor
node.blend_type = 'MULTIPLY'
# Outputs
mh.node_tree.links.new(sheenColor_socket, node.outputs[0])
# Inputs
node.inputs['Fac'].default_value = 1.0
sheenColor_socket = node.inputs['Color1']
node.inputs['Color2'].default_value = sheenColorFactor
node.inputs['Factor'].default_value = 1.0
sheenColor_socket = node.inputs[6]
node.inputs[7].default_value = sheenColorFactor
x_sheenColor -= 200
texture(

View File

@ -335,16 +335,17 @@ def original_specular( mh,
specular_color_factor = list(specular_color_factor) + [1.0]
if specular_color_factor != [1.0, 1.0, 1.0, 1.0]:
# Mix specularColorFactor
node = mh.node_tree.nodes.new('ShaderNodeMixRGB')
node = mh.node_tree.nodes.new('ShaderNodeMix')
node.label = 'SpecularColor Factor'
node.data_type = 'RGBA'
node.location = x_specularcolor - 140, y_specularcolor
node.blend_type = 'MULTIPLY'
# Outputs
mh.node_tree.links.new(original_specularcolor_socket, node.outputs[0])
# Inputs
node.inputs['Fac'].default_value = 1.0
original_specularcolor_socket = node.inputs['Color1']
node.inputs['Color2'].default_value = specular_color_factor
node.inputs['Factor'].default_value = 1.0
original_specularcolor_socket = node.inputs[6]
node.inputs[7].default_value = specular_color_factor
x_specularcolor -= 200
texture(

View File

@ -316,16 +316,17 @@ def emission(mh: MaterialHelper, location, color_socket, strength_socket):
# Otherwise, use a multiply node for it
else:
if emissive_factor != [1, 1, 1]:
node = mh.node_tree.nodes.new('ShaderNodeMixRGB')
node = mh.node_tree.nodes.new('ShaderNodeMix')
node.label = 'Emissive Factor'
node.data_type = 'RGBA'
node.location = x - 140, y
node.blend_type = 'MULTIPLY'
# Outputs
mh.node_tree.links.new(color_socket, node.outputs[0])
# Inputs
node.inputs['Fac'].default_value = 1.0
color_socket = node.inputs['Color1']
node.inputs['Color2'].default_value = emissive_factor + [1]
node.inputs['Factor'].default_value = 1.0
color_socket = node.inputs[6]
node.inputs[7].default_value = emissive_factor + [1]
x -= 200
@ -381,16 +382,17 @@ def base_color(
needs_alpha_factor = base_color_factor[3] != 1.0 and alpha_socket is not None
if needs_color_factor or needs_alpha_factor:
if needs_color_factor:
node = mh.node_tree.nodes.new('ShaderNodeMixRGB')
node = mh.node_tree.nodes.new('ShaderNodeMix')
node.label = 'Color Factor'
node.data_type = "RGBA"
node.location = x - 140, y
node.blend_type = 'MULTIPLY'
# Outputs
mh.node_tree.links.new(color_socket, node.outputs[0])
# Inputs
node.inputs['Fac'].default_value = 1.0
color_socket = node.inputs['Color1']
node.inputs['Color2'].default_value = base_color_factor[:3] + [1]
node.inputs['Factor'].default_value = 1.0
color_socket = node.inputs[6]
node.inputs[7].default_value = base_color_factor[:3] + [1]
if needs_alpha_factor:
node = mh.node_tree.nodes.new('ShaderNodeMath')
@ -413,7 +415,7 @@ def base_color(
# Mix texture and vertex color together
if base_color_texture is not None and mh.vertex_color:
node = mh.node_tree.nodes.new('ShaderNodeMixRGB')
node = mh.node_tree.nodes.new('ShaderNodeMix')
node.label = 'Mix Vertex Color'
node.location = x - 140, y
node.blend_type = 'MULTIPLY'
@ -575,16 +577,17 @@ def occlusion(mh: MaterialHelper, location, occlusion_socket):
if strength is None: strength = 1.0
if strength != 1.0:
# Mix with white
node = mh.node_tree.nodes.new('ShaderNodeMixRGB')
node = mh.node_tree.nodes.new('ShaderNodeMix')
node.label = 'Occlusion Strength'
node.data_type = 'RGBA'
node.location = x - 140, y
node.blend_type = 'MIX'
# Outputs
mh.node_tree.links.new(occlusion_socket, node.outputs[0])
# Inputs
node.inputs['Fac'].default_value = strength
node.inputs['Color1'].default_value = [1, 1, 1, 1]
occlusion_socket = node.inputs['Color2']
node.inputs['Factor'].default_value = strength
node.inputs[6].default_value = [1, 1, 1, 1]
occlusion_socket = node.inputs[7]
x -= 200