Displaying icons with the unknown id in the UI

To display an icon next to a property, text label, or on the operator button in the UI, we need to specify its identifier in the “icon” or “icon_value” parameter. But not all types of icons have their identifiers known in advance. For example, we cannot predefine the icon’s identifiers for procedural textures and materials because they are generated at runtime.

To get the identifier of such a dynamic icon, we can use the “icon” method of the “UILayout” class.

We can access the “UILayout” class inside the “draw” function with the “self.layout” pointer.

For example, let’s define a class that creates a custom subpanel in the N-panel of the 3D Viewport window, and display a list of all scene materials with their icons on it:

In the “draw” function, we set the “layout” variable with the “UILayout” class pointer.

Next, to display all the scene materials on our panel, let’s cycle through their list, creating a text label for each one.

Near the material name let’s display its icon.

We can get the material icon identifier through the “icon” method, specifying the current material in its parameter.

Add the received icon identifier to the “icon_value” parameter when creating a text label with the material name.

Now the material icon will be shown with the material name.

Full code:


*.blend file with sample code for my Patreon subscribers.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Inline Feedbacks
View all comments