Blender V5.3
socket_search_link.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 <fmt/format.h>
10
11#include "BLI_listbase.hh"
12#include "BLI_set.hh"
13
14#include "BKE_context.hh"
15#include "BKE_node.hh"
16
17#include "UI_interface.hh"
18
19#include "BLT_translation.hh"
20
22#include "NOD_socket.hh"
24
25namespace blender::nodes {
26
27void GatherLinkSearchOpParams::add_item(std::string socket_name,
29 const int weight)
30{
31 std::string name = fmt::format("{}{} " UI_MENU_ARROW_SEP " {}",
32 IFACE_(node_type_.ui_name),
33 node_type_.deprecation_notice ? IFACE_(" (Deprecated)") : "",
34 socket_name);
35 this->add_item_full_name(std::move(name), std::move(fn), weight);
36}
37
40 int weight)
41{
42 items_.append({std::move(name), std::move(fn), weight});
43}
44
46{
47 return other_socket_;
48}
49
51{
52 return snode_;
53}
54
56{
57 return node_tree_;
58}
59
61{
62 return node_type_;
63}
64
66{
67 return other_socket_.in_out == SOCK_IN ? SOCK_OUT : SOCK_IN;
68}
69
71{
74 new_node, in_out, socket_name.ref());
75 if (new_node_socket == nullptr) {
76 /* If the socket isn't found, some node's search gather functions probably aren't configured
77 * properly. It's likely enough that it's worth avoiding a crash in a release build though. */
79 return;
80 }
81 this->connect_socket(new_node, *new_node_socket);
82}
83
85 const UString socket_identifier)
86{
87 const eNodeSocketInOut in_out = this->socket.in_out == SOCK_IN ? SOCK_OUT : SOCK_IN;
88 bNodeSocket *new_node_socket = bke::node_find_socket(new_node, in_out, socket_identifier);
89 BLI_assert(new_node_socket);
90 this->connect_socket(new_node, *new_node_socket);
91}
92
94{
95 bke::node_add_link(this->node_tree, new_node, new_socket, this->node, this->socket);
96 if (new_socket.in_out == SOCK_OUT) {
97 /* If the old socket already contained a value, then transfer it to a new one, from
98 * which this value will get there. */
100 *CTX_data_main(&C), this->node_tree, this->socket, new_socket);
101 }
102}
103
105{
107 BLI_assert(node != nullptr);
108 added_nodes_.append(node);
109 return *node;
110}
111
113{
114 return this->add_node(node_type.idname);
115}
116
118 bNode &new_node, UString socket_identifier)
119{
121 if (new_node.typeinfo->updatefunc) {
122 new_node.typeinfo->updatefunc(&node_tree, &new_node);
123 }
124 this->connect_available_socket_by_identifier(new_node, socket_identifier);
125}
126
128{
130 if (new_node.typeinfo->updatefunc) {
131 new_node.typeinfo->updatefunc(&node_tree, &new_node);
132 }
133 this->connect_available_socket(new_node, socket_name);
134}
135
137 Span<SocketDeclaration *> declarations)
138{
139 const bke::bNodeType &node_type = params.node_type();
140
141 const SocketDeclaration *main_socket = nullptr;
142 Vector<const SocketDeclaration *> connectable_sockets;
143
144 Set<UString> socket_names;
145 for (const int i : declarations.index_range()) {
146 const SocketDeclaration &socket = *declarations[i];
147 if (!socket_names.add(socket.name)) {
148 /* Don't add sockets with the same name to the search. Needed to support being called from
149 * #search_link_ops_for_basic_node, which should have "okay" behavior for nodes with
150 * duplicate socket names. */
151 continue;
152 }
153 if (!socket.can_connect(params.other_socket())) {
154 continue;
155 }
156 if (socket.is_default_link_socket || main_socket == nullptr) {
157 /* Either the first connectable or explicitly tagged socket is the main socket. */
158 main_socket = &socket;
159 }
160 connectable_sockets.append(&socket);
161 }
162 for (const int i : connectable_sockets.index_range()) {
163 const SocketDeclaration &socket = *connectable_sockets[i];
164 /* Give non-main sockets a lower weight so that they don't show up at the top of the search
165 * when they are not explicitly searched for. The -1 is used to make sure that the first socket
166 * has a smaller weight than zero so that it does not have the same weight as the main socket.
167 * Negative weights are used to avoid making the highest weight dependent on the number of
168 * sockets. */
169 const int weight = (&socket == main_socket) ? 0 : -1 - i;
170 params.add_item(
171 IFACE_(socket.name.ref()),
172 [&node_type, &socket](LinkSearchOpParams &params) {
173 bNode &node = params.add_node(node_type);
174 socket.make_available(node);
175 params.update_and_connect_available_socket(node, socket.name);
176 },
177 weight);
178 }
179}
180
182{
183 const bke::bNodeType &node_type = params.node_type();
184 if (!node_type.static_declaration) {
185 return;
186 }
187 const NodeDeclaration &declaration = *node_type.static_declaration;
188 search_link_ops_for_declarations(params, declaration.sockets(params.in_out()));
189}
190
192 const Set<UString> &skip_socket_identifiers)
193{
194 const bke::bNodeType &node_type = params.node_type();
195 if (!node_type.static_declaration) {
196 return;
197 }
198 const NodeDeclaration &declaration = *node_type.static_declaration;
199 Vector<SocketDeclaration *> socket_declarations;
200 socket_declarations.reserve(declaration.sockets(params.in_out()).size());
201 for (SocketDeclaration *socket_decl : declaration.sockets(params.in_out())) {
202 if (skip_socket_identifiers.contains(socket_decl->identifier)) {
203 continue;
204 }
205 socket_declarations.append_unchecked(socket_decl);
206 }
207 search_link_ops_for_declarations(params, socket_declarations);
208}
209
210} // namespace blender::nodes
#define IFACE_(msgid)
#define BLI_assert_unreachable()
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
StringRefNull ref() const
void append(const T &value)
IndexRange index_range() const
void append_unchecked(const T &value)
void reserve(const int64_t min_capacity)
const bke::bNodeType & node_type() const
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void add_item_full_name(std::string name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void connect_available_socket_by_identifier(bNode &new_node, UString socket_identifier)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
void connect_socket(bNode &new_node, bNodeSocket &new_socket)
void connect_available_socket(bNode &new_node, UString socket_name)
void update_and_connect_available_socket_by_identifier(bNode &new_node, UString socket_identifier)
Span< SocketDeclaration * > sockets(eNodeSocketInOut in_out) const
virtual bool can_connect(const bNodeSocket &socket) const =0
bNode * node_add_node(const bContext *C, bNodeTree &ntree, UString idname, std::optional< int > unique_identifier=std::nullopt)
Definition node.cc:4260
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4638
bNodeSocket * node_find_enabled_socket(bNode &node, eNodeSocketInOut in_out, StringRef name)
Definition node.cc:3055
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
void node_socket_move_default_value(Main &bmain, bNodeTree &tree, bNodeSocket &src, bNodeSocket &dst)
Definition node.cc:4534
void search_link_ops_for_basic_node(GatherLinkSearchOpParams &params)
void search_link_ops_for_declarations(GatherLinkSearchOpParams &params, Span< SocketDeclaration * > declarations)
void search_filtered_link_ops_for_basic_node(GatherLinkSearchOpParams &params, const Set< UString > &skip_socket_identifiers)
void update_node_declaration_and_sockets(bNodeTree &ntree, bNode &node, Main *bmain)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
Main * CTX_data_main(const bContext *C)
#define UI_MENU_ARROW_SEP
eNodeSocketInOut in_out
bke::bNodeType * typeinfo
Defines a node type.
Definition BKE_node.hh:265
void(*) updatefunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:312
nodes::NodeDeclaration * static_declaration
Definition BKE_node.hh:402
std::function< void(LinkSearchOpParams &link_params)> LinkSocketFn