Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using Python to add a keyframe to an object seems easy (see here or here), but how can I add a keyframe to a scene property?

As a concrete example, take bpy.context.scene.gravity.

share|improve this question
up vote 8 down vote accepted

Even though the examples linked are to keyframe an object, there is in fact no difference and the same methods apply to both.


scene = bpy.context.scene
scene.keyframe_insert(data_path="gravity", frame=10.0)


scene = bpy.context.scene
if not scene.animation_data:
if not scene.animation_data.action:
    scene.animation_data.action ="GravityAction")

fcurve = None
for fcurve in scene.animation_data.action.fcurves:
    if fcurve.data_path == "gravity":
if not fcurve or fcurve.data_path != "gravity":
    fcurve ="gravity")
keyframe = fcurve.keyframe_points.insert(frame=10.0, value=20.0)
keyframe.interpolation = "LINEAR"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.