Python questions (addon development roadblocks I've hit)

Hi folks o/

I’ve been toying with addon development for a while now, and there are some problems I have encountered that I have not yet been able to solve:

  • How can I enable/disable some items of an EnumProperty? In my addon, I’m using an instance of an EnumProperty to populate a menu with a list of options. I want to mimic the functionality of the file/edit menus (because those menus seem to be capable of disabling menu items)
  • Is it possible to run a function when the blender window is focused? I want to update the menu items based on changes that take place outside of blender. The menu I want to update reads some state from the system, and then presents values in a menu. When I modify the resources outside of blender, and then click the menu in Blender, the menu items are updated and presented as expected. But I would like to be able to have the menu refresh without having to first click on it - perhaps when the user focuses the Blender window?
  • What type of UI elements can the status bar support? I’ve got some code that generates a panel with a drop down menu, but instead of having a panel, I want to embed the drop down menu into the status bar - is this possible?

I would really appreciate code samples if any of this is possible - I find the API docs to be lacking in examples, and I keep finding TODOs in place of documentation

Thanks

1 Like

I think you want to use a CollectionProperty instead of EnumProperty. EnumProperty is only what in other software we call drop down menu or combo box: a list of values for one single property. With the CollectionProperty, you will be able to create a list of multiple items with as many properties as you want (name, enable/disable, etc) and expose it in the UI the way you want, for example with UIList (check the ui_list_simple.py template).

1 Like

Thanks for your reply Vincent!

I guess I’ve just encountered more questions that I’d like answered since my last post!

Is it possible for an addon to create a Window in Blender? Similar to the file dialog that appears when you want to open a file?

I’ve tried changing my bpy.types.Panel UI element to a bpy.types.Window, but I get this error:

Read prefs: /home/vtx/.config/blender/2.90/config/userpref.blend
addon_utils.disable: myaddon not disabled
Modules Installed (myaddon) from '/home/vtx/development/myaddon/myaddon.zip' into '/home/vtx/.config/blender/2.90/scripts/addons'
Exception in module register(): /home/vtx/.config/blender/2.90/scripts/addons/myaddon/__init__.py
Traceback (most recent call last):
  File "/usr/share/blender/2.90/scripts/modules/addon_utils.py", line 382, in enable
    mod.register()
  File "/home/vtx/.config/blender/2.90/scripts/addons/myaddon/__init__.py", line 20, in register
    core_register()
  File "/home/vtx/.config/blender/2.90/scripts/addons/myaddon/core/__init__.py", line 6, in register
    bpy.utils.register_class(MYUI_PT_Panel)
ValueError: register_class(...): expected a subclass of a registerable RNA type (Window does not support registration)

Any ideas how I can render a window from the python API?

No, for that you need to use an operator with a file selector on the invoke method.

You can check the scripts/modules/bpy_extras/io_utils.py file in the ImportHelper class for example, or the Operator File Import template.

Sounds to me like you need a lot of different properties and a draw function that chooses when and how to show them. The user doesn’t need to know how it all works – it just needs to feel nice to use.