How to get it from the window area
>>> bpy.context.screen.areas[3].spaces.active.params.filename
'geometry_nodes\\procedural_hair_node_assets.blend\\NodeTree\\Braid Hair Curves'
If the asset browser area on the window has been identified, it is useful to learn the information from the console.
In the example,
it is "bpy.context.screen.areas[3]"
.
but the number will vary depending on the environment.
The area can be identified
by https://github.com/Pullusb/devTools
which is an add-on that provides a convenient button on the console.
Acquire information via operator
We already have the code for the answer, but some of the context data accesses have "mechanisms that can only be accessed via the operator".
For example, the following access is only accessible on the operator and not in the area
https://docs.blender.org/api/current/bpy.types.FileSelectEntry.html
Therefore, it is necessary to set up an access button in the area from which to obtain the divestment.
Of course, if you place this button anywhere other than on the asset browser, it will not work.
import bpy
from pathlib import Path
# breakpoint = bpy.types.BP_PT_bp.bp
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):
for asset in context.selected_asset_files:
asset_re_path=asset.relative_path
print("### asset path :",asset_re_path)
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.utils.unregister_class(PrintSelectedAssets)
bpy.types.ASSETBROWSER_MT_editor_menus.remove(display_button)
if __name__ == "__main__":
register()
For blender4.0
There seems to have been a change in the API.
The following are the changes
From
selected_asset_files
To
selected_assets
https://docs.blender.org/api/current/bpy.context.html#bpy.context.selected_assets
https://docs.blender.org/api/current/bpy.types.AssetRepresentation.html#bpy.types.AssetRepresentation
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_assets
def execute(self, context):
for asset in context.selected_assets:
asset_path=asset.full_path
print("### asset :",asset_path)
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():
for f in bpy.types.ASSETBROWSER_MT_editor_menus._dyn_ui_initialize():
if f.__name__ == display_button.__name__:
bpy.types.ASSETBROWSER_MT_editor_menus.remove(f)
return
if __name__ == "__main__":
unregister()
register()
startup/bl_operators/assets.py
. It looks like you need something likeactive_asset = SpaceAssetInfo.get_active_asset(context)
There only seems to be the concept of active and not selected? $\endgroup$.type
it's.ui_type
... was fooled by it too ! $\endgroup$