Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

viewport material export #2390

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions addons/io_scene_gltf2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand Down
23 changes: 23 additions & 0 deletions addons/io_scene_gltf2/blender/exp/material/material_utils.py
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions addons/io_scene_gltf2/blender/exp/material/material_viewport.py
Original file line number Diff line number Diff line change
@@ -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
)
29 changes: 13 additions & 16 deletions addons/io_scene_gltf2/blender/exp/material/materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -501,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,
Expand Down Expand Up @@ -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]
Expand Down
Loading