Outliner: New context menu building design #101007

Open
opened 2022-09-12 14:48:32 +02:00 by Julian Eisel · 8 comments
Member

Motivation
The way we build context menus in outliner_tools.cc is known to be quite a mess. Basic problems:

  • Bad Encapsulation: The entries typically operate on scene data. And usually the logic for these is implemented in the Outliner context menu code itself. This violates encapsulation, and the operations tend to have issues because they are badly maintained (ask @mont29 :) ). When mixing UI and scene data-management, responsibilities become unclear. Two separate departments need knowledge about each other to keep the features maintained.
  • Inconsistent: The way we build these menus is unlike any other menu building in Blender. Most entries don't use the regular operator system either. This breaks familiarity, and standard operator features like names, description and undo pushing have to be managed differently/manually.
  • Messy Code: The code is a bit chaotic and generally it's not as simple to add menu entries as it should be. Too much upfront digging is needed, you have to understand the unusual menu building system first.
  • User Visible Consequences: Some menu entries had a tooltip showing "(undocumented operator)", or the undo push name would differ from the operator name. Often menu entries were added that didn't make sense for the current selection, because hiding them requires special attention. With regular operators this is less likely to happen, since developers are used to implement them properly.

Proposed Design

TL;DR: Add batch editing operators outside of Outliner code, they can act on context set by the Outliner. Tree elements are asked to add context menu entries themselves.

Main idea is:

  • Rather than a central place to define the context menu (outliner_tools.cc), ask the active element to expand the context menu. For this, element types can override AbstractTreeElement::expandContextMenu().
  • Further general entries (e.g. the View and Area sub-menus) can be added in Python via OUTLINER_MT_context_menu. This is already the case in master.
  • Use proper operators for entries, defined in the relevant code modules, not the Outliner. Typically these operators would have to support batch operations to act on the whole Outliner selection.
  • The Outliner "broadcasts" information about its selection by setting context, which the operators can access.

Further Considerations
The context system has some limitations still:

  • Context can only hold RNA pointers. Data that isn't covered by RNA can't be "broadcast" this way currently.
  • Some operators rely on the Outliner hierarchy. For example library overrides use it to find out which data-blocks need to be overridden with a system override. How to "broadcast" this hierarchy via context (also see previous point)?
  • Querying lists of data (e.g. "selected_ids") can be inefficient. The Outliner context callback iterates over the entire tree to find the selection, and then allocates list elements for each.
  • Similarly, getting information about the selection can be inefficient. E.g. you have to request the entire list (that is then built as explained above), just to see if there are elements of interest selected. This is a problem for operator poll functions.

There are ideas to solve each, but they don't seem like a blocker for the design above. For now it's still fine to leave some menu entries as callbacks rather than operators, where context doesn't allow us to broadcast the necessary information. The actual logic should still be moved to the dedicated code modules. Performance impact is probably minor in practice.

Also:

  • It would be nice to have the context menu entries defined in Python. There could be a menu type for each element type (e.g. OUTLINER_MT_modifier_context_menu). But for as long as some entries may have to stick to a callback based approach, keeping things in C++ mostly seems better.
  • Some operators may invalidate the data displayed by the Outliner. So care needs to be taken to avoid use-after-free errors or similar. Hopefully this isn't an issue because the tree is rebuilt before any further access.
**Motivation** The way we build context menus in `outliner_tools.cc` is known to be quite a mess. Basic problems: - **Bad Encapsulation:** The entries typically operate on scene data. And usually the logic for these is implemented in the Outliner context menu code itself. This violates encapsulation, and the operations tend to have issues because they are badly maintained (ask @mont29 :) ). When mixing UI and scene data-management, responsibilities become unclear. Two separate departments need knowledge about each other to keep the features maintained. - **Inconsistent:** The way we build these menus is unlike any other menu building in Blender. Most entries don't use the regular operator system either. This breaks familiarity, and standard operator features like names, description and undo pushing have to be managed differently/manually. - **Messy Code:** The code is a bit chaotic and generally it's not as simple to add menu entries as it should be. Too much upfront digging is needed, you have to understand the unusual menu building system first. - **User Visible Consequences**: Some menu entries had a tooltip showing `"(undocumented operator)"`, or the undo push name would differ from the operator name. Often menu entries were added that didn't make sense for the current selection, because hiding them requires special attention. With regular operators this is less likely to happen, since developers are used to implement them properly. **Proposed Design** *TL;DR: Add batch editing operators outside of Outliner code, they can act on context set by the Outliner. Tree elements are asked to add context menu entries themselves.* Main idea is: - Rather than a central place to define the context menu (`outliner_tools.cc`), ask the active element to expand the context menu. For this, element types can override `AbstractTreeElement::expandContextMenu()`. - Further general entries (e.g. the *View* and *Area* sub-menus) can be added in Python via `OUTLINER_MT_context_menu`. This is already the case in master. - Use proper operators for entries, defined in the relevant code modules, not the Outliner. Typically these operators would have to support batch operations to act on the whole Outliner selection. - The Outliner "broadcasts" information about its selection by setting context, which the operators can access. **Further Considerations** The context system has some limitations still: - Context can only hold RNA pointers. Data that isn't covered by RNA can't be "broadcast" this way currently. - Some operators rely on the Outliner hierarchy. For example library overrides use it to find out which data-blocks need to be overridden with a system override. How to "broadcast" this hierarchy via context (also see previous point)? - Querying lists of data (e.g. `"selected_ids"`) can be inefficient. The Outliner context callback iterates over the entire tree to find the selection, and then allocates list elements for each. - Similarly, getting information about the selection can be inefficient. E.g. you have to request the entire list (that is then built as explained above), just to see if there are elements of interest selected. This is a problem for operator poll functions. There are ideas to solve each, but they don't seem like a blocker for the design above. For now it's still fine to leave some menu entries as callbacks rather than operators, where context doesn't allow us to broadcast the necessary information. The actual logic should still be moved to the dedicated code modules. Performance impact is probably minor in practice. Also: - It would be nice to have the context menu entries defined in Python. There could be a menu type for each element type (e.g. `OUTLINER_MT_modifier_context_menu`). But for as long as some entries may have to stick to a callback based approach, keeping things in C++ mostly seems better. - Some operators may invalidate the data displayed by the Outliner. So care needs to be taken to avoid use-after-free errors or similar. Hopefully this isn't an issue because the tree is rebuilt before any further access.
Author
Member

Added subscribers: @mont29, @JulianEisel

Added subscribers: @mont29, @JulianEisel

Added subscriber: @brecht

Added subscriber: @brecht

I think all menu entries being proper operators makes sense. I'm less sure about asking the active element to expand the context menu, and broadcasting information through context.

The purpose of context is that operators can run in different editors. If the information is outliner specific (hierarchy etc), it's an unnecessary layer of abstraction only adding complexity, and I would just let operators access outliner data structures directly.

Letting the elements fill the menu is unclear to me, not sure how that works when you have multiple selected elements, mixed types. There might be some logic there that's much more easily expressed in a central function to fill the menu. Maybe it's not an issue, but I vaguely remember some logic that would prioritize e.g. objects over other data types in mixed selections. If it can be made to work then the extensibility is nice though.

I think all menu entries being proper operators makes sense. I'm less sure about asking the active element to expand the context menu, and broadcasting information through context. The purpose of context is that operators can run in different editors. If the information is outliner specific (hierarchy etc), it's an unnecessary layer of abstraction only adding complexity, and I would just let operators access outliner data structures directly. Letting the elements fill the menu is unclear to me, not sure how that works when you have multiple selected elements, mixed types. There might be some logic there that's much more easily expressed in a central function to fill the menu. Maybe it's not an issue, but I vaguely remember some logic that would prioritize e.g. objects over other data types in mixed selections. If it can be made to work then the extensibility is nice though.
Author
Member

In #101007#1415770, @brecht wrote:
The purpose of context is that operators can run in different editors. If the information is outliner specific (hierarchy etc), it's an unnecessary layer of abstraction only adding complexity, and I would just let operators access outliner data structures directly.

Right the situation gets tricky with hierarchies and such. I see some options, but I wouldn't even mind outliner specific operators here, for as long as these are just thin wrappers calling into BKE code or such. My main concern is to get data-management code (even things like user counting) out of the Outliner, which should be "just a UI".

For other cases like batch deleting objects, I see no reason to keep this in the Outliner code at all. Broadcasting via context would be simple here and not require further abstractions.

Letting the elements fill the menu is unclear to me, not sure how that works when you have multiple selected elements, mixed types. There might be some logic there that's much more easily expressed in a central function to fill the menu. Maybe it's not an issue, but I vaguely remember some logic that would prioritize e.g. objects over other data types in mixed selections. If it can be made to work then the extensibility is nice though.

I think this used to be the case, but for a while we've only been building the context menu from the active element now. In fact I've just cleaned up get_element_operation_type(), which seems to have been designed to support this, but wasn't used that way anymore (which I checked carefully): 8f6a38ed7e.

> In #101007#1415770, @brecht wrote: > The purpose of context is that operators can run in different editors. If the information is outliner specific (hierarchy etc), it's an unnecessary layer of abstraction only adding complexity, and I would just let operators access outliner data structures directly. Right the situation gets tricky with hierarchies and such. I see some options, but I wouldn't even mind outliner specific operators here, for as long as these are just thin wrappers calling into BKE code or such. My main concern is to get data-management code (even things like user counting) out of the Outliner, which should be "just a UI". For other cases like batch deleting objects, I see no reason to keep this in the Outliner code at all. Broadcasting via context would be simple here and not require further abstractions. > Letting the elements fill the menu is unclear to me, not sure how that works when you have multiple selected elements, mixed types. There might be some logic there that's much more easily expressed in a central function to fill the menu. Maybe it's not an issue, but I vaguely remember some logic that would prioritize e.g. objects over other data types in mixed selections. If it can be made to work then the extensibility is nice though. I think this used to be the case, but for a while we've only been building the context menu from the active element now. In fact I've just cleaned up `get_element_operation_type()`, which seems to have been designed to support this, but wasn't used that way anymore (which I checked carefully): 8f6a38ed7e.

Added subscriber: @DuarteRamos

Added subscriber: @DuarteRamos
Author
Member

Right the situation gets tricky with hierarchies and such.

Just to note, for library overrides it was/is quite a problem that only the Outliner has a good enough understanding of the necessary relations. I think it's worth thinking about ways to better define scene hierarchies throughout in Blender, for a whole bunch of reasons. Of course this is a bigger discussion, just noting as another way this could go. All in all I'm not too concerned, I'm glad about any gradual improvement here, and I wasn't planning to spend much time on these cases.

> Right the situation gets tricky with hierarchies and such. Just to note, for library overrides it was/is quite a problem that only the Outliner has a good enough understanding of the necessary relations. I think it's worth thinking about ways to better define scene hierarchies throughout in Blender, for a whole bunch of reasons. Of course this is a bigger discussion, just noting as another way this could go. All in all I'm not too concerned, I'm glad about any gradual improvement here, and I wasn't planning to spend much time on these cases.

Would the "operatorification" of the outliner menus finally open up the possibility of more things working on selection rather than active object?

I'm thinking of things like (visibility or selectability) toggles working on all selection rather than just clicked item, or the possibility of keyframing toggles renderability toggles for multiple objects.

Would the "operatorification" of the outliner menus finally open up the possibility of more things working on selection rather than active object? I'm thinking of things like (visibility or selectability) toggles working on all selection rather than just clicked item, or the possibility of keyframing toggles renderability toggles for multiple objects.
Philipp Oeser removed the
Interest
User Interface
label 2023-02-10 09:21:46 +01:00
Pratik Borhade added
Type
Design
and removed
Type
Report
labels 2023-03-23 07:09:26 +01:00
Member

This is a design task so moving it out of the triaging queue

This is a design task so moving it out of the triaging queue
Iliya Katushenock removed the
Status
Needs Triage
label 2023-08-24 17:33:02 +02:00
Sign in to join this conversation.
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
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#101007
No description provided.