| # animate the size of objects to expand from size=0 to 1 and down to 0 again according to its position as a wave sweeps across the scene.
import bpy
from mathutils import *
def animate_size_sweep(scn, obj_to_frame_f, timescale=40, timescale2=15):
    for obj in scn.objects:
        if not obj.select:
            continue
        obj.animation_data_clear()
        fr = obj_to_frame_f(obj)
        obj.scale =(0,0,0)
        obj.keyframe_insert(data_path='scale', frame=fr-timescale-timescale2)
        obj.keyframe_insert(data_path='scale', frame=fr+timescale+timescale2)
        s = 0.9
        obj.scale=(s,s,s)
        obj.keyframe_insert(data_path='scale', frame=fr-timescale)
        obj.keyframe_insert(data_path='scale', frame=fr+timescale)
def x_sweep(obj):
    return 30*(obj.location.x +5.5)
def animate_size_2(scn, size_f, frame_range):
     for obj in scn.objects:
        if not obj.select:
            continue
        obj.animation_data_clear()
        for fr in frame_range:
            sz = size_f(fr, obj.location)
            obj.scale =(sz,sz,sz)
            obj.keyframe_insert(data_path='scale', frame=fr)
def animate_size_3(scn, size_f_name):
    for obj in scn.objects:
        if not obj.select:
            continue
        obj.animation_data_clear()
        script = (size_f_name+"( frame, [%f,%f,%f])"%(obj.location[:]) )
        for index in range(3):
            fcurve = obj.driver_add("scale", index)
            fcurve.driver.expression = script
def cell_size_for_sphere(loc1, center, r_1, r_0):
    r = (Vector(loc1)-Vector(center)).length
    pre = (r-r_0)/(r_1-r_0)
    return max(0, min(pre,1))
def travelling_spheres(fr, loc):
    things = [
        cell_size_for_sphere(loc, Vector([-6,5,5]) + Vector([1,0,0])*fr/15, 2.5, 3.5),
        cell_size_for_sphere(loc, Vector([5,-16,5]) + Vector([0,1,0])*fr/15, 2.5, 3.5),
        cell_size_for_sphere(loc, Vector([5,5,-26]) + Vector([0,0,1])*fr/15, 2.5, 3.5),
    ]
    return max(things)
def travelling_spheres2(fr, loc):
    if (fr>999):
        return 0.8
    things = [
        cell_size_for_sphere(loc, Vector([-5,0,0]) + Vector([1,0,0])*fr/15, 2.5, 3.5),
        cell_size_for_sphere(loc, Vector([0,-15,0]) + Vector([0,1,0])*fr/15, 2.5, 3.5),
        cell_size_for_sphere(loc, Vector([0,0,-25]) + Vector([0,0,1])*fr/15, 2.5, 3.5),
    ]
    return max(things)
#
#
#
bpy.app.driver_namespace['travelling_spheres'] = travelling_spheres
bpy.app.driver_namespace['travelling_spheres2'] = travelling_spheres2
scn = bpy.context.scene
if False:
    if scn.name=="all cubes":
        #animate_size_2(scn, travelling_spheres, range(600))
        animate_size_3(scn, "travelling_spheres")
    elif scn.name == "driver test":
        animate_size_3(scn, "travelling_spheres")
    elif scn.name == "many cells":
        fn = "travelling_spheres2"
        fn = "knot_worm"
        animate_size_3(scn, fn)
    elif scn.name == "disable":
        animate_size_sweep(scn, x_sweep, 20)
 | 
Blender python API quick-start
Syntax highlighting by Pygments.