Blender V5.3
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.hh"
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#include "fmt/format.h"
31
32namespace blender {
33
35
37static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *current_node_ptr)
38{
39 bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(current_node_ptr->owner_id);
40 bNode *current_node = static_cast<bNode *>(current_node_ptr->data);
41
42 const bke::bNodeTreeZones *zones = ntree.zones();
43 if (!zones) {
44 return;
45 }
46 const bke::bNodeTreeZone *zone = zones->get_zone_by_node(current_node->identifier);
47 if (!zone) {
48 return;
49 }
50 if (!zone->output_node_id) {
51 return;
52 }
53 const bool is_zone_input_node = current_node->type_legacy ==
55 bNode &output_node = const_cast<bNode &>(*zone->output_node());
57 current_node_ptr->owner_id, RNA_Node, &output_node);
58 auto &storage = *static_cast<NodeGeometryForeachGeometryElementOutput *>(output_node.storage);
59
60 if (is_zone_input_node) {
61 if (ui::Layout *panel = layout.panel(C, "input", false, IFACE_("Input Fields"))) {
62 socket_items::ui::draw_items_list_with_operators<ForeachGeometryElementInputItemsAccessor>(
63 C, panel, ntree, output_node);
64 socket_items::ui::draw_active_item_props<ForeachGeometryElementInputItemsAccessor>(
65 ntree, output_node, [&](PointerRNA *item_ptr) {
66 panel->use_property_split_set(true);
67 panel->use_property_decorate_set(false);
68 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
69 });
70 }
71 }
72 else {
73 if (ui::Layout *panel = layout.panel(C, "main_items", false, IFACE_("Main Geometry"))) {
74 socket_items::ui::draw_items_list_with_operators<ForeachGeometryElementMainItemsAccessor>(
75 C, panel, ntree, output_node);
76 socket_items::ui::draw_active_item_props<ForeachGeometryElementMainItemsAccessor>(
77 ntree, output_node, [&](PointerRNA *item_ptr) {
78 panel->use_property_split_set(true);
79 panel->use_property_decorate_set(false);
80 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
81 });
82 }
83 if (ui::Layout *panel = layout.panel(
84 C, "generation_items", false, IFACE_("Generated Geometry")))
85 {
86 socket_items::ui::draw_items_list_with_operators<
88 socket_items::ui::draw_active_item_props<ForeachGeometryElementGenerationItemsAccessor>(
89 ntree, output_node, [&](PointerRNA *item_ptr) {
91 storage.generation_items.items[storage.generation_items.active_index];
92 panel->use_property_split_set(true);
93 panel->use_property_decorate_set(false);
94 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
95 if (active_item.socket_type != SOCK_GEOMETRY) {
96 panel->prop(item_ptr, "domain", UI_ITEM_NONE, std::nullopt, ICON_NONE);
97 }
98 });
99 }
100 }
101
102 layout.prop(&output_node_ptr, "inspection_index", UI_ITEM_NONE, std::nullopt, ICON_NONE);
103}
104
105namespace input_node {
106
108
110{
111 b.use_custom_socket_order();
112 b.allow_any_socket_order();
113 const bNode *node = b.node_or_null();
114 const bNodeTree *tree = b.tree_or_null();
115
116 b.add_default_layout();
117
118 if (!node || !tree) {
119 return;
120 }
121
123 const bNode *output_node = tree->node_by_id(storage.output_node_id);
124 const auto &output_storage = output_node ?
125 static_cast<const NodeGeometryForeachGeometryElementOutput *>(
126 output_node->storage) :
127 nullptr;
128
129 b.add_output<decl::Int>("Index"_ustr)
130 .description(
131 "Index of the element in the source geometry. Note that the same index can occur more "
132 "than "
133 "once when iterating over multiple components at once");
134
135 b.add_output<decl::Geometry>("Element"_ustr)
136 .description(
137 "Single-element geometry for the current iteration. Note that it can be quite "
138 "inefficient to split up large geometries into many small geometries")
139 .propagate_all_geometry()
140 .available(output_storage && AttrDomain(output_storage->domain) != AttrDomain::Corner);
141
142 b.add_input<decl::Geometry>("Geometry"_ustr)
143 .description("Geometry whose elements are iterated over");
144
145 b.add_input<decl::Bool>("Selection"_ustr)
146 .default_value(true)
147 .hide_value()
148 .evaluated_geometry_field()
149 .description("Selection on the iteration domain");
150
151 if (output_storage) {
152 for (const int i : IndexRange(output_storage->input_items.items_num)) {
153 const NodeForeachGeometryElementInputItem &item = output_storage->input_items.items[i];
154 const eNodeSocketDatatype socket_type = item.socket_type;
155 const UString name = item.name ? UString(item.name) : ""_ustr;
156 const UString identifier(
158 b.add_input(socket_type, name, identifier)
159 .socket_name_ptr(
161 .description("Field that is evaluated on the iteration domain")
162 .evaluated_geometry_field();
163 b.add_output(socket_type, name, identifier)
164 .align_with_previous()
165 .description("Evaluated field value for the current element");
166 }
167 }
168
169 b.add_input<decl::Extend>(""_ustr, "__extend__"_ustr)
170 .structure_type(StructureType::Dynamic)
171 .custom_draw(
172 socket_items::ui::draw_extend_socket_fn<ForeachGeometryElementInputItemsAccessor>());
173 b.add_output<decl::Extend>(""_ustr, "__extend__"_ustr)
174 .structure_type(StructureType::Dynamic)
175 .align_with_previous();
176}
177
178static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
179{
180 bNodeTree &tree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
181 bNode &node = *static_cast<bNode *>(ptr->data);
183 bNode *output_node = tree.node_by_id(storage.output_node_id);
184
185 PointerRNA output_node_ptr = RNA_pointer_create_discrete(ptr->owner_id, RNA_Node, output_node);
186 layout.prop(&output_node_ptr, "domain", UI_ITEM_NONE, "", ICON_NONE);
187}
188
189static void node_init(bNodeTree * /*tree*/, bNode *node)
190{
191 NodeGeometryForeachGeometryElementInput *data = MEM_new<NodeGeometryForeachGeometryElementInput>(
192 __func__);
193 /* Needs to be initialized for the node to work. */
194 data->output_node_id = 0;
195 node->storage = data;
196}
197
198static void node_label(const bNodeTree * /*ntree*/,
199 const bNode * /*node*/,
200 char *label,
201 const int label_maxncpy)
202{
204 label, CTX_IFACE_(BLT_I18NCONTEXT_ID_NODETREE, "For Each Element"), label_maxncpy);
205}
206
208{
209 bNode *output_node = params.ntree.node_by_id(node_storage(params.node).output_node_id);
210 if (!output_node) {
211 return true;
212 }
215 params.ntree, params.node, *output_node, params.link);
216}
217
218static void node_register()
219{
220 static bke::bNodeType ntype;
221 geo_node_type_base(&ntype,
222 "GeometryNodeForeachGeometryElementInput"_ustr,
224 ntype.ui_name = "For Each Geometry Element Input";
225 ntype.enum_name_legacy = "FOREACH_GEOMETRY_ELEMENT_INPUT";
227 ntype.initfunc = node_init;
228 ntype.declare = node_declare;
231 ntype.labelfunc = node_label;
233 ntype.gather_link_search_ops = nullptr;
234 ntype.no_muting = true;
236 "NodeGeometryForeachGeometryElementInput",
240}
242
243} // namespace input_node
244
245namespace output_node {
246
248
250{
251 b.use_custom_socket_order();
252 b.allow_any_socket_order();
253
254 b.add_output<decl::Geometry>("Geometry"_ustr)
255 .description(
256 "The original input geometry with potentially new attributes that are output by the "
257 "zone");
258
259 const bNode *node = b.node_or_null();
260 const bNodeTree *tree = b.tree_or_null();
261 if (node && tree) {
263 for (const int i : IndexRange(storage.main_items.items_num)) {
264 const NodeForeachGeometryElementMainItem &item = storage.main_items.items[i];
265 const eNodeSocketDatatype socket_type = item.socket_type;
266 const UString name = item.name ? UString(item.name) : ""_ustr;
267 const UString identifier(
269 b.add_input(socket_type, name, identifier)
270 .socket_name_ptr(
272 .description(
273 "Attribute value that will be stored for the current element on the main geometry");
274 b.add_output(socket_type, name, identifier)
275 .align_with_previous()
276 .anonymous_attribute_output({0})
277 .description("Attribute on the geometry above");
278 }
279 b.add_input<decl::Extend>(""_ustr, "__extend__main"_ustr)
280 .custom_draw(
281 socket_items::ui::draw_extend_socket_fn<ForeachGeometryElementMainItemsAccessor>());
282 b.add_output<decl::Extend>(""_ustr, "__extend__main"_ustr).align_with_previous();
283
284 auto &panel = b.add_panel("Generated"_ustr);
285
286 int previous_output_geometry_index = -1;
287 int previous_input_geometry_index = -1;
288 for (const int i : IndexRange(storage.generation_items.items_num)) {
289 const NodeForeachGeometryElementGenerationItem &item = storage.generation_items.items[i];
290 const eNodeSocketDatatype socket_type = item.socket_type;
291 if (socket_type == SOCK_GEOMETRY && i > 0) {
292 panel.add_separator();
293 }
294 const UString name = item.name ? UString(item.name) : ""_ustr;
295 const UString identifier(
297 auto &input_decl = panel.add_input(socket_type, name, identifier)
298 .socket_name_ptr(
299 &tree->id,
301 &item,
302 "name");
303 auto &output_decl = panel.add_output(socket_type, name, identifier).align_with_previous();
304 if (socket_type == SOCK_GEOMETRY) {
305 previous_input_geometry_index = input_decl.index();
306 previous_output_geometry_index = output_decl.index();
307
308 input_decl.description(
309 "Geometry generated in the current iteration. Will be joined with geometries from all "
310 "other iterations");
311 output_decl.description("Result of joining generated geometries from each iteration");
312 }
313 else {
314 if (previous_output_geometry_index > 0) {
315 input_decl.description("Field that will be stored as attribute on the geometry above");
316 input_decl.evaluated_geometry_field({previous_input_geometry_index});
317 output_decl.anonymous_attribute_output({previous_output_geometry_index});
318 }
319 output_decl.description("Attribute on the geometry above");
320 }
321 }
322 panel.add_input<decl::Extend>(""_ustr, "__extend__generation"_ustr)
323 .custom_draw(socket_items::ui::draw_extend_socket_fn<
325 panel.add_output<decl::Extend>(""_ustr, "__extend__generation"_ustr).align_with_previous();
326 }
327}
328
329static void node_init(bNodeTree * /*tree*/, bNode *node)
330{
332 MEM_new<NodeGeometryForeachGeometryElementOutput>(__func__);
333
334 data->generation_items.items = MEM_new_array<NodeForeachGeometryElementGenerationItem>(1,
335 __func__);
336 NodeForeachGeometryElementGenerationItem &item = data->generation_items.items[0];
337 item.name = BLI_strdup(DATA_("Geometry"));
339 item.identifier = data->generation_items.next_identifier++;
340 data->generation_items.items_num = 1;
341
342 node->storage = data;
343}
344
352
353static void node_copy_storage(bNodeTree * /*dst_tree*/, bNode *dst_node, const bNode *src_node)
354{
355 const NodeGeometryForeachGeometryElementOutput &src_storage = node_storage(*src_node);
356 auto *dst_storage = MEM_new<NodeGeometryForeachGeometryElementOutput>(
357 __func__, dna::shallow_copy(src_storage));
358 dst_node->storage = dst_storage;
359
363}
364
366{
368 params.ntree, params.node, params.node, params.link, "__extend__main"))
369 {
370 return false;
371 }
374 params.ntree, params.node, params.node, params.link);
375}
376
383
385{
386 const NodeGeometryForeachGeometryElementOutput &storage = node_storage(params.node);
387 if (storage.generation_items.items_num > 0) {
388 if (storage.generation_items.items[0].socket_type != SOCK_GEOMETRY) {
390 row.text = RPT_("Missing Geometry");
391 row.tooltip = TIP_("Each output field has to correspond to a geometry that is above it");
392 row.icon = ICON_STATUS_ERROR;
393 params.rows.append(std::move(row));
394 }
395 }
396}
397
398static std::pair<bNode *, bNode *> add_foreach_zone(LinkSearchOpParams &params)
399{
400 bNode &input_node = params.add_node("GeometryNodeForeachGeometryElementInput"_ustr);
401 bNode &output_node = params.add_node("GeometryNodeForeachGeometryElementOutput"_ustr);
402 output_node.location[0] = 300;
403
404 auto &input_storage = *static_cast<NodeGeometryForeachGeometryElementInput *>(
405 input_node.storage);
406 input_storage.output_node_id = output_node.identifier;
407
408 return {&input_node, &output_node};
409}
410
412{
413 const bNodeSocket &other_socket = params.other_socket();
414 const eNodeSocketDatatype type = other_socket.type;
415 if (type != SOCK_GEOMETRY) {
416 return;
417 }
418 if (other_socket.in_out == SOCK_OUT) {
419 params.add_item_full_name(IFACE_("For Each Element"), [](LinkSearchOpParams &params) {
420 const auto [input_node, output_node] = add_foreach_zone(params);
421 params.update_and_connect_available_socket(*input_node, "Geometry"_ustr);
422 });
423 }
424 else {
425 params.add_item_full_name(
426 fmt::format(fmt::runtime(IFACE_("For Each Element {} Main")), UI_MENU_ARROW_SEP),
427 [](LinkSearchOpParams &params) {
428 const auto [input_node, output_node] = add_foreach_zone(params);
430 params.update_and_connect_available_socket(*output_node, "Geometry"_ustr);
431 });
432
433 params.add_item_full_name(
434 fmt::format(fmt::runtime(IFACE_("For Each Element {} Generated")), UI_MENU_ARROW_SEP),
435 [](LinkSearchOpParams &params) {
436 const auto [input_node, output_node] = add_foreach_zone(params);
437 params.node_tree.ensure_topology_cache();
440 output_node->output_socket(2),
441 params.node,
442 params.socket);
443 });
444 }
445}
446
453
460
461static void node_register()
462{
463 static bke::bNodeType ntype;
464 geo_node_type_base(&ntype,
465 "GeometryNodeForeachGeometryElementOutput"_ustr,
467 ntype.ui_name = "For Each Geometry Element Output";
468 ntype.enum_name_legacy = "FOREACH_GEOMETRY_ELEMENT_OUTPUT";
470 ntype.initfunc = node_init;
471 ntype.declare = node_declare;
478 ntype.no_muting = true;
482 ntype, "NodeGeometryForeachGeometryElementOutput", node_free_storage, node_copy_storage);
484}
486
487} // namespace output_node
488
489} // namespace nodes::node_geo_foreach_geometry_element_cc
490
491namespace nodes {
492
494 &RNA_ForeachGeometryElementInputItem;
495
497 const ItemT &item)
498{
499 writer->write_string(item.name);
500}
501
507
509 &RNA_ForeachGeometryElementMainItem;
510
512 const ItemT &item)
513{
514 writer->write_string(item.name);
515}
516
522
524 &RNA_ForeachGeometryElementGenerationItem;
525
531
537
538} // namespace nodes
539} // namespace blender
#define NODE_CLASS_INTERFACE
Definition BKE_node.hh:490
#define GEO_NODE_FOREACH_GEOMETRY_ELEMENT_INPUT
#define GEO_NODE_FOREACH_GEOMETRY_ELEMENT_OUTPUT
#define RPT_(msgid)
#define BLT_I18NCONTEXT_ID_NODETREE
#define TIP_(msgid)
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define DATA_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
constexpr IndexRange()=default
UString()=default
std::optional< int > output_node_id
const bNode * output_node() const
const bNodeTreeZone * get_zone_by_node(const int32_t node_id) const
void add_item_full_name(std::string name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
#define storage(slot, qualifiers)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4638
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:6166
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
static void node_label(const bNodeTree *, const bNode *, char *label, const int label_maxncpy)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
NODE_STORAGE_FUNCS(NodeGeometryForeachGeometryElementInput)
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)
NODE_STORAGE_FUNCS(NodeGeometryForeachGeometryElementOutput)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *current_node_ptr)
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)
BMesh Iterators.
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
BMesh const char void * data
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
#define UI_MENU_ARROW_SEP
void write_string(const char *data)
eNodeSocketDatatype type
eNodeSocketInOut in_out
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
NodeBlendWriteFunction blend_write_storage_content
Definition BKE_node.hh:421
void(*) register_operators()
Definition BKE_node.hh:448
NodeBlendDataReadFunction blend_data_read_storage_content
Definition BKE_node.hh:422
void(*) labelfunc(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:301
NodeExtraInfoFunction get_extra_info
Definition BKE_node.hh:412
void(*) draw_buttons_ex(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:292
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
bool(*) insert_link(NodeInsertLinkParams &params)
Definition BKE_node.hh:364
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)
Vector< NodeExtraInfoRow > & rows
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)