Blender V4.5
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").only_instances();
19 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
20 b.add_input<decl::Vector>("Position").implicit_field_on_all(NODE_DEFAULT_INPUT_POSITION_FIELD);
21 b.add_input<decl::Float>("Radius")
22 .default_value(0.05f)
23 .min(0.0f)
25 .field_on_all();
26 b.add_output<decl::Geometry>("Points").propagate_all();
27}
28
29static void convert_instances_to_points(GeometrySet &geometry_set,
30 Field<float3> position_field,
31 Field<float> radius_field,
32 Field<bool> selection_field,
33 const AttributeFilter &attribute_filter)
34{
35 const bke::Instances &instances = *geometry_set.get_instances();
36
37 const bke::InstancesFieldContext context{instances};
38 fn::FieldEvaluator evaluator{context, instances.instances_num()};
39 evaluator.set_selection(std::move(selection_field));
40 evaluator.add(std::move(position_field));
41 evaluator.add(std::move(radius_field));
42 evaluator.evaluate();
43 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
44 if (selection.is_empty()) {
45 return;
46 }
47 const VArray<float3> positions = evaluator.get_evaluated<float3>(0);
48 const VArray<float> radii = evaluator.get_evaluated<float>(1);
49
50 PointCloud *pointcloud = BKE_pointcloud_new_nomain(selection.size());
51 geometry_set.replace_pointcloud(pointcloud);
52 array_utils::gather(positions, selection, pointcloud->positions_for_write());
53
54 bke::MutableAttributeAccessor dst_attributes = pointcloud->attributes_for_write();
56 dst_attributes.lookup_or_add_for_write_only_span<float>("radius", AttrDomain::Point);
57 array_utils::gather(radii, selection, point_radii.span);
58 point_radii.finish();
59
60 const bke::AttributeAccessor src_attributes = instances.attributes();
61 Map<StringRef, AttributeDomainAndType> attributes_to_propagate;
62 geometry_set.gather_attributes_for_propagation({GeometryComponent::Type::Instance},
63 GeometryComponent::Type::PointCloud,
64 false,
65 attribute_filter,
66 attributes_to_propagate);
67 /* These two attributes are added by the implicit inputs above. */
68 attributes_to_propagate.remove("position");
69 attributes_to_propagate.remove("radius");
70
71 for (const auto item : attributes_to_propagate.items()) {
72 const StringRef id = item.key;
73 const eCustomDataType type = item.value.data_type;
74
75 const GAttributeReader src = src_attributes.lookup(id);
76 if (selection.size() == instances.instances_num() && src.sharing_info && src.varray.is_span())
77 {
79 *src.sharing_info);
80 dst_attributes.add(id, AttrDomain::Point, type, init);
81 }
82 else {
84 id, AttrDomain::Point, type);
85 array_utils::gather(src.varray, selection, dst.span);
86 dst.finish();
87 }
88 }
89}
90
92{
93 GeometrySet geometry_set = params.extract_input<GeometrySet>("Instances");
94
95 if (geometry_set.has_instances()) {
96 convert_instances_to_points(geometry_set,
97 params.extract_input<Field<float3>>("Position"),
98 params.extract_input<Field<float>>("Radius"),
99 params.extract_input<Field<bool>>("Selection"),
100 params.get_attribute_filter("Points"));
101 geometry_set.keep_only({GeometryComponent::Type::PointCloud, GeometryComponent::Type::Edit});
102 params.set_output("Points", std::move(geometry_set));
103 }
104 else {
105 params.set_default_remaining_outputs();
106 }
107}
108
109static void node_register()
110{
111 static blender::bke::bNodeType ntype;
112
113 geo_node_type_base(&ntype, "GeometryNodeInstancesToPoints", GEO_NODE_INSTANCES_TO_POINTS);
114 ntype.ui_name = "Instances to Points";
115 ntype.ui_description =
116 "Generate points at the origins of instances.\nNote: Nested instances are not affected by "
117 "this node";
118 ntype.enum_name_legacy = "INSTANCES_TO_POINTS";
120 ntype.declare = node_declare;
123}
125
126} // namespace blender::nodes::node_geo_instances_to_points_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
#define GEO_NODE_INSTANCES_TO_POINTS
General operations for point clouds.
PointCloud * BKE_pointcloud_new_nomain(int totpoint)
@ NODE_DEFAULT_INPUT_POSITION_FIELD
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_DISTANCE
Definition RNA_types.hh:244
void init()
const void * data() const
bool remove(const Key &key)
Definition BLI_map.hh:368
ItemIterator items() const &
Definition BLI_map.hh:902
GAttributeReader lookup(const StringRef attribute_id) const
bke::AttributeAccessor attributes() const
Definition instances.cc:63
int instances_num() const
Definition instances.cc:398
bool add(const StringRef attribute_id, const AttrDomain domain, const eCustomDataType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef attribute_id, AttrDomain domain, eCustomDataType 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:2748
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
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, Map< StringRef, AttributeDomainAndType > &r_attributes) const
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355