2
$\begingroup$

Right now I am trying to make a loop animation, and I need to copy 1st keyframe of active fcurve and paste it into fcurve's last+1 keyframe using python script. And this is the python script I am using right now.

Scene_Name = bpy.context.scene.name
current = bpy.data.scenes[Scene_Name].frame_current
endframe = bpy.data.scenes[Scene_Name].frame_end+1

# I need to select active F-curve's 1st keyframe here

bpy.ops.graph.copy()
bpy.context.scene.frame_set(endframe)
bpy.ops.graph.paste(offset='START', merge='MIX', flipped=False)
bpy.context.scene.frame_set(current)

it works fine but I need to select 1st keyframe of active fcurve manually before using this script.

Image below shows orange keyframes, and it is active keyframes. I would like to select those keyframes using python to make my script work automatically. Is it possible?

enter image description here

$\endgroup$
2
  • $\begingroup$ The graph editor does not expose its idea of an active fcurve to Python. Instead you have to select the fcurve from the object's animation data's action and then edit that fcurve by copying the first keyframe and creating a new one in the last position. $\endgroup$ Commented Dec 17, 2021 at 16:29
  • $\begingroup$ it seems hard to control fcurves using python $\endgroup$ Commented Dec 18, 2021 at 4:19

1 Answer 1

1
$\begingroup$

You need to find the fcurve by some other means. Here is an example where I want the Y location of the only action of the active object:

object = bpy.context.active_object
yloc = object.animation_data.action.fcurves[1]

Then you need to insert the new keyframe into the fcurve's KeyFramePoints data structure. Here's a function that will copy the first keyframe to a new frame number:

def dup_first_keyframe(fcurve, frame_number):
    keyframes = fcurve.keyframe_points
    keyframes.insert(frame_number, keyframes[0].co[1])

and here's how to invoke it, to copy the keyframe from the start to frame 48:

dup_first_keyframe(yloc, 48.0)

But it is not possible to get the graph editor's idea of 'active fcurve', because that's a property of the UI and not the animation data of the object.

$\endgroup$
1
  • $\begingroup$ it works fine. thank you. you said it's not possible to get graph editor's active fcurve using python. Right now I am editing fcurve's multiple channels.for example there is a bone which has keyframes on X,Y,Z locations. When I edit X location, I need to copy and paste keyframes of X location channel so I need to get what channel I am editing. I need script like active_channel = activefcurve.activechannel. I can't do that kinda thing using python? $\endgroup$ Commented Dec 18, 2021 at 4:25

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.