Blender V5.0
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.h"
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
27namespace blender::asset_system {
28
31 const int id_type,
32 std::unique_ptr<AssetMetaData> metadata,
34 : owner_asset_library_(owner_asset_library),
35 relative_identifier_(relative_asset_path),
36 asset_(AssetRepresentation::ExternalAsset{name, id_type, std::move(metadata), nullptr})
37{
38}
39
41 : owner_asset_library_(owner_asset_library), asset_(&id)
42{
43 if (!id.asset_data) {
44 throw std::invalid_argument("Passed ID is not an asset");
45 }
46}
47
49{
50 if (const ExternalAsset *extern_asset = std::get_if<ExternalAsset>(&asset_);
51 extern_asset && extern_asset->preview_)
52 {
54 }
55}
56
58{
59 return AssetWeakReference::make_reference(owner_asset_library_, library_relative_identifier());
60}
61
63{
64 if (ID *id = this->local_id()) {
66 BKE_icon_preview_ensure(id, preview);
67 return;
68 }
69
70 ExternalAsset &extern_asset = std::get<ExternalAsset>(asset_);
71
72 /* Use the full path as preview name, it's the only unique identifier we have. */
73 const std::string full_path = this->full_path();
74 /* Doesn't do the actual reading, just allocates and attaches the derived load info. */
75 extern_asset.preview_ = BKE_previewimg_cached_thumbnail_read(
76 full_path.c_str(), full_path.c_str(), THB_SOURCE_BLEND, false);
77
78 BKE_icon_preview_ensure(nullptr, extern_asset.preview_);
79}
80
82{
83 if (const ID *id = this->local_id()) {
84 return BKE_previewimg_id_get(id);
85 }
86
87 return std::get<ExternalAsset>(asset_).preview_;
88}
89
91{
92 if (const ID *id = this->local_id()) {
93 return id->name + 2;
94 }
95 return std::get<ExternalAsset>(asset_).name;
96}
97
99{
100 if (const ID *id = this->local_id()) {
101 return GS(id->name);
102 }
103 return ID_Type(std::get<ExternalAsset>(asset_).id_type);
104}
105
107{
108 if (const ID *id = this->local_id()) {
109 return *id->asset_data;
110 }
111 return *std::get<ExternalAsset>(asset_).metadata_;
112}
113
115{
116 if (const ID *id = this->local_id()) {
117 StringRef idname = BKE_id_name(*id);
118 /* Lazy-create/-update with the latest ID name. */
119 if (!StringRef{relative_identifier_}.endswith(idname)) {
120 relative_identifier_ = StringRef{BKE_idtype_idcode_to_name(GS(id->name))} + SEP_STR + idname;
121 }
122 }
123
124 return relative_identifier_;
125}
126
128{
129 char filepath[FILE_MAX];
130 BLI_path_join(filepath,
131 sizeof(filepath),
132 owner_asset_library_.root_path().c_str(),
134 return filepath;
135}
136
138{
139 std::string asset_path = full_path();
140
141 char blend_path[/*FILE_MAX_LIBEXTRA*/ 1282];
142 if (!BKE_blendfile_library_path_explode(asset_path.c_str(), blend_path, nullptr, nullptr)) {
143 return {};
144 }
145
146 return blend_path;
147}
148
149std::optional<eAssetImportMethod> AssetRepresentation::get_import_method() const
150{
151 return owner_asset_library_.import_method_;
152}
153
155{
156 if (!owner_asset_library_.import_method_) {
157 return true;
158 }
159 return owner_asset_library_.may_override_import_method_;
160}
161
163{
164 return owner_asset_library_.use_relative_path_;
165}
166
168{
169 return this->is_local_id() ? std::get<ID *>(asset_) : nullptr;
170}
171
173{
174 return std::holds_alternative<ID *>(asset_);
175}
176
178{
179 return owner_asset_library_;
180}
181
182} // namespace blender::asset_system
Main runtime representation of an asset.
bool BKE_blendfile_library_path_explode(const char *path, char *r_dir, char **r_group, char **r_name)
Definition blendfile.cc:92
int BKE_icon_preview_ensure(struct ID *id, struct PreviewImage *preview)
Definition icons.cc:335
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:164
const char * BKE_id_name(const ID &id)
PreviewImage * BKE_previewimg_id_get(const ID *id)
PreviewImage * BKE_previewimg_cached_thumbnail_read(const char *name, const char *filepath, int source, bool force_update)
void BKE_previewimg_cached_release(const char *name)
#define FILE_MAX
#define BLI_path_join(...)
ID and Library types, which are fundamental for SDNA.
ID_Type
@ THB_SOURCE_BLEND
Definition IMB_thumbs.hh:30
constexpr bool endswith(StringRef suffix) const
AssetRepresentation(StringRef relative_asset_path, StringRef name, int id_type, std::unique_ptr< AssetMetaData > metadata, AssetLibrary &owner_asset_library)
std::optional< eAssetImportMethod > get_import_method() const
#define GS(x)
const char * name
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
Definition DNA_ID.h:414
#define SEP_STR
Definition unit.cc:39