Refactor: Use asset representation from context instead of asset handle

f6a6b27ac1 made the asset representation type available through context
wherever asset handle was previously. This moves us closer to replacing
the asset handle type.

Part of #102877 and #108806.
This commit is contained in:
Julian Eisel 2023-09-19 15:41:15 +02:00
parent 954ae19b2b
commit 733e4d827a
6 changed files with 29 additions and 17 deletions

View File

@ -413,8 +413,6 @@ bool CTX_data_editable_gpencil_layers(const bContext *C, ListBase *list);
bool CTX_data_editable_gpencil_strokes(const bContext *C, ListBase *list);
const struct AssetLibraryReference *CTX_wm_asset_library_ref(const bContext *C);
struct AssetHandle CTX_wm_asset_handle(const bContext *C, bool *r_is_valid);
#ifdef __cplusplus
class blender::asset_system::AssetRepresentation *CTX_wm_asset(const bContext *C);
#endif

View File

@ -1487,7 +1487,7 @@ const AssetLibraryReference *CTX_wm_asset_library_ref(const bContext *C)
return static_cast<AssetLibraryReference *>(ctx_data_pointer_get(C, "asset_library_ref"));
}
AssetHandle CTX_wm_asset_handle(const bContext *C, bool *r_is_valid)
static AssetHandle ctx_wm_asset_handle(const bContext *C, bool *r_is_valid)
{
AssetHandle *asset_handle_p =
(AssetHandle *)CTX_data_pointer_get_type(C, "asset_handle", &RNA_AssetHandle).data;
@ -1523,7 +1523,7 @@ blender::asset_system::AssetRepresentation *CTX_wm_asset(const bContext *C)
/* 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) {
if (AssetHandle handle = ctx_wm_asset_handle(C, &is_valid); is_valid) {
return handle.file_data->asset;
}

View File

@ -9,6 +9,8 @@
#include <cmath>
#include <cstring>
#include "AS_asset_representation.hh"
#include "MEM_guardedalloc.h"
#include "BLI_string.h"
@ -285,14 +287,11 @@ static void poselib_tempload_exit(PoseBlendData *pbd)
static bAction *poselib_blend_init_get_action(bContext *C, wmOperator *op)
{
bool asset_handle_valid;
const AssetHandle asset_handle = CTX_wm_asset_handle(C, &asset_handle_valid);
/* Poll callback should check. */
BLI_assert(asset_handle_valid);
const AssetRepresentationHandle *asset = CTX_wm_asset(C);
PoseBlendData *pbd = static_cast<PoseBlendData *>(op->customdata);
pbd->temp_id_consumer = ED_asset_temp_id_consumer_create(&asset_handle);
pbd->temp_id_consumer = ED_asset_temp_id_consumer_create(asset);
return (bAction *)ED_asset_temp_id_consumer_ensure_local_id(
pbd->temp_id_consumer, ID_AC, CTX_data_main(C), op->reports);
}
@ -551,11 +550,9 @@ static int poselib_blend_exec(bContext *C, wmOperator *op)
static bool poselib_asset_in_context(bContext *C)
{
bool asset_handle_valid;
/* Check whether the context provides the asset data needed to add a pose. */
const AssetHandle asset_handle = CTX_wm_asset_handle(C, &asset_handle_valid);
return asset_handle_valid && (ED_asset_handle_get_id_type(&asset_handle) == ID_AC);
const AssetRepresentationHandle *asset = CTX_wm_asset(C);
return asset && (asset->get_id_type() == ID_AC);
}
/* Poll callback for operators that require existing PoseLib data (with poses) to work. */

View File

@ -15,6 +15,12 @@
#include "DNA_ID_enums.h"
#ifdef __cplusplus
namespace blender::asset_system {
class AssetRepresentation;
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
@ -26,6 +32,10 @@ struct Main;
struct ReportList;
AssetTempIDConsumer *ED_asset_temp_id_consumer_create(const struct AssetHandle *handle);
#ifdef __cplusplus
extern "C++" AssetTempIDConsumer *ED_asset_temp_id_consumer_create(
const blender::asset_system::AssetRepresentation *asset);
#endif
void ED_asset_temp_id_consumer_free(AssetTempIDConsumer **consumer);
struct ID *ED_asset_temp_id_consumer_ensure_local_id(AssetTempIDConsumer *consumer,
ID_Type id_type,

View File

@ -287,12 +287,9 @@ void AssetClearHelper::operator()(PointerRNAVec &ids)
void AssetClearHelper::reportResults(const bContext *C, ReportList &reports) const
{
if (!wasSuccessful()) {
bool is_valid;
/* Dedicated error message for when there is an active asset detected, but it's not an ID local
* to this file. Helps users better understanding what's going on. */
if (AssetHandle active_asset = CTX_wm_asset_handle(C, &is_valid);
is_valid && !ED_asset_handle_get_representation(&active_asset)->local_id())
{
if (AssetRepresentationHandle *active_asset = CTX_wm_asset(C); !active_asset->is_local_id()) {
BKE_report(&reports,
RPT_ERROR,
"No asset data-blocks from the current file selected (assets must be stored in "

View File

@ -79,6 +79,16 @@ AssetTempIDConsumer *ED_asset_temp_id_consumer_create(const AssetHandle *handle)
MEM_new<AssetTemporaryIDConsumer>(__func__, ED_asset_handle_get_representation(handle)));
}
AssetTempIDConsumer *ED_asset_temp_id_consumer_create(
const asset_system::AssetRepresentation *asset)
{
if (!asset) {
return nullptr;
}
return reinterpret_cast<AssetTempIDConsumer *>(
MEM_new<AssetTemporaryIDConsumer>(__func__, asset));
}
void ED_asset_temp_id_consumer_free(AssetTempIDConsumer **consumer)
{
MEM_delete(reinterpret_cast<AssetTemporaryIDConsumer *>(*consumer));