UI: Add asset shelf Python UI template

Adds a new "Ui Asset Shelf" template to the script editor, demonstrating
how scripts can register own asset shelves. The asset shelf is designed
as a new standard UI element that add-ons, application templates and the
like can use, so this is useful. It's also useful as a quick way to test
and customize the asset shelf.

The script is only available in the UI if the "Asset Shelf" experimental
feature is enabled in the Preferences.
This commit is contained in:
Julian Eisel
2023-08-03 17:16:10 +02:00
parent 94fdafe906
commit 6764e69491
2 changed files with 33 additions and 1 deletions

View File

@@ -287,12 +287,18 @@ class TEXT_MT_text(Menu):
class TEXT_MT_templates_py(Menu):
bl_label = "Python"
def draw(self, _context):
def draw(self, context):
prefs = context.preferences
use_asset_shelf = prefs.experimental.use_asset_shelf
self.path_menu(
bpy.utils.script_paths(subdir="templates_py"),
"text.open",
props_default={"internal": True},
filter_ext=lambda ext: (ext.lower() == ".py"),
# Filter out asset shelf template depending on experimental "Asset Shelf" option.
filter_path=lambda path, use_asset_shelf=use_asset_shelf:
(use_asset_shelf or not path.endswith("ui_asset_shelf.py")),
)

View File

@@ -0,0 +1,26 @@
import bpy
class MyAssetShelf(bpy.types.AssetShelf):
bl_space_type = 'VIEW_3D'
bl_idname = "my_template.my_material_asset_shelf"
@classmethod
def poll(cls, context):
return bool(context.object and context.object.mode == 'OBJECT')
@classmethod
def asset_poll(cls, asset):
return asset.file_data.id_type in {'MATERIAL', 'OBJECT'}
def register():
bpy.utils.register_class(MyAssetShelf)
def unregister():
bpy.utils.unregister_class(MyAssetShelf)
if __name__ == "__main__":
register()