Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active May 30, 2020 18:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/5593f5aab165de8d2043 to your computer and use it in GitHub Desktop.
Save zeffii/5593f5aab165de8d2043 to your computer and use it in GitHub Desktop.
import bpy
from mathutils import Vector
import bmesh
import numpy as np
sig = 0.3
n_frames = 101
bpy.context.scene.frame_end = n_frames
pi = np.pi
make_ico = bpy.ops.mesh.primitive_ico_sphere_add
make_ico(subdivisions=3, size = 2.0, location = (0,0,3))
obj = bpy.context.active_object
me = obj.data
ico0 = np.array([v.co for v in me.vertices]) # get ico vertices
zmin, zmax = ico0[:,2].min(), ico0[:,2].max()
zc = np.linspace(zmin, zmax, n_frames)
ico = np.zeros_like(ico0)
# pad pristine copy of the icosphere as first element and last element of data.
data = [ico0.copy()]
for i in range(n_frames):
ico[:,:2] = (1.0 + np.exp(-(ico0[:,2] - zc[i])**2/(2.*sig**2)))[:,None] * ico0[:,:2]
ico[:,2] = ico0[:,2]
data.append(ico.copy())
data.append(ico0.copy())
def insert_keyframe(fcurves, frame, values):
for fcu, val in zip(fcurves, values):
fcu.keyframe_points.insert(frame, val, {'FAST'})
action = bpy.data.actions.new("MeshAnimation")
me.animation_data_create()
me.animation_data.action = action
data_path = "vertices[%d].co"
vec_z = Vector((0.0, 0.0, 1.0))
frames = list(range(n_frames+2))
for index, v in enumerate(me.vertices):
fcurves = [action.fcurves.new(data_path % v.index, i) for i in range(3)]
co_rest = v.co
for t in frames:
co_kf = data[t][index]
insert_keyframe(fcurves, t, co_kf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment