Blender V5.3
node_geo_closure_to_list.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
6#include "BKE_volume_grid.hh"
7
8#include "BLO_read_write.hh"
9
18
19#include "RNA_prototypes.hh"
20
21#include "node_geometry_util.hh"
22
24
26
28
30{
31 b.use_custom_socket_order();
32 b.allow_any_socket_order();
33
34 const bNodeTree *tree = b.tree_or_null();
35 const bNode *node = b.node_or_null();
36 if (!node) {
37 return;
38 }
39 const GeometryNodeClosureToList &storage = node_storage(*node);
40 const Span<GeometryNodeClosureToListItem> items(storage.items, storage.items_num);
41
42 for (const int i : items.index_range()) {
43 const GeometryNodeClosureToListItem &item = items[i];
44 const UString output_identifier{ItemsAccessor::output_socket_identifier_for_item(item)};
45 const UString name{item.name};
46 const eNodeSocketDatatype type = item.socket_type;
47 b.add_output(type, name, output_identifier)
48 .structure_type(StructureType::List)
49 .socket_name_ptr(&tree->id, *ItemsAccessor::item_srna, &item, "name")
50 .propagate_all()
51 .references_other_outputs();
52 }
53
54 b.add_output<decl::Extend>(""_ustr, "__extend__"_ustr)
55 .structure_type(StructureType::List)
56 .custom_draw(socket_items::ui::draw_extend_socket_fn<ItemsAccessor>());
57
58 b.add_input<decl::Int>("Count"_ustr)
59 .default_value(1)
60 .min(0)
61 .description("The number of elements in the list");
62 b.add_input<decl::Closure>("Closure"_ustr).create_signature([](const bNode &node) {
64 });
65}
66
67static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
68{
69 bNodeTree &tree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
70 bNode &node = *static_cast<bNode *>(ptr->data);
71 if (ui::Layout *panel = layout.panel(C, "closure_to_list_items", false, IFACE_("Items"))) {
72 socket_items::ui::draw_items_list_with_operators<ItemsAccessor>(C, panel, tree, node);
73 socket_items::ui::draw_active_item_props<ItemsAccessor>(tree, node, [&](PointerRNA *item_ptr) {
74 panel->use_property_split_set(true);
75 panel->use_property_decorate_set(false);
76 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
77 panel->prop(item_ptr, "structure_type", UI_ITEM_NONE, IFACE_("Shape"), ICON_NONE);
78 });
79 }
80}
81
83{
84 const eNodeSocketDatatype data_type = params.other_socket().type;
85 if (params.in_out() == SOCK_IN) {
86 if (params.node_tree().typeinfo->validate_link(data_type, SOCK_INT)) {
87 params.add_item(IFACE_("Count"), [](LinkSearchOpParams &params) {
88 bNode &node = params.add_node("GeometryNodeClosureToList"_ustr);
89 params.update_and_connect_available_socket(node, "Count"_ustr);
90 });
91 }
92 if (params.node_tree().typeinfo->validate_link(data_type, SOCK_CLOSURE)) {
93 params.add_item(IFACE_("Closure"), [](LinkSearchOpParams &params) {
94 bNode &node = params.add_node("GeometryNodeClosureToList"_ustr);
95 params.update_and_connect_available_socket(node, "Closure"_ustr);
96 });
97 }
98 }
99 else {
100 params.add_item(IFACE_("List"), [data_type](LinkSearchOpParams &params) {
101 bNode &node = params.add_node("GeometryNodeClosureToList"_ustr);
103 params.node_tree, node, data_type, params.socket.name);
105 });
106 }
107}
108
110{
111 const int count = params.extract_input<int>("Count"_ustr);
112 if (count < 0) {
113 params.error_message_add(NodeWarningType::Error, "Count must not be negative");
115 return;
116 }
117 const bNode &node = params.node();
118 const GeometryNodeClosureToList &storage = node_storage(node);
119 const Span<GeometryNodeClosureToListItem> items(storage.items, storage.items_num);
120
121 ClosurePtr closure = params.extract_input<ClosurePtr>("Closure"_ustr);
122 if (!closure) {
124 return;
125 }
126
127 Vector<int> required_items;
128 for (const int i : items.index_range()) {
129 if (params.output_is_required(
131 {
132 required_items.append(i);
133 }
134 }
135
137 for (const int required_i : required_items.index_range()) {
138 const int item_i = required_items[required_i];
139 const eNodeSocketDatatype type = items[item_i].socket_type;
141 }
142 if (socket_types.as_span().contains(nullptr)) {
144 return;
145 }
146
147 Array<Array<bke::SocketValueVariant>> closure_results(required_items.size(), NoInitialization());
148 for (const int i : closure_results.index_range()) {
149 new (&closure_results[i]) Array<bke::SocketValueVariant>(count, NoInitialization());
150 }
151
152 const GeoNodesUserData &parent_user_data = *params.user_data();
153
154 /* The grain size is completely arbitrary since we don't know how expensive the closure is.
155 * However since the closure evaluation itself has fairly high overhead, it makes to optimize for
156 * the case where each task has a relatively high cost. */
157 const bke::bNodeSocketType *int_type = bke::node_socket_type_find("NodeSocketInt");
158 threading::parallel_for(IndexRange(count), 8, [&](const IndexRange range) {
159 ClosureEagerEvalParams closure_params;
160
161 /* Create inputs. */
162 closure_params.inputs.resize(1);
163 closure_params.inputs[0].key = "Index";
164 closure_params.inputs[0].type = int_type;
165
166 /* Create outputs. */
167 closure_params.outputs.resize(required_items.size());
168 for (const int required_i : required_items.index_range()) {
169 const int item_i = required_items[required_i];
170 closure_params.outputs[required_i].key = items[item_i].name;
171 closure_params.outputs[required_i].type = socket_types[required_i];
172 }
173
174 for (const int64_t list_i : range) {
175 /* Create input value. */
176 BLI_assert(list_i < std::numeric_limits<int>::max());
177 closure_params.inputs[0].value = bke::SocketValueVariant::from(int(list_i));
178
179 /* Set output locations. */
180 for (const int required_i : required_items.index_range()) {
181 closure_params.outputs[required_i].value = &closure_results[required_i][list_i];
182 }
183
185 parent_user_data.compute_context, node.identifier, int(list_i));
186 GeoNodesUserData user_data = parent_user_data;
187 user_data.compute_context = &context;
188 user_data.verbose_log = should_log_verbose_in_context(user_data, context.hash());
189 closure_params.user_data = &user_data;
190
191 evaluate_closure_eagerly(*closure, closure_params);
192 }
193 });
194
195 for (const int required_i : required_items.index_range()) {
196 const int item_i = required_items[required_i];
197 const UString identifier{ItemsAccessor::output_socket_identifier_for_item(items[item_i])};
198 Array<bke::SocketValueVariant> &values = closure_results[required_i];
199
200 if (std::all_of(values.begin(),
201 values.end(),
202 [](const bke::SocketValueVariant &value) { return value.is_single(); }))
203 {
204 const eNodeSocketDatatype socket_type = items[item_i].socket_type;
205 const CPPType &type = *bke::socket_type_to_geo_nodes_base_cpp_type(socket_type);
206
207 GArray<> array(type, count, NoInitialization());
208 threading::parallel_for(IndexRange(count), 128, [&](const IndexRange range) {
209 for (const int list_i : range) {
210 void *closure_result = values[list_i].get_if(type);
211 type.move_construct(closure_result, array[list_i]);
212 }
213 });
214 params.set_output(identifier, GList::from_garray(std::move(array)));
215 }
216 else {
217 params.set_output(identifier, GList::from_container(std::move(values)));
218 }
219 }
220}
221
222static void node_init(bNodeTree * /*tree*/, bNode *node)
223{
224 node->storage = MEM_new<GeometryNodeClosureToList>(__func__);
225}
226
228{
230 MEM_delete(static_cast<GeometryNodeClosureToList *>(node->storage));
231}
232
233static void node_copy_storage(bNodeTree * /*dst_tree*/, bNode *dst_node, const bNode *src_node)
234{
235 const GeometryNodeClosureToList &src_storage = node_storage(*src_node);
236 auto *dst_storage = MEM_new<GeometryNodeClosureToList>(__func__, src_storage);
237 dst_node->storage = dst_storage;
238
239 socket_items::copy_array<ItemsAccessor>(*src_node, *dst_node);
240}
241
246
248{
250 params.ntree, params.node, params.node, params.link);
251}
252
253static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
254{
256}
257
258static void node_blend_read(bNodeTree & /*tree*/, bNode &node, BlendDataReader &reader)
259{
261}
262
264 const bNode &node,
265 const bNodeSocket &output_socket)
266{
267 return node.input_by_identifier(output_socket.identifier_ustr());
268}
269
270static void node_register()
271{
272 static bke::bNodeType ntype;
273 geo_node_type_base(&ntype, "GeometryNodeClosureToList"_ustr);
274 ntype.ui_name = "Closure to List";
275 ntype.ui_description = "Create a list of values";
277 ntype.declare = node_declare;
278 ntype.initfunc = node_init;
279 bke::node_type_storage(ntype, "GeometryNodeClosureToList", node_free_storage, node_copy_storage);
290}
292
293} // namespace blender::nodes::node_geo_closure_to_list_cc
294
295namespace blender::nodes {
296
297StructRNA **ClosureToListItemsAccessor::item_srna = &RNA_GeometryNodeClosureToListItem;
298
300{
301 writer->write_string(item.name);
302}
303
308
309} // namespace blender::nodes
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
const T * end() const
Definition BLI_array.hh:336
IndexRange index_range() const
Definition BLI_array.hh:371
const T * begin() const
Definition BLI_array.hh:332
void move_construct(void *src, void *dst) const
constexpr IndexRange()=default
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
UString()=default
int64_t size() const
void append(const T &value)
IndexRange index_range() const
static SocketValueVariant from(T &&value)
static ClosureSignature from_closure_to_list_node(const bNode &node)
static GListPtr from_garray(GArray<> array)
static GListPtr from_container(ContainerT &&container)
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
GeoNodesUserData * user_data() const
bool output_is_required(const UString identifier) const
void set_output(const UString identifier, T &&value)
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
const CPPType * socket_type_to_geo_nodes_base_cpp_type(eNodeSocketDatatype type)
Definition node.cc:5969
bNodeSocketType * node_socket_type_find(StringRef idname)
Definition node.cc:2966
bNodeSocketType * node_socket_type_find_static(int type, int subtype=0)
Definition node.cc:2975
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
static bool node_insert_link(bke::NodeInsertLinkParams &params)
static const bNodeSocket * node_internally_linked_input(const bNodeTree &, const bNode &node, const bNodeSocket &output_socket)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
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 void node_init(bNodeTree *, bNode *node)
static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
static void node_geo_exec(GeoNodeExecParams params)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
static void node_declare(NodeDeclarationBuilder &b)
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)
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_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 evaluate_closure_eagerly(const Closure &closure, ClosureEagerEvalParams &params)
bool should_log_verbose_in_context(const GeoNodesUserData &user_data, const ComputeContextHash hash)
ImplicitSharingPtr< Closure > ClosurePtr
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
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
void write_string(const char *data)
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
Defines a socket type.
Definition BKE_node.hh:163
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
NodeInternallyLinkedInputFunction internally_linked_input
Definition BKE_node.hh:415
NodeBlendWriteFunction blend_write_storage_content
Definition BKE_node.hh:421
void(*) register_operators()
Definition BKE_node.hh:448
std::string ui_description
Definition BKE_node.hh:271
NodeBlendDataReadFunction blend_data_read_storage_content
Definition BKE_node.hh:422
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
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
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
bool ignore_inferred_input_socket_visibility
Definition BKE_node.hh:453
bool(*) insert_link(NodeInsertLinkParams &params)
Definition BKE_node.hh:364
static void blend_write_item(BlendWriter *writer, const ItemT &item)
static std::string output_socket_identifier_for_item(const GeometryNodeClosureToListItem &item)
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)