Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

May 27, 2011

Scripted Animation using keyframe_insert

Inspired by a post on BA, it just so happens that i can use this for my own particle script. It's pretty basic but easy enough to elaborate on.

While this first solution will work, it is not optimal on massive datasets because it uses bpy.ops inside the positions loop. The short story about bpy.ops is that ops operations are really meant to be called from the UI directly, where you won't notice the slowdown (blender state refreshes) between clicks.

(using bpy.ops like in the following snippet is not recommended)
import bpy

start_pos = (0,0,0)
bpy.ops.mesh.primitive_uv_sphere_add(segments=32, size=0.3, location=start_pos)
bpy.ops.object.shade_smooth()

positions = (0,0,2),(0,1,2),(3,2,1),(3,4,1),(1,2,1)
frame_num = 0
for position in positions:
    bpy.context.scene.frame_set(frame_num)
    bpy.context.active_object.location = position
    bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)
    frame_num += 10
    

Here is a version that uses the data_path and avoids using bpy.ops in the loop ( recommended )