Skip to main content
added 1658 characters in body
Source Link
Gorgious
  • 33.3k
  • 2
  • 54
  • 107

Blender 4.0+

Blender 4.0 introduces bpy.types.AssetRepresentation, bpy.context.selected_assets, bpy.context.asset we can use to our advantage.

import bpy class PrintSelectedAssets(bpy.types.Operator): bl_idname = "asset.print_selected_assets" bl_label = "Print Selected Assets" @classmethod def poll(cls, context): return context.selected_assets def execute(self, context): for asset_representation in context.selected_assets: print(f"{asset_representation.full_path=}") print(f"{asset_representation.full_library_path=}") print(f"{asset_representation.id_type=}") print(f"{asset_representation.name=}") # This will be None if the asset is not located in current file : print(f"{asset_representation.local_id=}") print(context.asset) # Active asset 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() 

Blender 3.6

Blender 4.0+

Blender 4.0 introduces bpy.types.AssetRepresentation, bpy.context.selected_assets, bpy.context.asset we can use to our advantage.

import bpy class PrintSelectedAssets(bpy.types.Operator): bl_idname = "asset.print_selected_assets" bl_label = "Print Selected Assets" @classmethod def poll(cls, context): return context.selected_assets def execute(self, context): for asset_representation in context.selected_assets: print(f"{asset_representation.full_path=}") print(f"{asset_representation.full_library_path=}") print(f"{asset_representation.id_type=}") print(f"{asset_representation.name=}") # This will be None if the asset is not located in current file : print(f"{asset_representation.local_id=}") print(context.asset) # Active asset 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() 

Blender 3.6

Source Link
Gorgious
  • 33.3k
  • 2
  • 54
  • 107

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()