1
$\begingroup$

Similar to Python : Get selected objects in outliner

How can one access the assets that are currently selected in the asset browser editor using the python API ?

I assume it should be available on a per-editor basis since, contrary to the outliner where selection state is shared between all editors, different asset browser editors can have different asset selection states :

enter image description here

$\endgroup$
8
  • $\begingroup$ i cannot even find the "right" area type 😱 TypeError: bpy_struct: item.attr = val: enum "ASSET_BROWSER" not found in ('EMPTY', 'VIEW_3D', 'IMAGE_EDITOR', 'NODE_EDITOR', 'SEQUENCE_EDITOR', 'CLIP_EDITOR', 'DOPESHEET_EDITOR', 'GRAPH_EDITOR', 'NLA_EDITOR', 'TEXT_EDITOR', 'CONSOLE', 'INFO', 'TOPBAR', 'STATUSBAR', 'OUTLINER', 'PROPERTIES', 'FILE_BROWSER', 'SPREADSHEET', 'PREFERENCES') $\endgroup$
    – Chris
    Jan 11, 2022 at 16:38
  • $\begingroup$ have a look at startup/bl_operators/assets.py. It looks like you need something like active_asset = SpaceAssetInfo.get_active_asset(context) There only seems to be the concept of active and not selected? $\endgroup$ Jan 11, 2022 at 16:50
  • $\begingroup$ @Chris Yeah it's not .type it's .ui_type... was fooled by it too ! $\endgroup$
    – Gorgious
    Jan 11, 2022 at 18:53
  • 1
    $\begingroup$ The quote from the docs: "Avoid using this, it will be replaced..." made my day :D $\endgroup$
    – brockmann
    Feb 3, 2022 at 11:36
  • 1
    $\begingroup$ @brockmann Fast forward in 5 years when this quote is still there in the docs... xD $\endgroup$
    – Gorgious
    Feb 3, 2022 at 13:00

1 Answer 1

3
$\begingroup$

I found the answer while researching another answer.

It's actually an attribute of File context which unless I'm mistaken does require the piece of code to be executed inside the context of the asset browser.

Here's the slightly altered script that adds a button in the Asset Browser header that, when clicked, outputs some information about the selected assets in the console :

import bpy
from pathlib import Path


class PrintSelectedAssets(bpy.types.Operator):
    bl_idname = "asset.print_selected_assets"
    bl_label = "Print Selected Assets"

    @classmethod
    def poll(cls, context):
        return context.selected_asset_files

    def execute(self, context):
        current_library_name = context.area.spaces.active.params.asset_library_ref
        if current_library_name != "LOCAL":  # NOT Current file
            library_path = Path(context.preferences.filepaths.asset_libraries.get(current_library_name).path)

        for asset_file in context.selected_asset_files:
            if current_library_name == "LOCAL":
                print(f"{asset_file.local_id.name} is selected in the asset browser. (Local File)")
            else:
                asset_fullpath = library_path / asset_file.relative_path
                print(f"{asset_fullpath} is selected in the asset browser.")
                print(f"It is located in a user library named '{current_library_name}'")
            
        return {"FINISHED"}


def display_button(self, context):
    self.layout.operator(PrintSelectedAssets.bl_idname)


def register():
    bpy.utils.register_class(PrintSelectedAssets)
    bpy.types.ASSETBROWSER_MT_editor_menus.append(display_button)


def unregister():
    bpy.types.ASSETBROWSER_MT_editor_menus.remove(display_button)
    bpy.utils.unregister_class(PrintSelectedAssets)


if __name__ == "__main__":
    register()
$\endgroup$

You must log in to answer this question.

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