Blender V4.3
asset_temp_id_consumer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12#include <new>
13#include <string>
14
16
17#include "BKE_report.hh"
18
19#include "BLI_utility_mixins.hh"
20
21#include "BLO_readfile.hh"
22
23#include "MEM_guardedalloc.h"
24
26
27namespace blender::ed::asset {
28
31 TempLibraryContext *temp_lib_context_ = nullptr;
32
33 public:
36 {
37 if (temp_lib_context_) {
38 BLO_library_temp_free(temp_lib_context_);
39 }
40 }
41
43 {
44 return asset_->local_id();
45 }
46
47 ID *import_id(ID_Type id_type, Main &bmain, ReportList &reports)
48 {
49 const char *asset_name = asset_->get_name().c_str();
50 std::string blend_file_path = asset_->full_library_path();
51
52 temp_lib_context_ = BLO_library_temp_load_id(
53 &bmain, blend_file_path.c_str(), id_type, asset_name, &reports);
54
55 if (temp_lib_context_ == nullptr || temp_lib_context_->temp_id == nullptr) {
57 &reports, RPT_ERROR, "Unable to load %s from %s", asset_name, blend_file_path.c_str());
58 return nullptr;
59 }
60
61 BLI_assert(GS(temp_lib_context_->temp_id->name) == id_type);
62 return temp_lib_context_->temp_id;
63 }
64};
65
67{
68 if (!asset) {
69 return nullptr;
70 }
71 return reinterpret_cast<AssetTempIDConsumer *>(
72 MEM_new<AssetTemporaryIDConsumer>(__func__, asset));
73}
74
75void temp_id_consumer_free(AssetTempIDConsumer **consumer)
76{
77 MEM_delete(reinterpret_cast<AssetTemporaryIDConsumer *>(*consumer));
78 *consumer = nullptr;
79}
80
81ID *temp_id_consumer_ensure_local_id(AssetTempIDConsumer *consumer_,
82 ID_Type id_type,
83 Main *bmain,
84 ReportList *reports)
85{
86 if (!(consumer_ && bmain && reports)) {
87 return nullptr;
88 }
89 AssetTemporaryIDConsumer *consumer = reinterpret_cast<AssetTemporaryIDConsumer *>(consumer_);
90
91 if (ID *local_id = consumer->get_local_id()) {
92 return local_id;
93 }
94 return consumer->import_id(id_type, *bmain, *reports);
95}
96
97} // namespace blender::ed::asset
Main runtime representation of an asset.
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
#define BLI_assert(a)
Definition BLI_assert.h:50
external readfile function prototypes.
TempLibraryContext * BLO_library_temp_load_id(Main *real_main, const char *blend_file_path, short idcode, const char *idname, ReportList *reports)
void BLO_library_temp_free(TempLibraryContext *temp_lib_ctx)
ID_Type
Read Guarded memory(de)allocation.
constexpr const char * c_str() const
ID * import_id(ID_Type id_type, Main &bmain, ReportList &reports)
AssetTemporaryIDConsumer(const asset_system::AssetRepresentation *asset)
#define GS(x)
Definition iris.cc:202
AssetTempIDConsumer * temp_id_consumer_create(const blender::asset_system::AssetRepresentation *asset)
ID * temp_id_consumer_ensure_local_id(AssetTempIDConsumer *consumer, ID_Type id_type, Main *bmain, ReportList *reports)
void temp_id_consumer_free(AssetTempIDConsumer **consumer)
Definition DNA_ID.h:413
char name[66]
Definition DNA_ID.h:425