Blender V5.3
node_geo_filter_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 "BLI_array_utils.hh"
6
9#include "NOD_rna_define.hh"
10#include "NOD_socket.hh"
12
13#include "RNA_enum_types.hh"
14
16#include "UI_resources.hh"
17
18#include "list_function_eval.hh"
19#include "node_geometry_util.hh"
20
22
24{
25 const bNode *node = b.node_or_null();
26 if (!node) {
27 return;
28 }
29 const auto type = eNodeSocketDatatype(node->custom1);
30 b.add_input(type, "List"_ustr).structure_type(StructureType::List).hide_value();
31 b.add_input<decl::Bool>("Selection"_ustr)
32 .default_value(true)
33 .hide_value()
34 .description("A field or list representing the values that will not be removed")
35 .structure_type(StructureType::Dynamic);
36 b.add_output(type, "Selection"_ustr)
37 .propagate_all({0})
38 .structure_type(StructureType::List)
39 .align_with_previous();
40 b.add_output(type, "Inverted"_ustr)
41 .propagate_all({0})
42 .structure_type(StructureType::List)
43 .align_with_previous();
44}
45
46static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
47{
48 layout.prop(ptr, "socket_type", UI_ITEM_NONE, "", ICON_NONE);
49}
50
52 public:
56 {
57 bNode &node = params.add_node("GeometryNodeFilterList"_ustr);
58 node.custom1 = socket_type;
60 }
61};
62
64{
65 const eNodeSocketDatatype socket_type = eNodeSocketDatatype(params.other_socket().type);
66 if (params.in_out() == SOCK_IN) {
67 if (params.node_tree().typeinfo->validate_link(socket_type, SOCK_BOOLEAN)) {
68 params.add_item(IFACE_("Selection"), SocketSearchOp{"Selection"_ustr, SOCK_BOOLEAN});
69 }
70 params.add_item(IFACE_("List"), SocketSearchOp{"List"_ustr, socket_type});
71 }
72 else {
73 params.add_item(IFACE_("Selection"), SocketSearchOp{"Selection"_ustr, socket_type});
74 params.add_item(IFACE_("Inverted"), SocketSearchOp{"Inverted"_ustr, socket_type});
75 }
76}
77
78static void node_rna(StructRNA *srna)
79{
81 srna,
82 "socket_type",
83 "Socket Type",
84 "",
88 [](bContext * /*C*/, PointerRNA *ptr, PropertyRNA * /*prop*/, bool *r_free) {
89 *r_free = true;
90 const bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
91 bke::bNodeTreeType *ntree_type = ntree.typeinfo;
92 return enum_items_filter(
95 return ntree_type->valid_socket_type(ntree_type, socket_type);
96 });
97 });
98}
99
101{
102 if (mask.size() == list->size()) {
103 return list;
104 }
105 const CPPType &list_type = list->cpp_type();
106 return std::visit(
107 [&]<typename T>(const T &src_data) {
108 if constexpr (std::is_same_v<T, GList::ArrayData>) {
109 GArray<> dst_data(list_type, mask.size());
110 array_utils::gather(GSpan(list_type, src_data.data, list->size()), mask, dst_data);
111 return GList::from_garray(std::move(dst_data));
112 }
113 else if constexpr (std::is_same_v<T, GList::SingleData>) {
114 return GList::create(list_type, src_data, mask.size());
115 }
116 },
117 list->data());
118}
119
120static void output_lists(GeoNodeExecParams &params,
121 const GListPtr &list,
122 const IndexMask &selection)
123{
124 if (params.output_is_required("Selection"_ustr)) {
125 params.set_output("Selection"_ustr, filter_list(list, selection));
126 }
127 if (params.output_is_required("Inverted"_ustr)) {
128 IndexMaskMemory memory;
129 const IndexMask inverted = selection.complement(IndexRange(list->size()), memory);
130 params.set_output("Inverted"_ustr, filter_list(list, inverted));
131 }
132}
133
135{
136 GListPtr list = params.extract_input<GListPtr>("List"_ustr);
137 if (!list) {
139 return;
140 }
141 auto filter_value = params.extract_input<bke::SocketValueVariant>("Selection"_ustr);
142 if (filter_value.is_single()) {
143 if (filter_value.ensure_type<bool>()) {
144 output_lists(params, list, IndexMask(list->size()));
145 }
146 else {
147 output_lists(params, list, {});
148 }
149 }
150 else if (filter_value.is_context_dependent_field()) {
151 ListFieldContext field_context;
152 fn::FieldEvaluator field_evaluator(field_context, list->size());
153 field_evaluator.add(filter_value.extract<Field<bool>>());
154 field_evaluator.evaluate();
155 output_lists(params, list, field_evaluator.get_evaluated_as_mask(0));
156 }
157 else if (filter_value.is_list()) {
158 const GListPtr keep_list = *filter_value.get_if<GListPtr>();
159 const VArray<bool> values = keep_list->varray().typed<bool>();
160 if (values.size() < list->size()) {
161 params.error_message_add(NodeWarningType::Error, "\"Selection\" list is too small");
163 return;
164 }
165 IndexMaskMemory memory;
166 output_lists(params, list, IndexMask::from_bools(IndexRange(list->size()), values, memory));
167 }
168 else {
170 "\"Selection\" input must be a field or a list");
171 params.set_output("Selection"_ustr, std::move(list));
172 params.set_output("Inverted"_ustr, GList::from_garray(GArray<>(list->cpp_type(), 0)));
174 }
175}
176
177static void node_register()
178{
179 static bke::bNodeType ntype;
180 geo_node_type_base(&ntype, "GeometryNodeFilterList"_ustr);
181 ntype.ui_name = "Filter List";
182 ntype.ui_description = "Remove items from a list";
186 ntype.declare = node_declare;
189 node_rna(ntype.rna_ext.srna);
190}
192
193} // namespace blender::nodes::node_geo_filter_list_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
GSpan()=default
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
constexpr IndexRange()=default
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_as_mask(int field_index)
IndexMask complement(const IndexMask &universe, LinearAllocator<> &memory) const
static GListPtr from_garray(GArray<> array)
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)
ccl_device_inline float3x3 inverted(const float3x3 m)
#define T
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
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_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void output_lists(GeoNodeExecParams &params, const GListPtr &list, const IndexMask &selection)
static GListPtr filter_list(const GListPtr &list, const IndexMask &mask)
static void node_declare(NodeDeclarationBuilder &b)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_geo_exec(GeoNodeExecParams params)
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)