Skip to content

Commit

Permalink
Support user extensions
Browse files Browse the repository at this point in the history
The extension plugin should receive a dictionary with the properties
  • Loading branch information
Juan Jose Casafranca committed Dec 11, 2019
1 parent a041801 commit 59dc4b2
Show file tree
Hide file tree
Showing 19 changed files with 284 additions and 7 deletions.
49 changes: 49 additions & 0 deletions addons/io_scene_gltf2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def reload_package_recursive(current_dir, module_dict):
# Functions / Classes.
#

extension_panel_unregister_functors = []

class ExportGLTF2_Base:
# TODO: refactor to avoid boilerplate
Expand Down Expand Up @@ -326,6 +327,7 @@ def __init__(self):
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():
Expand All @@ -336,6 +338,18 @@ def invoke(self, context, event):
self.report({"ERROR"}, "Loading export settings failed. Removed corrupted settings")
del context.scene[self.scene_key]

if bpy.app.version < (2,80,0):
pass
else:
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):
Expand Down Expand Up @@ -429,6 +443,18 @@ def execute(self, context):
export_settings['gltf_binaryfilename'] = os.path.splitext(os.path.basename(
bpy.path.ensure_ext(self.filepath,self.filename_ext)))[0] + '.bin'

user_extensions = []

if bpy.app.version < (2,80,0):
pass
else:
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)
if bpy.app.version < (2, 80, 0):
def draw(self, context):
Expand Down Expand Up @@ -788,6 +814,25 @@ def draw(self, context):
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"""
Expand Down Expand Up @@ -919,6 +964,7 @@ def menu_func_import(self, context):
GLTF_PT_export_animation_export,
GLTF_PT_export_animation_shapekeys,
GLTF_PT_export_animation_skinning,
GLTF_PT_export_user_extensions,
ImportGLTF2
)

Expand All @@ -940,6 +986,9 @@ def register():
def unregister():
for c in classes:
bpy.utils.unregister_class(c)
for f in extension_panel_unregister_functors:
f()

# bpy.utils.unregister_module(__name__)

# remove from the export / import menu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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],
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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
Expand Down Expand Up @@ -136,13 +137,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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
Expand Down Expand Up @@ -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,
Expand All @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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,
Expand Down Expand Up @@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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
Expand All @@ -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),
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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
Expand All @@ -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),
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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
Expand All @@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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_extensions import Extension
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions


@cached
Expand All @@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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
Expand Down Expand Up @@ -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']
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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
Expand All @@ -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


Expand Down
12 changes: 12 additions & 0 deletions addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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
Expand All @@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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):
Expand Down Expand Up @@ -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


Expand Down
Loading

0 comments on commit 59dc4b2

Please sign in to comment.