* New option on modifiers that don't change topology: Apply as Shape
[blender.git] / source / blender / editors / object / object_shapekey.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, shapekey support
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <math.h>
29 #include <string.h>
30
31 #ifndef WIN32
32 #include <unistd.h>
33 #else
34 #include <io.h>
35 #endif   
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_math.h"
41
42 #include "DNA_action_types.h"
43 #include "DNA_curve_types.h"
44 #include "DNA_ipo_types.h"
45 #include "DNA_key_types.h"
46 #include "DNA_lattice_types.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_meshdata_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_userdef_types.h"
54 #include "DNA_view2d_types.h"
55
56 #include "BKE_action.h"
57 #include "BKE_anim.h"
58 #include "BKE_context.h"
59 #include "BKE_curve.h"
60 #include "BKE_depsgraph.h"
61 #include "BKE_global.h"
62 #include "BKE_ipo.h"
63 #include "BKE_key.h"
64 #include "BKE_library.h"
65 #include "BKE_main.h"
66 #include "BKE_mesh.h"
67 #include "BKE_object.h"
68 #include "BKE_utildefines.h"
69
70 #include "BLO_sys_types.h" // for intptr_t support
71
72 #include "ED_object.h"
73 #include "ED_mesh.h"
74
75 #include "RNA_access.h"
76 #include "RNA_define.h"
77
78 #include "WM_api.h"
79 #include "WM_types.h"
80
81 #include "object_intern.h"
82
83 /************************* Mesh ************************/
84
85 void mesh_to_key(Mesh *me, KeyBlock *kb)
86 {
87         MVert *mvert;
88         float *fp;
89         int a;
90         
91         if(me->totvert==0) return;
92         
93         if(kb->data) MEM_freeN(kb->data);
94         
95         kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data");
96         kb->totelem= me->totvert;
97         
98         mvert= me->mvert;
99         fp= kb->data;
100         for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
101                 VECCOPY(fp, mvert->co);
102                 
103         }
104 }
105
106 void key_to_mesh(KeyBlock *kb, Mesh *me)
107 {
108         MVert *mvert;
109         float *fp;
110         int a, tot;
111         
112         mvert= me->mvert;
113         fp= kb->data;
114         
115         tot= MIN2(kb->totelem, me->totvert);
116         
117         for(a=0; a<tot; a++, fp+=3, mvert++) {
118                 VECCOPY(mvert->co, fp);
119         }
120 }
121
122 static void insert_meshkey(Scene *scene, Object *ob)
123 {
124         Mesh *me= ob->data;
125         Key *key= me->key;
126         KeyBlock *kb;
127         int newkey= 0;
128
129         if(key == NULL) {
130                 key= me->key= add_key((ID *)me);
131                 key->type= KEY_RELATIVE;
132                 newkey= 1;
133         }
134         
135         kb= add_keyblock(scene, key);
136         
137         if(newkey) {
138                 /* create from mesh */
139                 mesh_to_key(me, kb);
140         }
141         else {
142                 /* copy from current values */
143                 kb->data= do_ob_key(scene, ob);
144                 kb->totelem= me->totvert;
145         }
146 }
147
148 /************************* Lattice ************************/
149
150 void latt_to_key(Lattice *lt, KeyBlock *kb)
151 {
152         BPoint *bp;
153         float *fp;
154         int a, tot;
155         
156         tot= lt->pntsu*lt->pntsv*lt->pntsw;
157         if(tot==0) return;
158         
159         if(kb->data) MEM_freeN(kb->data);
160         
161         kb->data= MEM_callocN(lt->key->elemsize*tot, "kb->data");
162         kb->totelem= tot;
163         
164         bp= lt->def;
165         fp= kb->data;
166         for(a=0; a<kb->totelem; a++, fp+=3, bp++) {
167                 VECCOPY(fp, bp->vec);
168         }
169 }
170
171 void key_to_latt(KeyBlock *kb, Lattice *lt)
172 {
173         BPoint *bp;
174         float *fp;
175         int a, tot;
176         
177         bp= lt->def;
178         fp= kb->data;
179         
180         tot= lt->pntsu*lt->pntsv*lt->pntsw;
181         tot= MIN2(kb->totelem, tot);
182         
183         for(a=0; a<tot; a++, fp+=3, bp++) {
184                 VECCOPY(bp->vec, fp);
185         }
186 }
187
188 static void insert_lattkey(Scene *scene, Object *ob)
189 {
190         Lattice *lt= ob->data;
191         Key *key= lt->key;
192         KeyBlock *kb;
193         int newkey= 0;
194         
195         if(key==NULL) {
196                 key= lt->key= add_key( (ID *)lt);
197                 key->type= KEY_RELATIVE;
198                 newkey= 1;
199         }
200
201         kb= add_keyblock(scene, key);
202         
203         if(newkey) {
204                 /* create from lattice */
205                 latt_to_key(lt, kb);
206         }
207         else {
208                 /* copy from current values */
209                 kb->totelem= lt->pntsu*lt->pntsv*lt->pntsw;
210                 kb->data= do_ob_key(scene, ob);
211         }
212 }
213
214 /************************* Curve ************************/
215
216 void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb)
217 {
218         Nurb *nu;
219         BezTriple *bezt;
220         BPoint *bp;
221         float *fp;
222         int a, tot;
223         
224         /* count */
225         tot= count_curveverts(nurb);
226         if(tot==0) return;
227         
228         if(kb->data) MEM_freeN(kb->data);
229         
230         kb->data= MEM_callocN(cu->key->elemsize*tot, "kb->data");
231         kb->totelem= tot;
232         
233         nu= nurb->first;
234         fp= kb->data;
235         while(nu) {
236                 
237                 if(nu->bezt) {
238                         bezt= nu->bezt;
239                         a= nu->pntsu;
240                         while(a--) {
241                                 VECCOPY(fp, bezt->vec[0]);
242                                 fp+= 3;
243                                 VECCOPY(fp, bezt->vec[1]);
244                                 fp+= 3;
245                                 VECCOPY(fp, bezt->vec[2]);
246                                 fp+= 3;
247                                 fp[0]= bezt->alfa;
248                                 fp+= 3; /* alphas */
249                                 bezt++;
250                         }
251                 }
252                 else {
253                         bp= nu->bp;
254                         a= nu->pntsu*nu->pntsv;
255                         while(a--) {
256                                 VECCOPY(fp, bp->vec);
257                                 fp[3]= bp->alfa;
258                                 
259                                 fp+= 4;
260                                 bp++;
261                         }
262                 }
263                 nu= nu->next;
264         }
265 }
266
267 void key_to_curve(KeyBlock *kb, Curve  *cu, ListBase *nurb)
268 {
269         Nurb *nu;
270         BezTriple *bezt;
271         BPoint *bp;
272         float *fp;
273         int a, tot;
274         
275         nu= nurb->first;
276         fp= kb->data;
277         
278         tot= count_curveverts(nurb);
279
280         tot= MIN2(kb->totelem, tot);
281         
282         while(nu && tot>0) {
283                 
284                 if(nu->bezt) {
285                         bezt= nu->bezt;
286                         a= nu->pntsu;
287                         while(a-- && tot>0) {
288                                 VECCOPY(bezt->vec[0], fp);
289                                 fp+= 3;
290                                 VECCOPY(bezt->vec[1], fp);
291                                 fp+= 3;
292                                 VECCOPY(bezt->vec[2], fp);
293                                 fp+= 3;
294                                 bezt->alfa= fp[0];
295                                 fp+= 3; /* alphas */
296                         
297                                 tot-= 3;
298                                 bezt++;
299                         }
300                 }
301                 else {
302                         bp= nu->bp;
303                         a= nu->pntsu*nu->pntsv;
304                         while(a-- && tot>0) {
305                                 VECCOPY(bp->vec, fp);
306                                 bp->alfa= fp[3];
307                                 
308                                 fp+= 4;
309                                 tot--;
310                                 bp++;
311                         }
312                 }
313                 nu= nu->next;
314         }
315 }
316
317
318 static void insert_curvekey(Scene *scene, Object *ob)
319 {
320         Curve *cu= ob->data;
321         Key *key= cu->key;
322         KeyBlock *kb;
323         ListBase *lb= (cu->editnurb)? cu->editnurb: &cu->nurb;
324         int newkey= 0;
325         
326         if(key==NULL) {
327                 key= cu->key= add_key( (ID *)cu);
328                 key->type = KEY_RELATIVE;
329                 newkey= 1;
330         }
331         
332         kb= add_keyblock(scene, key);
333         
334         if(newkey) {
335                 /* create from curve */
336                 curve_to_key(cu, kb, lb);
337         }
338         else {
339                 /* copy from current values */
340                 kb->totelem= count_curveverts(lb);
341                 kb->data= do_ob_key(scene, ob);
342         }
343
344 }
345
346 /*********************** add shape key ***********************/
347
348 static void ED_object_shape_key_add(bContext *C, Scene *scene, Object *ob)
349 {
350         Key *key;
351
352         if(ob->type==OB_MESH) insert_meshkey(scene, ob);
353         else if ELEM(ob->type, OB_CURVE, OB_SURF) insert_curvekey(scene, ob);
354         else if(ob->type==OB_LATTICE) insert_lattkey(scene, ob);
355
356         key= ob_get_key(ob);
357         ob->shapenr= BLI_countlist(&key->block);
358
359         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
360 }
361
362 /*********************** remove shape key ***********************/
363
364 static int ED_object_shape_key_remove(bContext *C, Object *ob)
365 {
366         Main *bmain= CTX_data_main(C);
367         KeyBlock *kb, *rkb;
368         Key *key;
369         //IpoCurve *icu;
370
371         key= ob_get_key(ob);
372         if(key==NULL)
373                 return 0;
374         
375         kb= BLI_findlink(&key->block, ob->shapenr-1);
376
377         if(kb) {
378                 for(rkb= key->block.first; rkb; rkb= rkb->next)
379                         if(rkb->relative == ob->shapenr-1)
380                                 rkb->relative= 0;
381
382                 BLI_remlink(&key->block, kb);
383                 key->totkey--;
384                 if(key->refkey== kb)
385                         key->refkey= key->block.first;
386                         
387                 if(kb->data) MEM_freeN(kb->data);
388                 MEM_freeN(kb);
389                 
390                 for(kb= key->block.first; kb; kb= kb->next)
391                         if(kb->adrcode>=ob->shapenr)
392                                 kb->adrcode--;
393                 
394 #if 0 // XXX old animation system
395                 if(key->ipo) {
396                         
397                         for(icu= key->ipo->curve.first; icu; icu= icu->next) {
398                                 if(icu->adrcode==ob->shapenr-1) {
399                                         BLI_remlink(&key->ipo->curve, icu);
400                                         free_ipo_curve(icu);
401                                         break;
402                                 }
403                         }
404                         for(icu= key->ipo->curve.first; icu; icu= icu->next) 
405                                 if(icu->adrcode>=ob->shapenr)
406                                         icu->adrcode--;
407                 }
408 #endif // XXX old animation system              
409                 
410                 if(ob->shapenr>1) ob->shapenr--;
411         }
412         
413         if(key->totkey==0) {
414                 if(GS(key->from->name)==ID_ME) ((Mesh *)key->from)->key= NULL;
415                 else if(GS(key->from->name)==ID_CU) ((Curve *)key->from)->key= NULL;
416                 else if(GS(key->from->name)==ID_LT) ((Lattice *)key->from)->key= NULL;
417
418                 free_libblock_us(&(bmain->key), key);
419         }
420         
421         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
422         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
423
424         return 1;
425 }
426
427 static int ED_object_shape_key_mirror(bContext *C, Scene *scene, Object *ob)
428 {
429         KeyBlock *kb;
430         Key *key;
431
432         key= ob_get_key(ob);
433         if(key==NULL)
434                 return 0;
435         
436         kb= BLI_findlink(&key->block, ob->shapenr-1);
437
438         if(kb) {
439                 int i1, i2;
440                 float *fp1, *fp2;
441                 float tvec[3];
442                 char *tag_elem= MEM_callocN(sizeof(char) * kb->totelem, "shape_key_mirror");
443
444
445                 if(ob->type==OB_MESH) {
446                         Mesh *me= ob->data;
447                         MVert *mv;
448
449                         mesh_octree_table(ob, NULL, NULL, 's');
450
451                         for(i1=0, mv=me->mvert; i1<me->totvert; i1++, mv++) {
452                                 i2= mesh_get_x_mirror_vert(ob, i1);
453                                 if(i2==i1) {
454                                         fp1= ((float *)kb->data) + i1*3;
455                                         fp1[0] = -fp1[0];
456                                         tag_elem[i1]= 1;
457                                 }
458                                 else if(i2 != -1) {
459                                         if(tag_elem[i1]==0 && tag_elem[i2]==0) {
460                                                 fp1= ((float *)kb->data) + i1*3;
461                                                 fp2= ((float *)kb->data) + i2*3;
462
463                                                 VECCOPY(tvec,   fp1);
464                                                 VECCOPY(fp1,    fp2);
465                                                 VECCOPY(fp2,    tvec);
466
467                                                 /* flip x axis */
468                                                 fp1[0] = -fp1[0];
469                                                 fp2[0] = -fp2[0];
470                                         }
471                                         tag_elem[i1]= tag_elem[i2]= 1;
472                                 }
473                         }
474
475                         mesh_octree_table(ob, NULL, NULL, 'e');
476                 }
477                 /* todo, other types? */
478
479                 MEM_freeN(tag_elem);
480         }
481         
482         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
483         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
484
485         return 1;
486 }
487
488 /********************** shape key operators *********************/
489
490 static int shape_key_mode_poll(bContext *C)
491 {
492         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
493         ID *data= (ob)? ob->data: NULL;
494         return (ob && !ob->id.lib && data && !data->lib && ob->mode != OB_MODE_EDIT);
495 }
496
497 static int shape_key_poll(bContext *C)
498 {
499         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
500         ID *data= (ob)? ob->data: NULL;
501         return (ob && !ob->id.lib && data && !data->lib);
502 }
503
504 static int shape_key_add_exec(bContext *C, wmOperator *op)
505 {
506         Scene *scene= CTX_data_scene(C);
507         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
508
509         ED_object_shape_key_add(C, scene, ob);
510         
511         return OPERATOR_FINISHED;
512 }
513
514 void OBJECT_OT_shape_key_add(wmOperatorType *ot)
515 {
516         /* identifiers */
517         ot->name= "Add Shape Key";
518         ot->name= "Add shape key to the object.";
519         ot->idname= "OBJECT_OT_shape_key_add";
520         
521         /* api callbacks */
522         ot->poll= shape_key_mode_poll;
523         ot->exec= shape_key_add_exec;
524
525         /* flags */
526         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
527 }
528
529 static int shape_key_remove_exec(bContext *C, wmOperator *op)
530 {
531         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
532
533         if(!ED_object_shape_key_remove(C, ob))
534                 return OPERATOR_CANCELLED;
535         
536         return OPERATOR_FINISHED;
537 }
538
539 void OBJECT_OT_shape_key_remove(wmOperatorType *ot)
540 {
541         /* identifiers */
542         ot->name= "Remove Shape Key";
543         ot->name= "Remove shape key from the object.";
544         ot->idname= "OBJECT_OT_shape_key_remove";
545         
546         /* api callbacks */
547         ot->poll= shape_key_mode_poll;
548         ot->exec= shape_key_remove_exec;
549
550         /* flags */
551         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
552 }
553
554 static int shape_key_clear_exec(bContext *C, wmOperator *op)
555 {
556         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
557         Key *key= ob_get_key(ob);
558         KeyBlock *kb= ob_get_keyblock(ob);
559
560         if(!key || !kb)
561                 return OPERATOR_CANCELLED;
562         
563         for(kb=key->block.first; kb; kb=kb->next)
564                 kb->curval= 0.0f;
565
566         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
567         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
568         
569         return OPERATOR_FINISHED;
570 }
571
572 void OBJECT_OT_shape_key_clear(wmOperatorType *ot)
573 {
574         /* identifiers */
575         ot->name= "Clear Shape Keys";
576         ot->description= "Clear weights for all shape keys.";
577         ot->idname= "OBJECT_OT_shape_key_clear";
578         
579         /* api callbacks */
580         ot->poll= shape_key_poll;
581         ot->exec= shape_key_clear_exec;
582
583         /* flags */
584         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
585 }
586
587 static int shape_key_mirror_exec(bContext *C, wmOperator *op)
588 {
589         Scene *scene= CTX_data_scene(C);
590         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
591
592         if(!ED_object_shape_key_mirror(C, scene, ob))
593                 return OPERATOR_CANCELLED;
594
595         return OPERATOR_FINISHED;
596 }
597
598 void OBJECT_OT_shape_key_mirror(wmOperatorType *ot)
599 {
600         /* identifiers */
601         ot->name= "Mirror Shape Key";
602         ot->idname= "OBJECT_OT_shape_key_mirror";
603
604         /* api callbacks */
605         ot->poll= shape_key_mode_poll;
606         ot->exec= shape_key_mirror_exec;
607
608         /* flags */
609         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
610 }
611
612
613 static int shape_key_move_exec(bContext *C, wmOperator *op)
614 {
615         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
616
617         int type= RNA_enum_get(op->ptr, "type");
618         Key *key= ob_get_key(ob);
619
620         if(key) {
621                 KeyBlock *kb, *kb_other;
622                 kb= BLI_findlink(&key->block, ob->shapenr-1);
623
624                 if(type==-1) {
625                         /* move back */
626                         if(kb->prev) {
627                                 kb_other= kb->prev;
628                                 BLI_remlink(&key->block, kb);
629                                 BLI_insertlinkbefore(&key->block, kb_other, kb);
630                                 ob->shapenr--;
631                         }
632                 }
633                 else {
634                         /* move next */
635                         if(kb->next) {
636                                 kb_other= kb->next;
637                                 BLI_remlink(&key->block, kb);
638                                 BLI_insertlinkafter(&key->block, kb_other, kb);
639                                 ob->shapenr++;
640                         }
641                 }
642         }
643
644         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
645         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
646
647         return OPERATOR_FINISHED;
648 }
649
650 void OBJECT_OT_shape_key_move(wmOperatorType *ot)
651 {
652         static EnumPropertyItem slot_move[] = {
653                 {-1, "UP", 0, "Up", ""},
654                 {1, "DOWN", 0, "Down", ""},
655                 {0, NULL, 0, NULL, NULL}
656         };
657
658         /* identifiers */
659         ot->name= "Move Shape Key";
660         ot->idname= "OBJECT_OT_shape_key_move";
661
662         /* api callbacks */
663         ot->poll= shape_key_mode_poll;
664         ot->exec= shape_key_move_exec;
665
666         /* flags */
667         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
668
669         RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
670 }
671