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 void rna_Lattice_points_u_set(PointerRNA *ptr, int value)
183{
184 Lattice *lt = (Lattice *)ptr->data;
185
186 lt->opntsu = std::clamp(value, 1, 64);
187}
188
189static void rna_Lattice_points_v_set(PointerRNA *ptr, int value)
190{
191 Lattice *lt = (Lattice *)ptr->data;
192
193 lt->opntsv = std::clamp(value, 1, 64);
194}
195
196static void rna_Lattice_points_w_set(PointerRNA *ptr, int value)
197{
198 Lattice *lt = (Lattice *)ptr->data;
199
200 lt->opntsw = std::clamp(value, 1, 64);
201}
202
203static void rna_Lattice_vg_name_set(PointerRNA *ptr, const char *value)
204{
205 Lattice *lt = static_cast<Lattice *>(ptr->data);
206 STRNCPY(lt->vgroup, value);
207
208 if (lt->editlatt) {
209 STRNCPY(lt->editlatt->latt->vgroup, value);
210 }
211}
212
213/* annoying, but is a consequence of RNA structures... */
214static std::optional<std::string> rna_LatticePoint_path(const PointerRNA *ptr)
215{
216 const Lattice *lt = (Lattice *)ptr->owner_id;
217 const void *point = ptr->data;
218 const BPoint *points = nullptr;
219
220 if (lt->editlatt && lt->editlatt->latt->def) {
221 points = lt->editlatt->latt->def;
222 }
223 else {
224 points = lt->def;
225 }
226
227 if (points && point) {
228 int tot = lt->pntsu * lt->pntsv * lt->pntsw;
229
230 /* only return index if in range */
231 if ((point >= (void *)points) && (point < (void *)(points + tot))) {
232 int pt_index = int((BPoint *)point - points);
233
234 return fmt::format("points[{}]", pt_index);
235 }
236 }
237
238 return "";
239}
240
241static bool rna_Lattice_is_editmode_get(PointerRNA *ptr)
242{
243 Lattice *lt = (Lattice *)ptr->owner_id;
244 return (lt->editlatt != nullptr);
245}
246
247#else
248
250{
251 StructRNA *srna;
252 PropertyRNA *prop;
253
254 srna = RNA_def_struct(brna, "LatticePoint", nullptr);
255 RNA_def_struct_sdna(srna, "BPoint");
256 RNA_def_struct_ui_text(srna, "LatticePoint", "Point in the lattice grid");
257 RNA_def_struct_path_func(srna, "rna_LatticePoint_path");
258
259 prop = RNA_def_property(srna, "select", PROP_BOOLEAN, PROP_NONE);
260 RNA_def_property_boolean_sdna(prop, nullptr, "f1", SELECT);
261 RNA_def_property_ui_text(prop, "Point selected", "Selection status");
262
263 prop = RNA_def_property(srna, "co", PROP_FLOAT, PROP_TRANSLATION);
264 RNA_def_property_array(prop, 3);
266 RNA_def_property_float_funcs(prop, "rna_LatticePoint_co_get", nullptr, nullptr);
268 prop,
269 "Location",
270 "Original undeformed location used to calculate the strength of the deform effect "
271 "(edit/animate the Deformed Location instead)");
272
273 prop = RNA_def_property(srna, "co_deform", PROP_FLOAT, PROP_TRANSLATION);
274 RNA_def_property_float_sdna(prop, nullptr, "vec");
275 RNA_def_property_array(prop, 3);
276 RNA_def_property_ui_text(prop, "Deformed Location", "");
277 RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
278
279 prop = RNA_def_property(srna, "weight_softbody", PROP_FLOAT, PROP_NONE);
280 RNA_def_property_float_sdna(prop, nullptr, "weight");
281 RNA_def_property_range(prop, 0.01f, 100.0f);
282 RNA_def_property_ui_text(prop, "Weight", "Softbody goal weight");
283 RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
284
285 prop = RNA_def_property(srna, "groups", PROP_COLLECTION, PROP_NONE);
287 "rna_LatticePoint_groups_begin",
288 "rna_iterator_array_next",
289 "rna_iterator_array_end",
290 "rna_iterator_array_get",
291 nullptr,
292 nullptr,
293 nullptr,
294 nullptr);
295 RNA_def_property_struct_type(prop, "VertexGroupElement");
297 prop, "Groups", "Weights for the vertex groups this point is member of");
298}
299
300static void rna_def_lattice(BlenderRNA *brna)
301{
302 StructRNA *srna;
303 PropertyRNA *prop;
304
305 srna = RNA_def_struct(brna, "Lattice", "ID");
307 srna, "Lattice", "Lattice data-block defining a grid for deforming other objects");
308 RNA_def_struct_ui_icon(srna, ICON_LATTICE_DATA);
309
310 prop = RNA_def_property(srna, "points_u", PROP_INT, PROP_NONE);
311 RNA_def_property_int_sdna(prop, nullptr, "pntsu");
312 RNA_def_property_int_funcs(prop, nullptr, "rna_Lattice_points_u_set", nullptr);
313 RNA_def_property_range(prop, 1, 64);
316 prop, "U", "Points in U direction (cannot be changed when there are shape keys)");
317 RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
318 RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
319
320 prop = RNA_def_property(srna, "points_v", PROP_INT, PROP_NONE);
321 RNA_def_property_int_sdna(prop, nullptr, "pntsv");
322 RNA_def_property_int_funcs(prop, nullptr, "rna_Lattice_points_v_set", nullptr);
323 RNA_def_property_range(prop, 1, 64);
326 prop, "V", "Points in V direction (cannot be changed when there are shape keys)");
327 RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
328 RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
329
330 prop = RNA_def_property(srna, "points_w", PROP_INT, PROP_NONE);
331 RNA_def_property_int_sdna(prop, nullptr, "pntsw");
332 RNA_def_property_int_funcs(prop, nullptr, "rna_Lattice_points_w_set", nullptr);
333 RNA_def_property_range(prop, 1, 64);
336 prop, "W", "Points in W direction (cannot be changed when there are shape keys)");
337 RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
338 RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
339
340 prop = RNA_def_property(srna, "interpolation_type_u", PROP_ENUM, PROP_NONE);
341 RNA_def_property_enum_sdna(prop, nullptr, "typeu");
343 RNA_def_property_ui_text(prop, "Interpolation Type U", "");
344 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
345
346 prop = RNA_def_property(srna, "interpolation_type_v", PROP_ENUM, PROP_NONE);
347 RNA_def_property_enum_sdna(prop, nullptr, "typev");
349 RNA_def_property_ui_text(prop, "Interpolation Type V", "");
350 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
351
352 prop = RNA_def_property(srna, "interpolation_type_w", PROP_ENUM, PROP_NONE);
353 RNA_def_property_enum_sdna(prop, nullptr, "typew");
355 RNA_def_property_ui_text(prop, "Interpolation Type W", "");
356 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
357
358 prop = RNA_def_property(srna, "use_outside", PROP_BOOLEAN, PROP_NONE);
359 RNA_def_property_boolean_sdna(prop, nullptr, "flag", LT_OUTSIDE);
360 RNA_def_property_boolean_funcs(prop, nullptr, "rna_Lattice_use_outside_set");
362 prop, "Outside", "Only display and take into account the outer vertices");
363 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
364
365 prop = RNA_def_property(srna, "vertex_group", PROP_STRING, PROP_NONE);
366 RNA_def_property_string_sdna(prop, nullptr, "vgroup");
368 prop, "Vertex Group", "Vertex group to apply the influence of the lattice");
369 RNA_def_property_string_funcs(prop, nullptr, nullptr, "rna_Lattice_vg_name_set");
370 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
371
372 prop = RNA_def_property(srna, "shape_keys", PROP_POINTER, PROP_NONE);
373 RNA_def_property_pointer_sdna(prop, nullptr, "key");
376 RNA_def_property_ui_text(prop, "Shape Keys", "");
377
378 prop = RNA_def_property(srna, "points", PROP_COLLECTION, PROP_NONE);
379 RNA_def_property_struct_type(prop, "LatticePoint");
381 "rna_Lattice_points_begin",
382 "rna_iterator_array_next",
383 "rna_iterator_array_end",
384 "rna_iterator_array_get",
385 nullptr,
386 nullptr,
387 nullptr,
388 nullptr);
389 RNA_def_property_ui_text(prop, "Points", "Points of the lattice");
390
391 prop = RNA_def_property(srna, "is_editmode", PROP_BOOLEAN, PROP_NONE);
392 RNA_def_property_boolean_funcs(prop, "rna_Lattice_is_editmode_get", nullptr);
394 RNA_def_property_ui_text(prop, "Is Editmode", "True when used in editmode");
395
396 /* pointers */
398
399 RNA_api_lattice(srna);
400}
401
403{
404 rna_def_lattice(brna);
406}
407
408#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:4226