Blender 2.6: Set keyframes using Python script

I know that we can always use “Insert keyframes” functions that provided by Blender.
But I want to learn how to set keyframes using Python script.
Because I want to create dynamic animation. Means the animation is based on the Input data.

Can someone provide some sample on basic of how to insert keyframe using Python script?
It may be a simple example such as moving a cube from a location to another point within 20 frames.

Thanks in advance.

Will this help?

# prepare a scene
scn = bpy.context.scene
scn.frame_start = 1
scn.frame_end = 101

# move to frame 17
bpy.ops.anim.change_frame(frame = 17)

# create an object
newObject = bpy.context.object = "MyTriangle"
newMesh =
x, y, z = 0, 0, 0
width, depth, height = 1, 1, 0.5
newVerts = [(x,y,z), (x+width,y,z), (x+width,y+depth,z), (x,y+depth,z),
           (x,y,z+height), (x+width,y,z+height), (x+width,y+depth,z+height),
newEdges = []       # creating vertices and faces is sufficient.
newFaces = [(0,1,2,3), (2,3,7,6), (4,5,6,7), (0,3,7,4), (1,2,6,5), (0,1,5,4)]
newMesh.from_pydata(newVerts, newEdges, newFaces)

# select the created object

# do something with the object. A rotation, in this case
bpy.ops.transform.rotate(value=(-0.5*pi, ), axis=(-1, 0, 0))

# create keyframe

I think this will helps.
But I understand all the codes here. But can I ignore the create an object part, means i create an object(e.g. human model) inside the 3D view rather using code to create it?
then can I still select the created object that created using 3D view?

I think this will helps.
But I understand all the codes here. But can I ignore the create an object part, means i create an object(e.g. human model) inside the 3D view rather using code to create it?
then can I still select the created object that created using 3D view?

Here is an example of just inserting a location and rotation key assuming you are given an object and have a valid context.

# The object exists so lets add keyframes.
cf = context.scene.frame_current
keyInterp = context.user_preferences.edit.keyframe_new_interpolation_type
context.user_preferences.edit.keyframe_new_interpolation_type ='LINEAR'

temp_ob.keyframe_insert(data_path='location', frame=(cf))
temp_ob.keyframe_insert(data_path='rotation_euler', frame=(cf))

context.user_preferences.edit.keyframe_new_interpolation_type = keyInterp

This code also shows how to override the user preferences for keyframe type to LINEAR then restores it after the operation is complete.

Good day. I don’t know how to use scripts at all, but I feel it is time to learn. Now I’m sitting and setting many similar keyframes for many similar objects manually. The script i dream about is something like this:

  1. Set current frame to 100
  2. Select an object named Object1
  3. Change this object’s Solidify modifier’s thickness parameter to 200
  4. Insert keyframe for this Thickness
  5. Go to step 1 and continue with different parameters (frame, object name and thickness)
    I would write such script if I knew necessary commands. I just need 4 commands for 4 steps and will do the rest. Will be bery happy and grateful if you help me.

do you wanna specify the frames to be keyframed one by one? (meaning obj1 = frame 33, obj2 = 180, etc. -> arbitrary frames, no pattern)

is this just for solidify thickness, or different modifier settings? and shall the values be randomly chosen, be all the same or follow a certain pattern?

how many keyframes do you wanna create in total and per object? like start and end keyframe (=2/object) or 1 keyframe for a certain range (e.g. 20-200 = 180 keyframes)

A few frames, but many objects (in frame 500 many objects change thickness, in frame 600 many objects change thickness and so on). Only solidify thickness will vary with manually chosen values. 26 keyframes per object. Long script of similar commands. I think I’ll handle it if I knew commands to set current frame, select an object by its name, set solidify thickness of this object and make this thickness keyframed. For you I share blender model I’m currently working on and where such script can help. Thanks.

set keyframe:

select object:[n].select = True #but you may need to deselect first
bpy.ops.object.select_all(action=‘DESELECT’) #make sure there is a context.object and change to object mode when using this!

get object by name:, False) # return object if found or return False

make object active (only one in scene can be active, usually the last selected of multiple selected objects. If only one object shall be selected, it should also be active: = ob

set thickness for first solidify modifier of the object:

ob =[0]
for mod in ob.modifiers:
    if mod.type == 'SOLIDIFY':
        mod.thickness = 0.5

keyframing the thickness can be done by using keyframe_insert, but it’s an operator and therefore much slower than a low-level approach. See here for keyframe/animation example:

>>> ob.modifiers[0].thickness = 2
>>> ob.modifiers[0].keyframe_insert(data_path="thickness") # uses current frame
# But you can also keyframe another frame without frame_current / frame_set:
>>> ob.modifiers[0].keyframe_insert(data_path="thickness", frame=10)

using low level functions seems a little tricky, not a nice solution, but the only i could find in a hurry:

ob = C.object
ob.animation_data.action ="Solidify Thickness")
fcu ="modifiers[" + str(ob.modifiers.find(ob.modifiers[0].name)) + "].thickness")
fcu.keyframe_points.add(num) # it might have 1 already, not sure
fcu.keyframe_points[0].co = 10, 0.5 # example, 10 is the frame, 0.5 the thickness value

Wow, that’s great! Using your suggestions I composed simple script of such elements:

#Select an object named OBJECTNAME

#Set current frame to FRAME

#Change its solidify modifier thickness
ob.modifiers[0].thickness = 4

#Make a keyframe for this thickness

This is exectly what I was looking for. The final script is very long though it saves dozen of time and I’m satisfied with the result. Thank you very much for you detailed and operative answer!