Anim: implement 3D viewport keyframing functionality for the Animation data-block #120428

Closed
Nathan Vegdahl wants to merge 21 commits from nathanvegdahl/blender:baklava_keyframing into main

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

(Continues and supersedes #119669)

This PR has been abandoned in favor of splitting the same changes into multiple smaller PRs:

  • #121517: Various functions in the keyframing call stack were changed to return SingleKeyingResult, so
    that the failure cases for the new animation system can be propagated and reported to the user.
  • #121525: Moved the only-insert-needed logic into insert_vert_fcurve(), since that function is the
    common code path between the old and new animation system, and thus we can avoid duplicating
    that logic. It also just generally seems like a reasonable place for it to be.
  • #121476: Simplified the autokeying code around "only insert available" so that both the old and new
    animation system take the same top-level code path.
  • #121661: implement 3D viewport keyframing functionality for the new animation system.

TODO

  • Need to check the experimental flag before inserting keys to the Animation datablock.
  • Error collection via CombinedKeyingResult is currently mostly stubbed out, and needs to be properly filled in.
  • Error collection isn't done properly yet in the lower-level keying functions, like KeyframeStrip::keyframe_insert().
    • Note that even lower-level keying functions that predate the new animation system still don't propagate errors properly yet. Threading error propagation through the entire keyframing stack will be a separate project.
  • Some Dependency Graph update is missing because you need to move keys before they affect the viewport.
  • While auto-keying works, the flag check for "Only insert needed" and "Only insert available" does not.

For future PRs (previously part of TODO above):

  • Don't hard-code keyframing settings in the new animation system's keyframing code path.
  • Keyingsets do not work yet.
  • Inserting keys from e.g. the properties panel does not work yet.
  • Inserting keys from the graph editor does not work yet.
  • Inserting keys from the dopesheet editor does not work yet.
  • Datablock name should go through translation. Currently it is hardcoded to "Animation".
  • Refactor: insert_key_rna() should likely be merged with insert_keyframe().
  • Refactor: insert_key_rna() currently keys all elements of an array property (e.g. object location), but should instead take each element of the property separately to allow individual keying.
(Continues and supersedes #119669) This PR has been abandoned in favor of splitting the same changes into multiple smaller PRs: - #121517: Various functions in the keyframing call stack were changed to return `SingleKeyingResult`, so that the failure cases for the new animation system can be propagated and reported to the user. - #121525: Moved the only-insert-needed logic into `insert_vert_fcurve()`, since that function is the common code path between the old and new animation system, and thus we can avoid duplicating that logic. It also just generally seems like a reasonable place for it to be. - #121476: Simplified the autokeying code around "only insert available" so that both the old and new animation system take the same top-level code path. - #121661: implement 3D viewport keyframing functionality for the new animation system. ## TODO - [x] Need to check the experimental flag before inserting keys to the Animation datablock. - [x] Error collection via `CombinedKeyingResult` is currently mostly stubbed out, and needs to be properly filled in. - [x] Error collection isn't done properly yet in the lower-level keying functions, like `KeyframeStrip::keyframe_insert()`. - Note that even lower-level keying functions that predate the new animation system still don't propagate errors properly yet. Threading error propagation through the entire keyframing stack will be a separate project. - [x] Some Dependency Graph update is missing because you need to move keys before they affect the viewport. - [x] While auto-keying works, the flag check for "Only insert needed" and "Only insert available" does not. ## For future PRs (previously part of TODO above): - [ ] Don't hard-code keyframing settings in the new animation system's keyframing code path. - [ ] Keyingsets do not work yet. - [ ] Inserting keys from e.g. the properties panel does not work yet. - [ ] Inserting keys from the graph editor does not work yet. - [ ] Inserting keys from the dopesheet editor does not work yet. - [ ] Datablock name should go through translation. Currently it is hardcoded to "Animation". - [ ] Refactor: `insert_key_rna()` should likely be merged with `insert_keyframe()`. - [ ] Refactor: `insert_key_rna()` currently keys all elements of an array property (e.g. object location), but should instead take each element of the property separately to allow individual keying.
Nathan Vegdahl added this to the Animation & Rigging project 2024-04-09 14:08:58 +02:00
Nathan Vegdahl added the
Module
Animation & Rigging
label 2024-04-09 14:09:05 +02:00
Nathan Vegdahl force-pushed baklava_keyframing from 6179a5d1ac to a75d2d5cb5 2024-04-11 16:49:16 +02:00 Compare
Nathan Vegdahl added 1 commit 2024-04-30 15:32:28 +02:00
Nathan Vegdahl added 1 commit 2024-04-30 16:04:46 +02:00
Nathan Vegdahl added 2 commits 2024-04-30 16:32:41 +02:00
Nathan Vegdahl added 1 commit 2024-04-30 16:35:06 +02:00
Nathan Vegdahl added 1 commit 2024-04-30 17:40:37 +02:00
This still leaves even lower-level code not returning propert failure
results, but one step at a time.
Nathan Vegdahl added 1 commit 2024-04-30 17:53:03 +02:00
Nathan Vegdahl changed title from WIP: Anim: implement keyframing functionality for the Animation data-block to WIP: Anim: implement 3D viewport keyframing functionality for the Animation data-block 2024-05-01 14:09:37 +02:00
Nathan Vegdahl added 1 commit 2024-05-01 15:43:06 +02:00
Nathan Vegdahl added 1 commit 2024-05-01 16:19:17 +02:00
Nathan Vegdahl added 1 commit 2024-05-02 12:30:34 +02:00
Nathan Vegdahl added 1 commit 2024-05-02 12:47:58 +02:00
In the last commit I made `insert_vert_fcurves()` actually use
the `INSERTKEY_NEEDED` flag, whereas previously it was ignored.
However, there was a call site that was already passing
`INSERTKEY_NEEDED` to `insert_vert_fcurve()` despite it previously
having no effect.  In order to preserve behavior, this commit replaces
that with `INSERTKEY_NOFLAGS`.
Nathan Vegdahl added 1 commit 2024-05-02 13:00:59 +02:00
At this point it was literally the same signature, and just calling
the identical method on KeyframeStrip.  We can always reintroduce it
in the future when it makes sense.
Nathan Vegdahl reviewed 2024-05-02 14:21:16 +02:00
@ -493,3 +533,3 @@
for (n = 1, fp = value_cache; n < range && fp; n++, fp++) {
blender::animrig::insert_vert_fcurve(
fcu, {fp->frame, fp->val}, settings, eInsertKeyFlags(1));
fcu, {fp->frame, fp->val}, settings, INSERTKEY_NOFLAGS);
Author
Member

Note to reviewers: this is changed to INSERTKEY_NOFLAGS because the flag previously passed was actually not used by insert_vert_fcurve(), but now is. So this actually preserves the previous behavior.

This same change is elsewhere in this PR as well, for the same reason.

Note to reviewers: this is changed to `INSERTKEY_NOFLAGS` because the flag previously passed was actually not used by `insert_vert_fcurve()`, but now is. So this actually preserves the previous behavior. This same change is elsewhere in this PR as well, for the same reason.
Nathan Vegdahl reviewed 2024-05-02 15:23:19 +02:00
@ -1071,2 +1072,4 @@
rna_tag_animation_update(bmain, id);
bool replace;
const int index = BKE_fcurve_bezt_binarysearch_index(fcu->bezt, frame, fcu->totvert, &replace);
Author
Member

Note to reviewers: this is indeed a bit less efficient (though not too bad: just another binary search). In the future we can change insert_vert_fcurve() to return a "result" type of some kind, to allow returning the index again without preventing the error code from being propagated on failure.

Note to reviewers: this is indeed a bit less efficient (though not too bad: just another binary search). In the future we can change `insert_vert_fcurve()` to return a ["result" type of some kind](https://devtalk.blender.org/t/developer-discussion-handling-errors/33054), to allow returning the index again without preventing the error code from being propagated on failure.
Nathan Vegdahl added 2 commits 2024-05-02 15:25:31 +02:00
Misc cleanup
Some checks failed
buildbot/vexp-code-patch-lint Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
733a8d2632
Nathan Vegdahl changed title from WIP: Anim: implement 3D viewport keyframing functionality for the Animation data-block to Anim: implement 3D viewport keyframing functionality for the Animation data-block 2024-05-02 15:26:45 +02:00
Nathan Vegdahl requested review from Sybren A. Stüvel 2024-05-02 15:28:57 +02:00
Nathan Vegdahl requested review from Christoph Lendenfeld 2024-05-02 15:29:08 +02:00
Author
Member

@blender-bot build

@blender-bot build
Christoph Lendenfeld requested changes 2024-05-02 16:47:01 +02:00
Christoph Lendenfeld left a comment
Member

I found an issue with "Only Insert Available" with this PR.

to reproduce:

  • auto keying and "Only insert available" enabled.
  • press I to create keyframes on all channels of the default cube
  • delete keys of rotation
  • rotate cube -> gets rotation keys

This seems to be because channels are not deleted when the last key is deleted, opposed to the behavior of the old Action

I found an issue with "Only Insert Available" with this PR. to reproduce: * auto keying and "Only insert available" enabled. * press `I` to create keyframes on all channels of the default cube * delete keys of rotation * rotate cube -> gets rotation keys This seems to be because channels are not deleted when the last key is deleted, opposed to the behavior of the old Action
@ -443,1 +444,4 @@
SingleKeyingResult result_loc_a = key_strip.keyframe_insert(
binding, "location", 0, {1.0f, 47.0f}, INSERTKEY_NOFLAGS, settings);
ASSERT_EQ(SingleKeyingResult::SUCCESS, result_loc_a)
<< "Expect all the necessary data structures to be created on insertion of a key";

This description isn't really fitting anymore.
I'd expect something more like Expected keyframe insertion to be successful

This description isn't really fitting anymore. I'd expect something more like `Expected keyframe insertion to be successful`
Author
Member

To be honest, I don't think it was a fitting description even before. But I think the message here probably communicates the intent of what it's supposed to be testing, so maybe instead of changing the message I can change the test to match the message?

To be honest, I don't think it was a fitting description even before. But I think the message here probably communicates the intent of what it's *supposed* to be testing, so maybe instead of changing the message I can change the test to match the message?

you are already doing that further down by checking the amount of FCurves created, which is why I thought it is enough to just modify this message

you are already doing that further down by checking the amount of FCurves created, which is why I thought it is enough to just modify this message
Author
Member

Ah, that's a good point. Done.

Ah, that's a good point. Done.
nathanvegdahl marked this conversation as resolved
@ -291,1 +340,3 @@
return -1;
/* TODO: we need more info from `insert_bezt_fcurve()` called above to
* return a more specific failure. */
return SingleKeyingResult::GENERIC_FAILURE;

could be possible to add a FCURVE_KEYING_FAILED.
Depends on if you plan to use the generic failure more often in the future.
Currently it is only used here though

could be possible to add a `FCURVE_KEYING_FAILED`. Depends on if you plan to use the generic failure more often in the future. Currently it is only used here though
Author
Member

I'm using it as a marker for "we can't do proper error propagation here yet". The idea is that it should be completely removed in the future once error handling is properly threaded down to all the lower-level keying functions.

I'm using it as a marker for "we can't do proper error propagation here yet". The idea is that it should be completely removed in the future once error handling is properly threaded down to all the lower-level keying functions.

fair point :)

fair point :)
Author
Member

Maybe renaming it to UNKNOWN_FAILURE would be good, since that's the case wherever it's used: I only use it where we haven't propagated enough information to that site yet to know what caused keying to fail.

Maybe renaming it to `UNKNOWN_FAILURE` would be good, since that's the case wherever it's used: I only use it where we haven't propagated enough information to that site yet to know what caused keying to fail.
nathanvegdahl marked this conversation as resolved
@ -1060,2 +1169,3 @@
AnimData *adt = BKE_animdata_from_id(id);
if (USER_EXPERIMENTAL_TEST(&U, use_animation_baklava) && (adt->animation || !adt->action)) {
/* TODO: Don't hardcode key settings. */

that comment needs to move down

that comment needs to move down
nathanvegdahl marked this conversation as resolved
@ -43,6 +43,11 @@ static eInsertKeyFlags get_autokey_flags(Scene *scene)
flag |= INSERTKEY_NEEDED;
}
/* Only insert available. */

I don't think the changes to the autokeying code should be part of this PR.
While it's a really good change, I don't see how this is related to the Animation datablock.

I don't think the changes to the autokeying code should be part of this PR. While it's a really good change, I don't see how this is related to the Animation datablock.
Author
Member

That's totally fair. However, I did need to change something, because the old code was structured such that if there was an Animation datablock (as opposed to an action) and AUTOKEY_FLAG_INSERTAVAILABLE was set, it would always return early without doing anything.

The alternative way to do this (at least that occurred to me) without changing the structure of the code is to add an additional branch inside the if (is_keying_flag(scene, AUTOKEY_FLAG_INSERTAVAILABLE)) { block, to do different things depending on whether it's an action or an animation datablock. That would more-or-less end up duplicating the code that's at the end of the function anyway, but maybe that's indeed best for this PR, and then I can make a separate PR that changes things to this version.

Does that sound good?

That's totally fair. However, I did need to change *something*, because the old code was structured such that if there was an Animation datablock (as opposed to an action) and `AUTOKEY_FLAG_INSERTAVAILABLE` was set, it would *always* return early without doing anything. The alternative way to do this (at least that occurred to me) without changing the structure of the code is to add an additional branch inside the `if (is_keying_flag(scene, AUTOKEY_FLAG_INSERTAVAILABLE)) {` block, to do different things depending on whether it's an action or an animation datablock. That would more-or-less end up duplicating the code that's at the end of the function anyway, but maybe that's indeed best for this PR, and then I can make a separate PR that changes things to this version. Does that sound good?

what speaks against extracting that part of the PR and landing it beforehand? Might not be possible but that would be the cleanest option.

what speaks against extracting that part of the PR and landing it beforehand? Might not be possible but that would be the cleanest option.
Author
Member

Ah! Fair point. Yeah, I'll do that. Thanks for the suggestion. :-)

Ah! Fair point. Yeah, I'll do that. Thanks for the suggestion. :-)
Author
Member

Here's a separate PR for that:
#121476

Here's a separate PR for that: https://projects.blender.org/blender/blender/pulls/121476
nathanvegdahl marked this conversation as resolved
Nathan Vegdahl added 1 commit 2024-05-02 17:40:23 +02:00
Author
Member

This seems to be because channels are not deleted when the last key is deleted, opposed to the behavior of the old Action

Yeah, I ran into that as well. I don't think it's clear whether this is a bug in this PR vs the keyframe deletion code, though.

We basically need to decide whether fcurves should be removed when their last key is (like they are with actions), or whether it's okay for them to stay behind and the semantics of "Only Insert Available" need to change. (Or we could do both!) I'm fine either way, but I figured that's something we can address separately from this PR.

> This seems to be because channels are not deleted when the last key is deleted, opposed to the behavior of the old Action Yeah, I ran into that as well. I don't think it's clear whether this is a bug in this PR vs the keyframe deletion code, though. We basically need to decide whether fcurves should be removed when their last key is (like they are with actions), or whether it's okay for them to stay behind and the semantics of "Only Insert Available" need to change. (Or we could do both!) I'm fine either way, but I figured that's something we can address separately from this PR.
Nathan Vegdahl added 1 commit 2024-05-06 15:53:55 +02:00
This helps make it feel a little worse to use, which is good because
we want to remove it in the future (as explained in the comment).
Nathan Vegdahl added 1 commit 2024-05-06 15:56:01 +02:00
Sybren A. Stüvel requested changes 2024-05-07 14:13:37 +02:00
Sybren A. Stüvel left a comment
Member

I found an issue with "Only Insert Available" with this PR.

Personally I'm fine with "available" meaning "there already is an F-Curve for this property", or in other words "insertion of the key can happen without creating a new F-Curve". So from that perspective, it seems this PR is doing the right thing.

As part of that work, the following changes were also made:

  • Various functions in the keyframing call stack were changed to return SingleKeyingResult, so
    that the failure cases for the new animation system can be propagated and reported to the user.
  • Moved the only-insert-needed logic into insert_vert_fcurve(), since that function is the
    common code path between the old and new animation system, and thus we can avoid duplicating
    that logic. It also just generally seems like a reasonable place for it to be.

Could this be extracted to another PR, that'll be landed prior to this one?

  • Simplified the autokeying code around "only insert available" so that both the old and new
    animation system take the same top-level code path.

I think this was already extracted into another PR?

From what I can see (but I may be wrong) it could also be possible to move the handling of key insertion flags by the Animation datablock code into a separate PR? That could already be a nice improvement for keying via the Python API.

> I found an issue with "Only Insert Available" with this PR. Personally I'm fine with "available" meaning "there already is an F-Curve for this property", or in other words "insertion of the key can happen without creating a new F-Curve". So from that perspective, it seems this PR is doing the right thing. > As part of that work, the following changes were also made: > - Various functions in the keyframing call stack were changed to return `SingleKeyingResult`, so > that the failure cases for the new animation system can be propagated and reported to the user. > - Moved the only-insert-needed logic into `insert_vert_fcurve()`, since that function is the > common code path between the old and new animation system, and thus we can avoid duplicating > that logic. It also just generally seems like a reasonable place for it to be. Could this be extracted to another PR, that'll be landed prior to this one? > - Simplified the autokeying code around "only insert available" so that both the old and new > animation system take the same top-level code path. I think this was already extracted into another PR? From what I can see (but I may be wrong) it could also be possible to move the handling of key insertion flags by the `Animation` datablock code into a separate PR? That could already be a nice improvement for keying via the Python API.
@ -72,0 +87,4 @@
/**
* Return whether a new fcurve can be created according to the given keyframing
* flags.

I think a concrete example would help here:

INSERTKEY_REPLACE and INSERTKEY_AVAILABLE prohibit the creation of new F-Curves.

The generic naming (this is just in the animrig:: namespace) also threw me off a bit, as it's always possible to create a new FCurve. Maybe something like key_insertion_may_create_fcurve()?

I think a concrete example would help here: > `INSERTKEY_REPLACE` and `INSERTKEY_AVAILABLE` prohibit the creation of new F-Curves. The generic naming (this is just in the `animrig::` namespace) also threw me off a bit, as it's always possible to create a new FCurve. Maybe something like `key_insertion_may_create_fcurve()`?
nathanvegdahl marked this conversation as resolved
@ -646,3 +646,3 @@
BLI_assert(StringRef(this->name).size() >= name_length_min);
/* Avoid accessing an uninitialized part of the string accidentally. */
/* Avoid accessing an uninitialised part of the string accidentally. */

Commit such unrelated corrections separately. But also I think this was spelled correctly?

Commit such unrelated corrections separately. But also I think this was [spelled correctly](https://www.oxfordlearnersdictionaries.com/definition/english/initialize?q=initialized)?
Author
Member

Yeah, not sure how that happened. Thanks for catching that!

Yeah, not sure how that happened. Thanks for catching that!
nathanvegdahl marked this conversation as resolved
@ -878,3 +872,1 @@
array_index,
binding.name);
return nullptr;
/* Get the fcurve, or if one doesn't exist and the keying flags allow then

Replace "then" with a comma and add one after "or":

Get the fcurve, or, if one doesn't exist and the keying flags allow, create one.

Or maybe just reorder and simplify:

Get the fcurve, or create one if the keying flags allow.

Replace "then" with a comma and add one after "or": > Get the fcurve, or, if one doesn't exist and the keying flags allow, create one. Or maybe just reorder and simplify: > Get the fcurve, or create one if the keying flags allow.
nathanvegdahl marked this conversation as resolved
@ -454,1 +455,3 @@
<< "Expect same (binding/rna path/array index) tuple to return the same FCurve.";
SingleKeyingResult result_loc_b = key_strip.keyframe_insert(
binding, "location", 0, {5.0f, 47.1f}, INSERTKEY_NOFLAGS, settings);
ASSERT_EQ(SingleKeyingResult::SUCCESS, result_loc_b);

I think this should be an EXPECT_EQ as it's not a necessity for the rest of the code to not crash.

I think this should be an `EXPECT_EQ` as it's not a necessity for the rest of the code to not crash.
nathanvegdahl marked this conversation as resolved
@ -455,0 +457,4 @@
ASSERT_EQ(SingleKeyingResult::SUCCESS, result_loc_b);
ASSERT_EQ(1, channels->fcurves().size()) << "Expect insertion with the same (binding/rna "
"path/array index) tuple to go into the same FCurve";
ASSERT_EQ(2, channels->fcurves()[0]->totvert)

I think this should be an EXPECT_EQ as it's not a necessity for the rest of the code to not crash.

Same with similar asserts below.

I think this should be an `EXPECT_EQ` as it's not a necessity for the rest of the code to not crash. Same with similar asserts below.
nathanvegdahl marked this conversation as resolved
@ -455,0 +458,4 @@
ASSERT_EQ(1, channels->fcurves().size()) << "Expect insertion with the same (binding/rna "
"path/array index) tuple to go into the same FCurve";
ASSERT_EQ(2, channels->fcurves()[0]->totvert)
<< "Expect insertion with the same (binding/rna "

I think this code is wrapped a bit weird (or at least inconsistent with the very similar code above). Probably best to un-split the string and let the auto-formatter do its job. I think it can auto-split, but not auto-unsplit.

I think this code is wrapped a bit weird (or at least inconsistent with the very similar code above). Probably best to un-split the string and let the auto-formatter do its job. I think it can auto-split, but not auto-unsplit.
nathanvegdahl marked this conversation as resolved
@ -77,0 +86,4 @@
* this? If it's never supposed to happen, an assert would be better. If
* it *is* supposed to happen, then printf doesn't seem like the right way
* to report it, if it should be reported at all. */
printf("ERROR: Couldn't add AnimData (ID = %s)\n", (id) ? (id->name) : "<None>");

This is supposed to happen, as not all IDs are animatable. But it's kinda moot, given that this function will likely disappear when you update this PR for #121357.

This _is_ supposed to happen, as not all IDs are animatable. But it's kinda moot, given that this function will likely disappear when you update this PR for #121357.
nathanvegdahl marked this conversation as resolved
@ -278,1 +290,4 @@
*/
static bool new_key_needed(FCurve *fcu, const float frame, const float value)
{
if (fcu == nullptr) {

I don't think passing fcu = nullptr is a valid use case, just as calling insert_vert_fcurve(fcu=nullptr, ...) is not valid.

Use a reference FCurve &fcu instead. And since this is just a query, and not changing fcu, making it const would be appropriate.

...

I only now saw that this code was just move verbatim from another file. Fine to keep as-is for PR simplicity sake, but IMO this should be addressed at some point.

I don't think passing `fcu = nullptr` is a valid use case, just as calling `insert_vert_fcurve(fcu=nullptr, ...)` is not valid. Use a reference `FCurve &fcu` instead. And since this is just a query, and not changing `fcu`, making it `const` would be appropriate. ... I only now saw that this code was just move verbatim from another file. Fine to keep as-is for PR simplicity sake, but IMO this should be addressed at some point.
Author
Member

For posterity: fcu cannot be made const, because the functions that this function calls don't take it as const (even though they themselves are just query functions).

Hopefully we can follow the call chain and fix that all up as a separate PR relatively straightforwardly, as long as it's not too much of a tangled web. Fingers crossed.

For posterity: `fcu` cannot be made `const`, because the functions that this function calls don't take it as `const` (even though they themselves are just query functions). Hopefully we can follow the call chain and fix that all up as a separate PR relatively straightforwardly, as long as it's not too much of a tangled web. Fingers crossed.
Author
Member

Took a bit of time to see what it would take to make this const: #121788

Took a bit of time to see what it would take to make this const: #121788
nathanvegdahl marked this conversation as resolved
@ -92,6 +97,17 @@ void CombinedKeyingResult::generate_reports(ReportList *reports)
}
Vector<std::string> errors;
if (this->get_count(SingleKeyingResult::UNKNOWN_FAILURE) > 0) {

Check the discussion about this in #animation-module. We'll have to simplify these messages to just a single string that covers both singular & plural.

"Could not insert {:d} key(s) for unknown reasons."

Check the discussion about this in `#animation-module`. We'll have to simplify these messages to just a single string that covers both singular & plural. `"Could not insert {:d} key(s) for unknown reasons."`
Author
Member

Got it. If that gets addressed for the other report messages in main first, then I'll update these ones as well before merging. Otherwise I think it makes sense to merge as-is, and then they can all be changed together.

Got it. If that gets addressed for the other report messages in main first, then I'll update these ones as well before merging. Otherwise I think it makes sense to merge as-is, and then they can all be changed together.
nathanvegdahl marked this conversation as resolved
@ -1045,0 +1071,4 @@
const eInsertKeyFlags insert_key_flags,
const KeyframeSettings &key_settings)
{
if (layer.strips().size() == 0) {

Currently this code assumes that if a strip exists, it's the right one to insert keys into. However, it doesn't document this assumption, and doesn't have any guards to check that the assumption holds.

I think it's better for the Layer itself to determine which Strip the key is supposed to go into. Something like Layer::strip_for_keys(key_data.position[0]). Or, if we want to separate these concerns (which I actually think might be a nice idea), it should at least be a separate class / function from the "insert keys into layer" function. That way this insert_key_layer() function can be reduced to two steps:

  1. Get the Strip that should receive the keys
  2. Insert keys into that Strip.

Or, since the data model is now still so simple, just have a function ensure_layer_strip_for_keying(anim) that returns references to the Layer and Strip. That one function can then have the assumptions & logic for "create if not exists" in one place, and the rest of the code is more straight-forward.


Replace .size() == 0 with .is_empty()

Currently this code assumes that if a strip exists, it's the right one to insert keys into. However, it doesn't document this assumption, and doesn't have any guards to check that the assumption holds. I think it's better for the `Layer` itself to determine which `Strip` the key is supposed to go into. Something like `Layer::strip_for_keys(key_data.position[0])`. Or, if we want to separate these concerns (which I actually think might be a nice idea), it should at least be a separate class / function from the "insert keys into layer" function. That way this `insert_key_layer()` function can be reduced to two steps: 1. Get the Strip that should receive the keys 2. Insert keys into that Strip. Or, since the data model is now still so simple, just have a function `ensure_layer_strip_for_keying(anim)` that returns references to the Layer and Strip. That one function can then have the assumptions & logic for "create if not exists" in one place, and the rest of the code is more straight-forward. --------- Replace `.size() == 0` with `.is_empty()`
Author
Member

Something like Layer::strip_for_keys(key_data.position[0]).

Yeah, I think that's a really good idea. I feel like it shouldn't even need to be specific to keyframing. Maybe Layer::strip_at_time(...). We'll need to reassess that interface when we introduce overlapping strips, but putting it in one clear place will make it easy to see what impacts that change will have when we make it.

> Something like Layer::strip_for_keys(key_data.position[0]). Yeah, I think that's a really good idea. I feel like it shouldn't even need to be specific to keyframing. Maybe `Layer::strip_at_time(...)`. We'll need to reassess that interface when we introduce overlapping strips, but putting it in one clear place will make it easy to see what impacts that change will have when we make it.
nathanvegdahl marked this conversation as resolved
@ -1045,0 +1094,4 @@
ID *id = rna_pointer->owner_id;
CombinedKeyingResult combined_result;
Binding *binding = anim.binding_for_id(*id);

It's probably better to use anim.find_suitable_binding_for(*id) here. That uses the same logic as anim.assign_id() to find a suitable binding. I'll write a TODO for myself to document that API better (once #121357 lands, I don't want to create Git conflicts now).

Also I feel that "prepare this ID for being keyed with this Animation" should be done in a separate function. That can then also be properly documented about the post-call guarantees it makes.

It's probably better to use `anim.find_suitable_binding_for(*id)` here. That uses the same logic as `anim.assign_id()` to find a suitable binding. I'll write a TODO for myself to document that API better (once #121357 lands, I don't want to create Git conflicts now). Also I feel that "prepare this ID for being keyed with this Animation" should be done in a separate function. That can then also be properly documented about the post-call guarantees it makes.
nathanvegdahl marked this conversation as resolved
@ -1045,0 +1100,4 @@
const bool success = anim.assign_id(binding, *id);
if (!success) {
/* TODO: count the rna paths properly (e.g. accounting for multi-element properties). */
combined_result.add(SingleKeyingResult::NO_VALID_BINDING, rna_paths.size());

With a new Binding, the only reason Action::assign_id() can return false is when the ID itself cannot be animated. In that case I feel that NO_VALID_BINDING is a misleading thing to report, even when it's technically correct.

It's probably better to check that the ID can be animated far before this function gets called, and then use BLI_assert_msg() to check for this more locally.

With a new Binding, the only reason `Action::assign_id()` can return `false` is when the ID itself cannot be animated. In that case I feel that `NO_VALID_BINDING` is a misleading thing to report, even when it's technically correct. It's probably better to check that the ID can be animated far before this function gets called, and then use `BLI_assert_msg()` to check for this more locally.
nathanvegdahl marked this conversation as resolved
@ -1045,0 +1107,4 @@
Layer *layer;
if (anim.layers().size() == 0) {
layer = &anim.layer_add("Layer 0");

Similar to the code above, I don't think that "preparing the Animation for key insertion" should be done by this function.

Similar to the code above, I don't think that "preparing the Animation for key insertion" should be done by this function.
nathanvegdahl marked this conversation as resolved
@ -1045,0 +1113,4 @@
layer = anim.layer(0);
}
if (layer == nullptr) {

This can only happen when anim.layer(0) returns nullptr, which should never happen. I think it's safe to turn this into a BLI_assert().

This can only happen when `anim.layer(0)` returns `nullptr`, which should never happen. I think it's safe to turn this into a `BLI_assert()`.
nathanvegdahl marked this conversation as resolved
@ -1045,0 +1120,4 @@
}
for (const std::string &rna_path : rna_paths) {
const bool visual_keyframing = insert_key_flags & INSERTKEY_MATRIX;

Booleans typically start with use_, do_, is_ etc. In this case use_visual_keyframing would be better IMO.

Booleans typically start with `use_`, `do_`, `is_` etc. In this case `use_visual_keyframing` would be better IMO.
nathanvegdahl marked this conversation as resolved
@ -1045,0 +1126,4 @@
const bool path_resolved = RNA_path_resolve_property(
rna_pointer, rna_path.c_str(), &ptr, &prop);
if (!path_resolved) {
combined_result.add(SingleKeyingResult::CANNOT_RESOLVE_PATH);

Print to stdout which path could not be resolved. That'll make debugging things considerably easier.

Print to stdout which path could not be resolved. That'll make debugging things considerably easier.
nathanvegdahl marked this conversation as resolved
@ -1045,0 +1129,4 @@
combined_result.add(SingleKeyingResult::CANNOT_RESOLVE_PATH);
continue;
}
const std::optional<std::string> rna_path_id_to_prop = RNA_path_from_ID_to_property(&ptr,

This std::optional<> isn't checked for emptyness, so the .value() call below may crash.

This `std::optional<>` isn't checked for emptyness, so the `.value()` call below may crash.
nathanvegdahl marked this conversation as resolved
@ -1045,0 +1131,4 @@
}
const std::optional<std::string> rna_path_id_to_prop = RNA_path_from_ID_to_property(&ptr,
prop);
Vector<float> rna_values = get_keyframe_values(&ptr, prop, visual_keyframing);

Since the visual_keyframing constant is only used here, either move it here (to clarify the location of its use) or move it out of the loop (to clarify it's loop-invariant).

Since the `visual_keyframing` constant is only used here, either move it here (to clarify the location of its use) or move it out of the loop (to clarify it's loop-invariant).
nathanvegdahl marked this conversation as resolved
@ -1045,0 +1138,4 @@
key_data.array_index = property_index;
key_data.position = {scene_frame, rna_values[property_index]};
const SingleKeyingResult result = insert_key_layer(
*layer, *binding, rna_path_id_to_prop.value(), key_data, insert_key_flags, key_settings);

I got a bit confused by the .value() call, as above there's also rna_values. It's mostly me (I should stop this review for now and go home), but try out *rna_path_id_to_prop instead, to see how that looks for you.

I got a bit confused by the `.value()` call, as above there's also `rna_values`. It's mostly me (I should stop this review for now and go home), but try out `*rna_path_id_to_prop` instead, to see how that looks for you.
Author
Member

*rna_path_id_to_prop looks like a pointer dereference to me. In this case it's close to where rna_path_id_to_prop is defined, so it's fairly obvious what's actually going on. But IMO being explicit with .value() makes it clearer in the general case, and is the convention I'd prefer we follow.

Having said that, that also comes from my Rust background and the (nearly) equivalent .unwrap() call. So maybe it's just because it's what I'm used to.

`*rna_path_id_to_prop` looks like a pointer dereference to me. In this case it's close to where `rna_path_id_to_prop` is defined, so it's fairly obvious what's actually going on. But IMO being explicit with `.value()` makes it clearer in the general case, and is the convention I'd prefer we follow. Having said that, that also comes from my Rust background and the (nearly) equivalent `.unwrap()` call. So maybe it's just because it's what I'm used to.

I read * as "the value of what this thing points to", which also applies to std::optional<>. I usually use if (!optional) { handle no-value case } and *optional to get the value. But also I don't have a strong opinion here, apart from the already-mentioned confusion between "the value of the optional" and "the RNA values".

Personally I'd prefer Rust's .unwrap() over .value(), as the latter is a bit too generic for my taste ;-)

I read `*` as "the value of what this thing points to", which also applies to `std::optional<>`. I usually use `if (!optional) { handle no-value case }` and `*optional` to get the value. But also I don't have a strong opinion here, apart from the already-mentioned confusion between "the value of the optional" and "the RNA values". Personally I'd prefer Rust's `.unwrap()` over `.value()`, as the latter is a bit too generic for my taste ;-)
Author
Member

apart from the already-mentioned confusion between "the value of the optional" and "the RNA values".

Yeah, that's a good point. And indeed, "value" is quite generic compared to "unwrap".

(unwrap() also has slightly different semantics than value(): it actually moves/extracts the contained value out, rather than just providing access to it. With the way Rust's move semantics work, this works out really nicely. But it's not totally clear to me whether it could work the same way in C++ without being a giant foot gun. And it's moot in any case, since it doesn't work that way.)

I read * as "the value of what this thing points to", which also applies to std::optional<>.

For me that's exactly why it feels weird: std::optional doesn't point to anything, it contains something. But to counter my own point there, I think there's an alternative framing: * means something like "resolve indirection". And I think that applies well enough to std::optional: you only have indirect access to the contained value.

I think part of my gut feeling here also comes from the correspondence between std::optional and a hypothetical Result type in the future. I suppose Result could also use * to access the success value, and only accessing the error would be done via an explicit method call. But I like the explicitness of things like result.is_ok(), result.unwrap(), and result.unwrap_err() in Rust.

In any case, I think for now going with * is fine. And we can see how things shake out in the future, and if it starts to feel weird once we have a Result type.

> apart from the already-mentioned confusion between "the value of the optional" and "the RNA values". Yeah, that's a good point. And indeed, "value" is quite generic compared to "unwrap". (`unwrap()` also has slightly different semantics than `value()`: it actually moves/extracts the contained value out, rather than just providing access to it. With the way Rust's move semantics work, this works out *really* nicely. But it's not totally clear to me whether it could work the same way in C++ without being a giant foot gun. And it's moot in any case, since it *doesn't* work that way.) > I read `*` as "the value of what this thing points to", which also applies to `std::optional<>`. For me that's exactly why it feels weird: `std::optional` doesn't point to anything, it *contains* something. But to counter my own point there, I think there's an alternative framing: `*` means something like "resolve indirection". And I think that applies well enough to `std::optional`: you only have indirect access to the contained value. I think part of my gut feeling here also comes from the correspondence between `std::optional` and a hypothetical `Result` type in the future. I suppose `Result` could also use `*` to access the success value, and only accessing the error would be done via an explicit method call. But I like the explicitness of things like `result.is_ok()`, `result.unwrap()`, and `result.unwrap_err()` in Rust. In any case, I think for now going with `*` is fine. And we can see how things shake out in the future, and if it starts to feel weird once we have a `Result` type.
nathanvegdahl marked this conversation as resolved
@ -289,3 +262,1 @@
}
}
}
/* Optimization: if there's no animation at all and "Only Insert Available" is

Same question as above: is this really necessary, given that it creates sub-optimal reporting?

Same question as above: is this really necessary, given that it creates sub-optimal reporting?
nathanvegdahl marked this conversation as resolved
Author
Member

Closing in favor of #121661, after splitting off a bunch of parts of this into other PRs:

  • #121476 Anim: simplify object/pose autokeying code
  • #121517: Anim: add failure propagation to more lower-level keying functions
  • #121525: Refactor: centralize responsibility for "Only Insert Needed"

(I'm doing it this way to reduce the chances of gitea losing the context for all of the comments I already got on this PR so far.)

Closing in favor of #121661, after splitting off a bunch of parts of this into other PRs: - #121476 Anim: simplify object/pose autokeying code - #121517: Anim: add failure propagation to more lower-level keying functions - #121525: Refactor: centralize responsibility for "Only Insert Needed" (I'm doing it this way to reduce the chances of gitea losing the context for all of the comments I already got on this PR so far.)
Nathan Vegdahl closed this pull request 2024-05-10 17:59:40 +02:00

Pull request closed

Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
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
Interest: X11
Legacy
Asset Browser Project
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
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#120428
No description provided.