Blender V4.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
9#include <stdexcept>
10
11#include "BLI_path_utils.hh"
12
13#include "BKE_blendfile.hh"
14
15#include "DNA_ID.h"
16#include "DNA_asset_types.h"
17
18#include "AS_asset_library.hh"
20
21namespace blender::asset_system {
22
24 StringRef name,
25 const int id_type,
26 std::unique_ptr<AssetMetaData> metadata,
27 const AssetLibrary &owner_asset_library)
28 : owner_asset_library_(owner_asset_library),
29 relative_identifier_(relative_asset_path),
30 asset_(AssetRepresentation::ExternalAsset{name, id_type, std::move(metadata)})
31{
32}
33
35 ID &id,
36 const AssetLibrary &owner_asset_library)
37 : owner_asset_library_(owner_asset_library),
38 relative_identifier_(relative_asset_path),
39 asset_(&id)
40{
41 if (!id.asset_data) {
42 throw std::invalid_argument("Passed ID is not an asset");
43 }
44}
45
47{
48 return AssetWeakReference::make_reference(owner_asset_library_, relative_identifier_);
49}
50
52{
53 if (const ID *id = this->local_id()) {
54 return id->name + 2;
55 }
56 return std::get<ExternalAsset>(asset_).name;
57}
58
60{
61 if (const ID *id = this->local_id()) {
62 return GS(id->name);
63 }
64 return ID_Type(std::get<ExternalAsset>(asset_).id_type);
65}
66
68{
69 if (const ID *id = this->local_id()) {
70 return *id->asset_data;
71 }
72 return *std::get<ExternalAsset>(asset_).metadata_;
73}
74
76{
77 return relative_identifier_;
78}
79
81{
82 char filepath[FILE_MAX];
83 BLI_path_join(filepath,
84 sizeof(filepath),
85 owner_asset_library_.root_path().c_str(),
86 relative_identifier_.c_str());
87 return filepath;
88}
89
91{
92 std::string asset_path = full_path();
93
94 char blend_path[1090 /*FILE_MAX_LIBEXTRA*/];
95 if (!BKE_blendfile_library_path_explode(asset_path.c_str(), blend_path, nullptr, nullptr)) {
96 return {};
97 }
98
99 return blend_path;
100}
101
102std::optional<eAssetImportMethod> AssetRepresentation::get_import_method() const
103{
104 return owner_asset_library_.import_method_;
105}
106
108{
109 if (!owner_asset_library_.import_method_) {
110 return true;
111 }
112 return owner_asset_library_.may_override_import_method_;
113}
114
116{
117 return owner_asset_library_.use_relative_path_;
118}
119
121{
122 return this->is_local_id() ? std::get<ID *>(asset_) : nullptr;
123}
124
126{
127 return std::holds_alternative<ID *>(asset_);
128}
129
131{
132 return owner_asset_library_;
133}
134
135} // 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:89
#define FILE_MAX
#define BLI_path_join(...)
ID and Library types, which are fundamental for SDNA.
ID_Type
constexpr const char * c_str() const
std::optional< eAssetImportMethod > import_method_
AssetRepresentation(StringRef relative_asset_path, StringRef name, int id_type, std::unique_ptr< AssetMetaData > metadata, const AssetLibrary &owner_asset_library)
std::optional< eAssetImportMethod > get_import_method() const
#define GS(x)
Definition iris.cc:202
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
Definition DNA_ID.h:413