[Bf-extensions-cvs] [87ffe55f] master: glTF importer: better multi-object animation management

Julien Duroure noreply at git.blender.org
Tue Sep 17 22:18:58 CEST 2019


Commit: 87ffe55fd5f704fd981b678f94f3570268025955
Author: Julien Duroure
Date:   Tue Sep 17 22:18:19 2019 +0200
Branches: master
https://developer.blender.org/rBA87ffe55fd5f704fd981b678f94f3570268025955

glTF importer: better multi-object animation management

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

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_bone.py
M	io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py
M	io_scene_gltf2/blender/imp/gltf2_blender_animation_utils.py
A	io_scene_gltf2/blender/imp/gltf2_blender_animation_weight.py
M	io_scene_gltf2/blender/imp/gltf2_blender_gltf.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 6fa115e8..48b49ed5 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": (0, 9, 67),
+    "version": (0, 9, 68),
     'blender': (2, 81, 6),
     '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 93014d00..6394145d 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_animation.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_animation.py
@@ -12,8 +12,12 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import bpy
+
 from .gltf2_blender_animation_bone import BlenderBoneAnim
 from .gltf2_blender_animation_node import BlenderNodeAnim
+from .gltf2_blender_animation_weight import BlenderWeightAnim
+from .gltf2_blender_animation_utils import restore_animation_on_object
 
 
 class BlenderAnimation():
@@ -28,32 +32,28 @@ class BlenderAnimation():
             BlenderBoneAnim.anim(gltf, anim_idx, node_idx)
         else:
             BlenderNodeAnim.anim(gltf, anim_idx, node_idx)
+            BlenderWeightAnim.anim(gltf, anim_idx, node_idx)
 
         if gltf.data.nodes[node_idx].children:
             for child in gltf.data.nodes[node_idx].children:
                 BlenderAnimation.anim(gltf, anim_idx, child)
 
     @staticmethod
-    def stash_action(gltf, anim_idx, node_idx, action_name):
+    def restore_animation(gltf, node_idx, animation_name):
+        """Restores the actions for an animation by its track name on
+        the subtree starting at node_idx."""
+        node = gltf.data.nodes[node_idx]
 
-        if gltf.data.nodes[node_idx].is_joint:
-            BlenderBoneAnim.stash_action(gltf, anim_idx, node_idx, action_name)
+        if node.is_joint:
+            obj = bpy.data.objects[gltf.data.skins[node.skin_id].blender_armature_name]
         else:
-            BlenderNodeAnim.stash_action(gltf, anim_idx, node_idx, action_name)
-
-        if gltf.data.nodes[node_idx].children:
-            for child in gltf.data.nodes[node_idx].children:
-                BlenderAnimation.stash_action(gltf, anim_idx, child, action_name)
+            obj = bpy.data.objects[node.blender_object]
 
-    @staticmethod
-    def restore_last_action(gltf, node_idx):
-
-        if gltf.data.nodes[node_idx].is_joint:
-            BlenderBoneAnim.restore_last_action(gltf, node_idx)
-        else:
-            BlenderNodeAnim.restore_last_action(gltf, node_idx)
+        restore_animation_on_object(obj, animation_name)
+        if obj.data and hasattr(obj.data, 'shape_keys'):
+            restore_animation_on_object(obj.data.shape_keys, animation_name)
 
         if gltf.data.nodes[node_idx].children:
             for child in gltf.data.nodes[node_idx].children:
-                BlenderAnimation.restore_last_action(gltf, child)
+                BlenderAnimation.restore_animation(gltf, child, animation_name)
 
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_animation_bone.py b/io_scene_gltf2/blender/imp/gltf2_blender_animation_bone.py
index ef88bc37..74b907ae 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_animation_bone.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_animation_bone.py
@@ -18,7 +18,7 @@ from mathutils import Matrix
 
 from ..com.gltf2_blender_conversion import loc_gltf_to_blender, quaternion_gltf_to_blender, scale_to_matrix
 from ...io.imp.gltf2_io_binary import BinaryData
-from .gltf2_blender_animation_utils import simulate_stash, restore_last_action
+from .gltf2_blender_animation_utils import simulate_stash
 
 
 class BlenderBoneAnim():
@@ -40,31 +40,6 @@ class BlenderBoneAnim():
         else:
             kf.interpolation = 'LINEAR'
 
-    @staticmethod
-    def stash_action(gltf, anim_idx, node_idx, action_name):
-        node = gltf.data.nodes[node_idx]
-        obj = bpy.data.objects[gltf.data.skins[node.skin_id].blender_armature_name]
-
-        if anim_idx not in node.animations.keys():
-            return
-
-        if (obj.name, action_name) in gltf.actions_stashed.keys():
-            return
-
-        start_frame = bpy.context.scene.frame_start
-
-        animation_name = gltf.data.animations[anim_idx].name
-        simulate_stash(obj, animation_name, bpy.data.actions[action_name], start_frame)
-
-        gltf.actions_stashed[(obj.name, action_name)] = True
-
-    @staticmethod
-    def restore_last_action(gltf, node_idx):
-        node = gltf.data.nodes[node_idx]
-        obj = bpy.data.objects[gltf.data.skins[node.skin_id].blender_armature_name]
-
-        restore_last_action(obj)
-
     @staticmethod
     def parse_translation_channel(gltf, node, obj, bone, channel, animation):
         """Manage Location animation."""
@@ -239,7 +214,8 @@ class BlenderBoneAnim():
     def anim(gltf, anim_idx, node_idx):
         """Manage animation."""
         node = gltf.data.nodes[node_idx]
-        obj = bpy.data.objects[gltf.data.skins[node.skin_id].blender_armature_name]
+        blender_armature_name = gltf.data.skins[node.skin_id].blender_armature_name
+        obj = bpy.data.objects[blender_armature_name]
         bone = obj.pose.bones[node.blender_bone_name]
 
         if anim_idx not in node.animations.keys():
@@ -247,38 +223,16 @@ class BlenderBoneAnim():
 
         animation = gltf.data.animations[anim_idx]
 
-        if animation.name:
-            name = animation.name + "_" + obj.name
-        else:
-            name = "Animation_" + str(anim_idx) + "_" + obj.name
-        if len(name) >= 63:
-            # Name is too long to be kept, we are going to keep only animation name for now
-            name = animation.name
-            if len(name) >= 63:
-                # Very long name!
-                name = "Animation_" + str(anim_idx)
-        if name not in bpy.data.actions:
+        action = gltf.arma_cache.get(blender_armature_name)
+        if not action:
+            name = animation.track_name + "_" + obj.name
             action = bpy.data.actions.new(name)
-        else:
-            if name in gltf.animation_managed:
-                # multiple animation with same name in glTF file
-                # Create a new action with new name if needed
-                if name in gltf.current_animation_names.keys():
-                    action = bpy.data.actions[gltf.current_animation_names[name]]
-                    name = gltf.current_animation_names[name]
-                else:
-                    action = bpy.data.actions.new(name)
-            else:
-                action = bpy.data.actions[name]
-                # Check if this action has some users.
-                # If no user (only 1 indeed), that means that this action must be deleted
-                # (is an action from a deleted object)
-                if action.users == 1:
-                    bpy.data.actions.remove(action)
-                    action = bpy.data.actions.new(name)
+            gltf.needs_stash.append((obj, animation.track_name, action))
+            gltf.arma_cache[blender_armature_name] = action
+
         if not obj.animation_data:
             obj.animation_data_create()
-        obj.animation_data.action = bpy.data.actions[action.name]
+        obj.animation_data.action = action
 
         for channel_idx in node.animations[anim_idx]:
             channel = animation.channels[channel_idx]
@@ -292,6 +246,3 @@ class BlenderBoneAnim():
             elif channel.target.path == "scale":
                 BlenderBoneAnim.parse_scale_channel(gltf, node, obj, bone, channel, animation)
 
-        if action.name not in gltf.current_animation_names.keys():
-            gltf.current_animation_names[name] = action.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 cf570a20..0b020567 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py
@@ -18,7 +18,7 @@ from mathutils import Vector
 from ..com.gltf2_blender_conversion import loc_gltf_to_blender, quaternion_gltf_to_blender, scale_gltf_to_blender
 from ..com.gltf2_blender_conversion import correction_rotation
 from ...io.imp.gltf2_io_binary import BinaryData
-from .gltf2_blender_animation_utils import simulate_stash, restore_last_action
+from .gltf2_blender_animation_utils import simulate_stash
 
 
 class BlenderNodeAnim():
@@ -40,31 +40,6 @@ class BlenderNodeAnim():
         else:
             kf.interpolation = 'LINEAR'
 
-    @staticmethod
-    def stash_action(gltf, anim_idx, node_idx, action_name):
-        node = gltf.data.nodes[node_idx]
-        obj = bpy.data.objects[node.blender_object]
-
-        if anim_idx not in node.animations.keys():
-            return
-
-        if (obj.name, action_name) in gltf.actions_stashed.keys():
-            return
-
-        start_frame = bpy.context.scene.frame_start
-
-        animation_name = gltf.data.animations[anim_idx].name
-        simulate_stash(obj, animation_name, bpy.data.actions[action_name], start_frame)
-
-        gltf.actions_stashed[(obj.name, action_name)] = True
-
-    @staticmethod
-    def restore_last_action(gltf, node_idx):
-        node = gltf.data.nodes[node_idx]
-        obj = bpy.data.objects[node.blender_object]
-
-        restore_last_action(obj)
-
     @staticmethod
     def anim(gltf, anim_idx, node_idx):
         """Manage animation."""
@@ -72,31 +47,25 @@ class BlenderNodeAnim():
         obj = bpy.data.objects[node.blender_object]
         fps = bpy.context.scene.render.fps
 
+        animation = gltf.data.animations[anim_idx]
+
         if anim_idx not in node.animations.keys():
             return
 
-        animation = gltf.data.animations[anim_idx]
-
-        if animation.name:
-            name = animation.name + "_" + obj.name
+        for channel_idx in node.animations[anim_idx]:
+            channel 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list