Blender V5.3
node_geo_combine_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
5#include <algorithm>
6
7#include "BLO_read_write.hh"
8
10
13#include "NOD_rna_define.hh"
14#include "NOD_socket.hh"
19
20#include "RNA_enum_types.hh"
21#include "RNA_prototypes.hh"
22
24#include "UI_resources.hh"
25
26#include "node_geometry_util.hh"
27
29
31
33
35{
36 const bNode *node = b.node_or_null();
37 if (!node) {
38 return;
39 }
40 const NodeCombineList &storage = node_storage(*node);
41 const eNodeSocketDatatype data_type = storage.data_type;
42 const Span<CombineListItem> items = storage.items_span();
43
44 for (const int i : items.index_range()) {
45 const std::string identifier = ItemsAccessor::socket_identifier_for_item(items[i]);
46 auto &input = b.add_input(data_type, UString(std::to_string(i)), UString(identifier));
47 input.structure_type(StructureType::Dynamic);
48 /* Labels are ugly in combination with data-block pickers and are usually disabled. */
49 input.optional_label(ELEM(data_type,
58 SOCK_SOUND));
59 }
60
61 b.add_input<decl::Extend>(""_ustr, "__extend__"_ustr)
62 .structure_type(StructureType::Dynamic)
63 .custom_draw(socket_items::ui::draw_extend_socket_fn<ItemsAccessor>());
64
65 b.add_output(data_type, "List"_ustr)
66 .structure_type(StructureType::List)
67 .propagate_all()
68 .description("List with one element per item");
69}
70
71static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
72{
73 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
74}
75
76static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
77{
78 bNode &node = *static_cast<bNode *>(ptr->data);
79 NodeCombineList &storage = node_storage(node);
80 if (ui::Layout *panel = layout.panel(C, "combine_list_items", false, IFACE_("Items"))) {
81 panel->op("node.combine_list_item_add", IFACE_("Add Item"), ICON_ADD);
82 ui::Layout *col = &panel->column(false);
83 for (const int i : IndexRange(storage.items_num)) {
84 ui::Layout *row = &col->row(false);
85 row->label(node.input_socket(i).name, ICON_NONE);
86 PointerRNA op_ptr = row->op("node.combine_list_item_remove", "", ICON_REMOVE);
87 RNA_int_set(&op_ptr, "index", i);
88 }
89 }
90}
91
98
99static void node_init(bNodeTree * /*tree*/, bNode *node)
100{
101 NodeCombineList *data = MEM_new<NodeCombineList>(__func__);
102 data->data_type = SOCK_FLOAT;
103 data->next_identifier = 0;
104
105 const int default_items_num = 1;
106 data->items = MEM_new_array<CombineListItem>(default_items_num, __func__);
107 for (const int i : IndexRange(default_items_num)) {
108 data->items[i].identifier = data->next_identifier++;
109 }
110 data->items_num = default_items_num;
111
112 node->storage = data;
113}
114
116{
117 const eNodeSocketDatatype other_type = params.other_socket().type;
118 bke::bNodeTreeType &tree_type = *params.node_tree().typeinfo;
120 if (!socket_type ||
121 (tree_type.valid_socket_type && !tree_type.valid_socket_type(&tree_type, socket_type)))
122 {
123 return;
124 }
125
126 if (params.in_out() == SOCK_OUT) {
127 params.add_item(IFACE_("List"), [other_type](LinkSearchOpParams &params) {
128 bNode &node = params.add_node("GeometryNodeCombineList"_ustr);
129 node_storage(node).data_type = other_type;
130 params.update_and_connect_available_socket(node, "List"_ustr);
131 });
132 }
133 else {
134 params.add_item(IFACE_("Item"), [other_type](LinkSearchOpParams &params) {
135 bNode &node = params.add_node("GeometryNodeCombineList"_ustr);
136 node_storage(node).data_type = other_type;
138 });
139 }
140}
141
143{
144 const NodeCombineList &storage = node_storage(params.node());
145 const Span<CombineListItem> items = storage.items_span();
146 const eNodeSocketDatatype data_type = storage.data_type;
147
148 const CPPType *cpp_type = bke::socket_type_to_geo_nodes_base_cpp_type(data_type);
149 if (!cpp_type) {
151 return;
152 }
153
154 Array<bke::SocketValueVariant> values(items.size());
155 for (const int i : items.index_range()) {
156 const std::string identifier = ItemsAccessor::socket_identifier_for_item(items[i]);
157 values[i] = params.extract_input<bke::SocketValueVariant>(UString(identifier));
158 }
159
160 const bool all_single = std::all_of(
161 values.begin(), values.end(), [](const bke::SocketValueVariant &value) {
162 return value.is_single();
163 });
164
165 if (all_single) {
166 GArray<> array(*cpp_type, items.size(), NoInitialization());
167 for (const int i : items.index_range()) {
168 BLI_assert(values[i].is_single());
169 void *value_ptr = values[i].get_if(*cpp_type);
170 cpp_type->move_construct(value_ptr, array[i]);
171 }
172 params.set_output("List"_ustr, GList::from_garray(std::move(array)));
173 }
174 else {
175 params.set_output("List"_ustr, GList::from_container(std::move(values)));
176 }
177}
178
180 PointerRNA *ptr,
181 PropertyRNA * /*prop*/,
182 bool *r_free)
183{
184 *r_free = true;
185 const bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
186 bke::bNodeTreeType *ntree_type = ntree.typeinfo;
187 return enum_items_filter(
190 return ntree_type->valid_socket_type(ntree_type, socket_type);
191 });
192}
193
194static void node_rna(StructRNA *srna)
195{
197 "data_type",
198 "Data Type",
199 "",
204}
205
207{
209 MEM_delete(static_cast<NodeCombineList *>(node->storage));
210}
211
212static void node_copy_storage(bNodeTree * /*dst_tree*/, bNode *dst_node, const bNode *src_node)
213{
214 const NodeCombineList &src_storage = node_storage(*src_node);
215 auto *dst_storage = MEM_new<NodeCombineList>(__func__, dna::shallow_copy(src_storage));
216 dst_node->storage = dst_storage;
217
218 socket_items::copy_array<ItemsAccessor>(*src_node, *dst_node);
219}
220
222{
224 params.ntree, params.node, params.node, params.link);
225}
226
227static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
228{
230}
231
232static void node_blend_read(bNodeTree & /*tree*/, bNode &node, BlendDataReader &reader)
233{
235}
236
237static void node_register()
238{
239 static bke::bNodeType ntype;
240
241 geo_node_type_base(&ntype, "GeometryNodeCombineList"_ustr);
242 ntype.ui_name = "Combine List";
243 ntype.ui_description = "Combine an arbitrary number of values into a list";
245 ntype.declare = node_declare;
246 ntype.initfunc = node_init;
258
259 node_rna(ntype.rna_ext.srna);
260}
262
263} // namespace blender::nodes::node_geo_combine_list_cc
264
265namespace blender::nodes {
266
267StructRNA **CombineListItemsAccessor::item_srna = &RNA_CombineListItem;
268
270{
271}
272
276
277} // namespace blender::nodes
278
279namespace blender {
280
281Span<CombineListItem> NodeCombineList::items_span() const
282{
284}
285
286MutableSpan<CombineListItem> NodeCombineList::items_span()
287{
289}
290
291} // namespace blender
#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 NOD_storage_enum_accessors(member)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define ELEM(...)
const T * end() const
Definition BLI_array.hh:336
const T * begin() const
Definition BLI_array.hh:332
void move_construct(void *src, void *dst) const
constexpr IndexRange()=default
constexpr MutableSpan()=default
constexpr Span()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
UString()=default
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)
T extract_input(const UString identifier)
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_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
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
static void node_declare(NodeDeclarationBuilder &b)
static void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
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)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_geo_exec(GeoNodeExecParams params)
static void node_init(bNodeTree *, bNode *node)
static void node_copy_storage(bNodeTree *, bNode *dst_node, const bNode *src_node)
static const EnumPropertyItem * data_type_items_callback(bContext *, PointerRNA *ptr, PropertyRNA *, bool *r_free)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *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)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
const EnumPropertyItem * enum_items_filter(const EnumPropertyItem *original_item_array, FunctionRef< bool(const EnumPropertyItem &item)> fn)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
BMesh const char void * data
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
Defines a socket type.
Definition BKE_node.hh:163
bool(*) valid_socket_type(bNodeTreeType *ntreetype, bNodeSocketType *socket_type)
Definition BKE_node.hh:569
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
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_read_data_item(BlendDataReader *reader, ItemT &item)
static std::string socket_identifier_for_item(const CombineListItem &item)
static void blend_write_item(BlendWriter *writer, const ItemT &item)
void label(StringRef name, int icon)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
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)
Layout & column(bool align)
Layout & row(bool align)