Blender V5.3
node_geo_list_get_item.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
7#include "NOD_rna_define.hh"
8#include "NOD_socket.hh"
10
11#include "RNA_enum_types.hh"
12
14#include "UI_resources.hh"
15
16#include "list_function_eval.hh"
17#include "node_geometry_util.hh"
18
20
22
24{
25 const bNode *node = b.node_or_null();
26 if (!node) {
27 return;
28 }
29
30 const NodeGeometryListGetItem &storage = node_storage(*node);
31 const eNodeSocketDatatype type = storage.socket_type;
32 const bool is_auto_structure_type = storage.structure_type ==
34
35 auto &list = b.add_input(type, "List"_ustr).structure_type(StructureType::List).hide_value();
36 b.add_input<decl::Int>("Index"_ustr).min(0).structure_type(StructureType::Dynamic);
37 b.add_output(type, "Value"_ustr)
38 .propagate_all({list.index()})
39 .propagate_references()
40 .structure_type(is_auto_structure_type ? StructureType::Dynamic :
41 StructureType(storage.structure_type));
42}
43
44static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
45{
46 layout.prop(ptr, "socket_type", UI_ITEM_NONE, "", ICON_NONE);
47}
48
49static void node_layout_ex(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
50{
51 layout.prop(ptr, "socket_type", UI_ITEM_NONE, "", ICON_NONE);
52 layout.use_property_split_set(true);
53 layout.use_property_decorate_set(false);
54 layout.prop(ptr, "structure_type", UI_ITEM_NONE, IFACE_("Shape"), ICON_NONE);
55}
56
57static void node_init(bNodeTree * /*tree*/, bNode *node)
58{
59 auto *storage = MEM_new<NodeGeometryListGetItem>(__func__);
60 node->storage = storage;
61}
62
64 public:
68 {
69 bNode &node = params.add_node("GeometryNodeListGetItem"_ustr);
70 node_storage(node).socket_type = socket_type;
72 }
73};
74
76{
77 const eNodeSocketDatatype socket_type = params.other_socket().type;
78 if (params.in_out() == SOCK_IN) {
79 if (params.node_tree().typeinfo->validate_link(socket_type, SOCK_INT)) {
80 params.add_item(IFACE_("Index"), SocketSearchOp{"Index"_ustr, SOCK_INT});
81 }
82 params.add_item(IFACE_("List"), SocketSearchOp{"List"_ustr, socket_type});
83 }
84 else {
85 params.add_item(IFACE_("Value"), SocketSearchOp{"Value"_ustr, socket_type});
86 }
87}
88
89static void node_rna(StructRNA *srna)
90{
92 srna,
93 "socket_type",
94 "Socket Type",
95 "Value may be implicitly converted if the type does not match",
97 NOD_storage_enum_accessors(socket_type),
99 [](bContext * /*C*/, PointerRNA *ptr, PropertyRNA * /*prop*/, bool *r_free) {
100 *r_free = true;
101 const bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
102 bke::bNodeTreeType *ntree_type = ntree.typeinfo;
103 return enum_items_filter(
106 return ntree_type->valid_socket_type(ntree_type, socket_type);
107 });
108 });
110 "structure_type",
111 "Structure Type",
112 "What kind of higher order types are expected to flow through this socket",
114 NOD_storage_enum_accessors(structure_type));
115}
116
122 const eNodeSocketDatatype socket_type,
123 const int64_t index)
124{
126 const CPPType &cpp_type = *bke::socket_type_to_geo_nodes_base_cpp_type(socket_type);
127 void *value_ptr = value.allocate_single(cpp_type);
128 if (const auto *data = std::get_if<GList::ArrayData>(&list->data())) {
129 if (list->is_mutable() && data->sharing_info->is_mutable()) {
130 GMutableSpan data_span(list->cpp_type(), const_cast<void *>(data->data), list->size());
131 list->cpp_type().move_construct(data_span[index], value_ptr);
132 return value;
133 }
134 const GSpan data_span(list->cpp_type(), data->data, list->size());
135 list->cpp_type().copy_construct(data_span[index], value_ptr);
136 return value;
137 }
138 if (const auto *data = std::get_if<GList::SingleData>(&list->data())) {
139 if (list->is_mutable() && data->sharing_info->is_mutable()) {
140 list->cpp_type().move_construct(const_cast<void *>(data->value), value_ptr);
141 return value;
142 }
143 list->cpp_type().copy_construct(data->value, value_ptr);
144 return value;
145 }
147 return {};
148}
149
151{
152 if (const auto *data = std::get_if<GList::ArrayData>(&list->data())) {
153 if (list->is_mutable() && data->sharing_info->is_mutable()) {
154 MutableSpan data_span(static_cast<bke::SocketValueVariant *>(const_cast<void *>(data->data)),
155 list->size());
156 return std::move(data_span[index]);
157 }
158 const Span data_span(static_cast<bke::SocketValueVariant *>(const_cast<void *>(data->data)),
159 list->size());
160 return data_span[index];
161 }
162 if (const auto *data = std::get_if<GList::SingleData>(&list->data())) {
163 if (list->is_mutable() && data->sharing_info->is_mutable()) {
164 return std::move(*static_cast<bke::SocketValueVariant *>(const_cast<void *>(data->value)));
165 }
166 return *static_cast<const bke::SocketValueVariant *>(data->value);
167 }
169 return {};
170}
171
173{
175 GListPtr list = params.extract_input<GListPtr>("List"_ustr);
176 if (!list) {
178 return;
179 }
180 const CPPType &list_type = list->cpp_type();
181 const std::optional<eNodeSocketDatatype> socket_type =
183
184 if (list_type.is<bke::SocketValueVariant>() || !socket_type_supports_fields(*socket_type)) {
185 if (!index.is_single()) {
186 params.error_message_add(NodeWarningType::Error, "Index must be a single value");
188 return;
189 }
190 const int index_int = index.ensure_type<int>();
191 if (!IndexRange(list->size()).contains(index_int)) {
192 params.error_message_add(NodeWarningType::Error, "Index out of range");
194 return;
195 }
196 if (list->cpp_type().is<bke::SocketValueVariant>()) {
197 params.set_output("Value"_ustr, get_socket_value_item(list, index_int));
198 }
199 else {
200 params.set_output("Value"_ustr, get_single_item(list, *socket_type, index_int));
201 }
202 return;
203 }
204
205 std::string error_message;
206 bke::SocketValueVariant output_value;
208 std::make_shared<SampleIndexFunction>(std::move(list)),
209 {&index},
210 {&output_value},
211 params.user_data(),
212 error_message))
213 {
215 params.error_message_add(NodeWarningType::Error, std::move(error_message));
216 return;
217 }
218
219 params.set_output("Value"_ustr, std::move(output_value));
220}
221
222static void node_register()
223{
224 static bke::bNodeType ntype;
225 geo_node_type_base(&ntype, "GeometryNodeListGetItem"_ustr);
226 ntype.ui_name = "Get List Item";
227 ntype.ui_description = "Retrieve a value from a list";
231 ntype.declare = node_declare;
233 ntype.initfunc = node_init;
236 ntype, "NodeGeometryListGetItem", node_free_standard_storage, node_copy_standard_storage);
238 node_rna(ntype.rna_ext.srna);
239}
241
242} // namespace blender::nodes::node_geo_list_get_item_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
bool is() const
constexpr IndexRange()=default
void * allocate_single(const CPPType &type)
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
void set_output(const UString identifier, T &&value)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
#define storage(slot, qualifiers)
std::optional< eNodeSocketDatatype > geo_nodes_base_cpp_type_to_socket_type(const CPPType &type)
Definition node.cc:6008
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
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
NODE_STORAGE_FUNCS(NodeGeometryListGetItem)
static void node_declare(NodeDeclarationBuilder &b)
static void node_init(bNodeTree *, bNode *node)
static void node_geo_exec(GeoNodeExecParams params)
static bke::SocketValueVariant get_single_item(GListPtr &list, const eNodeSocketDatatype socket_type, const int64_t index)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_layout_ex(ui::Layout &layout, bContext *, PointerRNA *ptr)
static bke::SocketValueVariant get_socket_value_item(GListPtr &list, const int64_t index)
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)
bool execute_multi_function_on_value_variant(const MultiFunction &fn, const std::shared_ptr< MultiFunction > &owned_fn, const Span< SocketValueVariant * > input_values, const Span< SocketValueVariant * > output_values, GeoNodesUserData *user_data, std::string &r_error_message)
bool socket_type_supports_fields(const eNodeSocketDatatype socket_type)
const EnumPropertyItem * enum_items_filter(const EnumPropertyItem *original_item_array, FunctionRef< bool(const EnumPropertyItem &item)> fn)
__int64 int64_t
Definition BKE_text.h:90
BMesh const char void * data
const EnumPropertyItem rna_enum_node_socket_structure_type_items[]
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
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)
#define min(a, b)
Definition sort.cc:38
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
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
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
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
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)