From a8a487d0a358e88cb5f29efce95b9deea7e52fc5 Mon Sep 17 00:00:00 2001 From: Julien Duroure Date: Thu, 31 Oct 2024 00:07:25 +0100 Subject: [PATCH 1/2] viewport material export --- addons/io_scene_gltf2/__init__.py | 3 ++ .../blender/exp/material/material_utils.py | 23 ++++++++++ .../blender/exp/material/material_viewport.py | 42 +++++++++++++++++++ .../blender/exp/material/materials.py | 25 +++++------ 4 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 addons/io_scene_gltf2/blender/exp/material/material_utils.py create mode 100644 addons/io_scene_gltf2/blender/exp/material/material_viewport.py diff --git a/addons/io_scene_gltf2/__init__.py b/addons/io_scene_gltf2/__init__.py index 85b0ef72e..dbe14837c 100644 --- a/addons/io_scene_gltf2/__init__.py +++ b/addons/io_scene_gltf2/__init__.py @@ -497,6 +497,9 @@ class ExportGLTF2_Base(ConvertGLTF2_Base): ('PLACEHOLDER', 'Placeholder', 'Do not export materials, but write multiple primitive groups per mesh, keeping material slot information'), + ('VIEWPORT', + 'Viewport', + 'Export minimal materials as defined in Viewport display properties'), ('NONE', 'No export', 'Do not export materials, and combine mesh primitive groups, losing material slot information')), diff --git a/addons/io_scene_gltf2/blender/exp/material/material_utils.py b/addons/io_scene_gltf2/blender/exp/material/material_utils.py new file mode 100644 index 000000000..ec4c70c2f --- /dev/null +++ b/addons/io_scene_gltf2/blender/exp/material/material_utils.py @@ -0,0 +1,23 @@ +# Copyright 2018-2024 The glTF-Blender-IO authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ...com.extras import generate_extras + +def gather_extras(blender_material, export_settings): + if export_settings['gltf_extras']: + return generate_extras(blender_material) + return None + +def gather_name(blender_material, export_settings): + return blender_material.name diff --git a/addons/io_scene_gltf2/blender/exp/material/material_viewport.py b/addons/io_scene_gltf2/blender/exp/material/material_viewport.py new file mode 100644 index 000000000..34fad5b0e --- /dev/null +++ b/addons/io_scene_gltf2/blender/exp/material/material_viewport.py @@ -0,0 +1,42 @@ +# Copyright 2018-2024 The glTF-Blender-IO authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ....io.com import gltf2_io +from .material_utils import gather_extras, gather_name + +def export_viewport_material(blender_material, export_settings): + + pbr_metallic_roughness = gltf2_io.MaterialPBRMetallicRoughness( + base_color_factor=list(blender_material.diffuse_color), + base_color_texture=None, + metallic_factor=blender_material.metallic, + roughness_factor=blender_material.roughness, + metallic_roughness_texture=None, + extensions=None, + extras=None + ) + + return gltf2_io.Material( + alpha_cutoff=None, + alpha_mode=None, + double_sided=None, + emissive_factor=None, + emissive_texture=None, + extensions=None, + extras=gather_extras(blender_material, export_settings), + name=gather_name(blender_material, export_settings), + normal_texture=None, + occlusion_texture=None, + pbr_metallic_roughness=pbr_metallic_roughness + ) diff --git a/addons/io_scene_gltf2/blender/exp/material/materials.py b/addons/io_scene_gltf2/blender/exp/material/materials.py index f99a43a56..8c9a1fa48 100644 --- a/addons/io_scene_gltf2/blender/exp/material/materials.py +++ b/addons/io_scene_gltf2/blender/exp/material/materials.py @@ -18,11 +18,12 @@ from ....io.com import gltf2_io from ....io.com.gltf2_io_extensions import Extension from ....io.exp.user_extensions import export_user_extensions -from ...com.extras import generate_extras from ..cache import cached, cached_by_key from . import unlit as gltf2_unlit from . import texture_info as gltf2_blender_gather_texture_info from . import pbr_metallic_roughness as gltf2_pbr_metallic_roughness +from .material_utils import gather_extras, gather_name +from .material_viewport import export_viewport_material from .extensions.volume import export_volume from .extensions.emission import export_emission_factor, \ export_emission_texture, export_emission_strength_extension @@ -69,6 +70,12 @@ def gather_material(blender_material, export_settings): return None, {"uv_info": {}, "vc_info": {'color': None, 'alpha': None, 'color_type': None, 'alpha_type': None, 'alpha_mode': "OPAQUE"}, "udim_info": {}} + + if export_settings['gltf_materials'] == "VIEWPORT": + return export_viewport_material(blender_material, export_settings), {"uv_info": {}, "vc_info": {'color': None, 'alpha': None, + 'color_type': None, 'alpha_type': None, 'alpha_mode': "OPAQUE"}, "udim_info": {}} + + # Reset exported images / textures nodes export_settings['exported_texture_nodes'] = [] if blender_material.node_tree and blender_material.use_nodes: @@ -117,8 +124,8 @@ def gather_material(blender_material, export_settings): emissive_factor=emissive_factor, emissive_texture=emissive_texture, extensions=extensions, - extras=__gather_extras(blender_material, export_settings), - name=__gather_name(blender_material, export_settings), + extras=gather_extras(blender_material, export_settings), + name=gather_name(blender_material, export_settings), normal_texture=normal_texture, occlusion_texture=occlusion_texture, pbr_metallic_roughness=pbr_metallic_roughness @@ -327,16 +334,6 @@ def __gather_extensions(blender_material, emissive_factor, export_settings): return extensions, uvmap_infos, udim_infos -def __gather_extras(blender_material, export_settings): - if export_settings['gltf_extras']: - return generate_extras(blender_material) - return None - - -def __gather_name(blender_material, export_settings): - return blender_material.name - - def __gather_normal_texture(blender_material, export_settings): normal = get_socket(blender_material.node_tree, blender_material.use_nodes, "Normal") normal_texture, uvmap_info, udim_info, _ = gltf2_blender_gather_texture_info.gather_material_normal_texture_info_class( @@ -677,7 +674,7 @@ def __get_final_material_with_indices(blender_material, base_material, caching_i def get_material_from_idx(material_idx, materials, export_settings): mat = None - if export_settings['gltf_materials'] == "EXPORT" and material_idx is not None: + if export_settings['gltf_materials'] in ["EXPORT", "VIEWPORT"] and material_idx is not None: if materials: i = material_idx if material_idx < len(materials) else -1 mat = materials[i] From 689665dd0a176d654c438f7ee4c42e4d1b419ef8 Mon Sep 17 00:00:00 2001 From: Julien Duroure Date: Thu, 31 Oct 2024 00:17:51 +0100 Subject: [PATCH 2/2] Fix regression for unlit materials --- addons/io_scene_gltf2/blender/exp/material/materials.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/io_scene_gltf2/blender/exp/material/materials.py b/addons/io_scene_gltf2/blender/exp/material/materials.py index 8c9a1fa48..471ef558c 100644 --- a/addons/io_scene_gltf2/blender/exp/material/materials.py +++ b/addons/io_scene_gltf2/blender/exp/material/materials.py @@ -498,8 +498,8 @@ def __export_unlit(blender_material, export_settings): alpha_mode=__gather_alpha_mode(alpha_info, export_settings), double_sided=__gather_double_sided(blender_material, {}, export_settings), extensions={"KHR_materials_unlit": Extension("KHR_materials_unlit", {}, required=False)}, - extras=__gather_extras(blender_material, export_settings), - name=__gather_name(blender_material, export_settings), + extras=gather_extras(blender_material, export_settings), + name=gather_name(blender_material, export_settings), emissive_factor=None, emissive_texture=None, normal_texture=None,