Blender V4.3
asset_menu_utils.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 "AS_asset_catalog.hh"
11#include "AS_asset_library.hh"
13
14#include "DNA_screen_types.h"
15
16#include "BKE_asset.hh"
17#include "BKE_report.hh"
18
19#include "BLT_translation.hh"
20
21#include "RNA_access.hh"
22#include "RNA_define.hh"
23#include "RNA_enum_types.hh"
24#include "RNA_prototypes.hh"
25
26#include "ED_asset_list.hh"
28
29#include "UI_interface.hh"
30
31namespace blender::ed::asset {
32
34{
35 PropertyRNA *prop;
36 prop = RNA_def_enum(&srna,
37 "asset_library_type",
40 "Asset Library Type",
41 "");
43 prop = RNA_def_string(
44 &srna, "asset_library_identifier", nullptr, 0, "Asset Library Identifier", "");
46 prop = RNA_def_string(
47 &srna, "relative_asset_identifier", nullptr, 0, "Relative Asset Identifier", "");
49}
50
53{
54 const AssetWeakReference weak_ref = asset.make_weak_reference();
55 RNA_enum_set(&ptr, "asset_library_type", weak_ref.asset_library_type);
56 RNA_string_set(&ptr, "asset_library_identifier", weak_ref.asset_library_identifier);
57 RNA_string_set(&ptr, "relative_asset_identifier", weak_ref.relative_asset_identifier);
58}
59
65 const bContext &C, const StringRefNull relative_identifier, ReportList *reports)
66{
67 AssetLibraryReference library_ref{};
68 library_ref.type = ASSET_LIBRARY_LOCAL;
69 list::storage_fetch(&library_ref, &C);
70
71 const asset_system::AssetRepresentation *matching_asset = nullptr;
72 list::iterate(library_ref, [&](asset_system::AssetRepresentation &asset) {
73 if (asset.library_relative_identifier() == relative_identifier) {
74 matching_asset = &asset;
75 return false;
76 }
77 return true;
78 });
79
80 if (reports && !matching_asset) {
81 if (list::is_loaded(&library_ref)) {
83 reports, RPT_ERROR, "No asset found at path \"%s\"", relative_identifier.c_str());
84 }
85 else {
86 BKE_report(reports, RPT_WARNING, "Asset loading is unfinished");
87 }
88 }
89 return matching_asset;
90}
91
93 const bContext &C, const AssetWeakReference &weak_ref, ReportList *reports)
94{
97 C, weak_ref.relative_asset_identifier, reports);
98 }
99
101 list::storage_fetch(&library_ref, &C);
104 if (!all_library) {
105 BKE_report(reports, RPT_WARNING, "Asset loading is unfinished");
106 return nullptr;
107 }
108
109 const std::string full_path = all_library->resolve_asset_weak_reference_to_full_path(weak_ref);
110
111 const asset_system::AssetRepresentation *matching_asset = nullptr;
112 list::iterate(library_ref, [&](asset_system::AssetRepresentation &asset) {
113 if (asset.full_path() == full_path) {
114 matching_asset = &asset;
115 return false;
116 }
117 return true;
118 });
119
120 if (reports && !matching_asset) {
121 if (list::is_loaded(&library_ref)) {
122 BKE_reportf(reports, RPT_ERROR, "No asset found at path \"%s\"", full_path.c_str());
123 }
124 }
125 return matching_asset;
126}
127
129 const bContext &C, PointerRNA &ptr, ReportList *reports)
130{
131 AssetWeakReference weak_ref{};
132 weak_ref.asset_library_type = RNA_enum_get(&ptr, "asset_library_type");
133 weak_ref.asset_library_identifier = RNA_string_get_alloc(
134 &ptr, "asset_library_identifier", nullptr, 0, nullptr);
135 weak_ref.relative_asset_identifier = RNA_string_get_alloc(
136 &ptr, "relative_asset_identifier", nullptr, 0, nullptr);
137 return find_asset_from_weak_ref(C, weak_ref, reports);
138}
139
141 const StringRefNull menu_name,
142 uiLayout &layout)
143{
144 uiLayout *col = uiLayoutColumn(&layout, false);
145 uiLayoutSetContextString(col, "asset_catalog_path", item.catalog_path().c_str());
146 uiItemM(col, menu_name.c_str(), IFACE_(item.get_name().c_str()), ICON_NONE);
147}
148
149} // namespace blender::ed::asset
Main runtime representation of an asset.
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:125
#define IFACE_(msgid)
@ ASSET_LIBRARY_LOCAL
@ PROP_SKIP_SAVE
Definition RNA_types.hh:245
@ PROP_HIDDEN
Definition RNA_types.hh:239
uiLayout * uiLayoutColumn(uiLayout *layout, bool align)
void uiLayoutSetContextString(uiLayout *layout, const char *name, blender::StringRef value)
void uiItemM(uiLayout *layout, const char *menuname, const char *name, int icon)
constexpr const char * c_str() const
std::string resolve_asset_weak_reference_to_full_path(const AssetWeakReference &asset_reference)
uint col
AssetLibraryReference all_library_reference()
void storage_fetch(const AssetLibraryReference *library_reference, const bContext *C)
asset_system::AssetLibrary * library_get_once_available(const AssetLibraryReference &library_reference)
void iterate(const AssetLibraryReference &library_reference, AssetListHandleIterFn fn, FunctionRef< bool(asset_system::AssetRepresentation &)> prefilter_fn=nullptr)
bool is_loaded(const AssetLibraryReference *library_reference)
const asset_system::AssetRepresentation * find_asset_from_weak_ref(const bContext &C, const AssetWeakReference &weak_ref, ReportList *reports)
static const asset_system::AssetRepresentation * get_local_asset_from_relative_identifier(const bContext &C, const StringRefNull relative_identifier, ReportList *reports)
void operator_asset_reference_props_register(StructRNA &srna)
void draw_menu_for_catalog(const asset_system::AssetCatalogTreeItem &item, const StringRefNull menu_name, uiLayout &layout)
const asset_system::AssetRepresentation * operator_asset_reference_props_get_asset_from_all_library(const bContext &C, PointerRNA &ptr, ReportList *reports)
void operator_asset_reference_props_set(const asset_system::AssetRepresentation &asset, PointerRNA &ptr)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
char * RNA_string_get_alloc(PointerRNA *ptr, const char *name, char *fixedbuf, int fixedlen, int *r_len)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
int RNA_enum_get(PointerRNA *ptr, const char *name)
const EnumPropertyItem rna_enum_asset_library_type_items[]
Definition rna_asset.cc:22
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
const char * relative_asset_identifier
const char * asset_library_identifier
PointerRNA * ptr
Definition wm_files.cc:4126