2
$\begingroup$

I can add and remove keyframes like this via python, no issues arise.

#remove all existing keys
for i in reversed(range(0, len(fcu.keyframe_points))):
    fcu.keyframe_points.remove(fcu.keyframe_points[i])
#insert the corrected keys
for frame, sample in zip(new_frames, new_samples):
    fcu.keyframe_points.insert(frame, sample)

But I want to use the fast option to avoid updating the fcurve after each modification and only update after the loop:

#remove all existing keys
for i in reversed(range(0, len(fcu.keyframe_points))):
    fcu.keyframe_points.remove(fcu.keyframe_points[i], fast=True)
fcu.update()
#insert the corrected keys
for frame, sample in zip(new_frames, new_samples):
    fcu.keyframe_points.insert(frame, sample, set(("FAST", )))
fcu.update()

Now that code is also functional, but in the dope sheet, the resulting keyframes are dark grey instead of the usual white. Anything I'm missing or is this intended behavior? Does the color have any significance?

$\endgroup$
2
$\begingroup$

This answer doesn't address the FAST mode issue, it is another method to clear an fcurve and set new points that avoids looping, reversing or using either the insert or remove methods of FCurve.keyframe_points entirely.

import bpy
frames = [1, 9, 20]
samples = [0, 8, 0]
# some action
a = bpy.data.actions["SomeAction"]
fc = a.fcurves[0] # some fcurve
dp, i = fc.data_path, fc.array_index
# clear the fcurve by removing it
a.fcurves.remove(fc)
# new one
fc = a.fcurves.new(dp, index=i)
# new points
fc.keyframe_points.add(count=len(frames))
# populate points
fc.keyframe_points.foreach_set("co", [x for co in zip(frames, samples) for x in co])
# update 
fc.update()
$\endgroup$
1
  • $\begingroup$ Thanks! Ah yes, foreach_set()! I had that in mind but I always find the syntax so unintuitive that I don't come up with it on my own haha $\endgroup$ – HENDRIX Oct 14 '17 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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