UI: It's not possible to add a separator to EnumProperty via Python #68260

Closed
opened 2019-08-05 14:28:11 +02:00 by artem ivanov · 9 comments

System Information
Operating system: Win 10
Graphics card: GTX 1070

Blender Version
Broken: 2.81 9620b8f6bb and earlier

Short description of error

Currently it is not possible to add a separator to EnumProperty via python.

What

Separator in EnumProperty is EnumPropertyItem which looks like that:

EnumPropertyItem sepr = {0, "", 0, NULL, NULL};

*sepr.value       == 0* sepr.identifier  == ""
*sepr.icon        == 0* sepr.name        == NULL
*sepr.description == NULL* Notice `sepr.name == NULL`.

Such items are used for example in Cycles Math node (defined in C):

image.png

Why

However, currently it's not possible to make an EnumPropertyItem from python with required sepr.name = NULL (or with other field equal to nullptr).

Part that is responsible for checking if an item is a separator is:
/source/blender/editors/interface/interface.c:ui_def_but_rna__menu
It uses item->identifier- [x] == 0 and item->name == nullptr checks to determine if an item is a separator. (But item->name will never be nullptr if it's created through python).


Code for parsing EnumPropertyItem from python object:

bpy_types.c:1470
        (tmp.name = _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 1), &name_str_size)) &&

_PyUnicode_AsStringAndSize (which is PyUnicode_AsUTF8AndSize) never returns NULL unless an error occurred.

Solution

Add check for item->name = empty string in ui_def_but_rna__menu. That won't change the behaviour of existing nodes (which use identifier- [x] ==0 && name == nullptr) but will enable separator creation from python via tuple ("", "", "", "", 0).

**System Information** Operating system: Win 10 Graphics card: GTX 1070 **Blender Version** Broken: 2.81 9620b8f6bbcf and earlier **Short description of error** Currently it is not possible to add a separator to **EnumProperty** via python. ### What Separator in **EnumProperty** is **EnumPropertyItem** which looks like that: ``` EnumPropertyItem sepr = {0, "", 0, NULL, NULL}; *sepr.value == 0* sepr.identifier == "" *sepr.icon == 0* sepr.name == NULL *sepr.description == NULL* Notice `sepr.name == NULL`. ``` Such items are used for example in Cycles **Math** node (defined in C): ![image.png](https://archive.blender.org/developer/F7650102/image.png) ### Why However, currently it's not possible to make an **EnumPropertyItem** from python with required `sepr.name = NULL` (or with other field equal to nullptr). Part that is responsible for checking if an item is a separator is: `/source/blender/editors/interface/interface.c:ui_def_but_rna__menu` It uses `item->identifier- [x] == 0` and `item->name == nullptr` checks to determine if an item is a separator. (But `item->name` will never be `nullptr` if it's created through python). ---- Code for parsing **EnumPropertyItem** from python object: ``` bpy_types.c:1470 (tmp.name = _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 1), &name_str_size)) && ``` `_PyUnicode_AsStringAndSize` (which is `PyUnicode_AsUTF8AndSize`) never returns `NULL` unless an error occurred. ### Solution Add check for `item->name = empty string` in `ui_def_but_rna__menu`. That won't change the behaviour of existing nodes (which use `identifier- [x] ==0 && name == nullptr`) but will enable separator creation from python via tuple ("", "", "", "", 0).
Author

Added subscriber: @ixd

Added subscriber: @ixd
Author
Patch: https://developer.blender.org/D5414

This issue was referenced by 916e51a407

This issue was referenced by 916e51a4078b452774b2696d1daeb7286ab3f085

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'
Campbell Barton self-assigned this 2019-08-05 15:15:36 +02:00

Added subscriber: @RomboutVersluijs

Added subscriber: @RomboutVersluijs

Im tried using this in an enum menu and it divides dropdown into two parts? I also tried with ("", "", "", "", 0) but shows the same. Leaving out 0 returns error. Using bl2.82 on OSX

bpy.types.Scene.sortMenu = bpy.props.EnumProperty(
        name = "Sorting",
        description = "My enum description",
        items = [
            ("reset", "Reset Colors", "Resets color order in active palette"),
            ("", "", "",0),
            ("simple", "Simple", "Simple sorting"),
            ("hsl", "HSL", "HSL sorting"),
            ("hsv", "HSV", "HSV sorting"),
            ("lum", "LUM", "Lumnosity sorting"),
            ("step", "Step 1", "Step sorting"),
            ("stepTwo", "Step 2", "Step sorting clean noise")],
        update=update_Palette)

Screen Shot 2020-03-17 at 23.26.12.png

Im tried using this in an enum menu and it divides dropdown into two parts? I also tried with ("", "", "", "", 0) but shows the same. Leaving out 0 returns error. Using bl2.82 on OSX ``` bpy.types.Scene.sortMenu = bpy.props.EnumProperty( name = "Sorting", description = "My enum description", items = [ ("reset", "Reset Colors", "Resets color order in active palette"), ("", "", "",0), ("simple", "Simple", "Simple sorting"), ("hsl", "HSL", "HSL sorting"), ("hsv", "HSV", "HSV sorting"), ("lum", "LUM", "Lumnosity sorting"), ("step", "Step 1", "Step sorting"), ("stepTwo", "Step 2", "Step sorting clean noise")], update=update_Palette) ``` ![Screen Shot 2020-03-17 at 23.26.12.png](https://archive.blender.org/developer/F8413198/Screen_Shot_2020-03-17_at_23.26.12.png)

It seems it makes columns using that, not the separator as does with a menu

I did a test making the enum longer, repeated it 3 times. It results in 4 columns.

Screen Shot 2020-03-17 at 23.36.07.png

Screen Shot 2020-03-17 at 23.35.59.png

It seems it makes columns using that, not the separator as does with a menu I did a test making the enum longer, repeated it 3 times. It results in 4 columns. ![Screen Shot 2020-03-17 at 23.36.07.png](https://archive.blender.org/developer/F8413208/Screen_Shot_2020-03-17_at_23.36.07.png) ![Screen Shot 2020-03-17 at 23.35.59.png](https://archive.blender.org/developer/F8413207/Screen_Shot_2020-03-17_at_23.35.59.png)

BLender API shows a different solution i just noticed.

Separators may be added using None instead of a tuple. For dynamic values a callback can be passed which returns a list in the same format as the static list. This function must take 2 arguments (self, context), context may be None.

bpy.types.Scene.sortMenu = bpy.props.EnumProperty(
        name = "Sorting",
        description = "My enum description",
        items = [
            ("reset", "Reset Colors", "Resets color order in active palette",0),
            (None),
            ("simple", "Simple", "Simple sorting",2),
            ("hsl", "HSL", "HSL sorting",3),
            ("hsv", "HSV", "HSV sorting",4),
            ("lum", "LUM", "Lumnosity sorting",5),
            ("step", "Step 1", "Step sorting",6),
            ("stepTwo", "Step 2", "Step sorting clean noise",7)
            ],
        update=update_Palette
        )

See at the bottom, now using None it does work
Screen Shot 2020-03-18 at 00.02.39.png

BLender API shows a different solution i just noticed. > Separators may be added using None instead of a tuple. For dynamic values a callback can be passed which returns a list in the same format as the static list. This function must take 2 arguments (self, context), context may be None. > ``` bpy.types.Scene.sortMenu = bpy.props.EnumProperty( name = "Sorting", description = "My enum description", items = [ ("reset", "Reset Colors", "Resets color order in active palette",0), (None), ("simple", "Simple", "Simple sorting",2), ("hsl", "HSL", "HSL sorting",3), ("hsv", "HSV", "HSV sorting",4), ("lum", "LUM", "Lumnosity sorting",5), ("step", "Step 1", "Step sorting",6), ("stepTwo", "Step 2", "Step sorting clean noise",7) ], update=update_Palette ) ``` See at the bottom, now using None it does work ![Screen Shot 2020-03-18 at 00.02.39.png](https://archive.blender.org/developer/F8413236/Screen_Shot_2020-03-18_at_00.02.39.png)

PS i noticed some of Blender enum menu's also show this weird horizontal tabbing. For instance in Preferences > Addons > Sort Menu
Perhaps here its intentional since its probably more clear to read like this compared to a big list. But it looks kinda sloppy, its not aligning nice

keyfilter_wrong enum list usage.png

PS i noticed some of Blender enum menu's also show this weird horizontal tabbing. For instance in Preferences > Addons > Sort Menu Perhaps here its intentional since its probably more clear to read like this compared to a big list. But it looks kinda sloppy, its not aligning nice ![keyfilter_wrong enum list usage.png](https://archive.blender.org/developer/F8416427/keyfilter_wrong_enum_list_usage.png)
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#68260
No description provided.