Blender V5.0
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 "node_geometry_util.hh"
17
19
21{
22 const bNode *node = b.node_or_null();
23
24 if (node != nullptr) {
26 b.add_input(type, "List").structure_type(StructureType::List).hide_value();
27 }
28
29 b.add_input<decl::Int>("Index").min(0).structure_type(StructureType::Dynamic);
30
31 if (node != nullptr) {
33 b.add_output(type, "Value").dependent_field({1});
34 }
35}
36
37static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
38{
39 layout->prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
40}
41
43 public:
47 {
48 bNode &node = params.add_node("GeometryNodeListGetItem");
49 node.custom1 = socket_type;
50 params.update_and_connect_available_socket(node, socket_name);
51 }
52};
53
55{
56 if (!U.experimental.use_geometry_nodes_lists) {
57 return;
58 }
59 const eNodeSocketDatatype socket_type = eNodeSocketDatatype(params.other_socket().type);
60 if (params.in_out() == SOCK_IN) {
61 if (params.node_tree().typeinfo->validate_link(socket_type, SOCK_INT)) {
62 params.add_item(IFACE_("Index"), SocketSearchOp{"Index", SOCK_INT});
63 }
64 params.add_item(IFACE_("List"), SocketSearchOp{"List", socket_type});
65 }
66 else {
67 params.add_item(IFACE_("Value"), SocketSearchOp{"Value", socket_type});
68 }
69}
70
71class SampleIndexFunction : public mf::MultiFunction {
72 ListPtr list_;
73 mf::Signature signature_;
74
75 public:
76 SampleIndexFunction(ListPtr list) : list_(std::move(list))
77 {
78 mf::SignatureBuilder builder{"Sample Index", signature_};
79 builder.single_input<int>("Index");
80 builder.single_output("Value", list_->cpp_type());
81 this->set_signature(&signature_);
82 }
83
84 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
85 {
86 const VArray<int> &indices = params.readonly_single_input<int>(0, "Index");
87 GMutableSpan dst = params.uninitialized_single_output(1, "Value");
88 const List::DataVariant &data = list_->data();
89 if (const auto *array_data = std::get_if<nodes::List::ArrayData>(&data)) {
90 const GSpan span(list_->cpp_type(), array_data->data, list_->size());
92 }
93 else if (const auto *single_data = std::get_if<nodes::List::SingleData>(&data)) {
94 list_->cpp_type().fill_construct_indices(single_data->value, dst.data(), mask);
95 }
96 }
97};
98
99static void node_rna(StructRNA *srna)
100{
102 srna,
103 "data_type",
104 "Data Type",
105 "",
109 [](bContext * /*C*/, PointerRNA * /*ptr*/, PropertyRNA * /*prop*/, bool *r_free) {
110 *r_free = true;
111 return enum_items_filter(
114 });
115 });
116}
117
119{
120 bke::SocketValueVariant index = params.extract_input<bke::SocketValueVariant>("Index");
121 ListPtr list = params.extract_input<ListPtr>("List");
122 if (!list) {
123 params.set_default_remaining_outputs();
124 return;
125 }
126
127 std::string error_message;
128 bke::SocketValueVariant output_value;
130 std::make_shared<SampleIndexFunction>(std::move(list)),
131 {&index},
132 {&output_value},
133 params.user_data(),
134 error_message))
135 {
136 params.set_default_remaining_outputs();
137 params.error_message_add(NodeWarningType::Error, std::move(error_message));
138 return;
139 }
140
141 params.set_output("Value", std::move(output_value));
142}
143
144static void node_register()
145{
146 static blender::bke::bNodeType ntype;
147 geo_node_type_base(&ntype, "GeometryNodeListGetItem");
148 ntype.ui_name = "Get List Item";
149 ntype.ui_description = "Retrieve a value from a list";
153 ntype.declare = node_declare;
156 node_rna(ntype.rna_ext.srna);
157}
159
160} // namespace blender::nodes::node_geo_list_get_item_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define IFACE_(msgid)
@ SOCK_IN
eNodeSocketDatatype
@ SOCK_INT
@ SOCK_GEOMETRY
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
#define U
BMesh const char void * data
static GVArray from_span(GSpan span)
std::variant< ArrayData, SingleData > DataVariant
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
static ushort indices[]
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
void copy_with_checked_indices(const GVArray &src, const VArray< int > &indices, const IndexMask &mask, GMutableSpan dst)
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
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)
ImplicitSharingPtr< List > ListPtr
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)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
#define min(a, b)
Definition sort.cc:36
StructRNA * srna
int16_t custom1
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:378
NodeDeclareFunction declare
Definition BKE_node.hh:362
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4238