Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 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 @@ -302,6 +308,10 @@ def _evaluate_distance_per_frame(self, action, bone, brake_bone):
def _bake_wheel_rotation(self, context, baked_action, bone, brake_bone):
fc_rot = create_property_animation(context, bone.name.replace('MCH-', ''))

# Reset the transform of the wheel bone, otherwise baking yields wrong results
pb: bpy.types.PoseBone = context.object.pose.bones[bone.name]
pb.matrix_basis.identity()

for f, distance in self._evaluate_distance_per_frame(baked_action, bone, brake_bone):
kf = fc_rot.keyframe_points.insert(f, distance)
kf.interpolation = 'LINEAR'
Expand Down Expand Up @@ -377,15 +387,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