I am trying to bake a softbody animation into keyframes but I can't, does anyone knows how to do it? Thanks!
-
2$\begingroup$ Could you add some specifics like what version you are using, what you have tried already, etc? $\endgroup$– J SargentCommented Jan 21, 2015 at 19:33
-
$\begingroup$ Related: blender.stackexchange.com/q/1426/599 $\endgroup$– gandalf3Commented Jan 21, 2015 at 20:10
-
$\begingroup$ Hi there, I am using version 2.72 and I am trying to export a mesh with soft-body in .FBX format but can't achieve this. $\endgroup$– HernanCommented Jan 22, 2015 at 12:42
3 Answers
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.
-
$\begingroup$ 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! $\endgroup$– HernanCommented Jan 21, 2015 at 20:34
-
$\begingroup$ Also how can I generate a shape key for every frame of animation and also keyframe that shapekey to its frame? $\endgroup$– HernanCommented Jan 22, 2015 at 13:03
-
$\begingroup$ 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?? $\endgroup$– HernanCommented Jan 22, 2015 at 14:32
-
1$\begingroup$ @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. $\endgroup$ Commented Aug 11, 2015 at 7:14
-
1$\begingroup$ Thanks man! I've done so here: blender.stackexchange.com/questions/35547/… $\endgroup$– PeteCommented Aug 12, 2015 at 3:02
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!
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.
-
$\begingroup$ 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 :( $\endgroup$ Commented Dec 23, 2022 at 3:53