4
$\begingroup$

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.

Dope sheet keyframe setup

$\endgroup$
5
+50
$\begingroup$

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?

Notes:

  • Replace 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 == 2 Indexes start from 0.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.