Add/remove EnumProperty items?

I was wrong

What was my mistake from start - wrong understanding of what is class lead me to wrong expectations from what enum update/get/set does.

Those is just functions to be called when something happen (actually as it described in description). This is not functions used set this enum property items value.


After more practicing/learning what is class and how it work, I gues some things. Right now for my addon I use solution proposed by BlenderBoi2018. I guess you, same as me back in the days, have issues with understanding this logic.

Shortly (clearer):

  • you have some iterable (or any else) variable (or CollectionProperty) to store some data used to generate enum items base on this data
  • you have some function which use this data to generate enum items and return them
  • you put this function name in to enum property items parameter

Then code will run this function when you howering addon panel (Blender go through addon code to draw addon UI) and regenerate enum property from variable by using function.


Here example from my addon:

Variable pr_values used to store dictionary. I use it then in function upd_preset_enum to generate enum items. It return output similar as if I set such items manually in enum property parameters. On next image I do for loop for each pr_values dictionary item, generate tuple new_pr and each time append this tuple to list pr_enum which will return then as function output.

Some description for what function return.

If you check again API documentation - it has to return list of tuples each has to contain (identifier(string, has to be different for different enum items), name(string, what shown in UI, can be same as identifier, same for all items or what ever), description (string, what ever you want, shown as a hint whe mouse over enum item), icon(optional, may not be included), number(integer, different for different enum items)).

  • Identifier - what you will get from python API. My addon example bpy.context.scene.sot_props.presets (sot_props - my properties collection, presets - enum property name) will return some 'Preset.002' (currently selected enum item string identifier). You can use it to identify which enum item is selected.
  • Number - not same as identifier, has to be some different integer numbers (1,2,3,4,5) Bledner will use them to manage enum items.

Here - name of this function used as “items” in enum property. Return of this function become value of “items” of my enum property.

As you noticed (in my case) variable pr_values empty from start. It’s OK, I use add/remove operators (on next image) for add/remove interface buttons to manage pr_values dictionary and, as result, manage my enum property items.

Here code of panel where I use this enum property (selected enum property, add and remove operators buttons). As you see I use same pr_values variable also to manage UI, change “remove” button icon, show info about selected preset.

Here how it look in addon UI panel.