From e635a494408a6c7a826c2e6e83e487e4bb01278d Mon Sep 17 00:00:00 2001 From: Zdenek Dolezal Date: Mon, 13 Feb 2023 21:12:46 +0100 Subject: [PATCH] Bake operators fix By using bpy_extras.anim_utils module directly there is more control over baking actions, but the steering pose transform bone needs to be reset in order to correctly bake steering. --- bake_operators.py | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/bake_operators.py b/bake_operators.py index 65fed30..4e7d810 100644 --- a/bake_operators.py +++ b/bake_operators.py @@ -19,6 +19,7 @@ # import bpy +import bpy_extras.anim_utils import mathutils import math import itertools @@ -208,14 +209,15 @@ def _bake_action(self, context, *source_bones): source_bones_matrix_basis.append(context.object.pose.bones[source_bone.name].matrix_basis.copy()) source_bone.select = True - # Blender 2.81 : Another hack for another bug in the bake operator - # removing from the selection objects which are not the current one - for obj in context.selected_objects: - if obj is not context.object: - obj.select_set(state=False) - - bpy.ops.nla.bake(frame_start=self.frame_start, frame_end=self.frame_end, only_selected=True, bake_types={'POSE'}, visual_keying=True) - baked_action = context.object.animation_data.action + baked_action = bpy_extras.anim_utils.bake_action( + context.object, + action=None, + frames=range(self.frame_start, self.frame_end + 1), + only_selected=True, + do_pose=True, + do_object=False, + do_visual_keying=True, + ) # restoring context for source_bone, matrix_basis in zip(source_bones, source_bones_matrix_basis): @@ -262,6 +264,10 @@ def _bake_wheels_rotation(self, context): bones = set(wheel_bones + brake_bones) baked_action = self._bake_action(context, *bones) + if baked_action is None: + self.report({'WARNING'}, "Existing action failed to bake. Won't bake wheel rotation") + return + try: for wheel_bone, brake_bone in zip(wheel_bones, brake_bones): self._bake_wheel_rotation(context, baked_action, wheel_bone, brake_bone) @@ -377,15 +383,24 @@ def _bake_steering_rotation(self, context, bone_offset, bone): clear_property_animation(context, 'Steering.rotation') fix_old_steering_rotation(context.object) fc_rot = create_property_animation(context, 'Steering.rotation') - action = self._bake_action(context, bone) + + baked_action = self._bake_action(context, bone) + if baked_action is None: + self.report({'WARNING'}, "Existing action failed to bake. Won't bake steering rotation") + return try: - for f, steering_pos in self._evaluate_rotation_per_frame(action, bone_offset, bone): + # Reset the transform of the steering bone, because baking action manipulates the transform + # and evaluate_rotation_frame expects it at it's default position + pb: bpy.types.PoseBone = context.object.pose.bones[bone.name] + pb.matrix_basis.identity() + + for f, steering_pos in self._evaluate_rotation_per_frame(baked_action, bone_offset, bone): kf = fc_rot.keyframe_points.insert(f, steering_pos) kf.type = 'JITTER' kf.interpolation = 'LINEAR' finally: - bpy.data.actions.remove(action) + bpy.data.actions.remove(baked_action) class ANIM_OT_carClearSteeringWheelsRotation(bpy.types.Operator):