From 1757ec91e58a23c6dbd31b763267e1d3f5e40026 Mon Sep 17 00:00:00 2001 From: Julien Duroure Date: Mon, 23 Aug 2021 19:16:18 +0200 Subject: glTF exporter: remove some channel animation if bone is not animated Check is done after sampling: if animated is constant and bone has no fcurve, we remove the channel --- io_scene_gltf2/__init__.py | 2 +- .../exp/gltf2_blender_gather_animation_channels.py | 44 ++++++++++++++++++---- ...2_blender_gather_animation_sampler_keyframes.py | 17 +++++++++ .../exp/gltf2_blender_gather_animation_samplers.py | 19 +++++++++- 4 files changed, 71 insertions(+), 11 deletions(-) diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py index 33af1111..0abcb811 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, 7, 21), + "version": (1, 7, 22), 'blender': (2, 91, 0), 'location': 'File > Import-Export', 'description': 'Import-Export as glTF 2.0', 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 114ff901..6c184198 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 @@ -69,6 +69,11 @@ def gather_animation_channels(blender_action: bpy.types.Action, bones_to_be_animated, _, _ = gltf2_blender_gather_skins.get_bone_tree(None, blender_object) bones_to_be_animated = [blender_object.pose.bones[b.name] for b in bones_to_be_animated] + list_of_animated_bone_channels = [] + for channel_group in __get_channel_groups(blender_action, blender_object, export_settings): + channel_group_sorted = __get_channel_group_sorted(channel_group, blender_object) + list_of_animated_bone_channels.extend([(gltf2_blender_get.get_object_from_datapath(blender_object, get_target_object_path(i.data_path)).name, get_target_property_name(i.data_path)) for i in channel_group]) + for bone in bones_to_be_animated: for p in ["location", "rotation_quaternion", "scale"]: channel = __gather_animation_channel( @@ -80,8 +85,10 @@ def gather_animation_channels(blender_action: bpy.types.Action, bake_range_start, bake_range_end, blender_action.name, - None) - channels.append(channel) + None, + (bone.name, p) in list_of_animated_bone_channels) + if channel is not None: + channels.append(channel) # Retrieve animation on armature object itself, if any @@ -91,7 +98,7 @@ def gather_animation_channels(blender_action: bpy.types.Action, if len(channel_group) == 0: # Only errors on channels, ignoring continue - channel = __gather_animation_channel(channel_group, blender_object, export_settings, None, None, bake_range_start, bake_range_end, blender_action.name, None) + channel = __gather_animation_channel(channel_group, blender_object, export_settings, None, None, bake_range_start, bake_range_end, blender_action.name, None, False) if channel is not None: channels.append(channel) @@ -109,7 +116,8 @@ def gather_animation_channels(blender_action: bpy.types.Action, bake_range_start, bake_range_end, blender_action.name, - obj) + obj, + False) channels.append(channel) else: @@ -118,7 +126,17 @@ def gather_animation_channels(blender_action: bpy.types.Action, if len(channel_group_sorted) == 0: # Only errors on channels, ignoring continue - channel = __gather_animation_channel(channel_group_sorted, blender_object, export_settings, None, None, bake_range_start, bake_range_end, blender_action.name, None) + channel = __gather_animation_channel( + channel_group_sorted, + blender_object, + export_settings, + None, + None, + bake_range_start, + bake_range_end, + blender_action.name, + None, + False) if channel is not None: channels.append(channel) @@ -189,17 +207,25 @@ def __gather_animation_channel(channels: typing.Tuple[bpy.types.FCurve], bake_range_start, bake_range_end, action_name: str, - driver_obj + driver_obj, + node_channel_is_animated: bool ) -> typing.Union[gltf2_io.AnimationChannel, None]: if not __filter_animation_channel(channels, blender_object, export_settings): return None __target= __gather_target(channels, blender_object, export_settings, bake_bone, bake_channel, driver_obj) if __target.path is not None: + sampler = __gather_sampler(channels, blender_object, export_settings, bake_bone, bake_channel, bake_range_start, bake_range_end, action_name, driver_obj, node_channel_is_animated) + + if sampler is None: + # After check, no need to animate this node for this channel + return None + + 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, driver_obj), + sampler=sampler, target=__target ) @@ -249,7 +275,8 @@ def __gather_sampler(channels: typing.Tuple[bpy.types.FCurve], bake_range_start, bake_range_end, action_name, - driver_obj + driver_obj, + node_channel_is_animated: bool ) -> gltf2_io.AnimationSampler: return gltf2_blender_gather_animation_samplers.gather_animation_sampler( channels, @@ -260,6 +287,7 @@ def __gather_sampler(channels: typing.Tuple[bpy.types.FCurve], bake_range_end, action_name, driver_obj, + node_channel_is_animated, export_settings ) diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_sampler_keyframes.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_sampler_keyframes.py index 3bf5b97c..932ea25e 100755 --- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_sampler_keyframes.py +++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_sampler_keyframes.py @@ -22,6 +22,7 @@ from io_scene_gltf2.blender.exp import gltf2_blender_get from io_scene_gltf2.blender.exp.gltf2_blender_gather_drivers import get_sk_drivers, get_sk_driver_values from . import gltf2_blender_export_keys from io_scene_gltf2.io.com import gltf2_io_debug +import numpy as np class Keyframe: @@ -193,6 +194,7 @@ def gather_keyframes(blender_object_if_armature: typing.Optional[bpy.types.Objec bake_range_end, action_name: str, driver_obj, + node_channel_is_animated: bool, export_settings ) -> typing.List[Keyframe]: """Convert the blender action groups' fcurves to keyframes for use in glTF.""" @@ -309,6 +311,21 @@ def gather_keyframes(blender_object_if_armature: typing.Optional[bpy.types.Objec keyframes.append(key) + # For armature only + # Check if all values are the same + # In that case, if there is no real keyframe on this channel for this given bone, + # We can ignore this keyframes + # if there are some fcurve, we can keep only 2 keyframes, first and last + if blender_object_if_armature is not None: + std = np.ptp(np.ptp([[k.value[i] for i in range(len(keyframes[0].value))] for k in keyframes], axis=0)) + + if node_channel_is_animated is True: # fcurve on this bone for this property + # Keep animation, but keep only 2 keyframes if data are not changing + return [keyframes[0], keyframes[-1]] if std < 0.0001 and len(keyframes) >= 2 else keyframes + else: # bone is not animated (no fcurve) + # Not keeping if not changing property + return None if std < 0.0001 else keyframes + return keyframes 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 d555bcb9..d3b9e022 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 @@ -39,6 +39,7 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve], bake_range_end, action_name: str, driver_obj, + node_channel_is_animated: bool, export_settings ) -> gltf2_io.AnimationSampler: @@ -61,11 +62,17 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve], else: matrix_parent_inverse = mathutils.Matrix.Identity(4).freeze() + input = __gather_input(channels, blender_object_if_armature, non_keyed_values, + bake_bone, bake_channel, bake_range_start, bake_range_end, action_name, driver_obj, node_channel_is_animated, export_settings) + + if input is None: + # After check, no need to animate this node for this channel + return None + 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, - bake_bone, bake_channel, bake_range_start, bake_range_end, action_name, driver_obj, export_settings), + input=input, interpolation=__gather_interpolation(channels, blender_object_if_armature, export_settings, bake_bone, bake_channel), output=__gather_output(channels, matrix_parent_inverse, @@ -77,6 +84,7 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve], bake_range_end, action_name, driver_obj, + node_channel_is_animated, export_settings) ) @@ -229,6 +237,7 @@ def __gather_input(channels: typing.Tuple[bpy.types.FCurve], bake_range_end, action_name, driver_obj, + node_channel_is_animated: bool, export_settings ) -> gltf2_io.Accessor: """Gather the key time codes.""" @@ -241,7 +250,11 @@ def __gather_input(channels: typing.Tuple[bpy.types.FCurve], bake_range_end, action_name, driver_obj, + node_channel_is_animated, export_settings) + if keyframes is None: + # After check, no need to animation this node + return None times = [k.seconds for k in keyframes] return gltf2_blender_gather_accessors.gather_accessor( @@ -306,6 +319,7 @@ def __gather_output(channels: typing.Tuple[bpy.types.FCurve], bake_range_end, action_name, driver_obj, + node_channel_is_animated: bool, export_settings ) -> gltf2_io.Accessor: """Gather the data of the keyframes.""" @@ -318,6 +332,7 @@ def __gather_output(channels: typing.Tuple[bpy.types.FCurve], bake_range_end, action_name, driver_obj, + node_channel_is_animated, export_settings) if bake_bone is not None: target_datapath = "pose.bones['" + bake_bone + "']." + bake_channel -- cgit v1.2.3