Blender V5.3
add_menu_assets.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 "AS_asset_catalog.hh"
11#include "AS_asset_library.hh"
13
15#include "BLI_string_utf8.hh"
16
17#include "DNA_space_types.h"
18
19#include "BKE_asset.hh"
20#include "BKE_idprop.hh"
21#include "BKE_screen.hh"
22
23#include "BLT_translation.hh"
24
25#include "RNA_access.hh"
26
27#include "ED_asset.hh"
29#include "ED_node.hh"
30
31#include "node_intern.hh"
32
34
35static bool node_add_menu_poll(const bContext *C, MenuType * /*mt*/)
36{
37 return CTX_wm_space_node(C);
38}
39
41{
43 return asset::list::is_loaded(&all_library_ref);
44}
45
47{
48 asset::AssetFilterSettings type_filter{};
49 type_filter.id_types = FILTER_ID_NT;
50 auto meta_data_filter = [&](const AssetMetaData &meta_data) {
51 const IDProperty *tree_type = BKE_asset_metadata_idprop_find(&meta_data, "type");
52 if (tree_type == nullptr || IDP_int_get(tree_type) != node_tree.type) {
53 return false;
54 }
55 return true;
56 };
60 library, C, type_filter, meta_data_filter, node_tree.typeinfo->asset_catalog_path_prefix);
61}
62
71static Set<StringRef> get_builtin_menus(const int tree_type)
72{
73 Set<StringRef> menus;
74 switch (tree_type) {
75 case NTREE_GEOMETRY:
76 return {"Attribute",
77 "Input",
78 "Input/Constant",
79 "Input/Gizmo",
80 "Input/Group",
81 "Input/Import",
82 "Input/Scene",
83 "Output",
84 "Geometry",
85 "Geometry/Material",
86 "Geometry/Read",
87 "Geometry/Sample",
88 "Geometry/Write",
89 "Geometry/Operations",
90 "Curve",
91 "Curve/Read",
92 "Curve/Sample",
93 "Curve/Write",
94 "Curve/Operations",
95 "Curve/Primitives",
96 "Curve/Topology",
97 "Grease Pencil",
98 "Grease Pencil/Read",
99 "Grease Pencil/Operations",
100 "Grease Pencil/Write",
101 "Instances",
102 "Mesh",
103 "Mesh/Read",
104 "Mesh/Sample",
105 "Mesh/Write",
106 "Mesh/Operations",
107 "Mesh/Primitives",
108 "Mesh/Topology",
109 "Mesh/UV",
110 "Point",
111 "Volume",
112 "Volume/Operations",
113 "Volume/Primitives",
114 "Simulation",
115 "Color",
116 "Texture",
117 "Utilities",
118 "Utilities/Bundle",
119 "Utilities/Closure",
120 "Utilities/Text",
121 "Utilities/Vector",
122 "Utilities/Field",
123 "Utilities/Math",
124 "Utilities/Matrix",
125 "Utilities/Rotation",
126 "Utilities/Deprecated",
127 "Group",
128 "Layout"};
129 case NTREE_COMPOSIT:
130 return {"Input",
131 "Input/Constant",
132 "Input/Scene",
133 "Output",
134 "Color",
135 "Color/Adjust",
136 "Creative",
137 "Filter",
138 "Filter/Blur",
139 "Keying",
140 "Mask",
141 "Tracking",
142 "Transform",
143 "Texture",
144 "Utilities",
145 "Utilities/Math",
146 "Utilities/Vector",
147 "Group",
148 "Layout"};
149 case NTREE_SHADER:
150 return {"Input",
151 "Output",
152 "Shader",
153 "Displacement",
154 "Color",
155 "Texture",
156 "Utilities",
157 "Utilities/Math",
158 "Utilities/Vector",
159 "Group",
160 "Layout"};
161 }
162 return {};
163}
164
165static void node_catalog_assets_draw(const bContext *C, Menu *menu)
166{
167 SpaceNode &snode = *CTX_wm_space_node(C);
168 const bNodeTree *edit_tree = snode.edittree;
169 if (!edit_tree) {
170 return;
171 }
172 if (!snode.runtime->assets_for_menu) {
173 snode.runtime->assets_for_menu = std::make_shared<asset::AssetItemTree>(
174 build_catalog_tree(*C, *edit_tree));
175 return;
176 }
178
179 const std::optional<StringRefNull> menu_path = CTX_data_string_get(C, "asset_catalog_path");
180 if (!menu_path) {
181 return;
182 }
183
184 const std::optional<StringRefNull> operator_id = CTX_data_string_get(C, "operator_id");
185 if (!operator_id) {
186 return;
187 }
188
190 menu_path->c_str());
191 const asset_system::AssetCatalogTreeItem *catalog_item = tree.catalogs.find_item(
192 menu_path->c_str());
193 BLI_assert(catalog_item != nullptr);
194
195 if (assets.is_empty() && !catalog_item->has_children()) {
196 return;
197 }
198
199 ui::Layout *layout = menu->layout;
200 bool add_separator = true;
201
202 for (const asset_system::AssetRepresentation *asset : assets) {
203 if (add_separator) {
204 layout->separator();
205 add_separator = false;
206 }
208 asset, *operator_id, wm::OpCallContext::InvokeRegionWin, *layout);
209 }
210
211 const Set<StringRef> all_builtin_menus = get_builtin_menus(edit_tree->type);
212
213 catalog_item->foreach_child([&](const asset_system::AssetCatalogTreeItem &item) {
214 if (all_builtin_menus.contains_as(item.catalog_path().str())) {
215 return;
216 }
217 if (add_separator) {
218 layout->separator();
219 add_separator = false;
220 }
221 asset::draw_node_menu_for_catalog(item, *operator_id, "NODE_MT_node_catalog_assets", *layout);
222 });
223}
224
225static void node_unassigned_assets_draw(const bContext *C, Menu *menu)
226{
227 SpaceNode &snode = *CTX_wm_space_node(C);
228 const bNodeTree *edit_tree = snode.edittree;
229 if (!edit_tree) {
230 return;
231 }
232
233 const std::optional<StringRefNull> operator_id = CTX_data_string_get(C, "operator_id");
234 if (!operator_id) {
235 return;
236 }
237
238 if (!snode.runtime->assets_for_menu) {
239 snode.runtime->assets_for_menu = std::make_shared<asset::AssetItemTree>(
240 build_catalog_tree(*C, *edit_tree));
241 return;
242 }
246 asset, *operator_id, wm::OpCallContext::InvokeRegionWin, *menu->layout);
247 }
248}
249
250static void root_catalogs_draw(const bContext *C, Menu *menu, const StringRefNull operator_id)
251{
252 SpaceNode &snode = *CTX_wm_space_node(C);
253 ui::Layout *layout = menu->layout;
254 const bNodeTree *edit_tree = snode.edittree;
255 if (!edit_tree) {
256 return;
257 }
258
259 snode.runtime->assets_for_menu = std::make_shared<asset::AssetItemTree>(
260 build_catalog_tree(*C, *edit_tree));
261
262 const bool loading_finished = all_loading_finished();
263
265 if (tree.catalogs.is_empty() && loading_finished && tree.unassigned_assets.is_empty()) {
266 return;
267 }
268
269 layout->separator();
270
271 if (!loading_finished) {
272 layout->label(IFACE_("Loading Asset Libraries"), ICON_STATUS_INFO);
273 }
274
275 const Set<StringRef> all_builtin_menus = get_builtin_menus(edit_tree->type);
276
278 if (!all_builtin_menus.contains_as(item.catalog_path().str())) {
279 asset::draw_node_menu_for_catalog(item, operator_id, "NODE_MT_node_catalog_assets", *layout);
280 }
281 });
282
283 if (!tree.unassigned_assets.is_empty()) {
284 layout->separator();
285 layout->menu("NODE_MT_node_unassigned_assets", IFACE_("Unassigned"), ICON_FILE_HIDDEN);
286 }
287}
288
289static void add_root_catalogs_draw(const bContext *C, Menu *menu)
290{
291 const StringRefNull operator_id = "NODE_OT_add_group_asset";
292
293 menu->layout->context_string_set("operator_id", operator_id);
294 root_catalogs_draw(C, menu, operator_id);
295}
296
297static void swap_root_catalogs_draw(const bContext *C, Menu *menu)
298{
299 const StringRefNull operator_id = "NODE_OT_swap_group_asset";
300
301 menu->layout->context_string_set("operator_id", operator_id);
302 root_catalogs_draw(C, menu, operator_id);
303}
304
306{
307 MenuType type{};
308 STRNCPY_UTF8(type.idname, "NODE_MT_node_catalog_assets");
313 return type;
314}
315
317{
318 MenuType type{};
319 STRNCPY_UTF8(type.idname, "NODE_MT_node_unassigned_assets");
324 type.description = N_(
325 "Node group assets not assigned to a catalog.\n"
326 "Catalogs can be assigned in the Asset Browser");
327 return type;
328}
329
331{
332 MenuType type{};
333 STRNCPY_UTF8(type.idname, "NODE_MT_node_add_root_catalogs");
337 return type;
338}
339
341{
342 MenuType type{};
343 STRNCPY_UTF8(type.idname, "NODE_MT_node_swap_root_catalogs");
347 return type;
348}
349
351 const bContext &C,
352 const StringRef catalog_path,
353 const ui::NodeAssetMenuOperatorType operator_type)
354{
355 SpaceNode &snode = *CTX_wm_space_node(&C);
356 if (snode.runtime->assets_for_menu == nullptr) {
357 return;
358 }
360 const asset_system::AssetCatalogTreeItem *item = tree.catalogs.find_item(catalog_path);
361 if (!item) {
362 return;
363 }
364
365 StringRef operator_id;
366
367 switch (operator_type) {
369 operator_id = "NODE_OT_swap_group_asset";
370 break;
371 default:
372 operator_id = "NODE_OT_add_group_asset";
373 }
374
375 ui::Layout *col = &layout.column(false);
376 col->context_string_set("asset_catalog_path", item->catalog_path().str());
377 col->context_string_set("operator_id", operator_id);
378 col->menu_contents("NODE_MT_node_catalog_assets");
379}
380
381} // namespace blender::ed::space_node
Main runtime representation of an asset.
#define IDP_int_get(prop)
#define STRNCPY_UTF8(dst, src)
#define IFACE_(msgid)
#define FILTER_ID_NT
Definition DNA_ID.h:1215
#define C
Definition RandGen.cpp:29
bool contains_as(const ForwardKey &key) const
Definition BLI_set.hh:314
constexpr bool is_empty() const
Definition BLI_span.hh:264
void foreach_child(ItemIterFn callback) const
void foreach_root_item(ItemIterFn callback) const
const AssetCatalogTreeItem * find_item(const AssetCatalogPath &path) const
AssetLibraryReference all_library_reference()
void all_library_reload_catalogs_if_dirty()
bool is_loaded(const AssetLibraryReference *library_reference)
void asset_reading_region_listen_fn(const wmRegionListenerParams *params)
void draw_node_menu_for_catalog(const asset_system::AssetCatalogTreeItem &item, const StringRefNull operator_id, 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)
AssetItemTree build_filtered_all_catalog_tree(const AssetLibraryReference &library_ref, const bContext &C, const AssetFilterSettings &filter_settings, FunctionRef< bool(const AssetMetaData &)> meta_data_filter={}, const std::optional< StringRef > skip_prefix=std::nullopt)
static bool all_loading_finished()
void ui_template_node_asset_menu_items(ui::Layout &layout, const bContext &C, StringRef catalog_path, const ui::NodeAssetMenuOperatorType operator_type)
static Set< StringRef > get_builtin_menus(const int tree_type)
static void node_unassigned_assets_draw(const bContext *C, Menu *menu)
static void root_catalogs_draw(const bContext *C, Menu *menu, const StringRefNull operator_id)
MenuType swap_root_catalogs_menu_type()
static asset::AssetItemTree build_catalog_tree(const bContext &C, const bNodeTree &node_tree)
static void add_root_catalogs_draw(const bContext *C, Menu *menu)
static void swap_root_catalogs_draw(const bContext *C, Menu *menu)
static void node_catalog_assets_draw(const bContext *C, Menu *menu)
static bool node_add_menu_poll(const bContext *C, MenuType *)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
std::optional< StringRefNull > CTX_data_string_get(const bContext *C, const char *member)
IDProperty * BKE_asset_metadata_idprop_find(const AssetMetaData *asset_data, const char *name) ATTR_WARN_UNUSED_RESULT
Definition asset.cc:183
SpaceNode * CTX_wm_space_node(const bContext *C)
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
char idname[BKE_ST_MAXNAME]
bool(*) poll(const bContext *C, MenuType *mt)
MenuTypeFlag flag
void(*) draw(const bContext *C, Menu *menu)
const char * description
void(*) listener(const wmRegionListenerParams *params)
ui::Layout * layout
struct bNodeTree * edittree
ed::space_node::SpaceNode_Runtime * runtime
bke::bNodeTreeType * typeinfo
eNodeTree_Type type
std::optional< std::string > asset_catalog_path_prefix
Definition BKE_node.hh:546
MultiValueMap< asset_system::AssetCatalogPath, asset_system::AssetRepresentation * > assets_per_path
Vector< asset_system::AssetRepresentation * > unassigned_assets
asset_system::AssetCatalogTree catalogs
std::shared_ptr< asset::AssetItemTree > assets_for_menu
void label(StringRef name, int icon)
void menu_contents(StringRef menuname)
void menu(MenuType *mt, std::optional< StringRef > name, int icon)
Layout & column(bool align)
void context_string_set(StringRef name, StringRef value)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
#define N_(msgid)