# Set existing animation start frame with script

I have an object, which has two keyframes to make a short animation.

I have a script which duplicates this object and its animation and changes the start frame of the animation. I currently have this code :

line = duplicateObject(bpy.data.scenes["Scene"], "Line", bpy.data.objects["Line"])
line.location[0] = value[0]
line.hide = False
line.animation_data_create()
line.animation_data.action = bpy.data.objects['Line'].animation_data.action.copy()
frame_range = line.animation_data.action.frame_range
line.animation_data.action.frame_range = mathutils.Vector([frame + frame_range[0], frame + frame_range[1]])


Which doesn't work because I can't assign the frame_range variable, it's read only.

Any suggestion?

• are you trying to stretch the keyframes using their total frame_range ? – zeffii Jun 14 '13 at 7:44

You're right. Since frame_range is read-only, you can't modify it directly. It gives you the range over which all fcurves of that action are defined. AFAIK you'll have to change fcurve values of that action. Here's a small snippet of how to do that:

>>> for f in line.animation_data.action.fcurves:
...     for k in f.keyframe_points:
...         k.co[0] += frame
...         k.handle_left[0] += frame
...         k.handle_right[0] += frame


...where I assume you have predefined value for frame (e.g. frame = 10)

Here each of k.co, k.handle_left, k.handle_right, have 2 components: frame number and value. We have incremented the frame number of each keyframe (to translate all keyframes by constant amount). You can also play around with advanced things such as scaling or resampling.

Happy PyBlendering! :)

PS: Please note that if inside your duplicateObject(...) function if you're calling bpy.ops.object.duplicate(...), then you don't need to separately create animation data or action, since it is done automatically by the operator.

• @zeffii Just tested using b56533 but they seem to be absolute numbers only. Can you please verify this once again? – mg007 Jun 14 '13 at 12:26
• you are quite right, I didn't look carefully. I'll remove the comment. Thanks for the correction. dl.dropboxusercontent.com/u/3397495/blender_related/… works fine – zeffii Jun 14 '13 at 12:39

If you want to manipulate actions, you might want to consider using the NLA (non-linear-actions), where you can mix, loop, modify actions etc. Otherwise you are limited to having to move about each keyframe of the actions fcurve's.