Blender V5.3
rna_lattice.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
8
9#include <cstdlib>
10
11#include "DNA_lattice_types.h"
12#include "DNA_object_types.h"
13
14#include "RNA_define.hh"
15#include "RNA_enum_types.hh"
16#include "rna_internal.hh"
17
18#ifdef RNA_RUNTIME
19
20# include <algorithm>
21# include <fmt/format.h>
22
23# include "BLI_string.hh"
24
25# include "DNA_curve_types.h"
26# include "DNA_meshdata_types.h"
27# include "DNA_scene_types.h"
28
29# include "BKE_deform.hh"
30# include "BKE_lattice.hh"
31# include "BKE_main.hh"
32
33# include "DEG_depsgraph.hh"
34
35# include "WM_api.hh"
36# include "WM_types.hh"
37
38# include "ED_lattice.hh"
39
40namespace blender {
41
42static void rna_LatticePoint_co_get(PointerRNA *ptr, float *values)
43{
44 Lattice *lt = id_cast<Lattice *>(ptr->owner_id);
45 BPoint *bp = static_cast<BPoint *>(ptr->data);
46 int index = bp - lt->def;
47 int u, v, w;
48
49 BKE_lattice_index_to_uvw(lt, index, &u, &v, &w);
50
51 values[0] = lt->fu + u * lt->du;
52 values[1] = lt->fv + v * lt->dv;
53 values[2] = lt->fw + w * lt->dw;
54}
55
56static void rna_LatticePoint_groups_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
57{
58 Lattice *lt = id_cast<Lattice *>(ptr->owner_id);
59
60 if (lt->dvert) {
61 BPoint *bp = static_cast<BPoint *>(ptr->data);
62 MDeformVert *dvert = lt->dvert + (bp - lt->def);
63
65 ptr,
66 static_cast<void *>(dvert->dw),
67 sizeof(MDeformWeight),
68 dvert->totweight,
69 0,
70 nullptr);
71 }
72 else {
73 rna_iterator_array_begin(iter, ptr, nullptr, 0, 0, 0, nullptr);
74 }
75}
76
77static void rna_Lattice_points_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
78{
79 Lattice *lt = static_cast<Lattice *>(ptr->data);
80 int tot = lt->pntsu * lt->pntsv * lt->pntsw;
81
82 if (lt->editlatt && lt->editlatt->latt->def) {
84 iter, ptr, static_cast<void *>(lt->editlatt->latt->def), sizeof(BPoint), tot, 0, nullptr);
85 }
86 else if (lt->def) {
88 iter, ptr, static_cast<void *>(lt->def), sizeof(BPoint), tot, 0, nullptr);
89 }
90 else {
91 rna_iterator_array_begin(iter, ptr, nullptr, 0, 0, 0, nullptr);
92 }
93}
94
95static void rna_Lattice_update_data(Main * /*bmain*/, Scene * /*scene*/, PointerRNA *ptr)
96{
97 ID *id = ptr->owner_id;
98
99 DEG_id_tag_update(id, 0);
101}
102
107static void rna_Lattice_update_data_editlatt(Main * /*bmain*/, Scene * /*scene*/, PointerRNA *ptr)
108{
109 ID *id = ptr->owner_id;
110 const Lattice *lt = id_cast<Lattice *>(ptr->owner_id);
111 if (lt->editlatt) {
112 Lattice *lt_em = lt->editlatt->latt;
113 BKE_lattice_params_copy(lt_em, lt);
114 }
115
116 DEG_id_tag_update(id, 0);
118}
119
120static void rna_Lattice_update_size(Main *bmain, Scene *scene, PointerRNA *ptr)
121{
122 Lattice *lt = id_cast<Lattice *>(ptr->owner_id);
123 Object *ob;
124 int newu, newv, neww;
125
126 /* We don't modify the actual `pnts`, but go through `opnts` instead. */
127 newu = (lt->opntsu > 0) ? lt->opntsu : lt->pntsu;
128 newv = (lt->opntsv > 0) ? lt->opntsv : lt->pntsv;
129 neww = (lt->opntsw > 0) ? lt->opntsw : lt->pntsw;
130
131 /* #BKE_lattice_resize needs an object, any object will have the same result */
132 for (ob = bmain->objects.first(); ob; ob = static_cast<Object *>(ob->id.next)) {
133 if (ob->data == id_cast<const ID *>(lt)) {
134 BKE_lattice_resize(lt, newu, newv, neww, ob);
135 if (lt->editlatt) {
136 BKE_lattice_resize(lt->editlatt->latt, newu, newv, neww, ob);
137 }
138 break;
139 }
140 }
141
142 /* otherwise without, means old points are not repositioned */
143 if (!ob) {
144 BKE_lattice_resize(lt, newu, newv, neww, nullptr);
145 if (lt->editlatt) {
146 BKE_lattice_resize(lt->editlatt->latt, newu, newv, neww, nullptr);
147 }
148 }
149
150 lt->opntsu = lt->opntsv = lt->opntsw = 0;
151
152 rna_Lattice_update_data(bmain, scene, ptr);
153}
154
155static void rna_Lattice_use_outside_set(PointerRNA *ptr, bool value)
156{
157 Lattice *lt = static_cast<Lattice *>(ptr->data);
158
159 if (value) {
160 lt->flag |= LT_OUTSIDE;
161 }
162 else {
163 lt->flag &= ~LT_OUTSIDE;
164 }
165
166 outside_lattice(lt);
167
168 if (lt->editlatt) {
169 if (value) {
170 lt->editlatt->latt->flag |= LT_OUTSIDE;
171 }
172 else {
173 lt->editlatt->latt->flag &= ~LT_OUTSIDE;
174 }
175
176 outside_lattice(lt->editlatt->latt);
177 }
178}
179
180static int rna_Lattice_size_editable(const PointerRNA *ptr, const char ** /*r_info*/)
181{
182 Lattice *lt = static_cast<Lattice *>(ptr->data);
183
184 return (lt->key == nullptr) ? int(PROP_EDITABLE) : 0;
185}
186
187static int rna_Lattice_points_u_get(PointerRNA *ptr)
188{
189 Lattice *lt = static_cast<Lattice *>(ptr->data);
190 return (lt->opntsu > 0) ? lt->opntsu : lt->pntsu;
191}
192
193static void rna_Lattice_points_u_set(PointerRNA *ptr, int value)
194{
195 Lattice *lt = static_cast<Lattice *>(ptr->data);
196
197 lt->opntsu = std::clamp(value, 1, 64);
198}
199
200static int rna_Lattice_points_v_get(PointerRNA *ptr)
201{
202 Lattice *lt = static_cast<Lattice *>(ptr->data);
203 return (lt->opntsv > 0) ? lt->opntsv : lt->pntsv;
204}
205
206static void rna_Lattice_points_v_set(PointerRNA *ptr, int value)
207{
208 Lattice *lt = static_cast<Lattice *>(ptr->data);
209
210 lt->opntsv = std::clamp(value, 1, 64);
211}
212
213static int rna_Lattice_points_w_get(PointerRNA *ptr)
214{
215 Lattice *lt = static_cast<Lattice *>(ptr->data);
216 return (lt->opntsw > 0) ? lt->opntsw : lt->pntsw;
217}
218
219static void rna_Lattice_points_w_set(PointerRNA *ptr, int value)
220{
221 Lattice *lt = static_cast<Lattice *>(ptr->data);
222
223 lt->opntsw = std::clamp(value, 1, 64);
224}
225
226static void rna_Lattice_vg_name_set(PointerRNA *ptr, const char *value)
227{
228 Lattice *lt = static_cast<Lattice *>(ptr->data);
229 STRNCPY(lt->vgroup, value);
230
231 if (lt->editlatt) {
232 STRNCPY(lt->editlatt->latt->vgroup, value);
233 }
234}
235
236/* annoying, but is a consequence of RNA structures... */
237static std::optional<std::string> rna_LatticePoint_path(const PointerRNA *ptr)
238{
239 const Lattice *lt = id_cast<Lattice *>(ptr->owner_id);
240 const void *point = ptr->data;
241 const BPoint *points = nullptr;
242
243 if (lt->editlatt && lt->editlatt->latt->def) {
244 points = lt->editlatt->latt->def;
245 }
246 else {
247 points = lt->def;
248 }
249
250 if (points && point) {
251 int tot = lt->pntsu * lt->pntsv * lt->pntsw;
252
253 /* only return index if in range */
254 if ((point >= (void *)points) && (point < (void *)(points + tot))) {
255 int pt_index = int((BPoint *)point - points);
256
257 return fmt::format("points[{}]", pt_index);
258 }
259 }
260
261 return "";
262}
263
264static bool rna_Lattice_is_editmode_get(PointerRNA *ptr)
265{
266 Lattice *lt = id_cast<Lattice *>(ptr->owner_id);
267 return (lt->editlatt != nullptr);
268}
269
270} // namespace blender
271
272#else
273
274namespace blender {
275
277{
278 StructRNA *srna;
279 PropertyRNA *prop;
280
281 srna = RNA_def_struct(brna, "LatticePoint", nullptr);
282 RNA_def_struct_sdna(srna, "BPoint");
283 RNA_def_struct_ui_text(srna, "LatticePoint", "Point in the lattice grid");
284 RNA_def_struct_path_func(srna, "rna_LatticePoint_path");
285
286 prop = RNA_def_property(srna, "select", PROP_BOOLEAN, PROP_NONE);
287 RNA_def_property_boolean_sdna(prop, nullptr, "f1", SELECT);
288 RNA_def_property_ui_text(prop, "Point selected", "Selection status");
289
290 prop = RNA_def_property(srna, "co", PROP_FLOAT, PROP_TRANSLATION);
291 RNA_def_property_array(prop, 3);
293 RNA_def_property_float_funcs(prop, "rna_LatticePoint_co_get", nullptr, nullptr);
295 prop,
296 "Location",
297 "Original undeformed location used to calculate the strength of the deform effect "
298 "(edit/animate the Deformed Location instead)");
299
300 prop = RNA_def_property(srna, "co_deform", PROP_FLOAT, PROP_TRANSLATION);
301 RNA_def_property_float_sdna(prop, nullptr, "vec");
302 RNA_def_property_array(prop, 3);
303 RNA_def_property_ui_text(prop, "Deformed Location", "");
304 RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
305
306 prop = RNA_def_property(srna, "weight_softbody", PROP_FLOAT, PROP_NONE);
307 RNA_def_property_float_sdna(prop, nullptr, "weight");
308 RNA_def_property_range(prop, 0.01f, 100.0f);
309 RNA_def_property_ui_text(prop, "Weight", "Softbody goal weight");
310 RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
311
312 prop = RNA_def_property(srna, "groups", PROP_COLLECTION, PROP_NONE);
314 "rna_LatticePoint_groups_begin",
315 "rna_iterator_array_next",
316 "rna_iterator_array_end",
317 "rna_iterator_array_get",
318 nullptr,
319 nullptr,
320 nullptr,
321 nullptr);
322 RNA_def_property_struct_type(prop, "VertexGroupElement");
324 prop, "Groups", "Weights for the vertex groups this point is member of");
325}
326
327static void rna_def_lattice(BlenderRNA *brna)
328{
329 StructRNA *srna;
330 PropertyRNA *prop;
331
332 srna = RNA_def_struct(brna, "Lattice", "ID");
334 srna, "Lattice", "Lattice data-block defining a grid for deforming other objects");
335 RNA_def_struct_ui_icon(srna, ICON_LATTICE_DATA);
336
337 prop = RNA_def_property(srna, "points_u", PROP_INT, PROP_NONE);
338 RNA_def_property_int_sdna(prop, nullptr, "pntsu");
340 prop, "rna_Lattice_points_u_get", "rna_Lattice_points_u_set", nullptr);
341 RNA_def_property_range(prop, 1, 64);
344 prop, "U", "Points in U direction (cannot be changed when there are shape keys)");
345 RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
346 RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
347
348 prop = RNA_def_property(srna, "points_v", PROP_INT, PROP_NONE);
349 RNA_def_property_int_sdna(prop, nullptr, "pntsv");
351 prop, "rna_Lattice_points_v_get", "rna_Lattice_points_v_set", nullptr);
352 RNA_def_property_range(prop, 1, 64);
355 prop, "V", "Points in V direction (cannot be changed when there are shape keys)");
356 RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
357 RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
358
359 prop = RNA_def_property(srna, "points_w", PROP_INT, PROP_NONE);
360 RNA_def_property_int_sdna(prop, nullptr, "pntsw");
362 prop, "rna_Lattice_points_w_get", "rna_Lattice_points_w_set", nullptr);
363 RNA_def_property_range(prop, 1, 64);
366 prop, "W", "Points in W direction (cannot be changed when there are shape keys)");
367 RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
368 RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
369
370 prop = RNA_def_property(srna, "interpolation_type_u", PROP_ENUM, PROP_NONE);
371 RNA_def_property_enum_sdna(prop, nullptr, "typeu");
373 RNA_def_property_ui_text(prop, "Interpolation Type U", "");
374 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
375
376 prop = RNA_def_property(srna, "interpolation_type_v", PROP_ENUM, PROP_NONE);
377 RNA_def_property_enum_sdna(prop, nullptr, "typev");
379 RNA_def_property_ui_text(prop, "Interpolation Type V", "");
380 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
381
382 prop = RNA_def_property(srna, "interpolation_type_w", PROP_ENUM, PROP_NONE);
383 RNA_def_property_enum_sdna(prop, nullptr, "typew");
385 RNA_def_property_ui_text(prop, "Interpolation Type W", "");
386 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
387
388 prop = RNA_def_property(srna, "use_outside", PROP_BOOLEAN, PROP_NONE);
389 RNA_def_property_boolean_sdna(prop, nullptr, "flag", LT_OUTSIDE);
390 RNA_def_property_boolean_funcs(prop, nullptr, "rna_Lattice_use_outside_set");
392 prop, "Outside", "Only display and take into account the outer vertices");
393 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
394
395 prop = RNA_def_property(srna, "vertex_group", PROP_STRING, PROP_NONE);
396 RNA_def_property_string_sdna(prop, nullptr, "vgroup");
398 prop, "Vertex Group", "Vertex group to apply the influence of the lattice");
399 RNA_def_property_string_funcs(prop, nullptr, nullptr, "rna_Lattice_vg_name_set");
400 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
401
402 prop = RNA_def_property(srna, "shape_keys", PROP_POINTER, PROP_NONE);
403 RNA_def_property_pointer_sdna(prop, nullptr, "key");
406 RNA_def_property_ui_text(prop, "Shape Keys", "");
407
408 prop = RNA_def_property(srna, "points", PROP_COLLECTION, PROP_NONE);
409 RNA_def_property_struct_type(prop, "LatticePoint");
411 "rna_Lattice_points_begin",
412 "rna_iterator_array_next",
413 "rna_iterator_array_end",
414 "rna_iterator_array_get",
415 nullptr,
416 nullptr,
417 nullptr,
418 nullptr);
419 RNA_def_property_ui_text(prop, "Points", "Points of the lattice");
420
421 prop = RNA_def_property(srna, "is_editmode", PROP_BOOLEAN, PROP_NONE);
422 RNA_def_property_boolean_funcs(prop, "rna_Lattice_is_editmode_get", nullptr);
424 RNA_def_property_ui_text(prop, "Is Editmode", "True when used in editmode");
425
426 /* pointers */
428
429 RNA_api_lattice(srna);
430}
431
433{
434 rna_def_lattice(brna);
436}
437
438} // namespace blender
439
440#endif
support for deformation groups and hooks.
#define SELECT
Definition BKE_mask.hh:19
Object is a sort of wrapper for general info.
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
void RNA_def_property_editable_func(PropertyRNA *prop, const char *editable)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
char * STRNCPY(char(&dst)[N], const char *src)
void RNA_def_struct_ui_icon(StructRNA *srna, int icon)
void RNA_def_property_string_sdna(PropertyRNA *prop, const char *structname, const char *propname)
void BKE_lattice_resize(Lattice *lt, int u_new, int v_new, int w_new, Object *lt_ob)
Definition lattice.cc:277
void BKE_lattice_params_copy(Lattice *lt_dst, const Lattice *lt_src)
Definition lattice.cc:401
void RNA_def_property_float_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
static void rna_def_lattice(BlenderRNA *brna)
void RNA_def_property_pointer_sdna(PropertyRNA *prop, const char *structname, const char *propname)
void RNA_def_property_boolean_funcs(PropertyRNA *prop, const char *get, const char *set)
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void RNA_def_lattice(BlenderRNA *brna)
void RNA_def_property_float_sdna(PropertyRNA *prop, const char *structname, const char *propname)
void RNA_api_lattice(StructRNA *srna)
@ PROPOVERRIDE_OVERRIDABLE_LIBRARY
Definition RNA_types.hh:588
void rna_def_animdata_common(StructRNA *srna)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier_c_str, int type, int subtype)
void RNA_def_struct_sdna(StructRNA *srna, const char *structname)
void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item)
@ PROP_BOOLEAN
Definition RNA_types.hh:210
@ PROP_POINTER
Definition RNA_types.hh:215
@ PROP_STRING
Definition RNA_types.hh:213
@ PROP_COLLECTION
Definition RNA_types.hh:216
void RNA_def_property_collection_funcs(PropertyRNA *prop, const char *begin, const char *next, const char *end, const char *get, const char *length, const char *lookupint, const char *lookupstring, const char *assignint)
void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
void DEG_id_tag_update(ID *id, unsigned int flags)
void RNA_def_property_override_flag(PropertyRNA *prop, PropertyOverrideFlag flag)
void outside_lattice(Lattice *lt)
Definition lattice.cc:415
void RNA_def_property_boolean_sdna(PropertyRNA *prop, const char *structname, const char *propname, int64_t booleanbit)
void RNA_def_property_range(PropertyRNA *prop, double min, double max)
void RNA_def_struct_path_func(StructRNA *srna, const char *path)
const EnumPropertyItem rna_enum_keyblock_type_items[]
Definition rna_key.cc:25
static void rna_def_latticepoint(BlenderRNA *brna)
void RNA_def_property_enum_sdna(PropertyRNA *prop, const char *structname, const char *propname)
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
void BKE_lattice_index_to_uvw(const Lattice *lt, int index, int *r_u, int *r_v, int *r_w)
Definition lattice.cc:207
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
@ PROP_EDITABLE
Definition RNA_types.hh:376
@ PROP_PTR_NO_OWNERSHIP
Definition RNA_types.hh:465
@ PROP_ANIMATABLE
Definition RNA_types.hh:389
void RNA_def_property_int_sdna(PropertyRNA *prop, const char *structname, const char *propname)
void RNA_def_property_update(PropertyRNA *prop, int noteflag, const char *func)
void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set)
@ PROP_TRANSLATION
Definition RNA_types.hh:318
void RNA_def_property_array(PropertyRNA *prop, int length)
void rna_iterator_array_begin(CollectionPropertyIterator *iter, PointerRNA *ptr, void *data, size_t itemsize, int64_t length, bool free_ptr, IteratorSkipFunc skip)
struct Lattice * latt
struct EditLatt * editlatt