Blender V4.3
node_geo_set_id.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
8
10{
11 b.add_input<decl::Geometry>("Geometry");
12 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
13 b.add_input<decl::Int>("ID").implicit_field_on_all(implicit_field_inputs::index);
14 b.add_output<decl::Geometry>("Geometry").propagate_all();
15}
16
18 const Field<bool> &selection_field,
19 const Field<int> &id_field)
20{
21 const AttrDomain domain = (component.type() == GeometryComponent::Type::Instance) ?
22 AttrDomain::Instance :
23 AttrDomain::Point;
24 const int domain_size = component.attribute_domain_size(domain);
25 if (domain_size == 0) {
26 return;
27 }
28 MutableAttributeAccessor attributes = *component.attributes_for_write();
29
30 const bke::GeometryFieldContext field_context{component, domain};
31 fn::FieldEvaluator evaluator{field_context, domain_size};
32 evaluator.set_selection(selection_field);
33
34 /* Since adding the ID attribute can change the result of the field evaluation (the random value
35 * node uses the index if the ID is unavailable), make sure that it isn't added before evaluating
36 * the field. However, as an optimization, use a faster code path when it already exists. */
37 if (attributes.contains("id")) {
38 AttributeWriter<int> id_attribute = attributes.lookup_or_add_for_write<int>("id", domain);
39 evaluator.add_with_destination(id_field, id_attribute.varray);
40 evaluator.evaluate();
41 id_attribute.finish();
42 }
43 else {
44 evaluator.add(id_field);
45 evaluator.evaluate();
46 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
47 const VArray<int> result_ids = evaluator.get_evaluated<int>(0);
48 SpanAttributeWriter<int> id_attribute = attributes.lookup_or_add_for_write_span<int>("id",
49 domain);
50 result_ids.materialize(selection, id_attribute.span);
51 id_attribute.finish();
52 }
53}
54
56{
57 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
58 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
59 Field<int> id_field = params.extract_input<Field<int>>("ID");
60
65 {
66 if (geometry_set.has(type)) {
67 set_id_in_component(geometry_set.get_component_for_write(type), selection_field, id_field);
68 }
69 }
70
71 params.set_output("Geometry", std::move(geometry_set));
72}
73
74static void node_register()
75{
76 static blender::bke::bNodeType ntype;
77
78 geo_node_type_base(&ntype, GEO_NODE_SET_ID, "Set ID", NODE_CLASS_GEOMETRY);
80 ntype.declare = node_declare;
82}
84
85} // namespace blender::nodes::node_geo_set_id_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:418
#define NOD_REGISTER_NODE(REGISTER_FUNC)
void materialize(MutableSpan< T > r_span) const
int attribute_domain_size(AttrDomain domain) const
virtual std::optional< MutableAttributeAccessor > attributes_for_write()
void set_selection(Field< bool > selection)
Definition FN_field.hh:385
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
void index(const bNode &, void *r_value)
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
static void set_id_in_component(GeometryComponent &component, const Field< bool > &selection_field, const Field< int > &id_field)
void geo_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
bool has(const GeometryComponent::Type component_type) const
Defines a node type.
Definition BKE_node.hh:218
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
NodeDeclareFunction declare
Definition BKE_node.hh:347