Blender V5.3
node_geo_field_to_list.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLO_read_write.hh"
6
13
14#include "RNA_prototypes.hh"
15
16#include "list_function_eval.hh"
17
18#include "node_geometry_util.hh"
19
21
24
26{
27 b.use_custom_socket_order();
28 b.allow_any_socket_order();
29
30 b.add_input<decl::Int>("Count"_ustr)
32 .min(1)
33 .description("The number of elements in the list");
34
35 const bNode *node = b.node_or_null();
36 const bNodeTree *tree = b.tree_or_null();
37 if (!node) {
38 return;
39 }
40 const GeometryNodeFieldToList &storage = node_storage(*node);
41 const Span<GeometryNodeFieldToListItem> items(storage.items, storage.items_num);
42
43 for (const int i : items.index_range()) {
44 const GeometryNodeFieldToListItem &item = items[i];
45 const eNodeSocketDatatype type = item.socket_type;
46 const std::string input_identifier = ItemsAccessor::input_socket_identifier_for_item(item);
47 const std::string output_identifier = ItemsAccessor::output_socket_identifier_for_item(item);
48 const UString name(item.name);
49
50 b.add_input(type, name, UString(input_identifier))
51 .structure_type(StructureType::Field)
52 .socket_name_ptr(&tree->id, *ItemsAccessor::item_srna, &item, "name");
53 b.add_output(type, name, UString(output_identifier))
54 .structure_type(StructureType::List)
55 .align_with_previous()
56 .description("Output list with evaluated field values");
57 }
58
59 b.add_input<decl::Extend>(""_ustr, "__extend__"_ustr)
60 .structure_type(StructureType::Field)
61 .custom_draw(socket_items::ui::draw_extend_socket_fn<FieldToListItemsAccessor>());
62 b.add_output<decl::Extend>(""_ustr, "__extend__"_ustr)
63 .structure_type(StructureType::List)
64 .align_with_previous();
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, "field_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 });
78 }
79}
80
82{
83 const eNodeSocketDatatype data_type = params.other_socket().type;
84 if (params.in_out() == SOCK_IN) {
85 if (params.node_tree().typeinfo->validate_link(data_type, SOCK_INT)) {
86 params.add_item(IFACE_("Count"), [](LinkSearchOpParams &params) {
87 bNode &node = params.add_node("GeometryNodeFieldToList"_ustr);
88 params.update_and_connect_available_socket(node, "Count"_ustr);
89 });
90 }
92 params.add_item(IFACE_("Field"), [data_type](LinkSearchOpParams &params) {
93 bNode &node = params.add_node("GeometryNodeFieldToList"_ustr);
95 params.node_tree, node, data_type, params.socket.name);
98 });
99 }
100 }
101 else {
103 params.add_item(IFACE_("List"), [data_type](LinkSearchOpParams &params) {
104 bNode &node = params.add_node("GeometryNodeFieldToList"_ustr);
106 params.node_tree, node, data_type, params.socket.name);
108 });
109 }
110 }
111}
112
114{
115 const int count = params.extract_input<int>("Count"_ustr);
116 if (count < 0) {
117 params.error_message_add(NodeWarningType::Error, "Count must not be negative");
119 return;
120 }
121 const GeometryNodeFieldToList &storage = node_storage(params.node());
122 const Span<GeometryNodeFieldToListItem> items(storage.items, storage.items_num);
123
124 Vector<int> required_items;
125 for (const int i : items.index_range()) {
126 if (params.output_is_required(
128 {
129 required_items.append(i);
130 }
131 }
132
133 Vector<fn::GField> fields;
134 fields.reserve(required_items.size());
135 for (const int i : required_items.index_range()) {
136 const int item_i = required_items[i];
138 items[item_i].socket_type);
139 const std::string identifier = ItemsAccessor::input_socket_identifier_for_item(items[item_i]);
140 fn::GField field = params.extract_input<fn::GField>(UString(identifier));
141 if (!expected_type || field.cpp_type() != *expected_type) {
142 params.error_message_add(NodeWarningType::Error, "Wrong input type");
144 return;
145 }
146 fields.append(std::move(field));
147 }
148
149 Vector<GListPtr> lists(required_items.size());
150 for (const int i : required_items.index_range()) {
151 const CPPType &cpp_type = fields[i].cpp_type();
152 lists[i] = GList::create(cpp_type, GList::ArrayData::ForUninitialized(cpp_type, count), count);
153 }
154
155 ListFieldContext context;
156 fn::FieldEvaluator evaluator{context, count};
157 for (const int i : fields.index_range()) {
158 GMutableSpan values(lists[i]->cpp_type(),
159 const_cast<void *>(std::get<GList::ArrayData>(lists[i]->data()).data),
160 count);
161 evaluator.add_with_destination(std::move(fields[i]), values);
162 }
163
164 evaluator.evaluate();
165
166 for (const int i : required_items.index_range()) {
167 const int item_i = required_items[i];
168 const std::string identifier = ItemsAccessor::output_socket_identifier_for_item(items[item_i]);
169 params.set_output(UString(identifier), std::move(lists[i]));
170 }
171}
172
173static void node_init(bNodeTree * /*tree*/, bNode *node)
174{
175 node->storage = MEM_new<GeometryNodeFieldToList>(__func__);
176}
177
183
184static void node_copy_storage(bNodeTree * /*dst_tree*/, bNode *dst_node, const bNode *src_node)
185{
186 const GeometryNodeFieldToList &src_storage = node_storage(*src_node);
187 auto *dst_storage = MEM_new<GeometryNodeFieldToList>(__func__, src_storage);
188 dst_node->storage = dst_storage;
189
190 socket_items::copy_array<ItemsAccessor>(*src_node, *dst_node);
191}
192
197
199{
201 params.ntree, params.node, params.node, params.link);
202}
203
204static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
205{
207}
208
209static void node_blend_read(bNodeTree & /*tree*/, bNode &node, BlendDataReader &reader)
210{
212}
213
215 const bNode &node,
216 const bNodeSocket &output_socket)
217{
218 return node.input_by_identifier(output_socket.identifier_ustr());
219}
220
221static void node_register()
222{
223 static blender::bke::bNodeType ntype;
224 geo_node_type_base(&ntype, "GeometryNodeFieldToList"_ustr);
225 ntype.ui_name = "Field to List";
226 ntype.ui_description = "Create a list of values";
228 ntype.declare = node_declare;
229 ntype.initfunc = node_init;
231 ntype, "GeometryNodeFieldToList", node_free_storage, node_copy_storage);
242}
244
245} // namespace blender::nodes::node_geo_field_to_list_cc
246
247namespace blender::nodes {
248
249StructRNA **FieldToListItemsAccessor::item_srna = &RNA_GeometryNodeFieldToListItem;
250
252{
253 writer->write_string(item.name);
254}
255
260
261} // 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
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
void reserve(const int64_t min_capacity)
int add_with_destination(GField field, GVMutableArray dst)
const CPPType & cpp_type() const
Definition FN_field.hh:518
static ArrayData ForUninitialized(const CPPType &type, int64_t size)
static GListPtr create(const CPPType &type, DataVariant data, const int64_t size)
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)
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)
void update_and_connect_available_socket_by_identifier(bNode &new_node, UString socket_identifier)
#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
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 void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
static void node_declare(NodeDeclarationBuilder &b)
static void node_copy_storage(bNodeTree *, bNode *dst_node, const bNode *src_node)
static void node_geo_exec(GeoNodeExecParams params)
static void node_init(bNodeTree *, bNode *node)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
static const bNodeSocket * node_internally_linked_input(const bNodeTree &, const bNode &node, const bNodeSocket &output_socket)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
static bool node_insert_link(bke::NodeInsertLinkParams &params)
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 MEM_delete_void(void *vmemh)
BMesh const char void * data
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 min(a, b)
Definition sort.cc:38
void write_string(const char *data)
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
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 bool supports_socket_type(const eNodeSocketDatatype socket_type, const int)
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
static std::string output_socket_identifier_for_item(const GeometryNodeFieldToListItem &item)
static void blend_write_item(BlendWriter *writer, const ItemT &item)
static std::string input_socket_identifier_for_item(const GeometryNodeFieldToListItem &item)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)