I'd like to know how i can get the icon_id of an image already loaded. For exemple, if i use the template "ui_previews_dynamic_enum.py" and i change the directory, there is no problem as long as i don't return in a folder already used to display the icons. In this case, i've got this message error:

  File "C:\Program Files\Blender Foundation\Blender\2.77\scripts\modules\bpy\utils\previews.py", line 97, in load
  raise KeyError("key %r already exists" % name)

How can i "ask to blender" using the icon_id of the corresponding image if this one already exist ?


1 Answer 1


i'm not sure without seeing your code ( a github link would be great ). I can make a guess about how you might be able to avoid the KeyError

# ImagePreviewCollection(dict) is a subclass of Python's built-in dict type
main_collection = icon_collection["main"]
icon = main_collection.get(some_key)
if not icon:
    main_collection.load(icon_name, icon_path, 'IMAGE')

I use custom icons in mesh_tiny_cad

If i'm reading bpy/utils/previews.py correctly it isn't possible to overwrite or clear a key/value pair.

If you are keeping track of the IDs somehow then..

if icon_id in [v.icon_id for k, v in pcoll.items()]:
  • $\begingroup$ This question is directly linked to my addon Asset Mangement. In this addon, we can change libraries and categories. But, for now, i clean the preview_collection each time i change library or category. It involve reload the images each change. Although the combination library , category already used. So, the best way i think is to get the id of the icon if the image was already loaded but can't find the way to get it. Or maybe add a new key in the preview_collection. $\endgroup$
    – pistiwique
    Commented Aug 20, 2016 at 12:59
  • $\begingroup$ maybe the edit to my answer helps, if not.. i'm missing something. $\endgroup$
    – zeffii
    Commented Aug 20, 2016 at 13:55
  • $\begingroup$ Exactly what was missing. Thanks a lot zeffii. $\endgroup$
    – pistiwique
    Commented Aug 20, 2016 at 15:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .