vertex animation in blender 2.5

hi

does anybody have a simple python script
to animate the vertices of for example a standard cube ?

or another example code :slight_smile:

just to get started

thanks
sc3*2

You probably want to look into shape keys to animate verts.
To script it you’ll create a basis shape key, then another shape key with the verts in the new positions, then keyframe or drive the value of that shapekey.

I tried to write a little script to do this but I apologize I wasn’t able to get it to work. I’ll post what I had here to see if anybody will fill in the mistakes.


import bpy
from random import uniform

coords = [ (-1,-1,0), (-1, 1, 0), (1, 1, 0), (1, -1, 0) ]
faces = [ (0,1,2,3) ]

me = bpy.data.meshes.new('foo')
me.from_pydata(coords, [], faces)
ob = bpy.data.objects.new('foo', me)

ob.shape_key_add('basis')
key = ob.shape_key_add('myShapeKey')
key.value = 1.0

for vert in ob.data.vertices:
    vert.co.x += uniform(-0.35, 0.35)
    vert.co.y += uniform(-0.35, 0.35)
    vert.co.z += uniform(-0.35, 0.35)

bpy.context.scene.objects.link(ob)

teldredge
thanks for your help

i found this script about shapekeys
http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code_snippets/Meshes#Vertex_groups_and_shapekeys

#----------------------------------------------------------
# File shapekey.py
#----------------------------------------------------------
import bpy, random
 
def run(origin):
    # Add UV sphere
    bpy.ops.mesh.primitive_uv_sphere_add(
        segments=6, ring_count=5, size=1, location=origin)
    ob = bpy.context.object
    ob.name = 'ShapeKeyObject'
    ob.show_name = True
 
    # Create Left and Right vertex groups
    left = ob.vertex_groups.new('Left')
    right = ob.vertex_groups.new('Right')
    for v in ob.data.vertices:
        if v.co[0] > 0.001:
            left.add([v.index], 1.0, 'REPLACE')
        elif v.co[0] < -0.001:
            right.add([v.index], 1.0, 'REPLACE')
        else:
            left.add([v.index], 0.5, 'REPLACE')
            right.add([v.index], 0.5, 'REPLACE')
 
    # Add Basis key
    bpy.ops.object.shape_key_add(None)
    basis = ob.active_shape_key
 
    # Add FrontForward key: front verts move one unit forward
    # Slider from -1.0 to +2.0
    bpy.ops.object.shape_key_add(None)
    frontFwd = ob.active_shape_key
    frontFwd.name = 'FrontForward'
    frontFwd.slider_min = -1.0
    frontFwd.slider_max = 2.0    
    for v in [19, 20, 23, 24]:
        pt = frontFwd.data[v].co
        pt[1] = pt[1] - 1
 
    # Add TopUp keys: top verts move one unit up.  TopUp_L and 
    # TopUp_R only affect left and right halves, respectively
    keylist = [(None, ''), ('Left', '_L'), ('Right', '_R')]
    for (vgrp, suffix) in keylist:
        bpy.ops.object.shape_key_add(None)
        topUp = ob.active_shape_key
        topUp.name = 'TopUp' + suffix
        if vgrp:
            topUp.vertex_group = vgrp
        for v in [0, 1, 9, 10, 17, 18, 25]:
            pt = topUp.data[v].co
            pt[2] = pt[2] + 1
 
    # Pose shape keys
    for shape in ob.data.shape_keys.key_blocks:
        shape.value = random.random()
    return
 
if __name__ == "__main__":
    # Create five object with random shapekeys
    for j in range(5):
        run((3*j,0,0))

but the question remains
also after your script
how do you go about using those shapekeys in an animation ?
( through python :smiley: )

regards
sc3*2

the AnimAll add on in trunk animates vertices trough a single shapekey

thanks
will look into it

i, the crossposter, got an answer from the master himself
http://lists.blender.org/pipermail/bf-python/2011-May/005614.html

Assuming you added 2 shape keys to the active cube

bpy.context.object.data.shape_keys.key_blocks[“Key 1”].value = 0.5
bpy.context.object.data.shape_keys.key_blocks[“Key 1”].keyframe_insert(“value”)

For more info on keyframing see:
http://www.blender.org/documentation/blender_python_api_2_57_release/bpy.types.bpy_struct.html#bpy.types.bpy_struct.keyframe_insert

let me see if i can get it to work

ZanQdo,

where may I find a description about how to use AnimAll?

You’d think the wiki link in the script would be where to find the details on usage but that just has redirects…

Yeah, sometimes it just redirects to BlenderArtists thread and that’s fine as long as you find info about the script.
I’ve searched the forum here, but no luck so far.

Just googling this thread. So, did you have any success with your experiments? I’m trying to import vertices from FEM analysis to Blender and use Blender to visualize the deformation. This all should happen using the Python programming interface, so I’m very interested about hearing did you ever manage to solve the problems you have faced.