Blender V5.3
node_copy_util.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10
11#include "DNA_node_types.h"
12
13#include "BKE_anim_data.hh"
14#include "BLI_listbase.hh"
16#include "BLI_map.hh"
18#include "BLI_rand.hh"
19#include "BLI_string.hh"
20#include "BLI_vector.hh"
21
22#include "BKE_action.hh"
23#include "BKE_animsys.hh"
24#include "BKE_context.hh"
25#include "BKE_lib_id.hh"
26#include "BKE_library.hh"
27#include "BKE_main.hh"
29#include "BKE_node_runtime.hh"
32
33#include "ANIM_action.hh"
34
36
37#include "ED_node.hh"
38#include "ED_node_preview.hh"
39#include "ED_render.hh"
40#include "ED_screen.hh"
41
42#include "RNA_access.hh"
43#include "RNA_prototypes.hh"
44
45#include "WM_api.hh"
46#include "WM_types.hh"
47
48#include "NOD_common.hh"
50#include "NOD_socket.hh"
51
52#include "node_intern.hh" /* own include */
53
54namespace blender {
55
57{
58 /* Don't use "by_identifier" functions of bNode because they depend on valid topology cache. */
59 ListBaseT<bNodeSocket> sockets = (this->in_out == SOCK_IN) ? other_node.inputs :
60 other_node.outputs;
61 return reinterpret_cast<bNodeSocket *>(BLI_findstring(
62 &sockets, this->socket_identifier.c_str(), offsetof(bNodeSocket, identifier)));
63}
64
66{
67 return const_cast<bNodeSocket *>(
68 this->find_socket_in_node(const_cast<const bNode &>(other_node)));
69}
70
72{
73 /* Don't use "by_identifier" functions of bNode because they depend on valid topology cache. */
74 ListBaseT<bNodeSocket> sockets = (this->in_out == SOCK_IN) ? other_node.inputs :
75 other_node.outputs;
76 return reinterpret_cast<bNodeSocket *>(BLI_findstring(
77 &sockets, this->socket_identifier.c_str(), offsetof(bNodeSocket, identifier)));
78}
79
81{
82 return const_cast<bNodeSocket *>(
83 this->find_socket_in_node(const_cast<const bNode &>(other_node)));
84}
85
86namespace ed::space_node {
87
88std::optional<Bounds<float2>> node_bounds(Span<const bNode *> nodes)
89{
90 std::optional<Bounds<float2>> result = std::nullopt;
91 for (const bNode *node : nodes) {
92 const float2 loc(node->location);
93 const Bounds<float2> box(loc, loc + float2(node->width, -node->height));
95 }
96 return result;
97}
98
99std::optional<Bounds<float2>> node_location_bounds(Span<const bNode *> nodes)
100{
101 std::optional<Bounds<float2>> result = std::nullopt;
102 for (const bNode *node : nodes) {
103 const float2 loc(node->location);
105 }
106 return result;
107}
108
109/* -------------------------------------------------------------------- */
112
113using NodeFilterFn = FunctionRef<bool(const bNode &)>;
114static bool default_link_filter(const bNode & /*node*/)
115{
116 return true;
117}
118
119/* Links between nodes that should be copied.
120 * Contains only links between nodes of this set. */
122 const bNodeTree &tree,
124 NodeFilterFn link_filter = default_link_filter)
125{
126 Vector<const bNodeLink *> internal_links;
127 const Set<const bNode *> nodes_set(nodes);
128 for (const bNodeLink &link : tree.links) {
129 if (!link.is_available() || bke::node_link_is_hidden(link)) {
130 continue;
131 }
132 if (!link_filter(*link.fromnode) || !link_filter(*link.tonode)) {
133 continue;
134 }
135 if (!nodes_set.contains(link.fromnode) || !nodes_set.contains(link.tonode)) {
136 continue;
137 }
138
139 internal_links.append(&link);
140 }
141 return internal_links;
142}
143
145 const bNodeSocket &socket,
146 const bool skip_hidden,
147 NodeFilterFn link_filter = default_link_filter)
148{
150 for (const bNodeLink *link : socket.directly_linked_links()) {
151 if (!link->is_available()) {
152 continue;
153 }
154 if (skip_hidden && bke::node_link_is_hidden(*link)) {
155 continue;
156 }
157 bNode *link_node = socket.is_input() ? link->fromnode : link->tonode;
158 bNodeSocket *link_socket = socket.is_input() ? link->fromsock : link->tosock;
159 if (!link_filter(*link_node)) {
160 continue;
161 }
162 std::optional<int> multi_input_sort_id;
163 if (link_socket->is_multi_input()) {
164 multi_input_sort_id = link->multi_input_sort_id;
165 }
166 result.append(
167 {*link_node, *link_socket, bool(link->flag & NODE_LINK_MUTED), multi_input_sort_id});
168 }
169 return result;
170}
171
173 const bNodeTree &tree,
174 const bNodeTreeInterfaceSocket &io_socket,
175 const bool skip_hidden,
176 NodeFilterFn link_filter = default_link_filter)
177{
179 if (io_socket.flag & NODE_INTERFACE_SOCKET_INPUT) {
180 for (const bNode *group_input_node : tree.group_input_nodes()) {
181 const bNodeSocket *socket = group_input_node->output_by_identifier(
182 UString(io_socket.identifier));
183 BLI_assert(socket);
184 for (const bNodeLink *link : socket->directly_linked_links()) {
185 if (!link->is_available()) {
186 continue;
187 }
188 if (skip_hidden && bke::node_link_is_hidden(*link)) {
189 continue;
190 }
191 if (!link_filter(*link->tonode)) {
192 continue;
193 }
194 /* Don't map links to inactive group outputs. */
195 if (link->tonode->is_group_output() && (link->tonode->flag & NODE_DO_OUTPUT) == 0) {
196 continue;
197 }
198 std::optional<int> multi_input_sort_id;
199 if (link->tosock->is_multi_input()) {
200 multi_input_sort_id = link->multi_input_sort_id;
201 }
202 result.append({*link->tonode,
203 *link->tosock,
204 bool(link->flag & NODE_LINK_MUTED),
205 multi_input_sort_id});
206 }
207 }
208 }
209 if (io_socket.flag & NODE_INTERFACE_SOCKET_OUTPUT) {
210 if (const bNode *group_output_node = tree.group_output_node()) {
211 const bNodeSocket *socket = group_output_node->input_by_identifier(
212 UString(io_socket.identifier));
213 BLI_assert(socket);
214 for (const bNodeLink *link : socket->directly_linked_links()) {
215 if (!link->is_available()) {
216 continue;
217 }
218 if (skip_hidden && bke::node_link_is_hidden(*link)) {
219 continue;
220 }
221 if (!link_filter(*link->fromnode)) {
222 continue;
223 }
224 result.append({*link->fromnode, *link->fromsock, bool(link->flag & NODE_LINK_MUTED)});
225 }
226 }
227 }
228 return result;
229}
230
238 const bNodeSocket &socket)
239{
240 if (node_tree.has_available_link_cycle()) {
241 return socket;
242 }
243 const bNode &node = socket.owner_node();
244 if (!node.is_reroute()) {
245 return socket;
246 }
247 const bNodeSocket &other_socket = socket.in_out == SOCK_IN ? node.output_socket(0) :
248 node.input_socket(0);
249 if (!other_socket.is_logically_linked()) {
250 return socket;
251 }
252 return *other_socket.logically_linked_sockets().first();
253}
254
256 const bNodeSocket &socket,
257 const eNodeSocketInOut in_out,
258 bNodeTree &tree_for_interface,
260{
261 const bNode &node = socket.owner_node();
262 /* The output sockets of group nodes usually have consciously given names so they have
263 * precedence over socket names the link points to. */
264 const bool prefer_node_for_interface_name = node.is_group() || node.is_group_input() ||
265 node.is_group_output();
266
267 /* The "example socket" has to have the same `in_out` status as the new interface socket. */
268 const bNodeSocket &socket_for_io = find_socket_to_use_for_interface(original_tree, socket);
269 const bNode &node_for_io = socket_for_io.owner_node();
270 const bNodeSocket &socket_for_name = prefer_node_for_interface_name ? socket : socket_for_io;
272 tree_for_interface, node_for_io, socket_for_io, socket_for_name.name, in_out);
273 if (io_socket) {
274 tree_for_interface.tree_interface.move_item_to_parent(io_socket->item, parent, INT32_MAX);
275 }
276 return io_socket;
277}
278
279static std::string node_basepath(const bNodeTree &tree, const bNode &node)
280{
282 &const_cast<bNodeTree &>(tree).id, RNA_Node, &const_cast<bNode &>(node));
283 return *RNA_path_from_ID_to_struct(&ptr);
284}
285
286/* Maps old to new identifiers for simulation input node pairing. */
287static void remap_pairing(bNodeTree &dst_tree,
289 const Map<int32_t, int32_t> &identifier_map)
290{
291 for (bNode *dst_node : nodes) {
292 if (bke::all_zone_input_node_types().contains(dst_node->type_legacy)) {
293 const bke::bNodeZoneType &zone_type = *bke::zone_type_by_node_type(dst_node->type_legacy);
294 int &output_node_id = zone_type.get_corresponding_output_id(*dst_node);
295 if (output_node_id == 0) {
296 continue;
297 }
298 output_node_id = identifier_map.lookup_default(output_node_id, 0);
299 if (output_node_id == 0) {
301 }
302 }
303 }
304}
305
306/* Utility for sequentially adding interface sockets and panels. */
308 private:
309 using InterfaceSocketData = NodeTreeInterfaceMapping::InterfaceSocketData;
310 using InterfacePanelData = NodeTreeInterfaceMapping::InterfacePanelData;
311
313 NodeTreeInterfaceMapping &io_mapping_;
314 bNodeTree &dst_tree_;
315 Set<const bNode *> src_nodes_set_;
316 /* Multiple internal or external sockets may be mapped to the same interface item.
317 * This map tracks unique interface items based on identifying sockets. */
319
320 public:
322 NodeTreeInterfaceMapping &io_mapping,
323 bNodeTree &dst_tree,
324 const Span<const bNode *> src_nodes);
325
326 void expose_socket(const bNodeSocket &src_socket, bNodeTreeInterfacePanel *parent);
327 void expose_socket(const bNode &src_node,
328 const nodes::SocketDeclaration &socket_decl,
331 const nodes::PanelDeclaration &panel_decl,
333};
334
336 NodeTreeInterfaceMapping &io_mapping,
337 bNodeTree &dst_tree,
338 const Span<const bNode *> src_nodes)
339 : params_(std::move(params)),
340 io_mapping_(io_mapping),
341 dst_tree_(dst_tree),
342 src_nodes_set_(src_nodes)
343{
344}
345
348{
349 const bNodeTree &src_tree = src_socket.owner_tree();
350 if (!src_socket.is_available()) {
351 return;
352 }
353 if (params_.skip_hidden && !src_socket.is_visible()) {
354 return;
355 }
356 const eNodeSocketInOut in_out = eNodeSocketInOut(src_socket.in_out);
357
358 auto try_add_socket_data = [&](const bNodeSocket &key_socket) -> InterfaceSocketData * {
359 InterfaceSocketData *data = io_mapping_.socket_data.lookup_ptr(
360 data_by_socket_.lookup_default(&key_socket, nullptr));
361 if (data) {
362 return data;
363 }
364
366 src_tree, key_socket, in_out, dst_tree_, parent);
367 if (io_socket) {
368 data = &io_mapping_.socket_data.lookup_or_add(io_socket, {});
369 data_by_socket_.add_new(&key_socket, io_socket);
370
371 data->hidden = key_socket.is_user_hidden();
372 data->collapsed = key_socket.flag & SOCK_COLLAPSED;
373 return data;
374 }
375 return nullptr;
376 };
377
378 Vector<MutableNodeAndSocket> external_links;
379 if (params_.add_external_links) {
380 external_links = get_socket_links(src_socket, params_.skip_hidden, [&](const bNode &node) {
381 return !src_nodes_set_.contains(&node);
382 });
383 }
384
385 if (external_links.is_empty()) {
386 if (!params_.skip_unconnected) {
387 if (InterfaceSocketData *data = try_add_socket_data(src_socket)) {
388 data->internal_sockets.add({src_socket.owner_node(), src_socket, false});
389 }
390 }
391
392 return;
393 }
394
395 const bool use_external_socket_key = src_socket.is_input() ? !params_.use_unique_input :
396 params_.use_unique_output;
397 if (use_external_socket_key) {
398 /* Create a unique interface socket for each external link. */
399 for (const MutableNodeAndSocket &external_socket : external_links) {
400 if (const bNodeSocket *socket = external_socket.find_socket()) {
401 if (InterfaceSocketData *data = try_add_socket_data(*socket)) {
402 data->internal_sockets.add({src_socket.owner_node(), src_socket, false});
403 data->external_sockets.add(external_socket);
404 }
405 }
406 }
407 }
408 else {
409 /* Create interface based on the internal socket. */
410 if (InterfaceSocketData *data = try_add_socket_data(src_socket)) {
411 data->internal_sockets.add({src_socket.owner_node(), src_socket, false});
412 data->external_sockets.add_multiple(external_links);
413 }
414 }
415}
416
418 const nodes::SocketDeclaration &socket_decl,
420{
421 const bNodeSocket &socket = src_node.socket_by_decl(socket_decl);
422 this->expose_socket(socket, parent);
423}
424
426 const bNode &src_node,
427 const nodes::PanelDeclaration &panel_decl,
429{
431 if (panel_decl.default_collapsed) {
433 }
434 bNodeTreeInterfacePanel *io_panel = dst_tree_.tree_interface.add_panel(
435 panel_decl.name.ref(), panel_decl.description, flag, parent);
436 InterfacePanelData &data = io_mapping_.panel_data.lookup_or_add(io_panel, {});
437
438 const Span<bNodePanelState> panel_states = src_node.panel_states();
439 for (const bNodePanelState panel_state : panel_states) {
440 if (panel_state.identifier == panel_decl.identifier) {
441 data.collapsed = panel_state.is_collapsed();
442 break;
443 }
444 }
445
446 return io_panel;
447}
448
450 const bNodeTree &src_tree,
451 const Span<bNode *> src_nodes,
452 bNodeTree &dst_tree)
453{
455 NodeSetInterfaceBuilder builder(params, result, dst_tree, src_nodes);
456
457 src_tree.ensure_topology_cache();
458
459 const Set<const bNode *> nodes_set(src_nodes);
460 for (const bNode *node : src_nodes) {
461 for (const bNodeSocket *socket : node->input_sockets()) {
462 builder.expose_socket(*socket, nullptr);
463 }
464 for (const bNodeSocket *socket : node->output_sockets()) {
465 builder.expose_socket(*socket, nullptr);
466 }
467 }
468 return result;
469}
470
472 const bNode &src_node,
473 const nodes::ItemDeclaration &item_decl,
475{
476 if (const nodes::SocketDeclaration *socket_decl = dynamic_cast<const nodes::SocketDeclaration *>(
477 &item_decl))
478 {
479 builder.expose_socket(src_node, *socket_decl, parent);
480 }
481 else if (const nodes::PanelDeclaration *panel_decl =
482 dynamic_cast<const nodes::PanelDeclaration *>(&item_decl))
483 {
484 bNodeTreeInterfacePanel *io_panel = builder.expose_panel(src_node, *panel_decl, parent);
485 if (io_panel) {
486 for (const nodes::ItemDeclaration *child_item_decl : panel_decl->items) {
487 expose_declaration_item_recursive(builder, src_node, *child_item_decl, io_panel);
488 }
489 }
490 }
491}
492
493static void make_panel_visible(bNode &node, const nodes::PanelDeclaration &panel_decl)
494{
495 for (bNodePanelState &panel_state : node.panel_states()) {
496 if (panel_state.identifier == panel_decl.identifier) {
497 panel_state.flag &= ~NODE_PANEL_COLLAPSED;
498 return;
499 }
500 }
502}
503
505 StringRef socket_identifier,
506 const Span<nodes::ItemDeclaration *> item_decls)
507{
508 for (const nodes::ItemDeclaration *item_decl : item_decls) {
509 if (const auto *socket_decl = dynamic_cast<const nodes::SocketDeclaration *>(item_decl)) {
510 if (socket_identifier == socket_decl->identifier) {
511 return true;
512 }
513 }
514 if (const auto *child_panel_decl = dynamic_cast<const nodes::PanelDeclaration *>(item_decl)) {
515 if (make_parent_panels_visible_recursive(node, socket_identifier, child_panel_decl->items)) {
516 make_panel_visible(node, *child_panel_decl);
517 return true;
518 }
519 }
520 }
521 return false;
522}
523
525{
526 socket.flag &= ~SOCK_HIDDEN;
527 if (const nodes::NodeDeclaration *node_decl = node.declaration()) {
528 make_parent_panels_visible_recursive(node, socket.identifier, node_decl->root_items);
529 }
530}
531
533
540{
542 for (bNodeLink &link : tree.links) {
543 result.add(link.tosock, &link);
544 }
545 return result;
546}
547
548/* Add a link to a node tree. If one of the sockets is visible the other will be visible too. */
550 const SocketLinkMap &links_by_to_socket,
551 bNode &from_node,
552 bNodeSocket &from_socket,
553 bNode &to_node,
554 bNodeSocket &to_socket,
555 const bool muted,
556 std::optional<int> multi_input_sort_id)
557{
558 BLI_assert(from_socket.is_available());
559 BLI_assert(to_socket.is_available());
560 if (!from_socket.is_user_hidden() || !to_socket.is_user_hidden()) {
561 if (from_socket.is_user_hidden() || from_socket.is_panel_collapsed()) {
562 make_socket_visible(from_node, from_socket);
563 }
564 if (to_socket.is_user_hidden() || to_socket.is_panel_collapsed()) {
565 make_socket_visible(to_node, to_socket);
566 }
567 }
568
569 bNodeLink &link = bke::node_add_link(tree, from_node, from_socket, to_node, to_socket);
570 if (muted) {
571 link.flag |= NODE_LINK_MUTED;
572 }
573 if ((to_socket.flag & SOCK_MULTI_INPUT) && multi_input_sort_id) {
574 link.multi_input_sort_id = *multi_input_sort_id;
575
576 /* Multi-input sort IDs must be updated immediately to prevent invalid cases later on. */
577 for (bNodeLink *other_link : links_by_to_socket.lookup(&to_socket)) {
578 if (other_link->multi_input_sort_id >= *multi_input_sort_id) {
579 other_link->multi_input_sort_id += 1;
580 }
581 }
582 }
583
584 return link;
585}
586
588 const bNode &src_node,
589 bNodeTree &dst_tree)
590{
591 BLI_assert(src_node.declaration() != nullptr);
592 const nodes::NodeDeclaration &node_decl = *src_node.declaration();
593
595 NodeSetInterfaceBuilder builder(params, result, dst_tree, {&src_node});
596 for (const nodes::ItemDeclaration *item_decl : node_decl.root_items) {
597 expose_declaration_item_recursive(builder, src_node, *item_decl, nullptr);
598 }
599
600 return result;
601}
602
603static void map_socket(NodeTreeInterfaceMapping &io_mapping,
604 const NodeSetInterfaceParams &params,
605 const bNode &group_node,
606 const bNodeTreeInterfaceSocket &io_socket)
607{
608 const bNodeTree &group_tree = *id_cast<const bNodeTree *>(group_node.id);
609 const bool is_input = (io_socket.flag & NODE_INTERFACE_SOCKET_INPUT);
610 const bNodeSocket *group_socket =
611 is_input ? group_node.input_by_identifier(UString(io_socket.identifier)) :
612 group_node.output_by_identifier(UString(io_socket.identifier));
613 BLI_assert(group_socket);
614 if (!group_socket->is_available()) {
615 return;
616 }
617 if (params.skip_hidden && !group_socket->is_visible()) {
618 return;
619 }
620
622 data.internal_sockets.add_multiple(
623 get_internal_group_links(group_tree, io_socket, params.skip_hidden)
624 .as_span()
625 .cast<NodeAndSocket>());
626 data.external_sockets.add_multiple(get_socket_links(*group_socket, false));
627 data.hidden = group_socket->is_user_hidden();
628 data.collapsed = group_socket->flag & SOCK_COLLAPSED;
629 io_mapping.socket_data.add(&io_socket, std::move(data));
630}
631
632static void map_panel(NodeTreeInterfaceMapping &io_mapping,
633 const NodeSetInterfaceParams & /*params*/,
634 const bNode &group_node,
635 const bNodeTreeInterfacePanel &io_panel)
636{
638 for (const bNodePanelState &panel_state : group_node.panel_states()) {
639 if (panel_state.identifier == io_panel.identifier) {
640 data.collapsed = panel_state.is_collapsed();
641 }
642 }
643 io_mapping.panel_data.add(&io_panel, std::move(data));
644}
645
646/* Make sure the target node tree uses a different action than the source.
647 * Otherwise the target action is cleared to ensure a new action is created. */
648static void ensure_separate_actions(Main &bmain, const bNodeTree &src, bNodeTree &dst)
649{
650 const AnimData *src_adt = BKE_animdata_from_id(&src.id);
651 AnimData *dst_adt = BKE_animdata_from_id(&dst.id);
652 if (!src_adt || !dst_adt) {
653 /* Nothing to do:
654 * If the source has no animdata nothing will be copied.
655 * If the target has no animdata a new action will be created anyway. */
656 return;
657 }
658
659 if (dst_adt->action == src_adt->action) {
660 const bool unassign_ok = animrig::unassign_action({dst.id, *dst_adt});
661 BLI_assert_msg(unassign_ok, "Expected Action unassignment to work");
662 UNUSED_VARS_NDEBUG(unassign_ok);
663
665 }
666}
667
669 const bNodeTree &tree,
670 const bNode &group_node)
671{
672 BLI_assert(group_node.is_group());
673 const bNodeTree &group_tree = *id_cast<const bNodeTree *>(group_node.id);
674
675 tree.ensure_topology_cache();
676
678 for (const bNodeTreeInterfaceItem *io_item : group_tree.interface_items()) {
679 switch (io_item->item_type) {
681 const auto *io_panel = reinterpret_cast<const bNodeTreeInterfacePanel *>(io_item);
682 map_panel(result, params, group_node, *io_panel);
683 break;
684 }
686 const auto *io_socket = reinterpret_cast<const bNodeTreeInterfaceSocket *>(io_item);
687 map_socket(result, params, group_node, *io_socket);
688 }
689 }
690 }
691 return result;
692}
693
695{
696 return dst_tree_;
697}
698
700{
701 return node_map_;
702}
703
705{
706 return node_identifier_map_;
707}
708
709NodeSetCopy NodeSetCopy::from_nodes(Main &bmain,
710 const bNodeTree &src_tree,
711 const Span<const bNode *> src_nodes,
713{
714 const SocketLinkMap links_by_socket = get_links_by_to_socket(dst_tree);
715
716 NodeSetCopy result(dst_tree);
717 Vector<AnimationBasePathChange> anim_basepaths;
718 /* Note: socket map is not stored in NodeSetCopy because socket pointers are easily invalidated
719 * by adding links to the tree. This should only be used locally. */
721 for (const bNode *src_node : src_nodes) {
723 &dst_tree, *src_node, LIB_ID_COPY_DEFAULT, std::nullopt, std::nullopt, socket_map);
724
725 result.node_map_.add(src_node, dst_node);
726 result.node_identifier_map_.add(src_node->identifier, dst_node->identifier);
727
728 anim_basepaths.append(
729 {node_basepath(src_tree, *src_node), node_basepath(dst_tree, *dst_node)});
730 }
731
732 /* Recreate parent/child relationship of nodes. */
733 for (const auto &item : result.node_map_.items()) {
734 const bNode *node = item.key;
735 bNode *new_node = item.value;
736 if (node->parent) {
737 if (bNode *new_parent = result.node_map_.lookup_default(node->parent, nullptr)) {
738 bke::node_attach_node(dst_tree, *new_node, *new_parent);
739 }
740 else {
742 }
743 }
744 }
745
746 /* Recreate internal links. */
747 const Vector<const bNodeLink *> internal_links = find_internal_links(src_tree, src_nodes);
748 for (const bNodeLink *src_link : internal_links) {
750 links_by_socket,
751 *result.node_map_.lookup(src_link->fromnode),
752 *socket_map.lookup(src_link->fromsock),
753 *result.node_map_.lookup(src_link->tonode),
754 *socket_map.lookup(src_link->tosock),
755 src_link->flag & NODE_LINK_MUTED,
756 src_link->multi_input_sort_id);
757 }
758
759 /* Recreate zone pairing between new nodes. */
760 const Vector<bNode *> new_nodes(result.node_map_.values().begin(),
761 result.node_map_.values().end());
762 remap_pairing(dst_tree, new_nodes, result.node_identifier_map_);
763
764 /* Copy animation data of source nodes. */
765 if (&src_tree != &dst_tree) {
766 ensure_separate_actions(bmain, src_tree, dst_tree);
767 }
768 BKE_animdata_copy_by_basepath(bmain, src_tree.id, dst_tree.id, anim_basepaths);
769
770 /* Move nodes in the group to the center */
771 if (const std::optional<Bounds<float2>> bounds = node_location_bounds(src_nodes)) {
772 float2 center = bounds->center();
773 /* Round offset to a grid step, so grid-aligned nodes remain grid-aligned. */
774 center[0] = nearest_node_grid_coord(center[0]);
775 center[1] = nearest_node_grid_coord(center[1]);
776 for (bNode *node : new_nodes) {
777 node->location[0] -= center[0];
778 node->location[1] -= center[1];
779 }
780 }
781
782 return result;
783}
784
786 const bNodeTree &src_tree,
787 FunctionRef<bool(const bNode &node)> node_predicate,
789{
791 for (const bNode &node : src_tree.nodes) {
792 if (node_predicate(node)) {
793 nodes.append(&node);
794 }
795 }
796 return from_nodes(bmain, src_tree, nodes, dst_tree);
797}
798
800 const NodeSetCopy &copied_nodes,
801 const NodeTreeInterfaceMapping &io_mapping)
802{
803 Main &bmain = *CTX_data_main(&C);
804 bNodeTree &tree = copied_nodes.dst_tree();
805 tree.ensure_topology_cache();
806 const SocketLinkMap links_by_socket = get_links_by_to_socket(tree);
807
808 GroupInputOutputNodes io_nodes;
809 io_nodes.output_node = tree.group_output_node();
810 if (!io_nodes.output_node) {
812 }
814
815 /* This makes sure that all nodes have the correct sockets so that we can link. */
816 BKE_main_ensure_invariants(bmain, tree.id);
817
818 for (const auto &item : io_mapping.socket_data.items()) {
819 for (const NodeAndSocket &origin : item.value.internal_sockets) {
820 bNode &new_node = *copied_nodes.node_map().lookup(&origin.node);
821 bNodeSocket *new_socket = origin.find_socket_in_node(new_node);
822 /* Some nodes change after unlinking (e.g. viewers), socket may not exist any more. */
823 if (!new_socket) {
824 continue;
825 }
826 if (new_socket->is_input()) {
827 bNodeSocket *group_input_socket = node_group_input_find_socket(io_nodes.input_node,
828 item.key->identifier);
829 BLI_assert(group_input_socket);
831 links_by_socket,
832 *io_nodes.input_node,
833 *group_input_socket,
834 new_node,
835 *new_socket,
836 origin.link_muted,
837 origin.multi_input_sort_id);
838 }
839 else {
840 bNodeSocket *group_output_socket = node_group_output_find_socket(io_nodes.output_node,
841 item.key->identifier);
842 BLI_assert(group_output_socket);
844 links_by_socket,
845 new_node,
846 *new_socket,
847 *io_nodes.output_node,
848 *group_output_socket,
849 origin.link_muted,
850 std::nullopt);
851 }
852 }
853 }
854
855 /* Make sure group input/output node sockets match the tree interface. */
858
859 /* Move group input/output nodes to the edges of the bounding box. */
860 Vector<const bNode *> nodes_vec;
861 for (const bNode *node : copied_nodes.node_map().values()) {
862 nodes_vec.append(node);
863 }
864 if (const std::optional<Bounds<float2>> bounds = node_bounds(nodes_vec)) {
865 /* 3 grid units apart from the bounds, accounting for the width of the Group Input node. */
866 io_nodes.input_node->location[0] = bounds->min[0] - 200.0f;
867 io_nodes.output_node->location[0] = bounds->max[0] + 60.0f;
868 }
869 /* Ignore node dimensions for vertical placement. */
870 if (const std::optional<Bounds<float2>> bounds = node_location_bounds(nodes_vec)) {
871 float center_y_snap = nearest_node_grid_coord(bounds->center()[1]);
872 io_nodes.input_node->location[1] = center_y_snap;
873 io_nodes.output_node->location[1] = center_y_snap;
874 }
875
876 return io_nodes;
877}
878
879static bool socket_types_need_conversion(const StringRef from_type, const StringRef to_type)
880{
881 const bke::bNodeSocketType *from_typeinfo = bke::node_socket_type_find(from_type);
883 const bool from_static = node_is_static_socket_type(*from_typeinfo);
884 const bool to_static = node_is_static_socket_type(*to_typeinfo);
885 if (from_typeinfo == nullptr || to_typeinfo == nullptr) {
886 return true;
887 }
888 if (from_static != to_static) {
889 return true;
890 }
891 /* Dynamic socket types need conversion if type idnames are different. */
892 if (!from_static && !to_static) {
893 return from_type != to_type;
894 }
895 /* Static socket types need conversion only if the base types are different. */
896 return from_typeinfo->type != to_typeinfo->type;
897}
898
900 const bNodeTreeInterfaceSocket &io_socket)
901{
902 /* A proxy is needed if any internal or external connection has a different type and therefore
903 * cannot directly be connected without loss of conversion. */
904 for (const MutableNodeAndSocket &in_link : links) {
905 if (const bNodeSocket *in_socket = in_link.find_socket()) {
906 if (socket_types_need_conversion(in_socket->idname, io_socket.socket_type)) {
907 return true;
908 }
909 }
910 }
911 return false;
912}
913
914static std::pair<std::optional<MutableNodeAndSocket>, std::optional<MutableNodeAndSocket>>
916{
917 std::optional<MutableNodeAndSocket> in, out;
918 for (bNodeSocket &socket : proxy_node.inputs) {
919 if (socket.is_available() && !socket.is_user_hidden()) {
920 in.emplace(MutableNodeAndSocket{proxy_node, socket, false});
921 break;
922 }
923 }
924 for (bNodeSocket &socket : proxy_node.outputs) {
925 if (socket.is_available() && !socket.is_user_hidden()) {
926 out.emplace(MutableNodeAndSocket{proxy_node, socket, false});
927 break;
928 }
929 }
930 return {in, out};
931}
932
934
935/* Create a constant value or field input proxy node, using the source socket value. */
937 const bNodeTree &src_tree,
938 const bNodeSocket &src_socket,
939 bContext &C,
940 bNodeTree &dst_tree,
941 Vector<AnimationBasePathChange> &anim_basepaths)
942{
943 const eNodeSocketDatatype socket_type = bke::node_socket_type_find(io_socket.socket_type)->type;
944 const NodeDefaultInputType default_input_type = NodeDefaultInputType(io_socket.default_input);
945 /* TODO Inferred structure type isn't fully working yet, use declared structure type for now. */
946 // const nodes::StructureType structure_type = nodes::StructureType(
947 // src_socket.runtime->inferred_structure_type);
948 const nodes::StructureType structure_type = io_socket.structure_type ==
950 nodes::StructureType::Dynamic :
951 nodes::StructureType(io_socket.structure_type);
952
953 if (ELEM(structure_type, nodes::StructureType::Grid, nodes::StructureType::List)) {
954 /* Grids and Lists don't have input value nodes or implicit field inputs. */
955 return nullptr;
956 }
957
959 socket_type, default_input_type, C, dst_tree))
960 {
961 return proxy_node;
962 }
963
964 if (!src_socket.default_value) {
965 /* No proxy needed if the socket type does not have input values. */
966 return nullptr;
967 }
969 socket_type, src_tree, src_socket, C, dst_tree, anim_basepaths))
970 {
971 return proxy_node;
972 }
973
974 /* Fall back to reroute node. */
975 return bke::node_add_static_node(&C, dst_tree, NODE_REROUTE);
976}
977
978/* Create a converter proxy node for the interface socket. */
980 const bNodeTree &src_tree,
981 const bNodeSocket *src_socket,
982 bContext &C,
983 bNodeTree &dst_tree,
984 Vector<AnimationBasePathChange> &anim_basepaths)
985{
986 const eNodeSocketDatatype socket_type = bke::node_socket_type_find(io_socket.socket_type)->type;
987
989 socket_type, src_tree, src_socket, C, dst_tree, anim_basepaths))
990 {
991 return proxy_node;
992 }
993
994 /* Fall back to reroute node. */
995 return bke::node_add_static_node(&C, dst_tree, NODE_REROUTE);
996}
997
998/* Replace an interface socket by adding proxy nodes. */
1000 bContext &C,
1001 bNodeTree &dst_tree,
1002 const bNodeTreeInterfaceSocket &io_socket,
1003 const Span<MutableNodeAndSocket> incoming_links,
1004 const Span<MutableNodeAndSocket> outgoing_links,
1005 const bNode *group_node,
1006 InterfaceProxyNodes &interface_proxies,
1007 float2 &input_location,
1008 float2 &output_location,
1009 UniqueLinkSet &unique_links,
1010 Vector<AnimationBasePathChange> &anim_basepaths_for_dst_tree,
1011 Vector<AnimationBasePathChange> &anim_basepaths_for_group_tree)
1012{
1013 /* If there are no output connections the socket is unused and can be discarded. */
1014 if (outgoing_links.is_empty()) {
1015 return;
1016 }
1017
1018 /* Find the socket input value to use, if available. */
1019 const bNodeTree &group_tree = *id_cast<bNodeTree *>(group_node->id);
1020 const bNode *group_output_node = group_tree.group_output_node();
1021 /* The source for inputs is the group node, for constant outputs is the group output node. */
1022 const bool is_input = io_socket.flag & NODE_INTERFACE_SOCKET_INPUT;
1023 Vector<AnimationBasePathChange> &anim_basepaths = is_input ? anim_basepaths_for_dst_tree :
1024 anim_basepaths_for_group_tree;
1025 const bNodeTree &src_tree = is_input ? dst_tree : group_tree;
1026 const bNode *src_node = is_input ? group_node : group_output_node;
1027 const bNodeSocket *src_socket = src_node ? bke::node_find_socket(*src_node,
1028 SOCK_IN,
1029 UString(io_socket.identifier)) :
1030 nullptr;
1031
1032 /* Create a proxy node if necessary. */
1033 bNode *proxy_node = nullptr;
1034 if (incoming_links.is_empty()) {
1035 /* The socket has no incoming links. A proxy is needed if the socket value needs to be stored
1036 * or if the socket uses a default input field. */
1037 if (src_socket) {
1038 proxy_node = create_proxy_input_node(
1039 io_socket, src_tree, *src_socket, C, dst_tree, anim_basepaths);
1040 }
1041 }
1042 else {
1043 /* A proxy is needed if any internal or external connection has a different type
1044 * and therefore cannot directly be connected without loss of conversion. */
1045 if (any_link_need_conversion(incoming_links, io_socket) &&
1046 any_link_need_conversion(outgoing_links, io_socket))
1047 {
1048 proxy_node = create_proxy_converter_node(
1049 io_socket, src_tree, src_socket, C, dst_tree, anim_basepaths);
1050 }
1051 }
1052
1053 if (proxy_node) {
1054 BLI_assert(proxy_node);
1055 STRNCPY(proxy_node->label, io_socket.name().c_str());
1056
1057 const float width = (proxy_node->is_reroute() ? 0.0f : proxy_node->width);
1058 const float height = (proxy_node->is_reroute() ? 0.0f : proxy_node->height);
1059 if (is_input) {
1060 proxy_node->location[0] = input_location.x - width;
1061 proxy_node->location[1] = input_location.y;
1062 input_location.y -= height + 20.0f;
1063 }
1064 else {
1065 proxy_node->location[0] = output_location.x;
1066 proxy_node->location[1] = output_location.y;
1067 output_location.y -= height + 20.0f;
1068 }
1069
1070 interface_proxies.add_new(io_socket.identifier, proxy_node);
1071
1072 /* Connect incoming to outgoing sockets via the proxy node. */
1073 const auto [proxy_input, proxy_output] = find_proxy_node_sockets(*proxy_node);
1074 BLI_assert(proxy_input || incoming_links.is_empty());
1075 BLI_assert(proxy_output || outgoing_links.is_empty());
1076 for (const MutableNodeAndSocket &from_socket : incoming_links) {
1077 unique_links.add({from_socket, *proxy_input});
1078 }
1079 for (const MutableNodeAndSocket &to_socket : outgoing_links) {
1080 unique_links.add({*proxy_output, to_socket});
1081 }
1082 }
1083 else {
1084 /* Connect incoming to outgoing sockets directly.
1085 * Creates N * M links, in practice N is usually 1. */
1086 for (const MutableNodeAndSocket &from_socket : incoming_links) {
1087 for (const MutableNodeAndSocket &to_socket : outgoing_links) {
1088 unique_links.add({from_socket, to_socket});
1089 }
1090 }
1091 }
1092}
1093
1095 bContext &C,
1096 const bNodeTree &src_tree,
1097 const NodeSetCopy &copied_nodes,
1098 const NodeTreeInterfaceMapping &io_mapping,
1099 const bNode *group_node)
1100{
1101 bNodeTree &dst_tree = copied_nodes.dst_tree();
1102 const SocketLinkMap links_by_socket = get_links_by_to_socket(dst_tree);
1103
1104 /* Set location for proxy nodes, based on drawing order of interface items. */
1105 float2 input_location = {-50, 0}, output_location = {50, 0};
1106 Vector<const bNode *> nodes_vec;
1107 for (const bNode *node : copied_nodes.node_map().values()) {
1108 nodes_vec.append(node);
1109 }
1110 if (const std::optional<Bounds<float2>> bounds = node_bounds(nodes_vec)) {
1111 const Bounds<float2> loc_bounds = *node_location_bounds(nodes_vec);
1112 /* Move outputs to the edge of copied nodes, which are centered at zero. */
1113 input_location.x += -loc_bounds.size().x * 0.5f;
1114 output_location.x += -loc_bounds.size().x * 0.5f + bounds->size().x;
1115 }
1116
1117 /* Deduplicate links in case multiple connections get merged. This can happen because both
1118 * input and output sockets are connected, potentially adding redundant links. This can
1119 * theoretically create N * M links but in practice either N or M is usually 1. */
1120 UniqueLinkSet unique_links;
1121 /* Animdata paths for group sockets for copying animdata to proxy nodes. */
1122 Vector<AnimationBasePathChange> anim_basepaths_for_dst_tree;
1123 Vector<AnimationBasePathChange> anim_basepaths_for_group_tree;
1124
1125 InterfaceProxyNodes interface_proxies;
1126 /* Loop over mapped items based on the interface socket order. */
1127 for (const bNodeTreeInterfaceItem *io_item : src_tree.interface_items()) {
1128 if (io_item->item_type != NodeTreeInterfaceItemType::Socket) {
1129 continue;
1130 }
1131 const auto &io_socket = bke::node_interface::get_item_as<bNodeTreeInterfaceSocket>(*io_item);
1132 const std::optional<NodeTreeInterfaceMapping::InterfaceSocketData> socket_data =
1133 io_mapping.socket_data.lookup_try(&io_socket);
1134 if (!socket_data) {
1135 continue;
1136 }
1137 const bool is_input = io_socket.flag & NODE_INTERFACE_SOCKET_INPUT;
1138
1139 /* Gather all new links to and from the interface socket that must be added. */
1140 Vector<MutableNodeAndSocket> incoming_links, outgoing_links;
1141 if (is_input) {
1142 incoming_links.extend(socket_data->external_sockets);
1143 }
1144 else {
1145 outgoing_links.extend(socket_data->external_sockets);
1146 }
1147 for (const NodeAndSocket &origin : socket_data->internal_sockets) {
1148 if (origin.node.is_group_output()) {
1149 /* Directly connect to external output links. */
1150 BLI_assert(is_input);
1152 src_tree, origin.socket_identifier);
1153 outgoing_links.extend(
1154 io_mapping.socket_data.lookup_default(io_socket, {}).external_sockets);
1155 continue;
1156 }
1157 if (origin.node.is_group_input()) {
1158 /* Directly connect to external input links. */
1159 BLI_assert(!is_input);
1161 src_tree, origin.socket_identifier);
1162 incoming_links.extend(
1163 io_mapping.socket_data.lookup_default(io_socket, {}).external_sockets);
1164 continue;
1165 }
1166
1167 /* Map the old internal to the new socket in the target tree. */
1168 bNode &new_node = *copied_nodes.node_map().lookup_default(&origin.node, nullptr);
1169 bNodeSocket *new_socket = origin.find_socket_in_node(new_node);
1170 /* Some nodes change after unlinking (e.g. viewers), socket may not exist any more. */
1171 if (!new_socket) {
1172 continue;
1173 }
1174 if (is_input) {
1175 outgoing_links.append(
1176 {new_node, *new_socket, origin.link_muted, origin.multi_input_sort_id});
1177 }
1178 else {
1179 /* Outputs shouldn't have multi-input sort ids. */
1180 BLI_assert(!origin.multi_input_sort_id.has_value());
1181 incoming_links.append({new_node, *new_socket, origin.link_muted});
1182 }
1183 }
1184
1186 dst_tree,
1187 io_socket,
1188 incoming_links,
1189 outgoing_links,
1190 group_node,
1191 interface_proxies,
1192 input_location,
1193 output_location,
1194 unique_links,
1195 anim_basepaths_for_dst_tree,
1196 anim_basepaths_for_group_tree);
1197 }
1198
1199 /* Actually add deduplicated links to the tree. */
1200 for (const std::pair<MutableNodeAndSocket, MutableNodeAndSocket> &item : unique_links) {
1201 /* Link is muted if either side is muted. */
1202 const bool link_muted = item.first.link_muted || item.second.link_muted;
1203 /* Outputs shouldn't have multi-input sort ids. */
1204 BLI_assert(!item.first.multi_input_sort_id.has_value());
1205 bNodeSocket *from_socket = item.first.find_socket();
1206 bNodeSocket *to_socket = item.second.find_socket();
1207 if (from_socket && to_socket) {
1209 links_by_socket,
1210 item.first.node,
1211 *from_socket,
1212 item.second.node,
1213 *to_socket,
1214 link_muted,
1215 item.second.multi_input_sort_id);
1216 }
1217 }
1218
1220 *CTX_data_main(&C), dst_tree.id, dst_tree.id, anim_basepaths_for_dst_tree);
1222 *CTX_data_main(&C), *group_node->id, dst_tree.id, anim_basepaths_for_group_tree);
1223
1224 return interface_proxies;
1225}
1226
1228 const NodeTreeInterfaceMapping &io_mapping)
1229{
1230 using InterfaceSocketData = NodeTreeInterfaceMapping::InterfaceSocketData;
1231
1232 bNodeTree &owner_tree = group_node.owner_tree();
1233 const SocketLinkMap links_by_socket = get_links_by_to_socket(owner_tree);
1234 const bNodeTree &group_tree = *id_cast<bNodeTree *>(group_node.id);
1235
1236 /* Cache node socket lists to avoid invalid topology cache after linking. */
1237 owner_tree.ensure_topology_cache();
1238 const Span<bNodeSocket *> group_node_inputs = group_node.input_sockets();
1239 const Span<bNodeSocket *> group_node_outputs = group_node.output_sockets();
1240
1241 for (bNodeSocket *group_node_input : group_node_inputs) {
1243 group_tree, group_node_input->identifier);
1244 if (!interface) {
1245 continue;
1246 }
1247 const InterfaceSocketData *data = io_mapping.socket_data.lookup_ptr(interface);
1248 if (!data) {
1249 continue;
1250 }
1251 for (const MutableNodeAndSocket &link : data->external_sockets) {
1252 BLI_assert(owner_tree.all_nodes().contains(&link.node));
1253 if (bNodeSocket *link_socket = link.find_socket()) {
1254 add_link_and_make_visible(owner_tree,
1255 links_by_socket,
1256 link.node,
1257 *link_socket,
1258 group_node,
1259 *group_node_input,
1260 link.link_muted,
1261 std::nullopt);
1262 }
1263 }
1264 /* Keep old socket visibility. */
1265 SET_FLAG_FROM_TEST(group_node_input->flag, data->hidden, SOCK_HIDDEN);
1266 SET_FLAG_FROM_TEST(group_node_input->flag, data->collapsed, SOCK_COLLAPSED);
1267 }
1268 for (bNodeSocket *group_node_output : group_node_outputs) {
1270 group_tree, group_node_output->identifier);
1271 if (!interface) {
1272 continue;
1273 }
1274 const InterfaceSocketData *data = io_mapping.socket_data.lookup_ptr(interface);
1275 if (!data) {
1276 continue;
1277 }
1278 for (const MutableNodeAndSocket &link : data->external_sockets) {
1279 BLI_assert(owner_tree.all_nodes().contains(&link.node));
1280 if (bNodeSocket *link_socket = link.find_socket()) {
1281 add_link_and_make_visible(owner_tree,
1282 links_by_socket,
1283 group_node,
1284 *group_node_output,
1285 link.node,
1286 *link_socket,
1287 link.link_muted,
1288 link.multi_input_sort_id);
1289 }
1290 }
1291 /* Keep old socket visibility. */
1292 SET_FLAG_FROM_TEST(group_node_output->flag, data->hidden, SOCK_HIDDEN);
1293 SET_FLAG_FROM_TEST(group_node_output->flag, data->collapsed, SOCK_COLLAPSED);
1294 }
1295
1296 /* Keep old panel collapse status. */
1297 MutableSpan<bNodePanelState> panel_states = group_node.panel_states();
1298 for (const auto &item : io_mapping.panel_data.items()) {
1299 for (bNodePanelState &new_panel_state : panel_states) {
1300 if (new_panel_state.identifier == item.key->identifier) {
1301 SET_FLAG_FROM_TEST(new_panel_state.flag, item.value.collapsed, NODE_PANEL_COLLAPSED);
1302 }
1303 }
1304 }
1305}
1306
1308 private:
1309 Set<int32_t> used_ref_ids_;
1311
1312 public:
1314 : rng_(RandomNumberGenerator::from_random_seed())
1315 {
1316 for (const bNestedNodeRef &ref : used_nested_node_refs) {
1317 used_ref_ids_.add(ref.id);
1318 }
1319 }
1320
1322 {
1323 /* Find new unique identifier for the nested node ref. */
1324 while (true) {
1325 const int32_t new_id = rng_.get_int32(INT32_MAX);
1326 if (used_ref_ids_.add(new_id)) {
1327 return new_id;
1328 }
1329 }
1331 return 0;
1332 }
1333};
1334
1335static void append_nested_node_refs(bNodeTree &ntree, const Span<bNestedNodeRef> nested_node_refs)
1336{
1337 const int new_nested_node_refs_num = ntree.nested_node_refs_num + nested_node_refs.size();
1338 bNestedNodeRef *new_nested_node_refs = MEM_new_array<bNestedNodeRef>(new_nested_node_refs_num,
1339 __func__);
1340 uninitialized_copy_n(ntree.nested_node_refs, ntree.nested_node_refs_num, new_nested_node_refs);
1341 uninitialized_copy_n(nested_node_refs.data(),
1342 nested_node_refs.size(),
1343 new_nested_node_refs + ntree.nested_node_refs_num);
1344
1345 MEM_SAFE_DELETE(ntree.nested_node_refs);
1346 ntree.nested_node_refs = new_nested_node_refs;
1347 ntree.nested_node_refs_num = new_nested_node_refs_num;
1348}
1349
1351 const bNode &group_node,
1352 const NodeSetCopy &node_set_copy)
1353{
1354 BLI_assert(group_node.is_group());
1355 BLI_assert(group_node.id == &node_set_copy.dst_tree().id);
1356
1357 bNodeTree &dst_tree = *id_cast<bNodeTree *>(group_node.id);
1358 /* Update nested node references in the parent and child node tree. */
1359 NestedNodeRefIDGenerator ref_id_gen(dst_tree.nested_node_refs_span());
1360
1361 Vector<bNestedNodeRef> new_group_refs;
1362 for (bNestedNodeRef &ref : src_tree.nested_node_refs_span()) {
1363 const std::optional<int32_t> new_node_id = node_set_copy.node_identifier_map().lookup_try(
1364 ref.path.node_id);
1365 if (!new_node_id) {
1366 /* The node was not moved between node groups. */
1367 continue;
1368 }
1369 /* Find new unique identifier for the nested node ref. */
1370 const int32_t new_ref_id = ref_id_gen();
1371
1372 /* Add a new nested node ref inside the group. */
1373 bNestedNodeRef new_ref = ref;
1374 new_ref.id = new_ref_id;
1375 new_ref.path.node_id = *new_node_id;
1376 new_group_refs.append(new_ref);
1377
1378 /* Update the nested node ref in the parent so that it points to the same node that is now
1379 * inside of a nested group. */
1380 ref.path.node_id = group_node.identifier;
1381 ref.path.id_in_node = new_ref_id;
1382 }
1383
1384 append_nested_node_refs(dst_tree, new_group_refs);
1385}
1386
1388 const bNode &group_node,
1389 const NodeSetCopy &node_set_copy)
1390{
1391 BLI_assert(group_node.is_group());
1392 BLI_assert(group_node.id != nullptr);
1393 BLI_assert(&dst_tree == &node_set_copy.dst_tree());
1394
1395 const bNodeTree &src_tree = *id_cast<const bNodeTree *>(group_node.id);
1396 for (bNestedNodeRef &dst_ref : dst_tree.nested_node_refs_span()) {
1397 if (dst_ref.path.node_id != group_node.identifier) {
1398 continue;
1399 }
1400 const bNestedNodeRef *src_ref = src_tree.find_nested_node_ref(dst_ref.path.id_in_node);
1401 if (!src_ref) {
1402 continue;
1403 }
1404 constexpr int32_t missing_id = -1;
1405 const int32_t dst_node_id = node_set_copy.node_identifier_map().lookup_default(
1406 src_ref->path.node_id, missing_id);
1407 if (dst_node_id == missing_id) {
1408 continue;
1409 }
1410 dst_ref.path.node_id = dst_node_id;
1411 dst_ref.path.id_in_node = src_ref->path.id_in_node;
1412 }
1413}
1414
1416
1417} // namespace ed::space_node
1418
1419} // namespace blender
Functions and classes to work with Actions.
Blender kernel action and pose functionality.
#define NODE_REROUTE
Definition BKE_node.hh:867
#define NODE_GROUP_OUTPUT
Definition BKE_node.hh:869
#define NODE_GROUP_INPUT
Definition BKE_node.hh:868
#define INT32_MAX
#define C
Definition RandGen.cpp:29
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define SET_FLAG_FROM_TEST(value, test, flag)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
std::optional< Value > lookup_try(const Key &key) const
Definition BLI_map.hh:531
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
Span< Value > lookup(const Key &key) const
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr bool is_empty() const
Definition BLI_span.hh:264
StringRefNull ref() const
UString()=default
void append(const T &value)
bool is_empty() const
void extend(Span< T > array)
virtual const int & get_corresponding_output_id(const bNode &input_bnode) const =0
static NodeSetCopy from_predicate(Main &bmain, const bNodeTree &src_tree, FunctionRef< bool(const bNode &node)> node_predicate, bNodeTree &dst_tree)
const Map< int32_t, int32_t > & node_identifier_map() const
static NodeSetCopy from_nodes(Main &bmain, const bNodeTree &src_tree, const Span< const bNode * > src_nodes, bNodeTree &dst_tree)
const Map< const bNode *, bNode * > & node_map() const
NodeSetInterfaceBuilder(NodeSetInterfaceParams params, NodeTreeInterfaceMapping &io_mapping, bNodeTree &dst_tree, const Span< const bNode * > src_nodes)
void expose_socket(const bNodeSocket &src_socket, bNodeTreeInterfacePanel *parent)
bNodeTreeInterfacePanel * expose_panel(const bNode &src_node, const nodes::PanelDeclaration &panel_decl, bNodeTreeInterfacePanel *parent)
Map< const bNodeTreeInterfaceSocket *, InterfaceSocketData > socket_data
Map< const bNodeTreeInterfacePanel *, InterfacePanelData > panel_data
Vector< ItemDeclaration * > root_items
#define offsetof(t, d)
uint to_type(bool)
bool unassign_action(ID &animated_id)
T & get_item_as(bNodeTreeInterfaceItem &item)
bNode * create_proxy_implicit_input_node(eNodeSocketDatatype socket_type, NodeDefaultInputType default_input, bContext &C, bNodeTree &tree)
bNodeTreeInterfaceSocket * add_interface_socket_from_node(bNodeTree &ntree, const bNode &from_node, const bNodeSocket &from_sock, std::optional< StringRef > name=std::nullopt, std::optional< eNodeSocketInOut > in_out=std::nullopt)
bNode * create_proxy_converter_node(eNodeSocketDatatype socket_type, const bNodeTree &src_tree, const bNodeSocket *src_socket, bContext &C, bNodeTree &dst_tree, Vector< AnimationBasePathChange > &anim_basepaths)
bNode * create_proxy_const_input_node(eNodeSocketDatatype socket_type, const bNodeTree &src_tree, const bNodeSocket &src_socket, bContext &C, bNodeTree &dst_tree, Vector< AnimationBasePathChange > &anim_basepaths)
const bNodeZoneType * zone_type_by_node_type(const int node_type)
void node_attach_node(bNodeTree &ntree, bNode &node, bNode &parent)
Definition node.cc:4883
const bNodeTreeInterfaceSocket * node_find_interface_input_by_identifier(const bNodeTree &ntree, StringRef identifier)
Definition node.cc:4079
void node_detach_node(bNodeTree &ntree, bNode &node)
Definition node.cc:4891
bNodeSocketType * node_socket_type_find(StringRef idname)
Definition node.cc:2966
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
Span< int > all_zone_input_node_types()
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
bNode * node_copy_with_mapping(bNodeTree *dst_tree, const bNode &node_src, int flag, std::optional< StringRefNull > dst_unique_name, std::optional< int > dst_unique_identifier, Map< const bNodeSocket *, bNodeSocket * > &new_socket_map, bool allow_duplicate_names=false)
Definition node.cc:4327
const bNodeTreeInterfaceSocket * node_find_interface_output_by_identifier(const bNodeTree &ntree, StringRef identifier)
Definition node.cc:4091
bool node_is_static_socket_type(const bNodeSocketType &stype)
Definition node.cc:3483
bool node_link_is_hidden(const bNodeLink &link)
Definition node.cc:4711
Bounds< T > merge(const Bounds< T > &a, const Bounds< T > &b)
Definition BLI_bounds.hh:27
std::optional< Bounds< T > > min_max(const std::optional< Bounds< T > > &a, const T &b)
Definition BLI_bounds.hh:56
static bNode * create_proxy_converter_node(const bNodeTreeInterfaceSocket &io_socket, const bNodeTree &src_tree, const bNodeSocket *src_socket, bContext &C, bNodeTree &dst_tree, Vector< AnimationBasePathChange > &anim_basepaths)
static Vector< MutableNodeAndSocket > get_internal_group_links(const bNodeTree &tree, const bNodeTreeInterfaceSocket &io_socket, const bool skip_hidden, NodeFilterFn link_filter=default_link_filter)
Set< std::pair< MutableNodeAndSocket, MutableNodeAndSocket > > UniqueLinkSet
static void expose_declaration_item_recursive(NodeSetInterfaceBuilder &builder, const bNode &src_node, const nodes::ItemDeclaration &item_decl, bNodeTreeInterfacePanel *parent)
static void make_panel_visible(bNode &node, const nodes::PanelDeclaration &panel_decl)
static bool any_link_need_conversion(const Span< MutableNodeAndSocket > links, const bNodeTreeInterfaceSocket &io_socket)
static void append_nested_node_refs(bNodeTree &ntree, const Span< bNestedNodeRef > nested_node_refs)
static bool make_parent_panels_visible_recursive(bNode &node, StringRef socket_identifier, const Span< nodes::ItemDeclaration * > item_decls)
MultiValueMap< const bNodeSocket *, bNodeLink * > SocketLinkMap
static Vector< const bNodeLink * > find_internal_links(const bNodeTree &tree, const Span< const bNode * > nodes, NodeFilterFn link_filter=default_link_filter)
GroupInputOutputNodes connect_copied_nodes_to_interface(const bContext &C, const NodeSetCopy &copied_nodes, const NodeTreeInterfaceMapping &io_mapping)
Map< std::string, bNode * > InterfaceProxyNodes
NodeTreeInterfaceMapping build_node_set_interface(const NodeSetInterfaceParams &params, const bNodeTree &src_tree, const Span< bNode * > src_nodes, bNodeTree &dst_tree)
static void make_socket_visible(bNode &node, bNodeSocket &socket)
NodeTreeInterfaceMapping build_node_declaration_interface(const NodeSetInterfaceParams &params, const bNode &src_node, bNodeTree &dst_tree)
static const bNodeSocket & find_socket_to_use_for_interface(const bNodeTree &node_tree, const bNodeSocket &socket)
static void replace_interface_socket(bContext &C, bNodeTree &dst_tree, const bNodeTreeInterfaceSocket &io_socket, const Span< MutableNodeAndSocket > incoming_links, const Span< MutableNodeAndSocket > outgoing_links, const bNode *group_node, InterfaceProxyNodes &interface_proxies, float2 &input_location, float2 &output_location, UniqueLinkSet &unique_links, Vector< AnimationBasePathChange > &anim_basepaths_for_dst_tree, Vector< AnimationBasePathChange > &anim_basepaths_for_group_tree)
static SocketLinkMap get_links_by_to_socket(bNodeTree &tree)
FunctionRef< bool(const bNode &)> NodeFilterFn
void update_nested_node_refs_after_ungroup(bNodeTree &dst_tree, const bNode &group_node, const NodeSetCopy &node_set_copy)
static std::pair< std::optional< MutableNodeAndSocket >, std::optional< MutableNodeAndSocket > > find_proxy_node_sockets(bNode &proxy_node)
static void map_panel(NodeTreeInterfaceMapping &io_mapping, const NodeSetInterfaceParams &, const bNode &group_node, const bNodeTreeInterfacePanel &io_panel)
void connect_group_node_to_external_sockets(bNode &group_node, const NodeTreeInterfaceMapping &io_mapping)
static bool socket_types_need_conversion(const StringRef from_type, const StringRef to_type)
static bNode * create_proxy_input_node(const bNodeTreeInterfaceSocket &io_socket, const bNodeTree &src_tree, const bNodeSocket &src_socket, bContext &C, bNodeTree &dst_tree, Vector< AnimationBasePathChange > &anim_basepaths)
static std::string node_basepath(const bNodeTree &tree, const bNode &node)
static Vector< MutableNodeAndSocket > get_socket_links(const bNodeSocket &socket, const bool skip_hidden, NodeFilterFn link_filter=default_link_filter)
static bool default_link_filter(const bNode &)
NodeTreeInterfaceMapping map_group_node_interface(const NodeSetInterfaceParams &params, const bNodeTree &tree, const bNode &group_node)
static bNodeLink & add_link_and_make_visible(bNodeTree &tree, const SocketLinkMap &links_by_to_socket, bNode &from_node, bNodeSocket &from_socket, bNode &to_node, bNodeSocket &to_socket, const bool muted, std::optional< int > multi_input_sort_id)
static void remap_pairing(bNodeTree &dst_tree, Span< bNode * > nodes, const Map< int32_t, int32_t > &identifier_map)
std::optional< Bounds< float2 > > node_location_bounds(Span< const bNode * > nodes)
static void ensure_separate_actions(Main &bmain, const bNodeTree &src, bNodeTree &dst)
static void map_socket(NodeTreeInterfaceMapping &io_mapping, const NodeSetInterfaceParams &params, const bNode &group_node, const bNodeTreeInterfaceSocket &io_socket)
static bNodeTreeInterfaceSocket * add_interface_from_socket(const bNodeTree &original_tree, const bNodeSocket &socket, const eNodeSocketInOut in_out, bNodeTree &tree_for_interface, bNodeTreeInterfacePanel *parent)
std::optional< Bounds< float2 > > node_bounds(Span< const bNode * > nodes)
void update_nested_node_refs_after_moving_nodes_into_group(bNodeTree &src_tree, const bNode &group_node, const NodeSetCopy &node_set_copy)
float nearest_node_grid_coord(float co)
Definition node_edit.cc:516
InterfaceProxyNodes connect_copied_nodes_to_external_sockets(bContext &C, const bNodeTree &src_tree, const NodeSetCopy &copied_nodes, const NodeTreeInterfaceMapping &io_mapping, const bNode *group_node)
void update_node_declaration_and_sockets(bNodeTree &ntree, bNode &node, Main *bmain)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
char * STRNCPY(char(&dst)[N], const char *src)
bNodeSocket * node_group_output_find_socket(bNode *node, const StringRef identifier)
std::optional< std::string > RNA_path_from_ID_to_struct(const PointerRNA *ptr)
Definition rna_path.cc:1078
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
@ LIB_ID_COPY_DEFAULT
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
VecBase< float, 2 > float2
BMesh const char void * data
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)
void BKE_animdata_copy_by_basepath(Main &bmain, const ID &src_id, ID &dst_id, Span< AnimationBasePathChange > basepaths)
Definition anim_data.cc:640
Main * CTX_data_main(const bContext *C)
signed int int32_t
Definition BKE_text.h:78
void DEG_relations_tag_update(Main *bmain)
bNodeSocket * node_group_input_find_socket(bNode *node, const StringRef identifier)
@ NODE_PANEL_COLLAPSED
void uninitialized_copy_n(const T *src, int64_t n, T *dst)
const bNodeSocket * find_socket_in_node(const bNode &other_node) const
std::optional< int > multi_input_sort_id
const bNodeSocket * find_socket_in_node(const bNode &other_node) const
std::string socket_identifier
eNodeSocketInOut in_out
eNodeSocketInOut in_out
NodeSocketInterfaceStructureType structure_type
ListBaseT< bNodeLink > links
bNodeTreeInterface tree_interface
bNestedNodeRef * nested_node_refs
ListBaseT< bNode > nodes
ListBaseT< bNodeSocket > outputs
ListBaseT< bNodeSocket > inputs
Defines a socket type.
Definition BKE_node.hh:163
eNodeSocketDatatype type
Definition BKE_node.hh:198
NestedNodeRefIDGenerator(const Span< bNestedNodeRef > used_nested_node_refs)