Blender V4.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
9#include "RNA_define.hh"
10
11#include "rna_internal.hh" /* own include */
12
13#ifdef RNA_RUNTIME
14
15# include "BKE_curves.hh"
16# include "BKE_report.hh"
17
18# include "BLI_index_mask.hh"
19
20# include "ED_curves.hh"
21
22# include "rna_curves_utils.hh"
23
24/* Common `CurvesGeometry` API functions. */
25
26bool rna_CurvesGeometry_add_curves(blender::bke::CurvesGeometry &curves,
27 ReportList *reports,
28 const int *sizes_ptr,
29 const int sizes_num)
30{
31 using namespace blender;
32 if (std::any_of(sizes_ptr, sizes_ptr + sizes_num, [](const int size) { return size < 1; })) {
33 BKE_report(reports, RPT_ERROR, "Curve sizes must be greater than zero");
34 return false;
35 }
36
37 ed::curves::add_curves(curves, {sizes_ptr, sizes_num});
38 curves.tag_topology_changed();
39 return true;
40}
41
42bool rna_CurvesGeometry_remove_curves(blender::bke::CurvesGeometry &curves,
43 ReportList *reports,
44 const int *indices_ptr,
45 const int indices_num)
46{
47 using namespace blender;
48 if (indices_ptr != nullptr) {
49 const Span<int> indices(indices_ptr, indices_num);
50 if (std::any_of(indices.begin(), indices.end(), [&](const int index) {
51 return !curves.curves_range().contains(index);
52 }))
53 {
54 BKE_report(reports, RPT_ERROR, "Indices must be in range");
55 return false;
56 }
57 if (!std::is_sorted(indices.begin(), indices.end())) {
58 BKE_report(reports, RPT_ERROR, "Indices must be sorted in acending order");
59 return false;
60 }
61 if (std::adjacent_find(indices.begin(), indices.end(), std::greater_equal<int>()) !=
62 indices.end())
63 {
64 BKE_report(reports, RPT_ERROR, "Indices can't have duplicates");
65 return false;
66 }
67 /* Remove the curves by their indices. */
68 IndexMaskMemory memory;
69 IndexMask curves_to_delete = IndexMask::from_indices(indices, memory);
70 curves.remove_curves(curves_to_delete, {});
71 }
72 else {
73 /* Clear the CurvesGeometry. */
74 curves = {};
75 }
76 return true;
77}
78
79bool rna_CurvesGeometry_resize_curves(blender::bke::CurvesGeometry &curves,
80 ReportList *reports,
81 const int *sizes_ptr,
82 const int sizes_num,
83 const int *indices_ptr,
84 const int indices_num)
85{
86 using namespace blender;
87 const Span<int> new_sizes(sizes_ptr, sizes_num);
88 if (std::any_of(new_sizes.begin(), new_sizes.end(), [](const int size) { return size < 1; })) {
89 BKE_report(reports, RPT_ERROR, "Sizes must be greater than zero");
90 return false;
91 }
92 IndexMaskMemory memory;
93 IndexMask curves_to_resize;
94 if (indices_ptr != nullptr) {
95 if (indices_num != sizes_num) {
96 BKE_report(reports, RPT_ERROR, "Length of sizes and length of indices must be the same");
97 return false;
98 }
99 const Span<int> indices(indices_ptr, indices_num);
100 if (std::any_of(indices.begin(), indices.end(), [&](const int index) {
101 return !curves.curves_range().contains(index);
102 }))
103 {
104 BKE_report(reports, RPT_ERROR, "Indices must be in range");
105 return false;
106 }
107 if (!std::is_sorted(indices.begin(), indices.end())) {
108 BKE_report(reports, RPT_ERROR, "Indices must be sorted in acending order");
109 return false;
110 }
111 if (std::adjacent_find(indices.begin(), indices.end(), std::greater_equal<int>()) !=
112 indices.end())
113 {
114 BKE_report(reports, RPT_ERROR, "Indices can't have duplicates");
115 return false;
116 }
117 curves_to_resize = IndexMask::from_indices(indices, memory);
118 }
119 else {
120 if (sizes_num != curves.curves_num()) {
121 BKE_report(reports, RPT_ERROR, "Length of sizes and number of strokes must be the same");
122 return false;
123 }
124 curves_to_resize = curves.curves_range();
125 }
126
127 ed::curves::resize_curves(curves, curves_to_resize, {sizes_ptr, sizes_num});
128 return true;
129}
130
131/* Curves API functions. */
132
133static void rna_Curves_add_curves(Curves *curves_id,
134 ReportList *reports,
135 const int *sizes,
136 const int sizes_num)
137{
138 using namespace blender;
139 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
140 if (!rna_CurvesGeometry_add_curves(curves, reports, sizes, sizes_num)) {
141 return;
142 }
143
144 /* Avoid updates for importers creating curves. */
145 if (curves_id->id.us > 0) {
148 }
149}
150
151static void rna_Curves_remove_curves(Curves *curves_id,
152 ReportList *reports,
153 const int *indices_ptr,
154 const int indices_num)
155{
156 using namespace blender;
157 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
158 if (!rna_CurvesGeometry_remove_curves(curves, reports, indices_ptr, indices_num)) {
159 return;
160 }
161
162 /* Avoid updates for importers creating curves. */
163 if (curves_id->id.us > 0) {
166 }
167}
168
169static void rna_Curves_resize_curves(Curves *curves_id,
170 ReportList *reports,
171 const int *sizes_ptr,
172 const int sizes_num,
173 const int *indices_ptr,
174 const int indices_num)
175{
176 using namespace blender;
177 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
178 if (!rna_CurvesGeometry_resize_curves(
179 curves, reports, sizes_ptr, sizes_num, indices_ptr, indices_num))
180 {
181 return;
182 }
183
184 /* Avoid updates for importers creating curves. */
185 if (curves_id->id.us > 0) {
188 }
189}
190
191#else
192
194{
195 FunctionRNA *func;
196 PropertyRNA *parm;
197
198 func = RNA_def_function(srna, "add_curves", "rna_Curves_add_curves");
200 parm = RNA_def_int_array(func,
201 "sizes",
202 1,
203 nullptr,
204 0,
205 INT_MAX,
206 "Sizes",
207 "The number of points in each curve",
208 1,
209 10000);
211
212 func = RNA_def_function(srna, "remove_curves", "rna_Curves_remove_curves");
214 "Remove all curves. If indices are provided, remove only the "
215 "curves with the given indices.");
217 parm = RNA_def_int_array(func,
218 "indices",
219 1,
220 nullptr,
221 0,
222 INT_MAX,
223 "Indices",
224 "The indices of the curves to remove",
225 0,
226 10000);
228
229 func = RNA_def_function(srna, "resize_curves", "rna_Curves_resize_curves");
231 func,
232 "Resize all existing curves. If indices are provided, resize only the curves with the given "
233 "indices. If the new size for a curve is smaller, the curve is trimmed. If "
234 "the new size for a curve is larger, the new end values are default initialized.");
236 parm = RNA_def_int_array(func,
237 "sizes",
238 1,
239 nullptr,
240 1,
241 INT_MAX,
242 "Sizes",
243 "The number of points in each curve",
244 1,
245 10000);
247 parm = RNA_def_int_array(func,
248 "indices",
249 1,
250 nullptr,
251 0,
252 INT_MAX,
253 "Indices",
254 "The indices of the curves to resize",
255 0,
256 10000);
258}
259
260#endif
Low-level operations for curves.
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:125
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1041
ParameterFlag
Definition RNA_types.hh:396
@ PARM_REQUIRED
Definition RNA_types.hh:397
@ FUNC_USE_REPORTS
Definition RNA_types.hh:680
@ PROP_DYNAMIC
Definition RNA_types.hh:317
#define NC_GEOM
Definition WM_types.hh:360
#define ND_DATA
Definition WM_types.hh:475
static ushort indices[]
void RNA_api_curves(StructRNA *srna)
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)
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
void RNA_def_function_flag(FunctionRNA *func, int flag)
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
CurvesGeometry geometry
int us
Definition DNA_ID.h:435
void WM_main_add_notifier(uint type, void *reference)