glTF exporter: define a user extension API

This commit is contained in:
Julien Duroure 2019-12-14 08:42:04 +01:00
parent 13b7618409
commit 18246268e8
18 changed files with 176 additions and 8 deletions

View File

@ -15,7 +15,7 @@
bl_info = {
'name': 'glTF 2.0 format',
'author': 'Julien Duroure, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors',
"version": (1, 1, 23),
"version": (1, 1, 24),
'blender': (2, 81, 6),
'location': 'File > Import-Export',
'description': 'Import-Export as glTF 2.0',
@ -67,6 +67,7 @@ from bpy_extras.io_utils import ImportHelper, ExportHelper
# Functions / Classes.
#
extension_panel_unregister_functors = []
class ExportGLTF2_Base:
# TODO: refactor to avoid boilerplate
@ -334,6 +335,7 @@ class ExportGLTF2_Base:
def invoke(self, context, event):
settings = context.scene.get(self.scene_key)
self.will_save_settings = False
self.has_active_extenions = False
if settings:
try:
for (k, v) in settings.items():
@ -344,6 +346,15 @@ class ExportGLTF2_Base:
self.report({"ERROR"}, "Loading export settings failed. Removed corrupted settings")
del context.scene[self.scene_key]
import sys
for addon_name in bpy.context.preferences.addons.keys():
try:
if hasattr(sys.modules[addon_name], 'glTF2ExportUserExtension'):
extension_panel_unregister_functors.append(sys.modules[addon_name].register_panel())
self.has_active_extenions = True
except Exception:
pass
return ExportHelper.invoke(self, context, event)
def save_settings(self, context):
@ -438,6 +449,15 @@ class ExportGLTF2_Base:
export_settings['gltf_binaryfilename'] = os.path.splitext(os.path.basename(
bpy.path.ensure_ext(self.filepath,self.filename_ext)))[0] + '.bin'
user_extensions = []
import sys
for addon_name in bpy.context.preferences.addons.keys():
if hasattr(sys.modules[addon_name], 'glTF2ExportUserExtension'):
extension_ctor = sys.modules[addon_name].glTF2ExportUserExtension
user_extensions.append(extension_ctor())
export_settings['gltf_user_extensions'] = user_extensions
return gltf2_blender_export.save(context, export_settings)
@ -734,6 +754,25 @@ class GLTF_PT_export_animation_skinning(bpy.types.Panel):
layout.active = operator.export_skins
layout.prop(operator, 'export_all_influences')
class GLTF_PT_export_user_extensions(bpy.types.Panel):
bl_space_type = 'FILE_BROWSER'
bl_region_type = 'TOOL_PROPS'
bl_label = "Extensions"
bl_parent_id = "FILE_PT_operator"
bl_options = {'DEFAULT_CLOSED'}
@classmethod
def poll(cls, context):
sfile = context.space_data
operator = sfile.active_operator
return operator.bl_idname == "EXPORT_SCENE_OT_gltf" and operator.has_active_extenions
def draw(self, context):
layout = self.layout
layout.use_property_split = True
layout.use_property_decorate = False # No animation.
class ExportGLTF2(bpy.types.Operator, ExportGLTF2_Base, ExportHelper):
"""Export scene as glTF 2.0 file"""
@ -859,6 +898,7 @@ classes = (
GLTF_PT_export_animation_export,
GLTF_PT_export_animation_shapekeys,
GLTF_PT_export_animation_skinning,
GLTF_PT_export_user_extensions,
ImportGLTF2
)
@ -876,6 +916,10 @@ def register():
def unregister():
for c in classes:
bpy.utils.unregister_class(c)
for f in extension_panel_unregister_functors:
f()
extension_panel_unregister_functors.clear()
# bpy.utils.unregister_module(__name__)
# remove from the export / import menu

View File

@ -20,6 +20,7 @@ from io_scene_gltf2.blender.exp.gltf2_blender_gather_cache import cached
from io_scene_gltf2.blender.exp import gltf2_blender_gather_nodes
from io_scene_gltf2.blender.exp import gltf2_blender_gather_joints
from io_scene_gltf2.blender.exp import gltf2_blender_gather_skins
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
@cached
def gather_animation_channel_target(channels: typing.Tuple[bpy.types.FCurve],
@ -29,13 +30,23 @@ def gather_animation_channel_target(channels: typing.Tuple[bpy.types.FCurve],
export_settings
) -> gltf2_io.AnimationChannelTarget:
return gltf2_io.AnimationChannelTarget(
animation_channel_target = gltf2_io.AnimationChannelTarget(
extensions=__gather_extensions(channels, blender_object, export_settings, bake_bone),
extras=__gather_extras(channels, blender_object, export_settings, bake_bone),
node=__gather_node(channels, blender_object, export_settings, bake_bone),
path=__gather_path(channels, blender_object, export_settings, bake_bone, bake_channel)
)
export_user_extensions('gather_animation_channel_target_hook',
export_settings,
animation_channel_target,
channels,
blender_object,
bake_bone,
bake_channel)
return animation_channel_target
def __gather_extensions(channels: typing.Tuple[bpy.types.FCurve],
blender_object: bpy.types.Object,
export_settings,

View File

@ -23,6 +23,7 @@ from io_scene_gltf2.blender.exp import gltf2_blender_gather_animation_samplers
from io_scene_gltf2.blender.exp import gltf2_blender_gather_animation_channel_target
from io_scene_gltf2.blender.exp import gltf2_blender_get
from io_scene_gltf2.blender.exp import gltf2_blender_gather_skins
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
@cached
@ -149,13 +150,26 @@ def __gather_animation_channel(channels: typing.Tuple[bpy.types.FCurve],
if not __filter_animation_channel(channels, blender_object, export_settings):
return None
return gltf2_io.AnimationChannel(
animation_channel = gltf2_io.AnimationChannel(
extensions=__gather_extensions(channels, blender_object, export_settings, bake_bone),
extras=__gather_extras(channels, blender_object, export_settings, bake_bone),
sampler=__gather_sampler(channels, blender_object, export_settings, bake_bone, bake_channel, bake_range_start, bake_range_end, action_name),
target=__gather_target(channels, blender_object, export_settings, bake_bone, bake_channel)
)
export_user_extensions('gather_animation_channel_hook',
export_settings,
animation_channel,
channels,
blender_object,
bake_bone,
bake_channel,
bake_range_start,
bake_range_end,
action_name)
return animation_channel
def __filter_animation_channel(channels: typing.Tuple[bpy.types.FCurve],
blender_object: bpy.types.Object,

View File

@ -27,6 +27,7 @@ from io_scene_gltf2.io.com import gltf2_io
from io_scene_gltf2.io.com import gltf2_io_constants
from io_scene_gltf2.io.exp import gltf2_io_binary_data
from . import gltf2_blender_export_keys
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
@cached
@ -55,7 +56,7 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve],
export_settings)
return gltf2_io.AnimationSampler(
sampler = gltf2_io.AnimationSampler(
extensions=__gather_extensions(channels, blender_object_if_armature, export_settings, bake_bone, bake_channel),
extras=__gather_extras(channels, blender_object_if_armature, export_settings, bake_bone, bake_channel),
input=__gather_input(channels, blender_object_if_armature, non_keyed_values,
@ -72,6 +73,19 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve],
export_settings)
)
export_user_extensions('gather_animation_sampler_hook',
export_settings,
sampler,
channels,
blender_object,
bake_bone,
bake_channel,
bake_range_start,
bake_range_end,
action_name)
return sampler
def __gather_non_keyed_values(channels: typing.Tuple[bpy.types.FCurve],
blender_object: bpy.types.Object,
blender_object_if_armature: typing.Optional[bpy.types.Object],

View File

@ -19,6 +19,7 @@ from io_scene_gltf2.io.com import gltf2_io
from io_scene_gltf2.blender.exp import gltf2_blender_gather_animation_channels
from io_scene_gltf2.io.com.gltf2_io_debug import print_console
from ..com.gltf2_blender_extras import generate_extras
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
def gather_animations(blender_object: bpy.types.Object,
@ -108,6 +109,8 @@ def __gather_animation(blender_action: bpy.types.Action,
if not animation.channels:
return None
export_user_extensions('gather_animation_hook', export_settings, animation, blender_action, blender_object)
return animation

View File

@ -16,6 +16,7 @@ from . import gltf2_blender_export_keys
from io_scene_gltf2.blender.exp.gltf2_blender_gather_cache import cached
from ..com.gltf2_blender_extras import generate_extras
from io_scene_gltf2.io.com import gltf2_io
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
import bpy
import math
@ -26,7 +27,7 @@ def gather_camera(blender_camera, export_settings):
if not __filter_camera(blender_camera, export_settings):
return None
return gltf2_io.Camera(
camera = gltf2_io.Camera(
extensions=__gather_extensions(blender_camera, export_settings),
extras=__gather_extras(blender_camera, export_settings),
name=__gather_name(blender_camera, export_settings),
@ -35,6 +36,10 @@ def gather_camera(blender_camera, export_settings):
type=__gather_type(blender_camera, export_settings)
)
export_user_extensions('gather_camera_hook', export_settings, camera, blender_camera)
return camera
def __filter_camera(blender_camera, export_settings):
return bool(__gather_type(blender_camera, export_settings))

View File

@ -26,6 +26,7 @@ from io_scene_gltf2.io.exp import gltf2_io_image_data
from io_scene_gltf2.io.com import gltf2_io_debug
from io_scene_gltf2.blender.exp import gltf2_blender_image
from io_scene_gltf2.blender.exp.gltf2_blender_gather_cache import cached
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
@cached
@ -47,7 +48,7 @@ def gather_image(
uri = __gather_uri(image_data, mime_type, name, export_settings)
buffer_view = __gather_buffer_view(image_data, mime_type, name, export_settings)
return __make_image(
image = __make_image(
buffer_view,
__gather_extensions(blender_shader_sockets_or_texture_slots, export_settings),
__gather_extras(blender_shader_sockets_or_texture_slots, export_settings),
@ -57,6 +58,10 @@ def gather_image(
export_settings
)
export_user_extensions('gather_image_hook', export_settings, image, blender_shader_sockets_or_texture_slots)
return image
@cached
def __make_image(buffer_view, extensions, extras, mime_type, name, uri, export_settings):
return gltf2_io.Image(

View File

@ -21,6 +21,7 @@ from io_scene_gltf2.blender.exp import gltf2_blender_search_node_tree
from io_scene_gltf2.blender.exp import gltf2_blender_export_keys
from io_scene_gltf2.blender.exp import gltf2_blender_get
from io_scene_gltf2.io.com.gltf2_io_extensions import Extension
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
@cached
@ -41,6 +42,11 @@ def gather_material_normal_texture_info_class(blender_shader_sockets_or_texture_
if texture_info.index is None:
return None
export_user_extensions('gather_material_normal_texture_info_class_hook',
export_settings,
texture_info,
blender_shader_sockets_or_texture_slots)
return texture_info

View File

@ -20,6 +20,7 @@ from io_scene_gltf2.blender.exp import gltf2_blender_gather_texture
from io_scene_gltf2.blender.exp import gltf2_blender_search_node_tree
from io_scene_gltf2.blender.exp import gltf2_blender_get
from io_scene_gltf2.io.com.gltf2_io_extensions import Extension
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
@cached
@ -40,6 +41,11 @@ def gather_material_occlusion_texture_info_class(blender_shader_sockets_or_textu
if texture_info.index is None:
return None
export_user_extensions('gather_material_occlusion_texture_info_class_hook',
export_settings,
texture_info,
blender_shader_sockets_or_texture_slots)
return texture_info

View File

@ -25,6 +25,7 @@ from io_scene_gltf2.blender.exp import gltf2_blender_search_node_tree
from io_scene_gltf2.blender.exp import gltf2_blender_gather_materials_pbr_metallic_roughness
from ..com.gltf2_blender_extras import generate_extras
from io_scene_gltf2.blender.exp import gltf2_blender_get
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
@cached
@ -55,6 +56,8 @@ def gather_material(blender_material, mesh_double_sided, export_settings):
pbr_metallic_roughness=__gather_pbr_metallic_roughness(blender_material, orm_texture, export_settings)
)
export_user_extensions('gather_material_hook', export_settings, material, blender_material)
return material
# material = blender_primitive['material']
#

View File

@ -20,6 +20,7 @@ from io_scene_gltf2.blender.exp import gltf2_blender_gather_texture_info, gltf2_
from io_scene_gltf2.blender.exp import gltf2_blender_get
from io_scene_gltf2.blender.exp.gltf2_blender_gather_cache import cached
from io_scene_gltf2.io.com.gltf2_io_debug import print_console
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
@cached
@ -37,6 +38,8 @@ def gather_material_pbr_metallic_roughness(blender_material, orm_texture, export
roughness_factor=__gather_roughness_factor(blender_material, export_settings)
)
export_user_extensions('gather_material_pbr_metallic_roughness_hook', export_settings, material, blender_material, orm_texture)
return material

View File

@ -20,6 +20,7 @@ from io_scene_gltf2.io.com import gltf2_io
from io_scene_gltf2.blender.exp import gltf2_blender_gather_primitives
from ..com.gltf2_blender_extras import generate_extras
from io_scene_gltf2.io.com.gltf2_io_debug import print_console
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
@cached
@ -45,6 +46,17 @@ def gather_mesh(blender_mesh: bpy.types.Mesh,
if len(mesh.primitives) == 0:
print_console("WARNING", "Mesh '{}' has no primitives and will be omitted.".format(mesh.name))
return None
export_user_extensions('gather_mesh_hook',
export_settings,
mesh,
blender_mesh,
blender_object,
vertex_groups,
modifiers,
skip_filter,
material_names)
return mesh

View File

@ -28,6 +28,7 @@ from io_scene_gltf2.blender.exp import gltf2_blender_gather_lights
from ..com.gltf2_blender_extras import generate_extras
from io_scene_gltf2.io.com import gltf2_io
from io_scene_gltf2.io.com import gltf2_io_extensions
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
def gather_node(blender_object, blender_scene, export_settings):
@ -86,6 +87,8 @@ def __gather_node(blender_object, blender_scene, export_settings):
node.children.append(correction_node)
node.camera = None
export_user_extensions('gather_node_hook', export_settings, node, blender_object)
return node

View File

@ -15,6 +15,7 @@
import bpy
from io_scene_gltf2.io.com import gltf2_io
from io_scene_gltf2.blender.exp.gltf2_blender_gather_cache import cached
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
@cached
@ -22,7 +23,7 @@ def gather_sampler(blender_shader_node: bpy.types.Node, export_settings):
if not __filter_sampler(blender_shader_node, export_settings):
return None
return gltf2_io.Sampler(
sampler = gltf2_io.Sampler(
extensions=__gather_extensions(blender_shader_node, export_settings),
extras=__gather_extras(blender_shader_node, export_settings),
mag_filter=__gather_mag_filter(blender_shader_node, export_settings),
@ -32,6 +33,10 @@ def gather_sampler(blender_shader_node: bpy.types.Node, export_settings):
wrap_t=__gather_wrap_t(blender_shader_node, export_settings)
)
export_user_extensions('gather_sampler_hook', export_settings, sampler, blender_shader_node)
return sampler
def __filter_sampler(blender_shader_node, export_settings):
if not blender_shader_node.interpolation == 'Closest' and not blender_shader_node.extension == 'CLIP':

View File

@ -21,6 +21,7 @@ from io_scene_gltf2.io.com import gltf2_io_constants
from io_scene_gltf2.blender.exp import gltf2_blender_gather_accessors
from io_scene_gltf2.blender.exp import gltf2_blender_gather_joints
from io_scene_gltf2.blender.com import gltf2_blender_math
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
@cached
@ -35,7 +36,7 @@ def gather_skin(blender_object, export_settings):
if not __filter_skin(blender_object, export_settings):
return None
return gltf2_io.Skin(
skin = gltf2_io.Skin(
extensions=__gather_extensions(blender_object, export_settings),
extras=__gather_extras(blender_object, export_settings),
inverse_bind_matrices=__gather_inverse_bind_matrices(blender_object, export_settings),
@ -44,6 +45,10 @@ def gather_skin(blender_object, export_settings):
skeleton=__gather_skeleton(blender_object, export_settings)
)
export_user_extensions('gather_skin_hook', export_settings, skin, blender_object)
return skin
def __filter_skin(blender_object, export_settings):
if not export_settings[gltf2_blender_export_keys.SKINS]:

View File

@ -21,6 +21,7 @@ from io_scene_gltf2.blender.exp import gltf2_blender_gather_sampler
from io_scene_gltf2.blender.exp import gltf2_blender_search_node_tree
from io_scene_gltf2.blender.exp import gltf2_blender_gather_image
from io_scene_gltf2.io.com import gltf2_io_debug
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
@cached
@ -51,6 +52,8 @@ def gather_texture(
if texture.source is None:
return None
export_user_extensions('gather_texture_hook', export_settings, texture, blender_shader_sockets_or_texture_slots)
return texture

View File

@ -21,6 +21,7 @@ from io_scene_gltf2.blender.exp import gltf2_blender_search_node_tree
from io_scene_gltf2.blender.exp import gltf2_blender_get
from io_scene_gltf2.io.com.gltf2_io_debug import print_console
from io_scene_gltf2.io.com.gltf2_io_extensions import Extension
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
@cached
@ -40,6 +41,8 @@ def gather_texture_info(blender_shader_sockets_or_texture_slots: typing.Union[
if texture_info.index is None:
return None
export_user_extensions('gather_texture_info_hook', export_settings, texture_info, blender_shader_sockets_or_texture_slots)
return texture_info

View File

@ -0,0 +1,23 @@
# Copyright 2019 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.
def export_user_extensions(hook_name, export_settings, gltf2_object, *args):
if gltf2_object.extensions is None:
gltf2_object.extensions = {}
for extension in export_settings['gltf_user_extensions']:
hook = getattr(extension, hook_name, None)
if hook is not None:
hook(gltf2_object, *args, export_settings)