Blender V5.0
node_geo_foreach_geometry_element.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "BLI_string_utf8.h"
8
9#include "BLO_read_write.hh"
10
11#include "RNA_access.hh"
12#include "RNA_prototypes.hh"
13
20
22#include "UI_resources.hh"
23
24#include "BKE_library.hh"
25#include "BKE_screen.hh"
26
27#include "WM_api.hh"
28
29#include "fmt/core.h"
30
32
34static void node_layout_ex(uiLayout *layout, bContext *C, PointerRNA *current_node_ptr)
35{
36 bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(current_node_ptr->owner_id);
37 bNode *current_node = static_cast<bNode *>(current_node_ptr->data);
38
39 const bke::bNodeTreeZones *zones = ntree.zones();
40 if (!zones) {
41 return;
42 }
43 const bke::bNodeTreeZone *zone = zones->get_zone_by_node(current_node->identifier);
44 if (!zone) {
45 return;
46 }
47 if (!zone->output_node_id) {
48 return;
49 }
50 const bool is_zone_input_node = current_node->type_legacy ==
52 bNode &output_node = const_cast<bNode &>(*zone->output_node());
54 current_node_ptr->owner_id, &RNA_Node, &output_node);
55 auto &storage = *static_cast<NodeGeometryForeachGeometryElementOutput *>(output_node.storage);
56
57 if (is_zone_input_node) {
58 if (uiLayout *panel = layout->panel(C, "input", false, IFACE_("Input Fields"))) {
60 C, panel, ntree, output_node);
62 ntree, output_node, [&](PointerRNA *item_ptr) {
63 panel->use_property_split_set(true);
64 panel->use_property_decorate_set(false);
65 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
66 });
67 }
68 }
69 else {
70 if (uiLayout *panel = layout->panel(C, "main_items", false, IFACE_("Main Geometry"))) {
72 C, panel, ntree, output_node);
74 ntree, output_node, [&](PointerRNA *item_ptr) {
75 panel->use_property_split_set(true);
76 panel->use_property_decorate_set(false);
77 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
78 });
79 }
80 if (uiLayout *panel = layout->panel(
81 C, "generation_items", false, IFACE_("Generated Geometry")))
82 {
86 ntree, output_node, [&](PointerRNA *item_ptr) {
88 storage.generation_items.items[storage.generation_items.active_index];
89 panel->use_property_split_set(true);
90 panel->use_property_decorate_set(false);
91 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
92 if (active_item.socket_type != SOCK_GEOMETRY) {
93 panel->prop(item_ptr, "domain", UI_ITEM_NONE, std::nullopt, ICON_NONE);
94 }
95 });
96 }
97 }
98
99 layout->prop(&output_node_ptr, "inspection_index", UI_ITEM_NONE, std::nullopt, ICON_NONE);
100}
101
102namespace input_node {
103
105
107{
108 b.use_custom_socket_order();
109 b.allow_any_socket_order();
110 const bNode *node = b.node_or_null();
111 const bNodeTree *tree = b.tree_or_null();
112
113 b.add_default_layout();
114
115 if (!node || !tree) {
116 return;
117 }
118
119 const NodeGeometryForeachGeometryElementInput &storage = node_storage(*node);
120 const bNode *output_node = tree->node_by_id(storage.output_node_id);
121 const auto &output_storage = output_node ?
122 static_cast<const NodeGeometryForeachGeometryElementOutput *>(
123 output_node->storage) :
124 nullptr;
125
126 b.add_output<decl::Int>("Index").description(
127 "Index of the element in the source geometry. Note that the same index can occur more than "
128 "once when iterating over multiple components at once");
129
130 b.add_output<decl::Geometry>("Element")
131 .description(
132 "Single-element geometry for the current iteration. Note that it can be quite "
133 "inefficient to split up large geometries into many small geometries")
134 .propagate_all()
135 .available(output_storage && AttrDomain(output_storage->domain) != AttrDomain::Corner);
136
137 b.add_input<decl::Geometry>("Geometry").description("Geometry whose elements are iterated over");
138
139 b.add_input<decl::Bool>("Selection")
140 .default_value(true)
141 .hide_value()
142 .field_on_all()
143 .description("Selection on the iteration domain");
144
145 if (output_storage) {
146 for (const int i : IndexRange(output_storage->input_items.items_num)) {
147 const NodeForeachGeometryElementInputItem &item = output_storage->input_items.items[i];
148 const eNodeSocketDatatype socket_type = eNodeSocketDatatype(item.socket_type);
149 const StringRef name = item.name ? item.name : "";
150 const std::string identifier =
152 b.add_input(socket_type, name, identifier)
153 .socket_name_ptr(
155 .description("Field that is evaluated on the iteration domain")
156 .field_on_all();
157 b.add_output(socket_type, name, identifier)
158 .align_with_previous()
159 .description("Evaluated field value for the current element");
160 }
161 }
162
163 b.add_input<decl::Extend>("", "__extend__").structure_type(StructureType::Dynamic);
164 b.add_output<decl::Extend>("", "__extend__")
165 .structure_type(StructureType::Dynamic)
166 .align_with_previous();
167}
168
169static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
170{
171 bNodeTree &tree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
172 bNode &node = *static_cast<bNode *>(ptr->data);
173 const NodeGeometryForeachGeometryElementInput &storage = node_storage(node);
174 bNode *output_node = tree.node_by_id(storage.output_node_id);
175
176 PointerRNA output_node_ptr = RNA_pointer_create_discrete(ptr->owner_id, &RNA_Node, output_node);
177 layout->prop(&output_node_ptr, "domain", UI_ITEM_NONE, "", ICON_NONE);
178}
179
180static void node_init(bNodeTree * /*tree*/, bNode *node)
181{
184 /* Needs to be initialized for the node to work. */
185 data->output_node_id = 0;
186 node->storage = data;
187}
188
189static void node_label(const bNodeTree * /*ntree*/,
190 const bNode * /*node*/,
191 char *label,
192 const int label_maxncpy)
193{
195 label, CTX_IFACE_(BLT_I18NCONTEXT_ID_NODETREE, "For Each Element"), label_maxncpy);
196}
197
199{
200 bNode *output_node = params.ntree.node_by_id(node_storage(params.node).output_node_id);
201 if (!output_node) {
202 return true;
203 }
206 params.ntree, params.node, *output_node, params.link);
207}
208
209static void node_register()
210{
211 static blender::bke::bNodeType ntype;
213 &ntype, "GeometryNodeForeachGeometryElementInput", GEO_NODE_FOREACH_GEOMETRY_ELEMENT_INPUT);
214 ntype.ui_name = "For Each Geometry Element Input";
215 ntype.enum_name_legacy = "FOREACH_GEOMETRY_ELEMENT_INPUT";
217 ntype.initfunc = node_init;
218 ntype.declare = node_declare;
221 ntype.labelfunc = node_label;
223 ntype.gather_link_search_ops = nullptr;
224 ntype.no_muting = true;
226 "NodeGeometryForeachGeometryElementInput",
230}
232
233} // namespace input_node
234
235namespace output_node {
236
238
240{
241 b.use_custom_socket_order();
242 b.allow_any_socket_order();
243
244 b.add_output<decl::Geometry>("Geometry")
245 .description(
246 "The original input geometry with potentially new attributes that are output by the "
247 "zone");
248
249 const bNode *node = b.node_or_null();
250 const bNodeTree *tree = b.tree_or_null();
251 if (node && tree) {
252 const NodeGeometryForeachGeometryElementOutput &storage = node_storage(*node);
253 for (const int i : IndexRange(storage.main_items.items_num)) {
255 const eNodeSocketDatatype socket_type = eNodeSocketDatatype(item.socket_type);
256 const StringRef name = item.name ? item.name : "";
258 item);
259 b.add_input(socket_type, name, identifier)
260 .socket_name_ptr(
262 .description(
263 "Attribute value that will be stored for the current element on the main geometry");
264 b.add_output(socket_type, name, identifier)
265 .align_with_previous()
266 .field_on({0})
267 .description("Attribute on the geometry above");
268 }
269 b.add_input<decl::Extend>("", "__extend__main");
270 b.add_output<decl::Extend>("", "__extend__main").align_with_previous();
271
272 auto &panel = b.add_panel("Generated");
273
274 int previous_output_geometry_index = -1;
275 int previous_input_geometry_index = -1;
276 for (const int i : IndexRange(storage.generation_items.items_num)) {
278 const eNodeSocketDatatype socket_type = eNodeSocketDatatype(item.socket_type);
279 if (socket_type == SOCK_GEOMETRY && i > 0) {
280 panel.add_separator();
281 }
282 const StringRef name = item.name ? item.name : "";
283 std::string identifier =
285 auto &input_decl = panel.add_input(socket_type, name, identifier)
286 .socket_name_ptr(
287 &tree->id,
289 &item,
290 "name");
291 auto &output_decl = panel.add_output(socket_type, name, identifier).align_with_previous();
292 if (socket_type == SOCK_GEOMETRY) {
293 previous_input_geometry_index = input_decl.index();
294 previous_output_geometry_index = output_decl.index();
295
296 input_decl.description(
297 "Geometry generated in the current iteration. Will be joined with geometries from all "
298 "other iterations");
299 output_decl.description("Result of joining generated geometries from each iteration");
300 }
301 else {
302 if (previous_output_geometry_index > 0) {
303 input_decl.description("Field that will be stored as attribute on the geometry above");
304 input_decl.field_on({previous_input_geometry_index});
305 output_decl.field_on({previous_output_geometry_index});
306 }
307 output_decl.description("Attribute on the geometry above");
308 }
309 }
310 panel.add_input<decl::Extend>("", "__extend__generation");
311 panel.add_output<decl::Extend>("", "__extend__generation").align_with_previous();
312 }
313}
314
315static void node_init(bNodeTree * /*tree*/, bNode *node)
316{
319
321 1, __func__);
322 NodeForeachGeometryElementGenerationItem &item = data->generation_items.items[0];
323 item.name = BLI_strdup(DATA_("Geometry"));
325 item.identifier = data->generation_items.next_identifier++;
326 data->generation_items.items_num = 1;
327
328 node->storage = data;
329}
330
338
339static void node_copy_storage(bNodeTree * /*dst_tree*/, bNode *dst_node, const bNode *src_node)
340{
341 const NodeGeometryForeachGeometryElementOutput &src_storage = node_storage(*src_node);
343 src_storage);
344 dst_node->storage = dst_storage;
345
349}
350
362
369
371{
372 const NodeGeometryForeachGeometryElementOutput &storage = node_storage(params.node);
373 if (storage.generation_items.items_num > 0) {
376 row.text = RPT_("Missing Geometry");
377 row.tooltip = TIP_("Each output field has to correspond to a geometry that is above it");
378 row.icon = ICON_ERROR;
379 params.rows.append(std::move(row));
380 }
381 }
382}
383
384static std::pair<bNode *, bNode *> add_foreach_zone(LinkSearchOpParams &params)
385{
386 bNode &input_node = params.add_node("GeometryNodeForeachGeometryElementInput");
387 bNode &output_node = params.add_node("GeometryNodeForeachGeometryElementOutput");
388 output_node.location[0] = 300;
389
390 auto &input_storage = *static_cast<NodeGeometryForeachGeometryElementInput *>(
391 input_node.storage);
392 input_storage.output_node_id = output_node.identifier;
393
394 return {&input_node, &output_node};
395}
396
398{
399 const bNodeSocket &other_socket = params.other_socket();
400 const eNodeSocketDatatype type = eNodeSocketDatatype(other_socket.type);
401 if (type != SOCK_GEOMETRY) {
402 return;
403 }
404 if (other_socket.in_out == SOCK_OUT) {
405 params.add_item_full_name(IFACE_("For Each Element"), [](LinkSearchOpParams &params) {
407 params.update_and_connect_available_socket(*input_node, "Geometry");
408 });
409 }
410 else {
411 params.add_item_full_name(
412 fmt::format(IFACE_("For Each Element {} Main"), UI_MENU_ARROW_SEP),
416 params.update_and_connect_available_socket(*output_node, "Geometry");
417 });
418
419 params.add_item_full_name(
420 fmt::format(IFACE_("For Each Element {} Generated"), UI_MENU_ARROW_SEP),
423 params.node_tree.ensure_topology_cache();
424 bke::node_add_link(params.node_tree,
426 output_node->output_socket(2),
427 params.node,
428 params.socket);
429 });
430 }
431}
432
439
446
447static void node_register()
448{
449 static blender::bke::bNodeType ntype;
450 geo_node_type_base(&ntype,
451 "GeometryNodeForeachGeometryElementOutput",
453 ntype.ui_name = "For Each Geometry Element Output";
454 ntype.enum_name_legacy = "FOREACH_GEOMETRY_ELEMENT_OUTPUT";
456 ntype.initfunc = node_init;
457 ntype.declare = node_declare;
464 ntype.no_muting = true;
468 ntype, "NodeGeometryForeachGeometryElementOutput", node_free_storage, node_copy_storage);
470}
472
473} // namespace output_node
474
475} // namespace blender::nodes::node_geo_foreach_geometry_element_cc
476
477namespace blender::nodes {
478
480 &RNA_ForeachGeometryElementInputItem;
481
487
493
495 &RNA_ForeachGeometryElementMainItem;
496
498 const ItemT &item)
499{
500 BLO_write_string(writer, item.name);
501}
502
508
510 &RNA_ForeachGeometryElementGenerationItem;
511
517
523
524} // namespace blender::nodes
#define NODE_CLASS_INTERFACE
Definition BKE_node.hh:459
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1240
#define GEO_NODE_FOREACH_GEOMETRY_ELEMENT_INPUT
#define GEO_NODE_FOREACH_GEOMETRY_ELEMENT_OUTPUT
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
Definition string.cc:41
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:5828
void BLO_write_string(BlendWriter *writer, const char *data_ptr)
#define RPT_(msgid)
#define BLT_I18NCONTEXT_ID_NODETREE
#define TIP_(msgid)
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define DATA_(msgid)
@ SOCK_OUT
eNodeSocketDatatype
@ SOCK_GEOMETRY
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
BMesh const char void * data
std::optional< int > output_node_id
const bNode * output_node() const
const bNodeTreeZone * get_zone_by_node(const int32_t node_id) const
KDTree_3d * tree
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void * MEM_calloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:123
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:3810
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:5414
static void node_label(const bNodeTree *, const bNode *, char *label, const int label_maxncpy)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_copy_storage(bNodeTree *, bNode *dst_node, const bNode *src_node)
static void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
static std::pair< bNode *, bNode * > add_foreach_zone(LinkSearchOpParams &params)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
static void node_layout_ex(uiLayout *layout, bContext *C, PointerRNA *current_node_ptr)
static void draw_items_list_with_operators(const bContext *C, uiLayout *layout, const bNodeTree &tree, const bNode &node)
static void draw_active_item_props(const bNodeTree &tree, const bNode &node, const FunctionRef< void(PointerRNA *item_ptr)> draw_item)
void blend_write(BlendWriter *writer, const bNode &node)
void blend_read_data(BlendDataReader *reader, bNode &node)
void copy_array(const bNode &src_node, bNode &dst_node)
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)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:42
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:54
const char * name
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
#define UI_MENU_ARROW_SEP
NodeForeachGeometryElementGenerationItem * items
NodeForeachGeometryElementMainItem * items
NodeForeachGeometryElementMainItems main_items
NodeForeachGeometryElementGenerationItems generation_items
ID * owner_id
Definition RNA_types.hh:51
void * data
Definition RNA_types.hh:53
int16_t type_legacy
void * storage
int32_t identifier
Defines a node type.
Definition BKE_node.hh:238
NodeBlendWriteFunction blend_write_storage_content
Definition BKE_node.hh:390
NodeBlendDataReadFunction blend_data_read_storage_content
Definition BKE_node.hh:391
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
void(* labelfunc)(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:270
void(* draw_buttons_ex)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:261
NodeExtraInfoFunction get_extra_info
Definition BKE_node.hh:381
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
bool(* insert_link)(NodeInsertLinkParams &params)
Definition BKE_node.hh:333
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:378
NodeDeclareFunction declare
Definition BKE_node.hh:362
void(* register_operators)()
Definition BKE_node.hh:417
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
static void blend_write_item(BlendWriter *writer, const ItemT &item)
static void blend_write_item(BlendWriter *writer, const ItemT &item)
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
PanelLayout panel(const bContext *C, blender::StringRef idname, bool default_closed)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
i
Definition text_draw.cc:230
PointerRNA * ptr
Definition wm_files.cc:4238