# Animating cameras focal length via python code

I am trying to animate the focal length of the camera, based on focal length information imported from a file. setting the focal length is no problem:

focal_length = 35
timeline_frame = 10
cam = bpy.data.cameras.new('Camera')
cam.lens = focal_length


after setting this value I want to insert a keyframe for the focal length. According to my intuition, I am looking for something like this:

cam.keyframe_insert(data_path = 'lens', frame = (timeline_frame))


(this code deosn´t work...) is there any way to get this to work?

Your code works fine. But note that you are adding a new camera data when you do bpy.data.cameras.new('Camera'). If there already is a Camera data, the new one you created will be named Camera.001. Make sure this camera data is assigned to your camera object.