Blender V5.3
node_geo_list_length.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
6#include "NOD_rna_define.hh"
7#include "NOD_socket.hh"
9
10#include "RNA_enum_types.hh"
11
13#include "UI_resources.hh"
14
15#include "node_geometry_util.hh"
16
18
20{
21 const bNode *node = b.node_or_null();
22
23 if (node != nullptr) {
24 const eNodeSocketDatatype type = eNodeSocketDatatype(node->custom1);
25 b.add_input(type, "List"_ustr).structure_type(StructureType::List).hide_value();
26 }
27
28 b.add_output<decl::Int>("Length"_ustr);
29}
30
31static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
32{
33 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
34}
35
37 public:
41 {
42 bNode &node = params.add_node("GeometryNodeListLength"_ustr);
43 node.custom1 = socket_type;
45 }
46};
47
49{
50 const eNodeSocketDatatype socket_type = params.other_socket().type;
51 if (params.in_out() == SOCK_IN) {
52 params.add_item(IFACE_("List"), SocketSearchOp{"List"_ustr, socket_type});
53 }
54 else {
55 if (params.node_tree().typeinfo->validate_link(socket_type, SOCK_INT)) {
56 params.add_item(IFACE_("Length"), SocketSearchOp{"Length"_ustr, SOCK_INT});
57 }
58 }
59}
60
62{
63 auto list = params.extract_input<GListPtr>("List"_ustr);
64 if (!list) {
66 return;
67 }
68 params.set_output("Length"_ustr, int(list->size()));
69}
70
71static void node_rna(StructRNA *srna)
72{
74 srna,
75 "data_type",
76 "Data Type",
77 "",
81 [](bContext * /*C*/, PointerRNA *ptr, PropertyRNA * /*prop*/, bool *r_free) {
82 *r_free = true;
83 const bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
84 bke::bNodeTreeType *ntree_type = ntree.typeinfo;
85 return enum_items_filter(
88 return ntree_type->valid_socket_type(ntree_type, socket_type);
89 });
90 });
91}
92
93static void node_register()
94{
95 static bke::bNodeType ntype;
96 geo_node_type_base(&ntype, "GeometryNodeListLength"_ustr);
97 ntype.ui_name = "List Length";
98 ntype.ui_description = "Count how many items are in a given list";
101 ntype.declare = node_declare;
105 node_rna(ntype.rna_ext.srna);
106}
108
109} // namespace blender::nodes::node_geo_list_length_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
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)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
bNodeSocketType * node_socket_type_find_static(int type, int subtype=0)
Definition node.cc:2975
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_geo_exec(GeoNodeExecParams params)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
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)
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
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
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
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)