ed71af71ac9332b868d90944ab09e4e621f8ffe3
[blender.git] / source / blender / editors / object / object_shapekey.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, shapekey support
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/object/object_shapekey.c
27  *  \ingroup edobj
28  */
29
30
31 #include <math.h>
32 #include <string.h>
33
34 #ifndef WIN32
35 #include <unistd.h>
36 #else
37 #include <io.h>
38 #endif   
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_math.h"
44 #include "BLI_utildefines.h"
45
46 #include "DNA_curve_types.h"
47 #include "DNA_key_types.h"
48 #include "DNA_lattice_types.h"
49 #include "DNA_mesh_types.h"
50 #include "DNA_meshdata_types.h"
51 #include "DNA_object_types.h"
52
53 #include "BKE_context.h"
54 #include "BKE_depsgraph.h"
55 #include "BKE_key.h"
56 #include "BKE_library.h"
57 #include "BKE_main.h"
58 #include "BKE_object.h"
59 #include "BKE_lattice.h"
60 #include "BKE_curve.h"
61
62 #include "BLI_sys_types.h" // for intptr_t support
63
64 #include "ED_object.h"
65 #include "ED_mesh.h"
66
67 #include "RNA_access.h"
68 #include "RNA_define.h"
69
70 #include "WM_api.h"
71 #include "WM_types.h"
72
73 #include "object_intern.h"
74
75 /*********************** add shape key ***********************/
76
77 static void ED_object_shape_key_add(bContext *C, Object *ob, const bool from_mix)
78 {
79         KeyBlock *kb;
80         if ((kb = BKE_object_shapekey_insert(ob, NULL, from_mix))) {
81                 Key *key = BKE_key_from_object(ob);
82                 /* for absolute shape keys, new keys may not be added last */
83                 ob->shapenr = BLI_findindex(&key->block, kb) + 1;
84
85                 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
86         }
87 }
88
89 /*********************** remove shape key ***********************/
90
91 static bool object_shapekey_remove(Main *bmain, Object *ob)
92 {
93         KeyBlock *kb;
94         Key *key = BKE_key_from_object(ob);
95
96         if (key == NULL) {
97                 return false;
98         }
99
100         kb = BLI_findlink(&key->block, ob->shapenr - 1);
101         if (kb) {
102                 return BKE_object_shapekey_remove(bmain, ob, kb);
103         }
104
105         return false;
106 }
107
108 static bool object_shape_key_mirror(bContext *C, Object *ob,
109                                     int *r_totmirr, int *r_totfail, bool use_topology)
110 {
111         KeyBlock *kb;
112         Key *key;
113         int totmirr = 0, totfail = 0;
114
115         *r_totmirr = *r_totfail = 0;
116
117         key = BKE_key_from_object(ob);
118         if (key == NULL)
119                 return 0;
120         
121         kb = BLI_findlink(&key->block, ob->shapenr - 1);
122
123         if (kb) {
124                 char *tag_elem = MEM_callocN(sizeof(char) * kb->totelem, "shape_key_mirror");
125
126
127                 if (ob->type == OB_MESH) {
128                         Mesh *me = ob->data;
129                         MVert *mv;
130                         int i1, i2;
131                         float *fp1, *fp2;
132                         float tvec[3];
133
134                         ED_mesh_mirror_spatial_table(ob, NULL, NULL, 's');
135
136                         for (i1 = 0, mv = me->mvert; i1 < me->totvert; i1++, mv++) {
137                                 i2 = mesh_get_x_mirror_vert(ob, i1, use_topology);
138                                 if (i2 == i1) {
139                                         fp1 = ((float *)kb->data) + i1 * 3;
140                                         fp1[0] = -fp1[0];
141                                         tag_elem[i1] = 1;
142                                         totmirr++;
143                                 }
144                                 else if (i2 != -1) {
145                                         if (tag_elem[i1] == 0 && tag_elem[i2] == 0) {
146                                                 fp1 = ((float *)kb->data) + i1 * 3;
147                                                 fp2 = ((float *)kb->data) + i2 * 3;
148
149                                                 copy_v3_v3(tvec,    fp1);
150                                                 copy_v3_v3(fp1, fp2);
151                                                 copy_v3_v3(fp2, tvec);
152
153                                                 /* flip x axis */
154                                                 fp1[0] = -fp1[0];
155                                                 fp2[0] = -fp2[0];
156                                                 totmirr++;
157                                         }
158                                         tag_elem[i1] = tag_elem[i2] = 1;
159                                 }
160                                 else {
161                                         totfail++;
162                                 }
163                         }
164
165                         ED_mesh_mirror_spatial_table(ob, NULL, NULL, 'e');
166                 }
167                 else if (ob->type == OB_LATTICE) {
168                         Lattice *lt = ob->data;
169                         int i1, i2;
170                         float *fp1, *fp2;
171                         int u, v, w;
172                         /* half but found up odd value */
173                         const int pntsu_half = (lt->pntsu / 2) + (lt->pntsu % 2);
174
175                         /* currently editmode isn't supported by mesh so
176                          * ignore here for now too */
177
178                         /* if (lt->editlatt) lt = lt->editlatt->latt; */
179
180                         for (w = 0; w < lt->pntsw; w++) {
181                                 for (v = 0; v < lt->pntsv; v++) {
182                                         for (u = 0; u < pntsu_half; u++) {
183                                                 int u_inv = (lt->pntsu - 1) - u;
184                                                 float tvec[3];
185                                                 if (u == u_inv) {
186                                                         i1 = BKE_lattice_index_from_uvw(lt, u, v, w);
187                                                         fp1 = ((float *)kb->data) + i1 * 3;
188                                                         fp1[0] = -fp1[0];
189                                                         totmirr++;
190                                                 }
191                                                 else {
192                                                         i1 = BKE_lattice_index_from_uvw(lt, u, v, w);
193                                                         i2 = BKE_lattice_index_from_uvw(lt, u_inv, v, w);
194
195                                                         fp1 = ((float *)kb->data) + i1 * 3;
196                                                         fp2 = ((float *)kb->data) + i2 * 3;
197
198                                                         copy_v3_v3(tvec, fp1);
199                                                         copy_v3_v3(fp1, fp2);
200                                                         copy_v3_v3(fp2, tvec);
201                                                         fp1[0] = -fp1[0];
202                                                         fp2[0] = -fp2[0];
203                                                         totmirr++;
204                                                 }
205                                         }
206                                 }
207                         }
208                 }
209
210                 MEM_freeN(tag_elem);
211         }
212         
213         *r_totmirr = totmirr;
214         *r_totfail = totfail;
215
216         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
217         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
218
219         return 1;
220 }
221
222 /********************** shape key operators *********************/
223
224 static int shape_key_mode_poll(bContext *C)
225 {
226         Object *ob = ED_object_context(C);
227         ID *data = (ob) ? ob->data : NULL;
228         return (ob && !ob->id.lib && data && !data->lib && ob->mode != OB_MODE_EDIT);
229 }
230
231 static int shape_key_mode_exists_poll(bContext *C)
232 {
233         Object *ob = ED_object_context(C);
234         ID *data = (ob) ? ob->data : NULL;
235
236         /* same as shape_key_mode_poll */
237         return (ob && !ob->id.lib && data && !data->lib && ob->mode != OB_MODE_EDIT) &&
238                /* check a keyblock exists */
239                (BKE_keyblock_from_object(ob) != NULL);
240 }
241
242 static int shape_key_move_poll(bContext *C)
243 {
244         /* Same as shape_key_mode_exists_poll above, but ensure we have at least two shapes! */
245         Object *ob = ED_object_context(C);
246         ID *data = (ob) ? ob->data : NULL;
247         Key *key = BKE_key_from_object(ob);
248
249         return (ob && !ob->id.lib && data && !data->lib && ob->mode != OB_MODE_EDIT && key && key->totkey > 1);
250 }
251
252 static int shape_key_poll(bContext *C)
253 {
254         Object *ob = ED_object_context(C);
255         ID *data = (ob) ? ob->data : NULL;
256         return (ob && !ob->id.lib && data && !data->lib);
257 }
258
259 static int shape_key_add_exec(bContext *C, wmOperator *op)
260 {
261         Object *ob = ED_object_context(C);
262         const bool from_mix = RNA_boolean_get(op->ptr, "from_mix");
263
264         ED_object_shape_key_add(C, ob, from_mix);
265
266         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
267         DAG_relations_tag_update(CTX_data_main(C));
268
269         return OPERATOR_FINISHED;
270 }
271
272 void OBJECT_OT_shape_key_add(wmOperatorType *ot)
273 {
274         /* identifiers */
275         ot->name = "Add Shape Key";
276         ot->idname = "OBJECT_OT_shape_key_add";
277         ot->description = "Add shape key to the object";
278         
279         /* api callbacks */
280         ot->poll = shape_key_mode_poll;
281         ot->exec = shape_key_add_exec;
282
283         /* flags */
284         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
285
286         /* properties */
287         RNA_def_boolean(ot->srna, "from_mix", true, "From Mix", "Create the new shape key from the existing mix of keys");
288 }
289
290 static int shape_key_remove_exec(bContext *C, wmOperator *op)
291 {
292         Main *bmain = CTX_data_main(C);
293         Object *ob = ED_object_context(C);
294         bool changed = false;
295
296         if (RNA_boolean_get(op->ptr, "all")) {
297                 changed = BKE_object_shapekey_free(bmain, ob);
298         }
299         else {
300                 changed = object_shapekey_remove(bmain, ob);
301         }
302
303         if (changed) {
304                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
305                 DAG_relations_tag_update(CTX_data_main(C));
306                 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
307
308                 return OPERATOR_FINISHED;
309         }
310         else {
311                 return OPERATOR_CANCELLED;
312         }
313 }
314
315 void OBJECT_OT_shape_key_remove(wmOperatorType *ot)
316 {
317         /* identifiers */
318         ot->name = "Remove Shape Key";
319         ot->idname = "OBJECT_OT_shape_key_remove";
320         ot->description = "Remove shape key from the object";
321         
322         /* api callbacks */
323         ot->poll = shape_key_mode_exists_poll;
324         ot->exec = shape_key_remove_exec;
325
326         /* flags */
327         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
328
329         /* properties */
330         RNA_def_boolean(ot->srna, "all", 0, "All", "Remove all shape keys");
331 }
332
333 static int shape_key_clear_exec(bContext *C, wmOperator *UNUSED(op))
334 {
335         Object *ob = ED_object_context(C);
336         Key *key = BKE_key_from_object(ob);
337         KeyBlock *kb = BKE_keyblock_from_object(ob);
338
339         if (!key || !kb)
340                 return OPERATOR_CANCELLED;
341         
342         for (kb = key->block.first; kb; kb = kb->next)
343                 kb->curval = 0.0f;
344
345         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
346         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
347         
348         return OPERATOR_FINISHED;
349 }
350
351 void OBJECT_OT_shape_key_clear(wmOperatorType *ot)
352 {
353         /* identifiers */
354         ot->name = "Clear Shape Keys";
355         ot->description = "Clear weights for all shape keys";
356         ot->idname = "OBJECT_OT_shape_key_clear";
357         
358         /* api callbacks */
359         ot->poll = shape_key_poll;
360         ot->exec = shape_key_clear_exec;
361
362         /* flags */
363         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
364 }
365
366 /* starting point and step size could be optional */
367 static int shape_key_retime_exec(bContext *C, wmOperator *UNUSED(op))
368 {
369         Object *ob = ED_object_context(C);
370         Key *key = BKE_key_from_object(ob);
371         KeyBlock *kb = BKE_keyblock_from_object(ob);
372         float cfra = 0.0f;
373
374         if (!key || !kb)
375                 return OPERATOR_CANCELLED;
376
377         for (kb = key->block.first; kb; kb = kb->next)
378                 kb->pos = (cfra += 0.1f);
379
380         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
381         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
382
383         return OPERATOR_FINISHED;
384 }
385
386 void OBJECT_OT_shape_key_retime(wmOperatorType *ot)
387 {
388         /* identifiers */
389         ot->name = "Re-Time Shape Keys";
390         ot->description = "Resets the timing for absolute shape keys";
391         ot->idname = "OBJECT_OT_shape_key_retime";
392
393         /* api callbacks */
394         ot->poll = shape_key_poll;
395         ot->exec = shape_key_retime_exec;
396
397         /* flags */
398         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
399 }
400
401 static int shape_key_mirror_exec(bContext *C, wmOperator *op)
402 {
403         Object *ob = ED_object_context(C);
404         int totmirr = 0, totfail = 0;
405         bool use_topology = RNA_boolean_get(op->ptr, "use_topology");
406
407         if (!object_shape_key_mirror(C, ob, &totmirr, &totfail, use_topology))
408                 return OPERATOR_CANCELLED;
409
410         ED_mesh_report_mirror(op, totmirr, totfail);
411
412         return OPERATOR_FINISHED;
413 }
414
415 void OBJECT_OT_shape_key_mirror(wmOperatorType *ot)
416 {
417         /* identifiers */
418         ot->name = "Mirror Shape Key";
419         ot->idname = "OBJECT_OT_shape_key_mirror";
420         ot->description = "Mirror the current shape key along the local X axis";
421
422         /* api callbacks */
423         ot->poll = shape_key_mode_poll;
424         ot->exec = shape_key_mirror_exec;
425
426         /* flags */
427         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
428
429         /* properties */
430         RNA_def_boolean(ot->srna, "use_topology", 0, "Topology Mirror",
431                         "Use topology based mirroring (for when both sides of mesh have matching, unique topology)");
432 }
433
434
435 enum {
436         KB_MOVE_TOP = -2,
437         KB_MOVE_UP = -1,
438         KB_MOVE_DOWN = 1,
439         KB_MOVE_BOTTOM = 2,
440 };
441
442 static int shape_key_move_exec(bContext *C, wmOperator *op)
443 {
444         Object *ob = ED_object_context(C);
445
446         Key *key = BKE_key_from_object(ob);
447         const int type = RNA_enum_get(op->ptr, "type");
448         const int totkey = key->totkey;
449         const int act_index = ob->shapenr - 1;
450         int new_index;
451
452         switch (type) {
453                 case KB_MOVE_TOP:
454                         /* Replace the ref key only if we're at the top already (only for relative keys) */
455                         new_index = (ELEM(act_index, 0, 1) || key->type == KEY_NORMAL) ? 0 : 1;
456                         break;
457                 case KB_MOVE_BOTTOM:
458                         new_index = totkey - 1;
459                         break;
460                 case KB_MOVE_UP:
461                 case KB_MOVE_DOWN:
462                 default:
463                         new_index = (totkey + act_index + type) % totkey;
464                         break;
465         }
466
467         if (!BKE_keyblock_move(ob, act_index, new_index)) {
468                 return OPERATOR_CANCELLED;
469         }
470
471         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
472         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
473
474         return OPERATOR_FINISHED;
475 }
476
477 void OBJECT_OT_shape_key_move(wmOperatorType *ot)
478 {
479         static EnumPropertyItem slot_move[] = {
480                 {KB_MOVE_TOP, "TOP", 0, "Top", "Top of the list"},
481                 {KB_MOVE_UP, "UP", 0, "Up", ""},
482                 {KB_MOVE_DOWN, "DOWN", 0, "Down", ""},
483                 {KB_MOVE_BOTTOM, "BOTTOM", 0, "Bottom", "Bottom of the list"},
484                 { 0, NULL, 0, NULL, NULL }
485         };
486
487         /* identifiers */
488         ot->name = "Move Shape Key";
489         ot->idname = "OBJECT_OT_shape_key_move";
490         ot->description = "Move the active shape key up/down in the list";
491
492         /* api callbacks */
493         ot->poll = shape_key_move_poll;
494         ot->exec = shape_key_move_exec;
495
496         /* flags */
497         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
498
499         RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
500 }
501