Setting an icon from the tools panel to a button in the custom UI panel in Blender

To set an icon to a button when creating the custom user UI panel in Blender, we look for the text identifier of the desired icon (usually using the built-in Icon Viewer add-on) and set its value to the “icon” parameter when binding the desired operator to the button. However, the icons used in the tools panel are not existed in the common Blender icons list.

Even though tool icons are not in the standard set, we can still get their identifiers and then use them in custom panels.

To get the icon id, we need to get the id of the tool with the desired icon, and then use the ToolSelectPanelHelper class to get the actual icon id.

The id of the required tool can be obtained using the following script.

Let’s take the “Annotate” tool as an example. Its id is “ops.gpencil.draw”.

Now we need to import the ToolSelectPanelHelper class and use the “_icon_value_from_icon_handle” method to get the identifier of the icon.

Note that this method is not documented and is private; its correct work is not guaranteed by the developers!

Having an icon identifier, we can use it when designing buttons in a custom UI panel.

Let’s create a test panel class with single button.

In the draw() function, which is responsible for displaying the UI, we create one button. We bind the adding the default cube to the scene operator “mesh.primitive_cube_add” to this button.

Before this, we got the identifier of the tool icon, and then set the received value in the “icon_value” parameter when assigning an operator to the button.

Since the tool icons are larger than standard icons, we doubled the size of the buttons on our panel by setting the scale_y = 2.

0 0 votes
Article Rating
Subscribe
Notify of
guest

2 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Timm
Timm
3 months ago

nice work, is there a way to shrink the icon size to 16×16 px?