[Bf-extensions-cvs] [18246268] master: glTF exporter: define a user extension API

Julien Duroure noreply at git.blender.org
Sat Dec 14 08:43:08 CET 2019


Commit: 18246268e802958bf02ffa14c764d2cc32ddb4d4
Author: Julien Duroure
Date:   Sat Dec 14 08:42:04 2019 +0100
Branches: master
https://developer.blender.org/rBA18246268e802958bf02ffa14c764d2cc32ddb4d4

glTF exporter: define a user extension API

===================================================================

M	io_scene_gltf2/__init__.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channel_target.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channels.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_animations.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_cameras.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_material_normal_texture_info_class.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_material_occlusion_texture_info_class.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_materials.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_materials_pbr_metallic_roughness.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_mesh.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_nodes.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_sampler.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_skins.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_texture.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py
A	io_scene_gltf2/io/exp/gltf2_io_user_extensions.py

===================================================================

diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py
index a875b9a6..7a3cd13a 100755
--- a/io_scene_gltf2/__init__.py
+++ b/io_scene_gltf2/__init__.py
@@ -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
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channel_target.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channel_target.py
index fa0f9976..5fcd4ede 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channel_target.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channel_target.py
@@ -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,
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channels.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channels.py
index d217b217..50918b68 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channels.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channels.py
@@ -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,
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py
index 5d5d310c..cd237f35 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py
@@ -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],
diff --git a/io_

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list