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