2
$\begingroup$

I know that I can use icon_value = layout.icon(_context.object), and I also know how to implement custom icons in Blender. But what if I'm writing my own class, want to be able to store it as a template list and just want to use existing Blender icon as its icon in the template list? Is there any place in API where the full icons list is stored somehow? Can I perform in any way something like

for i, j in enumerate(bpy.app.all_blender_icons): # fake pass
    if j.name == "EVENT_M":
        return i

Of course I could just manually go over all int numbers and find out which one returns the icon I need, but it doesn't seem like a very good idea in any way.

$\endgroup$

2 Answers 2

5
$\begingroup$

Look at the icons addon.

If you look at the code to the Development Icons Addon will see that it uses data defined in that very UILayout.icon development_icon_get.py Line 86 in version 1.4 method mentioned in question.

Which if crunched into python console

>>> icons = bpy.types.UILayout.bl_rna.functions[
...                 "prop"].parameters["icon"].enum_items.keys()

>>> 
>>> icons
['NONE', 'QUESTION', 'ERROR', 'CANCEL', 'TRIA_RIGHT', 'TRIA_DOWN', 'TRIA_LEFT', 'TRIA_UP', ...

There are over 800

>>> len(icons)
802

Make a reverse lookup, name -> index

>>> icon_ref = {k : i for i, k in enumerate(icons)}
>>> icon_ref['XRAY']
563

Or using items()

>>> icons = bpy.types.UILayout.bl_rna.functions[
...                 "prop"].parameters["icon"].enum_items.items()
>>> icons[100]
('IMAGE', <bpy_struct, EnumPropertyItem("IMAGE") at 0xbb1c6c0>)
$\endgroup$
2
  • $\begingroup$ Thank you very much! This is exactly what I was looking for! $\endgroup$ May 29, 2021 at 16:44
  • $\begingroup$ This answer is incorrect; You cannot use enum_items.keys() and then construct a reverse lookup from it that maps icon names to icon integer values. I have confirmed that Blender won't return the icons in the correct order past about #300. The .items() call is the key however, see my answer below. $\endgroup$ Aug 23, 2023 at 11:06
3
$\begingroup$

We can access the RNA definition of the UILayout.prop() function's icon keyword. This is an EnumProperty definition, and its enum_items field contains all the possible icon names and their corresponding integer value:

def get_icon_value(icon_name: str) -> int:
    icon_items = bpy.types.UILayout.bl_rna.functions["prop"].parameters["icon"].enum_items.items()
    icon_dict = {tup[1].identifier : tup[1].value for tup in icon_items}

    return icon_dict[icon_name]

The bl_rna field is available on pretty much any element of Blender's PyAPI, and can be used to access the available values of any enum of an operator, UI element, function, or what have you. It's a super handy trick for add-on devs.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .