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