7
$\begingroup$

Via python, how do I create tags for an asset and then assign it to a catalog?

I'm able to mark the asset with this line:

bpy.data.materials["Test Material"].asset_mark()

but unable to set tags or categories with anything similar:

bpy.data.materials["Test Material"].tag_add("Tag A") #Incorrect example
bpy.data.materials["Test Material"].catalog_set("Materials A") #Incorrect example

I'm not seeing an option to set the specific catalog in the documentation, and also confused with how to even use the asset operators per the documentation such as:

bpy.ops.asset.tag_add()

Any help or direction would be great. Thanks!

$\endgroup$
2
  • $\begingroup$ You may want to look at the source for this add-on to see if it will help. It looks like there's not much documentation or tutorial material on using the Asset Browser from Python yet. $\endgroup$ Commented Jan 6, 2022 at 18:40
  • $\begingroup$ Awesome, thanks Marty. I'll check that out this afternoon, looks promising! $\endgroup$
    – mtlevison
    Commented Jan 6, 2022 at 19:03

1 Answer 1

12
$\begingroup$

You can access and edit tags without operators, using the asset meta data from any ID type :

import bpy

obj = bpy.data.objects["Suzanne"]  # Object name, case-sensitive !
asset_data = obj.asset_data

new_tag = asset_data.tags.new("Awesome Tag Name")  # Add a new tag
new_tag.name = "Other Tag Name"  # Change tag name

asset_data.tags[-1].name = "Yet Another Tag Name"  # Change last tag you added
asset_data.tags[0].name = "One Last Tag Name"  # Change tag #0

It's a bit harder to access and edit catalogs. Since different catalogs can share the same name, you have to access them by their UUID (unique identifier).

They are saved to disk on another file in the same folder as your blend file, or in the root folder of your asset library.

Let's say for simplicity that you want to add an asset to the first catalog named "Catalog" it finds in the file :

from pathlib import Path
import bpy

folder = Path(bpy.data.filepath).parent
target_catalog = "Catalog"

with (folder / "blender_assets.cats.txt").open() as f:
    for line in f.readlines():
        if line.startswith(("#", "VERSION", "\n")):
            continue
        # Each line contains : 'uuid:catalog_tree:catalog_name' + eol ('\n')
        name = line.split(":")[2].split("\n")[0]
        if name == target_catalog:
            uuid = line.split(":")[0]
            obj = bpy.data.objects["Suzanne"]  # Object name, case-sensitive !
            asset_data = obj.asset_data
            asset_data.catalog_id = uuid

Another alternative method to retrieve specific catalog information, if you already have an asset in your file in the catalog you're looking for. This doesn't require you to locate the catalog definition file. It uses bpy.types.AssetMetaData.catalog_id.

import bpy

obj = bpy.data.objects["Suzanne"]  # Object name, case-sensitive !
asset_data = obj.asset_data

other_asset = bpy.data.objects["OtherAsset"]
other_asset_data = other_asset.asset_data

asset_data.catalog_id = other_asset_data.catalog_id
$\endgroup$
2
  • 1
    $\begingroup$ Wow, thank you so much Gorgious! Everything here was exactly what I was after and really appreciate the links to the wiki, too. I wouldn't have considered needing to look for the catalog UUID info in the TXT file. Thanks again for your time and help! $\endgroup$
    – mtlevison
    Commented Jan 6, 2022 at 22:47
  • $\begingroup$ Happy to help ! I discovered it thanks to your question. Cheers :) $\endgroup$
    – Gorgious
    Commented Jan 6, 2022 at 23:18

You must log in to answer this question.

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