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...

Showing posts with label scripted keyframe. Show all posts
Showing posts with label scripted keyframe. Show all posts

August 12, 2011

Scripted Keyframe animation from imported mocha file

This will provide some insight into the pleasantly trivial, but still involved, nature of scripting keyframes for a spline object. Currently i only do basic 'POLY' form of spline. read the code here :

github.com/zeffii/AE--afterfx--Mocha-shape-import

August 11, 2011

scripted keyframing of Curve Coordinates

after poking around, it seems this is an ok way to script keyframes for a 'POLY' curve.
If you want NURBS or BEZIER, i suggest reading the rotobezier.py addon by zanqdo, available here, especially CURVE_OT_insert_keyframe


July 21, 2011

scripted keyframing of hide / hide render setting on object

###### place at t = T_START-1 a keyframe with hide set to True
current_frame = T_START-1
bpy.ops.anim.change_frame(frame=current_frame)
bpy.context.active_object.hide = True
bpy.context.active_object.keyframe_insert(  data_path="hide", 
                                            index=-1, 
                                            frame=current_frame)    
    
###### place at T_START a keyframe with hide set to False
current_frame = T_START
bpy.ops.anim.change_frame(frame=current_frame)
bpy.context.active_object.hide = False
bpy.context.active_object.keyframe_insert(  data_path="hide", 
                                            index=-1, 
                                            frame=current_frame)
It's handy to make keyframes first as experiment for lateron when you try to script something, if you can't get the basics going manually don't expect to be able to script it without understanding the sequence of operations required.

If you have a property that you want to keyframe, then you will need to know the data_path. In the code example below i choose to keyframe the 'hide' property. If you are unsure about the string name needed for data_path, then rightclick the property/icon and select "copy data_path". Then paste that into ( console / TextEditor / your code ) to see the path.

if you need to remove all your animation data use action-data-how-to-purge.html

you might set up a hide animation like this
( side note: data_path='hide' hides from view, not from render.. that's data_path='hide_render' ).

# For instance you first define T_START, T_END and LAST_FRAME
# T_START and T_END as the first and last frames that the object will be visible in.
# Having an additional keyframe on either side of those frames allows you to play
# the sequence backwards without messing up the hide duration.  
# "hide" ( hides from view ) use "hide_render" to make it apply to render.
    time_and_state_settings = ( (0, True), 
                                (T_START-1, True),
                                (T_START, False),
                                (T_END, False),
                                (T_END+1, True),
                                (LAST_FRAME, True))

    for time_val in time_and_state_settings:    
        current_frame = time_val[0]
        bpy.ops.anim.change_frame(frame=current_frame)
        bpy.context.active_object.hide = time_val[1]
        bpy.context.active_object.keyframe_insert(  data_path="hide", 
                                                    index=-1, 
                                                    frame=current_frame)
    

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 )