Reload a preview image collection

I have created a preview in a panel, and I want to refresh the image list after some user action, but I cannot clear the list, so I get an error of key duplicate in the collection.

How can I reload the previews with the new images?

This is the register


    WindowManager.my_previews = EnumProperty(items=load_enum_items)
    WindowManager.redraw = BoolProperty()


    import bpy.utils.previews
    pcoll = bpy.utils.previews.new()
    pcoll.my_previews_dir = ""
    pcoll.my_previews = ()


    preview_collections["main"] = pcoll

And this is the function


def load_enum_items(self, context):
    enum_items = []


    if context is None:
        return enum_items


    wm = context.window_manager
    directory = wm.my_previews_dir
    redraw = wm.redraw


    # Get the preview collection (defined in register func).
    pcoll = preview_collections["main"]


    # if redraw is True, must reload all images    
    if directory == pcoll.my_previews_dir and redraw is False:
        return pcoll.my_previews


    pcoll.my_previews.remove(0)???  <<<<<< How can I clear the previes


    if directory and os.path.exists(directory):
        # Scan the directory for files
        image_paths = []
        for fn in os.listdir(directory):
            if fn.lower().endswith(".jpg"):
                image_paths.append(fn)


        for i, name in enumerate(image_paths):
            # generates a thumbnail preview for a file.
            filepath = os.path.join(directory, name)
            thumb = pcoll.load(filepath, filepath, 'IMAGE')
            enum_items.append((name, name, "", thumb.icon_id, i))


    pcoll.my_previews = enum_items
    pcoll.my_previews_dir = directory
    wm.redraw = False
    return pcoll.my_previews

SOLVED: pcoll.clear()

1 Like

So its clear on the list as a main function. We dont need to loop over each single id in that list right?
Went crazy of this one. AIm trying to have a dynamic one which loads thumgs depending on the folder being accessed. It just wont show them then. In works when i hardcode the path address.

Think i need to check this link, it has a great example. Also at the bottom we see that clear() function :slight_smile:

https://docs.blender.org/api/2.80/bpy.utils.previews.html

Also found this version. I was already tinkering with a update version. I just didnt understand completely where which one needed to connect. I kept getting errors keys value already excists, bypassing didnt do anything.