Blender V4.5
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
25
26namespace blender::ed::asset {
27
30 TempLibraryContext *temp_lib_context_ = nullptr;
31
32 public:
35 {
36 if (temp_lib_context_) {
37 BLO_library_temp_free(temp_lib_context_);
38 }
39 }
40
42 {
43 return asset_->local_id();
44 }
45
47 {
48 const char *asset_name = asset_->get_name().c_str();
49 std::string blend_file_path = asset_->full_library_path();
50
51 temp_lib_context_ = BLO_library_temp_load_id(
52 &bmain, blend_file_path.c_str(), id_type, asset_name, &reports);
53
54 if (temp_lib_context_ == nullptr || temp_lib_context_->temp_id == nullptr) {
56 &reports, RPT_ERROR, "Unable to load %s from %s", asset_name, blend_file_path.c_str());
57 return nullptr;
58 }
59
60 BLI_assert(GS(temp_lib_context_->temp_id->name) == id_type);
61 return temp_lib_context_->temp_id;
62 }
63};
64
66{
67 if (!asset) {
68 return nullptr;
69 }
70 return reinterpret_cast<AssetTempIDConsumer *>(
71 MEM_new<AssetTemporaryIDConsumer>(__func__, asset));
72}
73
74void temp_id_consumer_free(AssetTempIDConsumer **consumer)
75{
76 MEM_delete(reinterpret_cast<AssetTemporaryIDConsumer *>(*consumer));
77 *consumer = nullptr;
78}
79
80ID *temp_id_consumer_ensure_local_id(AssetTempIDConsumer *consumer_,
81 ID_Type id_type,
82 Main *bmain,
84{
85 if (!(consumer_ && bmain && reports)) {
86 return nullptr;
87 }
88 AssetTemporaryIDConsumer *consumer = reinterpret_cast<AssetTemporaryIDConsumer *>(consumer_);
89
90 if (ID *local_id = consumer->get_local_id()) {
91 return local_id;
92 }
93 return consumer->import_id(id_type, *bmain, *reports);
94}
95
96} // 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: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_Type
Read Guarded memory(de)allocation.
ReportList * reports
Definition WM_types.hh:1025
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(a)
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:404