# How do I add a keyframe for a certain channel using Python?

If I want to add a keyframe in the graph editor, I know I can do this:

bpy.ops.graph.keyframe_insert(type='ALL')


And I suppose that if I change the 'ALL' to 'SEL', it will only apply to the selected channel, but if I want a script that changes multiple channels in different ways, how can I select them with python?

See this answer and these paragraphs #1, #2, #3, #4 in the docs for api methods/functions to create/manipulate channels.

You might define your own function:

def get_channel(action, data_path, index=0):
for fcurve in action.fcurves:
if fcurve.data_path == data_path and fcurve.array_index == index:
return fcurve
#nothing found
return None

action = bpy.data.actions['CubeAction']
data_path = "location"
index = 2

fcurve = get_channel(action, data_path, index)


In this answer some options are given to find out the data_path of a channel.

To select an fcurve in the UI you simply set its select property to True.