Cleanup: Refactor BKE_nlatrack_add to multiple methods to handle adding a new NLA track to the track list. Insert before, after, head, and tail #104929

Merged
Nate Rupsis merged 8 commits from nrupsis/blender:T82241-cleanup-NLATrack_insert into main 2023-03-02 15:02:42 +01:00
Member

This PR removes the BKE_nlatrack_add, and adds new:

  • BKE_nlatrack_insert_before
  • BKE_nlatrack_insert_after
  • BKE_nlatrack_new_before_and_set_active
  • BKE_nlatrack_new_after_and_set_active
  • BKE_nlatrack_new_head_and_set_active
  • BKE_nlatrack_new_tail_and_set_active

methods to easily add new NLA tracks in relation to the existing track list.

This PR removes the BKE_nlatrack_add, and adds new: * BKE_nlatrack_insert_before * BKE_nlatrack_insert_after * BKE_nlatrack_new_before_and_set_active * BKE_nlatrack_new_after_and_set_active * BKE_nlatrack_new_head_and_set_active * BKE_nlatrack_new_tail_and_set_active methods to easily add new NLA tracks in relation to the existing track list.
Nate Rupsis added the
Module
Animation & Rigging
label 2023-02-18 21:22:03 +01:00
Nate Rupsis added 1 commit 2023-02-18 21:22:05 +01:00
Nate Rupsis requested review from Sybren A. Stüvel 2023-02-18 21:22:15 +01:00
Brecht Van Lommel added this to the Animation & Rigging project 2023-02-20 10:39:41 +01:00
Sybren A. Stüvel requested changes 2023-02-20 11:34:29 +01:00
Sybren A. Stüvel left a comment
Member

Having those functions is way clearer than the old BKE_nlatrack_add. I also like how this separates the code from the AnimData struct.

Pondering further on the API, I'm not a fan of bool parameters. Having to add bool is_liboverride everywhere seems like an invasion of one Blender feature into another, and creates way too tight coupling. What would you think of an approach like this?

  • Give BKE_nlatrack_new() a parameter bool is_liboverride, so that it can properly set (or not) the NLATRACK_OVERRIDELIBRARY_LOCAL flag.
  • Add a function bool BKE_nlatrack_is_liboverride(const struct NlaTrack *nla_track) that uses this flag to determine whether or not it's a liboverride.
  • In the other functions that take a struct NlaTrack *new_track, remove the bool is_liboverride and call BKE_nlatrack_is_liboverride() instead.

The bool is_liboverride argument is also rather un-documented, which the above should partially address by simply removing it altogether ;-) Where it is still left, be sure to document well what is actually "being the override".

Having those functions is way clearer than the old `BKE_nlatrack_add`. I also like how this separates the code from the `AnimData` struct. Pondering further on the API, I'm not a fan of `bool` parameters. Having to add `bool is_liboverride` everywhere seems like an invasion of one Blender feature into another, and creates way too tight coupling. What would you think of an approach like this? - Give `BKE_nlatrack_new()` a parameter `bool is_liboverride`, so that it can properly set (or not) the `NLATRACK_OVERRIDELIBRARY_LOCAL` flag. - Add a function `bool BKE_nlatrack_is_liboverride(const struct NlaTrack *nla_track)` that uses this flag to determine whether or not it's a liboverride. - In the other functions that take a `struct NlaTrack *new_track`, remove the `bool is_liboverride` and call `BKE_nlatrack_is_liboverride()` instead. The `bool is_liboverride` argument is also rather un-documented, which the above should partially address by simply removing it altogether ;-) Where it is still left, be sure to document well **what** is actually "being the override".
@ -37,1 +37,4 @@
/**
* Create new NLA Track.
*/

This is one of those moments where I'm like.... wait, there was no function for this yet? 🤯

Anyway, also add a sentence like "The caller owns the returned pointer" for extra clarity.

Might also be good to document this with the functions that take such a pointer and add it to some list. What happens to the ownership of that pointer? Does it get owned by the owner of the list?

This is one of those moments where I'm like.... wait, there was no function for this yet? 🤯 Anyway, also add a sentence like "The caller owns the returned pointer" for extra clarity. Might also be good to document this with the functions that take such a pointer and add it to some list. What happens to the ownership of that pointer? Does it get owned by the owner of the list?
nrupsis marked this conversation as resolved
@ -93,0 +130,4 @@
* Calls #BKE_nlatrack_new to create a new NLA track, inserts it as the head of the
* NLA track list with #BKE_nlatrack_new_before_and_set_active, and sets it as active.
*/
struct NlaTrack *BKE_nlatrack_new_head_and_set_active(ListBase *nla_tracks, bool is_liboverride);

API-wise I think it's nicer to separate the BKE_nlatrack_new_xxx functions from the _and_set_active part. Having the functions a bit more atomic makes the calling part a bit more verbose, but I think it's a good call. Also the fact that the unit test doesn't cover the "and set active" part of the functionality might be an indication that the function is doing too much.

API-wise I think it's nicer to separate the `BKE_nlatrack_new_xxx` functions from the `_and_set_active` part. Having the functions a bit more atomic makes the calling part a bit more verbose, but I think it's a good call. Also the fact that the unit test doesn't cover the "and set active" part of the functionality might be an indication that the function is doing too much.
Author
Member

Considering we already have the BKE_nlatrack_set_active method, it would be easy to refactor that.

Would it be worth modifying:

  • BKE_nlatrack_new_before_and_set_active -> BKE_nlatrack_new_before
  • BKE_nlatrack_new_after_and_set_active -> BKE_nlatrack_new_after
    etc?

The only difference between these modified methods, and BKE_nlatrack_insert_before / after, is that we call BKE_nlatrack_new. Would it also be worth removing those compositional methods and leaving it on the user to create a new NLA Track?

Alternatively, we can create a unit test for BKE_nlatrack_set_active, and from the documentation for BKE_nlatrack_new_before_and_set_active, etc reference that method?

Considering we already have the `BKE_nlatrack_set_active method`, it would be easy to refactor that. Would it be worth modifying: * `BKE_nlatrack_new_before_and_set_active` -> `BKE_nlatrack_new_before` * `BKE_nlatrack_new_after_and_set_active` -> `BKE_nlatrack_new_after` etc? The only difference between these modified methods, and `BKE_nlatrack_insert_before` / after, is that we call `BKE_nlatrack_new`. Would it also be worth removing those compositional methods and leaving it on the user to create a new NLA Track? Alternatively, we can create a unit test for `BKE_nlatrack_set_active`, and from the documentation for `BKE_nlatrack_new_before_and_set_active`, etc reference that method?

The only difference between these modified methods, and BKE_nlatrack_insert_before / after, is that we call BKE_nlatrack_new. Would it also be worth removing those compositional methods and leaving it on the user to create a new NLA Track?

Hmmm that does make me think (in both directions -- i.e. doubting myself whether my original comment maybe pushed things too far, or whether we should push it even further than that). I think the important aspect here is that (as least AFAIK) an NLA track by itself doesn't do much. It should be in the NLA and not left 'floating' somewhere on the heap. As such, I think composite functions 'create & put in a useful place' are still good to keep around.

'Being the active one' makes no sense if it's a track by itself -- it only has meaning in the context of a list of tracks. You wouldn't use BKE_nlatrack_set_active() without BKE_nlatrack_new_before(), but I can see a use for BKE_nlatrack_new_before() without BKE_nlatrack_set_active().

Would that be a good enough reason to keep the 'new-and-insert' composites, and pull out the 'set active' part?

> The only difference between these modified methods, and `BKE_nlatrack_insert_before` / after, is that we call `BKE_nlatrack_new`. Would it also be worth removing those compositional methods and leaving it on the user to create a new NLA Track? Hmmm that does make me think (in both directions -- i.e. doubting myself whether my original comment maybe pushed things too far, or whether we should push it even further than that). I think the important aspect here is that (as least AFAIK) an NLA track by itself doesn't do much. It should be in the NLA and not left 'floating' somewhere on the heap. As such, I think composite functions 'create & put in a useful place' are still good to keep around. 'Being the active one' makes no sense if it's a track by itself -- it only has meaning in the context of a list of tracks. You wouldn't use `BKE_nlatrack_set_active()` without `BKE_nlatrack_new_before()`, but I can see a use for `BKE_nlatrack_new_before()` without `BKE_nlatrack_set_active()`. Would that be a good enough reason to keep the 'new-and-insert' composites, and pull out the 'set active' part?
Author
Member

I've had a lot of back and forth on this myself.

I think ultimately you're right. The and_set_active aspect of these methods are doing too much. I think having it be the responsibility of the caller isn't too much to ask for, especially if it's how we do it everywhere, it'll be easy to understand the pattern.

I also think the BKE_nlatrack_new_before/after/head/tail methods are a good way of easily understanding where a new track is going to placed. In theory, it could also allow us to add some more "placement" option when adding in new tracks (without needing them active), but unsure if we'd ever want that.

I've had a lot of back and forth on this myself. I think ultimately you're right. The `and_set_active` aspect of these methods are doing too much. I think having it be the responsibility of the caller isn't too much to ask for, especially if it's how we do it everywhere, it'll be easy to understand the pattern. I also think the `BKE_nlatrack_new_before/after/head/tail` methods are a good way of easily understanding where a new track is going to placed. In theory, it could also allow us to add some more "placement" option when adding in new tracks (without needing them active), but unsure if we'd ever want that.
nrupsis marked this conversation as resolved
@ -354,0 +354,4 @@
{
if (is_liboverride) {
/** Currently, all library override tracks are assumed to be grouped together at the start of

No need to use doxygen (/**) inside functions.

No need to use doxygen (`/**`) inside functions.
nrupsis marked this conversation as resolved
@ -354,0 +360,4 @@
BKE_nlatrack_insert_after(nla_tracks, next, new_track, is_liboverride);
return;
}
}

These nested conditions are getting complicated. The "So we can only add" seems to suggest that if this is not the case, the track cannot be added. However, there is no return then, so that means that it gets added as usual.

On one hand it's good that the track always gets added, because that's what this function is for, but I'm not 100% convinced it's the right thing to do here.

It would also be good to have the special-casing for liboverrides documented in the header file.

These nested conditions are getting complicated. The "So we can **only** add" seems to suggest that if this is **not** the case, the track cannot be added. However, there is no `return` then, so that means that it gets added as usual. On one hand it's good that the track always gets added, because that's what this function is for, but I'm not 100% convinced it's the right thing to do here. It would also be good to have the special-casing for liboverrides documented in the header file.
nrupsis marked this conversation as resolved
@ -354,0 +381,4 @@
struct NlaTrack *new_track,
bool is_liboverride)
{
BLI_assert(!ELEM(NULL, nla_tracks, new_track));

I think it's better to separate those out, so that when things go wrong it's clear which pointer was NULL:

BLI_assert(nla_tracks);
BLI_assert(new_track);
I think it's better to separate those out, so that when things go wrong it's clear which pointer was `NULL`: ```c BLI_assert(nla_tracks); BLI_assert(new_track); ```
nrupsis marked this conversation as resolved
@ -116,4 +114,38 @@ TEST(nla_track, BKE_nlatrack_remove_and_free)
EXPECT_EQ(-1, BLI_findindex(&adt.nla_tracks, track1));
}
TEST(nla_track, BKE_nlatrack_new_tail_and_set_active)

There should be separate tests for situations with liboverride tracks. The code to handle those is quite complex, so should be covered by tests.

There should be separate tests for situations with liboverride tracks. The code to handle those is quite complex, so should be covered by tests.
nrupsis marked this conversation as resolved
@ -576,1 +576,3 @@
NlaTrack *new_track = BKE_nlatrack_add(adt, track, ID_IS_OVERRIDE_LIBRARY(id));
NlaTrack *new_track;
if (track != NULL) {

Flip the condition and the if/else bodies. That removes a negation, making the code slightly simpler.

Flip the condition and the `if`/`else` bodies. That removes a negation, making the code slightly simpler.
nrupsis marked this conversation as resolved
Author
Member

Having those functions is way clearer than the old BKE_nlatrack_add. I also like how this separates the code from the AnimData struct.

Pondering further on the API, I'm not a fan of bool parameters. Having to add bool is_liboverride everywhere seems like an invasion of one Blender feature into another, and creates way too tight coupling. What would you think of an approach like this?

  • Give BKE_nlatrack_new() a parameter bool is_liboverride, so that it can properly set (or not) the NLATRACK_OVERRIDELIBRARY_LOCAL flag.
  • Add a function bool BKE_nlatrack_is_liboverride(const struct NlaTrack *nla_track) that uses this flag to determine whether or not it's a liboverride.
  • In the other functions that take a struct NlaTrack *new_track, remove the bool is_liboverride and call BKE_nlatrack_is_liboverride() instead.

The bool is_liboverride argument is also rather un-documented, which the above should partially address by simply removing it altogether ;-) Where it is still left, be sure to document well what is actually "being the override".

I love this idea. I was actually wondering how I could have removed is_liboverride all together. But without being familiar with the override system, i figured I'd do more harm than good 😁

But I like this approach.

> Having those functions is way clearer than the old `BKE_nlatrack_add`. I also like how this separates the code from the `AnimData` struct. > > Pondering further on the API, I'm not a fan of `bool` parameters. Having to add `bool is_liboverride` everywhere seems like an invasion of one Blender feature into another, and creates way too tight coupling. What would you think of an approach like this? > > - Give `BKE_nlatrack_new()` a parameter `bool is_liboverride`, so that it can properly set (or not) the `NLATRACK_OVERRIDELIBRARY_LOCAL` flag. > - Add a function `bool BKE_nlatrack_is_liboverride(const struct NlaTrack *nla_track)` that uses this flag to determine whether or not it's a liboverride. > - In the other functions that take a `struct NlaTrack *new_track`, remove the `bool is_liboverride` and call `BKE_nlatrack_is_liboverride()` instead. > > The `bool is_liboverride` argument is also rather un-documented, which the above should partially address by simply removing it altogether ;-) Where it is still left, be sure to document well **what** is actually "being the override". I love this idea. I was actually wondering how I could have removed `is_liboverride` all together. But without being familiar with the override system, i figured I'd do more harm than good 😁 But I like this approach.
Nate Rupsis added 5 commits 2023-03-02 04:34:28 +01:00
Author
Member

After and offline conversation with @dr.sybren, we've determined that the existing code / functionality around the NLA Track overrides needs to be reevaluated.

It's outside the scope of this patch, and I've created a design task to review / refactor: #105272

After and offline conversation with @dr.sybren, we've determined that the existing code / functionality around the NLA Track overrides needs to be reevaluated. It's outside the scope of this patch, and I've created a design task to review / refactor: https://projects.blender.org/blender/blender/issues/105272
Sybren A. Stüvel reviewed 2023-03-02 11:54:58 +01:00
@ -93,0 +113,4 @@
/**
* Calls #BKE_nlatrack_new to create a new NLA track, inserts it before the
* given NLA track with #BKE_nlatrack_insert_before, and sets it as active.

No longer sets as active ;-)

No longer sets as active ;-)
nrupsis marked this conversation as resolved
Nate Rupsis added 2 commits 2023-03-02 14:58:03 +01:00
Sybren A. Stüvel approved these changes 2023-03-02 15:00:12 +01:00
Sybren A. Stüvel left a comment
Member

👍

:+1:
Nate Rupsis merged commit 81b56f8bbc into main 2023-03-02 15:02:42 +01:00
Nate Rupsis deleted branch T82241-cleanup-NLATrack_insert 2023-03-02 15:02:43 +01:00
Sybren A. Stüvel removed this from the Animation & Rigging project 2023-03-02 17:05:54 +01: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
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
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
2 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#104929
No description provided.