Recreate Folder Tree inside Assets Browser

I may be dump but did someone acheive to recreate a folder tree inside Assets browser ?

Here my code

class CUSTOM_OT_GenerateCatalogs(Operator):
    bl_idname = "custom.generate_catalogs"
    bl_label = "Generate Catalogs"

    def execute(self, context):
        selected_folder = context.scene.selected_folder
        if not selected_folder:
            self.report({'ERROR'}, "Please select a folder.")
            return {'CANCELLED'}

        if not os.path.isdir(selected_folder):
            self.report({'ERROR'}, "Selected folder is not valid.")
            return {'CANCELLED'}

        try:
            self.create_catalogs(selected_folder)
        except RuntimeError:
            self.report({'ERROR'}, "Cannot create catalog. Please select an asset library.")
            return {'CANCELLED'}

        return {'FINISHED'}

    def create_catalogs(self, folder_path):
        root_collection = bpy.context.scene.collection
        if not root_collection:
            root_collection = bpy.data.collections.new("Root")
            bpy.context.scene.collection.children.link(root_collection)

        for root, dirs, _ in os.walk(folder_path):
            for dir_name in dirs:
                catalog_name = os.path.relpath(os.path.join(root, dir_name), folder_path)
                parent_path = os.path.dirname(catalog_name)
                if parent_path == '.':
                    parent_path = ''  # For the root folder
                bpy.ops.asset.catalog_new(parent_path=parent_path)
                print("Created catalog:", catalog_name)

This code work a bit but as long as it’s not possible to rename a catalog, I can only have a list of Catalog (the default name)

I can’t understand how they do it in this plugin https://github.com/Poly-Haven/polyhavenassets/tree/main

You can create levels by specifying the parent:

import bpy

bpy.ops.asset.catalog_new() # creates parent Catalog
bpy.ops.asset.catalog_new(parent_path='Catalog')
bpy.ops.asset.catalog_new(parent_path='Catalog/Catalog')
bpy.ops.asset.catalog_new(parent_path='Catalog/Catalog/Catalog')

This creates a parent Catalog, a sub-Catalog, a sub-sub Catalog, and a sub-sub-sub-Catalog:
SubCatalogs
But I also don’t know how to rename them with Python.

1 Like

Yep,finaly the solution was to edit the blender_assets.cats.txt

Hey ! FWIW here’s some functions I wrote to help with managing catalogs directly from the txt file. I hope in the future we get some higher level functions, because it’s cumbersome to say the least.

1 Like

Thanks for the answer, will know for the next time, I think it’s the same you do with your Lib

I did it myself writing directly in the file my code is opensource here