Blender V5.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
8
10#include "AS_asset_library.hh"
12
13#include "DNA_screen_types.h"
14
15#include "BKE_context.hh"
16#include "BKE_report.hh"
17
18#include "BLT_translation.hh"
19
20#include "RNA_access.hh"
21#include "RNA_define.hh"
22#include "RNA_enum_types.hh"
23#include "RNA_prototypes.hh"
24
25#include "ED_asset_list.hh"
27
28#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
52 PointerRNA &ptr)
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
61{
62 return RNA_struct_property_is_set(&ptr, "asset_library_type") &&
63 RNA_struct_property_is_set(&ptr, "asset_library_identifier") &&
64 RNA_struct_property_is_set(&ptr, "relative_asset_identifier");
65}
66
72 const bContext &C, const AssetWeakReference &weak_ref, ReportList *reports)
73{
74 AssetLibraryReference library_ref{};
75 library_ref.type = ASSET_LIBRARY_LOCAL;
76 list::storage_fetch(&library_ref, &C);
77
78 const asset_system::AssetRepresentation *matching_asset = nullptr;
80 if (asset.make_weak_reference() == weak_ref) {
81 matching_asset = &asset;
82 return false;
83 }
84 return true;
85 });
86
87 if (reports && !matching_asset) {
88 if (list::is_loaded(&library_ref)) {
90 reports, RPT_ERROR, "No asset found at path \"%s\"", weak_ref.relative_asset_identifier);
91 }
92 else {
93 BKE_report(reports, RPT_WARNING, "Asset loading is unfinished");
94 }
95 }
96 return matching_asset;
97}
98
100 const bContext &C, const AssetWeakReference &weak_ref, ReportList *reports)
101{
102 if (weak_ref.asset_library_type == ASSET_LIBRARY_LOCAL) {
103 return get_local_asset_from_weak_ref(C, weak_ref, reports);
104 }
105
107 list::storage_fetch(&library_ref, &C);
110 if (!all_library) {
111 BKE_report(reports, RPT_WARNING, "Asset loading is unfinished");
112 return nullptr;
113 }
114
115 const asset_system::AssetRepresentation *matching_asset = nullptr;
117 if (asset.make_weak_reference() == weak_ref) {
118 matching_asset = &asset;
119 return false;
120 }
121 return true;
122 });
123
124 if (reports && !matching_asset) {
125 if (list::is_loaded(&library_ref)) {
126 const std::string full_path = all_library->resolve_asset_weak_reference_to_full_path(
127 weak_ref);
128 BKE_reportf(reports, RPT_ERROR, "No asset found at path \"%s\"", full_path.c_str());
129 }
130 }
131 return matching_asset;
132}
133
135 const bContext &C, PointerRNA &ptr, ReportList *reports)
136{
137 AssetWeakReference weak_ref{};
138 weak_ref.asset_library_type = eAssetLibraryType(RNA_enum_get(&ptr, "asset_library_type"));
140 &ptr, "asset_library_identifier", nullptr, 0, nullptr);
142 &ptr, "relative_asset_identifier", nullptr, 0, nullptr);
143 return find_asset_from_weak_ref(C, weak_ref, reports);
144}
145
147 const StringRefNull menu_name,
148 ui::Layout &layout)
149{
150 ui::Layout &col = layout.column(false);
151 col.context_string_set("asset_catalog_path", item.catalog_path().c_str());
152 col.menu(menu_name, IFACE_(item.get_name()), ICON_NONE);
153}
154
156 const StringRefNull operator_id,
157 const StringRefNull menu_name,
158 ui::Layout &layout)
159{
160 ui::Layout &col = layout.column(false);
161 col.context_string_set("asset_catalog_path", item.catalog_path().c_str());
162 col.context_string_set("operator_id", operator_id);
163 col.menu(menu_name, IFACE_(item.get_name()), ICON_NONE);
164}
165
167 ui::Layout &layout,
168 FunctionRef<void(ui::Layout &, BIFIconID)> draw_item_fn)
169{
170 ui::Layout &row = layout.row(true);
171 if (asset->is_online_only()) {
172 row.enabled_set(false);
173 }
175 nullptr, RNA_AssetRepresentation, const_cast<asset_system::AssetRepresentation *>(asset));
176 row.context_ptr_set("asset", &asset_ptr);
177
178 const int icon_local = asset->remote_file_status() ==
180 ICON_STATUS_WARNING :
181 ICON_NONE;
182 const int icon = asset->is_online_only() ? ICON_INTERNET : icon_local;
183 draw_item_fn(row, icon);
184}
185
187 StringRefNull opname,
188 const wm::OpCallContext operator_call_context,
189 ui::Layout &layout)
190{
191 draw_asset_menu_item(asset, layout, [&](ui::Layout &sub, BIFIconID asset_icon) {
192 PointerRNA props_ptr = sub.op(
193 opname, IFACE_(asset->get_name()), asset_icon, operator_call_context, UI_ITEM_NONE);
195 });
196}
197
198} // namespace blender::ed::asset
Main runtime representation of an asset.
#define IFACE_(msgid)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
std::string resolve_asset_weak_reference_to_full_path(const AssetWeakReference &asset_reference)
AssetLibraryReference all_library_reference()
void storage_fetch(const AssetLibraryReference *library_reference, const bContext *C)
void iterate(const AssetLibraryReference &library_reference, AssetListIterFn fn)
asset_system::AssetLibrary * library_get_once_available(const AssetLibraryReference &library_reference)
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_weak_ref(const bContext &C, const AssetWeakReference &weak_ref, ReportList *reports)
void draw_node_menu_for_catalog(const asset_system::AssetCatalogTreeItem &item, const StringRefNull operator_id, const StringRefNull menu_name, ui::Layout &layout)
void operator_asset_reference_props_register(StructRNA &srna)
void draw_menu_for_catalog(const asset_system::AssetCatalogTreeItem &item, const StringRefNull menu_name, ui::Layout &layout)
void draw_asset_menu_item(const asset_system::AssetRepresentation *asset, ui::Layout &layout, FunctionRef< void(ui::Layout &, BIFIconID)> draw_item_fn)
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)
bool operator_asset_reference_props_is_set(PointerRNA &ptr)
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
@ RPT_WARNING
Definition BKE_report.hh:40
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
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_enum_set(PointerRNA *ptr, const char *name, int value)
const EnumPropertyItem rna_enum_asset_library_type_items[]
Definition rna_asset.cc:24
@ ASSET_LIBRARY_LOCAL
char * RNA_string_get_alloc(PointerRNA *ptr, const char *name, char *fixedbuf, int fixedlen, int *r_len)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
int BIFIconID
Definition ED_asset.hh:30
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
eAssetLibraryType asset_library_type
void enabled_set(bool enabled)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
void menu(MenuType *mt, std::optional< StringRef > name, int icon)
Layout & column(bool align)
void context_string_set(StringRef name, StringRef value)
Layout & row(bool align)
void context_ptr_set(StringRef name, const PointerRNA *ptr)