#https://blender.stackexchange.com/questions/82144/how-can-i-call-and-edit-animation-keys-from-the-dope-sheet-in-python
import bpy
def reverse_kps(keyframe_points, frame_start, frame_end):
rval = []
for i in range(len(keyframe_points)):
kp = keyframe_points[i]
copy = {}
copy["handle_left"] = temporally_reversed(kp.handle_right, frame_start, frame_end)
copy["handle_left_type"] = kp.handle_right_type
copy["handle_right"] = temporally_reversed(kp.handle_left, frame_start, frame_end)
copy["handle_right_type"] = kp.handle_left_type
copy['co'] = temporally_reversed(kp.co, frame_start, frame_end)
if (i>0):
copy["interpolation"] = keyframe_points[i-1].interpolation
copy["easing"] = keyframe_points[i-1].easing
rval.insert(0, copy)
return rval
def temporally_reversed(fry, frame_start, frame_end):
fr2 = frame_end - (fry[0] - frame_start)
c2 = (fr2, fry[1])
return c2
def overwrite(dst, src):
for i in range(len(src)):
for attr in src[i].keys():
setattr(dst[i], attr, src[i][attr])
def reverse_keyframes(action):
action_start = None
action_end = None
for fc in action.fcurves:
kp0 = fc.keyframe_points[0]
if action_start is None or action_start > kp0.co[0]:
action_start = kp0.co[0]
kp9 = fc.keyframe_points[-1]
if action_end is None or action_end > kp9.co[0]:
action_end = kp9.co[0]
for fc in action.fcurves:
reversed_kp = reverse_kps(fc.keyframe_points, action_start, action_end)
overwrite(fc.keyframe_points, reversed_kp)
def reverse_keyframes_o(obj):
reverse_keyframes(obj.animation_data.action)
#
#
#
reverse_keyframes_o(bpy.context.active_object)
|
Blender python API quick-start
Syntax highlighting by Pygments.