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