2
$\begingroup$

I have a custom add-on and I would like to create a dropdown list based on an EnumProperty, but I want the items in the list to take custom icons rather than the icons available within blender. I know that you can use import bpy.utils.previews to load custom icons into add-ons and set label's icon_value to custom icon ids, but I would like to be able to do the same with the items in a drop down list built based on an EnumProperty.

I have the following case for an add-on defining a class that will have a drop-down lists in which I want to have custom icons added to EACE item in the list. I want custom icons to show next to each item in the list as the drop down list is open (not just next to the closed drop down list: see image below):

# defined globally
easingItems = [
    ("EASE_IN", "Ease In", "", "", 0),
    ("EASE_OUT", "Ease Out", "", "", 1),
    ("EASE_IN_OUT", "Ease In-Out", "", "", 2)]

# prop defined in the addon/node class 
easingType = EnumProperty(
        name="Easing",
        default="EASE_IN", 
        items=easingItems,
        update=updateNode)

# then, in the draw UI
def draw_buttons(self, context, layout):
    # this is when I want the enum property to be displayed with custom icons (how?)
    layout.prop(self, ‘easingType', expand=False)

Left: This is a drop down list based on an enum property with some default blender icons. Right: This is a drop-down list based on a list needing custom icons (displayed as having default blender icons)

Default icons vs Custom Icons in open drop-down list

The result after Jerryno’s solution looks like this:

Drop-down list with custom icons

$\endgroup$
3
$\begingroup$

For each enum item an icon can be specified. You just need to edit this part of your code:

# defined globally
easingItems = [
    ("EASE_IN", "Ease In", "", customIcon["MY_EASEIN"].icon_id, 0),
    ("EASE_OUT", "Ease Out", "", customIcon["MY_EASEOUT"].icon_id, 1),
    ("EASE_IN_OUT", "Ease In-Out", "", customIcon["MY_EASEINOUT"].icon_id, 2)]
$\endgroup$
  • $\begingroup$ Thx @Jerryno. Could you be more specific ? (see my latest comment). And by the way, I want all the items in the drop down list to have icons, not just the collapsed drop down list. $\endgroup$ – DolphinDream Feb 17 '17 at 21:30
  • $\begingroup$ @DolphinDream Hi, thanks for the edit, hopefully now it fits your case better. But I am not sure if it will work, because I don't know where in the ui and how you are using this. draw_buttons and accessing the enum through self indicates it is not an ui panel. Try this and if it does not work I could improve it by seeing a piece of code I could test this on. $\endgroup$ – Jaroslav Jerryno Novotny Feb 18 '17 at 13:40
  • $\begingroup$ I tried it and it does’t quite work. I think the icon_value expects a single int value, thus your second line does change the icon only for the item currently selected in the drop down list, and only show on the closed drop down, not in the opened drop down (e.g. items in the open drop down do not show an icon). $\endgroup$ – DolphinDream Feb 18 '17 at 17:29
  • $\begingroup$ @DolphinDream now it should work $\endgroup$ – Jaroslav Jerryno Novotny Feb 18 '17 at 18:14
  • $\begingroup$ Awesome, @Jerryno ! :) sweet! The only thing I have to be careful though is that the customIcon is automatically loading the icons from disc (call to loadIcons) since this list may be defined outside of the class and perhaps registers before the custom icon module is registered (and icons loaded). Thanks again! $\endgroup$ – DolphinDream Feb 18 '17 at 19:14

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.