Simple python fix for script

This is a very tiny project, basically just fixing an error of a simple script.

  • Type of project: fix an error of a very simple script. (not even an addon).
  • Who am I: New Media Supply. I do (exciting) freelance work next to my (very boring) 32-hour partime job and breaking my head on how to get thumbnails in my addon (explaining later).
  • Deadline: I want to have this done before Wednesday, April 24.
  • Budget 15 pounds, or one of my products?
  • I don’t need anything extra, just that the addon is working as shown in the video in Blender 2.80

Complete description of task :

Traceback (most recent call last):
  File "\", line 29, in get_previews_collection
  File "C:\Users\Flatron\Downloads\blender-280-April_15\blender-2.80.0-git.860a9f979d60-windows64\2.80\scripts\modules\bpy\utils\", line 97, in load
    raise KeyError("key {name!r} already exists")
KeyError: 'key {name!r} already exists'
File "\", line 17, in get_previews_collection

Some background info:
At the end it’s for the EV-Express and took me a more than a week finding out how to get thumbnails / previews instead of buttons. I analysed other scripts that uses previews, asked on blender exchange and here on Blender artists but this specific part takes me to long to solve and I want to move on with other exciting features I have in mind. If I have this specific example as in the video working in Blender 2.80 I can implement that in the EV Express. I exactly need what’s in the video. (For example the template is not useful since you have to select a directory in the UI, and a selection doesn’t do anything.).

As long as you’re not reloading, try with :

if file not in custom_icons:
    	custom_icons.load(file, fullpath, 'IMAGE')
1 Like

Thanks!, I will check today after work how that works out.

Could you show a tiny bit more of the code? Tried several combinations, but seems not to work. And with “not reloading” you mean running the script again?

In your sample file, error occurs on line 29 : you try to reload the same image again, without checking if the pic already is loaded.

for file in os.listdir(directory):
    if file.endswith(".png"):
        fullpath = os.path.join(directory, file)   
        # check if file allready loaded if so skip loading
        if file not in custom_icons:         
            custom_icons.load(file, fullpath, 'IMAGE')
        images.append(( fullpath, file, fullpath, custom_icons[file].icon_id, i))

Also line 30 custom_icons(file).icon_id must be custom_icons[file] !

If you are not reloading your thumbs in middle of a session, then you’re fine with this code.
If you reload, you should cleanup custom_icons before reloading like you do in unregister line 73

Oh, that is great! Finally I get now previews. That was what I was asking for. It was crashing when clicking on one of the thumbnails but I don’t need that function def open_image(self, context): anyway. So I believe I should be able to manage the rest.
I will send you a private message regarding promised compensation.