I've got the same error RuntimeError: Error: Keyframe not in F-Curve
trying to delete keyframes with keyframe_points.remove
.
Here is my code that throws and error:
fc = bpy.context.object.animation_data.action.fcurves[200]
print([fc.co for fc in fc.keyframe_points])
print([id(fc) for fc in fc.keyframe_points])
for kf in fc.keyframe_points.values():
print(kf.co, id(kf))
if kf.co.x != 0 and kf.co.x != 28:
fc.keyframe_points.remove(kf)
print('removed')
print([fc.co for fc in fc.keyframe_points])
print([id(fc) for fc in fc.keyframe_points])
I have no idea why but it seems like it might something to do with how keyframe_points
and it's elements stored in memory - so at the moment when I'm trying to remove keyframe from keyframe_points
it's already moved somewhere and that's why I get the error.
It's so weird and it looks like Blender bug.
SOLUTION - workaround I've come up with:
fc = bpy.context.object.animation_data.action.fcurves[200]
print([fc.co for fc in fc.keyframe_points])
print([id(fc) for fc in fc.keyframe_points])
i = 0
while i < len(fc.keyframe_points):
kf = fc.keyframe_points[i]
print(i, kf.co, id(kf))
if kf.co.x != 0 and kf.co.x != 28:
print('removed')
fc.keyframe_points.remove(kf)
else:
i = i + 1
print([fc.co for fc in fc.keyframe_points])
print([id(fc) for fc in fc.keyframe_points])
Full error traceback for anyone curious:
fc = bpy.context.object.animation_data.action.fcurves[200]
print([fc.co for fc in fc.keyframe_points])
> [Vector((9.0, 0.0)), Vector((10.0, -1.0803276495607861e-08)), Vector((11.0, 1.8595179840374954e-09)), Vector((24.0, 1.8595179840374954e-09)), Vector((25.0, 1.1508858310804726e-09)), Vector((26.0, 1.6575807393337527e-09)), Vector((27.0, 1.1508858310804726e-09)), Vector((28.0, 0.007000000216066837)), Vector((29.0, -0.007000000216066837)), Vector((30.0, 0.0))]
print([id(fc) for fc in fc.keyframe_points])
> [1923420162368, 1923420090112, 1923420162368, 1923420090112, 1923420162368, 1923419915648, 1923420162368, 1923419915648, 1923420162368, 1923420090112]
for kf in fc.keyframe_points.values():
print(kf.co, id(kf))
if kf.co.x != 0 and kf.co.x != 28:
fc.keyframe_points.remove(kf)
print('removed')
> <Vector (9.0000, 0.0000)> 1923419919232
> removed
> <Vector (11.0000, 0.0000)> 1923419921408
> removed
> <Vector (25.0000, 0.0000)> 1923419915648
> removed
> <Vector (27.0000, 0.0000)> 1923419925824
> removed
> <Vector (29.0000, -0.0070)> 1923419925312
> removed
> <Vector (30.0000, 0.0000)> 1923419928192
> Traceback (most recent call last):
> File "<blender_console>", line 4, in <module>
> RuntimeError: Error: Keyframe not in F-Curve
print([fc.co for fc in fc.keyframe_points])
[Vector((10.0, -1.0803276495607861e-08)), Vector((24.0, 1.8595179840374954e-09)), Vector((26.0, 1.6575807393337527e-09)), Vector((28.0, 0.007000000216066837)), Vector((30.0, 0.0))]
print([id(fc) for fc in fc.keyframe_points])
[1923420349056, 1923420025856, 1923420349056, 1923420025856, 1923420349056]
```