3
$\begingroup$

This link specifies how to add a new tag to an asset present in current file.

After adding a new asset library, I want to add a tag to the assets present in that library.

This link mentions that it is not possible through Blender UI and it also mentions that it is possible through add-ons.

asset_libraries = bpy.context.preferences.filepaths.asset_libraries
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()]

    for blend_file in blend_files:
        with bpy.data.libraries.load(str(blend_file)) as (file_contents, data_to):
            print('file_content objects : ', file_contents.objects)

The above code prints an array of strings, i.e., all object names.

I want access to the object itself, in order to add a new tag to it.

Any help or direction would be great. Thanks!

$\endgroup$

1 Answer 1

5
$\begingroup$

bpy.data.libraries.load lets you import items in your current file, but it doesn't let you modify the data in the other files (AFAIK). So unfortunately you do have to individually open all these files, add the tags, then save the files.

import bpy
from pathlib import Path

tag_name = "My new tag"

asset_libraries = bpy.context.preferences.filepaths.asset_libraries
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()]

    for blend_file in blend_files:
        bpy.ops.wm.open_mainfile(filepath=str(blend_file))
        for obj in bpy.data.objects:
            if not obj.asset_data:
                continue
            obj.asset_data.tags.new(tag_name, skip_if_exists=True)
        bpy.ops.wm.save_mainfile()

Link to the docs

Plugging my (free) addon which lets you batch set tags, author and other good stuff from libraries from the Asset Browser interface :

https://github.com/Gorgious56/asset_browser_utilities

$\endgroup$
5
  • $\begingroup$ I have updated the code and it is crashing on open_mainfile() in add-on. $\endgroup$ Commented Jan 24, 2022 at 13:21
  • 1
    $\begingroup$ @KarthikNagaraj It's best not to modify the question too much because then the answers becomes irrelevant :) Without seeing more of your addon code it's hard to say why exactly this happens. Usually these kind of crashes come from the context not being initialized correctly. Using a timer to pause the script a little bit can help : docs.blender.org/api/current/bpy.app.timers.html $\endgroup$
    – Gorgious
    Commented Jan 24, 2022 at 13:25
  • $\begingroup$ 2) I am using from pathlib import Path whereas, your code uses from pathutils import Path and usind your import gives me error: ModuleNotFoundError: No module named 'pathutils' $\endgroup$ Commented Jan 24, 2022 at 13:26
  • 1
    $\begingroup$ With a couple of timers, I was able to open multiple blend files one by one, add tags to all assets, save the blend files and re-open the original file(where the script execution started). Thanks @Gorgious $\endgroup$ Commented Jan 25, 2022 at 9:23
  • $\begingroup$ @KarthikNagaraj glad it helped ! If you want to, you can modify my answer or add a new one to showcase your solution, might help other people in the future. Cheers :) $\endgroup$
    – Gorgious
    Commented Jan 25, 2022 at 9:54

You must log in to answer this question.

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