How to move last keyframes in blender by python

I have keyframes like this image:

I used below code to move last keyframes and got result as below image:

import bpy
for action in bpy.data.actions:
    for fcurve in action.fcurves:
        point = fcurve.keyframe_points[-1]
        point.co.x += 5

My question is

  1. why I have keyframes at 5th frame? How to move all last frames to 7th frame
  2. Is last frame (at 7th frame) = last frame (at 2nd frame before)?

Thanks you!


works for me

1 Like

Thank you for your help. But I just want to move last frame (3st frame move to 8st frame, start frame-2nd no need to move)

My guess is that you might have some actions with only a single keyframe on frame 0.

This code:
point = fcurve.keyframe_points[-1]
gets the last point in the array
and this:
point.co.x += 5
will move the point 5 frames.

So, if there is only one point in the array on frame 0 it’ll move that point to frame 5 since a single point is also the last point. That’s my guess.

1 Like

oh, thank you very much.
You are right! (I tried follow your suggestions).

Can I ask more about:

since a single point is also the last point

have any document about it or your guess?

It just has to do with how many elements are in an array.

Say you have:

array = [ "Banana" ]

You could access the string in the array with the index of the element.

array[0] gets the first element of the array, which is “Banana”
array[-1] gets the last element of the array, which is also “Banana”

There’s only one element in the array so mathematically that element is both the first and last item in the array.

But if you try array[1] or array[-2] you’ll get an error because the index is larger than the number of elements in the array.

Now try:

array = [ "Apple", "Orange" ]

array[1] returns “Orange”
array[-2] returns the second-to-last element, which is “Apple”

1 Like

oh, I got it, thank you!

No problem! :+1: