GPv3: send notifiers and undo push when changing the active layer #110378

Merged
Pratik Borhade merged 7 commits from PratikPB2123/blender:gpv3-rna-prop-active-layer-index into main 2023-07-27 11:37:53 +02:00
Member

Use RNA to update the active layer and also do an undo
push in on_activate function of tree-view. This is now
possible after recent refactor: 741c684bf6, 2e9bc6373c

Part of: #110133

Use RNA to update the active layer and also do an undo push in `on_activate` function of tree-view. This is now possible after recent refactor: 741c684bf6, 2e9bc6373c Part of: https://projects.blender.org/blender/blender/pulls/110133
Pratik Borhade added 1 commit 2023-07-22 16:05:22 +02:00
55107d6954 GPv3: RNA property to set active layer
Use "active_index" RNA property to change active layer. This will allow
to sen updates to other regions when active layer is changed and also
registers undo step.

Logic of this property is similar to legay GP.
Right now GPv3 is using `tree_row_click_fn/on_activate` function to change
the active layer.
Pratik Borhade requested review from Falk David 2023-07-22 16:05:30 +02:00
Pratik Borhade added the
Module
Grease Pencil
label 2023-07-22 16:05:46 +02:00
Pratik Borhade added this to the Grease Pencil project 2023-07-22 16:05:53 +02:00
Falk David requested changes 2023-07-24 10:39:59 +02:00
Falk David left a comment
Member

Some comments.

Some comments.
@ -117,0 +117,4 @@
static int rna_Grease_Pencil_active_layer_index_get(PointerRNA *ptr)
{
GreasePencil *grease_pencil = rna_grease_pencil(ptr);
int val = grease_pencil->layers().first_index(grease_pencil->get_active_layer());
Member

grease_pencil->get_active_layer can return a nullptr I think in this case we should return something like -1.

`grease_pencil->get_active_layer` can return a `nullptr` I think in this case we should return something like `-1`.
@ -117,0 +118,4 @@
{
GreasePencil *grease_pencil = rna_grease_pencil(ptr);
int val = grease_pencil->layers().first_index(grease_pencil->get_active_layer());
printf("something get");
Member

Remove debug print.

Remove debug print.
@ -117,0 +126,4 @@
{
using namespace blender::bke::greasepencil;
GreasePencil *grease_pencil = rna_grease_pencil(ptr);
blender::Span<const Layer *> layers = grease_pencil->layers();
Member

No need for a local variable for the span. const Layer *layer = grease_pencil->layers()[value]; is fine.

No need for a local variable for the span. `const Layer *layer = grease_pencil->layers()[value];` is fine.
filedescriptor marked this conversation as resolved
Pratik Borhade added 1 commit 2023-07-24 11:54:32 +02:00
Falk David requested changes 2023-07-24 12:07:40 +02:00
Falk David left a comment
Member

One more comment (my bad).

One more comment (my bad).
@ -117,0 +118,4 @@
{
GreasePencil *grease_pencil = rna_grease_pencil(ptr);
if (grease_pencil->get_active_layer() == nullptr) {
Member

My bad, we have a function for this (which reads a bit nicer) -> has_active_layer(). So if (!grease_pencil->has_active_layer()) {

My bad, we have a function for this (which reads a bit nicer) -> `has_active_layer()`. So `if (!grease_pencil->has_active_layer()) {`
Pratik Borhade added 1 commit 2023-07-24 12:19:42 +02:00
Member

This works well, but I'm a bit unsure about the active_index property. It feels like this should be done by setting the layers.active layer pointer instead.
E.g. setting layers.active_index to an invalid index crashes Blender right now.

I'd like @JulianEisel opinion here.

This works well, but I'm a bit unsure about the `active_index` property. It feels like this should be done by setting the `layers.active` layer pointer instead. E.g. setting `layers.active_index` to an invalid index crashes Blender right now. I'd like @JulianEisel opinion here.
Falk David requested review from Julian Eisel 2023-07-24 14:58:56 +02:00
Author
Member

It feels like this should be done by setting the layers.active layer pointer instead.

Yes, I also thought about that. But guess that would need additional changes in ui_but_value_set and other areas to support rna pointer property (I'll make necessary changes).

Looks like other list-rows (shapekeys, vertex groups, etc.) are also using same logic of active_index.

> It feels like this should be done by setting the layers.active layer pointer instead. Yes, I also thought about that. But guess that would need additional changes in `ui_but_value_set` and other areas to support rna pointer property (I'll make necessary changes). Looks like other list-rows (shapekeys, vertex groups, etc.) are also using same logic of `active_index`.
Member

I guess another option is to just call an operator in on_activate or just push an undo step and call WM_event_add_notifier manually.

I guess another option is to just call an operator in `on_activate` or just push an undo step and call `WM_event_add_notifier` manually.
Author
Member

Right, we can do that in activate or on_activate function (it will require context from tree_row_click_fn for undo_push and notifiers)

Right, we can do that in `activate or on_activate` function (it will require `context` from `tree_row_click_fn` for undo_push and notifiers)
Member

Ok, let's wait for @JulianEisel opinion on this :)

Ok, let's wait for @JulianEisel opinion on this :)
Julian Eisel requested changes 2023-07-26 12:45:55 +02:00
Julian Eisel left a comment
Member

We can't use the view item button like this, noted problems in-line. It should use on_activate() instead. I think it makes sense to let that take the context, there are a few places that need it already. Note that on_activate() can also set the value via RNA and call RNA_property_update(), instead of copying what that does already.

We can't use the view item button like this, noted problems in-line. It should use `on_activate()` instead. I think it makes sense to let that take the context, there are a few places that need it already. Note that `on_activate()` can also set the value via RNA and call `RNA_property_update()`, instead of copying what that does already.
@ -189,6 +189,7 @@ class AbstractTreeViewItem : public AbstractViewItem, public TreeViewItemContain
virtual ~AbstractTreeViewItem() = default;
virtual void build_row(uiLayout &row) = 0;
virtual void add_treerow_button(uiBlock &block);
Member

This is an internal function that does crucial things for the tree-view to work, it should not be virtual (making it possible to override and thus break the tree-view). API users would have to be careful to do exactly what the tree-view internals do, which is a big red flag for the API design.
And given that it's such a crucial internal function, it should stay private as well.

This is an internal function that does crucial things for the tree-view to work, it should not be `virtual` (making it possible to override and thus break the tree-view). API users would have to be careful to do exactly what the tree-view internals do, which is a big red flag for the API design. And given that it's such a crucial internal function, it should stay `private` as well.
@ -170,0 +191,4 @@
UI_but_flag_enable(view_item_but_, UI_BUT_UNDO);
view_item_but_->view_item = reinterpret_cast<uiViewItemHandle *>(this);
view_item_but_->rnaprop = RNA_struct_find_property(&layer_ptr, "active_index");
Member

View item buttons are not designed to control RNA properties. It just happens to work in this case because they reuse ui_apply_but_ROW() (which applies the value of but->hardmax which you set to the layer index here). But this is not intentional design and can break easily.

View item buttons are not designed to control RNA properties. It just happens to work in this case because they reuse `ui_apply_but_ROW()` (which applies the value of `but->hardmax` which you set to the layer index here). But this is not intentional design and can break easily.
@ -117,0 +129,4 @@
{
using namespace blender::bke::greasepencil;
GreasePencil *grease_pencil = rna_grease_pencil(ptr);
const Layer *layer = grease_pencil->layers()[value];
Member

value should be boundary checked, no change should be done if it's not in the range of valid indices.

`value` should be boundary checked, no change should be done if it's not in the range of valid indices.
@ -117,0 +131,4 @@
GreasePencil *grease_pencil = rna_grease_pencil(ptr);
const Layer *layer = grease_pencil->layers()[value];
grease_pencil->set_active_layer(layer);
WM_main_add_notifier(NC_GPENCIL | NA_EDITED, NULL);
Member

Don't send notifiers in a setter, that's what RNA_def_property_update() is for.

Don't send notifiers in a setter, that's what `RNA_def_property_update()` is for.
Author
Member

noted problems in-line...I think it makes sense to let that take the context

Thanks :)
I'll revert these changes.

> noted problems in-line...I think it makes sense to let that take the context Thanks :) I'll revert these changes.
Member

I see one problem to solve, exposed by letting on_activate() take context.

on_activate() should only be called when actually activating the item through the tree-view, e.g. when clicking on an item. It should not be called when the tree-view just updates its state to reflect external changes, i.e. when should_be_active() starts returning true.

I'd propose that I take this over, to fix this and expose context to on_activate().

I see one problem to solve, exposed by letting `on_activate()` take context. `on_activate()` should only be called when actually activating the item through the tree-view, e.g. when clicking on an item. It should not be called when the tree-view just updates its state to reflect external changes, i.e. when `should_be_active()` starts returning true. I'd propose that I take this over, to fix this and expose context to `on_activate()`.
Author
Member

on_activate() should only be called when actually activating the item through the tree-view

I think this is handled already in AbstractTreeViewItem::activate
returned early when state is not changed (maybe I'm wrong, haven't checked with breakpoint yet, building... 😅)

if (is_active()) {
    return;
}

I'd propose that I take this over, to fix this and expose context to on_activate().

Not problem. We can close this PR then


Edit: I see, it is called from other areas too, eg: change_state_delayed

> on_activate() should only be called when actually activating the item through the tree-view I think this is handled already in `AbstractTreeViewItem::activate` returned early when state is not changed (maybe I'm wrong, haven't checked with breakpoint yet, building... 😅) ``` if (is_active()) { return; } ``` > I'd propose that I take this over, to fix this and expose context to on_activate(). Not problem. We can close this PR then - - - Edit: I see, it is called from other areas too, eg: `change_state_delayed`
Member

Okay, I did a number of changes: dedd55df33, 2e9bc6373c, 741c684bf6 (and more). Context is passed to on_activate() now, so you can continue with this.

Okay, I did a number of changes: dedd55df33, 2e9bc6373c, 741c684bf6 (and more). Context is passed to `on_activate()` now, so you can continue with this.
Member

@JulianEisel Awesome, thank you 👍

@JulianEisel Awesome, thank you 👍
Author
Member

Thanks @JulianEisel :D

Thanks @JulianEisel :D
Pratik Borhade added 3 commits 2023-07-26 17:35:46 +02:00
Pratik Borhade changed title from GPv3: RNA property to set active layer to GPv3: send notifiers and push when changing the active layer 2023-07-26 17:36:23 +02:00
Pratik Borhade changed title from GPv3: send notifiers and push when changing the active layer to GPv3: send notifiers and undo push when changing the active layer 2023-07-26 18:03:14 +02:00
Julian Eisel reviewed 2023-07-26 18:19:35 +02:00
@ -184,2 +187,4 @@
void on_activate(bContext &C) override
{
this->grease_pencil_.set_active_layer(&layer_);
WM_event_add_notifier(&C, NC_GPENCIL | ND_DATA | NA_SELECTED, &(this->grease_pencil_));
Member

Passing a value to the reference parameter is usually only needed for specific cases, is there any reason your doing this? Otherwise I'd prefer keeping this null, so it's clear that it's not needed (making further refactors to remove the argument easier).

Passing a value to the `reference` parameter is usually only needed for specific cases, is there any reason your doing this? Otherwise I'd prefer keeping this null, so it's clear that it's not needed (making further refactors to remove the argument easier).
Member

Also, thinking about this, I think it's better to use RNA here, so that the message bus also gets notified about the change. Call RNA_property_pointer_set() and RNA_property_update() for this.

Also, thinking about this, I think it's better to use RNA here, so that the message bus also gets notified about the change. Call `RNA_property_pointer_set()` and `RNA_property_update()` for this.
Pratik Borhade added 1 commit 2023-07-27 10:42:50 +02:00
Julian Eisel approved these changes 2023-07-27 10:46:31 +02:00
Falk David approved these changes 2023-07-27 11:33:32 +02:00
Falk David left a comment
Member

Code LGTM. Also compiled and tested it. Thanks 👍

Code LGTM. Also compiled and tested it. Thanks 👍
Pratik Borhade merged commit 4c12801532 into main 2023-07-27 11:37:53 +02:00
Pratik Borhade deleted branch gpv3-rna-prop-active-layer-index 2023-07-27 11:37:54 +02:00
Author
Member

Merged, thanks for reviewing :)

Merged, thanks for reviewing :)
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
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#110378
No description provided.