Removing unnecessary semicolon, causing compile problems with MSVC.
[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_arithb.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 KeyBlock *add_keyblock(Scene *scene, Key *key)
123 {
124         KeyBlock *kb;
125         float curpos= -0.1;
126         int tot;
127         
128         kb= key->block.last;
129         if(kb) curpos= kb->pos;
130         
131         kb= MEM_callocN(sizeof(KeyBlock), "Keyblock");
132         BLI_addtail(&key->block, kb);
133         kb->type= KEY_CARDINAL;
134         
135         tot= BLI_countlist(&key->block);
136         if(tot==1) strcpy(kb->name, "Basis");
137         else sprintf(kb->name, "Key %d", tot-1);
138         
139                 // XXX this is old anim system stuff? (i.e. the 'index' of the shapekey)
140         kb->adrcode= tot-1;
141         
142         key->totkey++;
143         if(key->totkey==1) key->refkey= kb;
144         
145         kb->slidermin= 0.0f;
146         kb->slidermax= 1.0f;
147         
148         // XXX kb->pos is the confusing old horizontal-line RVK crap in old IPO Editor...
149         if(key->type == KEY_RELATIVE) 
150                 kb->pos= curpos+0.1;
151         else {
152 #if 0 // XXX old animation system
153                 curpos= bsystem_time(scene, 0, (float)CFRA, 0.0);
154                 if(calc_ipo_spec(key->ipo, KEY_SPEED, &curpos)==0) {
155                         curpos /= 100.0;
156                 }
157                 kb->pos= curpos;
158                 
159                 sort_keys(key);
160 #endif // XXX old animation system
161         }
162         return kb;
163 }
164
165 static void insert_meshkey(Scene *scene, Object *ob)
166 {
167         Mesh *me= ob->data;
168         Key *key= me->key;
169         KeyBlock *kb;
170         int newkey= 0;
171
172         if(key == NULL) {
173                 key= me->key= add_key((ID *)me);
174                 key->type= KEY_RELATIVE;
175                 newkey= 1;
176         }
177         
178         kb= add_keyblock(scene, key);
179         
180         if(newkey) {
181                 /* create from mesh */
182                 mesh_to_key(me, kb);
183         }
184         else {
185                 /* copy from current values */
186                 kb->data= do_ob_key(scene, ob);
187                 kb->totelem= me->totvert;
188         }
189 }
190
191 /************************* Lattice ************************/
192
193 void latt_to_key(Lattice *lt, KeyBlock *kb)
194 {
195         BPoint *bp;
196         float *fp;
197         int a, tot;
198         
199         tot= lt->pntsu*lt->pntsv*lt->pntsw;
200         if(tot==0) return;
201         
202         if(kb->data) MEM_freeN(kb->data);
203         
204         kb->data= MEM_callocN(lt->key->elemsize*tot, "kb->data");
205         kb->totelem= tot;
206         
207         bp= lt->def;
208         fp= kb->data;
209         for(a=0; a<kb->totelem; a++, fp+=3, bp++) {
210                 VECCOPY(fp, bp->vec);
211         }
212 }
213
214 void key_to_latt(KeyBlock *kb, Lattice *lt)
215 {
216         BPoint *bp;
217         float *fp;
218         int a, tot;
219         
220         bp= lt->def;
221         fp= kb->data;
222         
223         tot= lt->pntsu*lt->pntsv*lt->pntsw;
224         tot= MIN2(kb->totelem, tot);
225         
226         for(a=0; a<tot; a++, fp+=3, bp++) {
227                 VECCOPY(bp->vec, fp);
228         }
229 }
230
231 static void insert_lattkey(Scene *scene, Object *ob)
232 {
233         Lattice *lt= ob->data;
234         Key *key= lt->key;
235         KeyBlock *kb;
236         int newkey= 0;
237         
238         if(key==NULL) {
239                 key= lt->key= add_key( (ID *)lt);
240                 key->type= KEY_RELATIVE;
241         }
242
243         kb= add_keyblock(scene, key);
244         
245         if(newkey) {
246                 /* create from lattice */
247                 latt_to_key(lt, kb);
248         }
249         else {
250                 /* copy from current values */
251                 kb->totelem= lt->pntsu*lt->pntsv*lt->pntsw;
252                 kb->data= do_ob_key(scene, ob);
253         }
254 }
255
256 /************************* Curve ************************/
257
258 void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb)
259 {
260         Nurb *nu;
261         BezTriple *bezt;
262         BPoint *bp;
263         float *fp;
264         int a, tot;
265         
266         /* count */
267         tot= count_curveverts(nurb);
268         if(tot==0) return;
269         
270         if(kb->data) MEM_freeN(kb->data);
271         
272         kb->data= MEM_callocN(cu->key->elemsize*tot, "kb->data");
273         kb->totelem= tot;
274         
275         nu= nurb->first;
276         fp= kb->data;
277         while(nu) {
278                 
279                 if(nu->bezt) {
280                         bezt= nu->bezt;
281                         a= nu->pntsu;
282                         while(a--) {
283                                 VECCOPY(fp, bezt->vec[0]);
284                                 fp+= 3;
285                                 VECCOPY(fp, bezt->vec[1]);
286                                 fp+= 3;
287                                 VECCOPY(fp, bezt->vec[2]);
288                                 fp+= 3;
289                                 fp[0]= bezt->alfa;
290                                 fp+= 3; /* alphas */
291                                 bezt++;
292                         }
293                 }
294                 else {
295                         bp= nu->bp;
296                         a= nu->pntsu*nu->pntsv;
297                         while(a--) {
298                                 VECCOPY(fp, bp->vec);
299                                 fp[3]= bp->alfa;
300                                 
301                                 fp+= 4;
302                                 bp++;
303                         }
304                 }
305                 nu= nu->next;
306         }
307 }
308
309 void key_to_curve(KeyBlock *kb, Curve  *cu, ListBase *nurb)
310 {
311         Nurb *nu;
312         BezTriple *bezt;
313         BPoint *bp;
314         float *fp;
315         int a, tot;
316         
317         nu= nurb->first;
318         fp= kb->data;
319         
320         tot= count_curveverts(nurb);
321
322         tot= MIN2(kb->totelem, tot);
323         
324         while(nu && tot>0) {
325                 
326                 if(nu->bezt) {
327                         bezt= nu->bezt;
328                         a= nu->pntsu;
329                         while(a-- && tot>0) {
330                                 VECCOPY(bezt->vec[0], fp);
331                                 fp+= 3;
332                                 VECCOPY(bezt->vec[1], fp);
333                                 fp+= 3;
334                                 VECCOPY(bezt->vec[2], fp);
335                                 fp+= 3;
336                                 bezt->alfa= fp[0];
337                                 fp+= 3; /* alphas */
338                         
339                                 tot-= 3;
340                                 bezt++;
341                         }
342                 }
343                 else {
344                         bp= nu->bp;
345                         a= nu->pntsu*nu->pntsv;
346                         while(a-- && tot>0) {
347                                 VECCOPY(bp->vec, fp);
348                                 bp->alfa= fp[3];
349                                 
350                                 fp+= 4;
351                                 tot--;
352                                 bp++;
353                         }
354                 }
355                 nu= nu->next;
356         }
357 }
358
359
360 static void insert_curvekey(Scene *scene, Object *ob)
361 {
362         Curve *cu= ob->data;
363         Key *key= cu->key;
364         KeyBlock *kb;
365         ListBase *lb= (cu->editnurb)? cu->editnurb: &cu->nurb;
366         int newkey= 0;
367         
368         if(key==NULL) {
369                 key= cu->key= add_key( (ID *)cu);
370                 key->type = KEY_RELATIVE;
371                 newkey= 1;
372         }
373         
374         kb= add_keyblock(scene, key);
375         
376         if(newkey) {
377                 /* create from curve */
378                 curve_to_key(cu, kb, lb);
379         }
380         else {
381                 /* copy from current values */
382                 kb->totelem= count_curveverts(lb);
383                 kb->data= do_ob_key(scene, ob);
384         }
385
386 }
387
388 /*********************** add shape key ***********************/
389
390 static void ED_object_shape_key_add(bContext *C, Scene *scene, Object *ob)
391 {
392         Key *key;
393
394         if(ob->type==OB_MESH) insert_meshkey(scene, ob);
395         else if ELEM(ob->type, OB_CURVE, OB_SURF) insert_curvekey(scene, ob);
396         else if(ob->type==OB_LATTICE) insert_lattkey(scene, ob);
397
398         key= ob_get_key(ob);
399         ob->shapenr= BLI_countlist(&key->block);
400
401         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
402 }
403
404 /*********************** remove shape key ***********************/
405
406 static int ED_object_shape_key_remove(bContext *C, Object *ob)
407 {
408         Main *bmain= CTX_data_main(C);
409         KeyBlock *kb, *rkb;
410         Key *key;
411         //IpoCurve *icu;
412
413         key= ob_get_key(ob);
414         if(key==NULL)
415                 return 0;
416         
417         kb= BLI_findlink(&key->block, ob->shapenr-1);
418
419         if(kb) {
420                 for(rkb= key->block.first; rkb; rkb= rkb->next)
421                         if(rkb->relative == ob->shapenr-1)
422                                 rkb->relative= 0;
423
424                 BLI_remlink(&key->block, kb);
425                 key->totkey--;
426                 if(key->refkey== kb)
427                         key->refkey= key->block.first;
428                         
429                 if(kb->data) MEM_freeN(kb->data);
430                 MEM_freeN(kb);
431                 
432                 for(kb= key->block.first; kb; kb= kb->next)
433                         if(kb->adrcode>=ob->shapenr)
434                                 kb->adrcode--;
435                 
436 #if 0 // XXX old animation system
437                 if(key->ipo) {
438                         
439                         for(icu= key->ipo->curve.first; icu; icu= icu->next) {
440                                 if(icu->adrcode==ob->shapenr-1) {
441                                         BLI_remlink(&key->ipo->curve, icu);
442                                         free_ipo_curve(icu);
443                                         break;
444                                 }
445                         }
446                         for(icu= key->ipo->curve.first; icu; icu= icu->next) 
447                                 if(icu->adrcode>=ob->shapenr)
448                                         icu->adrcode--;
449                 }
450 #endif // XXX old animation system              
451                 
452                 if(ob->shapenr>1) ob->shapenr--;
453         }
454         
455         if(key->totkey==0) {
456                 if(GS(key->from->name)==ID_ME) ((Mesh *)key->from)->key= NULL;
457                 else if(GS(key->from->name)==ID_CU) ((Curve *)key->from)->key= NULL;
458                 else if(GS(key->from->name)==ID_LT) ((Lattice *)key->from)->key= NULL;
459
460                 free_libblock_us(&(bmain->key), key);
461         }
462         
463         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
464         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
465
466         return 1;
467 }
468
469 static int ED_object_shape_key_mirror(bContext *C, Scene *scene, Object *ob)
470 {
471         KeyBlock *kb;
472         Key *key;
473
474         key= ob_get_key(ob);
475         if(key==NULL)
476                 return 0;
477         
478         kb= BLI_findlink(&key->block, ob->shapenr-1);
479
480         if(kb) {
481                 int i1, i2;
482                 float *fp1, *fp2;
483                 float tvec[3];
484                 char *tag_elem= MEM_callocN(sizeof(char) * kb->totelem, "shape_key_mirror");
485
486
487                 if(ob->type==OB_MESH) {
488                         Mesh *me= ob->data;
489                         MVert *mv;
490
491                         mesh_octree_table(ob, NULL, NULL, 's');
492
493                         for(i1=0, mv=me->mvert; i1<me->totvert; i1++, mv++) {
494                                 i2= mesh_get_x_mirror_vert(ob, i1);
495                                 if(i2==i1) {
496                                         fp1= ((float *)kb->data) + i1*3;
497                                         fp1[0] = -fp1[0];
498                                         tag_elem[i1]= 1;
499                                 }
500                                 else if(i2 != -1) {
501                                         if(tag_elem[i1]==0 && tag_elem[i2]==0) {
502                                                 fp1= ((float *)kb->data) + i1*3;
503                                                 fp2= ((float *)kb->data) + i2*3;
504
505                                                 VECCOPY(tvec,   fp1);
506                                                 VECCOPY(fp1,    fp2);
507                                                 VECCOPY(fp2,    tvec);
508
509                                                 /* flip x axis */
510                                                 fp1[0] = -fp1[0];
511                                                 fp2[0] = -fp2[0];
512                                         }
513                                         tag_elem[i1]= tag_elem[i2]= 1;
514                                 }
515                         }
516
517                         mesh_octree_table(ob, NULL, NULL, 'e');
518                 }
519                 /* todo, other types? */
520
521                 MEM_freeN(tag_elem);
522         }
523         
524         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
525         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
526
527         return 1;
528 }
529
530 /********************** shape key operators *********************/
531
532 static int shape_key_mode_poll(bContext *C)
533 {
534         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
535         ID *data= (ob)? ob->data: NULL;
536         return (ob && !ob->id.lib && data && !data->lib && ob->mode != OB_MODE_EDIT);
537 }
538
539 static int shape_key_poll(bContext *C)
540 {
541         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
542         ID *data= (ob)? ob->data: NULL;
543         return (ob && !ob->id.lib && data && !data->lib);
544 }
545
546 static int shape_key_add_exec(bContext *C, wmOperator *op)
547 {
548         Scene *scene= CTX_data_scene(C);
549         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
550
551         ED_object_shape_key_add(C, scene, ob);
552         
553         return OPERATOR_FINISHED;
554 }
555
556 void OBJECT_OT_shape_key_add(wmOperatorType *ot)
557 {
558         /* identifiers */
559         ot->name= "Add Shape Key";
560         ot->name= "Add shape key to the object.";
561         ot->idname= "OBJECT_OT_shape_key_add";
562         
563         /* api callbacks */
564         ot->poll= shape_key_mode_poll;
565         ot->exec= shape_key_add_exec;
566
567         /* flags */
568         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
569 }
570
571 static int shape_key_remove_exec(bContext *C, wmOperator *op)
572 {
573         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
574
575         if(!ED_object_shape_key_remove(C, ob))
576                 return OPERATOR_CANCELLED;
577         
578         return OPERATOR_FINISHED;
579 }
580
581 void OBJECT_OT_shape_key_remove(wmOperatorType *ot)
582 {
583         /* identifiers */
584         ot->name= "Remove Shape Key";
585         ot->name= "Remove shape key from the object.";
586         ot->idname= "OBJECT_OT_shape_key_remove";
587         
588         /* api callbacks */
589         ot->poll= shape_key_mode_poll;
590         ot->exec= shape_key_remove_exec;
591
592         /* flags */
593         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
594 }
595
596 static int shape_key_clear_exec(bContext *C, wmOperator *op)
597 {
598         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
599         Key *key= ob_get_key(ob);
600         KeyBlock *kb= ob_get_keyblock(ob);
601
602         if(!key || !kb)
603                 return OPERATOR_CANCELLED;
604         
605         for(kb=key->block.first; kb; kb=kb->next)
606                 kb->curval= 0.0f;
607
608         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
609         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
610         
611         return OPERATOR_FINISHED;
612 }
613
614 void OBJECT_OT_shape_key_clear(wmOperatorType *ot)
615 {
616         /* identifiers */
617         ot->name= "Clear Shape Keys";
618         ot->description= "Clear weights for all shape keys.";
619         ot->idname= "OBJECT_OT_shape_key_clear";
620         
621         /* api callbacks */
622         ot->poll= shape_key_poll;
623         ot->exec= shape_key_clear_exec;
624
625         /* flags */
626         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
627 }
628
629 static int shape_key_mirror_exec(bContext *C, wmOperator *op)
630 {
631         Scene *scene= CTX_data_scene(C);
632         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
633
634         if(!ED_object_shape_key_mirror(C, scene, ob))
635                 return OPERATOR_CANCELLED;
636
637         return OPERATOR_FINISHED;
638 }
639
640 void OBJECT_OT_shape_key_mirror(wmOperatorType *ot)
641 {
642         /* identifiers */
643         ot->name= "Mirror Shape Key";
644         ot->idname= "OBJECT_OT_shape_key_mirror";
645
646         /* api callbacks */
647         ot->poll= shape_key_mode_poll;
648         ot->exec= shape_key_mirror_exec;
649
650         /* flags */
651         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
652 }
653
654
655 static int shape_key_move_exec(bContext *C, wmOperator *op)
656 {
657         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
658
659         int type= RNA_enum_get(op->ptr, "type");
660         Key *key= ob_get_key(ob);
661
662         if(key) {
663                 KeyBlock *kb, *kb_other;
664                 kb= BLI_findlink(&key->block, ob->shapenr-1);
665
666                 if(type==-1) {
667                         /* move back */
668                         if(kb->prev) {
669                                 kb_other= kb->prev;
670                                 BLI_remlink(&key->block, kb);
671                                 BLI_insertlinkbefore(&key->block, kb_other, kb);
672                                 ob->shapenr--;
673                         }
674                 }
675                 else {
676                         /* move next */
677                         if(kb->next) {
678                                 kb_other= kb->next;
679                                 BLI_remlink(&key->block, kb);
680                                 BLI_insertlinkafter(&key->block, kb_other, kb);
681                                 ob->shapenr++;
682                         }
683                 }
684         }
685
686         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
687         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
688
689         return OPERATOR_FINISHED;
690 }
691
692 void OBJECT_OT_shape_key_move(wmOperatorType *ot)
693 {
694         static EnumPropertyItem slot_move[] = {
695                 {-1, "UP", 0, "Up", ""},
696                 {1, "DOWN", 0, "Down", ""},
697                 {0, NULL, 0, NULL, NULL}
698         };
699
700         /* identifiers */
701         ot->name= "Move Shape Key";
702         ot->idname= "OBJECT_OT_shape_key_move";
703
704         /* api callbacks */
705         ot->poll= shape_key_mode_poll;
706         ot->exec= shape_key_move_exec;
707
708         /* flags */
709         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
710
711         RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
712 }
713