# get newly created keyframe in python

I would like to get a reference to a keyframe when I create it. Alas, keyframe_insert() returns a bool indicating success, not a handle to the keyframe.

In my application, each keyframe may need a different interpolation mode (or other parameter) so it seems good to do this at creation time. Consider doing the following for an object where dataset has many elements:

ob = bpy.context.active_object
prop='["MyParam"]'

for interp, value, frame_time in dataset:
ob['MyParam'] = value
ob.keyframe_insert(data_path=prop, frame=frame_time)
# interp = LINEAR or BEZIER or ...
# set this one appropriately:  How?


The only thing I can see at the moment is seems complicated:

1. get the fcurves from ob.animation_data.action.fcurves
2. ask the fcurves collection to look for the one with the right data_path,
3. go searching the fcurve for a keyframe with the right values

This seems to look like:

# go looking for it
curve = ob.animation_data.action.fcurves.find(prop)
pts = curve.keyframe_points
# pts.find(key) what is "key" other than "the identifier for the collection member."
# do I need a foreach() loop here?


What would the key parameter be? Is there a better way? This multiple-search step seems like it would get costly fast. Any hints/clues would be welcome.

• Can you provide a number for [many elements] such as 88 or 8888? Is this script run once every so often or on every frame? – atomicbezierslinger Apr 5 '19 at 20:21
• So the script is run once to set up the scene. However there are 10,000 objects each with a nearly arbitrary number of custom properties, each of which is keyframed at between 8 and 800 points. – IRayTrace Apr 8 '19 at 21:19

The find curve by path and insert the keyframe is very common in Blender.

You may not need to find the any keyframe in Blender by key. You can iterate through them and update a value such as curve type if the frame matches the frame in your inventory by frame number.

If you have potentially extra items you can remove(delete) the entire curve.

Your concern about time efficiency ("costly") is good yet now may not be the time to optimize.

There is the [add] method documented here.

https://docs.blender.org/api/current/bpy.types.FCurveKeyframePoints.html#bpy.types.FCurveKeyframePoints

• This looks promising. It does mean I will need to collect all the keyframes up to put in in one giant batch, but that is probably tolerable. Thanks!!!! – IRayTrace Apr 8 '19 at 21:22