Blender V5.3
geometry_nodes_caller_ui.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <fmt/format.h>
10#include <sstream>
11
13#include "BKE_context.hh"
14#include "BKE_global.hh"
15#include "BKE_lib_id.hh"
16#include "BKE_main.hh"
17#include "BKE_modifier.hh"
18#include "BKE_node.hh"
19#include "BKE_node_runtime.hh"
20#include "BKE_screen.hh"
21
22#include "BLI_string.hh"
23#include "BLI_string_utf8.hh"
24
25#include "BLT_translation.hh"
26
27#include "DNA_modifier_types.h"
29
30#include "ED_node.hh"
31#include "ED_object.hh"
32#include "ED_screen.hh"
33#include "ED_undo.hh"
34
35#include "MOD_nodes.hh"
36#include "NOD_caller_ui.hh"
37#include "NOD_eval_log.hh"
38#include "NOD_geometry.hh"
42
43#include "RNA_access.hh"
44#include "RNA_prototypes.hh"
45
46#include "UI_interface.hh"
48#include "UI_resources.hh"
49
51
52namespace blender::nodes {
53
54namespace {
55struct SearchInfo {
56 eval_log::NodeTreeLog *tree_log = nullptr;
57 bNodeTree *tree = nullptr;
58 std::optional<PointerRNA> socket_props_ptr;
59};
60
61struct ModifierSearchData {
62 uint32_t object_session_uid;
63 char modifier_name[MAX_NAME];
64};
65
66struct OperatorSearchData {
68 eval_log::NodeTreeLog *tree_log = nullptr;
69 bNodeTree *tree = nullptr;
70 wmOperator *op = nullptr;
71};
72
73struct SocketSearchData {
74 std::variant<ModifierSearchData, OperatorSearchData> search_data;
75 char socket_identifier[MAX_NAME];
76 bool is_output;
77
78 SearchInfo info(const bContext &C) const;
79};
80/* This class must not have a destructor, since it is used by buttons and freed with
81 * #MEM_delete_void. */
82BLI_STATIC_ASSERT(std::is_trivially_destructible_v<SocketSearchData>, "");
83
84struct DrawGroupInputsContext {
85 const bContext &C;
86 bNodeTree *tree;
87 eval_log::NodeTreeLog *tree_log;
88 PointerRNA *properties_ptr;
89 PointerRNA *bmain_ptr;
90 Array<nodes::socket_usage_inference::SocketUsage> input_usages;
91 Array<nodes::socket_usage_inference::SocketUsage> output_usages;
92 bool use_name_for_ids = false;
93 std::function<SocketSearchData(const bNodeTreeInterfaceSocket &)> socket_search_data_fn;
94 std::function<void(ui::Layout &, int icon, const bNodeTreeInterfaceSocket &)>
95 draw_attribute_toggle_fn;
96
97 bool input_is_visible(const bNodeTreeInterfaceSocket &socket) const
98 {
99 return this->input_usages[this->tree->interface_input_index(socket)].is_visible;
100 }
101
102 bool input_is_active(const bNodeTreeInterfaceSocket &socket) const
103 {
104 return this->input_usages[this->tree->interface_input_index(socket)].is_used;
105 }
106};
107} // namespace
108
110{
111 if (!nmd.runtime->eval_log) {
112 return nullptr;
113 }
114 bke::DataBlockComputeContext data_block_context{nullptr, object.id};
115 bke::GeometryNodesModifierComputeContext modifier_context{&data_block_context, nmd};
116 return &nmd.runtime->eval_log->get_tree_log(modifier_context.hash());
117}
118
119static std::optional<ed::space_node::ObjectAndModifier> get_modifier_data(
120 Main &bmain, const wmWindowManager &wm, const ModifierSearchData &data)
121{
123 /* Work around an issue where the attribute search exec function has stale pointers when data
124 * is reallocated when evaluating the node tree, causing a crash. This would be solved by
125 * allowing the UI search data to own arbitrary memory rather than just referencing it. */
126 return std::nullopt;
127 }
128
129 const Object *object = id_cast<Object *>(
130 BKE_libblock_find_session_uid(&bmain, ID_OB, data.object_session_uid));
131 if (object == nullptr) {
132 return std::nullopt;
133 }
134 ModifierData *md = BKE_modifiers_findby_name(object, data.modifier_name);
135 if (md == nullptr) {
136 return std::nullopt;
137 }
139 return ed::space_node::ObjectAndModifier{object, reinterpret_cast<NodesModifierData *>(md)};
140}
141
142SearchInfo SocketSearchData::info(const bContext &C) const
143{
144 if (const auto *modifier_search_data = std::get_if<ModifierSearchData>(&this->search_data)) {
145 const std::optional<ed::space_node::ObjectAndModifier> object_and_modifier = get_modifier_data(
146 *CTX_data_main(&C), *CTX_wm_manager(&C), *modifier_search_data);
147 if (!object_and_modifier) {
148 return {};
149 }
150 const NodesModifierData *nmd = object_and_modifier->nmd;
151 if (nmd->node_group == nullptr || ID_MISSING(nmd->node_group)) {
152 return {};
153 }
154 eval_log::NodeTreeLog *tree_log = get_root_tree_log(*object_and_modifier->object, *nmd);
155 PointerRNA nmd_ptr = RNA_pointer_create_discrete(
156 &const_cast<Object *>(object_and_modifier->object)->id,
157 RNA_NodesModifier,
158 const_cast<NodesModifierData *>(nmd));
159 PointerRNA properties_ptr = RNA_pointer_get(&nmd_ptr, "properties");
160 const bNodeTree &node_group = *nmd->node_group;
161 const bool is_output = bke::node_find_interface_output_by_identifier(node_group,
162 this->socket_identifier);
163 PointerRNA inputs_ptr = RNA_pointer_get(&properties_ptr, is_output ? "outputs" : "inputs");
164 PointerRNA socket_props_ptr = RNA_pointer_get(&inputs_ptr, this->socket_identifier);
165 return {tree_log, nmd->node_group, socket_props_ptr};
166 }
167 if (const auto *operator_search_data = std::get_if<OperatorSearchData>(&this->search_data)) {
168 PointerRNA op_ptr = RNA_pointer_create_discrete(
169 &CTX_wm_manager(&C)->id, RNA_Operator, operator_search_data->op);
170 PointerRNA properties_ptr = RNA_pointer_get(&op_ptr, "properties");
171 PointerRNA inputs_ptr = RNA_pointer_get(&properties_ptr, "inputs");
172 PointerRNA socket_props_ptr = RNA_pointer_get(&inputs_ptr, this->socket_identifier);
173 return {operator_search_data->tree_log, operator_search_data->tree, socket_props_ptr};
174 }
175 return {};
176}
177
179 const bContext *C, void *arg, const char *str, ui::SearchItems *items, const bool is_first)
180{
181 const SocketSearchData &data = *static_cast<SocketSearchData *>(arg);
182 const SearchInfo info = data.info(*C);
183 if (!info.tree || !info.tree_log) {
184 return;
185 }
186 info.tree_log->ensure_layer_names();
187 info.tree->ensure_topology_cache();
188
189 Vector<const bNodeSocket *> sockets_to_check;
190 for (const bNode *node : info.tree->group_input_nodes()) {
191 for (const bNodeSocket *socket : node->output_sockets()) {
192 if (socket->type == SOCK_GEOMETRY) {
193 sockets_to_check.append(socket);
194 }
195 }
196 }
197
198 Set<StringRef> names;
199 Vector<const std::string *> layer_names;
200 for (const bNodeSocket *socket : sockets_to_check) {
201 const eval_log::ValueLog *value_log = info.tree_log->find_socket_value_log(*socket);
202 if (value_log == nullptr) {
203 continue;
204 }
205 if (const auto *eval_log = dynamic_cast<const eval_log::GeometryInfoLog *>(value_log)) {
206 if (const std::optional<eval_log::GeometryInfoLog::GreasePencilInfo> &grease_pencil_info =
207 eval_log->grease_pencil_info)
208 {
209 for (const std::string &name : grease_pencil_info->layer_names) {
210 if (names.add(name)) {
211 layer_names.append(&name);
212 }
213 }
214 }
215 }
216 }
217 BLI_assert(items);
218 ui::grease_pencil_layer_search_add_items(str, layer_names.as_span(), *items, is_first);
219}
220
221static void layer_name_search_exec_fn(bContext *C, void *data_v, void *item_v)
222{
223 const SocketSearchData &data = *static_cast<SocketSearchData *>(data_v);
224 const std::string *item = static_cast<std::string *>(item_v);
225 if (!item) {
226 return;
227 }
228 SearchInfo info = data.info(*C);
229 if (!info.socket_props_ptr) {
230 return;
231 }
232
233 RNA_string_set(&*info.socket_props_ptr, "layer_name", item->c_str());
234 ED_undo_push(C, "Assign Layer Name");
235}
236
237static void add_layer_name_search_button(DrawGroupInputsContext &ctx,
238 ui::Layout &layout,
239 const bNodeTreeInterfaceSocket &socket,
240 PointerRNA *socket_props_ptr)
241{
242 if (!ctx.tree_log) {
243 layout.prop(socket_props_ptr, "layer_name", UI_ITEM_NONE, "", ICON_NONE);
244 return;
245 }
246
247 layout.use_property_decorate_set(false);
248
250 ui::Layout &name_row = split.row(false);
252
253 name_row.label(IFACE_(socket.name()), ICON_NONE);
254 ui::Layout &prop_row = split.row(true);
255
256 ui::Block *block = prop_row.block();
257 ui::Button *but = uiDefIconTextButR(block,
259 ICON_OUTLINER_DATA_GP_LAYER,
260 "",
261 0,
262 0,
263 10 * UI_UNIT_X, /* Dummy value, replaced by layout system. */
264 UI_UNIT_Y,
265 socket_props_ptr,
266 "layer_name",
267 0,
268 socket.description());
269 button_placeholder_set(but, IFACE_("Layer"));
270 layout.label("", ICON_BLANK1);
271
272 const Object *object = ed::object::context_object(&ctx.C);
273 BLI_assert(object != nullptr);
274 if (object == nullptr) {
275 return;
276 }
277
278 /* Using a custom free function make the search not work currently. So make sure this data can be
279 * freed with MEM_delete. */
280 SocketSearchData *data = static_cast<SocketSearchData *>(
281 MEM_new_uninitialized(sizeof(SocketSearchData), __func__));
282 *data = ctx.socket_search_data_fn(socket);
283 button_func_search_set_results_are_suggestions(but, true);
284 button_func_search_set_sep_string(but, UI_MENU_ARROW_SEP);
285 button_func_search_set(but,
286 nullptr,
288 data,
289 true,
290 nullptr,
292 nullptr);
293}
294
296 const bContext *C, void *arg, const char *str, ui::SearchItems *items, const bool is_first)
297{
298 SocketSearchData &data = *static_cast<SocketSearchData *>(arg);
299 const SearchInfo info = data.info(*C);
300 if (!info.tree || !info.tree_log) {
301 return;
302 }
303 info.tree_log->ensure_existing_attributes();
304 info.tree->ensure_topology_cache();
305
306 Vector<const bNodeSocket *> sockets_to_check;
307 if (data.is_output) {
308 for (const bNode *node : info.tree->nodes_by_type("NodeGroupOutput"_ustr)) {
309 for (const bNodeSocket *socket : node->input_sockets()) {
310 if (socket->type == SOCK_GEOMETRY) {
311 sockets_to_check.append(socket);
312 }
313 }
314 }
315 }
316 else {
317 for (const bNode *node : info.tree->group_input_nodes()) {
318 for (const bNodeSocket *socket : node->output_sockets()) {
319 if (socket->type == SOCK_GEOMETRY) {
320 sockets_to_check.append(socket);
321 }
322 }
323 }
324 }
325 Set<StringRef> names;
327 for (const bNodeSocket *socket : sockets_to_check) {
328 const eval_log::ValueLog *value_log = info.tree_log->find_socket_value_log(*socket);
329 if (value_log == nullptr) {
330 continue;
331 }
332 if (const auto *eval_log = dynamic_cast<const eval_log::GeometryInfoLog *>(value_log)) {
333 for (const eval_log::GeometryAttributeInfo &attribute : eval_log->attributes) {
334 if (names.add(attribute.name)) {
335 attributes.append(&attribute);
336 }
337 }
338 }
339 }
340 ui::attribute_search_add_items(str, data.is_output, attributes.as_span(), items, is_first);
341}
342
343static void attribute_search_exec_fn(bContext *C, void *data_v, void *item_v)
344{
345 if (item_v == nullptr) {
346 return;
347 }
348 SocketSearchData &data = *static_cast<SocketSearchData *>(data_v);
349 const auto &item = *static_cast<const eval_log::GeometryAttributeInfo *>(item_v);
350 SearchInfo info = data.info(*C);
351 if (!info.socket_props_ptr) {
352 return;
353 }
354
355 RNA_string_set(&*info.socket_props_ptr, "attribute_name", item.name.c_str());
356 ED_undo_push(C, "Assign Attribute Name");
357}
358
359static void add_attribute_search_button(DrawGroupInputsContext &ctx,
360 ui::Layout &layout,
361 PointerRNA *socket_props_ptr,
362 const bNodeTreeInterfaceSocket &socket)
363{
364 if (!ctx.tree_log) {
365 layout.prop(socket_props_ptr, "attribute_name", UI_ITEM_NONE, "", ICON_NONE);
366 return;
367 }
368
369 ui::Block *block = layout.block();
370 ui::Button *but = uiDefIconTextButR(block,
372 ICON_NONE,
373 "",
374 0,
375 0,
376 10 * UI_UNIT_X, /* Dummy value, replaced by layout system. */
377 UI_UNIT_Y,
378 socket_props_ptr,
379 "attribute_name",
380 0,
381 socket.description());
382
383 const Object *object = ed::object::context_object(&ctx.C);
384 BLI_assert(object != nullptr);
385 if (object == nullptr) {
386 return;
387 }
388
389 /* Using a custom free function make the search not work currently. So make sure this data can be
390 * freed with MEM_delete. */
391 SocketSearchData *data = static_cast<SocketSearchData *>(
392 MEM_new_uninitialized(sizeof(SocketSearchData), __func__));
393 *data = ctx.socket_search_data_fn(socket);
394 button_func_search_set_results_are_suggestions(but, true);
395 button_func_search_set_sep_string(but, UI_MENU_ARROW_SEP);
396 button_func_search_set(but,
397 nullptr,
399 data,
400 true,
401 nullptr,
403 nullptr);
404
405 std::string attribute_name = RNA_string_get(socket_props_ptr, "attribute_name");
406 const bool access_allowed = bke::allow_procedural_attribute_access(attribute_name);
407 if (!access_allowed) {
408 button_flag_enable(but, ui::BUT_REDALERT);
409 }
410}
411
413 DrawGroupInputsContext &ctx,
414 ui::Layout &layout,
415 const bNodeTreeInterfaceSocket &socket,
416 PointerRNA *socket_props_ptr,
417 const std::optional<StringRefNull> use_name = std::nullopt)
418{
419 const bke::bNodeSocketType *typeinfo = socket.socket_typeinfo();
420 const eNodeSocketDatatype type = typeinfo ? typeinfo->type : SOCK_CUSTOM;
421
422 const bool show_attribute_input = RNA_enum_get(socket_props_ptr, "type") ==
424
425 /* We're handling this manually in this case. */
426 layout.use_property_decorate_set(false);
427
429 ui::Layout &name_row = split.row(false);
431
432 ui::Layout *prop_row = nullptr;
433 const StringRefNull socket_name = use_name.has_value() ? (*use_name) :
434 IFACE_(socket.name().c_str());
435
436 if (type == SOCK_BOOLEAN && !show_attribute_input) {
437 name_row.label("", ICON_NONE);
438 prop_row = &split.row(true);
439 }
440 else {
441 prop_row = &layout.row(true);
442 }
443
444 if (type == SOCK_BOOLEAN) {
445 prop_row->use_property_split_set(false);
447 }
448
449 if (show_attribute_input) {
450 name_row.label(IFACE_(socket_name), ICON_NONE);
451 prop_row = &split.row(true);
452 add_attribute_search_button(ctx, *prop_row, socket_props_ptr, socket);
453 layout.label("", ICON_BLANK1);
454 }
455 else {
456 const StringRef name = IFACE_(socket_name);
457 prop_row->prop(socket_props_ptr, "value", UI_ITEM_NONE, name, ICON_NONE);
458 layout.decorator(socket_props_ptr, "value", -1);
459 }
460
461 ctx.draw_attribute_toggle_fn(*prop_row, ICON_SPREADSHEET, socket);
462}
463
464/* Drawing the properties manually with #ui::Layout::prop instead of #uiDefAutoButsRNA allows using
465 * the node socket identifier for the property names, since they are unique, but also having
466 * the correct label displayed in the UI. */
467static void draw_property_for_socket(DrawGroupInputsContext &ctx,
468 ui::Layout &layout,
469 const bNodeTreeInterfaceSocket &socket,
470 PointerRNA *socket_props_ptr,
471 const std::optional<StringRef> parent_name = std::nullopt)
472{
473 const int input_index = ctx.tree->interface_input_index(socket);
474 if (!ctx.input_is_visible(socket)) {
475 /* The input is not used currently, but it would be used if any menu input is changed.
476 * By convention, the input is hidden in this case instead of just grayed out. */
477 return;
478 }
479
480 ui::Layout &row = layout.row(true);
482 row.active_set(ctx.input_is_active(socket));
483
484 /* Use #ui::Layout::prop_search to draw pointer properties because #ui::Layout::prop would not
485 * have enough information about what type of ID to select for editing the values. This is
486 * because pointer IDProperties contain no information about their type. */
487 const bke::bNodeSocketType *typeinfo = socket.socket_typeinfo();
488 const eNodeSocketDatatype type = typeinfo ? typeinfo->type : SOCK_CUSTOM;
489
491 return;
492 }
493
494 std::string name = IFACE_(socket.name());
495
496 /* If the property has a prefix that's the same string as the name of the panel it's in, remove
497 * the prefix so it appears less verbose. */
498 if (parent_name.has_value()) {
499 const StringRef prefix_to_remove = *parent_name;
500 const int prefix_size = prefix_to_remove.size();
501 const int pos = name.find(prefix_to_remove);
502 if (pos == 0 && name.size() > prefix_size && name[prefix_size] == ' ') {
503 name = name.substr(prefix_size + 1);
504 }
505 }
506
507 switch (type) {
508 case SOCK_OBJECT: {
509 row.prop_search(socket_props_ptr, "value", ctx.bmain_ptr, "objects", name, ICON_OBJECT_DATA);
510 break;
511 }
512 case SOCK_COLLECTION: {
513 row.prop_search(
514 socket_props_ptr, "value", ctx.bmain_ptr, "collections", name, ICON_OUTLINER_COLLECTION);
515 break;
516 }
517 case SOCK_MATERIAL: {
518 row.prop_search(socket_props_ptr, "value", ctx.bmain_ptr, "materials", name, ICON_MATERIAL);
519 break;
520 }
521 case SOCK_TEXTURE: {
522 row.prop_search(socket_props_ptr, "value", ctx.bmain_ptr, "textures", name, ICON_TEXTURE);
523 break;
524 }
525 case SOCK_FONT: {
526 PropertyRNA *prop = RNA_struct_find_property(socket_props_ptr, "value");
527 if (prop && RNA_property_type(prop) == PROP_POINTER) {
528 template_id(&row,
529 &ctx.C,
530 socket_props_ptr,
531 "value",
532 nullptr,
533 "FONT_OT_open",
534 "FONT_OT_unlink",
536 false,
537 name);
538 }
539 else {
540 /* #template_id only supports pointer properties currently. Node tools store
541 * data-block pointers in strings currently. */
542 row.prop_search(socket_props_ptr, "value", ctx.bmain_ptr, "fonts", name, ICON_FONT_DATA);
543 }
544 break;
545 }
546 case SOCK_SCENE: {
547 row.prop_search(socket_props_ptr, "value", ctx.bmain_ptr, "scenes", name, ICON_SCENE);
548 break;
549 }
550 case SOCK_TEXT_ID: {
551 row.prop_search(socket_props_ptr, "value", ctx.bmain_ptr, "texts", name, ICON_TEXT);
552 break;
553 }
554 case SOCK_MASK: {
555 row.prop_search(socket_props_ptr, "value", ctx.bmain_ptr, "masks", name, ICON_NONE);
556 break;
557 }
558 case SOCK_SOUND: {
559 PropertyRNA *prop = RNA_struct_find_property(socket_props_ptr, "value");
560 if (prop && RNA_property_type(prop) == PROP_POINTER) {
561 template_id(&row, &ctx.C, socket_props_ptr, "value", nullptr, "SOUND_OT_open", nullptr);
562 }
563 else {
564 /* #template_id only supports pointer properties currently. Node tools store
565 * data-block pointers in strings currently. */
566 row.prop_search(socket_props_ptr, "value", ctx.bmain_ptr, "sounds", name, ICON_SOUND);
567 }
568
569 break;
570 }
571 case SOCK_IMAGE: {
572 PropertyRNA *prop = RNA_struct_find_property(socket_props_ptr, "value");
573 if (prop && RNA_property_type(prop) == PROP_POINTER) {
574 template_id(&row,
575 &ctx.C,
576 socket_props_ptr,
577 "value",
578 "image.new",
579 "image.open",
580 nullptr,
582 false,
583 name);
584 }
585 else {
586 /* #template_id only supports pointer properties currently. Node tools store
587 * data-block pointers in strings currently. */
588 row.prop_search(socket_props_ptr, "value", ctx.bmain_ptr, "images", name, ICON_IMAGE_DATA);
589 }
590 break;
591 }
592 case SOCK_MENU: {
594 /* Use a single space when the name is empty to work around a bug with expanded enums. Also
595 * see #ui_item_enum_expand_exec. */
596 row.prop(socket_props_ptr,
597 "value",
599 StringRef(name).is_empty() ? " " : name,
600 ICON_NONE);
601 }
602 else {
603 row.prop(socket_props_ptr, "value", UI_ITEM_NONE, name, ICON_NONE);
604 }
605 break;
606 }
607 case SOCK_BOOLEAN: {
608 if (is_layer_selection_field(socket)) {
609 add_layer_name_search_button(ctx, row, socket, socket_props_ptr);
610 /* Adds a spacing at the end of the row. */
611 row.label("", ICON_BLANK1);
612 break;
613 }
615 }
616 default: {
617 if (nodes::input_has_attribute_toggle(*ctx.tree, input_index)) {
618 add_attribute_search_or_value_buttons(ctx, row, socket, socket_props_ptr, name);
619 }
620 else {
621 row.prop(socket_props_ptr, "value", UI_ITEM_NONE, name, ICON_NONE);
622 }
623 break;
624 }
625 }
626 if (!nodes::input_has_attribute_toggle(*ctx.tree, input_index)) {
627 row.label("", ICON_BLANK1);
628 }
629}
630
631static std::string get_node_warning_panel_name(const int num_errors,
632 const int num_warnings,
633 const int num_infos)
634{
635 fmt::memory_buffer buffer;
636 fmt::appender buf = fmt::appender(buffer);
637 if (num_errors > 0) {
638 fmt::format_to(buf, "{} ({})", IFACE_("Errors"), num_errors);
639 }
640 if (num_warnings > 0) {
641 if (num_errors > 0) {
642 fmt::format_to(buf, ", ");
643 }
644 fmt::format_to(buf, "{} ({})", IFACE_("Warnings"), num_warnings);
645 }
646 if (num_infos > 0) {
647 if (num_errors > 0 || num_warnings > 0) {
648 fmt::format_to(buf, ", ");
649 }
650 fmt::format_to(buf, "{} ({})", IFACE_("Info"), num_infos);
651 }
652 return std::string(buffer.data(), buffer.size());
653}
654
655static void draw_warnings(const bContext *C,
656 const NodesModifierData &nmd,
657 ui::Layout &layout,
658 PointerRNA *md_ptr)
659{
660 if (G.is_rendering) {
661 /* Avoid accessing this data while baking in a separate thread. */
662 return;
663 }
664 using namespace eval_log;
665 Object &object = *id_cast<Object *>(md_ptr->owner_id);
666 NodeTreeLog *tree_log = get_root_tree_log(object, nmd);
667 if (!tree_log) {
668 return;
669 }
670 tree_log->ensure_node_warnings(*CTX_data_main(C));
671 const int warnings_num = tree_log->all_warnings.size();
672 if (warnings_num == 0) {
673 return;
674 }
675 Map<NodeWarningType, int> count_by_type;
676 for (const NodeWarning &warning : tree_log->all_warnings) {
677 count_by_type.lookup_or_add(warning.type, 0)++;
678 }
679 const int num_errors = count_by_type.lookup_default(NodeWarningType::Error, 0);
680 const int num_warnings = count_by_type.lookup_default(NodeWarningType::Warning, 0);
681 const int num_infos = count_by_type.lookup_default(NodeWarningType::Info, 0);
682 const std::string panel_name = get_node_warning_panel_name(num_errors, num_warnings, num_infos);
683 ui::PanelLayout panel = layout.panel_prop(C, md_ptr, "open_warnings_panel");
684 panel.header->label(panel_name, ICON_NONE);
685 if (!panel.body) {
686 return;
687 }
688 Vector<const NodeWarning *> warnings(tree_log->all_warnings.size());
689 for (const int i : warnings.index_range()) {
690 warnings[i] = &tree_log->all_warnings[i];
691 }
692 std::ranges::sort(warnings, [](const NodeWarning *a, const NodeWarning *b) {
693 const int severity_a = node_warning_type_severity(a->type);
694 const int severity_b = node_warning_type_severity(b->type);
695 if (severity_a > severity_b) {
696 return true;
697 }
698 if (severity_a < severity_b) {
699 return false;
700 }
701 return BLI_strcasecmp_natural(a->message.c_str(), b->message.c_str()) < 0;
702 });
703
704 ui::Layout &col = panel.body->column(false);
705 ui::Block *block = col.block();
706 for (const NodeWarning *warning : warnings) {
707 const int icon = node_warning_type_icon(warning->type);
708 const StringRef message = RPT_(warning->message);
709 ui::Button *but = uiDefIconTextBut(
710 block, ui::ButtonType::Label, icon, message, 0, 0, 1, UI_UNIT_Y, nullptr, std::nullopt);
711 /* Add tooltip containing the same message. This is helpful if the message is very long so that
712 * it doesn't fit in the panel. */
713 button_func_tooltip_set(
714 but,
715 [](bContext * /*C*/, void *argN, StringRef /*tip*/) -> std::string {
716 return *static_cast<std::string *>(argN);
717 },
718 MEM_new<std::string>(__func__, message),
719 [](void *arg) { MEM_delete(static_cast<std::string *>(arg)); });
720 }
721}
722
723static bool has_output_attribute(const DrawGroupInputsContext &ctx, const bNodeTree *tree)
724{
725 if (!tree || ID_MISSING(tree)) {
726 return false;
727 }
728 const Span<const bNodeTreeInterfaceSocket *> interface_outputs = ctx.tree->interface_outputs();
729 for (const int i : interface_outputs.index_range()) {
730 const bNodeTreeInterfaceSocket &socket = *interface_outputs[i];
731 if (!ctx.output_usages[i].is_visible) {
732 continue;
733 }
734 const bke::bNodeSocketType *typeinfo = socket.socket_typeinfo();
735 const eNodeSocketDatatype type = typeinfo ? typeinfo->type : SOCK_CUSTOM;
737 return true;
738 }
739 }
740 return false;
741}
742
743static void draw_property_for_output_socket(DrawGroupInputsContext &ctx,
744 ui::Layout &layout,
745 const bNodeTreeInterfaceSocket &socket)
746{
748 ui::Layout &name_row = split.row(false);
750 name_row.label(socket.name(), ICON_NONE);
751
752 PropertyRNA *prop = RNA_struct_find_property(ctx.properties_ptr, "outputs");
753 if (!prop) {
754 return;
755 }
756 PointerRNA outputs_ptr = RNA_property_pointer_get(ctx.properties_ptr, prop);
757 PointerRNA output_socket_ptr = RNA_pointer_get(&outputs_ptr, socket.identifier);
758
759 ui::Layout &row = split.row(true);
760 add_attribute_search_button(ctx, row, &output_socket_ptr, socket);
761}
762
763static void draw_output_attributes_panel(DrawGroupInputsContext &ctx, ui::Layout &layout)
764{
765 const Span<const bNodeTreeInterfaceSocket *> interface_outputs = ctx.tree->interface_outputs();
766 for (const int i : interface_outputs.index_range()) {
767 const bNodeTreeInterfaceSocket &socket = *interface_outputs[i];
768 const bke::bNodeSocketType *typeinfo = socket.socket_typeinfo();
769 const eNodeSocketDatatype type = typeinfo ? typeinfo->type : SOCK_CUSTOM;
770 if (!ctx.output_usages[i].is_visible) {
771 continue;
772 }
774 draw_property_for_output_socket(ctx, layout, socket);
775 }
776 }
777}
778
779static void draw_bake_panel(ui::Layout &layout, PointerRNA *modifier_ptr)
780{
781 ui::Layout &col = layout.column(false);
782 col.use_property_split_set(true);
783 col.use_property_decorate_set(false);
784 col.prop(modifier_ptr, "bake_target", UI_ITEM_NONE, std::nullopt, ICON_NONE);
785 col.prop(modifier_ptr, "bake_directory", UI_ITEM_NONE, IFACE_("Bake Path"), ICON_NONE);
786}
787
789{
790 if (G.is_rendering) {
791 /* Avoid accessing this data while baking in a separate thread. */
792 return;
793 }
794 eval_log::NodeTreeLog *tree_log = get_root_tree_log(object, nmd);
795 if (tree_log == nullptr) {
796 return;
797 }
798
800 const Map<StringRefNull, eval_log::NamedAttributeUsage> &usage_by_attribute =
801 tree_log->used_named_attributes;
802
803 if (usage_by_attribute.is_empty()) {
804 layout.label(RPT_("No named attributes used"), ICON_STATUS_INFO);
805 return;
806 }
807
808 struct NameWithUsage {
809 StringRefNull name;
811 };
812
813 Vector<NameWithUsage> sorted_used_attribute;
814 for (auto &&item : usage_by_attribute.items()) {
815 sorted_used_attribute.append({item.key, item.value});
816 }
817 std::ranges::sort(sorted_used_attribute, [](const NameWithUsage &a, const NameWithUsage &b) {
818 return BLI_strcasecmp_natural(a.name.c_str(), b.name.c_str()) < 0;
819 });
820
821 for (const NameWithUsage &attribute : sorted_used_attribute) {
822 const StringRef attribute_name = attribute.name;
823 const eval_log::NamedAttributeUsage usage = attribute.usage;
824
825 /* #uiLayoutRowWithHeading doesn't seem to work in this case. */
827
828 std::stringstream ss;
829 Vector<std::string> usages;
830 if (flag_is_set(usage, eval_log::NamedAttributeUsage::Read)) {
831 usages.append(IFACE_("Read"));
832 }
833 if (flag_is_set(usage, eval_log::NamedAttributeUsage::Write)) {
834 usages.append(IFACE_("Write"));
835 }
836 if (flag_is_set(usage, eval_log::NamedAttributeUsage::Remove)) {
838 }
839 for (const int i : usages.index_range()) {
840 ss << usages[i];
841 if (i < usages.size() - 1) {
842 ss << ", ";
843 }
844 }
845
846 ui::Layout &row = split.row(false);
848 row.active_set(false);
849 row.label(ss.str(), ICON_NONE);
850
851 split.row(false).label(attribute_name, ICON_NONE);
852 }
853}
854
855static void draw_manage_panel(const bContext *C,
856 ui::Layout &layout,
857 PointerRNA *modifier_ptr,
859{
860 if (ui::Layout *panel_layout = layout.panel_prop(
861 C, modifier_ptr, "open_bake_panel", IFACE_("Bake")))
862 {
863 draw_bake_panel(*panel_layout, modifier_ptr);
864 }
865 if (ui::Layout *panel_layout = layout.panel_prop(
866 C, modifier_ptr, "open_named_attributes_panel", IFACE_("Named Attributes")))
867 {
868 Object &object = *id_cast<Object *>(modifier_ptr->owner_id);
869 draw_named_attributes_panel(*panel_layout, object, nmd);
870 }
871}
872
874 PointerRNA *modifier_ptr,
875 ui::Layout &layout)
876{
877 Main *bmain = CTX_data_main(&C);
878 PointerRNA bmain_ptr = RNA_main_pointer_create(bmain);
879 NodesModifierData &nmd = *modifier_ptr->data_as<NodesModifierData>();
880 Object &object = *reinterpret_cast<Object *>(modifier_ptr->owner_id);
881 PointerRNA properties_ptr = RNA_pointer_get(modifier_ptr, "properties");
882
883 DrawGroupInputsContext ctx{
884 C, nmd.node_group, get_root_tree_log(object, nmd), &properties_ptr, &bmain_ptr};
885
886 ctx.socket_search_data_fn = [&](const bNodeTreeInterfaceSocket &io_socket) -> SocketSearchData {
887 SocketSearchData data{};
888 ModifierSearchData &modifier_search_data = data.search_data.emplace<ModifierSearchData>();
889 modifier_search_data.object_session_uid = object.id.session_uid;
890 STRNCPY_UTF8(modifier_search_data.modifier_name, nmd.modifier.name);
891 STRNCPY_UTF8(data.socket_identifier, io_socket.identifier);
892 data.is_output = io_socket.flag & NODE_INTERFACE_SOCKET_OUTPUT;
893 return data;
894 };
895 ctx.draw_attribute_toggle_fn =
896 [&](ui::Layout &layout, const int icon, const bNodeTreeInterfaceSocket &io_socket) {
897 PointerRNA props = layout.op("object.geometry_nodes_input_attribute_toggle",
898 "",
899 icon,
902 RNA_string_set(&props, "modifier_name", nmd.modifier.name);
903 RNA_string_set(&props, "input_name", io_socket.identifier);
904 };
905
906 layout.use_property_split_set(true);
907 /* Decorators are added manually for supported properties because the
908 * attribute/value toggle requires a manually built layout anyway. */
909 layout.use_property_decorate_set(false);
910
912 const char *newop = (nmd.node_group == nullptr) ? "node.new_geometry_node_group_assign" :
913 "object.geometry_node_tree_copy_assign";
914 template_id(&layout, &C, modifier_ptr, "node_group", newop, nullptr, nullptr);
915 }
916
917 if (nmd.node_group != nullptr && !ID_MISSING(nmd.node_group)) {
918 nmd.runtime->usage_cache.ensure(object, nmd);
919 ctx.input_usages = nmd.runtime->usage_cache.inputs;
920 ctx.output_usages = nmd.runtime->usage_cache.outputs;
922 C,
923 layout,
924 &properties_ptr,
926 [&](const bNodeTreeInterfaceSocket &socket) { return ctx.input_is_visible(socket); },
927 [&](const bNodeTreeInterfaceSocket &socket) { return ctx.input_is_active(socket); },
928 [&](ui::Layout &layout,
929 const bNodeTreeInterfaceSocket &socket,
930 PointerRNA *socket_props_ptr,
931 const std::optional<StringRef> parent_name) {
932 draw_property_for_socket(ctx, layout, socket, socket_props_ptr, parent_name);
933 });
934 }
935
936 modifier_error_message_draw(layout, modifier_ptr);
937
938 draw_warnings(&C, nmd, layout, modifier_ptr);
939
940 if (has_output_attribute(ctx, nmd.node_group)) {
941 if (ui::Layout *panel_layout = layout.panel_prop(
942 &C, modifier_ptr, "open_output_attributes_panel", IFACE_("Output Attributes")))
943 {
944 draw_output_attributes_panel(ctx, *panel_layout);
945 }
946 }
947
948 if ((nmd.flag & NODES_MODIFIER_HIDE_MANAGE_PANEL) == 0) {
949 if (ui::Layout *panel_layout = layout.panel_prop(
950 &C, modifier_ptr, "open_manage_panel", IFACE_("Manage")))
951 {
952 draw_manage_panel(&C, *panel_layout, modifier_ptr, nmd);
953 }
954 }
955}
956
958 wmOperator &op,
959 bNodeTree &tree,
960 eval_log::NodeTreeLog *tree_log)
961{
962 ui::Layout &layout = *op.layout;
963 Main &bmain = *CTX_data_main(&C);
964 PointerRNA bmain_ptr = RNA_main_pointer_create(&bmain);
965
966 DrawGroupInputsContext ctx{C, &tree, tree_log, op.ptr, &bmain_ptr};
967 ctx.socket_search_data_fn = [&](const bNodeTreeInterfaceSocket &io_socket) -> SocketSearchData {
968 SocketSearchData data{};
969 OperatorSearchData &operator_search_data = data.search_data.emplace<OperatorSearchData>();
970 operator_search_data.tree = &tree;
971 operator_search_data.tree_log = tree_log;
972 operator_search_data.op = &op;
973 STRNCPY_UTF8(data.socket_identifier, io_socket.identifier);
974 data.is_output = io_socket.flag & NODE_INTERFACE_SOCKET_OUTPUT;
975 return data;
976 };
977 ctx.draw_attribute_toggle_fn =
978 [&](ui::Layout &layout, const int icon, const bNodeTreeInterfaceSocket &io_socket) {
979 PointerRNA inputs_ptr = RNA_pointer_get(op.ptr, "inputs");
980 PointerRNA socket_props_ptr = RNA_pointer_get(&inputs_ptr, io_socket.identifier);
981 layout.prop(&socket_props_ptr, "type", ui::ITEM_R_ICON_ONLY, "", icon);
982 };
983 ctx.use_name_for_ids = true;
984
985 layout.use_property_split_set(true);
986 /* Decorators are added manually for supported properties because the
987 * attribute/value toggle requires a manually built layout anyway. */
988 layout.use_property_decorate_set(false);
989
990 tree.ensure_interface_cache();
991 ctx.input_usages.reinitialize(tree.interface_inputs().size());
992 ctx.output_usages.reinitialize(tree.interface_outputs().size());
994 tree, *ctx.properties_ptr, ctx.input_usages, ctx.output_usages);
996 C,
997 layout,
998 op.ptr,
1000 [&](const bNodeTreeInterfaceSocket &socket) { return ctx.input_is_visible(socket); },
1001 [&](const bNodeTreeInterfaceSocket &socket) { return ctx.input_is_active(socket); },
1002 [&](ui::Layout &layout,
1003 const bNodeTreeInterfaceSocket &socket,
1004 PointerRNA *socket_props_ptr,
1005 const std::optional<StringRef> parent_name) {
1006 draw_property_for_socket(ctx, layout, socket, socket_props_ptr, parent_name);
1007 });
1008}
1009
1010} // namespace blender::nodes
#define ATTR_FALLTHROUGH
#define STRNCPY_UTF8(dst, src)
#define RPT_(msgid)
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define IFACE_(msgid)
#define ID_MISSING(_id)
Definition DNA_ID.h:695
#define MAX_NAME
Definition DNA_defs.h:63
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UNIT_X
#define UI_ITEM_NONE
#define ELEM(...)
const ComputeContextHash & hash() const
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
bool is_empty() const
Definition BLI_map.hh:986
ItemIterator items() const &
Definition BLI_map.hh:902
Value & lookup_or_add(const Key &key, const Value &value)
Definition BLI_map.hh:588
Array< nodes::socket_usage_inference::SocketUsage > outputs
Definition MOD_nodes.hh:45
void ensure(const Object &object, const NodesModifierData &nmd)
Array< nodes::socket_usage_inference::SocketUsage > inputs
Definition MOD_nodes.hh:44
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr StringRef()
void append(const T &value)
IndexRange index_range() const
Span< T > as_span() const
constexpr int64_t size() const
Map< StringRefNull, NamedAttributeUsage > used_named_attributes
#define str(s)
#define attribute(slot)
bool allow_procedural_attribute_access(StringRef attribute_name)
BLI_STATIC_ASSERT(MBC_BATCH_LEN< 64, "Number of batches exceeded the limit of bit fields")
Object * context_object(const bContext *C)
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition abc_util.cc:68
void infer_group_interface_inputs_usage(const bNodeTree &group, Span< const bNodeSocket * > input_sockets, MutableSpan< SocketUsage > r_input_usages)
static void draw_property_for_output_socket(DrawGroupInputsContext &ctx, ui::Layout &layout, const bNodeTreeInterfaceSocket &socket)
static void layer_name_search_update_fn(const bContext *C, void *arg, const char *str, ui::SearchItems *items, const bool is_first)
int node_warning_type_severity(const NodeWarningType type)
Definition warning.cc:51
bool input_has_attribute_toggle(const bNodeTree &node_tree, const int socket_index)
static void attribute_search_exec_fn(bContext *C, void *data_v, void *item_v)
static eval_log::NodeTreeLog * get_root_tree_log(const Object &object, const NodesModifierData &nmd)
static void add_layer_name_search_button(DrawGroupInputsContext &ctx, ui::Layout &layout, const bNodeTreeInterfaceSocket &socket, PointerRNA *socket_props_ptr)
int node_warning_type_icon(const NodeWarningType type)
Definition warning.cc:37
void draw_geometry_nodes_operator_redo_ui(const bContext &C, wmOperator &op, bNodeTree &tree, eval_log::NodeTreeLog *tree_log)
static void draw_named_attributes_panel(ui::Layout &layout, Object &object, NodesModifierData &nmd)
static std::optional< ed::space_node::ObjectAndModifier > get_modifier_data(Main &bmain, const wmWindowManager &wm, const ModifierSearchData &data)
static bool has_output_attribute(const DrawGroupInputsContext &ctx, const bNodeTree *tree)
static void add_attribute_search_button(DrawGroupInputsContext &ctx, ui::Layout &layout, PointerRNA *socket_props_ptr, const bNodeTreeInterfaceSocket &socket)
static void draw_manage_panel(const bContext *C, ui::Layout &layout, PointerRNA *modifier_ptr, NodesModifierData &nmd)
static void draw_output_attributes_panel(DrawGroupInputsContext &ctx, ui::Layout &layout)
static std::string get_node_warning_panel_name(const int num_errors, const int num_warnings, const int num_infos)
void draw_interface_panel_content(const bContext &C, ui::Layout &layout, PointerRNA *properties_ptr, const bNodeTreeInterfacePanel &interface_panel, FunctionRef< bool(const bNodeTreeInterfaceSocket &)> fn_input_is_visible, FunctionRef< bool(const bNodeTreeInterfaceSocket &)> fn_input_is_active, FunctionRef< void(ui::Layout &, const bNodeTreeInterfaceSocket &, PointerRNA *, const std::optional< StringRef >)> fn_draw_property_for_socket, const bool skip_first, const std::optional< StringRef > parent_name)
static void layer_name_search_exec_fn(bContext *C, void *data_v, void *item_v)
static void add_attribute_search_or_value_buttons(DrawGroupInputsContext &ctx, ui::Layout &layout, const bNodeTreeInterfaceSocket &socket, PointerRNA *socket_props_ptr, const std::optional< StringRefNull > use_name=std::nullopt)
static void draw_property_for_socket(DrawGroupInputsContext &ctx, ui::Layout &layout, const bNodeTreeInterfaceSocket &socket, PointerRNA *socket_props_ptr, const std::optional< StringRef > parent_name=std::nullopt)
static void draw_warnings(const bContext *C, const NodesModifierData &nmd, ui::Layout &layout, PointerRNA *md_ptr)
static void draw_bake_panel(ui::Layout &layout, PointerRNA *modifier_ptr)
bool socket_type_has_attribute_toggle(const eNodeSocketDatatype type)
void draw_geometry_nodes_modifier_ui(const bContext &C, PointerRNA *modifier_ptr, ui::Layout &layout)
static void attribute_search_update_fn(const bContext *C, void *arg, const char *str, ui::SearchItems *items, const bool is_first)
void attribute_search_add_items(StringRef str, bool can_create_attribute, Span< const nodes::eval_log::GeometryAttributeInfo * > infos, SearchItems *items, bool is_first)
void grease_pencil_layer_search_add_items(StringRef str, Span< const std::string * > layer_names, SearchItems &items, bool is_first)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
ModifierData * BKE_modifiers_findby_name(const Object *ob, const char *name)
int RNA_enum_get(PointerRNA *ptr, const char *name)
PointerRNA RNA_main_pointer_create(Main *main)
int char char int int int BLI_strcasecmp_natural(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
ID * BKE_libblock_find_session_uid(Main *bmain, short type, uint32_t session_uid)
Definition lib_id.cc:1826
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
bool is_layer_selection_field(const bNodeTreeInterfaceSocket &socket)
Global G
Definition blender.cc:57
PropertyType RNA_property_type(PropertyRNA *prop)
void modifier_error_message_draw(ui::Layout &layout, PointerRNA *ptr)
unsigned int uint32_t
Definition BKE_text.h:81
@ PROP_POINTER
Definition RNA_types.hh:215
BMesh const char void * data
wmWindowManager * CTX_wm_manager(const bContext *C)
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
Main * CTX_data_main(const bContext *C)
@ NODES_MODIFIER_HIDE_DATABLOCK_SELECTOR
@ NODES_MODIFIER_HIDE_MANAGE_PANEL
std::string RNA_string_get(PointerRNA *ptr, const char *name)
bScreen * ED_screen_animation_playing(const wmWindowManager *wm)
#define UI_MENU_ARROW_SEP
NodesModifierRuntime * runtime
NodesModifierUsageInferenceCache usage_cache
Definition MOD_nodes.hh:69
std::shared_ptr< nodes::eval_log::NodesEvalLog > eval_log
Definition MOD_nodes.hh:58
T * data_as() const
Definition RNA_types.hh:139
bNodeTreeInterface tree_interface
Defines a socket type.
Definition BKE_node.hh:163
eNodeSocketDatatype type
Definition BKE_node.hh:198
void alignment_set(LayoutAlign alignment)
void prop_search(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop, PropertyRNA *item_searchpropname, std::optional< StringRefNull > name, int icon, bool results_are_suggestions)
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
void label(StringRef name, int icon)
void decorator(PointerRNA *ptr, PropertyRNA *prop, int index)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void active_set(bool active)
Layout & split(float percentage, bool align)
Layout & column(bool align)
PanelLayout panel_prop(const bContext *C, PointerRNA *open_prop_owner, StringRefNull open_prop_name)
Layout & row(bool align)
static constexpr float PROPERTY_SPLIT_FACTOR