Added a button to clear the weights of all shape keys, useful
[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
74 #include "RNA_access.h"
75 #include "RNA_define.h"
76
77 #include "WM_api.h"
78 #include "WM_types.h"
79
80 #include "object_intern.h"
81
82 #if 0 // XXX old animation system
83 static void default_key_ipo(Scene *scene, Key *key)
84 {
85         IpoCurve *icu;
86         BezTriple *bezt;
87         
88         key->ipo= add_ipo(scene, "KeyIpo", ID_KE);
89         
90         icu= MEM_callocN(sizeof(IpoCurve), "ipocurve");
91                         
92         icu->blocktype= ID_KE;
93         icu->adrcode= KEY_SPEED;
94         icu->flag= IPO_VISIBLE|IPO_SELECT|IPO_AUTO_HORIZ;
95         set_icu_vars(icu);
96         
97         BLI_addtail( &(key->ipo->curve), icu);
98         
99         icu->bezt= bezt= MEM_callocN(2*sizeof(BezTriple), "defaultipo");
100         icu->totvert= 2;
101         
102         bezt->hide= IPO_BEZ;
103         bezt->f1=bezt->f2= bezt->f3= SELECT;
104         bezt->h1= bezt->h2= HD_AUTO;
105         bezt++;
106         bezt->vec[1][0]= 100.0;
107         bezt->vec[1][1]= 1.0;
108         bezt->hide= IPO_BEZ;
109         bezt->f1=bezt->f2= bezt->f3= SELECT;
110         bezt->h1= bezt->h2= HD_AUTO;
111         
112         calchandles_ipocurve(icu);
113 }
114 #endif // XXX old animation system
115         
116
117 /************************* Mesh ************************/
118
119 void mesh_to_key(Mesh *me, KeyBlock *kb)
120 {
121         MVert *mvert;
122         float *fp;
123         int a;
124         
125         if(me->totvert==0) return;
126         
127         if(kb->data) MEM_freeN(kb->data);
128         
129         kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data");
130         kb->totelem= me->totvert;
131         
132         mvert= me->mvert;
133         fp= kb->data;
134         for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
135                 VECCOPY(fp, mvert->co);
136                 
137         }
138 }
139
140 void key_to_mesh(KeyBlock *kb, Mesh *me)
141 {
142         MVert *mvert;
143         float *fp;
144         int a, tot;
145         
146         mvert= me->mvert;
147         fp= kb->data;
148         
149         tot= MIN2(kb->totelem, me->totvert);
150         
151         for(a=0; a<tot; a++, fp+=3, mvert++) {
152                 VECCOPY(mvert->co, fp);
153         }
154 }
155
156 static KeyBlock *add_keyblock(Scene *scene, Key *key)
157 {
158         KeyBlock *kb;
159         float curpos= -0.1;
160         int tot;
161         
162         kb= key->block.last;
163         if(kb) curpos= kb->pos;
164         
165         kb= MEM_callocN(sizeof(KeyBlock), "Keyblock");
166         BLI_addtail(&key->block, kb);
167         kb->type= KEY_CARDINAL;
168         
169         tot= BLI_countlist(&key->block);
170         if(tot==1) strcpy(kb->name, "Basis");
171         else sprintf(kb->name, "Key %d", tot-1);
172         
173                 // XXX this is old anim system stuff? (i.e. the 'index' of the shapekey)
174         kb->adrcode= tot-1;
175         
176         key->totkey++;
177         if(key->totkey==1) key->refkey= kb;
178         
179         kb->slidermin= 0.0f;
180         kb->slidermax= 1.0f;
181         
182         // XXX kb->pos is the confusing old horizontal-line RVK crap in old IPO Editor...
183         if(key->type == KEY_RELATIVE) 
184                 kb->pos= curpos+0.1;
185         else {
186 #if 0 // XXX old animation system
187                 curpos= bsystem_time(scene, 0, (float)CFRA, 0.0);
188                 if(calc_ipo_spec(key->ipo, KEY_SPEED, &curpos)==0) {
189                         curpos /= 100.0;
190                 }
191                 kb->pos= curpos;
192                 
193                 sort_keys(key);
194 #endif // XXX old animation system
195         }
196         return kb;
197 }
198
199 void insert_meshkey(Scene *scene, Mesh *me, short rel)
200 {
201         Key *key;
202         KeyBlock *kb;
203
204         if(me->key==NULL) {
205                 me->key= add_key( (ID *)me);
206
207                 if(rel)
208                         me->key->type = KEY_RELATIVE;
209 //              else
210 //                      default_key_ipo(scene, me->key); // XXX old animation system
211         }
212         key= me->key;
213         
214         kb= add_keyblock(scene, key);
215         
216         mesh_to_key(me, kb);
217 }
218
219 /************************* Lattice ************************/
220
221 void latt_to_key(Lattice *lt, KeyBlock *kb)
222 {
223         BPoint *bp;
224         float *fp;
225         int a, tot;
226         
227         tot= lt->pntsu*lt->pntsv*lt->pntsw;
228         if(tot==0) return;
229         
230         if(kb->data) MEM_freeN(kb->data);
231         
232         kb->data= MEM_callocN(lt->key->elemsize*tot, "kb->data");
233         kb->totelem= tot;
234         
235         bp= lt->def;
236         fp= kb->data;
237         for(a=0; a<kb->totelem; a++, fp+=3, bp++) {
238                 VECCOPY(fp, bp->vec);
239         }
240 }
241
242 void key_to_latt(KeyBlock *kb, Lattice *lt)
243 {
244         BPoint *bp;
245         float *fp;
246         int a, tot;
247         
248         bp= lt->def;
249         fp= kb->data;
250         
251         tot= lt->pntsu*lt->pntsv*lt->pntsw;
252         tot= MIN2(kb->totelem, tot);
253         
254         for(a=0; a<tot; a++, fp+=3, bp++) {
255                 VECCOPY(bp->vec, fp);
256         }
257         
258 }
259
260 /* exported to python... hrms, should not, use object levels! (ton) */
261 void insert_lattkey(Scene *scene, Lattice *lt, short rel)
262 {
263         Key *key;
264         KeyBlock *kb;
265         
266         if(lt->key==NULL) {
267                 lt->key= add_key( (ID *)lt);
268 //              default_key_ipo(scene, lt->key); // XXX old animation system
269         }
270         key= lt->key;
271         
272         kb= add_keyblock(scene, key);
273         
274         latt_to_key(lt, kb);
275 }
276
277 /************************* Curve ************************/
278
279 void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb)
280 {
281         Nurb *nu;
282         BezTriple *bezt;
283         BPoint *bp;
284         float *fp;
285         int a, tot;
286         
287         /* count */
288         tot= count_curveverts(nurb);
289         if(tot==0) return;
290         
291         if(kb->data) MEM_freeN(kb->data);
292         
293         kb->data= MEM_callocN(cu->key->elemsize*tot, "kb->data");
294         kb->totelem= tot;
295         
296         nu= nurb->first;
297         fp= kb->data;
298         while(nu) {
299                 
300                 if(nu->bezt) {
301                         bezt= nu->bezt;
302                         a= nu->pntsu;
303                         while(a--) {
304                                 VECCOPY(fp, bezt->vec[0]);
305                                 fp+= 3;
306                                 VECCOPY(fp, bezt->vec[1]);
307                                 fp+= 3;
308                                 VECCOPY(fp, bezt->vec[2]);
309                                 fp+= 3;
310                                 fp[0]= bezt->alfa;
311                                 fp+= 3; /* alphas */
312                                 bezt++;
313                         }
314                 }
315                 else {
316                         bp= nu->bp;
317                         a= nu->pntsu*nu->pntsv;
318                         while(a--) {
319                                 VECCOPY(fp, bp->vec);
320                                 fp[3]= bp->alfa;
321                                 
322                                 fp+= 4;
323                                 bp++;
324                         }
325                 }
326                 nu= nu->next;
327         }
328 }
329
330 void key_to_curve(KeyBlock *kb, Curve  *cu, ListBase *nurb)
331 {
332         Nurb *nu;
333         BezTriple *bezt;
334         BPoint *bp;
335         float *fp;
336         int a, tot;
337         
338         nu= nurb->first;
339         fp= kb->data;
340         
341         tot= count_curveverts(nurb);
342
343         tot= MIN2(kb->totelem, tot);
344         
345         while(nu && tot>0) {
346                 
347                 if(nu->bezt) {
348                         bezt= nu->bezt;
349                         a= nu->pntsu;
350                         while(a-- && tot>0) {
351                                 VECCOPY(bezt->vec[0], fp);
352                                 fp+= 3;
353                                 VECCOPY(bezt->vec[1], fp);
354                                 fp+= 3;
355                                 VECCOPY(bezt->vec[2], fp);
356                                 fp+= 3;
357                                 bezt->alfa= fp[0];
358                                 fp+= 3; /* alphas */
359                         
360                                 tot-= 3;
361                                 bezt++;
362                         }
363                 }
364                 else {
365                         bp= nu->bp;
366                         a= nu->pntsu*nu->pntsv;
367                         while(a-- && tot>0) {
368                                 VECCOPY(bp->vec, fp);
369                                 bp->alfa= fp[3];
370                                 
371                                 fp+= 4;
372                                 tot--;
373                                 bp++;
374                         }
375                 }
376                 nu= nu->next;
377         }
378 }
379
380
381 void insert_curvekey(Scene *scene, Curve *cu, short rel) 
382 {
383         Key *key;
384         KeyBlock *kb;
385         
386         if(cu->key==NULL) {
387                 cu->key= add_key( (ID *)cu);
388
389                 if(rel)
390                         cu->key->type = KEY_RELATIVE;
391 //              else
392 //                      default_key_ipo(scene, cu->key);        // XXX old animation system
393         }
394         key= cu->key;
395         
396         kb= add_keyblock(scene, key);
397         
398         if(cu->editnurb->first) curve_to_key(cu, kb, cu->editnurb);
399         else curve_to_key(cu, kb, &cu->nurb);
400 }
401
402 /*********************** add shape key ***********************/
403
404 void ED_object_shape_key_add(bContext *C, Scene *scene, Object *ob)
405 {
406         Key *key;
407
408         if(ob->type==OB_MESH) insert_meshkey(scene, ob->data, 1);
409         else if ELEM(ob->type, OB_CURVE, OB_SURF) insert_curvekey(scene, ob->data, 1);
410         else if(ob->type==OB_LATTICE) insert_lattkey(scene, ob->data, 1);
411
412         key= ob_get_key(ob);
413         ob->shapenr= BLI_countlist(&key->block);
414
415         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
416 }
417
418 /*********************** remove shape key ***********************/
419
420 int ED_object_shape_key_remove(bContext *C, Object *ob)
421 {
422         Main *bmain= CTX_data_main(C);
423         KeyBlock *kb, *rkb;
424         Key *key;
425         //IpoCurve *icu;
426
427         key= ob_get_key(ob);
428         if(key==NULL)
429                 return 0;
430         
431         kb= BLI_findlink(&key->block, ob->shapenr-1);
432
433         if(kb) {
434                 for(rkb= key->block.first; rkb; rkb= rkb->next)
435                         if(rkb->relative == ob->shapenr-1)
436                                 rkb->relative= 0;
437
438                 BLI_remlink(&key->block, kb);
439                 key->totkey--;
440                 if(key->refkey== kb)
441                         key->refkey= key->block.first;
442                         
443                 if(kb->data) MEM_freeN(kb->data);
444                 MEM_freeN(kb);
445                 
446                 for(kb= key->block.first; kb; kb= kb->next)
447                         if(kb->adrcode>=ob->shapenr)
448                                 kb->adrcode--;
449                 
450 #if 0 // XXX old animation system
451                 if(key->ipo) {
452                         
453                         for(icu= key->ipo->curve.first; icu; icu= icu->next) {
454                                 if(icu->adrcode==ob->shapenr-1) {
455                                         BLI_remlink(&key->ipo->curve, icu);
456                                         free_ipo_curve(icu);
457                                         break;
458                                 }
459                         }
460                         for(icu= key->ipo->curve.first; icu; icu= icu->next) 
461                                 if(icu->adrcode>=ob->shapenr)
462                                         icu->adrcode--;
463                 }
464 #endif // XXX old animation system              
465                 
466                 if(ob->shapenr>1) ob->shapenr--;
467         }
468         
469         if(key->totkey==0) {
470                 if(GS(key->from->name)==ID_ME) ((Mesh *)key->from)->key= NULL;
471                 else if(GS(key->from->name)==ID_CU) ((Curve *)key->from)->key= NULL;
472                 else if(GS(key->from->name)==ID_LT) ((Lattice *)key->from)->key= NULL;
473
474                 free_libblock_us(&(bmain->key), key);
475         }
476         
477         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
478         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
479
480         return 1;
481 }
482
483 /********************** shape key operators *********************/
484
485 static int shape_key_poll(bContext *C)
486 {
487         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
488         ID *data= (ob)? ob->data: NULL;
489         return (ob && !ob->id.lib && data && !data->lib);
490 }
491
492 static int shape_key_add_exec(bContext *C, wmOperator *op)
493 {
494         Scene *scene= CTX_data_scene(C);
495         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
496
497         ED_object_shape_key_add(C, scene, ob);
498         
499         return OPERATOR_FINISHED;
500 }
501
502 void OBJECT_OT_shape_key_add(wmOperatorType *ot)
503 {
504         /* identifiers */
505         ot->name= "Add Shape Key";
506         ot->name= "Add shape key to the object.";
507         ot->idname= "OBJECT_OT_shape_key_add";
508         
509         /* api callbacks */
510         ot->poll= shape_key_poll;
511         ot->exec= shape_key_add_exec;
512
513         /* flags */
514         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
515 }
516
517 static int shape_key_remove_exec(bContext *C, wmOperator *op)
518 {
519         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
520
521         if(!ED_object_shape_key_remove(C, ob))
522                 return OPERATOR_CANCELLED;
523         
524         return OPERATOR_FINISHED;
525 }
526
527 void OBJECT_OT_shape_key_remove(wmOperatorType *ot)
528 {
529         /* identifiers */
530         ot->name= "Remove Shape Key";
531         ot->name= "Remove shape key from the object.";
532         ot->idname= "OBJECT_OT_shape_key_remove";
533         
534         /* api callbacks */
535         ot->poll= shape_key_poll;
536         ot->exec= shape_key_remove_exec;
537
538         /* flags */
539         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
540 }
541
542 static int shape_key_clear_exec(bContext *C, wmOperator *op)
543 {
544         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
545         Key *key= ob_get_key(ob);
546         KeyBlock *kb= ob_get_keyblock(ob);
547
548         if(!key || !kb)
549                 return OPERATOR_CANCELLED;
550         
551         for(kb=key->block.first; kb; kb=kb->next)
552                 kb->curval= 0.0f;
553
554         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
555         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
556         
557         return OPERATOR_FINISHED;
558 }
559
560 void OBJECT_OT_shape_key_clear(wmOperatorType *ot)
561 {
562         /* identifiers */
563         ot->name= "Clear Shape Keys";
564         ot->description= "Clear weights for all shape keys.";
565         ot->idname= "OBJECT_OT_shape_key_clear";
566         
567         /* api callbacks */
568         ot->poll= shape_key_poll;
569         ot->exec= shape_key_clear_exec;
570
571         /* flags */
572         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
573 }
574