Skip to content

Commit

Permalink
Bake operators fix
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Griperis committed Feb 13, 2023
1 parent f25da07 commit e635a49
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions bake_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# <pep8 compliant>

import bpy
import bpy_extras.anim_utils
import mathutils
import math
import itertools
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit e635a49

Please sign in to comment.