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

Fix for #106708: Fcurve Problem after baking animation

Introduce two new BKE functions and a new ED function:

  • void BKE_fcurve_deduplicate_keys(struct FCurve *fcu); deduplicate keys using a "last one wins" approach. Contrary to BKE_fcurve_merge_duplicate_keys(), which is meant for interactive use where the key selection state matters, this function is much simpler and intended to be part of a larger animation data processing pipeline.
  • void BKE_fcurve_bezt_shrink(struct FCurve *fcu, int new_totvert); reallocates fcu->bezt to shrink it down to the new size.
  • void ED_keyframes_add(struct FCurve *fcu, int num_keys_to_add); for adding uninitialized keys to an FCurve. This was placed in ED as the regular keyframe insertion functions are there as well.

Python API changes: fcurve.keyframe_points gets three new functions:

  • .deduplicate(), which calls BKE_fcurve_deduplicate_keys()
  • .sort() which chronologically sorts the keys, and
  • .handles_recalc(), which performs some recalculations on the handles (for example 'auto clamping').

The latter two reflect the functionality of FCurve.update(). The intention of this change is to prepare for an API change in Blender 4.0. Basically #107089 stemmed from the assumption that FCurve.update() would process the FCurve points so that they are in a valid state, which would thus also include deduplication. @cessen and I agree with this, so the intent is to make this happen in Blender 4.0. The newly introduced RNA functions described above can be used in scripts where the deduplication is undesired.

The Blender 4.0 work is tracked in #107126.

Fix for #106708: Fcurve Problem after baking animation Introduce two new `BKE` functions and a new `ED` function: - `void BKE_fcurve_deduplicate_keys(struct FCurve *fcu);` deduplicate keys using a "last one wins" approach. Contrary to `BKE_fcurve_merge_duplicate_keys()`, which is meant for interactive use where the key selection state matters, this function is much simpler and intended to be part of a larger animation data processing pipeline. - `void BKE_fcurve_bezt_shrink(struct FCurve *fcu, int new_totvert);` reallocates `fcu->bezt` to shrink it down to the new size. - `void ED_keyframes_add(struct FCurve *fcu, int num_keys_to_add);` for adding uninitialized keys to an FCurve. This was placed in `ED` as the regular keyframe insertion functions are there as well. Python API changes: `fcurve.keyframe_points` gets three new functions: - `.deduplicate()`, which calls `BKE_fcurve_deduplicate_keys()` - `.sort()` which chronologically sorts the keys, and - `.handles_recalc()`, which performs some recalculations on the handles (for example 'auto clamping'). The latter two reflect the functionality of `FCurve.update()`. The intention of this change is to prepare for an API change in Blender 4.0. Basically #107089 stemmed from the assumption that `FCurve.update()` would process the FCurve points so that they are in a valid state, which would thus also include deduplication. @cessen and I agree with this, so the intent is to make this happen in Blender 4.0. The newly introduced RNA functions described above can be used in scripts where the deduplication is undesired. The Blender 4.0 work is tracked in #107126.
Wayde Moss reviewed 2023-04-18 18:01:47 +02:00
@ -1121,0 +1110,4 @@
static void rna_FKeyframe_points_sort(ID *id, FCurve *fcu, Main *bmain)
{
sort_time_fcurve(fcu);
BKE_fcurve_handles_recalc(fcu);
Collaborator

handles_recalc() should not be added in the same function. The same goes for deduplicate(). User-wise, the proper way to use these functions would be: sort() then deduplicate() which recalculates all of the handles twice.

handles_recalc() should not be added in the same function. The same goes for deduplicate(). User-wise, the proper way to use these functions would be: sort() then deduplicate() which recalculates all of the handles twice.
Poster
Collaborator

You're absolutely right, you can see I was rushing to get the patch uploaded before running to a concert by a friend :)

You're absolutely right, you can see I was rushing to get the patch uploaded before running to a concert by a friend :)
dr.sybren marked this conversation as resolved
Nathan Vegdahl reviewed 2023-04-18 18:30:00 +02:00
@ -1852,0 +1871,4 @@
if (IS_EQT(bezt_x, prev_x, BEZT_BINARYSEARCH_THRESH)) {
/* Remove 'prevbezt', as it has the same X-coord as 'bezt' and the last one wins. */
BKE_fcurve_delete_key(fcu, prev_bezt_index);

I think this has quadratic performance in the general case. If you imagine all the keys except one getting deleted, then this call to BKE_fcurve_delete_key() shifts every remaining element in the array over by one for each visited element.

This probably(?) doesn't matter much in practice, since I imagine large numbers of duplicate elements are probably rare. But at least in principle making it linear isn't too hard, so it might be worthwhile?

I think this has quadratic performance in the general case. If you imagine all the keys except one getting deleted, then this call to `BKE_fcurve_delete_key()` shifts every remaining element in the array over by one for each visited element. This probably(?) doesn't matter much in practice, since I imagine large numbers of duplicate elements are probably rare. But at least in principle making it linear isn't too hard, so it might be worthwhile?
dr.sybren marked this conversation as resolved
Sybren A. Stüvel added this to the 3.6 LTS milestone 2023-04-19 12:02:16 +02:00
Sybren A. Stüvel added the
Module
Animation & Rigging
Interest
Python API
labels 2023-04-19 12:02:31 +02:00
Sybren A. Stüvel changed title from WIP: anim/deduplicate-keys to Add low-level function for simple FCurve key deduplication 2023-04-19 12:02:52 +02:00
Sybren A. Stüvel changed title from Add low-level function for simple FCurve key deduplication to Anim: add low-level function for simple FCurve key deduplication 2023-04-19 12:03:01 +02:00
Sybren A. Stüvel requested review from Nathan Vegdahl 2023-04-19 12:04:40 +02:00
Sybren A. Stüvel requested review from Wayde Moss 2023-04-19 12:04:40 +02:00
Sybren A. Stüvel added this to the Animation & Rigging project 2023-04-19 12:04:59 +02:00
Wayde Moss reviewed 2023-04-20 13:53:49 +02:00
@ -573,2 +573,4 @@
fcurve.update()
# TODO: in Blender 4.0 the next line can be removed, as it will be part of `fcurve.update()` anyway.
keyframe_points.deduplicate()
Collaborator

update() should not be there. I understand that it'll be combined in 4.0 but this patch is about performance. For mocap or animation exporting, we'd likely be baking hundreds of frames and thousands of keys.

update() should not be there. I understand that it'll be combined in 4.0 but this patch is about performance. For mocap or animation exporting, we'd likely be baking hundreds of frames and thousands of keys.

Unless I've misunderstood something (which is always possible), I believe update() is needed here because it sorts the keyframes, which is a necessary precondition for deduplicate() to function correctly.

Unless I've misunderstood something (which is always possible), I believe `update()` is needed here because it sorts the keyframes, which is a necessary precondition for `deduplicate()` to function correctly.
Collaborator

You've got it right. I should've said to add keyframe.sort() too. Update should still not be there.

You've got it right. I should've said to add keyframe.sort() too. Update should still not be there.

Ah, yeah, that makes sense. It looks like update() is also responsible for recalculating handles and tagging for animation update, which I assume need to happen? But those should probably be after deduplicating anyway.

Ah, yeah, that makes sense. It looks like `update()` is also responsible for recalculating handles and tagging for animation update, which I assume need to happen? But those should probably be after deduplicating anyway.
Poster
Collaborator

It'll still need recalculating the handles so I'll remove .update() and replace it with sort+dedup+handles. That way the recalculation of the handles is only done once.

It'll still need recalculating the handles so I'll remove `.update()` and replace it with sort+dedup+handles. That way the recalculation of the handles is only done once.

It'll still need recalculating the handles so I'll remove .update() and replace it with sort+dedup+handles.

Did you make this change already? I don't see it on Gitea. Or is this a change for 4.0?

> It'll still need recalculating the handles so I'll remove .update() and replace it with sort+dedup+handles. Did you make this change already? I don't see it on Gitea. Or is this a change for 4.0?

Huh, that's weird. As soon as I made this reply, it changed for me and showed that you did make the change. I guess Gitea is just being weird, or I misunderstood something about how it works.

Huh, that's weird. As soon as I made this reply, it changed for me and showed that you did make the change. I guess Gitea is just being weird, or I misunderstood something about how it works.
dr.sybren marked this conversation as resolved
Wayde Moss reviewed 2023-04-20 14:53:07 +02:00
@ -1852,0 +1884,4 @@
if (IS_EQT(bezt_x, prev_x, BEZT_BINARYSEARCH_THRESH)) {
/* Replace 'prevbezt', as it has the same X-coord as 'bezt' and the last one wins. */
*prev_bezt = *bezt;
Collaborator

Don't modify prev_bezt's time. (Edit: This would result in time-shifting of 0.01 but, atm, I don't think there's a better solution. Even if we stop comparing with a running prev_x and instead use the prev_x of the first replaced one, then that still allows two keys to be within the threshold for a follow-up deduplicate() call and we end up with a variation of the problem below)

If (bezt_x > prev_x) and (bezt_x != prev_x), then we're replacing prev_x with bezt_x and repeatedly doing so since bezt_x might be very close to the next one and so on. If, for whatever reason, the user has scaled their keys by 1/100 (BEZT_BINARYSEARCH_THRESH=0.01) and they had keys on every frame, then we're effectively deleting all keys except for the last one.

A problematic practical case (still a bit contrived) I can think of is a cyclic fcurve that has keyframe-implemented noise or oscillation (camera shake, hair wind simulation, etc) so time-scaling the curve changes the frequency.

Don't modify prev_bezt's time. (Edit: This would result in time-shifting of 0.01 but, atm, I don't think there's a better solution. Even if we stop comparing with a running prev_x and instead use the prev_x of the first replaced one, then that still allows two keys to be within the threshold for a follow-up deduplicate() call and we end up with a variation of the problem below) If (bezt_x > prev_x) and (bezt_x != prev_x), then we're replacing prev_x with bezt_x and repeatedly doing so since bezt_x might be very close to the next one and so on. If, for whatever reason, the user has scaled their keys by 1/100 (BEZT_BINARYSEARCH_THRESH=0.01) and they had keys on every frame, then we're effectively deleting all keys except for the last one. A problematic practical case (still a bit contrived) I can think of is a cyclic fcurve that has keyframe-implemented noise or oscillation (camera shake, hair wind simulation, etc) so time-scaling the curve changes the frequency.
Collaborator

LGTM

LGTM
Poster
Collaborator

LGTM

Yay!

To officially mark as 'accepted', it's a little bit cumbersome, sorry. You have to go to the 'Files Changed' tab and click the green 'Review' button there. There you find the 'Approve' button.

> LGTM Yay! To officially mark as 'accepted', it's a little bit cumbersome, sorry. You have to go to the 'Files Changed' tab and click the green 'Review' button there. There you find the 'Approve' button.
Sybren A. Stüvel force-pushed anim/deduplicate-keys from a563cb2b94 to 97f74b634d 2023-04-24 11:34:41 +02:00 Compare
Nathan Vegdahl approved these changes 2023-04-24 11:45:52 +02:00
Nathan Vegdahl left a comment
Collaborator

Looks ready to land to me. :-)

Looks ready to land to me. :-)
Sybren A. Stüvel force-pushed anim/deduplicate-keys from 97f74b634d to d747dd1087 2023-04-24 12:06:12 +02:00 Compare
Poster
Collaborator

👍 I just pushed again to ensure that there's only three commits left (refactor, add test for existing code, add the rest), no actual code changes.

👍 I just pushed again to ensure that there's only three commits left (refactor, add test for existing code, add the rest), no actual code changes.
Poster
Collaborator

@blender-bot build

@blender-bot build
Sybren A. Stüvel manually merged commit 85ed2e8c36 into main 2023-04-24 12:34:28 +02:00
Wayde Moss approved these changes 2023-04-24 16:03:52 +02:00
Sybren A. Stüvel deleted branch anim/deduplicate-keys 2023-05-15 11:27:40 +02:00
Sybren A. Stüvel removed this from the Animation & Rigging project 2023-05-15 11:35:10 +02:00
Howard Trickey referenced this issue from a commit 2023-05-29 02:51:44 +02:00

Reviewers

Nathan Vegdahl approved these changes 2023-04-24 11:45:52 +02:00
Wayde Moss approved these changes 2023-04-24 16:03:52 +02:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest: Wayland
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#107089
There is no content yet.