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

Export: All available materials #1080

Closed
wants to merge 5 commits into from
Closed
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
8 changes: 8 additions & 0 deletions addons/io_scene_gltf2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ def __init__(self):
default=True
)

export_all_materials: BoolProperty(
name='All materials',
description='Export all avalaible materials',
default=False
)

export_colors: BoolProperty(
name='Vertex Colors',
description='Export vertex colors with meshes',
Expand Down Expand Up @@ -432,6 +438,7 @@ def execute(self, context):
export_settings['gltf_draco_mesh_compression'] = False

export_settings['gltf_materials'] = self.export_materials
export_settings['gltf_all_materials'] = self.export_all_materials
export_settings['gltf_colors'] = self.export_colors
export_settings['gltf_cameras'] = self.export_cameras

Expand Down Expand Up @@ -567,6 +574,7 @@ def draw(self, context):
col.prop(operator, 'export_extras')
col.prop(operator, 'export_cameras')
col.prop(operator, 'export_lights')
col.prop(operator, 'export_all_materials')


class GLTF_PT_export_transform(bpy.types.Panel):
Expand Down
5 changes: 3 additions & 2 deletions addons/io_scene_gltf2/blender/exp/gltf2_blender_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __export(export_settings):


def __gather_gltf(exporter, export_settings):
active_scene_idx, scenes, animations = gltf2_blender_gather.gather_gltf2(export_settings)
active_scene_idx, scenes, animations, materials = gltf2_blender_gather.gather_gltf2(export_settings)

plan = {'active_scene_idx': active_scene_idx, 'scenes': scenes, 'animations': animations}
export_user_extensions('gather_gltf_hook', export_settings, plan)
Expand All @@ -75,7 +75,8 @@ def __gather_gltf(exporter, export_settings):
exporter.add_scene(scene, idx==active_scene_idx)
for animation in animations:
exporter.add_animation(animation)

for material in materials:
exporter.add_material(material)

def __create_buffer(exporter, export_settings):
buffer = bytes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
MORPH_TANGENT = 'gltf_morph_tangent'
MORPH_NORMAL = 'gltf_morph_normal'
MATERIALS = 'gltf_materials'
ALL_MATERIALS = 'gltf_all_materials'
EXTRAS = 'gltf_extras'
CAMERAS = 'gltf_cameras'
LIGHTS = 'gltf_lights'
Expand Down
31 changes: 30 additions & 1 deletion addons/io_scene_gltf2/blender/exp/gltf2_blender_gather.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from io_scene_gltf2.io.com.gltf2_io_debug import print_console
from io_scene_gltf2.blender.exp import gltf2_blender_gather_nodes
from io_scene_gltf2.blender.exp import gltf2_blender_gather_animations
from io_scene_gltf2.blender.exp import gltf2_blender_gather_materials
from io_scene_gltf2.blender.exp.gltf2_blender_gather_cache import cached
from ..com.gltf2_blender_extras import generate_extras
from io_scene_gltf2.blender.exp import gltf2_blender_export_keys
Expand All @@ -32,14 +33,18 @@ def gather_gltf2(export_settings):
"""
scenes = []
animations = [] # unfortunately animations in gltf2 are just as 'root' as scenes.
materials = []
active_scene = None
for blender_scene in bpy.data.scenes:
scenes.append(__gather_scene(blender_scene, export_settings))
if export_settings[gltf2_blender_export_keys.ANIMATIONS]:
animations += __gather_animations(blender_scene, export_settings)
if export_settings[gltf2_blender_export_keys.ALL_MATERIALS]:
materials += __gather_materials(blender_scene, export_settings)
if bpy.context.scene.name == blender_scene.name:
active_scene = len(scenes) -1
return active_scene, scenes, animations

return active_scene, scenes, animations, materials


@cached
Expand Down Expand Up @@ -147,6 +152,30 @@ def __gather_animations(blender_scene, export_settings):

return new_animations

def __gather_materials(blender_scene, export_settings):
materials = []

for _blender_object in blender_scene.objects:
blender_object = _blender_object.proxy if _blender_object.proxy else _blender_object

# Check if this object is exclude from view layer
node = gltf2_blender_gather_nodes.gather_node(
blender_object,
blender_object.library.name if blender_object.library else None,
blender_scene, None, export_settings)
if node is None:
continue

for slot in blender_object.material_slots:
# Check if this slot as assigned material
if slot.material is None:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scurest For than i understand, you thing to change if slot.material is None: to if slot.material is None or slot.material == blender_object.active_material: ?

For exclude active_material in __gather_materials array for better performance ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er, no. I was thinking instead of looking in any material slot in any object in an exported scene, only looking in any material slot in an object that gets exported.

Is that what the lastest "Check if this object is exclude from view layer" check is supposed to do?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You think is better to use bpy.data.objects directly ?
But i need blender_scene with gltf2_blender_gather_nodes.gather_node, i can use users_scene with Object but it's probably weird code because users_scene is tuple.

"Check if this object is exclude from view layer" is because if you uncheck collection, all objects associated with this collection are not exported and is not necessary to exported materials assigned to these objects

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking something like this.

Copy link
Author

@StidOfficial StidOfficial May 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, your code is more efficient.
But probably change setting name like "Variants materials" for KHR_materials_variants ?

continue

double_sided = not slot.material.use_backface_culling
materials.append(gltf2_blender_gather_materials.gather_material(slot.material, double_sided, export_settings))

return materials


def __gather_extras(blender_object, export_settings):
if export_settings[gltf2_blender_export_keys.EXTRAS]:
Expand Down
12 changes: 12 additions & 0 deletions addons/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ def add_animation(self, animation: gltf2_io.Animation):

self.__traverse(animation)

def add_material(self, material: gltf2_io.Material):
"""
Add an material to the glTF.

:param material: glTF material, with python style references (names)
:return: nothing
"""
if self.__finalized:
raise RuntimeError("Tried to add material to finalized glTF file")

self.__traverse(material)

def __to_reference(self, property):
"""
Append a child of root property to its respective list and return a reference into said list.
Expand Down