Blender V4.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.add_input<decl::Geometry>("Instances").only_instances();
18 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
19 b.add_input<decl::Vector>("Translation").subtype(PROP_TRANSLATION).field_on_all();
20 b.add_input<decl::Bool>("Local Space").default_value(true).field_on_all();
21 b.add_output<decl::Geometry>("Instances").propagate_all();
22}
23
25{
26 const bke::InstancesFieldContext context{instances};
27 fn::FieldEvaluator evaluator{context, instances.instances_num()};
28 evaluator.set_selection(params.extract_input<Field<bool>>("Selection"));
29 evaluator.add(params.extract_input<Field<float3>>("Translation"));
30 evaluator.add(params.extract_input<Field<bool>>("Local Space"));
31 evaluator.evaluate();
32
33 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
34 const VArray<float3> translations = evaluator.get_evaluated<float3>(0);
35 const VArray<bool> local_spaces = evaluator.get_evaluated<bool>(1);
36
37 MutableSpan<float4x4> transforms = instances.transforms_for_write();
38
39 selection.foreach_index(GrainSize(1024), [&](const int64_t i) {
40 if (local_spaces[i]) {
41 transforms[i] *= math::from_location<float4x4>(translations[i]);
42 }
43 else {
44 transforms[i].location() += translations[i];
45 }
46 });
47}
48
50{
51 GeometrySet geometry_set = params.extract_input<GeometrySet>("Instances");
52 if (bke::Instances *instances = geometry_set.get_instances_for_write()) {
53 translate_instances(params, *instances);
54 }
55 params.set_output("Instances", std::move(geometry_set));
56}
57
58static void register_node()
59{
60 static blender::bke::bNodeType ntype;
61
63 &ntype, GEO_NODE_TRANSLATE_INSTANCES, "Translate Instances", NODE_CLASS_GEOMETRY);
65 ntype.declare = node_declare;
67}
69
70} // namespace blender::nodes::node_geo_translate_instances_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:418
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_TRANSLATION
Definition RNA_types.hh:164
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
MatT from_location(const typename MatT::loc_type &location)
static void translate_instances(GeoNodeExecParams &params, bke::Instances &instances)
void geo_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
__int64 int64_t
Definition stdint.h:89
Instances * get_instances_for_write()
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