[Bf-extensions-cvs] [2183b1d2] blender-v3.2-release: glTF importer: more animation hooks for user extensions

Julien Duroure noreply at git.blender.org
Wed May 25 08:45:22 CEST 2022


Commit: 2183b1d29571dad1b8d2267f8e777a6600ecff33
Author: Julien Duroure
Date:   Wed May 25 08:45:03 2022 +0200
Branches: blender-v3.2-release
https://developer.blender.org/rBA2183b1d29571dad1b8d2267f8e777a6600ecff33

glTF importer: more animation hooks for user extensions

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

M	io_scene_gltf2/__init__.py
M	io_scene_gltf2/blender/imp/gltf2_blender_animation.py
M	io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py
M	io_scene_gltf2/blender/imp/gltf2_blender_mesh.py
M	io_scene_gltf2/blender/imp/gltf2_blender_scene.py

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

diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py
index cc102ffe..5e3dce0f 100755
--- a/io_scene_gltf2/__init__.py
+++ b/io_scene_gltf2/__init__.py
@@ -4,7 +4,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": (3, 2, 38),
+    "version": (3, 2, 39),
     'blender': (3, 1, 0),
     'location': 'File > Import-Export',
     'description': 'Import-Export as glTF 2.0',
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_animation.py b/io_scene_gltf2/blender/imp/gltf2_blender_animation.py
index c0dcd84e..76fe02c5 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_animation.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_animation.py
@@ -5,7 +5,7 @@ from .gltf2_blender_animation_node import BlenderNodeAnim
 from .gltf2_blender_animation_weight import BlenderWeightAnim
 from .gltf2_blender_animation_utils import simulate_stash, restore_animation_on_object
 from .gltf2_blender_vnode import VNode
-
+from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions
 
 class BlenderAnimation():
     """Dispatch Animation to node or morph weights animation."""
@@ -20,6 +20,8 @@ class BlenderAnimation():
         # Things we need to stash when we're done.
         gltf.needs_stash = []
 
+        import_user_extensions('gather_import_animation_before_hook', gltf, anim_idx)
+
         for vnode_id in gltf.vnodes:
             if isinstance(vnode_id, int):
                 BlenderNodeAnim.anim(gltf, anim_idx, vnode_id)
@@ -30,6 +32,8 @@ class BlenderAnimation():
         for (obj, action) in gltf.needs_stash:
             simulate_stash(obj, track_name, action)
 
+        import_user_extensions('gather_import_animation_after_hook', gltf, anim_idx, track_name)
+
     @staticmethod
     def restore_animation(gltf, animation_name):
         """Restores the actions for an animation by its track 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 acc7767f..ab2f496c 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py
@@ -20,6 +20,7 @@ class BlenderNodeAnim():
         """Manage animation targeting a node's TRS."""
         animation = gltf.data.animations[anim_idx]
         node = gltf.data.nodes[node_idx]
+
         if anim_idx not in node.animations.keys():
             return
 
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py b/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py
index 384cae7d..a3c1bd34 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py
@@ -58,6 +58,16 @@ def do_primitives(gltf, mesh_idx, skin_idx, mesh, ob):
     """Put all primitive data into the mesh."""
     pymesh = gltf.data.meshes[mesh_idx]
 
+    # Use a class here, to be able to pass data by reference to hook (to be able to change them inside hook)
+    class IMPORT_mesh_options:
+        def __init__(self, skinning: bool = True, skin_into_bind_pose: bool = True, use_auto_smooth: bool = True):
+            self.skinning = skinning
+            self.skin_into_bind_pose = skin_into_bind_pose
+            self.use_auto_smooth = use_auto_smooth
+
+    mesh_options = IMPORT_mesh_options()
+    import_user_extensions('gather_import_mesh_options', gltf, mesh_options, pymesh, skin_idx)
+
     # Scan the primitives to find out what we need to create
 
     has_normals = False
@@ -135,6 +145,8 @@ def do_primitives(gltf, mesh_idx, skin_idx, mesh, ob):
             print_console('INFO', 'Draco Decoder: Decode primitive {}'.format(pymesh.name or '[unnamed]'))
             decode_primitive(gltf, prim)
 
+        import_user_extensions('gather_import_decode_primitive', gltf, pymesh, prim, skin_idx)
+
         if prim.indices is not None:
             indices = BinaryData.decode_accessor(gltf, prim.indices)
             indices = indices.reshape(len(indices))
@@ -240,7 +252,7 @@ def do_primitives(gltf, mesh_idx, skin_idx, mesh, ob):
     for sk_locs in sk_vert_locs:
         gltf.locs_batch_gltf_to_blender(sk_locs)
 
-    if num_joint_sets:
+    if num_joint_sets and mesh_options.skin_into_bind_pose:
         skin_into_bind_pose(
             gltf, skin_idx, vert_joints, vert_weights,
             locs=[vert_locs] + sk_vert_locs,
@@ -286,14 +298,14 @@ def do_primitives(gltf, mesh_idx, skin_idx, mesh, ob):
 
         if layer is None:
             print("WARNING: Vertex colors are ignored because the maximum number of vertex color layers has been "
-                  "reached.")
+                "reached.")
             break
 
         mesh.color_attributes[layer.name].data.foreach_set('color', squish(loop_cols[col_i]))
 
     # Skinning
     # TODO: this is slow :/
-    if num_joint_sets:
+    if num_joint_sets and mesh_options.skinning:
         pyskin = gltf.data.skins[skin_idx]
         for i, node_idx in enumerate(pyskin.joints):
             bone = gltf.vnodes[node_idx]
@@ -340,7 +352,7 @@ def do_primitives(gltf, mesh_idx, skin_idx, mesh, ob):
             if prim.material is not None:
                 # Get the material
                 pymaterial = gltf.data.materials[prim.material]
-                vertex_color = 'COLOR_0' if 'COLOR_0' in prim.attributes else None
+                vertex_color = 'COLOR_0' if ('COLOR_0' in prim.attributes) else None
                 if vertex_color not in pymaterial.blender_material:
                     BlenderMaterial.create(gltf, prim.material, vertex_color)
                 material_name = pymaterial.blender_material[vertex_color]
@@ -374,7 +386,7 @@ def do_primitives(gltf, mesh_idx, skin_idx, mesh, ob):
     if has_normals:
         mesh.create_normals_split()
         mesh.normals_split_custom_set_from_vertices(vert_normals)
-        mesh.use_auto_smooth = True
+        mesh.use_auto_smooth = mesh_options.use_auto_smooth
 
 
 def points_edges_tris(mode, indices):
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_scene.py b/io_scene_gltf2/blender/imp/gltf2_blender_scene.py
index 672b68c8..d853aded 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_scene.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_scene.py
@@ -57,6 +57,15 @@ class BlenderScene():
     @staticmethod
     def create_animations(gltf):
         """Create animations."""
+
+        # Use a class here, to be able to pass data by reference to hook (to be able to change them inside hook)
+        class IMPORT_animation_options:
+            def __init__(self, restore_first_anim: bool = True):
+                self.restore_first_anim = restore_first_anim
+
+        animation_options = IMPORT_animation_options()
+        import_user_extensions('gather_import_animations', gltf, gltf.data.animations, animation_options)
+
         if gltf.data.animations:
             # NLA tracks are added bottom to top, so create animations in
             # reverse so the first winds up on top
@@ -64,8 +73,9 @@ class BlenderScene():
                 BlenderAnimation.anim(gltf, anim_idx)
 
             # Restore first animation
-            anim_name = gltf.data.animations[0].track_name
-            BlenderAnimation.restore_animation(gltf, anim_name)
+            if animation_options.restore_first_anim:
+                anim_name = gltf.data.animations[0].track_name
+                BlenderAnimation.restore_animation(gltf, anim_name)
 
     @staticmethod
     def select_imported_objects(gltf):



More information about the Bf-extensions-cvs mailing list