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