I've got the following dope sheet keyframes setup. I want to automate the process to select every 2nd keyframe of an object. It's sort of an edge loop selection on keyframes, but slightly different.
Select evenly indexed keyframe points and handles.
The keyframes in the fcurve are in order when iterated. Select every second (odd one) by making a
False, True, False, True,... list the same length as keyframe points to select the points and handles accordingly.
Test script, (every second) selects the keyframes of the context objects animation. The one shown when action editor is chosen from dope sheet.
import bpy context = bpy.context ob = context.object ad = ob.animation_data action = ad.action if ad else None if action: for fc in action.fcurves: #fc.select = True # select fcurve too? sel = list(bool(i % 2) for i in range(len(fc.keyframe_points))) # select point and handles fc.keyframe_points.foreach_set("select_control_point", sel) fc.keyframe_points.foreach_set("select_left_handle", sel) fc.keyframe_points.foreach_set("select_right_handle", sel) #del(sel) # clean up?
action = ad.action ...line above with
action = bpy.data.actions.get("SomeActionName")if you know the action you want to modify by name.
For every nth, in this case every 3rd, where for example sake there are 15 keyframes (py console code)
>>> nth = 3 >>> sel = list(not (i + 1) % nth for i in range(15)) >>> sel [False, False, True, False, False, True, False, False, True, False, False, True, False, False, True]
%is the modulus operator, for integers its the remainder, eg
5 % 3 == 2Indexes start from 0.