Anim: add low-level function for simple FCurve key deduplication #107089

Manually merged
Sybren A. Stüvel merged 3 commits from dr.sybren/blender:anim/deduplicate-keys into main 2023-04-24 12:34:28 +02:00

3 Commits

Author SHA1 Message Date
Sybren A. Stüvel d747dd1087 Animation: add function for deduplicating FCurve keys
buildbot/vexp-code-patch-coordinator Build done. Details
Introduce `BKE_fcurve_deduplicate_keys()` to merge keys that are on the
same time, or within the time comparison threshold (1/100th of a frame).

When merging two consecutive keys, the last one 'wins' and determines
the final key *value*. The first key's *time* is retained, to ensure the
reference point for the next comparisons is stable. The only exception
here is when there is a key exactly on an integer frame number, in which
case that one is preferred.

The function is exposed in RNA as `fcurve.keyframe_points.deduplicate()`

This commit also introduces a new function `BKE_fcurve_bezt_shrink(fcu,
new_totvert);` that can reallocate the `bezt` array to ensure removed
keys no longer take up memory.

The RNA function `fcurve.update()` currently performs two steps, which
are now exposed to RNA as well, as `keyframe_points.sort()` and
`keyframe_points.handles_recalc()`. This is so that Python code can
sort, deduplicate, and then recalculate the handles only once (calling
`update` + `deduplicate` would do the latter twice).

In Blender 4.0 the deduplication will also be part of `fcurve.update()`,
see #107126.

Reviewed on #107089
2023-04-24 12:05:36 +02:00
Sybren A. Stüvel 7e1d743bde Anim: add simple unit test for stability of `sort_time_fcurve()`
The `sort_time_fcurve()` function should be stable, i.e. not change the
relative order of keys that have the same X-coordinate. This is now
enforced by a unit test.

I want to build a new function that can deduplicate the keyframes after
sorting, for which it's important that the 'last key wins' to get reliable
results.
2023-04-24 11:55:43 +02:00
Sybren A. Stüvel d297a98160 Refactor: move keyframe-adding function from RNA to editors/animation
The RNA function `fcurve.keyframe_points.add(N)` was purely implemented
in the RNA wrapper. It now sits in `ED_keyframes_add(fcu, N)` where it
can be used by other C code as well.

It's in the editors code, as regular keyframe insertion is there as
well.

No functional changes.
2023-04-24 11:52:55 +02:00