Blender V4.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_curve_types.h"
12#include "DNA_key_types.h"
13#include "DNA_lattice_types.h"
14#include "DNA_meshdata_types.h"
15#include "DNA_object_types.h"
16
17#include "BLI_utildefines.h"
18
19#include "RNA_define.hh"
20#include "RNA_enum_types.hh"
21#include "rna_internal.hh"
22
23#ifdef RNA_RUNTIME
24
25# include <algorithm>
26# include <fmt/format.h>
27
28# include "DNA_object_types.h"
29# include "DNA_scene_types.h"
30
31# include "BKE_deform.hh"
32# include "BKE_lattice.hh"
33# include "BKE_main.hh"
34# include "BLI_string.h"
35
36# include "DEG_depsgraph.hh"
37
38# include "ED_lattice.hh"
39# include "WM_api.hh"
40# include "WM_types.hh"
41
42static void rna_LatticePoint_co_get(PointerRNA *ptr, float *values)
43{
44 Lattice *lt = (Lattice *)ptr->owner_id;
45 BPoint *bp = (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 = (Lattice *)ptr->owner_id;
59
60 if (lt->dvert) {
61 BPoint *bp = (BPoint *)ptr->data;
62 MDeformVert *dvert = lt->dvert + (bp - lt->def);
63
65 iter, (void *)dvert->dw, sizeof(MDeformWeight), dvert->totweight, 0, nullptr);
66 }
67 else {
68 rna_iterator_array_begin(iter, nullptr, 0, 0, 0, nullptr);
69 }
70}
71
72static void rna_Lattice_points_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
73{
74 Lattice *lt = (Lattice *)ptr->data;
75 int tot = lt->pntsu * lt->pntsv * lt->pntsw;
76
77 if (lt->editlatt && lt->editlatt->latt->def) {
79 iter, (void *)lt->editlatt->latt->def, sizeof(BPoint), tot, 0, nullptr);
80 }
81 else if (lt->def) {
82 rna_iterator_array_begin(iter, (void *)lt->def, sizeof(BPoint), tot, 0, nullptr);
83 }
84 else {
85 rna_iterator_array_begin(iter, nullptr, 0, 0, 0, nullptr);
86 }
87}
88
89static void rna_Lattice_update_data(Main * /*bmain*/, Scene * /*scene*/, PointerRNA *ptr)
90{
91 ID *id = ptr->owner_id;
92
93 DEG_id_tag_update(id, 0);
95}
96
101static void rna_Lattice_update_data_editlatt(Main * /*bmain*/, Scene * /*scene*/, PointerRNA *ptr)
102{
103 ID *id = ptr->owner_id;
104 Lattice *lt = (Lattice *)ptr->owner_id;
105
106 if (lt->editlatt) {
107 Lattice *lt_em = lt->editlatt->latt;
108 lt_em->typeu = lt->typeu;
109 lt_em->typev = lt->typev;
110 lt_em->typew = lt->typew;
111 lt_em->flag = lt->flag;
112 STRNCPY(lt_em->vgroup, lt->vgroup);
113 }
114
115 DEG_id_tag_update(id, 0);
117}
118
119static void rna_Lattice_update_size(Main *bmain, Scene *scene, PointerRNA *ptr)
120{
121 Lattice *lt = (Lattice *)ptr->owner_id;
122 Object *ob;
123 int newu, newv, neww;
124
125 /* We don't modify the actual `pnts`, but go through `opnts` instead. */
126 newu = (lt->opntsu > 0) ? lt->opntsu : lt->pntsu;
127 newv = (lt->opntsv > 0) ? lt->opntsv : lt->pntsv;
128 neww = (lt->opntsw > 0) ? lt->opntsw : lt->pntsw;
129
130 /* #BKE_lattice_resize needs an object, any object will have the same result */
131 for (ob = static_cast<Object *>(bmain->objects.first); ob;
132 ob = static_cast<Object *>(ob->id.next))
133 {
134 if (ob->data == lt) {
135 BKE_lattice_resize(lt, newu, newv, neww, ob);
136 if (lt->editlatt) {
137 BKE_lattice_resize(lt->editlatt->latt, newu, newv, neww, ob);
138 }
139 break;
140 }
141 }
142
143 /* otherwise without, means old points are not repositioned */
144 if (!ob) {
145 BKE_lattice_resize(lt, newu, newv, neww, nullptr);
146 if (lt->editlatt) {
147 BKE_lattice_resize(lt->editlatt->latt, newu, newv, neww, nullptr);
148 }
149 }
150
151 rna_Lattice_update_data(bmain, scene, ptr);
152}
153
154static void rna_Lattice_use_outside_set(PointerRNA *ptr, bool value)
155{
156 Lattice *lt = static_cast<Lattice *>(ptr->data);
157
158 if (value) {
159 lt->flag |= LT_OUTSIDE;
160 }
161 else {
162 lt->flag &= ~LT_OUTSIDE;
163 }
164
165 outside_lattice(lt);
166
167 if (lt->editlatt) {
168 if (value) {
169 lt->editlatt->latt->flag |= LT_OUTSIDE;
170 }
171 else {
172 lt->editlatt->latt->flag &= ~LT_OUTSIDE;
173 }
174
176 }
177}
178
179static int rna_Lattice_size_editable(const PointerRNA *ptr, const char ** /*r_info*/)
180{
181 Lattice *lt = (Lattice *)ptr->data;
182
183 return (lt->key == nullptr) ? int(PROP_EDITABLE) : 0;
184}
185
186static void rna_Lattice_points_u_set(PointerRNA *ptr, int value)
187{
188 Lattice *lt = (Lattice *)ptr->data;
189
190 lt->opntsu = std::clamp(value, 1, 64);
191}
192
193static void rna_Lattice_points_v_set(PointerRNA *ptr, int value)
194{
195 Lattice *lt = (Lattice *)ptr->data;
196
197 lt->opntsv = std::clamp(value, 1, 64);
198}
199
200static void rna_Lattice_points_w_set(PointerRNA *ptr, int value)
201{
202 Lattice *lt = (Lattice *)ptr->data;
203
204 lt->opntsw = std::clamp(value, 1, 64);
205}
206
207static void rna_Lattice_vg_name_set(PointerRNA *ptr, const char *value)
208{
209 Lattice *lt = static_cast<Lattice *>(ptr->data);
210 STRNCPY(lt->vgroup, value);
211
212 if (lt->editlatt) {
213 STRNCPY(lt->editlatt->latt->vgroup, value);
214 }
215}
216
217/* annoying, but is a consequence of RNA structures... */
218static std::optional<std::string> rna_LatticePoint_path(const PointerRNA *ptr)
219{
220 const Lattice *lt = (Lattice *)ptr->owner_id;
221 const void *point = ptr->data;
222 const BPoint *points = nullptr;
223
224 if (lt->editlatt && lt->editlatt->latt->def) {
225 points = lt->editlatt->latt->def;
226 }
227 else {
228 points = lt->def;
229 }
230
231 if (points && point) {
232 int tot = lt->pntsu * lt->pntsv * lt->pntsw;
233
234 /* only return index if in range */
235 if ((point >= (void *)points) && (point < (void *)(points + tot))) {
236 int pt_index = int((BPoint *)point - points);
237
238 return fmt::format("points[{}]", pt_index);
239 }
240 }
241
242 return "";
243}
244
245static bool rna_Lattice_is_editmode_get(PointerRNA *ptr)
246{
247 Lattice *lt = (Lattice *)ptr->owner_id;
248 return (lt->editlatt != nullptr);
249}
250
251#else
252
254{
255 StructRNA *srna;
256 PropertyRNA *prop;
257
258 srna = RNA_def_struct(brna, "LatticePoint", nullptr);
259 RNA_def_struct_sdna(srna, "BPoint");
260 RNA_def_struct_ui_text(srna, "LatticePoint", "Point in the lattice grid");
261 RNA_def_struct_path_func(srna, "rna_LatticePoint_path");
262
263 prop = RNA_def_property(srna, "select", PROP_BOOLEAN, PROP_NONE);
264 RNA_def_property_boolean_sdna(prop, nullptr, "f1", SELECT);
265 RNA_def_property_ui_text(prop, "Point selected", "Selection status");
266
267 prop = RNA_def_property(srna, "co", PROP_FLOAT, PROP_TRANSLATION);
268 RNA_def_property_array(prop, 3);
270 RNA_def_property_float_funcs(prop, "rna_LatticePoint_co_get", nullptr, nullptr);
272 prop,
273 "Location",
274 "Original undeformed location used to calculate the strength of the deform effect "
275 "(edit/animate the Deformed Location instead)");
276
277 prop = RNA_def_property(srna, "co_deform", PROP_FLOAT, PROP_TRANSLATION);
278 RNA_def_property_float_sdna(prop, nullptr, "vec");
279 RNA_def_property_array(prop, 3);
280 RNA_def_property_ui_text(prop, "Deformed Location", "");
281 RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
282
283 prop = RNA_def_property(srna, "weight_softbody", PROP_FLOAT, PROP_NONE);
284 RNA_def_property_float_sdna(prop, nullptr, "weight");
285 RNA_def_property_range(prop, 0.01f, 100.0f);
286 RNA_def_property_ui_text(prop, "Weight", "Softbody goal weight");
287 RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
288
289 prop = RNA_def_property(srna, "groups", PROP_COLLECTION, PROP_NONE);
291 "rna_LatticePoint_groups_begin",
292 "rna_iterator_array_next",
293 "rna_iterator_array_end",
294 "rna_iterator_array_get",
295 nullptr,
296 nullptr,
297 nullptr,
298 nullptr);
299 RNA_def_property_struct_type(prop, "VertexGroupElement");
301 prop, "Groups", "Weights for the vertex groups this point is member of");
302}
303
304static void rna_def_lattice(BlenderRNA *brna)
305{
306 StructRNA *srna;
307 PropertyRNA *prop;
308
309 srna = RNA_def_struct(brna, "Lattice", "ID");
311 srna, "Lattice", "Lattice data-block defining a grid for deforming other objects");
312 RNA_def_struct_ui_icon(srna, ICON_LATTICE_DATA);
313
314 prop = RNA_def_property(srna, "points_u", PROP_INT, PROP_NONE);
315 RNA_def_property_int_sdna(prop, nullptr, "pntsu");
316 RNA_def_property_int_funcs(prop, nullptr, "rna_Lattice_points_u_set", nullptr);
317 RNA_def_property_range(prop, 1, 64);
320 prop, "U", "Points in U direction (cannot be changed when there are shape keys)");
321 RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
322 RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
323
324 prop = RNA_def_property(srna, "points_v", PROP_INT, PROP_NONE);
325 RNA_def_property_int_sdna(prop, nullptr, "pntsv");
326 RNA_def_property_int_funcs(prop, nullptr, "rna_Lattice_points_v_set", nullptr);
327 RNA_def_property_range(prop, 1, 64);
330 prop, "V", "Points in V direction (cannot be changed when there are shape keys)");
331 RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
332 RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
333
334 prop = RNA_def_property(srna, "points_w", PROP_INT, PROP_NONE);
335 RNA_def_property_int_sdna(prop, nullptr, "pntsw");
336 RNA_def_property_int_funcs(prop, nullptr, "rna_Lattice_points_w_set", nullptr);
337 RNA_def_property_range(prop, 1, 64);
340 prop, "W", "Points in W direction (cannot be changed when there are shape keys)");
341 RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
342 RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
343
344 prop = RNA_def_property(srna, "interpolation_type_u", PROP_ENUM, PROP_NONE);
345 RNA_def_property_enum_sdna(prop, nullptr, "typeu");
347 RNA_def_property_ui_text(prop, "Interpolation Type U", "");
348 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
349
350 prop = RNA_def_property(srna, "interpolation_type_v", PROP_ENUM, PROP_NONE);
351 RNA_def_property_enum_sdna(prop, nullptr, "typev");
353 RNA_def_property_ui_text(prop, "Interpolation Type V", "");
354 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
355
356 prop = RNA_def_property(srna, "interpolation_type_w", PROP_ENUM, PROP_NONE);
357 RNA_def_property_enum_sdna(prop, nullptr, "typew");
359 RNA_def_property_ui_text(prop, "Interpolation Type W", "");
360 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
361
362 prop = RNA_def_property(srna, "use_outside", PROP_BOOLEAN, PROP_NONE);
363 RNA_def_property_boolean_sdna(prop, nullptr, "flag", LT_OUTSIDE);
364 RNA_def_property_boolean_funcs(prop, nullptr, "rna_Lattice_use_outside_set");
366 prop, "Outside", "Only display and take into account the outer vertices");
367 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
368
369 prop = RNA_def_property(srna, "vertex_group", PROP_STRING, PROP_NONE);
370 RNA_def_property_string_sdna(prop, nullptr, "vgroup");
372 prop, "Vertex Group", "Vertex group to apply the influence of the lattice");
373 RNA_def_property_string_funcs(prop, nullptr, nullptr, "rna_Lattice_vg_name_set");
374 RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
375
376 prop = RNA_def_property(srna, "shape_keys", PROP_POINTER, PROP_NONE);
377 RNA_def_property_pointer_sdna(prop, nullptr, "key");
380 RNA_def_property_ui_text(prop, "Shape Keys", "");
381
382 prop = RNA_def_property(srna, "points", PROP_COLLECTION, PROP_NONE);
383 RNA_def_property_struct_type(prop, "LatticePoint");
385 "rna_Lattice_points_begin",
386 "rna_iterator_array_next",
387 "rna_iterator_array_end",
388 "rna_iterator_array_get",
389 nullptr,
390 nullptr,
391 nullptr,
392 nullptr);
393 RNA_def_property_ui_text(prop, "Points", "Points of the lattice");
394
395 prop = RNA_def_property(srna, "is_editmode", PROP_BOOLEAN, PROP_NONE);
396 RNA_def_property_boolean_funcs(prop, "rna_Lattice_is_editmode_get", nullptr);
398 RNA_def_property_ui_text(prop, "Is Editmode", "True when used in editmode");
399
400 /* pointers */
402
403 RNA_api_lattice(srna);
404}
405
407{
408 rna_def_lattice(brna);
410}
411
412#endif
support for deformation groups and hooks.
void outside_lattice(Lattice *lt)
Definition lattice.cc:402
void BKE_lattice_index_to_uvw(const Lattice *lt, int index, int *r_u, int *r_v, int *r_w)
Definition lattice.cc:203
void BKE_lattice_resize(Lattice *lt, int u_new, int v_new, int w_new, Object *lt_ob)
Definition lattice.cc:273
#define STRNCPY(dst, src)
Definition BLI_string.h:593
void DEG_id_tag_update(ID *id, unsigned int flags)
@ LT_OUTSIDE
Object is a sort of wrapper for general info.
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Gabor Generate Gabor noise Gradient Generate interpolated color and intensity values based on the input vector Magic Generate a psychedelic color texture Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a point
@ PROP_FLOAT
Definition RNA_types.hh:67
@ PROP_BOOLEAN
Definition RNA_types.hh:65
@ PROP_ENUM
Definition RNA_types.hh:69
@ PROP_INT
Definition RNA_types.hh:66
@ PROP_STRING
Definition RNA_types.hh:68
@ PROP_POINTER
Definition RNA_types.hh:70
@ PROP_COLLECTION
Definition RNA_types.hh:71
@ PROPOVERRIDE_OVERRIDABLE_LIBRARY
Definition RNA_types.hh:355
@ PROP_ANIMATABLE
Definition RNA_types.hh:220
@ PROP_EDITABLE
Definition RNA_types.hh:207
@ PROP_PTR_NO_OWNERSHIP
Definition RNA_types.hh:284
@ PROP_NONE
Definition RNA_types.hh:136
@ PROP_TRANSLATION
Definition RNA_types.hh:164
#define NC_GEOM
Definition WM_types.hh:360
#define ND_DATA
Definition WM_types.hh:475
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define SELECT
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
void index(const bNode &, void *r_value)
void rna_iterator_array_begin(CollectionPropertyIterator *iter, void *ptr, int itemsize, int length, bool free_ptr, IteratorSkipFunc skip)
void rna_def_animdata_common(StructRNA *srna)
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_boolean_sdna(PropertyRNA *prop, const char *structname, const char *propname, int64_t bit)
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:31
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:413
void * next
Definition DNA_ID.h:416
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:4126