Blender V5.3
node_geo_sort_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_index_mask.hh"
6
8#include "FN_field.hh"
10#include "GEO_reorder.hh"
11
16#include "NOD_rna_define.hh"
17#include "NOD_socket.hh"
19
20#include "RNA_enum_types.hh"
21
23#include "UI_resources.hh"
24
25#include "list_function_eval.hh"
26#include "node_geometry_util.hh"
27
29
31{
32 const bNode *node = b.node_or_null();
33 if (!node) {
34 return;
35 }
36
37 b.use_custom_socket_order();
38 b.allow_any_socket_order();
39 b.add_default_layout();
40
41 const auto type = eNodeSocketDatatype(node->custom1);
42 b.add_input(type, "List"_ustr).structure_type(StructureType::List).hide_value();
43 b.add_output(type, "List"_ustr)
44 .propagate_all({0})
45 .structure_type(StructureType::List)
46 .align_with_previous();
47
48 b.add_input<decl::Bool>("Selection"_ustr)
49 .default_value(true)
50 .hide_value()
51 .structure_type(StructureType::Dynamic)
52 .description("Whether each element should participate in sorting");
53 b.add_input<decl::Int>("Group ID"_ustr)
54 .default_value(0)
55 .hide_value()
56 .structure_type(StructureType::Dynamic)
57 .description("Elements with the same Group ID are sorted together");
58 b.add_input<decl::Float>("Sort Weight"_ustr)
59 .default_value(0.0f)
60 .hide_value()
61 .structure_type(StructureType::Dynamic)
62 .description("A field or list of values that will determine the sorted order");
63}
64
65static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
66{
67 layout.prop(ptr, "socket_type", UI_ITEM_NONE, "", ICON_NONE);
68}
69
71 public:
75 {
76 bNode &node = params.add_node("GeometryNodeSortList"_ustr);
77 node.custom1 = socket_type;
79 }
80};
81
83{
84 const eNodeSocketDatatype socket_type = eNodeSocketDatatype(params.other_socket().type);
85 if (params.in_out() == SOCK_IN) {
86 if (params.node_tree().typeinfo->validate_link(socket_type, SOCK_FLOAT)) {
87 params.add_item(IFACE_("Sort Weight"), SocketSearchOp{"Sort Weight"_ustr, SOCK_FLOAT});
88 }
89 params.add_item(IFACE_("List"), SocketSearchOp{"List"_ustr, socket_type});
90 }
91 else {
92 params.add_item(IFACE_("List"), SocketSearchOp{"List"_ustr, socket_type});
93 }
94}
95
96template<typename T>
97static void get_varray_or_evaluate(const int list_size,
99 ResourceScope &scope,
100 fn::FieldEvaluator &field_evaluator,
101 const T &default_value,
102 VArray<T> &r_varray)
103{
104 if (value.is_context_dependent_field()) {
105 field_evaluator.add(value.extract<fn::Field<T>>(), &r_varray);
106 }
107 else if (value.is_list()) {
108 auto list = value.extract<GListPtr>();
109 if (list && list->size() == list_size) {
110 r_varray = scope.add_value(std::move(list))->varray().typed<T>();
111 }
112 else {
113 r_varray = VArray<T>::from_single(default_value, list_size);
114 }
115 }
116 else {
117 r_varray = VArray<T>::from_single(value.extract<T>(), list_size);
118 }
119}
120
122{
123 GListPtr list = params.extract_input<GListPtr>("List"_ustr);
124 if (!list) {
126 return;
127 }
128
129 const int list_size = list->size();
130 if (list_size <= 1) {
131 params.set_output("List"_ustr, std::move(list));
132 return;
133 }
134
135 auto selection_variant = params.extract_input<bke::SocketValueVariant>("Selection"_ustr);
136 auto group_id_variant = params.extract_input<bke::SocketValueVariant>("Group ID"_ustr);
137 auto weights_variant = params.extract_input<bke::SocketValueVariant>("Sort Weight"_ustr);
138
139 ResourceScope scope;
140
141 ListFieldContext field_context;
142 fn::FieldEvaluator field_evaluator(field_context, list_size);
143
144 if (selection_variant.is_context_dependent_field()) {
145 field_evaluator.set_selection(selection_variant.extract<fn::Field<bool>>());
146 }
147 else if (selection_variant.is_list()) {
149 std::make_shared<SampleIndexFunction>(selection_variant.extract<GListPtr>()),
150 {fn::IndexFieldInput::get_field()});
151 field_evaluator.set_selection(fn::Field<bool>(std::move(fn)));
152 }
153 else {
154 field_evaluator.set_selection(fn::Field<bool>(selection_variant.extract<bool>()));
155 }
156
157 VArray<int> group_id;
158 get_varray_or_evaluate(list_size, group_id_variant, scope, field_evaluator, 0, group_id);
159
160 VArray<float> weights;
161 get_varray_or_evaluate(list_size, weights_variant, scope, field_evaluator, 0.0f, weights);
162
163 field_evaluator.evaluate();
164 const IndexMask selection = field_evaluator.get_evaluated_selection_as_mask();
165
166 const std::optional<Array<int>> sorted = geometry::sort_indices_by_weights(
167 list_size, selection, group_id, weights);
168 if (!sorted) {
169 params.set_output("List"_ustr, std::move(list));
170 return;
171 }
172
173 const CPPType &type = list->cpp_type();
174 const GList::DataVariant &list_data = list->data();
175
176 if (std::get_if<GList::SingleData>(&list_data)) {
177 params.set_output("List"_ustr, std::move(list));
178 return;
179 }
180
181 if (const auto *array_data = std::get_if<GList::ArrayData>(&list_data)) {
182 GList::ArrayData sorted_array_data = GList::ArrayData::ForConstructed(type, list_size);
183 const GSpan src_span(type, array_data->data, list_size);
184 GMutableSpan dst_span = sorted_array_data.span_for_write(type, list_size);
186 float2,
187 float3,
188 float4,
189 int,
190 int2,
191 bool,
192 int8_t,
193 short2,
197 float4x4,
199 std::string,
203 Material *,
204 Object *,
205 Collection *,
206 Image *,
207 VFont *,
208 Scene *,
209 bSound *>([&]<typename T>() {
210 array_utils::gather(src_span.typed<T>(), sorted->as_span(), dst_span.typed<T>());
211 });
212
213 GListPtr sorted_list = GList::create(type, std::move(sorted_array_data), list_size);
214 params.set_output("List"_ustr, std::move(sorted_list));
215 return;
216 }
217
219}
220
221static void node_rna(StructRNA *srna)
222{
224 srna,
225 "socket_type",
226 "Socket Type",
227 "",
231 [](bContext * /*C*/, PointerRNA *ptr, PropertyRNA * /*prop*/, bool *r_free) {
232 *r_free = true;
233 const bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
234 bke::bNodeTreeType *ntree_type = ntree.typeinfo;
235 return enum_items_filter(
238 return ntree_type->valid_socket_type(ntree_type, socket_type);
239 });
240 });
241}
242
243static void node_register()
244{
245 static bke::bNodeType ntype;
246 geo_node_type_base(&ntype, "GeometryNodeSortList"_ustr);
247 ntype.ui_name = "Sort List";
248 ntype.ui_description = "Sort a list based on weights";
252 ntype.declare = node_declare;
255 node_rna(ntype.rna_ext.srna);
256}
258
259} // namespace blender::nodes::node_geo_sort_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
void to_static_type(Fn &&fn) const
MutableSpan< T > typed() const
Span< T > typed() const
static VArray from_single(T value, const int64_t size)
void set_selection(Field< bool > selection)
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_selection_as_mask() const
static FieldOperationPtr from(std::shared_ptr< const mf::MultiFunction > fn, Vector< GField > inputs)
Definition field.cc:252
static ArrayData ForConstructed(const CPPType &type, int64_t size)
GMutableSpan span_for_write(const CPPType &type, int64_t size)
std::variant< ArrayData, SingleData > DataVariant
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)
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 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
std::optional< Array< int > > sort_indices_by_weights(int domain_size, const IndexMask &mask, const VArray< int > &group_id, const VArray< float > &weight)
Definition reorder.cc:495
QuaternionBase< float > Quaternion
static void node_rna(StructRNA *srna)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static void get_varray_or_evaluate(const int list_size, bke::SocketValueVariant &value, ResourceScope &scope, fn::FieldEvaluator &field_evaluator, const T &default_value, VArray< T > &r_varray)
static void node_gather_link_searches(GatherLinkSearchOpParams &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)
ImplicitSharingPtr< Closure > ClosurePtr
const EnumPropertyItem * enum_items_filter(const EnumPropertyItem *original_item_array, FunctionRef< bool(const EnumPropertyItem &item)> fn)
ImplicitSharingPtr< Bundle > BundlePtr
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int16_t, 2 > short2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
VecBase< float, 3 > float3
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
signed char int8_t
Definition BKE_text.h:76
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
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)