move specific verts then add shape key

Hey,

I have a set of points/vertices, I want to edit their location and add a shape key to catch the new location. I want to do this around 30 times. In the end producing an animation through keyframes. I am trying to test on a cube. My original simplistic thought was to add a shape key and then move a specific vert:


import bpy
#moves the first vertice in the cube down 4 units
o = bpy.data.meshes['Cube']
verts = o.vertices
v = verts[0]
bpy.ops.object.shape_key_add(from_mix=True)
v.co[2] = -5

but that didn’t work. It seems that a specific vertice has to be moved in an edit mode session for the shape key to work properly. So I tried selecting a specific vert inside an edit session. This is where I am stuck. I am unable to select a specific vertice inside an edit session through python. If anyone has any ideas on how to select a vertice inside an edit session through python or animate a field of vertices where each vertice moves in a specific direction every time step please let me know.

Any and all help is appreciated.

Selecting vertices has to be done in objectmode

…vertices[i].select = True

Self experimenting gives maybe a better start?


import bpy
#moves the first vertice in the cube down 4 units
o = bpy.data.objects['Cube']
verts = o.data.vertices
v = verts[0]
bpy.ops.object.shape_key_add(from_mix=True)
bpy.ops.object.shape_key_add(from_mix=True)
o.active_shape_key.name = "Abracadabra"
#this does not work yet ?
v.co[2] = -5
#I think you must create vertexgroups assign them to a shapekey and...???

EDITed
Removing all keys one can see that vertex 2 is at z = -5 :eyebrowlift2:

Problems solved :evilgrin: see attachment!

Attachments

shapekeypython.blend (64.4 KB)

PKHG props to you my friend, you are both a gentleman and a scholar. Thank you for helping me figure this out. It would have taken me forever to figure out that I needed to add as a vertex group. I can’t wait to play with this today, thanks again!

PKHG looks like the vertex grouping isn’t required. The key that you gave was the ability to select specific vertices in object mode. For this I am eternally grateful. Code to make a shape key of defined vertice locations is below:


import bpy
o = bpy.data.objects['Cube']
verts = o.data.vertices
v = verts[2]
for all in verts:
    all.select = False
v.select = True      #in object mode!
bpy.ops.object.shape_key_add(from_mix=True)     #Basis
bpy.ops.object.shape_key_add(from_mix=True)     #First Key
o.active_shape_key.name = "Abracadabra"
bpy.ops.object.editmode_toggle()
bpy.ops.transform.translate(value=(0, 0, -4), constraint_axis=(False, False, True), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), texture_space=False, release_confirm=False)
bpy.ops.object.editmode_toggle()