Key thing here is from an event - this answer does a great job at explaining how to get the list from inside the asset browser window, which is not what I am looking to do (unless I'm missing something).
For context: I am trying to create an add-on that assigns the currently selected material asset to the current object(s) via a shortcut.
In other words - I'd like the user to be able to
- select a material
- select a mesh
- press Shift-Ctrl-A and assign the material to the mesh
- select another mesh
- press Shift-Ctrl-A and assign the material to the new mesh
in python I know
- how to assign a material to a mesh
- how to detect events
but I cannot for the life of me figure how to get the currently selected assets from an event. Bold because I am aware of this answer on how do one gets the selected Assets in the Asset Browser using the API - but it doesn't seem to work unless I invoke the functionality from a button inside the asset browser (which is not what I am looking for).
I got this far:
import bpy
import os
from pathlib import Path
from bpy.props import (StringProperty,
BoolProperty,
IntProperty,
FloatProperty,
FloatVectorProperty,
EnumProperty,
PointerProperty,
)
from bpy.types import (Panel,
Menu,
Operator,
PropertyGroup,
)
class WM_OT_GetAssetContext(Operator):
bl_label = "Get asset context"
bl_idname = "wm.get_asset_context"
pass
def cycle_all_areas(context):
for screenArea in context.window.screen.areas:
if screenArea.type == 'FILE_BROWSER':
print('here')
params = screenArea.spaces.active.params
print(dir(params.bl_rna))
# context.area = screenArea
try:
print(context.selected_asset_files)
except Exception as e:
print(e)
pass
print(params.asset_library_ref)
break
class WM_OT_ModalOperator(Operator):
"""Move an object with the mouse, example"""
bl_idname = "object.modal_operator"
bl_label = "Simple Modal Operator"
@classmethod
def poll(cls, context):
return True
def modal(self, context, event):
if not context.mode == "OBJECT":
return {'PASS_THROUGH'}
if event.type in ["A"] and event.value == "PRESS" and event.ctrl and event.shift:
print("Assignment")
print('area', context.area)
print('space', context.space_data)
cycle_all_areas(context)
else:
return {'PASS_THROUGH'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
print('been invoked')
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
classes = (
WM_OT_GetAssetContext,
WM_OT_ModalOperator
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
if __name__ == "__main__":
register()
bpy.ops.object.modal_operator('INVOKE_DEFAULT')
but all the lines printing area and space data output None
and shen I press shift+ctrl-A
I get the following error:
Context' object has no attribute 'selected_asset_files`
What's the right way to achieve this?