Blender  V2.93
rna_lattice.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <stdlib.h>
22 
23 #include "DNA_curve_types.h"
24 #include "DNA_key_types.h"
25 #include "DNA_lattice_types.h"
26 #include "DNA_meshdata_types.h"
27 #include "DNA_object_types.h"
28 
29 #include "BLI_utildefines.h"
30 
31 #include "RNA_define.h"
32 #include "RNA_enum_types.h"
33 #include "rna_internal.h"
34 
35 #ifdef RNA_RUNTIME
36 
37 # include "DNA_object_types.h"
38 # include "DNA_scene_types.h"
39 
40 # include "BKE_deform.h"
41 # include "BKE_lattice.h"
42 # include "BKE_main.h"
43 # include "BLI_string.h"
44 
45 # include "DEG_depsgraph.h"
46 
47 # include "ED_lattice.h"
48 # include "WM_api.h"
49 # include "WM_types.h"
50 
51 static void rna_LatticePoint_co_get(PointerRNA *ptr, float *values)
52 {
53  Lattice *lt = (Lattice *)ptr->owner_id;
54  BPoint *bp = (BPoint *)ptr->data;
55  int index = bp - lt->def;
56  int u, v, w;
57 
58  BKE_lattice_index_to_uvw(lt, index, &u, &v, &w);
59 
60  values[0] = lt->fu + u * lt->du;
61  values[1] = lt->fv + v * lt->dv;
62  values[2] = lt->fw + w * lt->dw;
63 }
64 
65 static void rna_LatticePoint_groups_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
66 {
67  Lattice *lt = (Lattice *)ptr->owner_id;
68 
69  if (lt->dvert) {
70  BPoint *bp = (BPoint *)ptr->data;
71  MDeformVert *dvert = lt->dvert + (bp - lt->def);
72 
74  iter, (void *)dvert->dw, sizeof(MDeformWeight), dvert->totweight, 0, NULL);
75  }
76  else {
77  rna_iterator_array_begin(iter, NULL, 0, 0, 0, NULL);
78  }
79 }
80 
81 static void rna_Lattice_points_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
82 {
83  Lattice *lt = (Lattice *)ptr->data;
84  int tot = lt->pntsu * lt->pntsv * lt->pntsw;
85 
86  if (lt->editlatt && lt->editlatt->latt->def) {
87  rna_iterator_array_begin(iter, (void *)lt->editlatt->latt->def, sizeof(BPoint), tot, 0, NULL);
88  }
89  else if (lt->def) {
90  rna_iterator_array_begin(iter, (void *)lt->def, sizeof(BPoint), tot, 0, NULL);
91  }
92  else {
93  rna_iterator_array_begin(iter, NULL, 0, 0, 0, NULL);
94  }
95 }
96 
97 static void rna_Lattice_update_data(Main *UNUSED(bmain), Scene *UNUSED(scene), PointerRNA *ptr)
98 {
99  ID *id = ptr->owner_id;
100 
101  DEG_id_tag_update(id, 0);
103 }
104 
105 /* copy settings to editlattice,
106  * we could split this up differently (one update call per property)
107  * but for now that's overkill
108  */
109 static void rna_Lattice_update_data_editlatt(Main *UNUSED(bmain),
110  Scene *UNUSED(scene),
111  PointerRNA *ptr)
112 {
113  ID *id = ptr->owner_id;
114  Lattice *lt = (Lattice *)ptr->owner_id;
115 
116  if (lt->editlatt) {
117  Lattice *lt_em = lt->editlatt->latt;
118  lt_em->typeu = lt->typeu;
119  lt_em->typev = lt->typev;
120  lt_em->typew = lt->typew;
121  lt_em->flag = lt->flag;
122  BLI_strncpy(lt_em->vgroup, lt->vgroup, sizeof(lt_em->vgroup));
123  }
124 
125  DEG_id_tag_update(id, 0);
127 }
128 
129 static void rna_Lattice_update_size(Main *bmain, Scene *scene, PointerRNA *ptr)
130 {
131  Lattice *lt = (Lattice *)ptr->owner_id;
132  Object *ob;
133  int newu, newv, neww;
134 
135  /* We don't modify the actual `pnts`, but go through `opnts` instead. */
136  newu = (lt->opntsu > 0) ? lt->opntsu : lt->pntsu;
137  newv = (lt->opntsv > 0) ? lt->opntsv : lt->pntsv;
138  neww = (lt->opntsw > 0) ? lt->opntsw : lt->pntsw;
139 
140  /* #BKE_lattice_resize needs an object, any object will have the same result */
141  for (ob = bmain->objects.first; ob; ob = ob->id.next) {
142  if (ob->data == lt) {
143  BKE_lattice_resize(lt, newu, newv, neww, ob);
144  if (lt->editlatt) {
145  BKE_lattice_resize(lt->editlatt->latt, newu, newv, neww, ob);
146  }
147  break;
148  }
149  }
150 
151  /* otherwise without, means old points are not repositioned */
152  if (!ob) {
153  BKE_lattice_resize(lt, newu, newv, neww, NULL);
154  if (lt->editlatt) {
155  BKE_lattice_resize(lt->editlatt->latt, newu, newv, neww, NULL);
156  }
157  }
158 
159  rna_Lattice_update_data(bmain, scene, ptr);
160 }
161 
162 static void rna_Lattice_use_outside_set(PointerRNA *ptr, bool value)
163 {
164  Lattice *lt = ptr->data;
165 
166  if (value) {
167  lt->flag |= LT_OUTSIDE;
168  }
169  else {
170  lt->flag &= ~LT_OUTSIDE;
171  }
172 
173  outside_lattice(lt);
174 
175  if (lt->editlatt) {
176  if (value) {
177  lt->editlatt->latt->flag |= LT_OUTSIDE;
178  }
179  else {
180  lt->editlatt->latt->flag &= ~LT_OUTSIDE;
181  }
182 
184  }
185 }
186 
187 static int rna_Lattice_size_editable(PointerRNA *ptr, const char **UNUSED(r_info))
188 {
189  Lattice *lt = (Lattice *)ptr->data;
190 
191  return (lt->key == NULL) ? PROP_EDITABLE : 0;
192 }
193 
194 static void rna_Lattice_points_u_set(PointerRNA *ptr, int value)
195 {
196  Lattice *lt = (Lattice *)ptr->data;
197 
198  lt->opntsu = CLAMPIS(value, 1, 64);
199 }
200 
201 static void rna_Lattice_points_v_set(PointerRNA *ptr, int value)
202 {
203  Lattice *lt = (Lattice *)ptr->data;
204 
205  lt->opntsv = CLAMPIS(value, 1, 64);
206 }
207 
208 static void rna_Lattice_points_w_set(PointerRNA *ptr, int value)
209 {
210  Lattice *lt = (Lattice *)ptr->data;
211 
212  lt->opntsw = CLAMPIS(value, 1, 64);
213 }
214 
215 static void rna_Lattice_vg_name_set(PointerRNA *ptr, const char *value)
216 {
217  Lattice *lt = ptr->data;
218  BLI_strncpy(lt->vgroup, value, sizeof(lt->vgroup));
219 
220  if (lt->editlatt) {
221  BLI_strncpy(lt->editlatt->latt->vgroup, value, sizeof(lt->editlatt->latt->vgroup));
222  }
223 }
224 
225 /* annoying, but is a consequence of RNA structures... */
226 static char *rna_LatticePoint_path(PointerRNA *ptr)
227 {
228  Lattice *lt = (Lattice *)ptr->owner_id;
229  void *point = ptr->data;
230  BPoint *points = NULL;
231 
232  if (lt->editlatt && lt->editlatt->latt->def) {
233  points = lt->editlatt->latt->def;
234  }
235  else {
236  points = lt->def;
237  }
238 
239  if (points && point) {
240  int tot = lt->pntsu * lt->pntsv * lt->pntsw;
241 
242  /* only return index if in range */
243  if ((point >= (void *)points) && (point < (void *)(points + tot))) {
244  int pt_index = (int)((BPoint *)point - points);
245 
246  return BLI_sprintfN("points[%d]", pt_index);
247  }
248  }
249 
250  return BLI_strdup("");
251 }
252 
253 static bool rna_Lattice_is_editmode_get(PointerRNA *ptr)
254 {
255  Lattice *lt = (Lattice *)ptr->owner_id;
256  return (lt->editlatt != NULL);
257 }
258 
259 #else
260 
262 {
263  StructRNA *srna;
264  PropertyRNA *prop;
265 
266  srna = RNA_def_struct(brna, "LatticePoint", NULL);
267  RNA_def_struct_sdna(srna, "BPoint");
268  RNA_def_struct_ui_text(srna, "LatticePoint", "Point in the lattice grid");
269  RNA_def_struct_path_func(srna, "rna_LatticePoint_path");
270 
271  prop = RNA_def_property(srna, "select", PROP_BOOLEAN, PROP_NONE);
273  RNA_def_property_ui_text(prop, "Point selected", "Selection status");
274 
275  prop = RNA_def_property(srna, "co", PROP_FLOAT, PROP_TRANSLATION);
276  RNA_def_property_array(prop, 3);
278  RNA_def_property_float_funcs(prop, "rna_LatticePoint_co_get", NULL, NULL);
280  prop,
281  "Location",
282  "Original undeformed location used to calculate the strength of the deform effect "
283  "(edit/animate the Deformed Location instead)");
284 
285  prop = RNA_def_property(srna, "co_deform", PROP_FLOAT, PROP_TRANSLATION);
286  RNA_def_property_float_sdna(prop, NULL, "vec");
287  RNA_def_property_array(prop, 3);
288  RNA_def_property_ui_text(prop, "Deformed Location", "");
289  RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
290 
291  prop = RNA_def_property(srna, "weight_softbody", PROP_FLOAT, PROP_NONE);
292  RNA_def_property_float_sdna(prop, NULL, "weight");
293  RNA_def_property_range(prop, 0.01f, 100.0f);
294  RNA_def_property_ui_text(prop, "Weight", "Softbody goal weight");
295  RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
296 
297  prop = RNA_def_property(srna, "groups", PROP_COLLECTION, PROP_NONE);
299  "rna_LatticePoint_groups_begin",
300  "rna_iterator_array_next",
301  "rna_iterator_array_end",
302  "rna_iterator_array_get",
303  NULL,
304  NULL,
305  NULL,
306  NULL);
307  RNA_def_property_struct_type(prop, "VertexGroupElement");
309  prop, "Groups", "Weights for the vertex groups this point is member of");
310 }
311 
312 static void rna_def_lattice(BlenderRNA *brna)
313 {
314  StructRNA *srna;
315  PropertyRNA *prop;
316 
317  srna = RNA_def_struct(brna, "Lattice", "ID");
319  srna, "Lattice", "Lattice data-block defining a grid for deforming other objects");
320  RNA_def_struct_ui_icon(srna, ICON_LATTICE_DATA);
321 
322  prop = RNA_def_property(srna, "points_u", PROP_INT, PROP_NONE);
323  RNA_def_property_int_sdna(prop, NULL, "pntsu");
324  RNA_def_property_int_funcs(prop, NULL, "rna_Lattice_points_u_set", NULL);
325  RNA_def_property_range(prop, 1, 64);
328  prop, "U", "Point in U direction (can't 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_v", PROP_INT, PROP_NONE);
333  RNA_def_property_int_sdna(prop, NULL, "pntsv");
334  RNA_def_property_int_funcs(prop, NULL, "rna_Lattice_points_v_set", NULL);
335  RNA_def_property_range(prop, 1, 64);
338  prop, "V", "Point in V direction (can't 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, "points_w", PROP_INT, PROP_NONE);
343  RNA_def_property_int_sdna(prop, NULL, "pntsw");
344  RNA_def_property_int_funcs(prop, NULL, "rna_Lattice_points_w_set", NULL);
345  RNA_def_property_range(prop, 1, 64);
348  prop, "W", "Point in W direction (can't be changed when there are shape keys)");
349  RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
350  RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
351 
352  prop = RNA_def_property(srna, "interpolation_type_u", PROP_ENUM, PROP_NONE);
353  RNA_def_property_enum_sdna(prop, NULL, "typeu");
355  RNA_def_property_ui_text(prop, "Interpolation Type U", "");
356  RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
357 
358  prop = RNA_def_property(srna, "interpolation_type_v", PROP_ENUM, PROP_NONE);
359  RNA_def_property_enum_sdna(prop, NULL, "typev");
361  RNA_def_property_ui_text(prop, "Interpolation Type V", "");
362  RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
363 
364  prop = RNA_def_property(srna, "interpolation_type_w", PROP_ENUM, PROP_NONE);
365  RNA_def_property_enum_sdna(prop, NULL, "typew");
367  RNA_def_property_ui_text(prop, "Interpolation Type W", "");
368  RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
369 
370  prop = RNA_def_property(srna, "use_outside", PROP_BOOLEAN, PROP_NONE);
372  RNA_def_property_boolean_funcs(prop, NULL, "rna_Lattice_use_outside_set");
374  prop, "Outside", "Only display and take into account the outer vertices");
375  RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
376 
377  prop = RNA_def_property(srna, "vertex_group", PROP_STRING, PROP_NONE);
378  RNA_def_property_string_sdna(prop, NULL, "vgroup");
380  prop, "Vertex Group", "Vertex group to apply the influence of the lattice");
381  RNA_def_property_string_funcs(prop, NULL, NULL, "rna_Lattice_vg_name_set");
382  RNA_def_property_update(prop, 0, "rna_Lattice_update_data_editlatt");
383 
384  prop = RNA_def_property(srna, "shape_keys", PROP_POINTER, PROP_NONE);
385  RNA_def_property_pointer_sdna(prop, NULL, "key");
388  RNA_def_property_ui_text(prop, "Shape Keys", "");
389 
390  prop = RNA_def_property(srna, "points", PROP_COLLECTION, PROP_NONE);
391  RNA_def_property_struct_type(prop, "LatticePoint");
393  "rna_Lattice_points_begin",
394  "rna_iterator_array_next",
395  "rna_iterator_array_end",
396  "rna_iterator_array_get",
397  NULL,
398  NULL,
399  NULL,
400  NULL);
401  RNA_def_property_ui_text(prop, "Points", "Points of the lattice");
402 
403  prop = RNA_def_property(srna, "is_editmode", PROP_BOOLEAN, PROP_NONE);
404  RNA_def_property_boolean_funcs(prop, "rna_Lattice_is_editmode_get", NULL);
406  RNA_def_property_ui_text(prop, "Is Editmode", "True when used in editmode");
407 
408  /* pointers */
410 
411  RNA_api_lattice(srna);
412 }
413 
415 {
416  rna_def_lattice(brna);
417  rna_def_latticepoint(brna);
418 }
419 
420 #endif
support for deformation groups and hooks.
void BKE_lattice_index_to_uvw(struct Lattice *lt, const int index, int *r_u, int *r_v, int *r_w)
Definition: lattice.c:222
void BKE_lattice_resize(struct Lattice *lt, int u, int v, int w, struct Object *ltOb)
Definition: lattice.c:289
void outside_lattice(struct Lattice *lt)
Definition: lattice.c:430
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:70
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
#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.
@ PROP_FLOAT
Definition: RNA_types.h:75
@ PROP_BOOLEAN
Definition: RNA_types.h:73
@ PROP_ENUM
Definition: RNA_types.h:77
@ PROP_INT
Definition: RNA_types.h:74
@ PROP_STRING
Definition: RNA_types.h:76
@ PROP_POINTER
Definition: RNA_types.h:78
@ PROP_COLLECTION
Definition: RNA_types.h:79
@ PROPOVERRIDE_OVERRIDABLE_LIBRARY
Definition: RNA_types.h:297
@ PROP_ANIMATABLE
Definition: RNA_types.h:188
@ PROP_EDITABLE
Definition: RNA_types.h:175
@ PROP_PTR_NO_OWNERSHIP
Definition: RNA_types.h:242
@ PROP_NONE
Definition: RNA_types.h:113
@ PROP_TRANSLATION
Definition: RNA_types.h:140
#define NC_GEOM
Definition: WM_types.h:294
#define ND_DATA
Definition: WM_types.h:408
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:4875
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:2762
void RNA_def_struct_path_func(StructRNA *srna, const char *path)
Definition: rna_define.c:1212
void RNA_def_property_boolean_sdna(PropertyRNA *prop, const char *structname, const char *propname, int64_t bit)
Definition: rna_define.c:2257
void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set)
Definition: rna_define.c:3312
void RNA_def_property_float_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
Definition: rna_define.c:3153
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
Definition: rna_define.c:1676
void RNA_def_property_string_sdna(PropertyRNA *prop, const char *structname, const char *propname)
Definition: rna_define.c:2717
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:3408
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
Definition: rna_define.c:1259
void RNA_def_property_boolean_funcs(PropertyRNA *prop, const char *get, const char *set)
Definition: rna_define.c:2971
void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item)
Definition: rna_define.c:1892
void RNA_def_struct_sdna(StructRNA *srna, const char *structname)
Definition: rna_define.c:1067
void RNA_def_property_array(PropertyRNA *prop, int length)
Definition: rna_define.c:1568
void RNA_def_property_range(PropertyRNA *prop, double min, double max)
Definition: rna_define.c:1757
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
Definition: rna_define.c:1792
void RNA_def_property_update(PropertyRNA *prop, int noteflag, const char *func)
Definition: rna_define.c:2927
void RNA_def_property_editable_func(PropertyRNA *prop, const char *editable)
Definition: rna_define.c:2877
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
Definition: rna_define.c:1279
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
Definition: rna_define.c:1517
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
Definition: rna_define.c:1047
void RNA_def_property_enum_sdna(PropertyRNA *prop, const char *structname, const char *propname)
Definition: rna_define.c:2623
void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
Definition: rna_define.c:3055
void RNA_def_struct_ui_icon(StructRNA *srna, int icon)
Definition: rna_define.c:1267
void RNA_def_property_float_sdna(PropertyRNA *prop, const char *structname, const char *propname)
Definition: rna_define.c:2515
void RNA_def_property_int_sdna(PropertyRNA *prop, const char *structname, const char *propname)
Definition: rna_define.c:2364
void RNA_def_property_override_flag(PropertyRNA *prop, PropertyOverrideFlag flag)
Definition: rna_define.c:1525
void RNA_api_lattice(struct StructRNA *srna)
const EnumPropertyItem rna_enum_keyblock_type_items[]
Definition: rna_key.c:803
static void rna_def_lattice(BlenderRNA *brna)
Definition: rna_lattice.c:312
void RNA_def_lattice(BlenderRNA *brna)
Definition: rna_lattice.c:414
static void rna_def_latticepoint(BlenderRNA *brna)
Definition: rna_lattice.c:261
struct Lattice * latt
Definition: DNA_ID.h:273
struct Key * key
struct MDeformVert * dvert
struct EditLatt * editlatt
char vgroup[64]
struct BPoint * def
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
ListBase objects
Definition: BKE_main.h:148
void * data
Definition: RNA_types.h:52
struct ID * owner_id
Definition: RNA_types.h:50
void WM_main_add_notifier(unsigned int type, void *reference)
PointerRNA * ptr
Definition: wm_files.c:3157