2
$\begingroup$

I'm writing an export script for Blender actions (in the context of video game animation). As part of that process, my script bakes actions that use inverse kinematics, as follows:

pairs = []
pairs.append((armature, None))
newAction = next(iter(anim_utils.bake_action_objects(pairs, frames=range(0, end), only_selected=True, do_pose=True, do_object=False, do_visual_keying=True, do_clean=True)), None)

Unfortunately, the resulting keyframes (for bones affected by IK) are wrong following this call. In this context, "wrong" means that each bone's local transforms (post-bake) are different than what's displayed in Blender (using Pose -> Animation -> Bake Action... from pose mode) using the same baking parameters. After scouring this site, the Blender API, and Blender's source code (Python and C), I've been unable to pinpoint why these results (a bone's local transforms post-bake) would differ between my script and Blender itself.

I've tried many tweaks to the script without success, including the following:

  • Changing mode before baking (e.g. OBJECT or EDIT rather than POSE)
  • Ensuring that all relevant bone layers are visible in Blender before exporting
  • Clearing pose transforms (position, rotation, and scale) prior to baking
  • Not clearing pose transforms prior to baking
  • Removing all other actions (such that only one IK-using action remains)
  • Swapping modes after the bake
  • Updating the view layer after the bake
  • Changing bake parameters
  • Using different bake functions in Python (e.g. a direct call to bpy_extras.anim_utils.bake_action_iter)

My experience with Blender's Python API so far tells me that there's likely an obscure detail that's causing my bake results to differ from Blender. What am I missing?

EDIT: I'm editing this question primarily to get extra attention (since, after a year, I still haven't solved the problem).

One additional piece of information: when baking an action in this way (by calling a bake function on anim_utils through a Python script), the resulting action is wrong even when viewed in Blender. Importantly, this means that the error is NOT a result of additional processing by my script or my game engine. The anim_utils call itself is, somehow, baking incorrectly.

Additionally, I revisited the Blender source code recently. The Pose -> Animation -> Bake Action... UI in Blender seems to use the NLA_OT_Bake class from anim.py, part of Blender's source code. Bafflingly, the execute function doesn't seem to perform any additional setup prior to calling anim_utils.bake_action_objects. I'm at a loss for why my call produces incorrect results.

$\endgroup$
2
  • 1
    $\begingroup$ Can you attach a simple .blend that demonstrates the problem? $\endgroup$
    – scurest
    Jan 26, 2023 at 5:26
  • $\begingroup$ See my accepted answer. I don't have a .blend file to share because the problem wasn't reproducible when running through Blender's scripting window. $\endgroup$
    – Grimelios
    Jan 26, 2023 at 10:36

1 Answer 1

0
$\begingroup$

Following scurest's suggestion, I tried to create a simple demo using the Blender scripting window. Surprisingly, this approach caused the bake to work correctly every time, even when copy/pasting the code from my export script verbatim. Given this knowledge, I fixed my export script by simply moving the baking code earlier in the export process.

So, while I have successfully fixed my problem, I still don't understand why it worked. I know something within Blender's codebase (i.e. some implicit state) can cause bakes to fail without error, but I'm currently unwilling to investigate further. I'm marking this answer as accepted anyway because there's valuable to knowing the fix even without understanding it.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .