Skip to main content
Bumped by Community user
Tweeted twitter.com/StackBlender/status/1481098654454890496
edited tags
Link
Gorgious
  • 31.8k
  • 2
  • 49
  • 106
Source Link
enenra
  • 81
  • 1
  • 2

How do I get all assets in a given UserAssetLibrary with the Python API?

Part of my addon's functionality is to replace local materials with their counterparts in linked libraries / assets.

In order to check if a material of the same name as a local material is available as an asset in the asset browser, I need some way to iterate through all assets of a given UserAssetLibrary and check for matches.

Unfortunately, I've not been able to find a method to get all assets contained in a UserAssetLibrary so far. After browsing the Data API it also doesn't seem like those are stored anywhere in the current BLEND file in the first place.

I suspect that since the asset browser seems to be a version of the file browser, I need to instead look into methods of getting a list of all files listed in the file browser. What I've found there has mostly been about finding the selcted file(s) though, not all files.

see:

Part of the function where this would be used below. Currently it only checks for linked files, but assets in the browser do not show up as materials in bpy.data.materials, and as such I cannot check for matches.

        for slot in obj.material_slots:
            if slot.material is not None and slot.material.library is None:
                old_material = slot.material
                new_material = None
                
                for mtl in bpy.data.materials:
                    # If an object is imported that has a material that already exists in the scene, it is numbered.
                    # Thus checking the substring of the name is necessary to catch all of them.
                    if mtl.library != None and ( mtl.name == old_material.name or mtl.name == old_material.name[:-4] ) and old_material.seut.overrideMatLib == False:
                        new_material = mtl
                        break                            

                if new_material != None:
                    slot.material = new_material
                    if old_material not in mtl_to_delete:
                        mtl_to_delete.append(old_material)