I've seen a sweet technique for baking vertex animation to keyframes by means of creating a shape key for every frame and keying the shape key to come on at the right time. An awesome user on this site (shoutout to Jerryno) wrote a script to take care of this process in one shot. I'm going to go ahead and include that script in my question in case that link ever dies; here it is:
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)
I've run in to some cases, however, where my vertex animation periodically pauses for a bunch of frames, then resumes, then pauses for a bunch of frames, resumes, etc. It seems needless to have all of those duplicate keyframes/shapes during the time when my animation is paused--and since performance is alright not the best on meshes with tons of shape keys I'd love to optimize the script so that it only creates a shape key/keyframe if the vertex data has changed from the previous frame.
I understand that in the code I'd need to do some kind of comparison between the vertices in each frame, but I don't even begin to have an idea how that would be done. Could someone point me in the right direction?