Blender  V3.3
rna_lattice.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <stdlib.h>
8 
9 #include "DNA_curve_types.h"
10 #include "DNA_key_types.h"
11 #include "DNA_lattice_types.h"
12 #include "DNA_meshdata_types.h"
13 #include "DNA_object_types.h"
14 
15 #include "BLI_utildefines.h"
16 
17 #include "RNA_define.h"
18 #include "RNA_enum_types.h"
19 #include "rna_internal.h"
20 
21 #ifdef RNA_RUNTIME
22 
23 # include "DNA_object_types.h"
24 # include "DNA_scene_types.h"
25 
26 # include "BKE_deform.h"
27 # include "BKE_lattice.h"
28 # include "BKE_main.h"
29 # include "BLI_string.h"
30 
31 # include "DEG_depsgraph.h"
32 
33 # include "ED_lattice.h"
34 # include "WM_api.h"
35 # include "WM_types.h"
36 
37 static void rna_LatticePoint_co_get(PointerRNA *ptr, float *values)
38 {
39  Lattice *lt = (Lattice *)ptr->owner_id;
40  BPoint *bp = (BPoint *)ptr->data;
41  int index = bp - lt->def;
42  int u, v, w;
43 
44  BKE_lattice_index_to_uvw(lt, index, &u, &v, &w);
45 
46  values[0] = lt->fu + u * lt->du;
47  values[1] = lt->fv + v * lt->dv;
48  values[2] = lt->fw + w * lt->dw;
49 }
50 
51 static void rna_LatticePoint_groups_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
52 {
53  Lattice *lt = (Lattice *)ptr->owner_id;
54 
55  if (lt->dvert) {
56  BPoint *bp = (BPoint *)ptr->data;
57  MDeformVert *dvert = lt->dvert + (bp - lt->def);
58 
60  iter, (void *)dvert->dw, sizeof(MDeformWeight), dvert->totweight, 0, NULL);
61  }
62  else {
63  rna_iterator_array_begin(iter, NULL, 0, 0, 0, NULL);
64  }
65 }
66 
67 static void rna_Lattice_points_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
68 {
69  Lattice *lt = (Lattice *)ptr->data;
70  int tot = lt->pntsu * lt->pntsv * lt->pntsw;
71 
72  if (lt->editlatt && lt->editlatt->latt->def) {
73  rna_iterator_array_begin(iter, (void *)lt->editlatt->latt->def, sizeof(BPoint), tot, 0, NULL);
74  }
75  else if (lt->def) {
76  rna_iterator_array_begin(iter, (void *)lt->def, sizeof(BPoint), tot, 0, NULL);
77  }
78  else {
79  rna_iterator_array_begin(iter, NULL, 0, 0, 0, NULL);
80  }
81 }
82 
83 static void rna_Lattice_update_data(Main *UNUSED(bmain), Scene *UNUSED(scene), PointerRNA *ptr)
84 {
85  ID *id = ptr->owner_id;
86 
87  DEG_id_tag_update(id, 0);
89 }
90 
91 /* copy settings to editlattice,
92  * we could split this up differently (one update call per property)
93  * but for now that's overkill
94  */
95 static void rna_Lattice_update_data_editlatt(Main *UNUSED(bmain),
96  Scene *UNUSED(scene),
97  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  BLI_strncpy(lt_em->vgroup, lt->vgroup, sizeof(lt_em->vgroup));
109  }
110 
111  DEG_id_tag_update(id, 0);
113 }
114 
115 static 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 = bmain->objects.first; ob; ob = ob->id.next) {
128  if (ob->data == lt) {
129  BKE_lattice_resize(lt, newu, newv, neww, ob);
130  if (lt->editlatt) {
131  BKE_lattice_resize(lt->editlatt->latt, newu, newv, neww, ob);
132  }
133  break;
134  }
135  }
136 
137  /* otherwise without, means old points are not repositioned */
138  if (!ob) {
139  BKE_lattice_resize(lt, newu, newv, neww, NULL);
140  if (lt->editlatt) {
141  BKE_lattice_resize(lt->editlatt->latt, newu, newv, neww, NULL);
142  }
143  }
144 
145  rna_Lattice_update_data(bmain, scene, ptr);
146 }
147 
148 static void rna_Lattice_use_outside_set(PointerRNA *ptr, bool value)
149 {
150  Lattice *lt = ptr->data;
151 
152  if (value) {
153  lt->flag |= LT_OUTSIDE;
154  }
155  else {
156  lt->flag &= ~LT_OUTSIDE;
157  }
158 
159  outside_lattice(lt);
160 
161  if (lt->editlatt) {
162  if (value) {
163  lt->editlatt->latt->flag |= LT_OUTSIDE;
164  }
165  else {
166  lt->editlatt->latt->flag &= ~LT_OUTSIDE;
167  }
168 
170  }
171 }
172 
173 static int rna_Lattice_size_editable(PointerRNA *ptr, const char **UNUSED(r_info))
174 {
175  Lattice *lt = (Lattice *)ptr->data;
176 
177  return (lt->key == NULL) ? PROP_EDITABLE : 0;
178 }
179 
180 static void rna_Lattice_points_u_set(PointerRNA *ptr, int value)
181 {
182  Lattice *lt = (Lattice *)ptr->data;
183 
184  lt->opntsu = CLAMPIS(value, 1, 64);
185 }
186 
187 static void rna_Lattice_points_v_set(PointerRNA *ptr, int value)
188 {
189  Lattice *lt = (Lattice *)ptr->data;
190 
191  lt->opntsv = CLAMPIS(value, 1, 64);
192 }
193 
194 static void rna_Lattice_points_w_set(PointerRNA *ptr, int value)
195 {
196  Lattice *lt = (Lattice *)ptr->data;
197 
198  lt->opntsw = CLAMPIS(value, 1, 64);
199 }
200 
201 static void rna_Lattice_vg_name_set(PointerRNA *ptr, const char *value)
202 {
203  Lattice *lt = ptr->data;
204  BLI_strncpy(lt->vgroup, value, sizeof(lt->vgroup));
205 
206  if (lt->editlatt) {
207  BLI_strncpy(lt->editlatt->latt->vgroup, value, sizeof(lt->editlatt->latt->vgroup));
208  }
209 }
210 
211 /* annoying, but is a consequence of RNA structures... */
212 static char *rna_LatticePoint_path(const PointerRNA *ptr)
213 {
214  const Lattice *lt = (Lattice *)ptr->owner_id;
215  const void *point = ptr->data;
216  const BPoint *points = NULL;
217 
218  if (lt->editlatt && lt->editlatt->latt->def) {
219  points = lt->editlatt->latt->def;
220  }
221  else {
222  points = lt->def;
223  }
224 
225  if (points && point) {
226  int tot = lt->pntsu * lt->pntsv * lt->pntsw;
227 
228  /* only return index if in range */
229  if ((point >= (void *)points) && (point < (void *)(points + tot))) {
230  int pt_index = (int)((BPoint *)point - points);
231 
232  return BLI_sprintfN("points[%d]", pt_index);
233  }
234  }
235 
236  return BLI_strdup("");
237 }
238 
239 static bool rna_Lattice_is_editmode_get(PointerRNA *ptr)
240 {
241  Lattice *lt = (Lattice *)ptr->owner_id;
242  return (lt->editlatt != NULL);
243 }
244 
245 #else
246 
248 {
249  StructRNA *srna;
250  PropertyRNA *prop;
251 
252  srna = RNA_def_struct(brna, "LatticePoint", NULL);
253  RNA_def_struct_sdna(srna, "BPoint");
254  RNA_def_struct_ui_text(srna, "LatticePoint", "Point in the lattice grid");
255  RNA_def_struct_path_func(srna, "rna_LatticePoint_path");
256 
257  prop = RNA_def_property(srna, "select", PROP_BOOLEAN, PROP_NONE);
259  RNA_def_property_ui_text(prop, "Point selected", "Selection status");
260 
261  prop = RNA_def_property(srna, "co", PROP_FLOAT, PROP_TRANSLATION);
262  RNA_def_property_array(prop, 3);
264  RNA_def_property_float_funcs(prop, "rna_LatticePoint_co_get", NULL, NULL);
266  prop,
267  "Location",
268  "Original undeformed location used to calculate the strength of the deform effect "
269  "(edit/animate the Deformed Location instead)");
270 
271  prop = RNA_def_property(srna, "co_deform", PROP_FLOAT, PROP_TRANSLATION);
272  RNA_def_property_float_sdna(prop, NULL, "vec");
273  RNA_def_property_array(prop, 3);
274  RNA_def_property_ui_text(prop, "Deformed Location", "");
275  RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
276 
277  prop = RNA_def_property(srna, "weight_softbody", PROP_FLOAT, PROP_NONE);
278  RNA_def_property_float_sdna(prop, NULL, "weight");
279  RNA_def_property_range(prop, 0.01f, 100.0f);
280  RNA_def_property_ui_text(prop, "Weight", "Softbody goal weight");
281  RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
282 
283  prop = RNA_def_property(srna, "groups", PROP_COLLECTION, PROP_NONE);
285  "rna_LatticePoint_groups_begin",
286  "rna_iterator_array_next",
287  "rna_iterator_array_end",
288  "rna_iterator_array_get",
289  NULL,
290  NULL,
291  NULL,
292  NULL);
293  RNA_def_property_struct_type(prop, "VertexGroupElement");
295  prop, "Groups", "Weights for the vertex groups this point is member of");
296 }
297 
298 static void rna_def_lattice(BlenderRNA *brna)
299 {
300  StructRNA *srna;
301  PropertyRNA *prop;
302 
303  srna = RNA_def_struct(brna, "Lattice", "ID");
305  srna, "Lattice", "Lattice data-block defining a grid for deforming other objects");
306  RNA_def_struct_ui_icon(srna, ICON_LATTICE_DATA);
307 
308  prop = RNA_def_property(srna, "points_u", PROP_INT, PROP_NONE);
309  RNA_def_property_int_sdna(prop, NULL, "pntsu");
310  RNA_def_property_int_funcs(prop, NULL, "rna_Lattice_points_u_set", NULL);
311  RNA_def_property_range(prop, 1, 64);
314  prop, "U", "Point in U direction (can't be changed when there are shape keys)");
315  RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
316  RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
317 
318  prop = RNA_def_property(srna, "points_v", PROP_INT, PROP_NONE);
319  RNA_def_property_int_sdna(prop, NULL, "pntsv");
320  RNA_def_property_int_funcs(prop, NULL, "rna_Lattice_points_v_set", NULL);
321  RNA_def_property_range(prop, 1, 64);
324  prop, "V", "Point in V direction (can't be changed when there are shape keys)");
325  RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
326  RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
327 
328  prop = RNA_def_property(srna, "points_w", PROP_INT, PROP_NONE);
329  RNA_def_property_int_sdna(prop, NULL, "pntsw");
330  RNA_def_property_int_funcs(prop, NULL, "rna_Lattice_points_w_set", NULL);
331  RNA_def_property_range(prop, 1, 64);
334  prop, "W", "Point in W direction (can't be changed when there are shape keys)");
335  RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
336  RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
337 
338  prop = RNA_def_property(srna, "interpolation_type_u", PROP_ENUM, PROP_NONE);
339  RNA_def_property_enum_sdna(prop, NULL, "typeu");
341  RNA_def_property_ui_text(prop, "Interpolation Type U", "");
342  RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
343 
344  prop = RNA_def_property(srna, "interpolation_type_v", PROP_ENUM, PROP_NONE);
345  RNA_def_property_enum_sdna(prop, NULL, "typev");
347  RNA_def_property_ui_text(prop, "Interpolation Type V", "");
348  RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
349 
350  prop = RNA_def_property(srna, "interpolation_type_w", PROP_ENUM, PROP_NONE);
351  RNA_def_property_enum_sdna(prop, NULL, "typew");
353  RNA_def_property_ui_text(prop, "Interpolation Type W", "");
354  RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
355 
356  prop = RNA_def_property(srna, "use_outside", PROP_BOOLEAN, PROP_NONE);
358  RNA_def_property_boolean_funcs(prop, NULL, "rna_Lattice_use_outside_set");
360  prop, "Outside", "Only display and take into account the outer vertices");
361  RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
362 
363  prop = RNA_def_property(srna, "vertex_group", PROP_STRING, PROP_NONE);
364  RNA_def_property_string_sdna(prop, NULL, "vgroup");
366  prop, "Vertex Group", "Vertex group to apply the influence of the lattice");
367  RNA_def_property_string_funcs(prop, NULL, NULL, "rna_Lattice_vg_name_set");
368  RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
369 
370  prop = RNA_def_property(srna, "shape_keys", PROP_POINTER, PROP_NONE);
371  RNA_def_property_pointer_sdna(prop, NULL, "key");
374  RNA_def_property_ui_text(prop, "Shape Keys", "");
375 
376  prop = RNA_def_property(srna, "points", PROP_COLLECTION, PROP_NONE);
377  RNA_def_property_struct_type(prop, "LatticePoint");
379  "rna_Lattice_points_begin",
380  "rna_iterator_array_next",
381  "rna_iterator_array_end",
382  "rna_iterator_array_get",
383  NULL,
384  NULL,
385  NULL,
386  NULL);
387  RNA_def_property_ui_text(prop, "Points", "Points of the lattice");
388 
389  prop = RNA_def_property(srna, "is_editmode", PROP_BOOLEAN, PROP_NONE);
390  RNA_def_property_boolean_funcs(prop, "rna_Lattice_is_editmode_get", NULL);
392  RNA_def_property_ui_text(prop, "Is Editmode", "True when used in editmode");
393 
394  /* pointers */
396 
397  RNA_api_lattice(srna);
398 }
399 
401 {
402  rna_def_lattice(brna);
403  rna_def_latticepoint(brna);
404 }
405 
406 #endif
support for deformation groups and hooks.
void BKE_lattice_index_to_uvw(struct Lattice *lt, int index, int *r_u, int *r_v, int *r_w)
Definition: lattice.c:213
void BKE_lattice_resize(struct Lattice *lt, int u, int v, int w, struct Object *ltOb)
Definition: lattice.c:280
void outside_lattice(struct Lattice *lt)
Definition: lattice.c:421
size_t size_t char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:42
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
#define CLAMPIS(a, b, c)
#define UNUSED(x)
void DEG_id_tag_update(struct ID *id, int flag)
#define 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 Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise 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.h:61
@ PROP_BOOLEAN
Definition: RNA_types.h:59
@ PROP_ENUM
Definition: RNA_types.h:63
@ PROP_INT
Definition: RNA_types.h:60
@ PROP_STRING
Definition: RNA_types.h:62
@ PROP_POINTER
Definition: RNA_types.h:64
@ PROP_COLLECTION
Definition: RNA_types.h:65
@ PROPOVERRIDE_OVERRIDABLE_LIBRARY
Definition: RNA_types.h:312
@ PROP_ANIMATABLE
Definition: RNA_types.h:202
@ PROP_EDITABLE
Definition: RNA_types.h:189
@ PROP_PTR_NO_OWNERSHIP
Definition: RNA_types.h:257
@ PROP_NONE
Definition: RNA_types.h:126
@ PROP_TRANSLATION
Definition: RNA_types.h:154
#define NC_GEOM
Definition: WM_types.h:343
#define ND_DATA
Definition: WM_types.h:456
return(oflags[bm->toolflag_index].f &oflag) !=0
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
#define SELECT
Scene scene
void rna_iterator_array_begin(CollectionPropertyIterator *iter, void *ptr, int itemsize, int length, bool free_ptr, IteratorSkipFunc skip)
Definition: rna_access.c:4781
void rna_def_animdata_common(StructRNA *srna)
void RNA_def_property_pointer_sdna(PropertyRNA *prop, const char *structname, const char *propname)
Definition: rna_define.c:2740
void RNA_def_struct_path_func(StructRNA *srna, const char *path)
Definition: rna_define.c:1193
void RNA_def_property_boolean_sdna(PropertyRNA *prop, const char *structname, const char *propname, int64_t bit)
Definition: rna_define.c:2236
void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set)
Definition: rna_define.c:3285
void RNA_def_property_float_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
Definition: rna_define.c:3126
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
Definition: rna_define.c:1645
void RNA_def_property_string_sdna(PropertyRNA *prop, const char *structname, const char *propname)
Definition: rna_define.c:2695
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)
Definition: rna_define.c:3420
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
Definition: rna_define.c:1237
void RNA_def_property_boolean_funcs(PropertyRNA *prop, const char *get, const char *set)
Definition: rna_define.c:2944
void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item)
Definition: rna_define.c:1872
void RNA_def_struct_sdna(StructRNA *srna, const char *structname)
Definition: rna_define.c:1048
void RNA_def_property_array(PropertyRNA *prop, int length)
Definition: rna_define.c:1539
void RNA_def_property_range(PropertyRNA *prop, double min, double max)
Definition: rna_define.c:1737
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
Definition: rna_define.c:1772
void RNA_def_property_update(PropertyRNA *prop, int noteflag, const char *func)
Definition: rna_define.c:2900
void RNA_def_property_editable_func(PropertyRNA *prop, const char *editable)
Definition: rna_define.c:2855
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
Definition: rna_define.c:1257
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
Definition: rna_define.c:1495
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
Definition: rna_define.c:1028
void RNA_def_property_enum_sdna(PropertyRNA *prop, const char *structname, const char *propname)
Definition: rna_define.c:2601
void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
Definition: rna_define.c:3028
void RNA_def_struct_ui_icon(StructRNA *srna, int icon)
Definition: rna_define.c:1245
void RNA_def_property_float_sdna(PropertyRNA *prop, const char *structname, const char *propname)
Definition: rna_define.c:2493
void RNA_def_property_int_sdna(PropertyRNA *prop, const char *structname, const char *propname)
Definition: rna_define.c:2343
void RNA_def_property_override_flag(PropertyRNA *prop, PropertyOverrideFlag flag)
Definition: rna_define.c:1503
void RNA_api_lattice(struct StructRNA *srna)
const EnumPropertyItem rna_enum_keyblock_type_items[]
Definition: rna_key.c:29
static void rna_def_lattice(BlenderRNA *brna)
Definition: rna_lattice.c:298
void RNA_def_lattice(BlenderRNA *brna)
Definition: rna_lattice.c:400
static void rna_def_latticepoint(BlenderRNA *brna)
Definition: rna_lattice.c:247
struct Lattice * latt
Definition: DNA_ID.h:368
struct Key * key
struct MDeformVert * dvert
struct EditLatt * editlatt
char vgroup[64]
struct BPoint * def
void * first
Definition: DNA_listBase.h:31
Definition: BKE_main.h:121
ListBase objects
Definition: BKE_main.h:170
void * data
Definition: RNA_types.h:38
struct ID * owner_id
Definition: RNA_types.h:36
void WM_main_add_notifier(unsigned int type, void *reference)
PointerRNA * ptr
Definition: wm_files.c:3480