Refactor: Assets: Avoid hacky asset handle context query

The `AssetRepresentation` should now be available directly in context
where previously the `AssetHandle` type was required.

By adding an `"asset"` context member to the file browser, this should
avoid any behavior change. (Previously we would get the asset from
context by constructing a asset handle from the "active_file" context
member. So make sure "asset" is available wherever "active_file" is.)

This is another important step towards removing `AssetHandle`, see
blender/blender#108806.
This commit is contained in:
Julian Eisel 2025-06-12 16:53:11 +02:00
parent 767d86b543
commit 3f9c943243
2 changed files with 11 additions and 22 deletions

View File

@ -1529,20 +1529,6 @@ const AssetLibraryReference *CTX_wm_asset_library_ref(const bContext *C)
return static_cast<AssetLibraryReference *>(ctx_data_pointer_get(C, "asset_library_reference"));
}
static AssetHandle ctx_wm_asset_handle(const bContext *C, bool *r_is_valid)
{
/* TODO remove and use #AssetRepresentation instead. */
FileDirEntry *file =
(FileDirEntry *)CTX_data_pointer_get_type(C, "active_file", &RNA_FileSelectEntry).data;
if (file && file->asset) {
*r_is_valid = true;
return AssetHandle{file};
}
*r_is_valid = false;
return AssetHandle{nullptr};
}
blender::asset_system::AssetRepresentation *CTX_wm_asset(const bContext *C)
{
if (auto *asset = static_cast<blender::asset_system::AssetRepresentation *>(
@ -1550,14 +1536,6 @@ blender::asset_system::AssetRepresentation *CTX_wm_asset(const bContext *C)
{
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;
}

View File

@ -20,6 +20,7 @@ const char *file_context_dir[] = {
"active_file",
"selected_files",
"asset_library_reference",
"asset",
"selected_assets",
"id",
"selected_ids",
@ -79,6 +80,16 @@ int /*eContextResult*/ file_context(const bContext *C,
result, &screen->id, &RNA_AssetLibraryReference, &asset_params->asset_library_ref);
return CTX_RESULT_OK;
}
if (CTX_data_equals(member, "asset")) {
FileDirEntry *file = filelist_file(sfile->files, params->active_file);
if (file == nullptr) {
return CTX_RESULT_NO_DATA;
}
CTX_data_pointer_set(result, &screen->id, &RNA_AssetRepresentation, file->asset);
return CTX_RESULT_OK;
}
if (CTX_data_equals(member, "selected_assets")) {
const int num_files_filtered = filelist_files_ensure(sfile->files);