Blender V5.3
rna_curves_api.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_curves_types.h"
10
11#include "RNA_define.hh"
12
13#include "RNA_enum_types.hh"
14
15#include "rna_internal.hh" /* own include */
16
17#ifdef RNA_RUNTIME
18
19# include "BKE_curves.hh"
21# include "BKE_report.hh"
22
23# include "BLI_index_mask.hh"
24# include "BLI_set.hh"
25
26# include "ED_curves.hh"
27
28# include "DEG_depsgraph.hh"
29
30# include "WM_api.hh"
31
32# include "rna_curves_utils.hh"
33
34namespace blender {
35
36/* Common `CurvesGeometry` API functions. */
37
38bool rna_CurvesGeometry_add_curves(bke::CurvesGeometry &curves,
39 ReportList *reports,
40 const int *sizes_ptr,
41 const int sizes_num)
42{
43 if (std::any_of(sizes_ptr, sizes_ptr + sizes_num, [](const int size) { return size < 1; })) {
44 BKE_report(reports, RPT_ERROR, "Curve sizes must be greater than zero");
45 return false;
46 }
47
48 ed::curves::add_curves(curves, {sizes_ptr, sizes_num});
49 curves.tag_topology_changed();
50 return true;
51}
52
53static std::optional<IndexMask> rna_indices_to_mask(const IndexRange universe,
54 const int *indices_ptr,
55 const int indices_num,
56 ReportList &reports,
57 IndexMaskMemory &memory)
58{
59 if (!indices_ptr) {
60 return IndexMask(universe);
61 }
62 const Span<int> indices(indices_ptr, indices_num);
63 if (std::any_of(indices.begin(),
64 indices.end(),
65 [&](const int index) { return !universe.contains(index); }))
66 {
67 BKE_report(&reports, RPT_ERROR, "Indices must be in range");
68 return std::nullopt;
69 }
70 if (!std::is_sorted(indices.begin(), indices.end())) {
71 BKE_report(&reports, RPT_ERROR, "Indices must be sorted in ascending order");
72 return std::nullopt;
73 }
74 if (std::adjacent_find(indices.begin(), indices.end(), std::greater_equal<>()) != indices.end())
75 {
76 BKE_report(&reports, RPT_ERROR, "Indices cannot have duplicates");
77 return std::nullopt;
78 }
79 return IndexMask::from_indices(indices, memory);
80}
81
82bool rna_CurvesGeometry_remove_curves(bke::CurvesGeometry &curves,
83 ReportList *reports,
84 const int *indices_ptr,
85 const int indices_num)
86{
87 IndexMaskMemory memory;
88 const std::optional<IndexMask> curves_to_delete = rna_indices_to_mask(
89 curves.curves_range(), indices_ptr, indices_num, *reports, memory);
90 if (!curves_to_delete) {
91 return false;
92 }
93 curves.remove_curves(*curves_to_delete, {});
94 return true;
95}
96
97bool rna_CurvesGeometry_resize_curves(bke::CurvesGeometry &curves,
98 ReportList *reports,
99 const int *sizes_ptr,
100 const int sizes_num,
101 const int *indices_ptr,
102 const int indices_num)
103{
104 const Span<int> new_sizes(sizes_ptr, sizes_num);
105 if (std::any_of(new_sizes.begin(), new_sizes.end(), [](const int size) { return size < 1; })) {
106 BKE_report(reports, RPT_ERROR, "Sizes must be greater than zero");
107 return false;
108 }
109 IndexMaskMemory memory;
110 const std::optional<IndexMask> curves_to_resize = rna_indices_to_mask(
111 curves.curves_range(), indices_ptr, indices_num, *reports, memory);
112 if (!curves_to_resize) {
113 return false;
114 }
115 if (curves_to_resize->size() != sizes_num) {
116 BKE_report(reports, RPT_ERROR, "Length of sizes must be the same as the selection size");
117 return false;
118 }
119 ed::curves::resize_curves(curves, *curves_to_resize, {sizes_ptr, sizes_num});
120 return true;
121}
122
123bool rna_CurvesGeometry_reorder_curves(bke::CurvesGeometry &curves,
124 ReportList *reports,
125 const int *reorder_indices_ptr,
126 const int reorder_indices_num)
127{
128 const Span<int> new_to_old_indices_map(reorder_indices_ptr, reorder_indices_num);
129 if (curves.curves_num() != reorder_indices_num) {
131 reports, RPT_ERROR, "Length of reorder indices must be the same as the number of curves");
132 return false;
133 }
134 if (std::any_of(new_to_old_indices_map.begin(),
135 new_to_old_indices_map.end(),
136 [&](const int index) { return !curves.curves_range().contains(index); }))
137 {
138 BKE_report(reports, RPT_ERROR, "Reorder indices must be valid");
139 return false;
140 }
141 if (Set(new_to_old_indices_map).size() != reorder_indices_num) {
142 BKE_report(reports, RPT_ERROR, "Reorder indices must not have duplicates");
143 return false;
144 }
145 ed::curves::reorder_curves(curves, new_to_old_indices_map);
146 return true;
147}
148
149bool rna_CurvesGeometry_set_types(bke::CurvesGeometry &curves,
150 ReportList *reports,
151 const int type,
152 const int *indices_ptr,
153 const int indices_num)
154{
155 IndexMaskMemory memory;
156 const std::optional<IndexMask> selection = rna_indices_to_mask(
157 curves.curves_range(), indices_ptr, indices_num, *reports, memory);
158 if (!selection) {
159 return false;
160 }
161 curves.fill_curve_types(*selection, CurveType(type));
162 return true;
163}
164
165/* Curves API functions. */
166
167static void rna_Curves_add_curves(Curves *curves_id,
168 ReportList *reports,
169 const int *sizes,
170 const int sizes_num)
171{
172 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
173 if (!rna_CurvesGeometry_add_curves(curves, reports, sizes, sizes_num)) {
174 return;
175 }
176
177 /* Avoid updates for importers creating curves. */
178 if (curves_id->id.us > 0) {
179 DEG_id_tag_update(&curves_id->id, ID_RECALC_GEOMETRY);
181 }
182}
183
184static void rna_Curves_remove_curves(Curves *curves_id,
185 ReportList *reports,
186 const int *indices_ptr,
187 const int indices_num)
188{
189 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
190 if (!rna_CurvesGeometry_remove_curves(curves, reports, indices_ptr, indices_num)) {
191 return;
192 }
193
194 /* Avoid updates for importers creating curves. */
195 if (curves_id->id.us > 0) {
196 DEG_id_tag_update(&curves_id->id, ID_RECALC_GEOMETRY);
198 }
199}
200
201static void rna_Curves_resize_curves(Curves *curves_id,
202 ReportList *reports,
203 const int *sizes_ptr,
204 const int sizes_num,
205 const int *indices_ptr,
206 const int indices_num)
207{
208 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
209 if (!rna_CurvesGeometry_resize_curves(
210 curves, reports, sizes_ptr, sizes_num, indices_ptr, indices_num))
211 {
212 return;
213 }
214
215 /* Avoid updates for importers creating curves. */
216 if (curves_id->id.us > 0) {
217 DEG_id_tag_update(&curves_id->id, ID_RECALC_GEOMETRY);
219 }
220}
221
222static void rna_Curves_reorder_curves(Curves *curves_id,
223 ReportList *reports,
224 const int *reorder_indices_ptr,
225 const int reorder_indices_num)
226{
227 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
228 if (!rna_CurvesGeometry_reorder_curves(
229 curves, reports, reorder_indices_ptr, reorder_indices_num))
230 {
231 return;
232 }
233
234 /* Avoid updates for importers creating curves. */
235 if (curves_id->id.us > 0) {
236 DEG_id_tag_update(&curves_id->id, ID_RECALC_GEOMETRY);
238 }
239}
240
241static void rna_Curves_set_types(Curves *curves_id,
242 ReportList *reports,
243 const int type,
244 const int *indices_ptr,
245 const int indices_num)
246{
247 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
248 if (!rna_CurvesGeometry_set_types(curves, reports, type, indices_ptr, indices_num)) {
249 return;
250 }
251 /* Avoid updates for importers creating curves. */
252 if (curves_id->id.us > 0) {
253 DEG_id_tag_update(&curves_id->id, ID_RECALC_GEOMETRY);
255 }
256}
257
258static const char *rna_Curves_unit_test_compare(Curves *curves1, Curves *curves2, float threshold)
259{
260 using namespace bke::compare_geometry;
261
262 const std::optional<GeoMismatch> mismatch = compare_curves(
263 curves1->geometry.wrap(), curves2->geometry.wrap(), threshold);
264
265 if (!mismatch) {
266 return "Same";
267 }
268
269 return mismatch_to_string(mismatch.value());
270}
271
272} // namespace blender
273
274#else
275
276namespace blender {
277
279{
280 FunctionRNA *func;
281 PropertyRNA *parm;
282
283 func = RNA_def_function(srna, "add_curves", "rna_Curves_add_curves");
285 parm = RNA_def_int_array(func,
286 "sizes",
287 1,
288 nullptr,
289 0,
290 INT_MAX,
291 "Sizes",
292 "The number of points in each curve",
293 1,
294 10000);
296
297 func = RNA_def_function(srna, "remove_curves", "rna_Curves_remove_curves");
299 "Remove all curves. If indices are provided, remove only the "
300 "curves with the given indices.");
302 parm = RNA_def_int_array(func,
303 "indices",
304 1,
305 nullptr,
306 0,
307 INT_MAX,
308 "Indices",
309 "The indices of the curves to remove",
310 0,
311 10000);
313
314 func = RNA_def_function(srna, "resize_curves", "rna_Curves_resize_curves");
316 func,
317 "Resize all existing curves. If indices are provided, resize only the curves with the given "
318 "indices. If the new size for a curve is smaller, the curve is trimmed. If "
319 "the new size for a curve is larger, the new end values are default initialized.");
321 parm = RNA_def_int_array(func,
322 "sizes",
323 1,
324 nullptr,
325 1,
326 INT_MAX,
327 "Sizes",
328 "The number of points in each curve",
329 1,
330 10000);
332 parm = RNA_def_int_array(func,
333 "indices",
334 1,
335 nullptr,
336 0,
337 INT_MAX,
338 "Indices",
339 "The indices of the curves to resize",
340 0,
341 10000);
343
344 func = RNA_def_function(srna, "reorder_curves", "rna_Curves_reorder_curves");
345 RNA_def_function_ui_description(func, "Reorder the curves by the new indices.");
347 parm = RNA_def_int_array(func,
348 "new_indices",
349 1,
350 nullptr,
351 0,
352 INT_MAX,
353 "New indices",
354 "The new index for each of the curves",
355 0,
356 10000);
358
359 func = RNA_def_function(srna, "set_types", "rna_Curves_set_types");
361 "Set the curve type. If indices are provided, set only the "
362 "types with the given curve indices.");
365 parm = RNA_def_int_array(func,
366 "indices",
367 1,
368 nullptr,
369 0,
370 INT_MAX,
371 "Indices",
372 "The indices of the curves to resize",
373 0,
374 INT_MAX);
376
377 func = RNA_def_function(srna, "unit_test_compare", "rna_Curves_unit_test_compare");
378 RNA_def_pointer(func, "curves", "Curves", "", "Curves to compare to");
380 "threshold",
381 FLT_EPSILON * 60,
382 0.0f,
383 FLT_MAX,
384 "Threshold",
385 "Comparison tolerance threshold",
386 0.0f,
387 FLT_MAX);
388 /* return value */
389 parm = RNA_def_string(
390 func, "result", "nothing", 64, "Return value", "String description of result of comparison");
391 RNA_def_function_return(func, parm);
392}
393
394} // namespace blender
395
396#endif
Low-level operations for curves.
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
static IndexMask from_indices(Span< T > indices, LinearAllocator<> &memory)
Set(Allocator allocator={}) noexcept
Definition BLI_set.hh:168
#define FLT_MAX
#define INT_MAX
#define FLT_EPSILON
std::optional< GeoMismatch > compare_curves(const CurvesGeometry &curves1, const CurvesGeometry &curves2, float threshold)
Checks if the two curves geometries are different, returning the type of mismatch if any....
const char * mismatch_to_string(const GeoMismatch &mismatch)
int size(const AssetLibraryReference *library_reference)
void resize_curves(bke::CurvesGeometry &curves, const IndexMask &curves_to_resize, const Span< int > new_sizes)
void add_curves(bke::CurvesGeometry &curves, const Span< int > new_sizes)
void reorder_curves(bke::CurvesGeometry &curves, const Span< int > old_by_new_indices_map)
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
const EnumPropertyItem rna_enum_curves_type_items[]
Definition rna_curves.cc:26
@ FUNC_USE_REPORTS
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
PropertyRNA * RNA_def_float_factor(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier_c_str, const char *call)
void RNA_api_curves(StructRNA *srna)
void RNA_def_function_flag(FunctionRNA *func, FunctionFlag flag)
@ CURVE_TYPE_CATMULL_ROM
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_DYNAMIC
Definition RNA_types.hh:498
PropertyRNA * RNA_def_int_array(StructOrFunctionRNA *cont_, const char *identifier, const int len, const int *default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
@ PARM_REQUIRED
Definition RNA_types.hh:631