Effects of python script are not permanent I created my first python script in blender, the purpose of which is to manipulate an animation.
To be more precise, it loops over some F-curve and on some of its BEZIER segments, it changes the intermediate control points (left_handle and right_handle in blender speak). I am quite happy because the script does what it is supposed to do.

However, whenever I manipulate the animation otherwise (create a new keyframe or use the F-curve editor), the effects of the python script are undone. So I keep the editor window open and when needed, hover over it and do Alt-P; so by and large, the problem is only mildly annoying, but anyhow. Also, I meanwhile noticed that the scripted changes do survive a save, blender close and re-open. But the first added keyframe spoils it again.

Q: Did I miss some special call that needs to be made in order to make my scripted manipulations "permanent"?

For the record, here's the script, slightly shortened

import bpy
import os

fc = bpy.context.active_object.animation_data.action.fcurves
loc_z_curve = fc.find('location', index=2)

extrafactor = -1.5 * bpy.context.scene.gravity[2] / bpy.context.scene.render.fps / bpy.context.scene.render.fps

prevk = None
for k in loc_z_curve.keyframe_points:
    if not prevk is None and prevk.interpolation == 'BEZIER':
        frames = (k.co[0] - prevk.co[0])/3.0
        dist = (k.co[1] - prevk.co[1])/3.0
        extra = extrafactor * frames * frames
        prevk.handle_right[0] = prevk.co[0] + frames
        prevk.handle_right[1] = prevk.co[1] + dist + extra
        k.handle_left[0] = k.co[0] - frames
        k.handle_left[1] = k.co[1] - dist + extra
    prevk = k
All I needed was to add two lines

prevk.handle_right_type = 'FREE'
k.handle_left_type = 'FREE'


The original / default handle types caused a recalculation with the consequences observed once the F-curve was "touched" in any way, and that is expected behaviour.