I have a combined animation (position of object, bone rotations...) with about 600 keyframes. Now I want to make a script that clones a selected range of these keyframes and pastes them after the initial range's end.
This works:
def duplicate_keyframe(armature_object, source_keyframe, target_keyframe):
anim = armature_object.animation_data
action = anim.action
for fcurve in action.fcurves:
if source_keyframe > len(fcurve.keyframe_points) - 1 or not fcurve.keyframe_points[source_keyframe]:
continue
old_keyframe = fcurve.keyframe_points[source_keyframe]
new_keyframe = fcurve.keyframe_points.insert(target_keyframe, old_keyframe.co.y)
new_keyframe.amplitude = old_keyframe.amplitude
new_keyframe.interpolation = old_keyframe.interpolation
new_keyframe.handle_left = old_keyframe.handle_left
new_keyframe.handle_right = old_keyframe.handle_right
new_keyframe.handle_left_type = old_keyframe.handle_left_type
new_keyframe.handle_right_type = old_keyframe.handle_right_type
new_keyframe.period = old_keyframe.period
new_keyframe.easing = old_keyframe.easing
new_keyframe.back = old_keyframe.back
def duplicate_keyframes(armature_object, start_duplicate_at, first_keyframe, last_keyframe):
for i in range(first_keyframe, last_keyframe):
duplicate_keyframe(armature_object, i, start_duplicate_at + i - first_keyframe)
... however, it is very slow. The culprit seems to be fcurve.keyframe_points.insert(), which takes a few milliseconds. Multiply that with the number of fcurves and the number of frames, and the whole operation takes several seconds.
So question: Is there a more efficient way to clone a set of keyframes for all fcurves?