# Change extrapolation type from Python running in the background

I have a script that that changes objects in my scene and then creates and renders an animation of the object. It does this on a large batch of objects and makes the same four animations for each object. I typically run this from the command prompt with Blender running in background mode.

The problem I have run into is that I need the extrapolation type for these animations to be linear but I think by default is constant. I need a way of changing the extrapolation type from python while Blender is running the background.

I have tried the changing the extrapolation using:

old_area_type = context.area.type
context.area.type = 'GRAPH_EDITOR'
bpy.ops.graph.extrapolation_type(type='LINEAR')
bpy.ops.graph.handle_type(type='VECTOR')
context.area.type = old_area_type


However, this gives me an attribute error for context.area.type saying that it is set to 'NoneType'.

I looked closer at the documentation and area.type refers to subdivided screens within the editor. However, since my script is running in the background there are no area types to change to/from, thereby causing a NoneType error.

But, if I remove the area.type lines it gives me an incorrect context error for the extrapolation changes.

How can I change the extrapolation type from the background if I can't change the area type?

You can't really directly set the context.area.type, or any context property for that matter - it is a read only reflection of the current state of your scene.

To use the graph editor's operators under bpy.ops.graph, you need to override the context for the operator to "make it think" it's running within the graph editor. Here's an example how to do this for another operator.

But instead I would suggest changing the interpolation and handle types using lower level code instead of using the graph editor's operators. Here's a chunk of code that will do this for all of the active object's animation curves:

import bpy
C = bpy.context
o = C.object

# Iterate over all the objects animation function curves
for fc in o.animation_data.action.fcurves:
fc.extrapolation = 'LINEAR' # Set extrapolation type

# Iterate over this fcurve's keyframes and set handles to vector
for kp in fc.keyframe_points:
kp.handle_left_type  = 'VECTOR'
kp.handle_right_type = 'VECTOR'