Blender V5.3
node_geo_set_curve_handles.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 <atomic>
6
7#include "BLI_task.hh"
8
9#include "BKE_curves.hh"
10
11#include "NOD_rna_define.hh"
12
14#include "UI_resources.hh"
15
16#include "RNA_enum_types.hh"
17
19
20#include "node_geometry_util.hh"
21
23
25
27{
28 b.use_custom_socket_order();
29 b.allow_any_socket_order();
30 b.add_default_layout();
31
32 const bNode *node = b.node_or_null();
33
34 b.add_input<decl::Geometry>("Curve"_ustr)
35 .supported_type(GeometryComponent::Type::Curve)
36 .description("Curves to change the handles on");
37 b.add_output<decl::Geometry>("Curve"_ustr).propagate_all_geometry().align_with_previous();
38 b.add_input<decl::Bool>("Selection"_ustr)
39 .default_value(true)
40 .hide_value()
41 .evaluated_geometry_field();
42 auto &position = b.add_input<decl::Vector>("Position"_ustr).evaluated_geometry_field();
43 if (node) {
44 const NodeGeometrySetCurveHandlePositions &storage = node_storage(*node);
45 position.default_input_type(storage.mode == GEO_NODE_CURVE_HANDLE_LEFT ?
48 }
49 b.add_input<decl::Vector>("Offset"_ustr)
50 .default_value(float3(0.0f, 0.0f, 0.0f))
51 .subtype(PROP_TRANSLATION)
52 .evaluated_geometry_field();
53}
54
55static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
56{
57 layout.prop(ptr, "mode", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
58}
59
60static void node_init(bNodeTree * /*tree*/, bNode *node)
61{
62 NodeGeometrySetCurveHandlePositions *data = MEM_new<NodeGeometrySetCurveHandlePositions>(
63 __func__);
64
66 node->storage = data;
67}
68
73{
74 switch (type) {
76 return false;
78 /* Converting auto handles to aligned handled instead of free handles is
79 * arbitrary, but expected and "standard" based on behavior in edit mode. */
80 if (other == BEZIER_HANDLE_AUTO) {
81 /* Convert pairs of auto handles to aligned handles when moving one side. */
83 other = BEZIER_HANDLE_ALIGN;
84 }
85 else {
86 /* If the other handle isn't automatic, just make the handle free. */
87 type = BEZIER_HANDLE_FREE;
88 }
89 return false;
91 type = BEZIER_HANDLE_FREE;
92 return true;
94 /* The handle can stay aligned if the other handle is also aligned (in which case the other
95 * handle should be updated to be consistent). But otherwise the handle must be made free to
96 * avoid conflicting with its "aligned" type. */
97 if (other != BEZIER_HANDLE_ALIGN) {
98 type = BEZIER_HANDLE_FREE;
99 }
100 return false;
101 }
102 return false;
103}
104
105static void set_position_in_component(Curves &curves_id,
107 const Field<bool> &selection_field,
108 const Field<float3> &position_field,
109 const Field<float3> &offset_field)
110{
111 bke::CurvesGeometry &curves = curves_id.geometry.wrap();
112 if (curves.is_empty()) {
113 return;
114 }
115
116 const bke::CurvesFieldContext field_context{curves_id, AttrDomain::Point};
117 fn::FieldEvaluator evaluator{field_context, curves.points_num()};
118 evaluator.set_selection(selection_field);
119 evaluator.add(position_field);
120 evaluator.add(offset_field);
121 evaluator.evaluate();
122 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
123 const VArray<float3> new_positions = evaluator.get_evaluated<float3>(0);
124 const VArray<float3> new_offsets = evaluator.get_evaluated<float3>(1);
125
126 Span<float3> positions = curves.positions();
127
128 const bool use_left = mode == GEO_NODE_CURVE_HANDLE_LEFT;
129 MutableSpan<int8_t> handle_types = use_left ? curves.handle_types_left_for_write() :
130 curves.handle_types_right_for_write();
131 MutableSpan<int8_t> handle_types_other = use_left ? curves.handle_types_right_for_write() :
132 curves.handle_types_left_for_write();
133 MutableSpan<float3> handle_positions = use_left ? curves.handle_positions_left_for_write() :
134 curves.handle_positions_right_for_write();
135 MutableSpan<float3> handle_positions_other = use_left ?
136 curves.handle_positions_right_for_write() :
137 curves.handle_positions_left_for_write();
138
139 const bool types_changed = threading::parallel_reduce(
140 selection.index_range(),
141 2048,
142 false,
143 [&](const IndexRange range, bool changed) {
144 selection.slice(range).foreach_index_optimized<int>([&](const int i) {
145 if (update_handle_types_for_movement(handle_types[i], handle_types_other[i])) {
146 changed = true;
147 }
148 });
149 return changed;
150 },
151 std::logical_or<>());
152
153 selection.foreach_segment(
154 [&](const IndexMaskSegment segment) {
155 for (const int i : segment) {
157 HandleType(handle_types[i]),
158 HandleType(handle_types_other[i]),
159 new_positions[i] + new_offsets[i],
160 handle_positions[i],
161 handle_positions_other[i]);
162 }
163 },
165
166 if (types_changed) {
167 curves.tag_topology_changed();
168 }
169 curves.calculate_bezier_auto_handles();
170 curves.tag_positions_changed();
171}
172
174{
175 const NodeGeometrySetCurveHandlePositions &storage = node_storage(params.node());
177
178 GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve"_ustr);
179 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection"_ustr);
180 Field<float3> position_field = params.extract_input<Field<float3>>("Position"_ustr);
181 Field<float3> offset_field = params.extract_input<Field<float3>>("Offset"_ustr);
182
183 std::atomic<bool> has_curves = false;
184 std::atomic<bool> has_bezier = false;
185
186 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
187 if (Curves *curves_id = geometry_set.get_curves_for_write()) {
188 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
189 has_curves = true;
190 const AttributeAccessor attributes = curves.attributes();
191 if (!attributes.contains("handle_left") || !attributes.contains("handle_right")) {
192 return;
193 }
194 has_bezier = true;
195
196 set_position_in_component(*curves_id, mode, selection_field, position_field, offset_field);
197 }
198 });
199
200 if (has_curves && !has_bezier) {
201 params.error_message_add(NodeWarningType::Info, TIP_("Input curves do not have Bézier type"));
202 }
203
204 params.set_output("Curve"_ustr, std::move(geometry_set));
205}
206
207static void node_rna(StructRNA *srna)
208{
210 "mode",
211 "Mode",
212 "Whether to update left and right handles",
216}
217
218static void node_register()
219{
220 static bke::bNodeType ntype;
221
223 &ntype, "GeometryNodeSetCurveHandlePositions"_ustr, GEO_NODE_SET_CURVE_HANDLES);
224 ntype.ui_name = "Set Handle Positions";
225 ntype.ui_description = "Set the positions for the handles of Bézier curves";
226 ntype.enum_name_legacy = "SET_CURVE_HANDLES";
229 ntype.declare = node_declare;
230 ntype.initfunc = node_init;
233 "NodeGeometrySetCurveHandlePositions",
238
239 node_rna(ntype.rna_ext.srna);
240}
241NOD_REGISTER_NODE(node_register)
242
243} // namespace blender::nodes::node_geo_set_curve_handles_cc
Low-level operations for curves.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_SET_CURVE_HANDLES
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
bool contains(StringRef name) const
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 error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
#define storage(slot, qualifiers)
#define position
void set_handle_position(const float3 &position, HandleType type, HandleType type_other, const float3 &new_handle, float3 &handle, float3 &handle_other)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
constexpr ParallelGrainSize grain_size(int grain_size)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static bool update_handle_types_for_movement(int8_t &type, int8_t &other)
static void set_position_in_component(Curves &curves_id, const GeometryNodeCurveHandleMode mode, const Field< bool > &selection_field, const Field< float3 > &position_field, const Field< float3 > &offset_field)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
BMesh const char void * data
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
VecBase< float, 3 > float3
@ PROP_TRANSLATION
Definition RNA_types.hh:318
const EnumPropertyItem rna_enum_node_geometry_curve_handle_side_items[]
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
signed char int8_t
Definition BKE_text.h:76
GeometryNodeCurveHandleMode
@ GEO_NODE_CURVE_HANDLE_LEFT
static constexpr int _160
Definition BKE_node.hh:246
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)