How do you animate verts using keyframes?

I have a noob question about how to change the object data (ie. verts) via keyframes, in order to morph a shape over time. I’ve posted my basic script that creates a cube but I’m unsure about where to go to achieve changes in the shape - is this possible? and how would I do this?

heres the code

import bpy
import bmesh
from mathutils import Matrix
from math import radians

verts = [(-1, -1, -1), (-1, 1, -1), (1, 1, -1), (1, -1, -1), (-1, -1, 1), (-1, 1, 1), (1, 1, 1), (1, -1, 1)]
faces = [(0,1,2,3), (0,1,5,4), (1,2,6,5), (2,3,7,6), (3,0,4,7), (4,5,6,7)]
me = bpy.data.meshes.new("cubey")

ob = bpy.data.objects.new("cubey", me)
bpy.context.scene.objects.link(ob)

me.from_pydata(verts, [], faces)
me.update(calc_edges=True)

me = bpy.context.object
ob = bpy.context.object

If you want to animate the shape of a mesh, you should look at shapekeys.

Here is a little script to give you an example (I’m not an expert on this subject so there should be a better way to do it but it gives you the idea) :


import bpy, bmesh


ob = bpy.context.active_object


#To add shapekeys, we need to be in object mode
bpy.ops.object.shape_key_add(from_mix=False)    #Create the Basis shapekey : it contains the original shape of your mesh
bpy.ops.object.shape_key_add(from_mix=False)    #Create the first shapekey


bpy.context.object.active_shape_key_index = 1   #Select the first shapekey as the current one (so that we can edit it)


#Once we've selected the shape key, we can go in edit mode
bpy.ops.object.editmode_toggle()


b = bmesh.from_edit_mesh(ob.data)
b.verts[0].co.x += 1                            #Modify the mesh data


shpkey = ob.data.shape_keys.key_blocks[1]


#At frame 1 : set the influence of the first shapekey at 0 : the mesh will look like the base shape
shpkey.value = 0
shpkey.keyframe_insert(data_path="value", frame=1, index=-1)


#At frame 10 : set the influence of the first shapekey at 1 : the mesh will look like the first shape
shpkey.value = 1
shpkey.keyframe_insert(data_path="value", frame=10, index=-1)


#We quit the edit mode so we can see the animation when playing it
bpy.ops.object.editmode_toggle()

You may wanna look into bmesh module, the custom data layers for shape keys can be found here: bm.verts.layers.deform