Blender V4.5
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
13#include "UI_interface.hh"
14#include "UI_resources.hh"
15
16#include "RNA_enum_types.hh"
17
18#include "node_geometry_util.hh"
19
21
23
25{
26 b.use_custom_socket_order();
27 b.allow_any_socket_order();
28 b.add_default_layout();
29
30 const bNode *node = b.node_or_null();
31
32 b.add_input<decl::Geometry>("Curve").supported_type(GeometryComponent::Type::Curve);
33 b.add_output<decl::Geometry>("Curve").propagate_all().align_with_previous();
34 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
35 auto &position = b.add_input<decl::Vector>("Position");
36 if (node) {
37 const NodeGeometrySetCurveHandlePositions &storage = node_storage(*node);
38 position.implicit_field_on_all(storage.mode == GEO_NODE_CURVE_HANDLE_LEFT ?
41 }
42 b.add_input<decl::Vector>("Offset")
43 .default_value(float3(0.0f, 0.0f, 0.0f))
44 .subtype(PROP_TRANSLATION)
45 .field_on_all();
46}
47
48static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
49{
50 layout->prop(ptr, "mode", UI_ITEM_R_EXPAND, std::nullopt, ICON_NONE);
51}
52
61
65static bool update_handle_types_for_movement(int8_t &type, int8_t &other)
66{
67 switch (type) {
69 return false;
71 /* Converting auto handles to aligned handled instead of free handles is
72 * arbitrary, but expected and "standard" based on behavior in edit mode. */
73 if (other == BEZIER_HANDLE_AUTO) {
74 /* Convert pairs of auto handles to aligned handles when moving one side. */
76 other = BEZIER_HANDLE_ALIGN;
77 }
78 else {
79 /* If the other handle isn't automatic, just make the handle free. */
80 type = BEZIER_HANDLE_FREE;
81 }
82 return false;
84 type = BEZIER_HANDLE_FREE;
85 return true;
87 /* The handle can stay aligned if the other handle is also aligned (in which case the other
88 * handle should be updated to be consistent). But otherwise the handle must be made free to
89 * avoid conflicting with its "aligned" type. */
90 if (other != BEZIER_HANDLE_ALIGN) {
91 type = BEZIER_HANDLE_FREE;
92 }
93 return false;
94 }
95 return false;
96}
97
98static void set_position_in_component(Curves &curves_id,
100 const Field<bool> &selection_field,
101 const Field<float3> &position_field,
102 const Field<float3> &offset_field)
103{
104 bke::CurvesGeometry &curves = curves_id.geometry.wrap();
105 if (curves.is_empty()) {
106 return;
107 }
108
109 const bke::CurvesFieldContext field_context{curves_id, AttrDomain::Point};
110 fn::FieldEvaluator evaluator{field_context, curves.points_num()};
111 evaluator.set_selection(selection_field);
112 evaluator.add(position_field);
113 evaluator.add(offset_field);
114 evaluator.evaluate();
115 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
116 const VArray<float3> new_positions = evaluator.get_evaluated<float3>(0);
117 const VArray<float3> new_offsets = evaluator.get_evaluated<float3>(1);
118
119 Span<float3> positions = curves.positions();
120
121 const bool use_left = mode == GEO_NODE_CURVE_HANDLE_LEFT;
122 MutableSpan<int8_t> handle_types = use_left ? curves.handle_types_left_for_write() :
124 MutableSpan<int8_t> handle_types_other = use_left ? curves.handle_types_right_for_write() :
126 MutableSpan<float3> handle_positions = use_left ? curves.handle_positions_left_for_write() :
128 MutableSpan<float3> handle_positions_other = use_left ?
131
132 const bool types_changed = threading::parallel_reduce(
133 selection.index_range(),
134 2048,
135 false,
136 [&](const IndexRange range, bool changed) {
137 selection.slice(range).foreach_index_optimized<int>([&](const int i) {
138 if (update_handle_types_for_movement(handle_types[i], handle_types_other[i])) {
139 changed = true;
140 }
141 });
142 return changed;
143 },
144 std::logical_or<>());
145
146 selection.foreach_segment(GrainSize(2048), [&](const IndexMaskSegment segment) {
147 for (const int i : segment) {
149 HandleType(handle_types[i]),
150 HandleType(handle_types_other[i]),
151 new_positions[i] + new_offsets[i],
152 handle_positions[i],
153 handle_positions_other[i]);
154 }
155 });
156
157 if (types_changed) {
158 curves.tag_topology_changed();
159 }
160 curves.calculate_bezier_auto_handles();
161 curves.tag_positions_changed();
162}
163
165{
166 const NodeGeometrySetCurveHandlePositions &storage = node_storage(params.node());
168
169 GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve");
170 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
171 Field<float3> position_field = params.extract_input<Field<float3>>("Position");
172 Field<float3> offset_field = params.extract_input<Field<float3>>("Offset");
173
174 std::atomic<bool> has_curves = false;
175 std::atomic<bool> has_bezier = false;
176
177 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
178 if (Curves *curves_id = geometry_set.get_curves_for_write()) {
179 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
180 has_curves = true;
181 const AttributeAccessor attributes = curves.attributes();
182 if (!attributes.contains("handle_left") || !attributes.contains("handle_right")) {
183 return;
184 }
185 has_bezier = true;
186
187 set_position_in_component(*curves_id, mode, selection_field, position_field, offset_field);
188 }
189 });
190
191 if (has_curves && !has_bezier) {
192 params.error_message_add(NodeWarningType::Info, TIP_("Input curves do not have Bézier type"));
193 }
194
195 params.set_output("Curve", std::move(geometry_set));
196}
197
198static void node_rna(StructRNA *srna)
199{
201 "mode",
202 "Mode",
203 "Whether to update left and right handles",
207}
208
209static void node_register()
210{
211 static blender::bke::bNodeType ntype;
212
213 geo_node_type_base(&ntype, "GeometryNodeSetCurveHandlePositions", GEO_NODE_SET_CURVE_HANDLES);
214 ntype.ui_name = "Set Handle Positions";
215 ntype.ui_description = "Set the positions for the handles of Bézier curves";
216 ntype.enum_name_legacy = "SET_CURVE_HANDLES";
219 ntype.declare = node_declare;
220 ntype.minwidth = 100.0f;
221 ntype.initfunc = node_init;
223 "NodeGeometrySetCurveHandlePositions",
228
229 node_rna(ntype.rna_ext.srna);
230}
231NOD_REGISTER_NODE(node_register)
232
233} // namespace blender::nodes::node_geo_set_curve_handles_cc
Low-level operations for curves.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1215
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
#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:249
@ 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:2748
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:5603
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
Definition RNA_types.hh:909
void * storage
void modify_geometry_sets(ForeachSubGeometryCallback callback)
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:355
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:4227