ba613db17406ff9e7fe2310720c4643a1c2b4956
[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_scene_types.h"
52 #include "DNA_object_types.h"
53
54 #include "BKE_context.h"
55 #include "BKE_depsgraph.h"
56 #include "BKE_key.h"
57 #include "BKE_library.h"
58 #include "BKE_main.h"
59 #include "BKE_object.h"
60 #include "BKE_curve.h"
61
62 #include "BLO_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, Scene *scene, Object *ob, int from_mix)
78 {
79         KeyBlock *kb;
80         if ((kb = object_insert_shape_key(scene, ob, NULL, from_mix))) {
81                 Key *key= ob_get_key(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 int ED_object_shape_key_remove(bContext *C, Object *ob)
92 {
93         Main *bmain= CTX_data_main(C);
94         KeyBlock *kb, *rkb;
95         Key *key;
96         //IpoCurve *icu;
97
98         key= ob_get_key(ob);
99         if (key==NULL)
100                 return 0;
101         
102         kb= BLI_findlink(&key->block, ob->shapenr-1);
103
104         if (kb) {
105                 for (rkb= key->block.first; rkb; rkb= rkb->next)
106                         if (rkb->relative == ob->shapenr-1)
107                                 rkb->relative= 0;
108
109                 BLI_remlink(&key->block, kb);
110                 key->totkey--;
111                 if (key->refkey== kb) {
112                         key->refkey= key->block.first;
113
114                         if (key->refkey) {
115                                 /* apply new basis key on original data */
116                                 switch(ob->type) {
117                                         case OB_MESH:
118                                                 key_to_mesh(key->refkey, ob->data);
119                                                 break;
120                                         case OB_CURVE:
121                                         case OB_SURF:
122                                                 key_to_curve(key->refkey, ob->data, BKE_curve_nurbs(ob->data));
123                                                 break;
124                                         case OB_LATTICE:
125                                                 key_to_latt(key->refkey, ob->data);
126                                                 break;
127                                 }
128                         }
129                 }
130                         
131                 if (kb->data) MEM_freeN(kb->data);
132                 MEM_freeN(kb);
133                 
134                 for (kb= key->block.first; kb; kb= kb->next)
135                         if (kb->adrcode>=ob->shapenr)
136                                 kb->adrcode--;
137                 
138 #if 0 // XXX old animation system
139                 if (key->ipo) {
140                         
141                         for (icu= key->ipo->curve.first; icu; icu= icu->next) {
142                                 if (icu->adrcode==ob->shapenr-1) {
143                                         BLI_remlink(&key->ipo->curve, icu);
144                                         free_ipo_curve(icu);
145                                         break;
146                                 }
147                         }
148                         for (icu= key->ipo->curve.first; icu; icu= icu->next) 
149                                 if (icu->adrcode>=ob->shapenr)
150                                         icu->adrcode--;
151                 }
152 #endif // XXX old animation system              
153                 
154                 if (ob->shapenr>1) ob->shapenr--;
155         }
156         
157         if (key->totkey==0) {
158                 if (GS(key->from->name)==ID_ME) ((Mesh *)key->from)->key= NULL;
159                 else if (GS(key->from->name)==ID_CU) ((Curve *)key->from)->key= NULL;
160                 else if (GS(key->from->name)==ID_LT) ((Lattice *)key->from)->key= NULL;
161
162                 free_libblock_us(&(bmain->key), key);
163         }
164         
165         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
166         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
167
168         return 1;
169 }
170
171 static int object_shape_key_mirror(bContext *C, Object *ob)
172 {
173         KeyBlock *kb;
174         Key *key;
175
176         key= ob_get_key(ob);
177         if (key==NULL)
178                 return 0;
179         
180         kb= BLI_findlink(&key->block, ob->shapenr-1);
181
182         if (kb) {
183                 int i1, i2;
184                 float *fp1, *fp2;
185                 float tvec[3];
186                 char *tag_elem= MEM_callocN(sizeof(char) * kb->totelem, "shape_key_mirror");
187
188
189                 if (ob->type==OB_MESH) {
190                         Mesh *me= ob->data;
191                         MVert *mv;
192
193                         mesh_octree_table(ob, NULL, NULL, 's');
194
195                         for (i1=0, mv=me->mvert; i1<me->totvert; i1++, mv++) {
196                                 i2= mesh_get_x_mirror_vert(ob, i1);
197                                 if (i2==i1) {
198                                         fp1= ((float *)kb->data) + i1*3;
199                                         fp1[0] = -fp1[0];
200                                         tag_elem[i1]= 1;
201                                 }
202                                 else if (i2 != -1) {
203                                         if (tag_elem[i1]==0 && tag_elem[i2]==0) {
204                                                 fp1= ((float *)kb->data) + i1*3;
205                                                 fp2= ((float *)kb->data) + i2*3;
206
207                                                 copy_v3_v3(tvec,        fp1);
208                                                 copy_v3_v3(fp1, fp2);
209                                                 copy_v3_v3(fp2, tvec);
210
211                                                 /* flip x axis */
212                                                 fp1[0] = -fp1[0];
213                                                 fp2[0] = -fp2[0];
214                                         }
215                                         tag_elem[i1]= tag_elem[i2]= 1;
216                                 }
217                         }
218
219                         mesh_octree_table(ob, NULL, NULL, 'e');
220                 }
221                 else if (ob->type == OB_LATTICE) {
222                         Lattice *lt= ob->data;
223                         int i1, i2;
224                         float *fp1, *fp2;
225                         int u, v, w;
226                         /* half but found up odd value */
227                         const int pntsu_half = (lt->pntsu / 2) + (lt->pntsu % 2);
228
229                         /* currently editmode isn't supported by mesh so
230                          * ignore here for now too */
231
232                         /* if (lt->editlatt) lt= lt->editlatt->latt; */
233
234                         for (w=0; w<lt->pntsw; w++) {
235                                 for (v=0; v<lt->pntsv; v++) {
236                                         for (u=0; u<pntsu_half; u++) {
237                                                 int u_inv= (lt->pntsu - 1) - u;
238                                                 float tvec[3];
239                                                 if (u == u_inv) {
240                                                         i1= LT_INDEX(lt, u, v, w);
241                                                         fp1= ((float *)kb->data) + i1*3;
242                                                         fp1[0]= -fp1[0];
243                                                 }
244                                                 else {
245                                                         i1= LT_INDEX(lt, u, v, w);
246                                                         i2= LT_INDEX(lt, u_inv, v, w);
247
248                                                         fp1= ((float *)kb->data) + i1*3;
249                                                         fp2= ((float *)kb->data) + i2*3;
250
251                                                         copy_v3_v3(tvec, fp1);
252                                                         copy_v3_v3(fp1, fp2);
253                                                         copy_v3_v3(fp2, tvec);
254                                                         fp1[0]= -fp1[0];
255                                                         fp2[0]= -fp2[0];
256                                                 }
257                                         }
258                                 }
259                         }
260                 }
261
262                 MEM_freeN(tag_elem);
263         }
264         
265         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
266         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
267
268         return 1;
269 }
270
271 /********************** shape key operators *********************/
272
273 static int shape_key_mode_poll(bContext *C)
274 {
275         Object *ob= ED_object_context(C);
276         ID *data= (ob)? ob->data: NULL;
277         return (ob && !ob->id.lib && data && !data->lib && ob->mode != OB_MODE_EDIT);
278 }
279
280 static int shape_key_poll(bContext *C)
281 {
282         Object *ob= ED_object_context(C);
283         ID *data= (ob)? ob->data: NULL;
284         return (ob && !ob->id.lib && data && !data->lib);
285 }
286
287 static int shape_key_add_exec(bContext *C, wmOperator *op)
288 {
289         Scene *scene= CTX_data_scene(C);
290         Object *ob= ED_object_context(C);
291         int from_mix = RNA_boolean_get(op->ptr, "from_mix");
292
293         ED_object_shape_key_add(C, scene, ob, from_mix);
294
295         return OPERATOR_FINISHED;
296 }
297
298 void OBJECT_OT_shape_key_add(wmOperatorType *ot)
299 {
300         /* identifiers */
301         ot->name = "Add Shape Key";
302         ot->idname = "OBJECT_OT_shape_key_add";
303         ot->description = "Add shape key to the object";
304         
305         /* api callbacks */
306         ot->poll = shape_key_mode_poll;
307         ot->exec = shape_key_add_exec;
308
309         /* flags */
310         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
311
312         /* properties */
313         RNA_def_boolean(ot->srna, "from_mix", 1, "From Mix", "Create the new shape key from the existing mix of keys");
314 }
315
316 static int shape_key_remove_exec(bContext *C, wmOperator *UNUSED(op))
317 {
318         Object *ob= ED_object_context(C);
319
320         if (!ED_object_shape_key_remove(C, ob))
321                 return OPERATOR_CANCELLED;
322         
323         return OPERATOR_FINISHED;
324 }
325
326 void OBJECT_OT_shape_key_remove(wmOperatorType *ot)
327 {
328         /* identifiers */
329         ot->name = "Remove Shape Key";
330         ot->idname = "OBJECT_OT_shape_key_remove";
331         ot->description = "Remove shape key from the object";
332         
333         /* api callbacks */
334         ot->poll = shape_key_mode_poll;
335         ot->exec = shape_key_remove_exec;
336
337         /* flags */
338         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
339 }
340
341 static int shape_key_clear_exec(bContext *C, wmOperator *UNUSED(op))
342 {
343         Object *ob= ED_object_context(C);
344         Key *key= ob_get_key(ob);
345         KeyBlock *kb= ob_get_keyblock(ob);
346
347         if (!key || !kb)
348                 return OPERATOR_CANCELLED;
349         
350         for (kb=key->block.first; kb; kb=kb->next)
351                 kb->curval= 0.0f;
352
353         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
354         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
355         
356         return OPERATOR_FINISHED;
357 }
358
359 void OBJECT_OT_shape_key_clear(wmOperatorType *ot)
360 {
361         /* identifiers */
362         ot->name = "Clear Shape Keys";
363         ot->description = "Clear weights for all shape keys";
364         ot->idname = "OBJECT_OT_shape_key_clear";
365         
366         /* api callbacks */
367         ot->poll = shape_key_poll;
368         ot->exec = shape_key_clear_exec;
369
370         /* flags */
371         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
372 }
373
374 /* starting point and step size could be optional */
375 static int shape_key_retime_exec(bContext *C, wmOperator *UNUSED(op))
376 {
377         Object *ob = ED_object_context(C);
378         Key *key = ob_get_key(ob);
379         KeyBlock *kb = ob_get_keyblock(ob);
380         float cfra = 0.0f;
381
382         if (!key || !kb)
383                 return OPERATOR_CANCELLED;
384
385         for (kb=key->block.first; kb; kb=kb->next)
386                 kb->pos = (cfra += 0.1f);
387
388         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
389         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
390
391         return OPERATOR_FINISHED;
392 }
393
394 void OBJECT_OT_shape_key_retime(wmOperatorType *ot)
395 {
396         /* identifiers */
397         ot->name = "Re-Time Shape Keys";
398         ot->description = "Resets the timing for absolute shape keys";
399         ot->idname = "OBJECT_OT_shape_key_retime";
400
401         /* api callbacks */
402         ot->poll = shape_key_poll;
403         ot->exec = shape_key_retime_exec;
404
405         /* flags */
406         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
407 }
408
409 static int shape_key_mirror_exec(bContext *C, wmOperator *UNUSED(op))
410 {
411         Object *ob= ED_object_context(C);
412
413         if (!object_shape_key_mirror(C, ob))
414                 return OPERATOR_CANCELLED;
415
416         return OPERATOR_FINISHED;
417 }
418
419 void OBJECT_OT_shape_key_mirror(wmOperatorType *ot)
420 {
421         /* identifiers */
422         ot->name = "Mirror Shape Key";
423         ot->idname = "OBJECT_OT_shape_key_mirror";
424
425         /* api callbacks */
426         ot->poll = shape_key_mode_poll;
427         ot->exec = shape_key_mirror_exec;
428
429         /* flags */
430         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
431 }
432
433
434 static int shape_key_move_exec(bContext *C, wmOperator *op)
435 {
436         Object *ob= ED_object_context(C);
437
438         int type= RNA_enum_get(op->ptr, "type");
439         Key *key= ob_get_key(ob);
440
441         if (key) {
442                 KeyBlock *kb, *kb_other;
443                 int shapenr_act= ob->shapenr-1;
444                 int shapenr_swap= shapenr_act + type;
445                 kb= BLI_findlink(&key->block, shapenr_act);
446
447                 if ((type==-1 && kb->prev==NULL) || (type==1 && kb->next==NULL)) {
448                         return OPERATOR_CANCELLED;
449                 }
450
451                 for (kb_other= key->block.first; kb_other; kb_other= kb_other->next) {
452                         if (kb_other->relative == shapenr_act) {
453                                 kb_other->relative += type;
454                         }
455                         else if (kb_other->relative == shapenr_swap) {
456                                 kb_other->relative -= type;
457                         }
458                 }
459
460                 if (type==-1) {
461                         /* move back */
462                         kb_other= kb->prev;
463                         BLI_remlink(&key->block, kb);
464                         BLI_insertlinkbefore(&key->block, kb_other, kb);
465                         ob->shapenr--;
466                 }
467                 else {
468                         /* move next */
469                         kb_other= kb->next;
470                         BLI_remlink(&key->block, kb);
471                         BLI_insertlinkafter(&key->block, kb_other, kb);
472                         ob->shapenr++;
473                 }
474
475                 SWAP(float, kb_other->pos, kb->pos) /* for absolute shape keys */
476         }
477
478         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
479         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
480
481         return OPERATOR_FINISHED;
482 }
483
484 void OBJECT_OT_shape_key_move(wmOperatorType *ot)
485 {
486         static EnumPropertyItem slot_move[] = {
487                 {-1, "UP", 0, "Up", ""},
488                 {1, "DOWN", 0, "Down", ""},
489                 {0, NULL, 0, NULL, NULL}
490         };
491
492         /* identifiers */
493         ot->name = "Move Shape Key";
494         ot->idname = "OBJECT_OT_shape_key_move";
495
496         /* api callbacks */
497         ot->poll = shape_key_mode_poll;
498         ot->exec = shape_key_move_exec;
499
500         /* flags */
501         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
502
503         RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
504 }
505