Selecting only the next keyframe with python?

I am writing my first addon and want to select only the next keyframe in order to manipulate it. There is something called ‘After Current Frame’, but that selects all of the keyframes after the current frame. Is there any way to select only one keyframe to the right of the current frame, or do I have to map every keyframe to an index and select them that way?


use this operator:

you could also manually walk over current object’s animation_data.action.fcurves and find the next keyframe via, but probably easier to use that built-in op

I forgot to mention that I want the animation position (current frame) to stay in place since my addon will be used with animation that is being played back. Is there any way to do this or should I just save the position of all the keyframes beforehand?

your question wasn’t quite clear, so you actually wanna select keyframe points in some view? (dopesheet, fcurve, ?)

Yes, I want to select the next column of keyframe points to the right of the current frame in the dopesheet view, so kind of a relative select based on the playback marker. If I didn’t care about the playback position I could jump to the next keyframe using bpy.ops.screen.keyframe_jump() and then bpy.ops.action.select_column(mode=‘KEYS’) to select it, but if I want to let blender’s playback dictate where the current frame is things get more complicated.

Actually it’s pretty strange if there’s no way to select the next keyframe column in the way that I’m trying to, I think that would be pretty useful. That is, a relative mode based on the current frame and an absolute mode that only takes in account the selected column and goes to the next/previous one.

Maybe I should add this functionality first, but that seems a lot scarier than writing an addon… :stuck_out_tongue:

I guess I’ll have to include this functionality in my addon by writing the keyframes’ location to a python list or something. If they were even called lists.

ok, just one more question: do you really wanna do this (jump to next keyframe and select keyframe points there, but not the points inbetween current playback frame and that keyframe) while animation playback is running?

No, I don’t want to jump, I want to select only the next keyframe column (to the right of the current frame) without jumping. I don’t want it to jump because the whole point of the addon is that you use it while playing back the animation normally. I’ll post some images to help explain.

The first image is before pressing anything. The second image is after the first keypress for the selection. The third image is after the second keypress for the selection.

Press key to select

Animation has played for a while, press key again to select

Hope this helps to clear stuff up. :slight_smile:

ok, so while user is playing back animation, there should be a shortcut/hotkey to select the column of keyframe points, which is the next after current playback position (frame_current). When user presses that hotkey, playback shall continue without jumping and previous keyframe selection should get cleared. Is that correct?

Yes! It may seem a bit odd, but I need this functionality for the rest of the addon to work as I want it to. That isn’t the only thing that should happen when a user presses it, but I’ll keep the rest a secret for now (until I have it working). :smiley:
Anyway, I’ll just start coding and use a list of the keyframe… frames and use discrete selections for now.

this is actually something for c-coding, e.g. action_select.c:

static void actkeys_select_leftright(bAnimContext *ac, short leftright, short select_mode)

	/* select keys */
	for (ale = anim_data.first; ale; ale = ale->next) {
		AnimData *adt = ANIM_nla_mapping_get(ac, ale);

Instead of selecting all subsequent keyframe points, you’d have to somehow add a break here to only select the next column…

So, if I want to implement a new way to select keyframes I’ll need to do it in C? I guess adding such low level (relatively) code isn’t easy for a new developer. Especially since the main thing I want to do is an addon.

Maybe I should complete the addon first using another method and after that examine whether it’s viable or even possible to add a new way to select in C.

you can probably do this with python too, but since there is already this select_leftright operator, it sounded logical to me to add two new parameters (“Next” / “Previous”) here. You don’t have to do this from scratch. However, I’m not familiar with such stuff either and can’t give you a patch for this. But if you find out how the actual selection is made (the function calls below “AnimData *adt = ANIM_nla_mapping_get(ac, ale);”), it should be easy to add two new selection types which only select next/previous keyframe column.