8

I am trying to bake a softbody animation into keyframes but I can't, does anyone knows how to do it? Thanks!

Jaroslav Jerryno Novotny
  • 51,077
  • 7
  • 129
  • 218
Hernan
  • 81
  • 1
  • 3

3 Answers3

15

You can't bake softbody into keyframes anymore, blender uses shape keys now. So you would need to generate a shape key for every frame of animation and also keyframe that shapekey to its frame. You do that with running this script:

import bpy

obj = bpy.context.active_object
start = bpy.context.scene.frame_start
end = bpy.context.scene.frame_end

def insert_keyframe(sk, f):
    sk.keyframe_insert("value", frame=f-1)
    sk.keyframe_insert("value", frame=f+1)
    sk.value = 1.0
    sk.keyframe_insert("value", frame=f)

meshes = []
for f in range(start, end+1):
    bpy.context.scene.frame_set(f)
    meshes.append(obj.to_mesh(scene=bpy.context.scene, apply_modifiers=True, settings='PREVIEW'))

obj.shape_key_add(name="Basis", from_mix=False)
for i in range(0, end-start+1):
    key = obj.shape_key_add(name=str(i+start), from_mix=False)
    insert_keyframe(key, i+start)
    for vert_id in range(len(obj.data.vertices)):
        key.data[vert_id].co = meshes[i].vertices[vert_id].co

for mesh in meshes:
    bpy.data.meshes.remove(mesh)

Don't forget to turn your modifier off or to delete it (or it will override the shape key animation)

Also you can export your softbody animation as an New Tek's .mdd mesh cache format (export addon will do this) and load this file into Mesh Cache modifier. This allows you to for example offset the animation and further manipulate it.

Jaroslav Jerryno Novotny
  • 51,077
  • 7
  • 129
  • 218
  • Thanks Jerryno for your response!, ok I achieved exporting in .mdd format and load it in the Mesh Cache modifier. Also y deleted the soft-body physics and it works perfectly. Now I would need to export it in .FBX, but apparently something is missing because in the fbx preview visor the effect doesn't appear, should I do something else to export it correctly? Thank you! – Hernan Jan 21 '15 at 20:34
  • Also how can I generate a shape key for every frame of animation and also keyframe that shapekey to its frame? – Hernan Jan 22 '15 at 13:03
  • OK, I could manage to have my mesh with shape keys!, the problem is that i need to export it in .FBX (ASCII), do you know how can I solve this?? – Hernan Jan 22 '15 at 14:32
  • Look for export shapekey with .fbx and if you can't find it pls ask it as a new question. As that will draw in people who understand that. Also mark this one as correct if it worked, thx.) Also check if you cannot import your animation as that .mdd mesh cache or also .pc2 instead of .fbx – Jaroslav Jerryno Novotny Jan 22 '15 at 14:49
  • This is an incredibly awesome script. Thanks for posting it!

    What would need to be done to make it so that it will only create the shape key for frames that actually have changes?

    – Pete Aug 08 '15 at 05:02
  • @Pete It would need to compare the previous mesh vertices if they differ agains the current frame mesh vertices. If some do differ, then create shapekey and keyframe it. But from the nature of softbody sims it's very likely every frame will differ. – Jaroslav Jerryno Novotny Aug 10 '15 at 08:18
  • @Jerryno, Thanks for the reply! In my case, I'm using the script to allow me to transition animation from Blender to Cinema4D via FBX. In my particular situation, my meshes freeze in place for frames at a time, so it would be super useful to optimize it so that it doesn't create any more shape keys/keyframes than necessary. Is there a simple way to do that comparison you just described? Or is it pretty complex?

    Thanks so much for the information you've provided already, and no worries if that's a bigger question that you can answer on here.

    – Pete Aug 10 '15 at 20:07
  • 1
    @Pete Yep for such situation it would be better to design the script a bit differently. Maybe also some workflow already exist for your application. This would be a good new question on this site, better searchable for other users also. If no one beats me to it I'll post the altered script as an answer or a better solution if there is. – Jaroslav Jerryno Novotny Aug 11 '15 at 07:14
  • 1
    Thanks man! I've done so here: http://blender.stackexchange.com/questions/35547/how-can-i-bake-vertex-animation-into-keyframes-but-make-sure-that-there-arent – Pete Aug 12 '15 at 03:02
3

I need to chime in, just because I had the same question - but wound up with a different take on kind of the same answer as above:

I agree with the sentiment that "you can't bake them into key frames" anymore - but you can run the script above to do so - however, (at least in my case) what I wanted to do was to tweak and better control how/when/where the animation was played back.

So in my case, merely animating the shape keys themselves was ideal. This means, after you have your softbody/cloth or whatever simulation - go into the "modifier" and "save to shape key". Then, just animate that shape key in the graph editor/dopesheet - just as you would have done to your mesh.

This is actually much cleaner than editing animations recorder through the blender game engine (which is what I thought it would be like to "export" the physics mesh animation to keyframes) - where you would have one keyframe for every frame. (This is what the script above gives you - and you wouldnt' need that either). You just have keyframe points at any time you create a keyframe for your shape key changing.

Sorry for the longwinded explanation - but now I understand why they mad you do it with the shape keys - and am happier for it!

Brad
  • 225
  • 1
  • 7
2

Updated version of the script, that works with Blender 3.3.1:

import bpy

obj = bpy.context.active_object

note: you can just set start and end frame numbers manually

start = bpy.context.scene.frame_start end = bpy.context.scene.frame_end dg = bpy.context.evaluated_depsgraph_get()

def insert_keyframe(sk, f): sk.keyframe_insert("value", frame=f-1) sk.keyframe_insert("value", frame=f+1) sk.value = 1.0 sk.keyframe_insert("value", frame=f)

obj.shape_key_add(name="Basis", from_mix=False)

for f in range(start, end+1): bpy.context.scene.frame_set(f) obj_evaluated = bpy.context.object.evaluated_get(dg) mesh = obj_evaluated.to_mesh(preserve_all_data_layers=True, depsgraph=dg)

key = obj.shape_key_add(name=str(f), from_mix=False)
insert_keyframe(key, f)
for vert_id in range(len(obj.data.vertices)):
    key.data[vert_id].co = mesh.vertices[vert_id].co

obj_evaluated.to_mesh_clear()

Note you still need to remove the modifier afterwards.

mik01aj
  • 294
  • 2
  • 9
  • Hi @mik01aj, thanks for the updated script. Doesn't work for me in Blender 3.4.1 sadly. I get four "unexpected indent" errors and nothing happens :( – Glen Candle Dec 23 '22 at 03:53