Marking an Asset with Python

I am trying to automate the creation of assets in a Blender file, but when I try to call bpy.ops.asset.mark() it reports “No data-block selected that supports asset operation” even though an object is selected.

I assume because this is only intended to be called from the outliner. I have tried overriding the context, but haven’t be able to figure it out.

You can reproduce this by typing:

import bpy
bpy.ops.asset.mark()

Into the scripting interface and running the script.

Does anyone know of a way to mark an asset using the python API?

Can confirm.

You can, however, mark an object as asset by calling its asset_mark method, like so:
bpy.data.objects['my_object'].asset_mark()

4 Likes

I missed this in the API. Thank you so much! That is exactly what I was looking for.

1 Like

unfortunately after using “only” that command, the preview in the asset browser is missing… :frowning:

Call bpy.data.objects['my_object'].asset_generate_preview() right afterwards.

And just in case you’d like to edit asset data…

bpy.data.objects['my_object'].asset_data.description
bpy.data.objects['my_object'].asset_data.tags
bpy.data.objects['my_object'].asset_data.author
bpy.data.objects['my_object'].asset_data.catalog_id

…might be the next things you’ll need.

1 Like

great, thank you!!!