Blender V4.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
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.add_input<decl::Geometry>("Curve").supported_type(GeometryComponent::Type::Curve);
27 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
28 b.add_input<decl::Vector>("Position")
29 .implicit_field_on_all([](const bNode &node, void *r_value) {
30 const StringRef side = node_storage(node).mode == GEO_NODE_CURVE_HANDLE_LEFT ?
31 "handle_left" :
32 "handle_right";
34 });
35 b.add_input<decl::Vector>("Offset").default_value(float3(0.0f, 0.0f, 0.0f)).field_on_all();
36 b.add_output<decl::Geometry>("Curve").propagate_all();
37}
38
39static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
40{
41 uiItemR(layout, ptr, "mode", UI_ITEM_R_EXPAND, nullptr, ICON_NONE);
42}
43
44static void node_init(bNodeTree * /*tree*/, bNode *node)
45{
46 NodeGeometrySetCurveHandlePositions *data = MEM_cnew<NodeGeometrySetCurveHandlePositions>(
47 __func__);
48
49 data->mode = GEO_NODE_CURVE_HANDLE_LEFT;
50 node->storage = data;
51}
52
54{
55 switch (type) {
57 break;
59 /* Converting auto handles to aligned handled instead of free handles is
60 * arbitrary, but expected and "standard" based on behavior in edit mode. */
61 if (other == BEZIER_HANDLE_AUTO) {
62 /* Convert pairs of auto handles to aligned handles when moving one side. */
64 other = BEZIER_HANDLE_ALIGN;
65 }
66 else {
67 /* If the other handle isn't automatic, just make the handle free. */
68 type = BEZIER_HANDLE_FREE;
69 }
70 break;
72 type = BEZIER_HANDLE_FREE;
73 break;
75 /* The handle can stay aligned if the other handle is also aligned (in which case the other
76 * handle should be updated to be consistent). But otherwise the handle must be made free to
77 * avoid conflicting with its "aligned" type. */
78 if (other != BEZIER_HANDLE_ALIGN) {
79 type = BEZIER_HANDLE_FREE;
80 }
81 break;
82 }
83}
84
85static void set_position_in_component(Curves &curves_id,
87 const Field<bool> &selection_field,
88 const Field<float3> &position_field,
89 const Field<float3> &offset_field)
90{
91 bke::CurvesGeometry &curves = curves_id.geometry.wrap();
92 if (curves.points_num() == 0) {
93 return;
94 }
95
96 const bke::CurvesFieldContext field_context{curves_id, AttrDomain::Point};
97 fn::FieldEvaluator evaluator{field_context, curves.points_num()};
98 evaluator.set_selection(selection_field);
99 evaluator.add(position_field);
100 evaluator.add(offset_field);
101 evaluator.evaluate();
102 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
103 const VArray<float3> new_positions = evaluator.get_evaluated<float3>(0);
104 const VArray<float3> new_offsets = evaluator.get_evaluated<float3>(1);
105
106 Span<float3> positions = curves.positions();
107
108 const bool use_left = mode == GEO_NODE_CURVE_HANDLE_LEFT;
109 MutableSpan<int8_t> handle_types = use_left ? curves.handle_types_left_for_write() :
110 curves.handle_types_right_for_write();
111 MutableSpan<int8_t> handle_types_other = use_left ? curves.handle_types_right_for_write() :
112 curves.handle_types_left_for_write();
113 MutableSpan<float3> handle_positions = use_left ? curves.handle_positions_left_for_write() :
114 curves.handle_positions_right_for_write();
115 MutableSpan<float3> handle_positions_other = use_left ?
116 curves.handle_positions_right_for_write() :
117 curves.handle_positions_left_for_write();
118
119 selection.foreach_segment(GrainSize(2048), [&](const IndexMaskSegment segment) {
120 for (const int i : segment) {
121 update_handle_types_for_movement(handle_types[i], handle_types_other[i]);
122 }
123 for (const int i : segment) {
125 HandleType(handle_types[i]),
126 HandleType(handle_types_other[i]),
127 new_positions[i] + new_offsets[i],
128 handle_positions[i],
129 handle_positions_other[i]);
130 }
131 });
132
133 curves.calculate_bezier_auto_handles();
134
135 curves.tag_positions_changed();
136}
137
139{
140 const NodeGeometrySetCurveHandlePositions &storage = node_storage(params.node());
142
143 GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve");
144 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
145 Field<float3> position_field = params.extract_input<Field<float3>>("Position");
146 Field<float3> offset_field = params.extract_input<Field<float3>>("Offset");
147
148 std::atomic<bool> has_curves = false;
149 std::atomic<bool> has_bezier = false;
150
151 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
152 if (Curves *curves_id = geometry_set.get_curves_for_write()) {
153 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
154 has_curves = true;
155 const AttributeAccessor attributes = curves.attributes();
156 if (!attributes.contains("handle_left") || !attributes.contains("handle_right")) {
157 return;
158 }
159 has_bezier = true;
160
161 set_position_in_component(*curves_id, mode, selection_field, position_field, offset_field);
162 }
163 });
164
165 if (has_curves && !has_bezier) {
166 params.error_message_add(NodeWarningType::Info, TIP_("Input curves do not have Bézier type"));
167 }
168
169 params.set_output("Curve", std::move(geometry_set));
170}
171
172static void node_rna(StructRNA *srna)
173{
175 "mode",
176 "Mode",
177 "Whether to update left and right handles",
181}
182
183static void node_register()
184{
185 static blender::bke::bNodeType ntype;
186
188 &ntype, GEO_NODE_SET_CURVE_HANDLES, "Set Handle Positions", NODE_CLASS_GEOMETRY);
190 ntype.declare = node_declare;
191 ntype.minwidth = 100.0f;
192 ntype.initfunc = node_init;
194 "NodeGeometrySetCurveHandlePositions",
199
200 node_rna(ntype.rna_ext.srna);
201}
203
204} // namespace blender::nodes::node_geo_set_curve_handles_cc
Low-level operations for curves.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1799
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:418
#define TIP_(msgid)
HandleType
@ BEZIER_HANDLE_FREE
@ BEZIER_HANDLE_ALIGN
@ BEZIER_HANDLE_VECTOR
@ BEZIER_HANDLE_AUTO
GeometryNodeCurveHandleMode
@ GEO_NODE_CURVE_HANDLE_LEFT
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
@ UI_ITEM_R_EXPAND
static fn::GField Create(std::string name, const CPPType &type, std::optional< std::string > socket_inspection_name=std::nullopt)
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 set_handle_position(const float3 &position, HandleType type, HandleType type_other, const float3 &new_handle, float3 &handle, float3 &handle_other)
void node_type_storage(bNodeType *ntype, const char *storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:4632
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
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 update_handle_types_for_movement(int8_t &type, int8_t &other)
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)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:46
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:58
const EnumPropertyItem rna_enum_node_geometry_curve_handle_side_items[]
signed char int8_t
Definition stdint.h:75
CurvesGeometry geometry
StructRNA * srna
Definition RNA_types.hh:780
void modify_geometry_sets(ForeachSubGeometryCallback callback)
Defines a node type.
Definition BKE_node.hh:218
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:267
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeDeclareFunction declare
Definition BKE_node.hh:347
PointerRNA * ptr
Definition wm_files.cc:4126