Blender V5.0
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
11
12#include <string>
13
15
16#include "BKE_report.hh"
17
18#include "BLI_utility_mixins.hh"
19
20#include "BLO_readfile.hh"
21
22#include "MEM_guardedalloc.h"
23
24#include "DNA_ID.h"
25
27
28namespace blender::ed::asset {
29
32 TempLibraryContext *temp_lib_context_ = nullptr;
33
34 public:
37 {
38 if (temp_lib_context_) {
39 BLO_library_temp_free(temp_lib_context_);
40 }
41 }
42
44 {
45 return asset_->local_id();
46 }
47
48 ID *import_id(ID_Type id_type, Main &bmain, ReportList &reports)
49 {
50 const char *asset_name = asset_->get_name().c_str();
51 std::string blend_file_path = asset_->full_library_path();
52
53 temp_lib_context_ = BLO_library_temp_load_id(
54 &bmain, blend_file_path.c_str(), id_type, asset_name, &reports);
55
56 if (temp_lib_context_ == nullptr || temp_lib_context_->temp_id == nullptr) {
58 &reports, RPT_ERROR, "Unable to load %s from %s", asset_name, blend_file_path.c_str());
59 return nullptr;
60 }
61
62 BLI_assert(GS(temp_lib_context_->temp_id->name) == id_type);
63 return temp_lib_context_->temp_id;
64 }
65};
66
68{
69 if (!asset) {
70 return nullptr;
71 }
72 return reinterpret_cast<AssetTempIDConsumer *>(
73 MEM_new<AssetTemporaryIDConsumer>(__func__, asset));
74}
75
76void temp_id_consumer_free(AssetTempIDConsumer **consumer)
77{
78 MEM_delete(reinterpret_cast<AssetTemporaryIDConsumer *>(*consumer));
79 *consumer = nullptr;
80}
81
82ID *temp_id_consumer_ensure_local_id(AssetTempIDConsumer *consumer_,
83 ID_Type id_type,
84 Main *bmain,
85 ReportList *reports)
86{
87 if (!(consumer_ && bmain && reports)) {
88 return nullptr;
89 }
90 AssetTemporaryIDConsumer *consumer = reinterpret_cast<AssetTemporaryIDConsumer *>(consumer_);
91
92 if (ID *local_id = consumer->get_local_id()) {
93 return local_id;
94 }
95 return consumer->import_id(id_type, *bmain, *reports);
96}
97
98} // namespace blender::ed::asset
Main runtime representation of an asset.
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
@ RPT_ERROR
Definition BKE_report.hh:39
#define BLI_assert(a)
Definition BLI_assert.h:46
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 and Library types, which are fundamental for SDNA.
ID_Type
Read Guarded memory(de)allocation.
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
ID * import_id(ID_Type id_type, Main &bmain, ReportList &reports)
AssetTemporaryIDConsumer(const asset_system::AssetRepresentation *asset)
#define GS(x)
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:414