Blender V5.3
asset_representation.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
8
9#include <stdexcept>
10
11#include "BLI_path_utils.hh"
12
13#include "BKE_blendfile.hh"
14#include "BKE_icons.hh"
15#include "BKE_idtype.hh"
16#include "BKE_lib_id.hh"
17#include "BKE_preview_image.hh"
18
19#include "DNA_ID.h"
20#include "DNA_asset_types.h"
21
22#include "IMB_thumbs.hh"
23
24#include "AS_asset_library.hh"
26#include "AS_remote_library.hh"
27
28namespace blender::asset_system {
29
31 StringRef name,
32 const int id_type,
33 std::unique_ptr<AssetMetaData> metadata,
35 : owner_asset_library_(owner_asset_library),
36 relative_identifier_(relative_asset_path),
37 asset_(AssetRepresentation::ExternalAsset{name, id_type, std::move(metadata)})
38{
39}
40
42 StringRef name,
43 const int id_type,
44 std::unique_ptr<AssetMetaData> metadata,
46 OnlineAssetInfo online_info)
47 : owner_asset_library_(owner_asset_library),
48 relative_identifier_(relative_asset_path),
49 asset_(AssetRepresentation::ExternalAsset{
50 name,
51 id_type,
52 std::move(metadata),
53 nullptr,
55 std::make_unique<OnlineAssetInfo>(std::move(online_info))})
56{
57}
58
60 : owner_asset_library_(owner_asset_library), asset_(&id)
61{
62 if (!id.asset_data) {
63 throw std::invalid_argument("Passed ID is not an asset");
64 }
65}
66
68{
69 if (const ExternalAsset *extern_asset = std::get_if<ExternalAsset>(&asset_);
70 extern_asset && extern_asset->preview_)
71 {
73 }
74}
75
77{
78 return AssetWeakReference::make_reference(owner_asset_library_, library_relative_identifier());
79}
80
82{
83 if (ID *id = this->local_id()) {
85 BKE_icon_preview_ensure(id, preview);
86 return;
87 }
88
89 ExternalAsset &extern_asset = std::get<ExternalAsset>(asset_);
90
91 if (extern_asset.preview_ && extern_asset.preview_->runtime->icon_id) {
92 return;
93 }
94
95 /* The asset may be in multiple libraries, so multiple #AssetRepresentation's may refer to the
96 * same preview. Use user counting so the preview is only released with the last representation.
97 */
98 const bool count_preview_users = true;
99
100 /* Only use the remote thumbnail when there is no asset file on disk. Otherwise use the on-disk
101 * file. */
102 if (this->is_online_only()) {
103 if (!extern_asset.online_info_->preview_url) {
104 return;
105 }
106
107 const std::string preview_path = remote_library_asset_preview_path(*this);
108 /* Doesn't do the actual reading, just allocates and attaches the derived load info. */
109 extern_asset.preview_ = BKE_previewimg_online_thumbnail_read(
110 this->full_path().c_str(), preview_path.c_str(), false, count_preview_users);
111 remote_library_request_preview_download(C, *this, preview_path, reports);
112 }
113 else {
114 /* Use the full path as preview name, it's the only unique identifier we have. */
115 const std::string full_path = this->full_path();
116
117 /* Doesn't do the actual reading, just allocates and attaches the derived load info. */
118 extern_asset.preview_ = BKE_previewimg_cached_thumbnail_read(
119 full_path.c_str(), full_path.c_str(), THB_SOURCE_BLEND, false, count_preview_users);
120 }
121
122 BKE_icon_preview_ensure(nullptr, extern_asset.preview_);
123}
124
126{
127 if (const ID *id = this->local_id()) {
128 return BKE_previewimg_id_get(id);
129 }
130
131 return std::get<ExternalAsset>(asset_).preview_;
132}
133
135{
136 if (const ID *id = this->local_id()) {
137 return id->name + 2;
138 }
139 return std::get<ExternalAsset>(asset_).name;
140}
141
143{
144 if (const ID *id = this->local_id()) {
145 return GS(id->name);
146 }
147 return ID_Type(std::get<ExternalAsset>(asset_).id_type);
148}
149
151{
152 if (const ID *id = this->local_id()) {
153 return *id->asset_data;
154 }
155 return *std::get<ExternalAsset>(asset_).metadata_;
156}
157
159{
160 if (const ID *id = this->local_id()) {
161 StringRef idname = BKE_id_name(*id);
162 /* Lazy-create/-update with the latest ID name. */
163 if (!StringRef{relative_identifier_}.endswith(idname)) {
164 relative_identifier_ = StringRef{BKE_idtype_idcode_to_name(GS(id->name))} + SEP_STR + idname;
165 }
166 }
167
168 return relative_identifier_;
169}
170
172{
173 char filepath[FILE_MAX];
174 BLI_path_join(filepath,
175 sizeof(filepath),
176 owner_asset_library_.root_path().c_str(),
178 return filepath;
179}
180
182{
183 std::string asset_path = full_path();
184
185 char blend_path[/*FILE_MAX_LIBEXTRA*/ 1282];
186 if (!BKE_blendfile_library_path_explode(asset_path.c_str(), blend_path, nullptr, nullptr)) {
187 return {};
188 }
189
190 return blend_path;
191}
192
194{
195 const ExternalAsset *extern_asset = std::get_if<ExternalAsset>(&asset_);
196 if (!extern_asset || !extern_asset->online_info_) {
197 return {};
198 }
199 return extern_asset->online_info_->files;
200}
201
203{
204 const ExternalAsset *extern_asset = std::get_if<ExternalAsset>(&asset_);
205 if (!extern_asset || !extern_asset->online_info_) {
206 return {};
207 }
208 int64_t size = 0;
209 for (const OnlineAssetFile &file : online_asset_files()) {
210 size += file.size_in_bytes;
211 }
212 return size;
213}
214
215std::optional<StringRefNull> AssetRepresentation::online_asset_preview_url() const
216{
217 if (!this->is_online_only()) {
218 return {};
219 }
220 std::optional<URLWithHash> &url_with_hash =
221 std::get<ExternalAsset>(asset_).online_info_->preview_url;
222 if (!url_with_hash) {
223 return {};
224 }
225 return url_with_hash->url;
226}
227
228std::optional<StringRefNull> AssetRepresentation::online_asset_preview_hash() const
229{
230 if (!this->is_online_only()) {
231 return {};
232 }
233 std::optional<URLWithHash> &url_with_hash =
234 std::get<ExternalAsset>(asset_).online_info_->preview_url;
235 if (!url_with_hash) {
236 return {};
237 }
238 return url_with_hash->hash;
239}
240
242{
243 ExternalAsset *extern_asset = std::get_if<ExternalAsset>(&asset_);
244 if (!extern_asset) {
245 return;
246 }
247 /* Since it was just downloaded, let's assume the file matches the listed hash. If not, the
248 * next refresh will show the correct status.
249 * TODO: ensure that the file status is actually checked, instead of just making assumptions. */
250 extern_asset->remote_file_status_ = RemoteAssetFileStatus::MATCH;
251}
252
253std::optional<eAssetImportMethod> AssetRepresentation::get_import_method() const
254{
255 const AssetMetaData &metadata = this->get_metadata();
257 return metadata.preferred_import_method;
258 }
259 return owner_asset_library_.import_method();
260}
261
263{
264 if (!owner_asset_library_.import_method()) {
265 return true;
266 }
267 return owner_asset_library_.may_override_import_method_;
268}
269
271{
272 return owner_asset_library_.use_relative_paths();
273}
274
276{
277 return this->is_local_id() ? std::get<ID *>(asset_) : nullptr;
278}
279
281{
282 return std::holds_alternative<ID *>(asset_);
283}
284
286{
287 const ExternalAsset *extern_asset = std::get_if<ExternalAsset>(&asset_);
288 if (!extern_asset || !extern_asset->online_info_) {
289 return false;
290 }
291 /* An asset is considered 'online' if there is no file on disk for it.
292 *
293 * About also allowing UNSET: This function is (indirectly) called from all kinds of
294 * places, like `get_node_tools_type_data()` in `node_group_operators.cc` to figure out which
295 * node tools are available. Since that happens on startup, the actual on-disk file status may
296 * not have been checked yet. Until that time, just assume that having `online_info_` means "it
297 * is online". */
298 return ELEM(extern_asset->remote_file_status_,
301}
302
304{
305 if (this->owner_asset_library().is_read_only()) {
306 return false;
307 }
308
309 std::string lib_path = this->full_library_path();
310 return StringRef(lib_path).endswith(BLENDER_ASSET_FILE_SUFFIX);
311}
312
314{
315 const ExternalAsset *extern_asset = std::get_if<ExternalAsset>(&asset_);
316 if (!extern_asset) {
318 }
319 return extern_asset->remote_file_status_;
320}
321
323{
324 ExternalAsset *extern_asset = std::get_if<ExternalAsset>(&asset_);
325 if (!extern_asset) {
326 return;
327 }
328 extern_asset->online_info_ = std::make_unique<OnlineAssetInfo>(std::move(info));
329}
330
332{
333 ExternalAsset *extern_asset = std::get_if<ExternalAsset>(&asset_);
334 if (!extern_asset) {
335 return;
336 }
337 extern_asset->remote_file_status_ = status;
338}
339
344
346{
347 return owner_asset_library_;
348}
349
350} // namespace blender::asset_system
Main runtime representation of an asset.
#define BLENDER_ASSET_FILE_SUFFIX
#define FILE_MAX
#define BLI_path_join(...)
ID and Library types, which are fundamental for SDNA.
#define C
Definition RandGen.cpp:29
#define ELEM(...)
constexpr StringRef()
AssetRepresentation(StringRef relative_asset_path, StringRef name, int id_type, std::unique_ptr< AssetMetaData > metadata, AssetLibrary &owner_asset_library)
std::optional< StringRefNull > online_asset_preview_url() const
void ensure_previewable(const bContext &C, ReportList *reports=nullptr)
Span< OnlineAssetFile > online_asset_files() const
std::optional< eAssetImportMethod > get_import_method() const
void remote_file_status_set(RemoteAssetFileStatus status)
std::optional< StringRefNull > online_asset_preview_hash() const
std::optional< int64_t > online_asset_files_combined_size_in_bytes() const
constexpr bool endswith(StringRef suffix) const
void remote_library_request_preview_download(const bContext &C, const AssetRepresentation &asset, const StringRef dst_filepath, ReportList *reports)
std::string remote_library_asset_preview_path(const AssetRepresentation &asset)
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:261
__int64 int64_t
Definition BKE_text.h:90
PreviewImage * BKE_previewimg_cached_thumbnail_read(const char *name, const char *filepath, int source, bool force_update, bool count_users=false)
bool BKE_blendfile_library_path_explode(const char *path, char *r_dir, char **r_group, char **r_name)
Definition blendfile.cc:94
PreviewImage * BKE_previewimg_online_thumbnail_read(const char *name, const char *dst_filepath, bool force_update, bool count_users=false)
const char * BKE_id_name(const ID &id)
int BKE_icon_preview_ensure(ID *id, PreviewImage *preview)
Definition icons.cc:347
@ ASSETDATA_USE_OWN_IMPORT_METHOD
PreviewImage * BKE_previewimg_id_get(const ID *id)
@ THB_SOURCE_BLEND
Definition IMB_thumbs.hh:35
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void BKE_previewimg_cached_release(const char *name)
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
bke::PreviewImageRuntime * runtime
Definition DNA_ID.h:651
#define SEP_STR
Definition unit.cc:41