From d421ebac5e31b231c8b53e5d640d631e7b3c5f34 Mon Sep 17 00:00:00 2001 From: Julian Eisel Date: Fri, 15 Sep 2023 15:47:12 +0200 Subject: [PATCH] Assets: Expose ID type in asset representation RNA for Python access Previously Python code would require the hacky `AssetHandle` type and query the ID type through the file data it contained (like `asset_handle.file_data.id_type`). We want to get rid of `AssetHandle`. With this commit we can change asset shelf methods like `AssetShelf.asset_poll()` to take an `AssetRepresentation` instead of `AssetHandle`, since the ID type is currently the only relevant part. This is much nicer and won't require breaking compatibility in future to deprecate `AssetHandle`. --- source/blender/makesrna/intern/rna_asset.cc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/source/blender/makesrna/intern/rna_asset.cc b/source/blender/makesrna/intern/rna_asset.cc index 03a4e364368..8a9c4cba37f 100644 --- a/source/blender/makesrna/intern/rna_asset.cc +++ b/source/blender/makesrna/intern/rna_asset.cc @@ -8,6 +8,8 @@ #include +#include "BLT_translation.h" + #include "RNA_define.hh" #include "RNA_enum_types.hh" @@ -380,6 +382,14 @@ static PointerRNA rna_AssetHandle_local_id_get(PointerRNA *ptr) return rna_pointer_inherit_refine(ptr, &RNA_ID, id); } +static int rna_AssetRepresentation_id_type_get(PointerRNA *ptr) +{ + using namespace blender::asset_system; + + const AssetRepresentation *asset = static_cast(ptr->data); + return asset->get_id_type(); +} + const EnumPropertyItem *rna_asset_library_reference_itemf(bContext * /*C*/, PointerRNA * /*ptr*/, PropertyRNA * /*prop*/, @@ -585,12 +595,23 @@ static void rna_def_asset_handle(BlenderRNA *brna) static void rna_def_asset_representation(BlenderRNA *brna) { StructRNA *srna; + PropertyRNA *prop; srna = RNA_def_struct(brna, "AssetRepresentation", nullptr); RNA_def_struct_ui_text(srna, "Asset Representation", "Information about an entity that makes it possible for the asset system " "to deal with the entity as asset"); + + prop = RNA_def_property(srna, "id_type", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_items(prop, rna_enum_id_type_items); + RNA_def_property_enum_funcs(prop, "rna_AssetRepresentation_id_type_get", nullptr, nullptr); + RNA_def_property_clear_flag(prop, PROP_EDITABLE); + RNA_def_property_ui_text( + prop, + "Data-block Type", + "The type of the data-block, if the asset represents one ('NONE' otherwise)"); + RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_ID); } static void rna_def_asset_catalog_path(BlenderRNA *brna)