Blender V4.5
node_geo_translate_instances.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_task.hh"
6
7#include "BLI_math_matrix.hh"
8
9#include "BKE_instances.hh"
10
11#include "node_geometry_util.hh"
12
14
16{
17 b.use_custom_socket_order();
18 b.allow_any_socket_order();
19 b.add_input<decl::Geometry>("Instances").only_instances();
20 b.add_output<decl::Geometry>("Instances").propagate_all().align_with_previous();
21 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
22 b.add_input<decl::Vector>("Translation").subtype(PROP_TRANSLATION).field_on_all();
23 b.add_input<decl::Bool>("Local Space").default_value(true).field_on_all();
24}
25
27{
28 const bke::InstancesFieldContext context{instances};
29 fn::FieldEvaluator evaluator{context, instances.instances_num()};
30 evaluator.set_selection(params.extract_input<Field<bool>>("Selection"));
31 evaluator.add(params.extract_input<Field<float3>>("Translation"));
32 evaluator.add(params.extract_input<Field<bool>>("Local Space"));
33 evaluator.evaluate();
34
35 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
36 const VArray<float3> translations = evaluator.get_evaluated<float3>(0);
37 const VArray<bool> local_spaces = evaluator.get_evaluated<bool>(1);
38
39 MutableSpan<float4x4> transforms = instances.transforms_for_write();
40
41 selection.foreach_index(GrainSize(1024), [&](const int64_t i) {
42 if (local_spaces[i]) {
43 transforms[i] *= math::from_location<float4x4>(translations[i]);
44 }
45 else {
46 transforms[i].location() += translations[i];
47 }
48 });
49}
50
52{
53 GeometrySet geometry_set = params.extract_input<GeometrySet>("Instances");
54 if (bke::Instances *instances = geometry_set.get_instances_for_write()) {
55 translate_instances(params, *instances);
56 }
57 params.set_output("Instances", std::move(geometry_set));
58}
59
60static void register_node()
61{
62 static blender::bke::bNodeType ntype;
63
64 geo_node_type_base(&ntype, "GeometryNodeTranslateInstances", GEO_NODE_TRANSLATE_INSTANCES);
65 ntype.ui_name = "Translate Instances";
66 ntype.ui_description = "Move top-level geometry instances in local or global space";
67 ntype.enum_name_legacy = "TRANSLATE_INSTANCES";
70 ntype.declare = node_declare;
72}
74
75} // namespace blender::nodes::node_geo_translate_instances_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
#define GEO_NODE_TRANSLATE_INSTANCES
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_TRANSLATION
Definition RNA_types.hh:249
long long int int64_t
int instances_num() const
Definition instances.cc:398
MutableSpan< float4x4 > transforms_for_write()
Definition instances.cc:240
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
void foreach_index(Fn &&fn) const
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
MatT from_location(const typename MatT::loc_type &location)
static void translate_instances(GeoNodeExecParams &params, bke::Instances &instances)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
Instances * get_instances_for_write()
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
i
Definition text_draw.cc:230