2
$\begingroup$

I have a giant asset browser with over 2000 items. Some of the items have tags (product numbers), but not many. I would now like to output all the names of the items and their stored tags so that I can then enter all the entries in Excel, find the matching product numbers manually and then maintain the asset library and add product numbers as tags. I already have a script that outputs all names and writes them to a text file. However, this must now be extended to get and include the stored tags.

import bpy
from pathlib import Path

output_file_path = Path("/tmp/asset_library_contents.txt")

prefs = bpy.context.preferences
filepaths = prefs.filepaths
asset_libraries = filepaths.asset_libraries

with output_file_path.open("w") as output_file:
    for asset_library in asset_libraries:
        library_name = asset_library.name
        library_path = Path(asset_library.path)
        blend_files = [fp for fp in library_path.glob("**/*.blend") if fp.is_file()]
        output_file.write(f"Checking the content of library '{library_name}' :\n")
        
        for blend_file in blend_files:
            with bpy.data.libraries.load(str(blend_file), assets_only=True) as (file_contents, _):
                output_file.write(f"File: {blend_file.name}\n")
                output_file.write("Objects:\n")
                for obj in file_contents.objects:
                    output_file.write(f" - {obj}\n")
                output_file.write("Collections:\n")
                for col in file_contents.collections:
                    output_file.write(f" - {col}\n")
                output_file.write("\n")
```
$\endgroup$

1 Answer 1

0
$\begingroup$

I'm pretty sure the only two ways of accessing an asset tag are 1. open the asset blend file (not realistic if you have thousands of them) or 2. Read the information from the asset browser in an open blend file (it can be any blend file, so long as it has an asset browser open).

Since you're interested in seeing all your assets and their tags you can go to the "All" asset browser library that contains all the available assets. I don't know of a trivial way to get the list of all the assets in this special library and it doesn't really matter to the core of the question so I will assume you followed these steps prior to running the script :

  • Open a new blend file
  • Make sure a text editor and an asset browser editor are open in your current workspace
  • Set the library to "All" in the top left of the asset browser editor
  • Select every single asset with Select > All or A while hovering over the assets
  • Copy / paste the script in a new text data block in the text editor
  • Run the script
  • Open the console with Window > Toggle System Console (On Windows)
  • ?? profit
import bpy

asset_browser_area = next(a for a in bpy.context.screen.areas if a.ui_type == "ASSETS")

with bpy.context.temp_override(area=asset_browser_area):
    # We must override the area otherwise we can't access context.selected_assets
    for asset in bpy.context.selected_assets:
        print(f"{asset.name=}")
        for i, tag in enumerate(asset.metadata.tags):
            print(f"  Tag n°{i} : {tag.name}")
$\endgroup$
2
  • 1
    $\begingroup$ Thanks a million this will do the trick. And as I hopefully don't have to execute this task this often, its totally fine, to do the extra steps before. $\endgroup$
    – Erdorano
    Commented May 22 at 20:18
  • $\begingroup$ @Erdorano Cheers. FWIW I'm still looking for a way to get all assets in an asset browser library without having to parse the files on disk or having to select them first. I will update my answer if I ever find it ;) $\endgroup$
    – Gorgious
    Commented May 23 at 6:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .