From 6764e69491216ee4cfac654af6be5f2cb73fe699 Mon Sep 17 00:00:00 2001 From: Julian Eisel Date: Thu, 3 Aug 2023 17:16:10 +0200 Subject: [PATCH] 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. --- scripts/startup/bl_ui/space_text.py | 8 +++++++- scripts/templates_py/ui_asset_shelf.py | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 scripts/templates_py/ui_asset_shelf.py diff --git a/scripts/startup/bl_ui/space_text.py b/scripts/startup/bl_ui/space_text.py index a01dbb46651..3375645f566 100644 --- a/scripts/startup/bl_ui/space_text.py +++ b/scripts/startup/bl_ui/space_text.py @@ -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")), ) diff --git a/scripts/templates_py/ui_asset_shelf.py b/scripts/templates_py/ui_asset_shelf.py new file mode 100644 index 00000000000..dd5b3af1fae --- /dev/null +++ b/scripts/templates_py/ui_asset_shelf.py @@ -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()