From 295984b200890cf6d5bdea6af5a3bd3d4260b765 Mon Sep 17 00:00:00 2001 From: Julien Duroure Date: Sun, 26 May 2019 22:34:29 +0200 Subject: glTF exporter: fix animation when a property is single keyframed (not all property index) --- .../exp/gltf2_blender_gather_animation_samplers.py | 70 +++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) (limited to 'io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py') 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 827ec91e..cdd4be7b 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 @@ -34,17 +34,81 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve], blender_object: bpy.types.Object, export_settings ) -> gltf2_io.AnimationSampler: + blender_object_if_armature = blender_object if blender_object.type == "ARMATURE" else None + if blender_object_if_armature is not None: + pose_bone_if_armature = gltf2_blender_get.get_object_from_datapath(blender_object_if_armature, + channels[0].data_path) + else: + pose_bone_if_armature = None + non_keyed_values = __gather_non_keyed_values(channels, blender_object, + blender_object_if_armature, pose_bone_if_armature, + export_settings) + + return gltf2_io.AnimationSampler( extensions=__gather_extensions(channels, blender_object_if_armature, export_settings), extras=__gather_extras(channels, blender_object_if_armature, export_settings), - input=__gather_input(channels, blender_object_if_armature, export_settings), + input=__gather_input(channels, blender_object_if_armature, non_keyed_values, export_settings), interpolation=__gather_interpolation(channels, blender_object_if_armature, export_settings), output=__gather_output(channels, blender_object.matrix_parent_inverse.copy().freeze(), blender_object_if_armature, + non_keyed_values, export_settings) ) +def __gather_non_keyed_values(channels: typing.Tuple[bpy.types.FCurve], + blender_object: bpy.types.Object, + blender_object_if_armature: typing.Optional[bpy.types.Object], + pose_bone_if_armature: typing.Optional[bpy.types.PoseBone], + export_settings + ) -> typing.Tuple[typing.Optional[float]]: + + non_keyed_values = [] + + target = channels[0].data_path.split('.')[-1] + if target == "value": + return () + + indices = [c.array_index for c in channels] + indices.sort() + length = { + "delta_location": 3, + "delta_rotation_euler": 3, + "location": 3, + "rotation_axis_angle": 4, + "rotation_euler": 3, + "rotation_quaternion": 4, + "scale": 3, + "value": 1 + }.get(target) + + for i in range(0, length): + if i in indices: + non_keyed_values.append(None) + else: + if blender_object_if_armature is None: + non_keyed_values.append({ + "delta_location" : blender_object.delta_location, + "delta_rotation_euler" : blender_object.delta_rotation_euler, + "location" : blender_object.location, + "rotation_axis_angle" : blender_object.rotation_axis_angle, + "rotation_euler" : blender_object.rotation_euler, + "rotation_quaternion" : blender_object.rotation_quaternion, + "scale" : blender_object.scale + }[target][i]) + else: + # TODO, this is not working if the action is not active (NLA case for example) + trans, rot, scale = pose_bone_if_armature.matrix_basis.decompose() + non_keyed_values.append({ + "location": trans, + "rotation_axis_angle": rot, + "rotation_euler": rot, + "rotation_quaternion": rot, + "scale": scale + }[target][i]) + + return tuple(non_keyed_values) def __gather_extensions(channels: typing.Tuple[bpy.types.FCurve], blender_object_if_armature: typing.Optional[bpy.types.Object], @@ -63,11 +127,13 @@ def __gather_extras(channels: typing.Tuple[bpy.types.FCurve], @cached def __gather_input(channels: typing.Tuple[bpy.types.FCurve], blender_object_if_armature: typing.Optional[bpy.types.Object], + non_keyed_values: typing.Tuple[typing.Optional[float]], export_settings ) -> gltf2_io.Accessor: """Gather the key time codes.""" keyframes = gltf2_blender_gather_animation_sampler_keyframes.gather_keyframes(blender_object_if_armature, channels, + non_keyed_values, export_settings) times = [k.seconds for k in keyframes] @@ -107,11 +173,13 @@ def __gather_interpolation(channels: typing.Tuple[bpy.types.FCurve], def __gather_output(channels: typing.Tuple[bpy.types.FCurve], parent_inverse, blender_object_if_armature: typing.Optional[bpy.types.Object], + non_keyed_values: typing.Tuple[typing.Optional[float]], export_settings ) -> gltf2_io.Accessor: """Gather the data of the keyframes.""" keyframes = gltf2_blender_gather_animation_sampler_keyframes.gather_keyframes(blender_object_if_armature, channels, + non_keyed_values, export_settings) target_datapath = channels[0].data_path -- cgit v1.2.3