Blender V5.0
node_geo_instances_to_points.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
5#include "BLI_array_utils.hh"
6
8
9#include "BKE_instances.hh"
10#include "BKE_pointcloud.hh"
11
12#include "node_geometry_util.hh"
13
15
17{
18 b.add_input<decl::Geometry>("Instances")
19 .only_instances()
20 .description("Instances to convert to points");
21 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
22 b.add_input<decl::Vector>("Position").implicit_field_on_all(NODE_DEFAULT_INPUT_POSITION_FIELD);
23 b.add_input<decl::Float>("Radius")
24 .default_value(0.05f)
25 .min(0.0f)
27 .field_on_all();
28 b.add_output<decl::Geometry>("Points").propagate_all();
29}
30
31static void convert_instances_to_points(GeometrySet &geometry_set,
32 Field<float3> position_field,
33 Field<float> radius_field,
34 Field<bool> selection_field,
35 const AttributeFilter &attribute_filter)
36{
37 const bke::Instances &instances = *geometry_set.get_instances();
38
39 const bke::InstancesFieldContext context{instances};
40 fn::FieldEvaluator evaluator{context, instances.instances_num()};
41 evaluator.set_selection(std::move(selection_field));
42 evaluator.add(std::move(position_field));
43 evaluator.add(std::move(radius_field));
44 evaluator.evaluate();
45 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
46 if (selection.is_empty()) {
47 return;
48 }
49 const VArray<float3> positions = evaluator.get_evaluated<float3>(0);
50 const VArray<float> radii = evaluator.get_evaluated<float>(1);
51
52 PointCloud *pointcloud = BKE_pointcloud_new_nomain(selection.size());
53 geometry_set.replace_pointcloud(pointcloud);
54 array_utils::gather(positions, selection, pointcloud->positions_for_write());
55
56 bke::MutableAttributeAccessor dst_attributes = pointcloud->attributes_for_write();
58 dst_attributes.lookup_or_add_for_write_only_span<float>("radius", AttrDomain::Point);
59 array_utils::gather(radii, selection, point_radii.span);
60 point_radii.finish();
61
62 const bke::AttributeAccessor src_attributes = instances.attributes();
63 bke::GeometrySet::GatheredAttributes attributes_to_propagate;
64 geometry_set.gather_attributes_for_propagation({GeometryComponent::Type::Instance},
65 GeometryComponent::Type::PointCloud,
66 false,
67 attribute_filter,
68 attributes_to_propagate);
69
70 for (const int i : attributes_to_propagate.names.index_range()) {
71 /* These two attributes are added by the implicit inputs above. */
72 if (ELEM(attributes_to_propagate.names[i], "position", "radius")) {
73 continue;
74 }
75 const StringRef id = attributes_to_propagate.names[i];
76 const bke::AttrType type = attributes_to_propagate.kinds[i].data_type;
77
78 const GAttributeReader src = src_attributes.lookup(id);
79 if (selection.size() == instances.instances_num() && src.sharing_info && src.varray.is_span())
80 {
82 *src.sharing_info);
83 dst_attributes.add(id, AttrDomain::Point, type, init);
84 }
85 else {
87 id, AttrDomain::Point, type);
88 array_utils::gather(src.varray, selection, dst.span);
89 dst.finish();
90 }
91 }
92}
93
95{
96 GeometrySet geometry_set = params.extract_input<GeometrySet>("Instances");
97
98 if (geometry_set.has_instances()) {
99 convert_instances_to_points(geometry_set,
100 params.extract_input<Field<float3>>("Position"),
101 params.extract_input<Field<float>>("Radius"),
102 params.extract_input<Field<bool>>("Selection"),
103 params.get_attribute_filter("Points"));
104 geometry_set.keep_only({GeometryComponent::Type::PointCloud, GeometryComponent::Type::Edit});
105 params.set_output("Points", std::move(geometry_set));
106 }
107 else {
108 params.set_default_remaining_outputs();
109 }
110}
111
112static void node_register()
113{
114 static blender::bke::bNodeType ntype;
115
116 geo_node_type_base(&ntype, "GeometryNodeInstancesToPoints", GEO_NODE_INSTANCES_TO_POINTS);
117 ntype.ui_name = "Instances to Points";
118 ntype.ui_description =
119 "Generate points at the origins of instances.\nNote: Nested instances are not affected by "
120 "this node";
121 ntype.enum_name_legacy = "INSTANCES_TO_POINTS";
123 ntype.declare = node_declare;
126}
128
129} // namespace blender::nodes::node_geo_instances_to_points_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_INSTANCES_TO_POINTS
General operations for point clouds.
PointCloud * BKE_pointcloud_new_nomain(int totpoint)
#define ELEM(...)
@ NODE_DEFAULT_INPUT_POSITION_FIELD
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_DISTANCE
Definition RNA_types.hh:256
void init()
const void * data() const
GAttributeReader lookup(const StringRef attribute_id) const
bke::AttributeAccessor attributes() const
Definition instances.cc:64
int instances_num() const
Definition instances.cc:393
bool add(const StringRef attribute_id, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef attribute_id, AttrDomain domain, AttrType data_type)
void set_selection(Field< bool > selection)
Definition FN_field.hh:383
int add(GField field, GVArray *varray_ptr)
Definition field.cc:751
IndexMask get_evaluated_selection_as_mask() const
Definition field.cc:817
const GVArray & get_evaluated(const int field_index) const
Definition FN_field.hh:448
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, int64_t grain_size=4096)
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
static void convert_instances_to_points(GeometrySet &geometry_set, Field< float3 > position_field, Field< float > radius_field, Field< bool > selection_field, const AttributeFilter &attribute_filter)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
static void init(bNodeTree *, bNode *node)
const ImplicitSharingInfo * sharing_info
Vector< AttributeDomainAndType, 16 > kinds
void keep_only(Span< GeometryComponent::Type > component_types)
void replace_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const Instances * get_instances() const
void gather_attributes_for_propagation(Span< GeometryComponent::Type > component_types, GeometryComponent::Type dst_component_type, bool include_instances, const AttributeFilter &attribute_filter, GatheredAttributes &r_attributes) const
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
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362
i
Definition text_draw.cc:230