Blender  V3.3
asset_temp_id_consumer.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
10 #include <new>
11 
12 #include "DNA_space_types.h"
13 
14 #include "BKE_report.h"
15 
16 #include "BLI_utility_mixins.hh"
17 
18 #include "BLO_readfile.h"
19 
20 #include "MEM_guardedalloc.h"
21 
22 #include "ED_asset_handle.h"
24 
25 using namespace blender;
26 
28  const AssetHandle &handle_;
29  TempLibraryContext *temp_lib_context_ = nullptr;
30 
31  public:
32  AssetTemporaryIDConsumer(const AssetHandle &handle) : handle_(handle)
33  {
34  }
36  {
37  if (temp_lib_context_) {
38  BLO_library_temp_free(temp_lib_context_);
39  }
40  }
41 
43  {
44  return ED_asset_handle_get_local_id(&handle_);
45  }
46 
47  ID *import_id(const bContext *C,
48  const AssetLibraryReference &asset_library_ref,
49  ID_Type id_type,
50  Main &bmain,
51  ReportList &reports)
52  {
53  const char *asset_name = ED_asset_handle_get_name(&handle_);
54  char blend_file_path[FILE_MAX_LIBEXTRA];
55  ED_asset_handle_get_full_library_path(C, &asset_library_ref, &handle_, blend_file_path);
56 
57  temp_lib_context_ = BLO_library_temp_load_id(
58  &bmain, blend_file_path, id_type, asset_name, &reports);
59 
60  if (temp_lib_context_ == nullptr || temp_lib_context_->temp_id == nullptr) {
61  BKE_reportf(&reports, RPT_ERROR, "Unable to load %s from %s", asset_name, blend_file_path);
62  return nullptr;
63  }
64 
65  BLI_assert(GS(temp_lib_context_->temp_id->name) == id_type);
66  return temp_lib_context_->temp_id;
67  }
68 };
69 
71 {
72  if (!handle) {
73  return nullptr;
74  }
75  BLI_assert(handle->file_data->asset_data != nullptr);
76  return reinterpret_cast<AssetTempIDConsumer *>(
77  MEM_new<AssetTemporaryIDConsumer>(__func__, *handle));
78 }
79 
81 {
82  MEM_delete(reinterpret_cast<AssetTemporaryIDConsumer *>(*consumer));
83  *consumer = nullptr;
84 }
85 
87  const bContext *C,
88  const AssetLibraryReference *asset_library_ref,
89  ID_Type id_type,
90  Main *bmain,
91  ReportList *reports)
92 {
93  if (!(consumer_ && asset_library_ref && bmain && reports)) {
94  return nullptr;
95  }
96  AssetTemporaryIDConsumer *consumer = reinterpret_cast<AssetTemporaryIDConsumer *>(consumer_);
97 
98  if (ID *local_id = consumer->get_local_id()) {
99  return local_id;
100  }
101  return consumer->import_id(C, *asset_library_ref, id_type, *bmain, *reports);
102 }
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
#define BLI_assert(a)
Definition: BLI_assert.h:46
external readfile function prototypes.
void BLO_library_temp_free(TempLibraryContext *temp_lib_ctx)
TempLibraryContext * BLO_library_temp_load_id(struct Main *real_main, const char *blend_file_path, short idcode, const char *idname, struct ReportList *reports)
ID_Type
Definition: DNA_ID_enums.h:44
#define FILE_MAX_LIBEXTRA
struct ID * ED_asset_handle_get_local_id(const struct AssetHandle *asset)
const char * ED_asset_handle_get_name(const struct AssetHandle *asset)
void ED_asset_handle_get_full_library_path(const struct bContext *C, const struct AssetLibraryReference *asset_library_ref, const struct AssetHandle *asset, char r_full_lib_path[])
struct AssetTempIDConsumer AssetTempIDConsumer
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
void ED_asset_temp_id_consumer_free(AssetTempIDConsumer **consumer)
ID * ED_asset_temp_id_consumer_ensure_local_id(AssetTempIDConsumer *consumer_, const bContext *C, const AssetLibraryReference *asset_library_ref, ID_Type id_type, Main *bmain, ReportList *reports)
AssetTempIDConsumer * ED_asset_temp_id_consumer_create(const AssetHandle *handle)
AssetTemporaryIDConsumer(const AssetHandle &handle)
ID * import_id(const bContext *C, const AssetLibraryReference &asset_library_ref, ID_Type id_type, Main &bmain, ReportList &reports)
#define GS(x)
Definition: iris.c:225
const struct FileDirEntry * file_data
struct AssetMetaData * asset_data
Definition: DNA_ID.h:368
char name[66]
Definition: DNA_ID.h:378
Definition: BKE_main.h:121
struct ID * temp_id
Definition: BLO_readfile.h:438