Asset shelf: Use asset representation for asset shelf BPY methods

Changes the `asset_poll()` and `draw_context_menu()` methods for asset
shelves to use the `AssetRepresentation` type, instead of `AssetHandle`.
The latter should be removed, so it's better to avoid using it in the
asset shelf BPY to avoid future compatibility breakage. This is possible
now with d421ebac5e.
This commit is contained in:
Julian Eisel
2023-09-15 16:17:44 +02:00
parent d421ebac5e
commit 728d47f3e4
10 changed files with 38 additions and 23 deletions

View File

@@ -8433,7 +8433,7 @@ class VIEW3D_AST_sculpt_brushes(bpy.types.AssetShelf):
@classmethod @classmethod
def asset_poll(cls, asset): def asset_poll(cls, asset):
return asset.file_data.id_type == 'BRUSH' return asset.id_type == 'BRUSH'
classes = ( classes = (

View File

@@ -11,7 +11,7 @@ class MyAssetShelf(bpy.types.AssetShelf):
@classmethod @classmethod
def asset_poll(cls, asset): def asset_poll(cls, asset):
return asset.file_data.id_type in {'MATERIAL', 'OBJECT'} return asset.id_type in {'MATERIAL', 'OBJECT'}
def register(): def register():

View File

@@ -13,6 +13,15 @@
#include "BKE_context.h" #include "BKE_context.h"
#ifdef __cplusplus
namespace blender::asset_system {
class AssetRepresentation;
}
using AssetRepresentationHandle = blender::asset_system::AssetRepresentation;
#else
typedef struct AssetRepresentationHandle AssetRepresentationHandle;
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@@ -464,12 +473,13 @@ typedef struct AssetShelfType {
/** Determine if an individual asset should be visible or not. May be a temporary design, /** Determine if an individual asset should be visible or not. May be a temporary design,
* visibility should first and foremost be controlled by asset traits. */ * visibility should first and foremost be controlled by asset traits. */
bool (*asset_poll)(const struct AssetShelfType *shelf_type, const struct AssetHandle *asset); bool (*asset_poll)(const struct AssetShelfType *shelf_type,
const AssetRepresentationHandle *asset);
/** Asset shelves can define their own context menu via this layout definition callback. */ /** Asset shelves can define their own context menu via this layout definition callback. */
void (*draw_context_menu)(const struct bContext *C, void (*draw_context_menu)(const struct bContext *C,
const struct AssetShelfType *shelf_type, const struct AssetShelfType *shelf_type,
const struct AssetHandle *asset, const AssetRepresentationHandle *asset,
struct uiLayout *layout); struct uiLayout *layout);
/* RNA integration */ /* RNA integration */

View File

@@ -18,7 +18,6 @@
#include "AS_asset_catalog_tree.hh" #include "AS_asset_catalog_tree.hh"
struct AssetFilterSettings; struct AssetFilterSettings;
struct AssetHandle;
struct AssetLibraryReference; struct AssetLibraryReference;
struct bContext; struct bContext;
@@ -55,7 +54,7 @@ struct AssetItemTree {
asset_system::AssetCatalogTree build_filtered_catalog_tree( asset_system::AssetCatalogTree build_filtered_catalog_tree(
const asset_system::AssetLibrary &library, const asset_system::AssetLibrary &library,
const AssetLibraryReference &library_ref, const AssetLibraryReference &library_ref,
blender::FunctionRef<bool(const AssetHandle &)> is_asset_visible_fn); blender::FunctionRef<bool(const asset_system::AssetRepresentation &)> is_asset_visible_fn);
AssetItemTree build_filtered_all_catalog_tree( AssetItemTree build_filtered_all_catalog_tree(
const AssetLibraryReference &library_ref, const AssetLibraryReference &library_ref,
const bContext &C, const bContext &C,

View File

@@ -55,17 +55,17 @@ namespace blender::ed::asset {
asset_system::AssetCatalogTree build_filtered_catalog_tree( asset_system::AssetCatalogTree build_filtered_catalog_tree(
const asset_system::AssetLibrary &library, const asset_system::AssetLibrary &library,
const AssetLibraryReference &library_ref, const AssetLibraryReference &library_ref,
const blender::FunctionRef<bool(const AssetHandle &)> is_asset_visible_fn) const blender::FunctionRef<bool(const asset_system::AssetRepresentation &)>
is_asset_visible_fn)
{ {
Set<StringRef> known_paths; Set<StringRef> known_paths;
/* Collect paths containing assets. */ /* Collect paths containing assets. */
ED_assetlist_iterate(library_ref, [&](AssetHandle asset_handle) { ED_assetlist_iterate(library_ref, [&](asset_system::AssetRepresentation &asset) {
if (!is_asset_visible_fn(asset_handle)) { if (!is_asset_visible_fn(asset)) {
return true; return true;
} }
asset_system::AssetRepresentation &asset = *ED_asset_handle_get_representation(&asset_handle);
const AssetMetaData &meta_data = asset.get_metadata(); const AssetMetaData &meta_data = asset.get_metadata();
if (BLI_uuid_is_nil(meta_data.catalog_id)) { if (BLI_uuid_is_nil(meta_data.catalog_id)) {
return true; return true;

View File

@@ -104,12 +104,13 @@ void AssetView::build_items()
} }
ED_assetlist_iterate(library_ref_, [&](AssetHandle asset_handle) { ED_assetlist_iterate(library_ref_, [&](AssetHandle asset_handle) {
if (shelf_.type->asset_poll && !shelf_.type->asset_poll(shelf_.type, &asset_handle)) { const asset_system::AssetRepresentation *asset = ED_asset_handle_get_representation(
&asset_handle);
if (shelf_.type->asset_poll && !shelf_.type->asset_poll(shelf_.type, asset)) {
return true; return true;
} }
const asset_system::AssetRepresentation *asset = ED_asset_handle_get_representation(
&asset_handle);
const AssetMetaData &asset_data = asset->get_metadata(); const AssetMetaData &asset_data = asset->get_metadata();
if (catalog_filter_ && !catalog_filter_->contains(asset_data.catalog_id)) { if (catalog_filter_ && !catalog_filter_->contains(asset_data.catalog_id)) {
@@ -213,7 +214,8 @@ void AssetViewItem::build_context_menu(bContext &C, uiLayout &column) const
const AssetView &asset_view = dynamic_cast<const AssetView &>(get_view()); const AssetView &asset_view = dynamic_cast<const AssetView &>(get_view());
const AssetShelfType &shelf_type = *asset_view.shelf_.type; const AssetShelfType &shelf_type = *asset_view.shelf_.type;
if (shelf_type.draw_context_menu) { if (shelf_type.draw_context_menu) {
shelf_type.draw_context_menu(&C, &shelf_type, &asset_, &column); asset_system::AssetRepresentation *asset = ED_asset_handle_get_representation(&asset_);
shelf_type.draw_context_menu(&C, &shelf_type, asset, &column);
} }
} }

View File

@@ -45,8 +45,10 @@ class AssetCatalogSelectorTree : public ui::AbstractTreeView {
: shelf_(shelf), shelf_settings_(shelf_.settings) : shelf_(shelf), shelf_settings_(shelf_.settings)
{ {
catalog_tree_ = build_filtered_catalog_tree( catalog_tree_ = build_filtered_catalog_tree(
library, asset_system::all_library_reference(), [this](const AssetHandle asset_handle) { library,
return (!shelf_.type->asset_poll || shelf_.type->asset_poll(shelf_.type, &asset_handle)); asset_system::all_library_reference(),
[this](const asset_system::AssetRepresentation &asset) {
return (!shelf_.type->asset_poll || shelf_.type->asset_poll(shelf_.type, &asset));
}); });
} }

View File

@@ -23,7 +23,6 @@
struct ARegion; struct ARegion;
struct AssetFilterSettings; struct AssetFilterSettings;
struct AssetRepresentation;
struct AutoComplete; struct AutoComplete;
struct EnumPropertyItem; struct EnumPropertyItem;
struct FileSelectParams; struct FileSelectParams;

View File

@@ -610,6 +610,8 @@ static void rna_def_asset_representation(BlenderRNA *brna)
RNA_def_property_ui_text( RNA_def_property_ui_text(
prop, prop,
"Data-block Type", "Data-block Type",
/* Won't ever actually return 'NONE' currently, this is just for information for once non-ID
* assets are supported. */
"The type of the data-block, if the asset represents one ('NONE' otherwise)"); "The type of the data-block, if the asset represents one ('NONE' otherwise)");
RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_ID); RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_ID);
} }

View File

@@ -1071,7 +1071,8 @@ static StructRNA *rna_Menu_refine(PointerRNA *mtr)
/* Asset Shelf */ /* Asset Shelf */
static bool asset_shelf_asset_poll(const AssetShelfType *shelf_type, const AssetHandle *asset) static bool asset_shelf_asset_poll(const AssetShelfType *shelf_type,
const AssetRepresentationHandle *asset)
{ {
extern FunctionRNA rna_AssetShelf_asset_poll_func; extern FunctionRNA rna_AssetShelf_asset_poll_func;
@@ -1080,7 +1081,7 @@ static bool asset_shelf_asset_poll(const AssetShelfType *shelf_type, const Asset
ParameterList list; ParameterList list;
RNA_parameter_list_create(&list, &ptr, func); RNA_parameter_list_create(&list, &ptr, func);
RNA_parameter_set_lookup(&list, "asset_handle", &asset); RNA_parameter_set_lookup(&list, "asset", &asset);
shelf_type->rna_ext.call(nullptr, &ptr, func, &list); shelf_type->rna_ext.call(nullptr, &ptr, func, &list);
void *ret; void *ret;
@@ -1117,7 +1118,7 @@ static bool asset_shelf_poll(const bContext *C, const AssetShelfType *shelf_type
static void asset_shelf_draw_context_menu(const bContext *C, static void asset_shelf_draw_context_menu(const bContext *C,
const AssetShelfType *shelf_type, const AssetShelfType *shelf_type,
const AssetHandle *asset, const AssetRepresentationHandle *asset,
uiLayout *layout) uiLayout *layout)
{ {
extern FunctionRNA rna_AssetShelf_draw_context_menu_func; extern FunctionRNA rna_AssetShelf_draw_context_menu_func;
@@ -1130,7 +1131,7 @@ static void asset_shelf_draw_context_menu(const bContext *C,
ParameterList list; ParameterList list;
RNA_parameter_list_create(&list, &ptr, func); RNA_parameter_list_create(&list, &ptr, func);
RNA_parameter_set_lookup(&list, "context", &C); RNA_parameter_set_lookup(&list, "context", &C);
RNA_parameter_set_lookup(&list, "asset_handle", &asset); RNA_parameter_set_lookup(&list, "asset", &asset);
RNA_parameter_set_lookup(&list, "layout", &layout); RNA_parameter_set_lookup(&list, "layout", &layout);
shelf_type->rna_ext.call((bContext *)C, &ptr, func, &list); shelf_type->rna_ext.call((bContext *)C, &ptr, func, &list);
@@ -2143,7 +2144,7 @@ static void rna_def_asset_shelf(BlenderRNA *brna)
"non-null output, the asset will be visible"); "non-null output, the asset will be visible");
RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_REGISTER_OPTIONAL); RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_REGISTER_OPTIONAL);
RNA_def_function_return(func, RNA_def_boolean(func, "visible", true, "", "")); RNA_def_function_return(func, RNA_def_boolean(func, "visible", true, "", ""));
parm = RNA_def_pointer(func, "asset_handle", "AssetHandle", "", ""); parm = RNA_def_pointer(func, "asset", "AssetRepresentation", "", "");
RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED); RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED);
func = RNA_def_function(srna, "draw_context_menu", nullptr); func = RNA_def_function(srna, "draw_context_menu", nullptr);
@@ -2152,7 +2153,7 @@ static void rna_def_asset_shelf(BlenderRNA *brna)
RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_REGISTER_OPTIONAL); RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_REGISTER_OPTIONAL);
parm = RNA_def_pointer(func, "context", "Context", "", ""); parm = RNA_def_pointer(func, "context", "Context", "", "");
RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED); RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED);
parm = RNA_def_pointer(func, "asset_handle", "AssetHandle", "", ""); parm = RNA_def_pointer(func, "asset", "AssetRepresentation", "", "");
RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED); RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED);
parm = RNA_def_pointer(func, "layout", "UILayout", "", ""); parm = RNA_def_pointer(func, "layout", "UILayout", "", "");
RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED); RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED);