Skip to content
Snippets Groups Projects
Commit ae29cfd1 authored by Julien Duroure's avatar Julien Duroure
Browse files

glTF: Merge glTF Material Output nodes into a single one

parent eca1bdeb
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
bl_info = { bl_info = {
'name': 'glTF 2.0 format', 'name': 'glTF 2.0 format',
'author': 'Julien Duroure, Scurest, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors', '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), 'blender': (3, 3, 0),
'location': 'File > Import-Export', 'location': 'File > Import-Export',
'description': 'Import-Export as glTF 2.0', 'description': 'Import-Export as glTF 2.0',
......
...@@ -17,21 +17,9 @@ def create_settings_group(name): ...@@ -17,21 +17,9 @@ def create_settings_group(name):
thicknessFactor.default_value = 0.0 thicknessFactor.default_value = 0.0
gltf_node_group.nodes.new('NodeGroupOutput') gltf_node_group.nodes.new('NodeGroupOutput')
gltf_node_group_input = gltf_node_group.nodes.new('NodeGroupInput') gltf_node_group_input = gltf_node_group.nodes.new('NodeGroupInput')
gltf_node_group_input.location = -200, 0 specular = gltf_node_group.inputs.new("NodeSocketFloat", "Specular")
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 specular.default_value = 1.0
specularColor = gltf_node_group.inputs.new("NodeSocketColor", "specularColor glTF") specularColor = gltf_node_group.inputs.new("NodeSocketColor", "Specular Color")
specularColor.default_value = [1.0,1.0,1.0,1.0] specularColor.default_value = [1.0,1.0,1.0,1.0]
gltf_node_group_input.location = -200, 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 return gltf_node_group
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
import bpy 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_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 ########################################### ################ glTF Material Output node ###########################################
...@@ -43,36 +42,6 @@ def add_gltf_settings_to_menu(self, context) : ...@@ -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: if bpy.context.preferences.addons['io_scene_gltf2'].preferences.settings_node_ui is True:
self.layout.operator("node.gltf_settings_node_operator") 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 #################### ################################### KHR_materials_variants ####################
# Global UI panel # Global UI panel
...@@ -469,9 +438,7 @@ class SCENE_OT_gltf2_remove_material_variant(bpy.types.Operator): ...@@ -469,9 +438,7 @@ class SCENE_OT_gltf2_remove_material_variant(bpy.types.Operator):
def register(): def register():
bpy.utils.register_class(NODE_OT_GLTF_SETTINGS) 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_settings_to_menu)
bpy.types.NODE_MT_category_SH_NEW_OUTPUT.append(add_gltf_pbr_non_converted_extensions_to_menu)
def variant_register(): def variant_register():
bpy.utils.register_class(SCENE_OT_gltf2_display_variant) bpy.utils.register_class(SCENE_OT_gltf2_display_variant)
...@@ -499,7 +466,6 @@ def variant_register(): ...@@ -499,7 +466,6 @@ def variant_register():
def unregister(): def unregister():
bpy.utils.unregister_class(NODE_OT_GLTF_SETTINGS) bpy.utils.unregister_class(NODE_OT_GLTF_SETTINGS)
bpy.utils.unregister_class(NODE_OT_GLTF_PBR_NON_CONVERTED_EXTENSIONS)
def variant_unregister(): def variant_unregister():
bpy.utils.unregister_class(SCENE_OT_gltf2_variant_add) bpy.utils.unregister_class(SCENE_OT_gltf2_variant_add)
......
...@@ -188,7 +188,7 @@ def __get_image_data(sockets, export_settings) -> ExportImage: ...@@ -188,7 +188,7 @@ def __get_image_data(sockets, export_settings) -> ExportImage:
results = [__get_tex_from_socket(socket, export_settings) for socket in sockets] results = [__get_tex_from_socket(socket, export_settings) for socket in sockets]
# Check if we need a simple mapping or more complex calculation # 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) return __get_image_data_specular(sockets, results, export_settings)
else: else:
return __get_image_data_mapping(sockets, results, export_settings) return __get_image_data_mapping(sockets, results, export_settings)
...@@ -238,7 +238,7 @@ def __get_image_data_mapping(sockets, results, export_settings) -> ExportImage: ...@@ -238,7 +238,7 @@ def __get_image_data_mapping(sockets, results, export_settings) -> ExportImage:
dst_chan = Channel.G dst_chan = Channel.G
elif socket.name == 'Thickness': # For KHR_materials_volume elif socket.name == 'Thickness': # For KHR_materials_volume
dst_chan = Channel.G 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 dst_chan = Channel.A
elif socket.name == "Sigma": # For KHR_materials_sheen elif socket.name == "Sigma": # For KHR_materials_sheen
dst_chan = Channel.A dst_chan = Channel.A
......
...@@ -13,8 +13,8 @@ from io_scene_gltf2.blender.exp import gltf2_blender_gather_texture_info ...@@ -13,8 +13,8 @@ from io_scene_gltf2.blender.exp import gltf2_blender_gather_texture_info
def export_original_specular(blender_material, export_settings): def export_original_specular(blender_material, export_settings):
specular_extension = {} specular_extension = {}
original_specular_socket = gltf2_blender_get.get_socket_original(blender_material, 'specular glTF') original_specular_socket = gltf2_blender_get.get_socket_old(blender_material, 'Specular')
original_specularcolor_socket = gltf2_blender_get.get_socket_original(blender_material, 'specularColor glTF') 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: if original_specular_socket is None or original_specularcolor_socket is None:
return None, None return None, None
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
import bpy import bpy
from mathutils import Vector, Matrix 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 ...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.io.com import gltf2_io_debug
from io_scene_gltf2.blender.exp import gltf2_blender_search_node_tree 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): ...@@ -105,24 +105,6 @@ def get_socket_old(blender_material: bpy.types.Material, name: str):
return None 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): def check_if_is_linked_to_active_output(shader_socket):
for link in shader_socket.links: for link in shader_socket.links:
if isinstance(link.to_node, bpy.types.ShaderNodeOutputMaterial) and link.to_node.is_active_output is True: if isinstance(link.to_node, bpy.types.ShaderNodeOutputMaterial) and link.to_node.is_active_output is True:
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
from ...io.com.gltf2_io import TextureInfo from ...io.com.gltf2_io import TextureInfo
from .gltf2_blender_texture import texture from .gltf2_blender_texture import texture
from .gltf2_blender_image import BlenderImage 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 numpy as np
import bpy import bpy
......
...@@ -5,7 +5,6 @@ from re import M ...@@ -5,7 +5,6 @@ from re import M
import bpy import bpy
from ...io.com.gltf2_io import TextureInfo, MaterialPBRMetallicRoughness 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_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_texture import texture
from .gltf2_blender_KHR_materials_clearcoat import \ from .gltf2_blender_KHR_materials_clearcoat import \
clearcoat, clearcoat_roughness, clearcoat_normal clearcoat, clearcoat_roughness, clearcoat_normal
...@@ -28,7 +27,6 @@ class MaterialHelper: ...@@ -28,7 +27,6 @@ class MaterialHelper:
pymat.pbr_metallic_roughness = \ pymat.pbr_metallic_roughness = \
MaterialPBRMetallicRoughness.from_dict({}) MaterialPBRMetallicRoughness.from_dict({})
self.settings_node = None self.settings_node = None
self.original_pbr_node = None
def is_opaque(self): def is_opaque(self):
alpha_mode = self.pymat.alpha_mode alpha_mode = self.pymat.alpha_mode
...@@ -51,7 +49,7 @@ def pbr_metallic_roughness(mh: MaterialHelper): ...@@ -51,7 +49,7 @@ def pbr_metallic_roughness(mh: MaterialHelper):
# This value may be overidden later if IOR extension is set on file # This value may be overidden later if IOR extension is set on file
pbr_node.inputs['IOR'].default_value = GLTF_IOR 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: if mh.settings_node is None:
mh.settings_node = make_settings_node(mh) mh.settings_node = make_settings_node(mh)
mh.settings_node.location = additional_location mh.settings_node.location = additional_location
...@@ -87,12 +85,6 @@ def pbr_metallic_roughness(mh: MaterialHelper): ...@@ -87,12 +85,6 @@ def pbr_metallic_roughness(mh: MaterialHelper):
make_velvet_socket=need_velvet_node 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': if mh.pymat.extensions and 'KHR_materials_sheen':
pass #TOTOEXT pass #TOTOEXT
...@@ -171,8 +163,8 @@ def pbr_metallic_roughness(mh: MaterialHelper): ...@@ -171,8 +163,8 @@ def pbr_metallic_roughness(mh: MaterialHelper):
location_specular_tint=locs['specularColorTexture'], location_specular_tint=locs['specularColorTexture'],
specular_socket=pbr_node.inputs['Specular'], specular_socket=pbr_node.inputs['Specular'],
specular_tint_socket=pbr_node.inputs['Specular Tint'], 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_specular_socket=mh.settings_node.inputs[2] if mh.settings_node else None,
original_specularcolor_socket=mh.original_pbr_node.inputs[1] if mh.original_pbr_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_specular=locs['original_specularTexture'],
location_original_specularcolor=locs['original_specularColorTexture'] location_original_specularcolor=locs['original_specularColorTexture']
) )
...@@ -752,21 +744,3 @@ def get_settings_group(): ...@@ -752,21 +744,3 @@ def get_settings_group():
# Create a new node group # Create a new node group
gltf_node_group = create_settings_group(gltf_node_group_name) gltf_node_group = create_settings_group(gltf_node_group_name)
return gltf_node_group 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
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment