Blender V5.3
node_templates.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 <cstdlib>
10#include <cstring>
11#include <optional>
12
13#include <fmt/format.h>
14
15#include "MEM_guardedalloc.h"
16
17#include "DNA_node_types.h"
18#include "DNA_screen_types.h"
19
20#include "BLI_listbase.hh"
21#include "BLI_string.hh"
22#include "BLI_string_utf8.hh"
23#include "BLI_vector.hh"
24
25#include "BLT_translation.hh"
26
27#include "BKE_context.hh"
28#include "BKE_lib_id.hh"
29#include "BKE_library.hh"
30#include "BKE_main.hh"
32#include "BKE_node_runtime.hh"
34
35#include "RNA_access.hh"
36#include "RNA_prototypes.hh"
37
39#include "NOD_socket.hh"
40
42
43#include "ED_node.hh" /* own include */
44#include "node_intern.hh"
45
46#include "ED_undo.hh"
47
48#include "WM_api.hh"
49
50namespace blender {
51
53
54namespace ed::space_node {
55
56/************************* Node Socket Manipulation **************************/
57
58/* describes an instance of a node type and a specific socket to link */
60 int socket_index; /* index for linking */
61 int socket_type; /* socket type for compatibility check */
62 const char *socket_name; /* ui label of the socket */
63 const char *node_name; /* ui label of the node */
64
65 /* extra settings */
66 bNodeTree *ngroup; /* group node tree */
67};
68
70{
71 item.socket_index = -1;
73 item.socket_name = nullptr;
74 item.node_name = nullptr;
75 item.ngroup = nullptr;
76}
77
78/* Compare an existing node to a link item to see if it can be reused.
79 * item must be for the same node type!
80 * XXX should become a node type callback
81 */
83{
84 if (node->is_group()) {
85 return (node->id == id_cast<ID *>(item->ngroup));
86 }
87 return true;
88}
89
91{
92 if (node->is_group()) {
93 node->id = id_cast<ID *>(item->ngroup);
95 }
96 else {
97 /* nothing to do for now */
98 }
99
100 if (node->id) {
101 id_us_plus(node->id);
102 }
103}
104
106{
107 if (!node || (node->flag & NODE_TEST)) {
108 return; /* in case of cycles */
109 }
110
111 node->flag |= NODE_TEST;
112
113 for (bNodeSocket &input : node->inputs) {
114 if (input.link) {
116 }
117 }
118}
119
121{
122 if (!node || !(node->flag & NODE_TEST)) {
123 return; /* in case of cycles */
124 }
125
126 node->flag &= ~NODE_TEST;
127
128 for (bNodeSocket &input : node->inputs) {
129 if (input.link) {
131 }
132 }
133}
134
135static void node_remove_linked(Main *bmain, bNodeTree *ntree, bNode *rem_node)
136{
137 bNode *node, *next;
138
139 if (!rem_node) {
140 return;
141 }
142
143 /* tag linked nodes to be removed */
144 for (bNode *node : ntree->all_nodes()) {
145 node->flag &= ~NODE_TEST;
146 }
147
148 node_tag_recursive(rem_node);
149
150 /* clear tags on nodes that are still used by other nodes */
151 for (bNode *node : ntree->all_nodes()) {
152 if (!(node->flag & NODE_TEST)) {
153 for (bNodeSocket &sock : node->inputs) {
154 if (sock.link && sock.link->fromnode != rem_node) {
156 }
157 }
158 }
159 }
160
161 /* remove nodes */
162 for (node = ntree->nodes.first(); node; node = next) {
163 next = node->next;
164
165 if (node->flag & NODE_TEST) {
166 bke::node_remove_node(bmain, *ntree, *node, true);
167 }
168 }
169}
170
171/* disconnect socket from the node it is connected to */
172static void node_socket_disconnect(Main *bmain,
173 bNodeTree *ntree,
174 bNode *node_to,
175 bNodeSocket *sock_to)
176{
177 if (!sock_to->link) {
178 return;
179 }
180
181 bke::node_remove_link(ntree, *sock_to->link);
182 sock_to->flag |= SOCK_COLLAPSED;
183
185 BKE_main_ensure_invariants(*bmain, ntree->id);
186}
187
188/* remove all nodes connected to this socket, if they aren't connected to other nodes */
189static void node_socket_remove(Main *bmain, bNodeTree *ntree, bNode *node_to, bNodeSocket *sock_to)
190{
191 if (!sock_to->link) {
192 return;
193 }
194
195 node_remove_linked(bmain, ntree, sock_to->link->fromnode);
196 sock_to->flag |= SOCK_COLLAPSED;
197
199 BKE_main_ensure_invariants(*bmain, ntree->id);
200}
201
202/* add new node connected to this socket, or replace an existing one */
204 bNodeTree *ntree,
205 bNode *node_to,
206 bNodeSocket *sock_to,
207 int type,
208 NodeLinkItem *item)
209{
210 Main *bmain = CTX_data_main(C);
211 bNode *node_from;
212 bNodeSocket *sock_from_tmp;
213 bNode *node_prev = nullptr;
214
215 /* unlink existing node */
216 if (sock_to->link) {
217 node_prev = sock_to->link->fromnode;
218 bke::node_remove_link(ntree, *sock_to->link);
219 }
220
221 /* find existing node that we can use */
222 for (node_from = ntree->nodes.first(); node_from; node_from = node_from->next) {
223 if (node_from->type_legacy == type) {
224 break;
225 }
226 }
227
228 if (node_from) {
229 if (node_from->inputs.first() || node_from->typeinfo->draw_buttons ||
230 node_from->typeinfo->draw_buttons_ex)
231 {
232 node_from = nullptr;
233 }
234 }
235
236 if (node_prev && node_prev->type_legacy == type && node_link_item_compare(node_prev, item)) {
237 /* keep the previous node if it's the same type */
238 node_from = node_prev;
239 }
240 else if (!node_from) {
241 node_from = bke::node_add_static_node(C, *ntree, type);
242 if (node_prev != nullptr) {
243 /* If we're replacing existing node, use its location. */
244 node_from->location[0] = node_prev->location[0];
245 node_from->location[1] = node_prev->location[1];
246 }
247 else {
248 sock_from_tmp = static_cast<bNodeSocket *>(
249 BLI_findlink(&node_from->outputs, item->socket_index));
250 bke::node_position_relative(*node_from, *node_to, sock_from_tmp, *sock_to);
251 }
252
253 node_link_item_apply(ntree, node_from, item);
254 BKE_main_ensure_invariants(*bmain, ntree->id);
255 }
256
257 bke::node_set_active(*ntree, *node_from);
258
259 /* add link */
260 sock_from_tmp = static_cast<bNodeSocket *>(
261 BLI_findlink(&node_from->outputs, item->socket_index));
262 bke::node_add_link(*ntree, *node_from, *sock_from_tmp, *node_to, *sock_to);
263 sock_to->flag &= ~SOCK_COLLAPSED;
264
265 /* copy input sockets from previous node */
266 if (node_prev && node_from != node_prev) {
267 for (bNodeSocket &sock_prev : node_prev->inputs) {
268 for (bNodeSocket &sock_from : node_from->inputs) {
269 if (bke::node_count_socket_links(*ntree, sock_from) >=
271 {
272 continue;
273 }
274
275 if (STREQ(sock_prev.identifier, sock_from.identifier) && sock_prev.type == sock_from.type)
276 {
277 bNodeLink *link = sock_prev.link;
278
279 if (link && link->fromnode) {
280 bke::node_add_link(*ntree, *link->fromnode, *link->fromsock, *node_from, sock_from);
281 bke::node_remove_link(ntree, *link);
282 }
284 sock_from.type, sock_from.default_value, sock_prev.default_value, true);
285 }
286 }
287 }
288
289 /* also preserve mapping for texture nodes */
290 if (node_from->typeinfo->nclass == NODE_CLASS_TEXTURE &&
291 node_prev->typeinfo->nclass == NODE_CLASS_TEXTURE &&
292 /* White noise texture node does not have NodeTexBase. */
293 node_from->storage != nullptr && node_prev->storage != nullptr)
294 {
295 memcpy(node_from->storage, node_prev->storage, sizeof(NodeTexBase));
296 }
297
298 /* remove node */
299 node_remove_linked(bmain, ntree, node_prev);
300 }
301
302 BKE_ntree_update_tag_node_property(ntree, node_from);
304 BKE_main_ensure_invariants(*bmain, ntree->id);
305}
306
307/****************************** Node Link Menu *******************************/
308
309// #define UI_NODE_LINK_ADD 0
310#define UI_NODE_LINK_DISCONNECT -1
311#define UI_NODE_LINK_REMOVE -2
312
325
327 int in_out,
328 std::optional<NodeDeclaration> &r_node_decl)
329{
331
332 if (arg->node_type->type_legacy == NODE_GROUP) {
333 for (bNodeTree &ngroup : arg->bmain->nodetrees) {
334 if (BKE_id_name(ngroup.id)[0] == '.') {
335 /* Don't display hidden node groups, just like the add menu. */
336 continue;
337 }
338
339 const char *disabled_hint;
340 if ((ngroup.type != arg->ntree->type) ||
341 !bke::node_group_poll(arg->ntree, &ngroup, &disabled_hint))
342 {
343 continue;
344 }
345
346 ngroup.ensure_interface_cache();
347 Span<bNodeTreeInterfaceSocket *> iosockets = (in_out == SOCK_IN ?
348 ngroup.interface_inputs() :
349 ngroup.interface_outputs());
350 for (const int index : iosockets.index_range()) {
351 bNodeTreeInterfaceSocket *iosock = iosockets[index];
352 NodeLinkItem item;
354 item.socket_index = index;
355 /* NOTE: int stemp->type is not fully reliable, not used for node group
356 * interface sockets. use the typeinfo->type instead.
357 */
358 const bke::bNodeSocketType *typeinfo = iosock->socket_typeinfo();
359 item.socket_type = typeinfo->type;
360 item.socket_name = iosock->name().c_str();
361 item.node_name = ngroup.id.name + 2;
362 item.ngroup = &ngroup;
363
364 items.append(item);
365 }
366 }
367 }
368 else if (arg->node_type->declare != nullptr) {
369 using namespace blender::nodes;
370
371 r_node_decl.emplace(NodeDeclaration());
372 nodes::build_node_declaration(*arg->node_type, *r_node_decl, nullptr, nullptr);
373 Span<SocketDeclaration *> socket_decls = (in_out == SOCK_IN) ? r_node_decl->inputs :
374 r_node_decl->outputs;
375 int index = 0;
376 for (const SocketDeclaration *socket_decl_ptr : socket_decls) {
377 const SocketDeclaration &socket_decl = *socket_decl_ptr;
378 NodeLinkItem item;
380 item.socket_index = index++;
381 item.socket_type = socket_decl.socket_type;
382 item.socket_name = socket_decl.name.c_str();
383 item.node_name = arg->node_type->ui_name.c_str();
384 items.append(item);
385 }
386 }
387 else {
388 bke::bNodeSocketTemplate *socket_templates = (in_out == SOCK_IN ? arg->node_type->inputs :
389 arg->node_type->outputs);
391 int i;
392
393 i = 0;
394 for (stemp = socket_templates; stemp && stemp->type != -1; stemp++, i++) {
395 NodeLinkItem item;
397 item.socket_index = i;
398 item.socket_type = stemp->type;
399 item.socket_name = stemp->name;
400 item.node_name = arg->node_type->ui_name.c_str();
401 items.append(item);
402 }
403 }
404
405 return items;
406}
407
408static void ui_node_link(bContext *C, void *arg_p, void *event_p)
409{
410 NodeLinkArg *arg = static_cast<NodeLinkArg *>(arg_p);
411 Main *bmain = arg->bmain;
412 bNode *node_to = arg->node;
413 bNodeSocket *sock_to = arg->sock;
414 bNodeTree *ntree = arg->ntree;
415 int event = POINTER_AS_INT(event_p);
416
417 if (event == UI_NODE_LINK_DISCONNECT) {
418 node_socket_disconnect(bmain, ntree, node_to, sock_to);
419 }
420 else if (event == UI_NODE_LINK_REMOVE) {
421 node_socket_remove(bmain, ntree, node_to, sock_to);
422 }
423 else {
424 node_socket_add_replace(C, ntree, node_to, sock_to, arg->node_type->type_legacy, &arg->item);
425 }
426
427 ED_undo_push(C, "Node input modify");
428}
429
430static void ui_node_sock_name(const bNodeTree *ntree,
431 bNodeSocket *sock,
432 char name[UI_MAX_NAME_STR])
433{
434 if (sock->link && sock->link->fromnode) {
435 bNode *node = sock->link->fromnode;
436 const std::string node_name = bke::node_label(*ntree, *node);
437
438 if (node->inputs.is_empty() && node->outputs.first() != node->outputs.last()) {
441 "%s | %s",
442 IFACE_(node_name.c_str()),
443 IFACE_(sock->link->fromsock->name));
444 }
445 else {
446 BLI_strncpy_utf8(name, IFACE_(node_name.c_str()), UI_MAX_NAME_STR);
447 }
448 }
449 else if (sock->type == SOCK_SHADER) {
451 }
452 else {
453 BLI_strncpy_utf8(name, IFACE_("Default"), UI_MAX_NAME_STR);
454 }
455}
456
457static int ui_compatible_sockets(int typeA, int typeB)
458{
459 return (typeA == typeB);
460}
461
462static int ui_node_item_name_compare(const void *a, const void *b)
463{
464 const bke::bNodeType *type_a = *static_cast<const bke::bNodeType **>(const_cast<void *>(a));
465 const bke::bNodeType *type_b = *static_cast<const bke::bNodeType **>(const_cast<void *>(b));
466 return BLI_strcasecmp_natural(type_a->ui_name.c_str(), type_b->ui_name.c_str());
467}
468
469static bool ui_node_item_special_poll(const bNodeTree * /*ntree*/, const bke::bNodeType *ntype)
470{
471 if (ntype->is_type("ShaderNodeUVAlongStroke"_ustr)) {
472 /* TODO(sergey): Currently we don't have Freestyle nodes edited from
473 * the buttons context, so can ignore its nodes completely.
474 *
475 * However, we might want to do some extra checks here later.
476 */
477 return false;
478 }
479 return true;
480}
481
482static void ui_node_menu_column(NodeLinkArg *arg, int nclass, const char *cname)
483{
484 bNodeTree *ntree = arg->ntree;
485 bNodeSocket *sock = arg->sock;
486 ui::Layout *layout = arg->layout;
487 ui::Layout *column = nullptr;
488 ui::Block *block = layout->block();
489 NodeLinkArg *argN;
490 int first = 1;
491
492 /* generate array of node types sorted by UI name */
493 Vector<bke::bNodeType *> sorted_ntypes;
494
495 for (bke::bNodeType *ntype : bke::node_types_get()) {
496 const char *disabled_hint;
497 if (!(ntype->poll && ntype->poll(ntype, ntree, &disabled_hint))) {
498 continue;
499 }
500
501 if (ntype->nclass != nclass) {
502 continue;
503 }
504
505 if (!ui_node_item_special_poll(ntree, ntype)) {
506 continue;
507 }
508
509 sorted_ntypes.append(ntype);
510 }
511
512 qsort(sorted_ntypes.data(),
513 sorted_ntypes.size(),
514 sizeof(bke::bNodeType *),
516
517 /* generate UI */
518 for (int j = 0; j < sorted_ntypes.size(); j++) {
519 bke::bNodeType *ntype = sorted_ntypes[j];
520 char name[UI_MAX_NAME_STR];
521 const char *cur_node_name = nullptr;
522 int num = 0;
523 int icon = ICON_NONE;
524
525 arg->node_type = ntype;
526
527 std::optional<nodes::NodeDeclaration> node_decl;
528 Vector<NodeLinkItem> items = ui_node_link_items(arg, SOCK_OUT, node_decl);
529
530 for (const NodeLinkItem &item : items) {
531 if (ui_compatible_sockets(item.socket_type, sock->type)) {
532 num++;
533 }
534 }
535
536 for (const NodeLinkItem &item : items) {
537 if (!ui_compatible_sockets(item.socket_type, sock->type)) {
538 continue;
539 }
540
541 if (first) {
542 column = &layout->column(false);
543 ui::block_layout_set_current(block, column);
544
545 column->label(IFACE_(cname), ICON_NODE);
546 first = 0;
547 }
548
549 if (num > 1) {
550 if (!cur_node_name || !STREQ(cur_node_name, item.node_name)) {
551 cur_node_name = item.node_name;
552 /* XXX Do not use ui::Layout::label here,
553 * it would add an empty icon as we are in a menu! */
554 uiDefBut(block,
556 IFACE_(cur_node_name),
557 0,
558 0,
559 UI_UNIT_X * 4,
560 UI_UNIT_Y,
561 nullptr,
562 0.0,
563 0.0,
564 "");
565 }
566
567 STRNCPY_UTF8(name, IFACE_(item.socket_name));
568 icon = ICON_BLANK1;
569 }
570 else {
571 STRNCPY_UTF8(name, IFACE_(item.node_name));
572 icon = ICON_NONE;
573 }
574
575 ui::Button *but = uiDefIconTextBut(block,
577 icon,
578 name,
579 0,
580 0,
581 UI_UNIT_X * 4,
582 UI_UNIT_Y,
583 nullptr,
584 TIP_("Add node to input"));
585
586 argN = MEM_dupalloc(arg);
587 argN->item = item;
588 button_funcN_set(but, ui_node_link, argN, nullptr);
589 }
590 }
591}
592
593static void node_menu_column_foreach_cb(void *calldata, int nclass, const StringRefNull name)
594{
595 NodeLinkArg *arg = static_cast<NodeLinkArg *>(calldata);
596
597 if (!ELEM(nclass, NODE_CLASS_GROUP, NODE_CLASS_LAYOUT)) {
598 ui_node_menu_column(arg, nclass, name.c_str());
599 }
600}
601
602static void ui_template_node_link_menu(bContext *C, ui::Layout *layout, void *but_p)
603{
604 Main *bmain = CTX_data_main(C);
605 Scene *scene = CTX_data_scene(C);
606 ui::Block *block = layout->block();
607 ui::Button *but = static_cast<ui::Button *>(but_p);
608 ui::Layout *split, *column;
609 NodeLinkArg *arg = static_cast<NodeLinkArg *>(ui::button_func_argN_get(but));
610 bNodeSocket *sock = arg->sock;
611 bke::bNodeTreeType *ntreetype = arg->ntree->typeinfo;
612
613 ui::block_layout_set_current(block, layout);
614 split = &layout->split(0.0f, false);
615
616 arg->bmain = bmain;
617 arg->scene = scene;
618 arg->layout = split;
619
620 if (ntreetype && ntreetype->foreach_nodeclass) {
622 }
623
624 column = &split->column(false);
625 ui::block_layout_set_current(block, column);
626
627 if (sock->link) {
628 column->label(IFACE_("Link"), ICON_NONE);
629
630 ui::Button *but = uiDefBut(block,
633 0,
634 0,
635 UI_UNIT_X * 4,
636 UI_UNIT_Y,
637 nullptr,
638 0.0,
639 0.0,
640 TIP_("Remove nodes connected to the input"));
641 button_funcN_set(but, ui_node_link, MEM_dupalloc(arg), POINTER_FROM_INT(UI_NODE_LINK_REMOVE));
642
643 but = uiDefBut(block,
645 IFACE_("Disconnect"),
646 0,
647 0,
648 UI_UNIT_X * 4,
649 UI_UNIT_Y,
650 nullptr,
651 0.0,
652 0.0,
653 TIP_("Disconnect nodes connected to the input"));
654 button_funcN_set(
655 but, ui_node_link, MEM_dupalloc(arg), POINTER_FROM_INT(UI_NODE_LINK_DISCONNECT));
656 }
657
659}
660
661} // namespace ed::space_node
662
664 ui::Layout *layout, bContext *C, bNodeTree *ntree, bNode *node, bNodeSocket *input)
665{
666 using namespace blender::ed::space_node;
667
668 layout->active_set(!input->is_inactive());
669
670 ui::Block *block = layout->block();
671 NodeLinkArg *arg;
672 ui::Button *but;
673 float socket_col[4];
674
675 arg = MEM_new_zeroed<NodeLinkArg>("NodeLinkArg");
676 arg->ntree = ntree;
677 arg->node = node;
678 arg->sock = input;
680
681 PointerRNA node_ptr = RNA_pointer_create_discrete(&ntree->id, RNA_Node, node);
682 node_socket_color_get(*C, *ntree, node_ptr, *input, socket_col);
683
684 ui::block_layout_set_current(block, layout);
685
686 if ((input->link && !input->link->fromsock->owner_node().is_muted()) ||
687 (input->type == SOCK_SHADER || (input->flag & SOCK_HIDE_VALUE)))
688 {
689 char name[UI_MAX_NAME_STR];
690 ui_node_sock_name(ntree, input, name);
691 but = uiDefMenuBut(
692 block, ui_template_node_link_menu, nullptr, name, 0, 0, UI_UNIT_X * 4, UI_UNIT_Y, "");
693 }
694 else {
695 but = uiDefIconMenuBut(
696 block, ui_template_node_link_menu, nullptr, ICON_NONE, 0, 0, UI_UNIT_X, UI_UNIT_Y, "");
697 }
698
699 button_type_set_menu_from_pulldown(but);
700 button_node_link_set(but, input, socket_col);
701 button_drawflag_enable(but, ui::BUT_ICON_LEFT);
702
704
705 if (input->link && input->link->fromnode) {
706 if (input->link->fromnode->flag & NODE_ACTIVE_TEXTURE) {
708 }
709 }
710
711 if (!ID_IS_EDITABLE(ntree)) {
712 button_disable(but, "Cannot edit linked node tree");
713 }
714}
715
716namespace ed::space_node {
717
718/**************************** Node Tree Layout *******************************/
719
720static void ui_node_draw_input(ui::Layout &layout,
721 bContext &C,
722 bNodeTree &ntree,
723 bNode &node,
724 bNodeSocket &input,
725 int depth,
726 const char *panel_label);
727
729 bContext &C,
730 bNodeTree &ntree,
731 bNode &node,
732 const nodes::PanelDeclaration &panel_decl,
733 const int depth)
734{
735 const nodes::SocketDeclaration *panel_toggle_decl = panel_decl.panel_input_decl();
736 const std::string panel_id = fmt::format(
737 "{}_{}_{}", ntree.id.name, node.identifier, panel_decl.identifier);
738 const StringRef panel_translation_context = panel_decl.translation_context.has_value() ?
739 *panel_decl.translation_context :
740 "";
741 ui::PanelLayout panel_layout = layout.panel(&C, panel_id.c_str(), panel_decl.default_collapsed);
742 if (panel_toggle_decl) {
743 panel_layout.header->use_property_split_set(false);
744 panel_layout.header->use_property_decorate_set(false);
746 &ntree.id, RNA_NodeSocket, &node.socket_by_decl(*panel_toggle_decl));
747 panel_layout.header->prop(&toggle_ptr,
748 "default_value",
750 CTX_IFACE_(panel_translation_context, panel_decl.name.ref()),
751 ICON_NONE);
752 }
753 else {
754 panel_layout.header->label(CTX_IFACE_(panel_translation_context, panel_decl.name.ref()),
755 ICON_NONE);
756 }
757
758 if (!panel_layout.body) {
759 return;
760 }
761 for (const nodes::ItemDeclaration *item_decl : panel_decl.items) {
762 if (item_decl == panel_toggle_decl) {
763 continue;
764 }
765 if (const auto *socket_decl = dynamic_cast<const nodes::SocketDeclaration *>(item_decl)) {
766 bNodeSocket &socket = node.socket_by_decl(*socket_decl);
767 if (socket_decl->custom_draw_fn) {
769 C,
770 *panel_layout.body,
771 ntree,
772 node,
773 socket,
774 RNA_pointer_create_discrete(&ntree.id, RNA_Node, &node),
775 RNA_pointer_create_discrete(&ntree.id, RNA_NodeSocket, &socket)};
776 (*socket_decl->custom_draw_fn)(params);
777 }
778 else if (socket_decl->in_out == SOCK_IN) {
780 *panel_layout.body, C, ntree, node, socket, depth, panel_decl.name.c_str());
781 }
782 }
783 else if (const auto *sub_panel_decl = dynamic_cast<const nodes::PanelDeclaration *>(item_decl))
784 {
785 ui_node_draw_recursive(*panel_layout.body, C, ntree, node, *sub_panel_decl, depth + 1);
786 }
787 else if (const auto *layout_decl = dynamic_cast<const nodes::LayoutDeclaration *>(item_decl)) {
788 PointerRNA nodeptr = RNA_pointer_create_discrete(&ntree.id, RNA_Node, &node);
789 ui::Layout &layout = panel_layout.body->column(false);
790 layout_decl->draw(layout, &C, &nodeptr);
791 }
792 }
793}
794
796 ui::Layout &layout, bContext &C, bNodeTree &ntree, bNode &node, int depth)
797{
798 PointerRNA nodeptr = RNA_pointer_create_discrete(&ntree.id, RNA_Node, &node);
799
800 /* Draw top-level node buttons. */
801 if (node.typeinfo->draw_buttons) {
802 if (node.type_legacy != NODE_GROUP) {
803 layout.use_property_split_set(true);
804 node.typeinfo->draw_buttons(layout, &C, &nodeptr);
805 }
806 }
807
808 if (node.declaration()) {
809 const nodes::NodeDeclaration &node_decl = *node.declaration();
810 for (const nodes::ItemDeclaration *item_decl : node_decl.root_items) {
811 if (const auto *panel_decl = dynamic_cast<const nodes::PanelDeclaration *>(item_decl)) {
812 ui_node_draw_recursive(layout, C, ntree, node, *panel_decl, depth + 1);
813 }
814 else if (const auto *socket_decl = dynamic_cast<const nodes::SocketDeclaration *>(item_decl))
815 {
816 bNodeSocket &socket = node.socket_by_decl(*socket_decl);
817 if (socket_decl->custom_draw_fn) {
819 C,
820 layout,
821 ntree,
822 node,
823 socket,
824 RNA_pointer_create_discrete(&ntree.id, RNA_Node, &node),
825 RNA_pointer_create_discrete(&ntree.id, RNA_NodeSocket, &socket)};
826 (*socket_decl->custom_draw_fn)(params);
827 }
828 else if (socket_decl->in_out == SOCK_IN) {
829 ui_node_draw_input(layout, C, ntree, node, socket, depth + 1, nullptr);
830 }
831 }
832 else if (const auto *layout_decl = dynamic_cast<const nodes::LayoutDeclaration *>(item_decl))
833 {
834 if (!layout_decl->is_default) {
835 PointerRNA nodeptr = RNA_pointer_create_discrete(&ntree.id, RNA_Node, &node);
836 ui::Layout &column = layout.column(false);
837 layout_decl->draw(column, &C, &nodeptr);
838 }
839 }
840 }
841 }
842 else {
843 /* Draw socket values using the flat inputs list. */
844 for (bNodeSocket &input : node.inputs) {
845 ui_node_draw_input(layout, C, ntree, node, input, depth + 1, nullptr);
846 }
847 }
848}
849
850static void ui_node_draw_input(ui::Layout &layout,
851 bContext &C,
852 bNodeTree &ntree,
853 bNode &node,
854 bNodeSocket &input,
855 int depth,
856 const char *panel_label)
857{
858 ui::Block *block = layout.block();
859 ui::Layout *row = nullptr;
860 bool dependency_loop;
861
862 if (input.flag & SOCK_UNAVAIL) {
863 return;
864 }
865
866 /* to avoid eternal loops on cyclic dependencies */
867 node.flag |= NODE_TEST;
868 bNode *lnode = (input.link) ? input.link->fromnode : nullptr;
869
870 dependency_loop = (lnode && (lnode->flag & NODE_TEST));
871 if (dependency_loop) {
872 lnode = nullptr;
873 }
874
875 /* socket RNA pointer */
876 PointerRNA inputptr = RNA_pointer_create_discrete(&ntree.id, RNA_NodeSocket, &input);
877 PointerRNA nodeptr = RNA_pointer_create_discrete(&ntree.id, RNA_Node, &node);
878
879 row = &layout.row(true);
880
881 ui::PropertySplitWrapper split_wrapper = uiItemPropertySplitWrapperCreate(row);
882 /* Decorations are added manually here. */
883 row->use_property_decorate_set(false);
884 /* Empty decorator item for alignment. */
885 bool add_dummy_decorator = false;
886
887 {
888 ui::Layout *sub = &split_wrapper.label_column->row(true);
889
890 if (depth > 0) {
891 block_emboss_set(block, ui::EmbossType::None);
892
893 if (lnode) {
894 /* Input linked to a node, we can expand/collapse if
895 * - linked node has inputs
896 * - linked node has dedicated button drawing
897 * - linked node has dedicated socket drawing */
898 bool can_expand = lnode->inputs.first();
899 if (lnode->type_legacy != NODE_GROUP) {
900 if (lnode->typeinfo->draw_buttons) {
901 can_expand = true;
902 }
903 else if (lnode->declaration()) {
904 const nodes::NodeDeclaration &lnode_decl = *lnode->declaration();
905 for (const nodes::ItemDeclaration *item_decl : lnode_decl.root_items) {
906 if (const auto *socket_decl = dynamic_cast<const nodes::SocketDeclaration *>(
907 item_decl))
908 {
909 if (socket_decl->custom_draw_fn) {
910 can_expand = true;
911 }
912 }
913 }
914 }
915 }
916 if (can_expand && !lnode->is_muted()) {
917 int icon = (input.flag & SOCK_COLLAPSED) ? ICON_RIGHTARROW : ICON_DOWNARROW_HLT;
918 sub->prop(&inputptr, "show_expanded", ui::ITEM_R_ICON_ONLY, "", icon);
919 }
920 }
921
922 block_emboss_set(block, ui::EmbossType::Emboss);
923 }
924
925 sub = &sub->row(true);
927 sub->label(node_socket_get_label(&input, panel_label), ICON_NONE);
928 }
929
930 if (dependency_loop) {
931 row->label(RPT_("Dependency Loop"), ICON_STATUS_WARNING_FILLED);
932 add_dummy_decorator = true;
933 }
934 else if (lnode && !lnode->is_muted()) {
935 /* input linked to a node */
936 uiTemplateNodeLink(row, &C, &ntree, &node, &input);
937 add_dummy_decorator = true;
938
939 if (depth == 0 || !(input.flag & SOCK_COLLAPSED)) {
940 if (depth == 0) {
941 layout.separator();
942 }
943
944 ui_node_draw_node(layout, C, ntree, *lnode, depth);
945 }
946 }
947 else {
948 ui::Layout *sub = &row->row(true);
949
950 uiTemplateNodeLink(sub, &C, &ntree, &node, &input);
951
952 if (input.flag & SOCK_HIDE_VALUE) {
953 add_dummy_decorator = true;
954 }
955 /* input not linked, show value */
956 else {
957 switch (input.type) {
958 case SOCK_VECTOR:
959 sub->separator();
960 sub = &sub->column(true);
962 case SOCK_FLOAT:
963 case SOCK_INT:
964 case SOCK_ROTATION:
965 case SOCK_BOOLEAN:
966 case SOCK_RGBA:
967 case SOCK_MENU:
968 sub->prop(&inputptr, "default_value", UI_ITEM_NONE, "", ICON_NONE);
969 if (split_wrapper.decorate_column) {
970 split_wrapper.decorate_column->decorator(&inputptr, "default_value", RNA_NO_INDEX);
971 }
972 break;
973 case SOCK_STRING: {
974 const bNodeTree *node_tree = id_cast<const bNodeTree *>(nodeptr.owner_id);
975 SpaceNode *snode = CTX_wm_space_node(&C);
976 if (node_tree->type == NTREE_GEOMETRY && snode != nullptr) {
977 /* Only add the attribute search in the node editor, in other places there is not
978 * enough context. */
980 }
981 else {
982 sub->prop(&inputptr, "default_value", UI_ITEM_NONE, "", ICON_NONE);
983 }
984 if (split_wrapper.decorate_column) {
985 split_wrapper.decorate_column->decorator(&inputptr, "default_value", RNA_NO_INDEX);
986 }
987 break;
988 }
989 case SOCK_CUSTOM:
990 input.typeinfo->draw(&C, sub, &inputptr, &nodeptr, input.name);
991 break;
992 default:
993 add_dummy_decorator = true;
994 }
995 }
996 }
997
998 if (add_dummy_decorator && split_wrapper.decorate_column) {
999 split_wrapper.decorate_column->decorator(nullptr, std::nullopt, 0);
1000 }
1001
1002 node_socket_add_tooltip(ntree, input, *row);
1003
1004 /* clear */
1005 node.flag &= ~NODE_TEST;
1006}
1007
1008} // namespace ed::space_node
1009
1011 ui::Layout *layout, bContext *C, bNodeTree *ntree, bNode *node, bNodeSocket *input)
1012{
1013 using namespace blender::ed::space_node;
1014
1015 if (!ntree) {
1016 return;
1017 }
1018 ntree->ensure_topology_cache();
1019
1020 /* clear for cycle check */
1021 for (bNode *tnode : ntree->all_nodes()) {
1022 tnode->flag &= ~NODE_TEST;
1023 }
1024
1025 if (input) {
1026 ui_node_draw_input(*layout, *C, *ntree, *node, *input, 0, nullptr);
1027 }
1028 else {
1029 ui_node_draw_node(*layout, *C, *ntree, *node, 0);
1030 }
1031}
1032
1033} // namespace blender
#define NODE_GROUP
Definition BKE_node.hh:865
#define NODE_CLASS_LAYOUT
Definition BKE_node.hh:494
#define NODE_CLASS_GROUP
Definition BKE_node.hh:483
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define ATTR_FALLTHROUGH
#define STRNCPY_UTF8(dst, src)
#define RPT_(msgid)
#define TIP_(msgid)
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define IFACE_(msgid)
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UNIT_X
#define UI_ITEM_NONE
#define RNA_NO_INDEX
#define UI_MAX_NAME_STR
#define POINTER_FROM_INT(i)
#define POINTER_AS_INT(i)
#define ELEM(...)
#define STREQ(a, b)
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr const char * c_str() const
const char * c_str() const
StringRefNull ref() const
int64_t size() const
void append(const T &value)
Vector< ItemDeclaration * > root_items
std::optional< std::string > translation_context
Vector< ItemDeclaration * > items
const SocketDeclaration * panel_input_decl() const
std::string node_label(const bNodeTree &ntree, const bNode &node)
Definition node.cc:5732
void node_remove_node(Main *bmain, bNodeTree &ntree, bNode &node, bool do_id_user, bool remove_animation=true)
Definition node.cc:5155
bool node_group_poll(const bNodeTree *nodetree, const bNodeTree *grouptree, const char **r_disabled_hint)
void node_remove_link(bNodeTree *ntree, bNodeLink &link)
Definition node.cc:4676
Span< bNodeType * > node_types_get()
Definition node.cc:2951
int node_count_socket_links(const bNodeTree &ntree, const bNodeSocket &sock)
Definition node.cc:5434
void node_position_relative(bNode &from_node, const bNode &to_node, const bNodeSocket *from_sock, const bNodeSocket &to_sock)
Definition node.cc:4900
bNode * node_add_static_node(const bContext *C, bNodeTree &ntree, int type)
Definition node.cc:4283
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4638
void socket_value_copy_content(eNodeSocketDatatype type, void *dst, const void *src, bool do_id_user)
Definition node.cc:3312
void node_set_active(bNodeTree &ntree, bNode &node)
Definition node.cc:5484
int node_socket_link_limit(const bNodeSocket &sock)
Definition node.cc:5513
static void node_clear_recursive(bNode *node)
static void node_socket_disconnect(Main *bmain, bNodeTree *ntree, bNode *node_to, bNodeSocket *sock_to)
void node_geometry_add_attribute_search_button(const bContext &, const bNode &node, PointerRNA &socket_ptr, ui::Layout &layout, const StringRef placeholder)
static void ui_template_node_link_menu(bContext *C, ui::Layout *layout, void *but_p)
static void ui_node_draw_node(ui::Layout &layout, bContext &C, bNodeTree &ntree, bNode &node, int depth)
static void node_link_item_apply(bNodeTree *ntree, bNode *node, NodeLinkItem *item)
static int ui_node_item_name_compare(const void *a, const void *b)
static void node_tag_recursive(bNode *node)
static Vector< NodeLinkItem > ui_node_link_items(NodeLinkArg *arg, int in_out, std::optional< NodeDeclaration > &r_node_decl)
static void ui_node_sock_name(const bNodeTree *ntree, bNodeSocket *sock, char name[UI_MAX_NAME_STR])
void node_socket_color_get(const bContext &C, const bNodeTree &ntree, PointerRNA &node_ptr, const bNodeSocket &sock, float r_color[4])
void node_socket_add_tooltip(const bNodeTree &ntree, const bNodeSocket &sock, ui::Layout &layout)
static void node_socket_add_replace(const bContext *C, bNodeTree *ntree, bNode *node_to, bNodeSocket *sock_to, int type, NodeLinkItem *item)
static void ui_node_draw_input(ui::Layout &layout, bContext &C, bNodeTree &ntree, bNode &node, bNodeSocket &input, int depth, const char *panel_label)
static void node_remove_linked(Main *bmain, bNodeTree *ntree, bNode *rem_node)
static int ui_compatible_sockets(int typeA, int typeB)
const char * node_socket_get_label(const bNodeSocket *socket, const char *panel_label=nullptr)
Definition node_draw.cc:398
static void ui_node_menu_column(NodeLinkArg *arg, int nclass, const char *cname)
static void node_socket_remove(Main *bmain, bNodeTree *ntree, bNode *node_to, bNodeSocket *sock_to)
static bool node_link_item_compare(bNode *node, NodeLinkItem *item)
static void node_menu_column_foreach_cb(void *calldata, int nclass, const StringRefNull name)
static bool ui_node_item_special_poll(const bNodeTree *, const bke::bNodeType *ntype)
static void ui_node_link(bContext *C, void *arg_p, void *event_p)
static void ui_node_draw_recursive(ui::Layout &layout, bContext &C, bNodeTree &ntree, bNode &node, const nodes::PanelDeclaration &panel_decl, const int depth)
static void node_link_item_init(NodeLinkItem &item)
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition abc_util.cc:68
void build_node_declaration(const bke::bNodeType &typeinfo, NodeDeclaration &r_declaration, const bNodeTree *ntree, const bNode *node)
void button_poin_menu_argN_set(Button *but, void *poin, void *argN, ButtonArgNFree func_argN_free_fn, ButtonArgNCopy func_argN_copy_fn)
void button_flag_enable(Button *but, int64_t flag)
void * button_func_argN_get(const Button *but)
void block_layout_set_current(Block *block, Layout *layout)
BMesh Iterators.
size_t size_t size_t BLI_snprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
@ NODE_ACTIVE_TEXTURE
int char char int int int BLI_strcasecmp_natural(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void id_us_plus(ID *id)
Definition lib_id.cc:433
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void uiTemplateNodeLink(ui::Layout *layout, bContext *C, bNodeTree *ntree, bNode *node, bNodeSocket *input)
const char * BKE_id_name(const ID &id)
void MEM_delete_void(void *vmemh)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
void * MEM_dupalloc_void(const void *vmemh) ATTR_WARN_UNUSED_RESULT
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
void BKE_main_ensure_invariants(Main &bmain, std::optional< Span< ID * > > modified_ids=std::nullopt)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
SpaceNode * CTX_wm_space_node(const bContext *C)
ATTR_WARN_UNUSED_RESULT const size_t num
void uiTemplateNodeView(ui::Layout *layout, bContext *C, bNodeTree *ntree, bNode *node, bNodeSocket *input)
void BKE_ntree_update_tag_node_property(bNodeTree *ntree, bNode *node)
Main * CTX_data_main(const bContext *C)
Scene * CTX_data_scene(const bContext *C)
#define UI_NODE_LINK_DISCONNECT
#define UI_NODE_LINK_REMOVE
char name[258]
Definition DNA_ID.h:433
ListBaseT< bNodeTree > nodetrees
Definition BKE_main.hh:409
bke::bNodeSocketType * typeinfo
eNodeSocketDatatype type
struct bNodeLink * link
bke::bNodeTreeType * typeinfo
eNodeTree_Type type
ListBaseT< bNode > nodes
ListBaseT< bNodeSocket > outputs
ListBaseT< bNodeSocket > inputs
bke::bNodeType * typeinfo
struct bNode * next
Compact definition of a node socket.
Definition BKE_node.hh:102
Defines a socket type.
Definition BKE_node.hh:163
eNodeSocketDatatype type
Definition BKE_node.hh:198
void(*) draw(bContext *C, ui::Layout *layout, PointerRNA *ptr, PointerRNA *node_ptr, StringRef text)
Definition BKE_node.hh:171
void(*) foreach_nodeclass(void *calldata, bNodeClassCallback func)
Definition BKE_node.hh:550
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
bNodeSocketTemplate * inputs
Definition BKE_node.hh:285
void(*) draw_buttons_ex(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:292
bool is_type(UString query_idname) const
Definition BKE_node.hh:1342
bNodeSocketTemplate * outputs
Definition BKE_node.hh:285
NodeDeclareFunction declare
Definition BKE_node.hh:393
void alignment_set(LayoutAlign alignment)
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)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)
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)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
Layout & row(bool align)
#define N_(msgid)