Is it possible to use different icons other than the default set and how?
-
7$\begingroup$ +1 See TextEditor -> Templates -> Python -> UI Previews Custom Icon / Dynamic Enum $\endgroup$ – zeffii Jun 12 '15 at 9:40
Since 2.75 the python API allows for custom icons.
You can store custom icons and load them with bpy.utils.previews.ImagePreviewCollection class, which works like a dictionary.
First let's create such icon dictionary:
import bpy.utils.previews
icons_dict = bpy.utils.previews.new()
Next, we need to find path to our icons. Its best to put them inside a folder named 'icons' next to the script file:
import os
# this will work for addons
icons_dir = os.path.join(os.path.dirname(__file__), "icons")
# but it won't give you usefull path when you opened a file in text editor and hit run.
# this will work in that case:
script_path = bpy.context.space_data.text.filepath
icons_dir = os.path.join(os.path.dirname(script_path), "icons")
Finally load icon.png into the dictionary. Blender uses 32x32 icons in UI:
icons_dict.load("custom_icon", os.path.join(icons_dir, "icon.png"), 'IMAGE')
Reference the icon by it's ID, use like this inside UI elements:
icon_value=icons_dict["custom_icon"].icon_id
It's best to handle loading of icons in the register
function, clearing the icons in unregister
function, and storing them in a global variable. Save this script, create 'icons' directory next to it and put icon.png inside. Load into Text Editor and hit Run:
import os
import bpy
import bpy.utils.previews
class Panel(bpy.types.Panel):
"""Creates a Panel in the 3D view Tools panel"""
bl_label = "Custom Icon Preview Panel"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
def draw(self, context):
global custom_icons
self.layout.label(text="Blender SE", icon_value=custom_icons["custom_icon"].icon_id)
# global variable to store icons in
custom_icons = None
def register():
global custom_icons
custom_icons = bpy.utils.previews.new()
script_path = bpy.context.space_data.text.filepath
icons_dir = os.path.join(os.path.dirname(script_path), "icons")
custom_icons.load("custom_icon", os.path.join(icons_dir, "icon.png"), 'IMAGE')
bpy.utils.register_module(__name__)
def unregister():
global custom_icons
bpy.utils.previews.remove(custom_icons)
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()
Result:
-
-
$\begingroup$ Can you set the icon this way to an add-on that appears in a search menu? e.g. setting the bl_icon to a custom value. $\endgroup$ – DolphinDream Feb 16 '17 at 2:19
-
$\begingroup$ @Jerryno, sorry that I cannot comment. I want to ask what format of icon image is needed. I use blender2.78. My image format is
.png
, size16X16
. But it cannot show the icon. And I also tried theTextEditor -> Templates -> Python -> UI Previews Custom Icon / Dynamic Enum
method, but it also does not work. Thanks. $\endgroup$ – zzj May 27 '17 at 2:32