Blender V5.3
NOD_socket_items.hh
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#pragma once
10
23
24#include <optional>
25
26#include "BLI_string.hh"
27#include "BLI_string_utils.hh"
28
29#include "BKE_node.hh"
30#include "BKE_node_runtime.hh"
32
33#include "DNA_array_utils.hh"
34
35#include "NOD_socket.hh"
36
38
40 static constexpr bool has_single_identifier_str = true;
41 static constexpr bool has_name_validation = false;
42 static constexpr bool has_custom_initial_name = false;
43 static constexpr bool has_vector_dimensions = false;
44 static constexpr bool can_have_empty_name = false;
45 static constexpr char unique_name_separator = '.';
46};
47
52template<typename T> struct SocketItemsRef {
56};
57
61template<typename Accessor>
62inline bNode *find_node_by_item(bNodeTree &ntree, const typename Accessor::ItemT &item)
63{
64 ntree.ensure_topology_cache();
65 for (bNode *node : ntree.nodes_by_type(UString(Accessor::node_idname))) {
66 SocketItemsRef array = Accessor::get_items_from_node(*node);
67 if (&item >= *array.items && &item < *array.items + *array.items_num) {
68 return node;
69 }
70 }
71 return nullptr;
72}
73
75template<typename Accessor>
76inline typename Accessor::ItemT *find_item_by_identifier(bNode &node, const StringRef identifier)
77{
78
79 SocketItemsRef array = Accessor::get_items_from_node(node);
80 for (const int i : IndexRange(*array.items_num)) {
81 typename Accessor::ItemT &item = (*array.items)[i];
82 if (Accessor::socket_identifier_for_item(item) == identifier) {
83 return &item;
84 }
85 }
86 return nullptr;
87}
88
92template<typename Accessor> inline void destruct_array(bNode &node)
93{
94 using ItemT = typename Accessor::ItemT;
95 SocketItemsRef ref = Accessor::get_items_from_node(node);
96 for (const int i : IndexRange(*ref.items_num)) {
97 ItemT &item = (*ref.items)[i];
98 Accessor::destruct_item(&item);
99 }
100 MEM_SAFE_DELETE(*ref.items);
101}
102
106template<typename Accessor> inline void clear(bNode &node)
107{
109 SocketItemsRef ref = Accessor::get_items_from_node(node);
110 *ref.items_num = 0;
111 if (ref.active_index) {
112 *ref.active_index = 0;
113 }
114}
115
119template<typename Accessor> inline void copy_array(const bNode &src_node, bNode &dst_node)
120{
121 using ItemT = typename Accessor::ItemT;
122 SocketItemsRef src_ref = Accessor::get_items_from_node(const_cast<bNode &>(src_node));
123 SocketItemsRef dst_ref = Accessor::get_items_from_node(dst_node);
124 const int items_num = *src_ref.items_num;
125 *dst_ref.items = MEM_new_array<ItemT>(items_num, __func__);
126 for (const int i : IndexRange(items_num)) {
127 Accessor::copy_item((*src_ref.items)[i], (*dst_ref.items)[i]);
128 }
129}
130
134template<typename Accessor> inline std::string get_validated_name(const StringRef name)
135{
136 if constexpr (Accessor::has_name_validation) {
137 return Accessor::validate_name(name);
138 }
139 else {
140 return name;
141 }
142}
143
148template<typename Accessor>
150 typename Accessor::ItemT &item,
151 const char *value)
152{
153 using ItemT = typename Accessor::ItemT;
154 SocketItemsRef array = Accessor::get_items_from_node(node);
155
156 std::string name = value;
157 if constexpr (!Accessor::can_have_empty_name) {
158 if (name.empty()) {
159 if constexpr (Accessor::has_type) {
160 name = *bke::node_static_socket_label(Accessor::get_socket_type(item), 0);
161 }
162 else {
163 name = "Item";
164 }
165 }
166 }
167
168 const std::string validated_name = get_validated_name<Accessor>(name);
169
170 const std::string unique_name = BLI_uniquename_cb(
171 [&](const StringRef name) {
172 for (ItemT &item_iter : MutableSpan(*array.items, *array.items_num)) {
173 if (&item_iter != &item) {
174 if (*Accessor::get_name(item_iter) == name) {
175 return true;
176 }
177 }
178 }
179 return false;
180 },
181 Accessor::unique_name_separator,
182 validated_name);
183
184 /* The unique name should still be valid. */
185 BLI_assert(unique_name == get_validated_name<Accessor>(unique_name));
186
187 char **item_name = Accessor::get_name(item);
188 MEM_SAFE_DELETE(*item_name);
189 *item_name = BLI_strdup(unique_name.c_str());
190}
191
192namespace detail {
193
194template<typename Accessor> inline typename Accessor::ItemT &add_item_to_array(bNode &node)
195{
196 using ItemT = typename Accessor::ItemT;
197 SocketItemsRef array = Accessor::get_items_from_node(node);
198
199 ItemT *old_items = *array.items;
200 const int old_items_num = *array.items_num;
201 const int new_items_num = old_items_num + 1;
202
203 ItemT *new_items = MEM_new_array<ItemT>(new_items_num, __func__);
204 std::copy_n(old_items, old_items_num, new_items);
205 ItemT &new_item = new_items[old_items_num];
206
207 MEM_SAFE_DELETE(old_items);
208 *array.items = new_items;
209 *array.items_num = new_items_num;
210 if (array.active_index) {
211 *array.active_index = old_items_num;
212 }
213
214 return new_item;
215}
216
217} // namespace detail
218
224template<typename Accessor>
225inline typename Accessor::ItemT *add_item_with_socket_type_and_name(
226 bNodeTree &ntree,
227 bNode &node,
228 const eNodeSocketDatatype socket_type,
229 const char *name,
230 std::optional<int> dimensions = std::nullopt)
231{
232 using ItemT = typename Accessor::ItemT;
233 BLI_assert(Accessor::supports_socket_type(socket_type, ntree.type));
234 BLI_assert(!(dimensions.has_value() && socket_type != SOCK_VECTOR));
235 BLI_assert(ELEM(dimensions.value_or(3), 2, 3, 4));
236 UNUSED_VARS_NDEBUG(ntree);
237 ItemT &new_item = detail::add_item_to_array<Accessor>(node);
238 if constexpr (Accessor::has_vector_dimensions) {
239 Accessor::init_with_socket_type_and_name(node, new_item, socket_type, name, dimensions);
240 }
241 else {
242 Accessor::init_with_socket_type_and_name(node, new_item, socket_type, name);
243 }
244 return &new_item;
245}
246
250template<typename Accessor>
251inline typename Accessor::ItemT *add_item_with_name(bNode &node, const char *name)
252{
253 using ItemT = typename Accessor::ItemT;
254 ItemT &new_item = detail::add_item_to_array<Accessor>(node);
255 Accessor::init_with_name(node, new_item, name);
256 return &new_item;
257}
258
262template<typename Accessor> inline typename Accessor::ItemT *add_item(bNode &node)
263{
264 using ItemT = typename Accessor::ItemT;
265 ItemT &new_item = detail::add_item_to_array<Accessor>(node);
266 Accessor::init(node, new_item);
267 return &new_item;
268}
269
270template<typename Accessor>
271inline std::string get_socket_identifier(const typename Accessor::ItemT &item,
272 const eNodeSocketInOut in_out)
273{
274 if constexpr (Accessor::has_single_identifier_str) {
275 return Accessor::socket_identifier_for_item(item);
276 }
277 else {
278 if (in_out == SOCK_IN) {
279 return Accessor::input_socket_identifier_for_item(item);
280 }
281 return Accessor::output_socket_identifier_for_item(item);
282 }
283}
284
285inline std::optional<eNodeSocketDatatype> get_socket_item_type_to_add(
286 const eNodeSocketDatatype linked_type,
287 const FunctionRef<bool(eNodeSocketDatatype type)> is_supported)
288{
289 if (is_supported(linked_type)) {
290 return linked_type;
291 }
292 if (linked_type == SOCK_RGBA) {
293 if (is_supported(SOCK_VECTOR)) {
294 return SOCK_VECTOR;
295 }
296 }
297 return std::nullopt;
298}
299
305template<typename Accessor>
306[[nodiscard]] inline bool try_add_item_via_extend_socket(
307 bNodeTree &ntree,
308 bNode &extend_node,
309 bNodeSocket &extend_socket,
310 bNode &storage_node,
311 bNodeLink &link,
312 typename Accessor::ItemT **r_new_item = nullptr)
313{
314 using ItemT = typename Accessor::ItemT;
315 bNodeSocket *src_socket = nullptr;
316 if (link.tosock == &extend_socket) {
317 src_socket = link.fromsock;
318 }
319 else if (link.fromsock == &extend_socket) {
320 src_socket = link.tosock;
321 }
322 else {
323 return false;
324 }
325
326 ItemT *item = nullptr;
327 if constexpr (Accessor::has_name && Accessor::has_type) {
328 const eNodeSocketDatatype src_socket_type = src_socket->type;
329 const std::optional<eNodeSocketDatatype> added_socket_type = get_socket_item_type_to_add(
330 src_socket_type, [&](const eNodeSocketDatatype type) {
331 return Accessor::supports_socket_type(type, ntree.type);
332 });
333 if (!added_socket_type) {
334 return false;
335 }
336 /* Ensure the source node declaration is up-to-date before capturing the socket label.
337 * This is necessary to correctly capture dynamic labels at creation time. */
338 ntree.ensure_topology_cache();
339 blender::bke::node_declaration_ensure(src_socket->owner_tree(), src_socket->owner_node());
340 std::string name = blender::bke::node_socket_label(*src_socket);
341
342 std::optional<int> dimensions = std::nullopt;
343 if (src_socket_type == SOCK_VECTOR && added_socket_type == SOCK_VECTOR) {
344 dimensions = src_socket->default_value_typed<bNodeSocketValueVector>()->dimensions;
345 }
346
347 if constexpr (Accessor::has_custom_initial_name) {
348 name = Accessor::custom_initial_name(storage_node, name);
349 }
350
352 ntree, storage_node, *added_socket_type, name.c_str(), dimensions);
353 }
354 else if constexpr (Accessor::has_name && !Accessor::has_type) {
355 item = add_item_with_name<Accessor>(storage_node, src_socket->name);
356 }
357 else {
358 item = add_item<Accessor>(storage_node);
359 }
360 if (item == nullptr) {
361 return false;
362 }
363 if (r_new_item) {
364 *r_new_item = item;
365 }
366
367 update_node_declaration_and_sockets(ntree, extend_node);
368 if (extend_socket.is_input()) {
369 const std::string item_identifier = get_socket_identifier<Accessor>(*item, SOCK_IN);
371 extend_node, SOCK_IN, UString(item_identifier));
372 link.tosock = new_socket;
373 }
374 else {
375 const std::string item_identifier = get_socket_identifier<Accessor>(*item, SOCK_OUT);
377 extend_node, SOCK_OUT, UString(item_identifier.c_str()));
378 link.fromsock = new_socket;
379 }
380 BKE_ntree_update_tag_node_property(&ntree, &storage_node);
381 return true;
382}
383
388template<typename Accessor>
389[[nodiscard]] inline bool try_add_item_via_any_extend_socket(
390 bNodeTree &ntree,
391 bNode &extend_node,
392 bNode &storage_node,
393 bNodeLink &link,
394 const std::optional<StringRef> socket_identifier = std::nullopt,
395 typename Accessor::ItemT **r_new_item = nullptr)
396{
397 bNodeSocket *possible_extend_socket = nullptr;
398 if (link.fromnode == &extend_node) {
399 possible_extend_socket = link.fromsock;
400 }
401 if (link.tonode == &extend_node) {
402 possible_extend_socket = link.tosock;
403 }
404 if (possible_extend_socket == nullptr) {
405 return true;
406 }
407 if (!STREQ(possible_extend_socket->idname, "NodeSocketVirtual")) {
408 return true;
409 }
410 if (socket_identifier.has_value()) {
411 if (possible_extend_socket->identifier != socket_identifier) {
412 return true;
413 }
414 }
416 ntree, extend_node, *possible_extend_socket, storage_node, link, r_new_item);
417}
418
419} // namespace blender::nodes::socket_items
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define STREQ(a, b)
constexpr IndexRange()=default
constexpr MutableSpan()=default
UString()=default
#define T
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
std::optional< StringRefNull > node_static_socket_label(int type, int subtype)
Definition node.cc:3923
bool node_declaration_ensure(bNodeTree &ntree, bNode &node)
Definition node.cc:5578
StringRefNull node_socket_label(const bNodeSocket &sock)
Definition node.cc:5758
Accessor::ItemT & add_item_to_array(bNode &node)
std::optional< eNodeSocketDatatype > get_socket_item_type_to_add(const eNodeSocketDatatype linked_type, const FunctionRef< bool(eNodeSocketDatatype type)> is_supported)
void set_item_name_and_make_unique(bNode &node, typename Accessor::ItemT &item, const char *value)
Accessor::ItemT * add_item_with_name(bNode &node, const char *name)
Accessor::ItemT * add_item(bNode &node)
void copy_array(const bNode &src_node, bNode &dst_node)
std::string get_validated_name(const StringRef name)
Accessor::ItemT * find_item_by_identifier(bNode &node, const StringRef identifier)
bNode * find_node_by_item(bNodeTree &ntree, const typename Accessor::ItemT &item)
Accessor::ItemT * add_item_with_socket_type_and_name(bNodeTree &ntree, bNode &node, const eNodeSocketDatatype socket_type, const char *name, std::optional< int > dimensions=std::nullopt)
bool try_add_item_via_extend_socket(bNodeTree &ntree, bNode &extend_node, bNodeSocket &extend_socket, bNode &storage_node, bNodeLink &link, typename Accessor::ItemT **r_new_item=nullptr)
bool try_add_item_via_any_extend_socket(bNodeTree &ntree, bNode &extend_node, bNode &storage_node, bNodeLink &link, const std::optional< StringRef > socket_identifier=std::nullopt, typename Accessor::ItemT **r_new_item=nullptr)
std::string get_socket_identifier(const typename Accessor::ItemT &item, const eNodeSocketInOut in_out)
void update_node_declaration_and_sockets(bNodeTree &ntree, bNode &node, Main *bmain)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
size_t void BLI_uniquename_cb(FunctionRef< bool(StringRefNull)> unique_check, const char *defname, char delim, char *name, size_t name_maxncpy) ATTR_NONNULL(2
void BKE_ntree_update_tag_node_property(bNodeTree *ntree, bNode *node)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
eNodeSocketDatatype type
eNodeTree_Type type