Blender V5.3
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"_ustr)
20 .only_instances()
21 .description("Instances to translate individually");
22 b.add_output<decl::Geometry>("Instances"_ustr).propagate_all_geometry().align_with_previous();
23 b.add_input<decl::Bool>("Selection"_ustr)
24 .default_value(true)
25 .hide_value()
26 .evaluated_geometry_field();
27 b.add_input<decl::Vector>("Translation"_ustr)
28 .subtype(PROP_TRANSLATION)
29 .evaluated_geometry_field();
30 b.add_input<decl::Bool>("Local Space"_ustr).default_value(true).evaluated_geometry_field();
31}
32
34{
35 const bke::InstancesFieldContext context{instances};
36 fn::FieldEvaluator evaluator{context, instances.instances_num()};
37 evaluator.set_selection(params.extract_input<Field<bool>>("Selection"_ustr));
38 evaluator.add(params.extract_input<Field<float3>>("Translation"_ustr));
39 evaluator.add(params.extract_input<Field<bool>>("Local Space"_ustr));
40 evaluator.evaluate();
41
42 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
43 const VArray<float3> translations = evaluator.get_evaluated<float3>(0);
44 const VArray<bool> local_spaces = evaluator.get_evaluated<bool>(1);
45
46 MutableSpan<float4x4> transforms = instances.transforms_for_write();
47
48 selection.foreach_index(
49 [&](const int64_t i) {
50 if (local_spaces[i]) {
51 transforms[i] *= math::from_location<float4x4>(translations[i]);
52 }
53 else {
54 transforms[i].location() += translations[i];
55 }
56 },
58}
59
61{
62 GeometrySet geometry_set = params.extract_input<GeometrySet>("Instances"_ustr);
63 if (bke::Instances *instances = geometry_set.get_instances_for_write()) {
64 translate_instances(params, *instances);
65 }
66 params.set_output("Instances"_ustr, std::move(geometry_set));
67}
68
69static void register_node()
70{
71 static bke::bNodeType ntype;
72
73 geo_node_type_base(&ntype, "GeometryNodeTranslateInstances"_ustr, GEO_NODE_TRANSLATE_INSTANCES);
74 ntype.ui_name = "Translate Instances";
75 ntype.ui_description = "Move top-level geometry instances in local or global space";
76 ntype.enum_name_legacy = "TRANSLATE_INSTANCES";
79 ntype.declare = node_declare;
81}
83
84} // namespace blender::nodes::node_geo_translate_instances_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_TRANSLATE_INSTANCES
#define NOD_REGISTER_NODE(REGISTER_FUNC)
int instances_num() const
Definition instances.cc:408
MutableSpan< float4x4 > transforms_for_write()
Definition instances.cc:242
void set_selection(Field< bool > selection)
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_selection_as_mask() const
const GVArray & get_evaluated(const int field_index) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
constexpr ParallelGrainSize grain_size(int grain_size)
MatT from_location(const typename MatT::loc_type &location)
static void translate_instances(GeoNodeExecParams &params, bke::Instances &instances)
__int64 int64_t
Definition BKE_text.h:90
VecBase< float, 3 > float3
@ PROP_TRANSLATION
Definition RNA_types.hh:318
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Instances * get_instances_for_write()
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393