Blender V5.3
node_geo_curve_resample.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
7
8#include "BKE_curves.hh"
10
12#include "UI_resources.hh"
13
14#include "NOD_rna_define.hh"
15
16#include "node_geometry_util.hh"
17
19
21
24 "EVALUATED",
25 0,
26 N_("Evaluated"),
27 N_("Output the input spline's evaluated points, based on the resolution attribute for NURBS "
28 "and Bézier splines. Poly splines are unchanged")},
30 "COUNT",
31 0,
32 N_("Count"),
33 N_("Sample the specified number of points along each spline")},
35 "LENGTH",
36 0,
37 N_("Length"),
38 N_("Calculate the number of samples by splitting each spline into segments with the "
39 "specified "
40 "length")},
41 {0, nullptr, 0, nullptr, nullptr},
42};
43
45{
46 b.use_custom_socket_order();
47 b.allow_any_socket_order();
48 b.add_input<decl::Geometry>("Curve"_ustr)
50 .description("Curves to resample");
51 b.add_output<decl::Geometry>("Curve"_ustr).propagate_all_geometry().align_with_previous();
52 b.add_input<decl::Bool>("Selection"_ustr)
53 .default_value(true)
54 .evaluated_geometry_field()
55 .hide_value();
56 b.add_input<decl::Menu>("Mode"_ustr)
57 .static_items(mode_items)
59 .description("How to specify the amount of samples");
60 b.add_input<decl::Int>("Count"_ustr)
61 .default_value(10)
62 .min(1)
63 .max(100000)
64 .evaluated_geometry_field()
65 .usage_by_single_menu(GEO_NODE_CURVE_RESAMPLE_COUNT);
66 b.add_input<decl::Float>("Length"_ustr)
67 .default_value(0.1f)
68 .min(0.01f)
70 .evaluated_geometry_field()
71 .usage_by_single_menu(GEO_NODE_CURVE_RESAMPLE_LENGTH);
72}
73
74static void node_layout_ex(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
75{
76 layout.prop(ptr, "keep_last_segment", UI_ITEM_NONE, std::nullopt, ICON_NONE);
77}
78
79static void node_init(bNodeTree * /*tree*/, bNode *node)
80{
81 NodeGeometryCurveResample *data = MEM_new<NodeGeometryCurveResample>(__func__);
82 data->keep_last_segment = true;
83 node->storage = data;
84}
85
87{
88 GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve"_ustr);
89 const auto mode = params.extract_input<GeometryNodeCurveResampleMode>("Mode"_ustr);
90
91 const NodeGeometryCurveResample &storage = node_storage(params.node());
92
93 const Field<bool> selection = params.extract_input<Field<bool>>("Selection"_ustr);
94
96
97 switch (mode) {
99 Field<int> count = params.extract_input<Field<int>>("Count"_ustr);
101 if (const Curves *src_curves_id = geometry.get_curves()) {
102 const bke::CurvesGeometry &src_curves = src_curves_id->geometry.wrap();
103 const bke::CurvesFieldContext field_context{*src_curves_id, AttrDomain::Curve};
105 src_curves, field_context, selection, count);
106 Curves *dst_curves_id = bke::curves_new_nomain(std::move(dst_curves));
107 bke::curves_copy_parameters(*src_curves_id, *dst_curves_id);
108 geometry.replace_curves(dst_curves_id);
109 }
110 if (GreasePencil *grease_pencil = geometry.get_grease_pencil_for_write()) {
111 using namespace blender::bke::greasepencil;
112 for (const int layer_index : grease_pencil->layers().index_range()) {
113 Drawing *drawing = grease_pencil->get_eval_drawing(grease_pencil->layer(layer_index));
114
115 if (drawing == nullptr) {
116 continue;
117 }
118 const bke::CurvesGeometry &src_curves = drawing->strokes();
119 const bke::GreasePencilLayerFieldContext field_context(
120 *grease_pencil, AttrDomain::Curve, layer_index);
122 src_curves, field_context, selection, count);
123 drawing->strokes_for_write() = std::move(dst_curves);
124 drawing->tag_topology_changed();
125 }
126 }
127 });
128 break;
129 }
131 Field<float> length = params.extract_input<Field<float>>("Length"_ustr);
133 if (const Curves *src_curves_id = geometry.get_curves()) {
134 const bke::CurvesGeometry &src_curves = src_curves_id->geometry.wrap();
135 const bke::CurvesFieldContext field_context{*src_curves_id, AttrDomain::Curve};
137 src_curves, field_context, selection, length, {}, storage.keep_last_segment);
138 Curves *dst_curves_id = bke::curves_new_nomain(std::move(dst_curves));
139 bke::curves_copy_parameters(*src_curves_id, *dst_curves_id);
140 geometry.replace_curves(dst_curves_id);
141 }
142 if (GreasePencil *grease_pencil = geometry.get_grease_pencil_for_write()) {
143 using namespace blender::bke::greasepencil;
144 for (const int layer_index : grease_pencil->layers().index_range()) {
145 Drawing *drawing = grease_pencil->get_eval_drawing(grease_pencil->layer(layer_index));
146 if (drawing == nullptr) {
147 continue;
148 }
149 const bke::CurvesGeometry &src_curves = drawing->strokes();
150 const bke::GreasePencilLayerFieldContext field_context(
151 *grease_pencil, AttrDomain::Curve, layer_index);
153 src_curves, field_context, selection, length, {}, storage.keep_last_segment);
154 drawing->strokes_for_write() = std::move(dst_curves);
155 drawing->tag_topology_changed();
156 }
157 }
158 });
159 break;
160 }
163 if (const Curves *src_curves_id = geometry.get_curves()) {
164 const bke::CurvesGeometry &src_curves = src_curves_id->geometry.wrap();
165 const bke::CurvesFieldContext field_context{*src_curves_id, AttrDomain::Curve};
167 src_curves, field_context, selection);
168 Curves *dst_curves_id = bke::curves_new_nomain(std::move(dst_curves));
169 bke::curves_copy_parameters(*src_curves_id, *dst_curves_id);
170 geometry.replace_curves(dst_curves_id);
171 }
172 if (GreasePencil *grease_pencil = geometry.get_grease_pencil_for_write()) {
173 using namespace blender::bke::greasepencil;
174 for (const int layer_index : grease_pencil->layers().index_range()) {
175 Drawing *drawing = grease_pencil->get_eval_drawing(grease_pencil->layer(layer_index));
176 if (drawing == nullptr) {
177 continue;
178 }
179 const bke::CurvesGeometry &src_curves = drawing->strokes();
180 const bke::GreasePencilLayerFieldContext field_context(
181 *grease_pencil, AttrDomain::Curve, layer_index);
183 src_curves, field_context, selection);
184 drawing->strokes_for_write() = std::move(dst_curves);
185 drawing->tag_topology_changed();
186 }
187 }
188 });
189 break;
190 }
191
192 params.set_output("Curve"_ustr, std::move(geometry_set));
193}
194
195static void node_rna(StructRNA *srna)
196{
198 "keep_last_segment",
199 "Keep Last Segment",
200 "Do not collapse curves to single points if they are shorter than the "
201 "given length. The collapsing behavior exists for compatibility reasons.",
202 NOD_storage_boolean_accessors(keep_last_segment, 1),
203 true);
204}
205
206static void node_register()
207{
208 static bke::bNodeType ntype;
209
210 geo_node_type_base(&ntype, "GeometryNodeResampleCurve"_ustr, GEO_NODE_RESAMPLE_CURVE);
211 ntype.ui_name = "Resample Curve";
212 ntype.ui_description = "Generate a poly spline for each input spline";
213 ntype.enum_name_legacy = "RESAMPLE_CURVE";
215 ntype.declare = node_declare;
218 ntype, "NodeGeometryCurveResample", node_free_standard_storage, node_copy_standard_storage);
219 ntype.initfunc = node_init;
222
223 node_rna(ntype.rna_ext.srna);
224}
226
227} // namespace blender::nodes::node_geo_curve_resample_cc
Low-level operations for curves.
Low-level operations for grease pencil.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_RESAMPLE_CURVE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_boolean_accessors(member, flag)
#define UI_ITEM_NONE
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
static void remember_deformed_positions_if_necessary(GeometrySet &geometry)
#define storage(slot, qualifiers)
float length(VecOp< float, D >) RET
void curves_copy_parameters(const Curves &src, Curves &dst)
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
Curves * curves_new_nomain(int points_num, int curves_num)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
CurvesGeometry resample_to_count(const CurvesGeometry &src_curves, const IndexMask &selection, const VArray< int > &counts, const ResampleCurvesOutputAttributeIDs &output_ids={})
CurvesGeometry resample_to_evaluated(const CurvesGeometry &src_curves, const IndexMask &selection, const ResampleCurvesOutputAttributeIDs &output_ids={})
CurvesGeometry resample_to_length(const CurvesGeometry &src_curves, const IndexMask &selection, const VArray< float > &sample_lengths, const ResampleCurvesOutputAttributeIDs &output_ids={}, bool keep_last_segment=false)
static void node_init(bNodeTree *, bNode *node)
static void node_layout_ex(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
PropertyRNA * RNA_def_node_boolean(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const BooleanRNAAccessors accessors, std::optional< bool > default_value, bool allow_animation)
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
@ PROP_DISTANCE
Definition RNA_types.hh:313
GeometryNodeCurveResampleMode
@ GEO_NODE_CURVE_RESAMPLE_LENGTH
@ GEO_NODE_CURVE_RESAMPLE_COUNT
@ GEO_NODE_CURVE_RESAMPLE_EVALUATED
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) draw_buttons_ex(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:292
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)
#define N_(msgid)