# Animating a mesh deformation using python

I have a 3D mesh in Blender that undergoes some modifications. The mesh is imported from python data. By using a python script, I modify the location of the vertices of the mesh to move them along the normal according to a function which is defined for every single vertex in the mesh. This function is also defined for N frames and determines the amount of vertex translation along the local normal for each frame, i.e. f(iVertex, iFrame). This can create an animation if played from frame number 1 to frame N. In addition, the mesh itself is not translating or rotating and in fact, it is its shape that undergoes gradual deformation and change. I am looking for a strategy that can animate this shape deformation using a python script.

• Since you have the function just append it to a handler ( frame_change_pre ) you'll have to wrap it in another function to loop through verts Apr 25 '16 at 20:26
• Thank you for the great suggestion. It worked just fine. Apr 25 '16 at 22:15
• you should write an answer to help others with the same problem Apr 25 '16 at 22:26

As suggested by Chebhou, the problem can be addressed perfectly and very elegantly by using frame_change_pre. I was inspired by the example that was posted here and I bring it here for the readers' convenience:

import bpy
import math

# for demo it creates a cube if it doesn't exist,
# but this can be any existing named object.
if not 'Cube' in bpy.data.objects:

frames_per_revolution = 120.0
step_size = 2*math.pi / frames_per_revolution

def set_object_location(n):
x = math.sin(n) * 5
y = math.cos(n) * 5
z = 0.0
ob = bpy.data.objects.get("Cube")
ob.location = (x, y, z)

# every frame change, this function is called.
def my_handler(scene):
frame = scene.frame_current
n = frame % frames_per_revolution

if n == 0:
set_object_location(n)
else:
set_object_location(n*step_size)

bpy.app.handlers.frame_change_pre.append(my_handler)