Change preview menu directory according to the category A time ago I asked about how to create a custom preview menu, but now I want to change the directory of the previews according to the id of the category selected. Below there is the code of the script and a screenshot of the script in Blender



import bpy
from bpy.types import Panel, EnumProperty, WindowManager
from bpy.props import *
import bpy.utils.previews

import os
import json


# UI
class evp_ui(bpy.types.Panel):
bl_label = "Everyday Stuff Pack"
bl_category = "Everyday Stuff Pack"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'

def draw(self, context):
    layout = self.layout
    wm = context.window_manager

    row = layout.row()
    row.prop(context.scene, 'MyEnum')

    row = layout.row()
    # This tells Blender to draw the my_previews window manager object
    # (Which is our preview)
    row.template_icon_view(context.scene, "my_thumbnails", show_labels=True)

    # Just a way to access which one is selected
    row = layout.row()
    row.label(text="You selected: " + bpy.context.scene.MyEnum)

    row = layout.row()
    row.operator(
        evp_op.bl_idname,
        text = "Import",
        icon='APPEND_BLEND')            

preview_collections = {}

class evp_op(bpy.types.Operator):
    bl_idname = "object.evp_x"
    bl_label = "Import"
    bl_options = {'REGISTER', 'UNDO'}  

def execute(self, context):

    dirpath = os.path.join(os.path.dirname(__file__),    "blends/retro.blend/Object/")

    bpy.ops.wm.append(filename="Stop", directory=dirpath)

    return {'FINISHED'}     


def generate_previews():
# We are accessing all of the information that we generated in the register    function below
   pcoll = preview_collections["thumbnail_previews"]
   image_location = pcoll.images_location
   VALID_EXTENSIONS = ('.png', '.jpg', '.jpeg')

enum_items = []


# Generate the thumbnails
for i, image in enumerate(os.listdir(image_location)):
    if image.endswith(VALID_EXTENSIONS):
        filepath = os.path.join(image_location, image)
        thumb = pcoll.load(filepath, filepath, 'IMAGE')
        enum_items.append((image, image, "", thumb.icon_id, i))


return enum_items

def register():
    from bpy.types import Scene
    from bpy.props import StringProperty, EnumProperty

# Create a new preview collection (only upon register)
pcoll = bpy.utils.previews.new()

# This line needs to be uncommented if you install as an addon
pcoll.images_location = os.path.join(os.path.dirname(__file__), "images")

# Enable access to our preview collection outside of this function
preview_collections["thumbnail_previews"] = pcoll

# This is an EnumProperty to hold all of the images
# You really can save it anywhere in bpy.types.*  Just make sure the location makes sense
bpy.types.Scene.my_thumbnails = EnumProperty(
    items=generate_previews(),
    )
bpy.types.Scene.MyEnum = EnumProperty(
    items = [('cat1', 'Category 1', ''), 
             ('cat2', 'Category 2', ''),
             ('cat3', 'Category 3', ''),
             ('cat4', 'Category 4', ''),
             ('cat5', 'Category 5', ''),
             ('cat6', 'Category 6', '')],
    name = "Category")

def unregister():
    from bpy.types import WindowManager
    for pcoll in preview_collections.values():
        bpy.utils.previews.remove(pcoll)
    preview_collections.clear()

del bpy.types.Scene.my_thumbnails
del bpy.types.Scene.MyEnum

if __name__ == "__main__":
    register()
I already solved, if someone has the same question I leave the answer below.
Create an update function
This will execute a function everytime the category is changed
def update_func(self, context):
    
    for pcoll in preview_collections.values():
        bpy.utils.previews.remove(pcoll)
    preview_collections.clear()

    pcoll = bpy.utils.previews.new()
    pcoll.images_location = os.path.join(os.path.dirname(__file__), &quot;images/&quot; + bpy.context.scene.MyEnum)
    preview_collections[&quot;thumbnail_previews&quot;] = pcoll
    
    bpy.types.Scene.my_thumbnails = EnumProperty(
        items=generate_previews(),
        )
        
    return None

In this code the value changed is the pcoll.image_location, where the previews folder is changed based on the category id
Set the update
Then we have to set the update function in the category menu
bpy.types.Scene.MyEnum = EnumProperty(
    items = [('cat1', 'Category 1', ''), 
             ('cat2', 'Category 2', ''),
             ('cat3', 'Category 3', ''),
             ('cat4', 'Category 4', ''),
             ('cat5', 'Category 5', ''),
             ('cat6', 'Category 6', '')],
    name = &quot;Category&quot;,
    update=update_func)