1
$\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
$\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$
1
  • $\begingroup$ Thank you very much! This is exactly what I was looking for! $\endgroup$ May 29 '21 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.