[Bf-extensions-cvs] [8dd0687a] master: glTF importer: refactoring animation import

Julien Duroure noreply at git.blender.org
Sat Apr 11 15:42:36 CEST 2020


Commit: 8dd0687a6796227c3846f94454dcd40358896c92
Author: Julien Duroure
Date:   Sat Apr 11 15:36:52 2020 +0200
Branches: master
https://developer.blender.org/rBA8dd0687a6796227c3846f94454dcd40358896c92

glTF importer: refactoring animation import

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

M	io_scene_gltf2/__init__.py
M	io_scene_gltf2/blender/imp/gltf2_blender_animation.py
D	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
M	io_scene_gltf2/blender/imp/gltf2_blender_animation_weight.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 ffcfeb7d..5317f0f8 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, 2, 61),
+    "version": (1, 2, 62),
     'blender': (2, 82, 7),
     '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 71f3ec00..f01e5ee9 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_animation.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_animation.py
@@ -14,48 +14,47 @@
 
 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
+from .gltf2_blender_animation_utils import simulate_stash, restore_animation_on_object
 from .gltf2_blender_vnode import VNode
 
 
 class BlenderAnimation():
-    """Dispatch Animation to bone or object animation."""
+    """Dispatch Animation to node or morph weights animation."""
     def __new__(cls, *args, **kwargs):
         raise RuntimeError("%s should not be instantiated" % cls)
 
     @staticmethod
-    def anim(gltf, anim_idx, vnode_id):
-        """Dispatch Animation to bone or object."""
-        if isinstance(vnode_id, int):
-            if gltf.vnodes[vnode_id].type == VNode.Bone:
-                BlenderBoneAnim.anim(gltf, anim_idx, vnode_id)
-            elif gltf.vnodes[vnode_id].type == VNode.Object:
+    def anim(gltf, anim_idx):
+        """Create actions/tracks for one animation."""
+        # Caches the action for each object (keyed by object name)
+        gltf.action_cache = {}
+        # Things we need to stash when we're done.
+        gltf.needs_stash = []
+
+        for vnode_id in gltf.vnodes:
+            if isinstance(vnode_id, int):
                 BlenderNodeAnim.anim(gltf, anim_idx, vnode_id)
+            BlenderWeightAnim.anim(gltf, anim_idx, vnode_id)
 
-        BlenderWeightAnim.anim(gltf, anim_idx, vnode_id)
-
-        for child in gltf.vnodes[vnode_id].children:
-            BlenderAnimation.anim(gltf, anim_idx, child)
+        # Push all actions onto NLA tracks with this animation's name
+        track_name = gltf.data.animations[anim_idx].track_name
+        for (obj, action) in gltf.needs_stash:
+            simulate_stash(obj, track_name, action)
 
     @staticmethod
-    def restore_animation(gltf, vnode_id, animation_name):
-        """Restores the actions for an animation by its track name on
-        the subtree starting at node_idx."""
-        vnode = gltf.vnodes[vnode_id]
-
-        obj = None
-        if vnode.type == VNode.Bone:
-            obj = gltf.vnodes[vnode.bone_arma].blender_object
-        elif vnode.type == VNode.Object:
-            obj = vnode.blender_object
-
-        if obj is not None:
+    def restore_animation(gltf, animation_name):
+        """Restores the actions for an animation by its track name."""
+        for vnode_id in gltf.vnodes:
+            vnode = gltf.vnodes[vnode_id]
+            if vnode.type == VNode.Bone:
+                obj = gltf.vnodes[vnode.bone_arma].blender_object
+            elif vnode.type == VNode.Object:
+                obj = vnode.blender_object
+            else:
+                continue
+
             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)
-
-        for child in gltf.vnodes[vnode_id].children:
-            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
deleted file mode 100755
index 6f65de43..00000000
--- a/io_scene_gltf2/blender/imp/gltf2_blender_animation_bone.py
+++ /dev/null
@@ -1,206 +0,0 @@
-# Copyright 2018-2019 The glTF-Blender-IO authors.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-import json
-import bpy
-from mathutils import Vector
-
-from ...io.imp.gltf2_io_binary import BinaryData
-from .gltf2_blender_animation_utils import simulate_stash, make_fcurve
-
-
-# In Blender we animate a pose bone. The final TRS of the bone depends on
-# both the edit bone and pose bone
-#
-#     Final = EditBone * PoseBone
-#   where
-#     Final =    Trans[ft] Rot[fr] Scale[fs]
-#     EditBone = Trans[et] Rot[er]                (edit bones have no scale)
-#     PoseBone = Trans[pt] Rot[pr] Scale[ps]
-#
-# Given Final we can solve for the PoseBone we need to use with
-#
-#     pt = Rot[er^{-1}] (ft - et)
-#     pr = er^{-1} fr
-#     ps = fs
-
-class BlenderBoneAnim():
-    """Blender Bone Animation."""
-    def __new__(cls, *args, **kwargs):
-        raise RuntimeError("%s should not be instantiated" % cls)
-
-    @staticmethod
-    def parse_translation_channel(gltf, vnode, obj, bone, channel, animation):
-        """Manage Location animation."""
-        blender_path = "pose.bones[" + json.dumps(bone.name) + "].location"
-        group_name = bone.name
-
-        keys = BinaryData.get_data_from_accessor(gltf, animation.samplers[channel.sampler].input)
-        values = BinaryData.get_data_from_accessor(gltf, animation.samplers[channel.sampler].output)
-
-        if animation.samplers[channel.sampler].interpolation == "CUBICSPLINE":
-            # TODO manage tangent?
-            translation_keyframes = (
-                gltf.loc_gltf_to_blender(values[idx * 3 + 1])
-                for idx in range(0, len(keys))
-            )
-        else:
-            translation_keyframes = (gltf.loc_gltf_to_blender(vals) for vals in values)
-
-        final_translations = vnode.base_locs_to_final_locs(translation_keyframes)
-
-        # Calculate pose bone trans from final bone trans
-        edit_trans, edit_rot = vnode.editbone_trans, vnode.editbone_rot
-        edit_rot_inv = edit_rot.conjugated()
-        pose_translations = [
-            edit_rot_inv @ (trans - edit_trans)
-            for trans in final_translations
-        ]
-
-        BlenderBoneAnim.fill_fcurves(
-            obj.animation_data.action,
-            keys,
-            pose_translations,
-            group_name,
-            blender_path,
-            animation.samplers[channel.sampler].interpolation
-        )
-
-    @staticmethod
-    def parse_rotation_channel(gltf, vnode, obj, bone, channel, animation):
-        """Manage rotation animation."""
-        blender_path = "pose.bones[" + json.dumps(bone.name) + "].rotation_quaternion"
-        group_name = bone.name
-
-        keys = BinaryData.get_data_from_accessor(gltf, animation.samplers[channel.sampler].input)
-        values = BinaryData.get_data_from_accessor(gltf, animation.samplers[channel.sampler].output)
-
-        if animation.samplers[channel.sampler].interpolation == "CUBICSPLINE":
-            # TODO manage tangent?
-            quat_keyframes = [
-                gltf.quaternion_gltf_to_blender(values[idx * 3 + 1])
-                for idx in range(0, len(keys))
-            ]
-        else:
-            quat_keyframes = [gltf.quaternion_gltf_to_blender(vals) for vals in values]
-
-        final_rots = vnode.base_rots_to_final_rots(quat_keyframes)
-
-        # Calculate pose bone rotation from final bone rotation
-        edit_rot = vnode.editbone_rot
-        edit_rot_inv = edit_rot.conjugated()
-        pose_rots = [
-            edit_rot_inv @ rot
-            for rot in final_rots
-        ]
-
-        # Manage antipodal quaternions
-        for i in range(1, len(pose_rots)):
-            if pose_rots[i].dot(pose_rots[i-1]) < 0:
-                pose_rots[i] = -pose_rots[i]
-
-        BlenderBoneAnim.fill_fcurves(
-            obj.animation_data.action,
-            keys,
-            pose_rots,
-            group_name,
-            blender_path,
-            animation.samplers[channel.sampler].interpolation
-        )
-
-    @staticmethod
-    def parse_scale_channel(gltf, vnode, obj, bone, channel, animation):
-        """Manage scaling animation."""
-        blender_path = "pose.bones[" + json.dumps(bone.name) + "].scale"
-        group_name = bone.name
-
-        keys = BinaryData.get_data_from_accessor(gltf, animation.samplers[channel.sampler].input)
-        values = BinaryData.get_data_from_accessor(gltf, animation.samplers[channel.sampler].output)
-
-        if animation.samplers[channel.sampler].interpolation == "CUBICSPLINE":
-            # TODO manage tangent?
-            scale_keyframes = [
-                gltf.scale_gltf_to_blender(values[idx * 3 + 1])
-                for idx in range(0, len(keys))
-            ]
-        else:
-            scale_keyframes = [gltf.scale_gltf_to_blender(vals) for vals in values]
-
-        final_scales = vnode.base_scales_to_final_scales(scale_keyframes)
-        pose_scales = final_scales  # no change needed
-
-        BlenderBoneAnim.fill_fcurves(
-            obj.animation_data.action,
-            keys,
-            pose_scales,
-            group_name,
-            blender_path,
-            animation.samplers[channel.sampler].interpolation
-        )
-
-    @staticmethod
-    def fill_fcurves(action, keys, values, group_name, blender_path, interpolation):
-        """Create FCurves from the keyframe-value pairs (one per component)."""
-        fps = bp

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list