Assets: Expose asset representation in context RNA/BPY

Makes the asset representation type available in RNA/BPY context
whenever the asset handle type is, so that it can be used instead. See
d04cd3f3e6. With this change we can now replace virtually all usages of
the asset handle type in Python with the asset representation. (Only for
the asset view template we require a collection property taking asset
handles still, for internal reasons.)

Idea is now to first get rid of all usages of asset handle in Python,
so that there's almost no need for further compatibility breaking
changes (unsure if the asset view template can be removed for 4.0
already though). Internal hacks related to it can be removed at any time
still.

Part of #102877 and #108806.
This commit is contained in:
Julian Eisel 2023-09-19 12:50:27 +02:00
parent 3a4c238d50
commit f6a6b27ac1
2 changed files with 25 additions and 2 deletions

@ -1514,8 +1514,20 @@ AssetHandle CTX_wm_asset_handle(const bContext *C, bool *r_is_valid)
blender::asset_system::AssetRepresentation *CTX_wm_asset(const bContext *C)
{
return static_cast<blender::asset_system::AssetRepresentation *>(
ctx_data_pointer_get(C, "asset"));
if (auto *asset = static_cast<blender::asset_system::AssetRepresentation *>(
ctx_data_pointer_get(C, "asset")))
{
return asset;
}
/* Expose the asset representation from the asset-handle.
* TODO(Julian): #AssetHandle should be properly replaced by #AssetRepresentation. */
bool is_valid;
if (AssetHandle handle = CTX_wm_asset_handle(C, &is_valid); is_valid) {
return handle.file_data->asset;
}
return nullptr;
}
Depsgraph *CTX_data_depsgraph_pointer(const bContext *C)

@ -125,6 +125,12 @@ static PointerRNA rna_Context_gizmo_group_get(PointerRNA *ptr)
return newptr;
}
static PointerRNA rna_Context_asset_get(PointerRNA *ptr)
{
bContext *C = (bContext *)ptr->data;
return RNA_pointer_create(nullptr, &RNA_AssetRepresentation, CTX_wm_asset(C));
}
static PointerRNA rna_Context_asset_file_handle_get(PointerRNA *ptr)
{
bContext *C = (bContext *)ptr->data;
@ -290,6 +296,11 @@ void RNA_def_context(BlenderRNA *brna)
RNA_def_property_struct_type(prop, "GizmoGroup");
RNA_def_property_pointer_funcs(prop, "rna_Context_gizmo_group_get", nullptr, nullptr, nullptr);
prop = RNA_def_property(srna, "asset", PROP_POINTER, PROP_NONE);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_struct_type(prop, "AssetRepresentation");
RNA_def_property_pointer_funcs(prop, "rna_Context_asset_get", nullptr, nullptr, nullptr);
/* TODO can't expose AssetHandle, since there is no permanent storage to it (so we can't
* return a pointer). Instead provide the FileDirEntry pointer it wraps. */
prop = RNA_def_property(srna, "asset_file_handle", PROP_POINTER, PROP_NONE);