Results 1 to 9 of 9
  1. #1

    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.



  2. #2
    Will this help?

    Code:
    # 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
    bpy.ops.object.add(type='MESH')
    newObject = bpy.context.object
    newObject.name = "MyTriangle"
    newMesh = newObject.data
    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),
               (x,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)
    newMesh.update()
    
    # select the created object
    bpy.ops.object.select_name(name="myTriangle")
    
    # do something with the object. A rotation, in this case
    bpy.ops.transform.rotate(value=(-0.5*pi, ), axis=(-1, 0, 0))
    
    # create keyframe
    bpy.ops.anim.keyframe_insert_menu(type='Rotation')
    Last edited by appultaart; 26-Nov-11 at 09:39. Reason: forgot command to insert keyframe



  3. #3
    Originally Posted by appultaart View Post
    Will this help?

    Code:
    # 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
    bpy.ops.object.add(type='MESH')
    newObject = bpy.context.object
    newObject.name = "MyTriangle"
    newMesh = newObject.data
    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),
               (x,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)
    newMesh.update()
    
    # select the created object
    bpy.ops.object.select_name(name="myTriangle")
    
    # do something with the object. A rotation, in this case
    bpy.ops.transform.rotate(value=(-0.5*pi, ), axis=(-1, 0, 0))
    
    # create keyframe
    bpy.ops.anim.keyframe_insert_menu(type='Rotation')
    Thanks.
    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?



  4. #4
    Member Atom's Avatar
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    11,646
    Here is an example of just inserting a location and rotation key assuming you are given an object and have a valid context.
    Code:
    # 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.



  5. #5
    Member
    Join Date
    Feb 2011
    Location
    Russia, Irkutsk
    Posts
    16
    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
    Attachment 198892
    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.



  6. #6
    Member
    Join Date
    Dec 2011
    Location
    Germany
    Posts
    3,990
    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)



  7. #7
    Member
    Join Date
    Feb 2011
    Location
    Russia, Irkutsk
    Posts
    16
    Originally Posted by CoDEmanX View Post
    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.
    https://www.dropbox.com/s/ko2my9qwn6...999Model.blend



  8. #8
    Member
    Join Date
    Dec 2011
    Location
    Germany
    Posts
    3,990
    set keyframe:
    bpy.context.scene.frame_set(frame_number)

    select object:
    bpy.data.objects[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!
    bpy.ops.object.mode_set(mode='OBJECT')

    get object by name:
    bpy.data.objects.get(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:
    bpy.context.scene.objects.active = ob

    set thickness for first solidify modifier of the object:
    Code:
    ob = bpy.data.objects[0]
    for mod in ob.modifiers:
        if mod.type == 'SOLIDIFY':
            mod.thickness = 0.5
            break
    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:
    http://www.blender.org/documentation...html#animation

    Code:
    >>> ob.modifiers[0].thickness = 2
    >>> ob.modifiers[0].keyframe_insert(data_path="thickness") # uses current frame
    True
    # 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:
    Code:
    ob = C.object
    ob.animation_data_create()
    ob.animation_data.action = bpy.data.actions.new("Solidify Thickness")
    fcu = ob.animation_data.action.fcurves.new(data_path="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



  9. #9
    Member
    Join Date
    Feb 2011
    Location
    Russia, Irkutsk
    Posts
    16
    Wow, that's great! Using your suggestions I composed simple script of such elements:
    Code:
    #Select an object named OBJECTNAME
    ob = bpy.data.objects['OBJECTNAME']
    bpy.context.scene.objects.active=ob
    
    #Set current frame to FRAME
    bpy.context.scene.frame_set(FRAME)
    
    #Change its solidify modifier thickness
    ob.modifiers[0].thickness = 4
    
    #Make a keyframe for this thickness
    ob.modifiers[0].keyframe_insert(data_path="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!



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •