Blender V5.0
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")
35 .supported_type(GeometryComponent::Type::Curve)
36 .description("Curves to change the handles on");
37 b.add_output<decl::Geometry>("Curve").propagate_all().align_with_previous();
38 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
39 auto &position = b.add_input<decl::Vector>("Position");
40 if (node) {
41 const NodeGeometrySetCurveHandlePositions &storage = node_storage(*node);
42 position.implicit_field_on_all(storage.mode == GEO_NODE_CURVE_HANDLE_LEFT ?
45 }
46 b.add_input<decl::Vector>("Offset")
47 .default_value(float3(0.0f, 0.0f, 0.0f))
48 .subtype(PROP_TRANSLATION)
49 .field_on_all();
50}
51
52static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
53{
54 layout->prop(ptr, "mode", UI_ITEM_R_EXPAND, std::nullopt, ICON_NONE);
55}
56
65
69static bool update_handle_types_for_movement(int8_t &type, int8_t &other)
70{
71 switch (type) {
73 return false;
75 /* Converting auto handles to aligned handled instead of free handles is
76 * arbitrary, but expected and "standard" based on behavior in edit mode. */
77 if (other == BEZIER_HANDLE_AUTO) {
78 /* Convert pairs of auto handles to aligned handles when moving one side. */
80 other = BEZIER_HANDLE_ALIGN;
81 }
82 else {
83 /* If the other handle isn't automatic, just make the handle free. */
84 type = BEZIER_HANDLE_FREE;
85 }
86 return false;
88 type = BEZIER_HANDLE_FREE;
89 return true;
91 /* The handle can stay aligned if the other handle is also aligned (in which case the other
92 * handle should be updated to be consistent). But otherwise the handle must be made free to
93 * avoid conflicting with its "aligned" type. */
94 if (other != BEZIER_HANDLE_ALIGN) {
95 type = BEZIER_HANDLE_FREE;
96 }
97 return false;
98 }
99 return false;
100}
101
102static void set_position_in_component(Curves &curves_id,
104 const Field<bool> &selection_field,
105 const Field<float3> &position_field,
106 const Field<float3> &offset_field)
107{
108 bke::CurvesGeometry &curves = curves_id.geometry.wrap();
109 if (curves.is_empty()) {
110 return;
111 }
112
113 const bke::CurvesFieldContext field_context{curves_id, AttrDomain::Point};
114 fn::FieldEvaluator evaluator{field_context, curves.points_num()};
115 evaluator.set_selection(selection_field);
116 evaluator.add(position_field);
117 evaluator.add(offset_field);
118 evaluator.evaluate();
119 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
120 const VArray<float3> new_positions = evaluator.get_evaluated<float3>(0);
121 const VArray<float3> new_offsets = evaluator.get_evaluated<float3>(1);
122
123 Span<float3> positions = curves.positions();
124
125 const bool use_left = mode == GEO_NODE_CURVE_HANDLE_LEFT;
126 MutableSpan<int8_t> handle_types = use_left ? curves.handle_types_left_for_write() :
128 MutableSpan<int8_t> handle_types_other = use_left ? curves.handle_types_right_for_write() :
130 MutableSpan<float3> handle_positions = use_left ? curves.handle_positions_left_for_write() :
132 MutableSpan<float3> handle_positions_other = use_left ?
135
136 const bool types_changed = threading::parallel_reduce(
137 selection.index_range(),
138 2048,
139 false,
140 [&](const IndexRange range, bool changed) {
141 selection.slice(range).foreach_index_optimized<int>([&](const int i) {
142 if (update_handle_types_for_movement(handle_types[i], handle_types_other[i])) {
143 changed = true;
144 }
145 });
146 return changed;
147 },
148 std::logical_or<>());
149
150 selection.foreach_segment(GrainSize(2048), [&](const IndexMaskSegment segment) {
151 for (const int i : segment) {
153 HandleType(handle_types[i]),
154 HandleType(handle_types_other[i]),
155 new_positions[i] + new_offsets[i],
156 handle_positions[i],
157 handle_positions_other[i]);
158 }
159 });
160
161 if (types_changed) {
162 curves.tag_topology_changed();
163 }
164 curves.calculate_bezier_auto_handles();
165 curves.tag_positions_changed();
166}
167
169{
170 const NodeGeometrySetCurveHandlePositions &storage = node_storage(params.node());
172
173 GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve");
174 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
175 Field<float3> position_field = params.extract_input<Field<float3>>("Position");
176 Field<float3> offset_field = params.extract_input<Field<float3>>("Offset");
177
178 std::atomic<bool> has_curves = false;
179 std::atomic<bool> has_bezier = false;
180
181 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
182 if (Curves *curves_id = geometry_set.get_curves_for_write()) {
183 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
184 has_curves = true;
185 const AttributeAccessor attributes = curves.attributes();
186 if (!attributes.contains("handle_left") || !attributes.contains("handle_right")) {
187 return;
188 }
189 has_bezier = true;
190
191 set_position_in_component(*curves_id, mode, selection_field, position_field, offset_field);
192 }
193 });
194
195 if (has_curves && !has_bezier) {
196 params.error_message_add(NodeWarningType::Info, TIP_("Input curves do not have Bézier type"));
197 }
198
199 params.set_output("Curve", std::move(geometry_set));
200}
201
202static void node_rna(StructRNA *srna)
203{
205 "mode",
206 "Mode",
207 "Whether to update left and right handles",
211}
212
213static void node_register()
214{
215 static blender::bke::bNodeType ntype;
216
217 geo_node_type_base(&ntype, "GeometryNodeSetCurveHandlePositions", GEO_NODE_SET_CURVE_HANDLES);
218 ntype.ui_name = "Set Handle Positions";
219 ntype.ui_description = "Set the positions for the handles of Bézier curves";
220 ntype.enum_name_legacy = "SET_CURVE_HANDLES";
223 ntype.declare = node_declare;
224 ntype.minwidth = 100.0f;
225 ntype.initfunc = node_init;
227 "NodeGeometrySetCurveHandlePositions",
232
233 node_rna(ntype.rna_ext.srna);
234}
235NOD_REGISTER_NODE(node_register)
236
237} // namespace blender::nodes::node_geo_set_curve_handles_cc
Low-level operations for curves.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1240
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_SET_CURVE_HANDLES
#define TIP_(msgid)
HandleType
@ BEZIER_HANDLE_FREE
@ BEZIER_HANDLE_ALIGN
@ BEZIER_HANDLE_VECTOR
@ BEZIER_HANDLE_AUTO
@ NODE_DEFAULT_INPUT_HANDLE_RIGHT_FIELD
@ NODE_DEFAULT_INPUT_HANDLE_LEFT_FIELD
GeometryNodeCurveHandleMode
@ GEO_NODE_CURVE_HANDLE_LEFT
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
@ PROP_TRANSLATION
Definition RNA_types.hh:261
@ UI_ITEM_R_EXPAND
BMesh const char void * data
bool contains(StringRef attribute_id) const
MutableSpan< int8_t > handle_types_right_for_write()
MutableSpan< float3 > handle_positions_left_for_write()
MutableSpan< float3 > handle_positions_right_for_write()
Span< float3 > positions() const
AttributeAccessor attributes() const
MutableSpan< int8_t > handle_types_left_for_write()
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 * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
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:2416
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:5414
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
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)
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:151
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:42
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:54
const EnumPropertyItem rna_enum_node_geometry_curve_handle_side_items[]
CurvesGeometry geometry
StructRNA * srna
void * storage
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeDeclareFunction declare
Definition BKE_node.hh:362
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
i
Definition text_draw.cc:230
PointerRNA * ptr
Definition wm_files.cc:4238