Blender V4.5
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_preview_image.hh"
16
17#include "DNA_ID.h"
18#include "DNA_asset_types.h"
19
20#include "IMB_thumbs.hh"
21
22#include "AS_asset_library.hh"
24
25namespace blender::asset_system {
26
28 StringRef name,
29 const int id_type,
30 std::unique_ptr<AssetMetaData> metadata,
32 : owner_asset_library_(owner_asset_library),
33 relative_identifier_(relative_asset_path),
34 asset_(AssetRepresentation::ExternalAsset{name, id_type, std::move(metadata), nullptr})
35{
36}
37
39 ID &id,
41 : owner_asset_library_(owner_asset_library),
42 relative_identifier_(relative_asset_path),
43 asset_(&id)
44{
45 if (!id.asset_data) {
46 throw std::invalid_argument("Passed ID is not an asset");
47 }
48}
49
51{
52 if (const ExternalAsset *extern_asset = std::get_if<ExternalAsset>(&asset_);
53 extern_asset && extern_asset->preview_)
54 {
56 }
57}
58
60{
61 return AssetWeakReference::make_reference(owner_asset_library_, relative_identifier_);
62}
63
65{
66 if (ID *id = this->local_id()) {
68 BKE_icon_preview_ensure(id, preview);
69 return;
70 }
71
72 ExternalAsset &extern_asset = std::get<ExternalAsset>(asset_);
73
74 /* Use the full path as preview name, it's the only unique identifier we have. */
75 const std::string full_path = this->full_path();
76 /* Doesn't do the actual reading, just allocates and attaches the derived load info. */
77 extern_asset.preview_ = BKE_previewimg_cached_thumbnail_read(
78 full_path.c_str(), full_path.c_str(), THB_SOURCE_BLEND, false);
79
80 BKE_icon_preview_ensure(nullptr, extern_asset.preview_);
81}
82
84{
85 if (const ID *id = this->local_id()) {
86 return BKE_previewimg_id_get(id);
87 }
88
89 return std::get<ExternalAsset>(asset_).preview_;
90}
91
93{
94 if (const ID *id = this->local_id()) {
95 return id->name + 2;
96 }
97 return std::get<ExternalAsset>(asset_).name;
98}
99
101{
102 if (const ID *id = this->local_id()) {
103 return GS(id->name);
104 }
105 return ID_Type(std::get<ExternalAsset>(asset_).id_type);
106}
107
109{
110 if (const ID *id = this->local_id()) {
111 return *id->asset_data;
112 }
113 return *std::get<ExternalAsset>(asset_).metadata_;
114}
115
117{
118 return relative_identifier_;
119}
120
122{
123 char filepath[FILE_MAX];
124 BLI_path_join(filepath,
125 sizeof(filepath),
126 owner_asset_library_.root_path().c_str(),
127 relative_identifier_.c_str());
128 return filepath;
129}
130
132{
133 std::string asset_path = full_path();
134
135 char blend_path[/*FILE_MAX_LIBEXTRA*/ 1090];
136 if (!BKE_blendfile_library_path_explode(asset_path.c_str(), blend_path, nullptr, nullptr)) {
137 return {};
138 }
139
140 return blend_path;
141}
142
143std::optional<eAssetImportMethod> AssetRepresentation::get_import_method() const
144{
145 return owner_asset_library_.import_method_;
146}
147
149{
150 if (!owner_asset_library_.import_method_) {
151 return true;
152 }
153 return owner_asset_library_.may_override_import_method_;
154}
155
157{
158 return owner_asset_library_.use_relative_path_;
159}
160
162{
163 return this->is_local_id() ? std::get<ID *>(asset_) : nullptr;
164}
165
167{
168 return std::holds_alternative<ID *>(asset_);
169}
170
172{
173 return owner_asset_library_;
174}
175
176} // 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:90
int BKE_icon_preview_ensure(struct ID *id, struct PreviewImage *preview)
Definition icons.cc:335
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)
PreviewImage * BKE_previewimg_id_ensure(ID *id)
#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
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(a)
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
Definition DNA_ID.h:404