# Refresh an F-curve with Python after changing Extrapolation Mode [closed]

After creating an F-curve for the default cube and then setting the Extrapolation Mode to 'LINEAR', the graph editor displays a very strange curve:

The script I ran in the Text Editor of a new .blend file was:

import bpy

obj = bpy.context.object
obj.animation_data_create()
obj.animation_data.action = bpy.data.actions.new(name="MyAction")
fcu_z = obj.animation_data.action.fcurves.new(data_path="location", index=2)
fcu_z.keyframe_points[0].co = 10.0, 0.0
fcu_z.keyframe_points[1].co = 20.0, 1.0

fcu_z.extrapolation='LINEAR'


The F-curve can be manually "refreshed" by selecting a control point and hitting GRMB.

So my questions are:

1. Can I "refresh" the F-curve in the script?
2. Should I submit a bug report?
• Modern blender have FCurve.update(). Docs: Ensure keyframes are sorted in chronological order and handles are set correctly Jun 22 '18 at 9:59

If you're changing the area.type anyway, I suggest to use a meaningful operator and replace the fcu_z.extrapolation = 'LINEAR' by graph.extrapolation_type() operator:

import bpy
context = bpy.context

obj = context.object
obj.animation_data_create()
obj.animation_data.action = bpy.data.actions.new(name="MyAction")
fcu_z = obj.animation_data.action.fcurves.new(data_path="location", index=2)
fcu_z.keyframe_points[0].co = 10.0, 0.0
fcu_z.keyframe_points[1].co = 20.0, 1.0

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


Submitted bug report: https://developer.blender.org/T38774 (fixed in 2.70, see report)

You don't set the coordinates for the handles.

Blender won't check if these are valid. In this case the right handle is initialized with the default value
(0.0, 0.0) which is invalid since the x-coordinate of the left endpoint is 10.0. So handle_x > endpoint_x is violated here.

Moving the point blender will automatically recalculate the handles for the points.

BTW: keyframe_points.insert will recalculate the curve unless the option FAST is chosen.

This example works:

import bpy

obj = bpy.context.object
obj.animation_data_create()
obj.animation_data.action = bpy.data.actions.new(name="MyAction")
fcu_z = obj.animation_data.action.fcurves.new(data_path="location", index=2)
fcu_z.extrapolation='LINEAR'
fcu_z.keyframe_points.insert(10.0, 0.0)
fcu_z.keyframe_points.insert(20.0, 1.0)


I've found a workaround. This function seems to refresh the F-Curve Editor:

def refresh_fcurve_editor():
'''execute a meaningless command on F-Curve Editor which has the effect of
refreshing the graph.'''
C=bpy.context
old_area_type = C.area.type
C.area.type='GRAPH_EDITOR'
bpy.ops.graph.clean( threshold = 0)
C.area.type=old_area_type