Anim: Bake Channel operator #111263

Open
Christoph Lendenfeld wants to merge 30 commits from ChrisLend/blender:bake_channel_operator into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.

This is a replacement for the workflow that uses "Bake Curve" and "Unbake Curve" to quickly generate dense key data.

Compared to the existing workflow it has the advantage of allowing the user more control over the key types, and distance between keys, as well as the frame range affected.

Operator options

  • Range: the range that will be baked. Defaults to the scene range or preview range.
  • Step: Distance between keyframes. Can be used to bake on 2s or even bake to subframes.
  • Remove Existing Keys: Enum option to do one of the following
    -- Keep existing keys
    -- Remove keys within the range
    -- Remove keys outside of the range
    -- Remove all keys
  • Interpolation Type: Either keep the current key type or replace it with a different type, e.g. Constant
  • Bake Modifiers: If true bakes the effect of the modifier stack to keys and deletes the modifier stack. If false, the code disables the modifiers before baking, so the resulting keys will behave as if the modifiers didn't exist

The operator can be found in the Graph Editor under Channel->Bake Channels


Part of this task
#111050: Better Baking in the Graph Editor

This is a replacement for the workflow that uses "Bake Curve" and "Unbake Curve" to quickly generate dense key data. Compared to the existing workflow it has the advantage of allowing the user more control over the key types, and distance between keys, as well as the frame range affected. Operator options * Range: the range that will be baked. Defaults to the scene range or preview range. * Step: Distance between keyframes. Can be used to bake on 2s or even bake to subframes. * Remove Existing Keys: Enum option to do one of the following -- Keep existing keys -- Remove keys within the range -- Remove keys outside of the range -- Remove all keys * Interpolation Type: Either keep the current key type or replace it with a different type, e.g. Constant * Bake Modifiers: If true bakes the effect of the modifier stack to keys and deletes the modifier stack. If false, the code disables the modifiers before baking, so the resulting keys will behave as if the modifiers didn't exist The operator can be found in the Graph Editor under `Channel->Bake Channels` ------ Part of this task [#111050: Better Baking in the Graph Editor](https://projects.blender.org/blender/blender/issues/111050)
Christoph Lendenfeld added the
Module
Animation & Rigging
label 2023-08-18 14:48:42 +02:00
Christoph Lendenfeld added 4 commits 2023-08-18 14:48:54 +02:00
Christoph Lendenfeld changed title from Animation: Bake Channel operator to WIP: Animation: Bake Channel operator 2023-08-18 14:49:02 +02:00
Author
Member

@blender-bot package

@blender-bot package
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR111263) when ready.
Christoph Lendenfeld added 2 commits 2023-09-01 11:07:02 +02:00
Christoph Lendenfeld added 1 commit 2023-09-01 11:11:38 +02:00
Christoph Lendenfeld changed title from WIP: Animation: Bake Channel operator to Animation: Bake Channel operator 2023-09-01 11:16:49 +02:00
Christoph Lendenfeld requested review from Brad Clark 2023-09-01 11:17:25 +02:00
Christoph Lendenfeld requested review from Nathan Vegdahl 2023-09-01 11:17:36 +02:00
Christoph Lendenfeld added 1 commit 2023-09-01 11:18:54 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
8e784d4331
rename key_type to interpolation_type
Christoph Lendenfeld added this to the Animation & Rigging project 2023-09-01 11:19:06 +02:00
Author
Member

@blender-bot package

@blender-bot package
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR111263) when ready.
Christoph Lendenfeld added 1 commit 2023-09-12 11:05:45 +02:00
Christoph Lendenfeld added 2 commits 2023-09-14 12:11:38 +02:00
Author
Member

@blender-bot package

@blender-bot package
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR111263) when ready.
Christoph Lendenfeld added 2 commits 2023-09-15 11:22:46 +02:00
Author
Member

@blender-bot package

@blender-bot package
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR111263) when ready.
Christoph Lendenfeld changed title from Animation: Bake Channel operator to Anim: Bake Channel operator 2023-09-15 11:57:19 +02:00
Christoph Lendenfeld added 1 commit 2023-09-21 10:49:30 +02:00
Member

Found a couple of bugs while testing:

  • If you set the start range > end range, Blender crashes.
  • If you set the frame step <= 0.0, Blender freezes.

And I have a couple of questions about functionality:

  • This appears to treat the frame range as exclusive on the right. I think we may want it to be inclusive on both sides, because right now it just feels like the last key doesn't get baked. This is particularly jarring when "Remove Existing" is set to "All".
  • Regarding the "Remove Existing" option, it's not clear to me why someone would ever not want to remove the in-range keys, since the point is to bake that range instead. I get that with the frame step set > 1 it has an effect. And functionally it's not a problem to have the option, of course. But I was initially confused by the set of options because of this, and I think it might be confusing to others as well. So if there's not a strong use case, maybe we can change the option to only be about removing the out-of-range keys or not.
Found a couple of bugs while testing: - If you set the start range > end range, Blender crashes. - If you set the frame step <= 0.0, Blender freezes. And I have a couple of questions about functionality: - This appears to treat the frame range as exclusive on the right. I think we may want it to be inclusive on both sides, because right now it just feels like the last key doesn't get baked. This is particularly jarring when "Remove Existing" is set to "All". - Regarding the "Remove Existing" option, it's not clear to me why someone would ever *not* want to remove the in-range keys, since the point is to bake that range instead. I get that with the frame step set > 1 it has an effect. And functionally it's not a problem to have the option, of course. But I was initially confused by the set of options because of this, and I think it might be confusing to others as well. So if there's not a strong use case, maybe we can change the option to only be about removing the out-of-range keys or not.
Christoph Lendenfeld added 1 commit 2023-09-21 16:25:29 +02:00
Christoph Lendenfeld added 1 commit 2023-09-21 17:01:58 +02:00
Christoph Lendenfeld added 1 commit 2023-09-21 17:08:55 +02:00
Author
Member

thanks for having a look at it

  • If you set the start range > end range, Blender crashes.

fixed that by clamping the range

  • If you set the frame step <= 0.0, Blender freezes.

The hardmin is 0.01 so it will get quite slow on high frame count. But I am not sure what the minimum number should be. I thought about setting it to 0.1 (so 10 subframes) but it feels like an arbitrary number so I kept it at 0.01 because I don't see anyone wanting more than 100 subframes. Users that venture into that realm better be careful. Let me know what you think

  • This appears to treat the frame range as exclusive on the right. I think we may want it to be inclusive on both sides, because right now it just feels like the last key doesn't get baked. This is particularly jarring when "Remove Existing" is set to "All".

yes good point. Will make it inclusive

  • Regarding the "Remove Existing" option, it's not clear to me why someone would ever not want to remove the in-range keys, since the point is to bake that range instead. I get that with the frame step set > 1 it has an effect. And functionally it's not a problem to have the option, of course. But I was initially confused by the set of options because of this, and I think it might be confusing to others as well. So if there's not a strong use case, maybe we can change the option to only be about removing the out-of-range keys or not.

Made the default to remove in range.
Still kept the option to not remove any keys. While I don't have an immediate use case, it feels more complete to have it, and it doesn't add much complexity.

thanks for having a look at it > - If you set the start range > end range, Blender crashes. fixed that by clamping the range > - If you set the frame step <= 0.0, Blender freezes. The hardmin is 0.01 so it will get quite slow on high frame count. But I am not sure what the minimum number should be. I thought about setting it to 0.1 (so 10 subframes) but it feels like an arbitrary number so I kept it at 0.01 because I don't see anyone wanting more than 100 subframes. Users that venture into that realm better be careful. Let me know what you think > - This appears to treat the frame range as exclusive on the right. I think we may want it to be inclusive on both sides, because right now it just feels like the last key doesn't get baked. This is particularly jarring when "Remove Existing" is set to "All". yes good point. Will make it inclusive > - Regarding the "Remove Existing" option, it's not clear to me why someone would ever *not* want to remove the in-range keys, since the point is to bake that range instead. I get that with the frame step set > 1 it has an effect. And functionally it's not a problem to have the option, of course. But I was initially confused by the set of options because of this, and I think it might be confusing to others as well. So if there's not a strong use case, maybe we can change the option to only be about removing the out-of-range keys or not. Made the default to remove in range. Still kept the option to not remove any keys. While I don't have an immediate use case, it feels more complete to have it, and it doesn't add much complexity.
Nathan Vegdahl requested changes 2023-09-21 17:12:38 +02:00
Nathan Vegdahl left a comment
Member

Generally looking good! But there are some changes I'd like.

Generally looking good! But there are some changes I'd like.
@ -4326,0 +4327,4 @@
static const EnumPropertyItem channel_bake_key_options[] = {
{CHANNEL_BAKE_KEEP, "KEEP", 0, "Keep", "Keep the current key types"},
{BEZT_IPO_BEZ, "BEZIER", 0, "Bezier", "New keys will be beziers"},
Member

I'd prefer new constants for this enum, rather than awkwardly having just one that's new and defined arbitrarily high to avoid conflicting with the others. It's not as convenient in other code, because then you have to map between the two sets of constants later. But it feels cleaner to me that way.

I'd prefer new constants for this enum, rather than awkwardly having just one that's new and defined arbitrarily high to avoid conflicting with the others. It's not as convenient in other code, because then you have to map between the two sets of constants later. But it feels cleaner to me that way.
Author
Member

not sure I agree with this. It would add a level of indirection to the enum which I think isn't needed. Atm it is quite clear what key types the options correspond to since they use the same constants.

I agree it does feel a bit weird, but I feel like it is the better option

not sure I agree with this. It would add a level of indirection to the enum which I think isn't needed. Atm it is quite clear what key types the options correspond to since they use the same constants. I agree it does feel a bit weird, but I feel like it is the better option
Member

That's fair. Let's leave it as-is, then. Thanks!

That's fair. Let's leave it as-is, then. Thanks!
nathanvegdahl marked this conversation as resolved
@ -4326,0 +4449,4 @@
/* Identifiers */
ot->name = "Bake Channels";
ot->idname = "ANIM_OT_channels_bake";
ot->description = "Create keyframes on the F-Curves of selected channels";
Member

This description feels non-specific to me. There are a lot of operators that create key frames.

On the other hand, I'm struggling to think of a succinct alternative that doesn't suffer similar problems.

This description feels non-specific to me. There are a lot of operators that create key frames. On the other hand, I'm struggling to think of a succinct alternative that doesn't suffer similar problems.
Author
Member

I think I made it better
Create keyframes following the current shape of F-Curves of selected channels
The distinction to other keyframe adding operators is that this one looks at the shape of the current curve, so I think it makes sense to mention that

I think I made it better `Create keyframes following the current shape of F-Curves of selected channels` The distinction to other keyframe adding operators is that this one looks at the shape of the current curve, so I think it makes sense to mention that
Member

Yeah, that's great. Thanks!

Yeah, that's great. Thanks!
nathanvegdahl marked this conversation as resolved
@ -4326,0 +4480,4 @@
RNA_def_enum(ot->srna,
"remove_existing",
channel_bake_remove_options,
int(BakeCurveRemove::REMOVE_NONE),
Member

If we do keep these options as-are, I think REMOVE_IN_RANGE is a better default. I don't think people usually want to leave behind the old keys in the range being baked.

If we do keep these options as-are, I think `REMOVE_IN_RANGE` is a better default. I don't think people usually want to leave behind the old keys in the range being baked.
nathanvegdahl marked this conversation as resolved
@ -4326,0 +4482,4 @@
channel_bake_remove_options,
int(BakeCurveRemove::REMOVE_NONE),
"Remove Existing Keys",
"If enabled removes the keys that currently make up the curve");
Member

This description reads like it's a boolean option.

This description reads like it's a boolean option.
Author
Member

indeed it used to be a boolean. changed it to be more representative of the enum

indeed it used to be a boolean. changed it to be more representative of the enum
nathanvegdahl marked this conversation as resolved
Member

While that's true, this is also accidentally quadratic, because on every removal (BKE_fcurve_delete_key) it has to shift all the keys after the removed key back. If you iterate forward and keep track of the gap as it's created, you can shift keys over the gap as you go for linear runtime.

While that's true, this is also accidentally quadratic, because on every removal (`BKE_fcurve_delete_key`) it has to shift all the keys after the removed key back. If you iterate forward and keep track of the gap as it's created, you can shift keys over the gap as you go for linear runtime.
Member

Oops, I think a change got pushed while I was reviewing, so these last two comments got detached. I'll resubmit them.

Oops, I think a change got pushed while I was reviewing, so these last two comments got detached. I'll resubmit them.
nathanvegdahl marked this conversation as resolved
Member

This is also accidentally quadratic (actually slightly worse, because it additionally does a binary search to find where to insert each key), and does an allocation and full copy of the f-curve's entire bezt data on every insert.

However, this one is a bit dicier (but certainly not impossible) to make linear. So maybe it's better to leave this one as an optimization for later.

This is also accidentally quadratic (actually slightly worse, because it additionally does a binary search to find where to insert each key), and does an allocation and full copy of the f-curve's entire bezt data on every insert. However, this one is a bit dicier (but certainly not impossible) to make linear. So maybe it's better to leave this one as an optimization for later.
nathanvegdahl marked this conversation as resolved
Nathan Vegdahl requested changes 2023-09-21 17:18:01 +02:00
Nathan Vegdahl left a comment
Member

(Re-attached comments.)

(Re-attached comments.)
@ -1219,0 +1222,4 @@
const BakeCurveRemove removal_mode)
{
/* Iterating backwards to not cause issues because the bezt array is modified during the
* loop. */
Member

While that's true, this is also accidentally quadratic, because on every removal (BKE_fcurve_delete_key) it has to shift all the keys after the removed key back. If you iterate forward and keep track of the gap as it's created, you can shift keys over the gap as you go for linear runtime.

While that's true, this is also accidentally quadratic, because on every removal (BKE_fcurve_delete_key) it has to shift all the keys after the removed key back. If you iterate forward and keep track of the gap as it's created, you can shift keys over the gap as you go for linear runtime.
Author
Member

solved by adding a new function BKE_fcurve_delete_keys that removes a range of keys in one swoop

solved by adding a new function `BKE_fcurve_delete_keys` that removes a range of keys in one swoop
nathanvegdahl marked this conversation as resolved
@ -1219,0 +1265,4 @@
}
for (int i = 0; i < sample_count; i++) {
insert_vert_fcurve(
Member

This is also accidentally quadratic (actually slightly worse, because it additionally does a binary search to find where to insert each key), and does an allocation and full copy of the f-curve's entire bezt data on every insert.

However, this one is a bit dicier (but certainly not impossible) to make linear. So maybe it's better to leave this one as an optimization for later.

This is also accidentally quadratic (actually slightly worse, because it additionally does a binary search to find where to insert each key), and does an allocation and full copy of the f-curve's entire bezt data on every insert. However, this one is a bit dicier (but certainly not impossible) to make linear. So maybe it's better to leave this one as an optimization for later.
Author
Member

I ran it through a profiler. 90% of the time was spent on BKE_fcurve_recalc_handles
I changed the flag to INSERTKEY_FAST so it doesn't recalculate on every key insert
Now 83% is spent on __memcpy_avx_unaligned_erms which I have no Idea where that comes from

I ran it through a profiler. 90% of the time was spent on `BKE_fcurve_recalc_handles` I changed the flag to `INSERTKEY_FAST` so it doesn't recalculate on every key insert Now 83% is spent on `__memcpy_avx_unaligned_erms` which I have no Idea where that comes from
Member

The hardmin is 0.01 so it will get quite slow on high frame count. But I am not sure what the minimum number should be. I thought about setting it to 0.1 (so 10 subframes) but it feels like an arbitrary number so I kept it at 0.01 because I don't see anyone wanting more than 100 subframes. Users that venture into that realm better be careful. Let me know what you think

Ooooh, got it. Yeah, since you have to manually type it in for < 1.0, that's probably fine.

With some testing, I also suspect this is running into the quadratic performance I mentioned in the code review. Just counting seconds off, the time it takes to bake seems to increase super-linearly with the number of baked keys as the step size gets smaller.

Made the default to remove in range.
Still kept the option to not remove any keys. While I don't have an immediate use case, it feels more complete to have it, and it doesn't add much complexity.

My concern isn't the complexity, but rather that it makes the drop-down initially confusing (or at least it was to me). But maybe it could be made clearer by just changing the naming a bit? Like, "In Range" -> "Old In Range". And/or maybe it would be easier to grok if it was two boolean options ("Remove Keys In Range" and "Remove Keys Outside Range") rather than a single four-way option.

> The hardmin is 0.01 so it will get quite slow on high frame count. But I am not sure what the minimum number should be. I thought about setting it to 0.1 (so 10 subframes) but it feels like an arbitrary number so I kept it at 0.01 because I don't see anyone wanting more than 100 subframes. Users that venture into that realm better be careful. Let me know what you think Ooooh, got it. Yeah, since you have to manually type it in for < 1.0, that's probably fine. With some testing, I also suspect this is running into the quadratic performance I mentioned in the code review. Just counting seconds off, the time it takes to bake seems to increase super-linearly with the number of baked keys as the step size gets smaller. > Made the default to remove in range. > Still kept the option to not remove any keys. While I don't have an immediate use case, it feels more complete to have it, and it doesn't add much complexity. My concern isn't the complexity, but rather that it makes the drop-down initially confusing (or at least it was to me). But maybe it could be made clearer by just changing the naming a bit? Like, "In Range" -> "Old In Range". And/or maybe it would be easier to grok if it was two boolean options ("Remove Keys In Range" and "Remove Keys Outside Range") rather than a single four-way option.
Christoph Lendenfeld added 1 commit 2023-09-22 11:15:08 +02:00
Christoph Lendenfeld added 1 commit 2023-09-22 11:35:43 +02:00
Christoph Lendenfeld added 1 commit 2023-09-22 11:48:02 +02:00
Christoph Lendenfeld added 1 commit 2023-09-22 11:54:22 +02:00
Christoph Lendenfeld added 1 commit 2023-09-22 12:30:45 +02:00
Christoph Lendenfeld added 2 commits 2023-09-22 15:02:17 +02:00
Author
Member

@nathanvegdahl I addressed the review comments but I am not quite ready for another review. The operator just runs too poorly on heavy scenes. The profiler tells me its the key insertion that eats the time and it makes sense since for every insert it reallocates the bezt array and copies data over.
I will look into a better way

@nathanvegdahl I addressed the review comments but I am not quite ready for another review. The operator just runs too poorly on heavy scenes. The profiler tells me its the key insertion that eats the time and it makes sense since for every insert it reallocates the bezt array and copies data over. I will look into a better way
Member

The profiler tells me its the key insertion that eats the time and it makes sense since for every insert it reallocates the bezt array and copies data over.

That makes sense.

I was thinking a good approach might be to make a new function that takes two fcurves and merges them into a new one. That way all the complexity can be contained there (and it seems like the kind of utility function that would be generally useful). Then in the baking code you could start by creating two fcurves: the fcurve with the desired keys removed, and the fcurve with the new baked keys. And then you finish by simply merging them.

> The profiler tells me its the key insertion that eats the time and it makes sense since for every insert it reallocates the bezt array and copies data over. That makes sense. I was thinking a good approach might be to make a new function that takes two fcurves and merges them into a new one. That way all the complexity can be contained there (and it seems like the kind of utility function that would be generally useful). Then in the baking code you could start by creating two fcurves: the fcurve with the desired keys removed, and the fcurve with the new baked keys. And then you finish by simply merging them.
Christoph Lendenfeld added 1 commit 2023-09-26 09:16:29 +02:00
Christoph Lendenfeld added 1 commit 2023-11-17 10:12:14 +01:00
Christoph Lendenfeld added 4 commits 2023-11-17 12:44:29 +01:00
Author
Member

@nathanvegdahl I implemented a function BKE_bezier_array_merge. Now the thing runs on my heavy test scene.
It's not realtime, but profiling it, most of the time is taken up by recalculating the handles. The merge function is insanely fast. I was quite surprised by that. The first block on the left above bake_fcurve is the merge

It might be possible to use that in other places as well.

image

@nathanvegdahl I implemented a function `BKE_bezier_array_merge`. Now the thing runs on my heavy test scene. It's not realtime, but profiling it, most of the time is taken up by recalculating the handles. The merge function is insanely fast. I was quite surprised by that. The first block on the left above bake_fcurve is the merge It might be possible to use that in other places as well. ![image](/attachments/789cb2ec-e38e-43c1-b51d-b06c06d61ac6)
Christoph Lendenfeld requested review from Nathan Vegdahl 2023-11-17 12:47:52 +01:00
Christoph Lendenfeld added this to the 4.1 milestone 2023-11-17 12:52:09 +01:00
Author
Member

@blender-bot package

@BClark can you review this once more. Should be super fast now, even on heavy scenes

@blender-bot package @BClark can you review this once more. Should be super fast now, even on heavy scenes
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR111263) when ready.
Member

Wow it works great!!!! That is so nice to have the control over the modifier bake and to be able to replace keys!!!!

Wow it works great!!!! That is so nice to have the control over the modifier bake and to be able to replace keys!!!!
Some checks failed
buildbot/vexp-code-patch-coordinator Build done.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u bake_channel_operator:ChrisLend-bake_channel_operator
git checkout ChrisLend-bake_channel_operator
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
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
Overlay
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
Interest
Workbench
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 Assignees
4 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#111263
No description provided.