[Bf-extensions-cvs] [a5205b02] master: glTF importer: Implement a user extension system

Julien Duroure noreply at git.blender.org
Thu Jan 20 19:20:25 CET 2022


Commit: a5205b0289717dc418c1a6070c89039204a2e951
Author: Julien Duroure
Date:   Thu Jan 20 19:05:09 2022 +0100
Branches: master
https://developer.blender.org/rBAa5205b0289717dc418c1a6070c89039204a2e951

glTF importer: Implement a user extension system

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

M	io_scene_gltf2/__init__.py
M	io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py
M	io_scene_gltf2/blender/imp/gltf2_blender_animation_weight.py
M	io_scene_gltf2/blender/imp/gltf2_blender_camera.py
M	io_scene_gltf2/blender/imp/gltf2_blender_image.py
M	io_scene_gltf2/blender/imp/gltf2_blender_light.py
M	io_scene_gltf2/blender/imp/gltf2_blender_material.py
M	io_scene_gltf2/blender/imp/gltf2_blender_mesh.py
M	io_scene_gltf2/blender/imp/gltf2_blender_node.py
M	io_scene_gltf2/blender/imp/gltf2_blender_scene.py
M	io_scene_gltf2/blender/imp/gltf2_blender_texture.py
M	io_scene_gltf2/io/imp/gltf2_io_gltf.py
A	io_scene_gltf2/io/imp/gltf2_io_user_extensions.py

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

diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py
index 97ef843b..1a97bee8 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, Scurest, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors',
-    "version": (1, 8, 12),
+    "version": (1, 8, 13),
     'blender': (3, 1, 0),
     'location': 'File > Import-Export',
     'description': 'Import-Export as glTF 2.0',
@@ -67,7 +67,8 @@ from bpy_extras.io_utils import ImportHelper, ExportHelper
 #  Functions / Classes.
 #
 
-extension_panel_unregister_functors = []
+exporter_extension_panel_unregister_functors = []
+importer_extension_panel_unregister_functors = []
 
 
 def ensure_filepath_matches_export_format(filepath, export_format):
@@ -479,11 +480,11 @@ class ExportGLTF2_Base:
         for addon_name in preferences.addons.keys():
             try:
                 if hasattr(sys.modules[addon_name], 'glTF2ExportUserExtension') or hasattr(sys.modules[addon_name], 'glTF2ExportUserExtensions'):
-                    extension_panel_unregister_functors.append(sys.modules[addon_name].register_panel())
+                    exporter_extension_panel_unregister_functors.append(sys.modules[addon_name].register_panel())
             except Exception:
                 pass
 
-        self.has_active_extensions = len(extension_panel_unregister_functors) > 0
+        self.has_active_exporter_extensions = len(exporter_extension_panel_unregister_functors) > 0
         return ExportHelper.invoke(self, context, event)
 
     def save_settings(self, context):
@@ -945,7 +946,7 @@ class GLTF_PT_export_animation_skinning(bpy.types.Panel):
 class GLTF_PT_export_user_extensions(bpy.types.Panel):
     bl_space_type = 'FILE_BROWSER'
     bl_region_type = 'TOOL_PROPS'
-    bl_label = "Extensions"
+    bl_label = "Exporter Extensions"
     bl_parent_id = "FILE_PT_operator"
     bl_options = {'DEFAULT_CLOSED'}
 
@@ -954,13 +955,30 @@ class GLTF_PT_export_user_extensions(bpy.types.Panel):
         sfile = context.space_data
         operator = sfile.active_operator
 
-        return operator.bl_idname == "EXPORT_SCENE_OT_gltf" and operator.has_active_extensions
+        return operator.bl_idname == "EXPORT_SCENE_OT_gltf" and operator.has_active_exporter_extensions
 
     def draw(self, context):
         layout = self.layout
         layout.use_property_split = True
         layout.use_property_decorate = False  # No animation.
 
+class GLTF_PT_import_user_extensions(bpy.types.Panel):
+    bl_space_type = 'FILE_BROWSER'
+    bl_region_type = 'TOOL_PROPS'
+    bl_label = "Importer 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 == "IMPORT_SCENE_OT_gltf" and operator.has_active_importer_extensions
+
+    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"""
@@ -1060,6 +1078,19 @@ class ImportGLTF2(Operator, ImportHelper):
         layout.prop(self, 'guess_original_bind_pose')
         layout.prop(self, 'bone_heuristic')
 
+    def invoke(self, context, event):
+        import sys
+        preferences = bpy.context.preferences
+        for addon_name in preferences.addons.keys():
+            try:
+                if hasattr(sys.modules[addon_name], 'glTF2ImportUserExtension') or hasattr(sys.modules[addon_name], 'glTF2ImportUserExtensions'):
+                    importer_extension_panel_unregister_functors.append(sys.modules[addon_name].register_panel())
+            except Exception:
+                pass
+
+        self.has_active_importer_extensions = len(importer_extension_panel_unregister_functors) > 0
+        return ImportHelper.invoke(self, context, event)
+
     def execute(self, context):
         return self.import_gltf2(context)
 
@@ -1069,6 +1100,20 @@ class ImportGLTF2(Operator, ImportHelper):
         self.set_debug_log()
         import_settings = self.as_keywords()
 
+        user_extensions = []
+
+        import sys
+        preferences = bpy.context.preferences
+        for addon_name in preferences.addons.keys():
+            try:
+                module = sys.modules[addon_name]
+            except Exception:
+                continue
+            if hasattr(module, 'glTF2ImportUserExtension'):
+                extension_ctor = module.glTF2ImportUserExtension
+                user_extensions.append(extension_ctor())
+        import_settings['import_user_extensions'] = user_extensions
+
         if self.files:
             # Multiple file import
             ret = {'CANCELLED'}
@@ -1137,7 +1182,8 @@ classes = (
     GLTF_PT_export_animation_shapekeys,
     GLTF_PT_export_animation_skinning,
     GLTF_PT_export_user_extensions,
-    ImportGLTF2
+    ImportGLTF2,
+    GLTF_PT_import_user_extensions
 )
 
 
@@ -1154,9 +1200,13 @@ def register():
 def unregister():
     for c in classes:
         bpy.utils.unregister_class(c)
-    for f in extension_panel_unregister_functors:
+    for f in exporter_extension_panel_unregister_functors:
+        f()
+    exporter_extension_panel_unregister_functors.clear()
+
+    for f in importer_extension_panel_unregister_functors:
         f()
-    extension_panel_unregister_functors.clear()
+    importer_extension_panel_unregister_functors.clear()
 
     # bpy.utils.unregister_module(__name__)
 
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py b/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py
index 642515aa..03f8592c 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py
@@ -18,6 +18,7 @@ from mathutils import Vector
 from ...io.imp.gltf2_io_binary import BinaryData
 from .gltf2_blender_animation_utils import make_fcurve
 from .gltf2_blender_vnode import VNode
+from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions
 
 
 class BlenderNodeAnim():
@@ -46,6 +47,8 @@ class BlenderNodeAnim():
         vnode = gltf.vnodes[node_idx]
         path = channel.target.path
 
+        import_user_extensions('gather_import_animation_channel_before_hook', gltf, animation, vnode, path, channel)
+
         action = BlenderNodeAnim.get_or_create_action(gltf, node_idx, animation.track_name)
 
         keys = BinaryData.get_data_from_accessor(gltf, animation.samplers[channel.sampler].input)
@@ -152,6 +155,8 @@ class BlenderNodeAnim():
                 interpolation=animation.samplers[channel.sampler].interpolation,
             )
 
+        import_user_extensions('gather_import_animation_channel_after_hook', gltf, animation, vnode, path, channel, action)
+
     @staticmethod
     def get_or_create_action(gltf, node_idx, anim_name):
         vnode = gltf.vnodes[node_idx]
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_animation_weight.py b/io_scene_gltf2/blender/imp/gltf2_blender_animation_weight.py
index 19723ed9..3b864d6b 100644
--- a/io_scene_gltf2/blender/imp/gltf2_blender_animation_weight.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_animation_weight.py
@@ -16,6 +16,7 @@ import bpy
 
 from ...io.imp.gltf2_io_binary import BinaryData
 from .gltf2_blender_animation_utils import make_fcurve
+from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions
 
 
 class BlenderWeightAnim():
@@ -29,6 +30,9 @@ class BlenderWeightAnim():
         vnode = gltf.vnodes[vnode_id]
 
         node_idx = vnode.mesh_node_idx
+
+        import_user_extensions('gather_import_animation_weight_before_hook', gltf, vnode, gltf.data.animations[anim_idx])
+
         if node_idx is None:
             return
 
@@ -90,3 +94,5 @@ class BlenderWeightAnim():
                 max_weight = max(coords[1:2])
                 if min_weight < kb.slider_min: kb.slider_min = min_weight
                 if max_weight > kb.slider_max: kb.slider_max = max_weight
+
+        import_user_extensions('gather_import_animation_weight_after_hook', gltf, vnode, animation)
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_camera.py b/io_scene_gltf2/blender/imp/gltf2_blender_camera.py
index cc73a690..e5f6e3d1 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_camera.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_camera.py
@@ -14,6 +14,7 @@
 
 import bpy
 from ..com.gltf2_blender_extras import set_extras
+from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions
 
 
 class BlenderCamera():
@@ -22,10 +23,12 @@ class BlenderCamera():
         raise RuntimeError("%s should not be instantiated" % cls)
 
     @staticmethod
-    def create(gltf, camera_id):
+    def create(gltf, vnode, camera_id):
         """Camera creation."""
         pycamera = gltf.data.cameras[camera_id]
 
+        import_user_extensions('gather_import_camera_before_hook', gltf, vnode, pycamera)
+
         if not pycamera.name:
             pycamera.name = "Camera"
 
@@ -55,5 +58,4 @@ class BlenderCamera():
                 # Infinite projection
                 cam.clip_end = 1e12  # some big number
 
-
         return cam
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_image.py b/io_scene_gltf2/blender/imp/gltf2_blender_image.py
index 3acf1786..0df6dc0e 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_image.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_image.py
@@ -20,6 +20,7 @@ import urllib.parse
 import re
 
 from ...io.imp.gltf2_io_binary import BinaryData
+from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions
 
 
 # Note that Image is not a glTF2.0 object
@@ -32,6 +33,9 @@ class BlenderImage():
     def create(gltf, img_idx):
         """Image creation."""
         img = gltf.data.images[img_idx]
+
+        import_user_extensions('gather_import_image_before_hook', gltf, img)
+
         img_name = img.name
 
         if img.bl

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list