glTF: Merge glTF Material Output nodes into a single one

This commit is contained in:
Julien Duroure 2022-08-08 17:03:41 +02:00
parent eca1bdeb88
commit ae29cfd186
8 changed files with 14 additions and 104 deletions

View File

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

View File

@ -17,21 +17,9 @@ def create_settings_group(name):
thicknessFactor.default_value = 0.0
gltf_node_group.nodes.new('NodeGroupOutput')
gltf_node_group_input = gltf_node_group.nodes.new('NodeGroupInput')
specular = gltf_node_group.inputs.new("NodeSocketFloat", "Specular")
specular.default_value = 1.0
specularColor = gltf_node_group.inputs.new("NodeSocketColor", "Specular Color")
specularColor.default_value = [1.0,1.0,1.0,1.0]
gltf_node_group_input.location = -200, 0
return gltf_node_group
def get_gltf_pbr_non_converted_name():
return "original glTF PBR data"
def create_gltf_pbr_non_converted_group(name):
gltf_node_group = bpy.data.node_groups.new(name, 'ShaderNodeTree')
specular = gltf_node_group.inputs.new("NodeSocketFloat", "specular glTF")
specular.default_value = 1.0
specularColor = gltf_node_group.inputs.new("NodeSocketColor", "specularColor glTF")
specularColor.default_value = [1.0,1.0,1.0,1.0]
gltf_node_group.nodes.new('NodeGroupOutput')
gltf_node_group_input = gltf_node_group.nodes.new('NodeGroupInput')
gltf_node_group_input.location = -400, 0
return gltf_node_group

View File

@ -3,7 +3,6 @@
import bpy
from ..com.gltf2_blender_material_helpers import get_gltf_node_name, create_settings_group
from ..com.gltf2_blender_material_helpers import get_gltf_pbr_non_converted_name, create_gltf_pbr_non_converted_group
################ glTF Material Output node ###########################################
@ -43,36 +42,6 @@ def add_gltf_settings_to_menu(self, context) :
if bpy.context.preferences.addons['io_scene_gltf2'].preferences.settings_node_ui is True:
self.layout.operator("node.gltf_settings_node_operator")
class NODE_OT_GLTF_PBR_NON_CONVERTED_EXTENSIONS(bpy.types.Operator):
bl_idname = "node.gltf_pbr_non_converted_extensions_operator"
bl_label = "glTF Original PBR data"
bl_description = "Add a node to the active tree for glTF export"
@classmethod
def poll(cls, context):
space = context.space_data
return space.type == "NODE_EDITOR" \
and context.object and context.object.active_material \
and context.object.active_material.use_nodes is True \
and bpy.context.preferences.addons['io_scene_gltf2'].preferences.settings_node_ui is True
def execute(self, context):
gltf_node_name = get_gltf_pbr_non_converted_name()
if gltf_node_name in bpy.data.node_groups:
my_group = bpy.data.node_groups[get_gltf_pbr_non_converted_name()]
else:
my_group = create_gltf_pbr_non_converted_group(gltf_node_name)
node_tree = context.object.active_material.node_tree
new_node = node_tree.nodes.new("ShaderNodeGroup")
new_node.node_tree = bpy.data.node_groups[my_group.name]
return {"FINISHED"}
def add_gltf_pbr_non_converted_extensions_to_menu(self, context) :
if bpy.context.preferences.addons['io_scene_gltf2'].preferences.settings_node_ui is True:
self.layout.operator("node.gltf_pbr_non_converted_extensions_operator")
################################### KHR_materials_variants ####################
# Global UI panel
@ -469,9 +438,7 @@ class SCENE_OT_gltf2_remove_material_variant(bpy.types.Operator):
def register():
bpy.utils.register_class(NODE_OT_GLTF_SETTINGS)
bpy.utils.register_class(NODE_OT_GLTF_PBR_NON_CONVERTED_EXTENSIONS)
bpy.types.NODE_MT_category_SH_NEW_OUTPUT.append(add_gltf_settings_to_menu)
bpy.types.NODE_MT_category_SH_NEW_OUTPUT.append(add_gltf_pbr_non_converted_extensions_to_menu)
def variant_register():
bpy.utils.register_class(SCENE_OT_gltf2_display_variant)
@ -499,7 +466,6 @@ def variant_register():
def unregister():
bpy.utils.unregister_class(NODE_OT_GLTF_SETTINGS)
bpy.utils.unregister_class(NODE_OT_GLTF_PBR_NON_CONVERTED_EXTENSIONS)
def variant_unregister():
bpy.utils.unregister_class(SCENE_OT_gltf2_variant_add)

View File

@ -188,7 +188,7 @@ def __get_image_data(sockets, export_settings) -> ExportImage:
results = [__get_tex_from_socket(socket, export_settings) for socket in sockets]
# Check if we need a simple mapping or more complex calculation
if any([socket.name == "Specular" for socket in sockets]):
if any([socket.name == "Specular" and socket.node.type == "BSDF_PRINCIPLED" for socket in sockets]):
return __get_image_data_specular(sockets, results, export_settings)
else:
return __get_image_data_mapping(sockets, results, export_settings)
@ -238,7 +238,7 @@ def __get_image_data_mapping(sockets, results, export_settings) -> ExportImage:
dst_chan = Channel.G
elif socket.name == 'Thickness': # For KHR_materials_volume
dst_chan = Channel.G
elif socket.name == "specular glTF": # For original KHR_material_specular
elif socket.name == "Specular": # For original KHR_material_specular
dst_chan = Channel.A
elif socket.name == "Sigma": # For KHR_materials_sheen
dst_chan = Channel.A

View File

@ -13,8 +13,8 @@ from io_scene_gltf2.blender.exp import gltf2_blender_gather_texture_info
def export_original_specular(blender_material, export_settings):
specular_extension = {}
original_specular_socket = gltf2_blender_get.get_socket_original(blender_material, 'specular glTF')
original_specularcolor_socket = gltf2_blender_get.get_socket_original(blender_material, 'specularColor glTF')
original_specular_socket = gltf2_blender_get.get_socket_old(blender_material, 'Specular')
original_specularcolor_socket = gltf2_blender_get.get_socket_old(blender_material, 'Specular Color')
if original_specular_socket is None or original_specularcolor_socket is None:
return None, None

View File

@ -4,7 +4,7 @@
import bpy
from mathutils import Vector, Matrix
from ..com.gltf2_blender_material_helpers import get_gltf_node_name, get_gltf_node_old_name, get_gltf_pbr_non_converted_name
from ..com.gltf2_blender_material_helpers import get_gltf_node_name, get_gltf_node_old_name
from ...blender.com.gltf2_blender_conversion import texture_transform_blender_to_gltf
from io_scene_gltf2.io.com import gltf2_io_debug
from io_scene_gltf2.blender.exp import gltf2_blender_search_node_tree
@ -105,24 +105,6 @@ def get_socket_old(blender_material: bpy.types.Material, name: str):
return None
def get_socket_original(blender_material: bpy.types.Material, name: str):
"""
For a given material input name, retrieve the corresponding node tree socket in the special glTF node group.
:param blender_material: a blender material for which to get the socket
:param name: the name of the socket
:return: a blender NodeSocket
"""
gltf_node_group_name = get_gltf_pbr_non_converted_name().lower()
if blender_material.node_tree and blender_material.use_nodes:
nodes = [n for n in blender_material.node_tree.nodes if \
isinstance(n, bpy.types.ShaderNodeGroup) and n.node_tree.name.lower() == gltf_node_group_name]
inputs = sum([[input for input in node.inputs if input.name == name] for node in nodes], [])
if inputs:
return inputs[0]
return None
def check_if_is_linked_to_active_output(shader_socket):
for link in shader_socket.links:
if isinstance(link.to_node, bpy.types.ShaderNodeOutputMaterial) and link.to_node.is_active_output is True:

View File

@ -4,7 +4,7 @@
from ...io.com.gltf2_io import TextureInfo
from .gltf2_blender_texture import texture
from .gltf2_blender_image import BlenderImage
from ..exp.gltf2_blender_image import TmpImageGuard, make_temp_image_copy
from ..exp.gltf2_blender_image import TmpImageGuard
import numpy as np
import bpy

View File

@ -5,7 +5,6 @@ from re import M
import bpy
from ...io.com.gltf2_io import TextureInfo, MaterialPBRMetallicRoughness
from ..com.gltf2_blender_material_helpers import get_gltf_node_name, create_settings_group
from ..com.gltf2_blender_material_helpers import get_gltf_pbr_non_converted_name, create_gltf_pbr_non_converted_group
from .gltf2_blender_texture import texture
from .gltf2_blender_KHR_materials_clearcoat import \
clearcoat, clearcoat_roughness, clearcoat_normal
@ -28,7 +27,6 @@ class MaterialHelper:
pymat.pbr_metallic_roughness = \
MaterialPBRMetallicRoughness.from_dict({})
self.settings_node = None
self.original_pbr_node = None
def is_opaque(self):
alpha_mode = self.pymat.alpha_mode
@ -51,7 +49,7 @@ def pbr_metallic_roughness(mh: MaterialHelper):
# This value may be overidden later if IOR extension is set on file
pbr_node.inputs['IOR'].default_value = GLTF_IOR
if mh.pymat.occlusion_texture is not None:
if mh.pymat.occlusion_texture is not None or (mh.pymat.extensions and 'KHR_materials_specular' in mh.pymat.extensions):
if mh.settings_node is None:
mh.settings_node = make_settings_node(mh)
mh.settings_node.location = additional_location
@ -87,12 +85,6 @@ def pbr_metallic_roughness(mh: MaterialHelper):
make_velvet_socket=need_velvet_node
)
if mh.pymat.extensions and 'KHR_materials_specular' in mh.pymat.extensions:
# We need glTF PBR Non Converted Extensions Node
mh.original_pbr_node = make_pbr_non_converted_extensions_node(mh)
mh.original_pbr_node.location = additional_location
mh.original_pbr_node.width = 180
additional_location = additional_location[0], additional_location[1] - 150
if mh.pymat.extensions and 'KHR_materials_sheen':
pass #TOTOEXT
@ -171,8 +163,8 @@ def pbr_metallic_roughness(mh: MaterialHelper):
location_specular_tint=locs['specularColorTexture'],
specular_socket=pbr_node.inputs['Specular'],
specular_tint_socket=pbr_node.inputs['Specular Tint'],
original_specular_socket=mh.original_pbr_node.inputs[0] if mh.original_pbr_node else None,
original_specularcolor_socket=mh.original_pbr_node.inputs[1] if mh.original_pbr_node else None,
original_specular_socket=mh.settings_node.inputs[2] if mh.settings_node else None,
original_specularcolor_socket=mh.settings_node.inputs[3] if mh.settings_node else None,
location_original_specular=locs['original_specularTexture'],
location_original_specularcolor=locs['original_specularColorTexture']
)
@ -752,21 +744,3 @@ def get_settings_group():
# Create a new node group
gltf_node_group = create_settings_group(gltf_node_group_name)
return gltf_node_group
def make_pbr_non_converted_extensions_node(mh):
"""
Make a Group node with a hookup for PBR Non Converted Extensions. No effect in Blender, but
used to tell the exporter what the original map(s) should be.
"""
node = mh.node_tree.nodes.new('ShaderNodeGroup')
node.node_tree = get_pbr_non_converted_extensions_group()
return node
def get_pbr_non_converted_extensions_group():
gltf_node_group_name = get_gltf_pbr_non_converted_name()
if gltf_node_group_name in bpy.data.node_groups:
gltf_node_group = bpy.data.node_groups[gltf_node_group_name]
else:
# Create a new node group
gltf_node_group = create_gltf_pbr_non_converted_group(gltf_node_group_name)
return gltf_node_group