Last active
May 30, 2020 18:41
-
-
Save zeffii/5593f5aab165de8d2043 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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