Custom icon for work space tool

I’ve registered a costum png file using the following code:

custom_icons ={}

def registericons():
    global custom_icons
    custom_icons = bpy.utils.previews.new()
    icons_dir = os.path.join(os.path.dirname(__file__), "icons")
    custom_icons.load("custom_icon", os.path.join(icons_dir, "mmc_logo.png"), 'IMAGE')

def unregistericons():
    global custom_icons
    bpy.utils.previews.remove(custom_icons)

This works for class panel. But i would like to use this new icon in the main tool section, using the workspacetool class.
From all the examples i have seem bl_icon attribute needs string to select the icon.
While the way i saved the image is in a class called ImagePreview. I have noticed all the workspacetools icons are located in a icons file in blender, but all the icons has .dat extension which i don’t know how is the data saved. Any idea on how to solve this?

Thanx!

Hey @xiwei,

Welcome to Devtalk :slight_smile: You’re right that you can’t just pass the string 'custom_icon' to the icon parameter of UILayout functions.

After calling custom_icons.load(identifier) you can use custom_icons[identifier].icon_id, which gives you an integer that you can pass to the icon_value parameter:

layout.label(text='Blender Sync with Blender Cloud', icon_value=custom_icons['custom_icon'].icon_cd)

On a side note about, generally code-style wise speaking, I would recommend against changing the type of variables. In the example code you posted, custom_icons is declared as dict, but later is assigned an object of a different type. This can cause confusion, so it’s better to initialise to None instead. You can then annotate it with an explicit type:

from typing import Optional
import bpy.utils.previews

custom_icons: Optional[bpy.utils.previews.ImagePreviewCollection] = None

@sybren thx for the reply :slight_smile:

As you mentioned you can pass the id to layout elements. So there is no way of passing to the workspace tools? thx

class EDIT_MT_TOOLS(WorkSpaceTool):
bl_space_type='VIEW_3D'
bl_context_mode='EDIT_MESH'
bl_idname = "mcc.tools"
bl_label = "Slice"
bl_options = {'REGISTER', 'UNDO'}
bl_description = (
    "Slice with 2 points in 3D world"
)
**bl_icon = 'None'**
bl_widget = None
bl_keymap = (
    ("wm.modal_quick_slice", {"type": 'LEFTMOUSE', "value": 'PRESS'},
 None),
    )

Any news about how to use custom icons in Workspace Tools?

This is what i foound. WorkSpaceTool uses .dat files as icons and you can find all icon .dat in the icon folder. In order to create your .dat you need to create it using the following script:

https://svn.blender.org/svnroot/bf-blender/trunk/lib/resources/

once you’ve create .dat put it the icon folder and bl_icon = ‘dat_file_name’. Hope it helps.

Edit: for reference Creating costum icons

1 Like