913046c5ab80b02fc0cef036000d5d0f665fa25a
[blender-staging.git] / source / blender / editors / object / editkey.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_curve.h"
59 #include "BKE_depsgraph.h"
60 #include "BKE_global.h"
61 #include "BKE_ipo.h"
62 #include "BKE_key.h"
63 #include "BKE_library.h"
64 #include "BKE_main.h"
65 #include "BKE_mesh.h"
66 #include "BKE_object.h"
67 #include "BKE_utildefines.h"
68
69 #include "BLO_sys_types.h" // for intptr_t support
70
71 #include "ED_object.h"
72
73 #include "object_intern.h"
74
75 /* XXX */
76 static void BIF_undo_push() {}
77 static void error() {}
78 /* XXX */
79
80 #if 0 // XXX old animation system
81 static void default_key_ipo(Scene *scene, Key *key)
82 {
83         IpoCurve *icu;
84         BezTriple *bezt;
85         
86         key->ipo= add_ipo(scene, "KeyIpo", ID_KE);
87         
88         icu= MEM_callocN(sizeof(IpoCurve), "ipocurve");
89                         
90         icu->blocktype= ID_KE;
91         icu->adrcode= KEY_SPEED;
92         icu->flag= IPO_VISIBLE|IPO_SELECT|IPO_AUTO_HORIZ;
93         set_icu_vars(icu);
94         
95         BLI_addtail( &(key->ipo->curve), icu);
96         
97         icu->bezt= bezt= MEM_callocN(2*sizeof(BezTriple), "defaultipo");
98         icu->totvert= 2;
99         
100         bezt->hide= IPO_BEZ;
101         bezt->f1=bezt->f2= bezt->f3= SELECT;
102         bezt->h1= bezt->h2= HD_AUTO;
103         bezt++;
104         bezt->vec[1][0]= 100.0;
105         bezt->vec[1][1]= 1.0;
106         bezt->hide= IPO_BEZ;
107         bezt->f1=bezt->f2= bezt->f3= SELECT;
108         bezt->h1= bezt->h2= HD_AUTO;
109         
110         calchandles_ipocurve(icu);
111 }
112 #endif // XXX old animation system
113         
114
115 /* **************************************** */
116
117 void mesh_to_key(Mesh *me, KeyBlock *kb)
118 {
119         MVert *mvert;
120         float *fp;
121         int a;
122         
123         if(me->totvert==0) return;
124         
125         if(kb->data) MEM_freeN(kb->data);
126         
127         kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data");
128         kb->totelem= me->totvert;
129         
130         mvert= me->mvert;
131         fp= kb->data;
132         for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
133                 VECCOPY(fp, mvert->co);
134                 
135         }
136 }
137
138 void key_to_mesh(KeyBlock *kb, Mesh *me)
139 {
140         MVert *mvert;
141         float *fp;
142         int a, tot;
143         
144         mvert= me->mvert;
145         fp= kb->data;
146         
147         tot= MIN2(kb->totelem, me->totvert);
148         
149         for(a=0; a<tot; a++, fp+=3, mvert++) {
150                 VECCOPY(mvert->co, fp);
151         }
152 }
153
154 static KeyBlock *add_keyblock(Scene *scene, Key *key)
155 {
156         KeyBlock *kb;
157         float curpos= -0.1;
158         int tot;
159         
160         kb= key->block.last;
161         if(kb) curpos= kb->pos;
162         
163         kb= MEM_callocN(sizeof(KeyBlock), "Keyblock");
164         BLI_addtail(&key->block, kb);
165         kb->type= KEY_CARDINAL;
166         tot= BLI_countlist(&key->block);
167         if(tot==1) strcpy(kb->name, "Basis");
168         else sprintf(kb->name, "Key %d", tot-1);
169         kb->adrcode= tot-1;
170         
171         key->totkey++;
172         if(key->totkey==1) key->refkey= kb;
173         
174         // XXX kb->pos is the confusing old horizontal-line RVK crap in old IPO Editor...
175         if(key->type == KEY_RELATIVE) 
176                 kb->pos= curpos+0.1;
177         else {
178 #if 0 // XXX old animation system
179                 curpos= bsystem_time(scene, 0, (float)CFRA, 0.0);
180                 if(calc_ipo_spec(key->ipo, KEY_SPEED, &curpos)==0) {
181                         curpos /= 100.0;
182                 }
183                 kb->pos= curpos;
184                 
185                 sort_keys(key);
186 #endif // XXX old animation system
187         }
188         return kb;
189 }
190
191 void insert_meshkey(Scene *scene, Mesh *me, short rel)
192 {
193         Key *key;
194         KeyBlock *kb;
195
196         if(me->key==NULL) {
197                 me->key= add_key( (ID *)me);
198
199                 if(rel)
200                         me->key->type = KEY_RELATIVE;
201 //              else
202 //                      default_key_ipo(scene, me->key); // XXX old animation system
203         }
204         key= me->key;
205         
206         kb= add_keyblock(scene, key);
207         
208         mesh_to_key(me, kb);
209 }
210
211 /* ******************** */
212
213 void latt_to_key(Lattice *lt, KeyBlock *kb)
214 {
215         BPoint *bp;
216         float *fp;
217         int a, tot;
218         
219         tot= lt->pntsu*lt->pntsv*lt->pntsw;
220         if(tot==0) return;
221         
222         if(kb->data) MEM_freeN(kb->data);
223         
224         kb->data= MEM_callocN(lt->key->elemsize*tot, "kb->data");
225         kb->totelem= tot;
226         
227         bp= lt->def;
228         fp= kb->data;
229         for(a=0; a<kb->totelem; a++, fp+=3, bp++) {
230                 VECCOPY(fp, bp->vec);
231         }
232 }
233
234 void key_to_latt(KeyBlock *kb, Lattice *lt)
235 {
236         BPoint *bp;
237         float *fp;
238         int a, tot;
239         
240         bp= lt->def;
241         fp= kb->data;
242         
243         tot= lt->pntsu*lt->pntsv*lt->pntsw;
244         tot= MIN2(kb->totelem, tot);
245         
246         for(a=0; a<tot; a++, fp+=3, bp++) {
247                 VECCOPY(bp->vec, fp);
248         }
249         
250 }
251
252 /* exported to python... hrms, should not, use object levels! (ton) */
253 void insert_lattkey(Scene *scene, Lattice *lt, short rel)
254 {
255         Key *key;
256         KeyBlock *kb;
257         
258         if(lt->key==NULL) {
259                 lt->key= add_key( (ID *)lt);
260 //              default_key_ipo(scene, lt->key); // XXX old animation system
261         }
262         key= lt->key;
263         
264         kb= add_keyblock(scene, key);
265         
266         latt_to_key(lt, kb);
267 }
268
269 /* ******************************** */
270
271 void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb)
272 {
273         Nurb *nu;
274         BezTriple *bezt;
275         BPoint *bp;
276         float *fp;
277         int a, tot;
278         
279         /* count */
280         tot= count_curveverts(nurb);
281         if(tot==0) return;
282         
283         if(kb->data) MEM_freeN(kb->data);
284         
285         kb->data= MEM_callocN(cu->key->elemsize*tot, "kb->data");
286         kb->totelem= tot;
287         
288         nu= nurb->first;
289         fp= kb->data;
290         while(nu) {
291                 
292                 if(nu->bezt) {
293                         bezt= nu->bezt;
294                         a= nu->pntsu;
295                         while(a--) {
296                                 VECCOPY(fp, bezt->vec[0]);
297                                 fp+= 3;
298                                 VECCOPY(fp, bezt->vec[1]);
299                                 fp+= 3;
300                                 VECCOPY(fp, bezt->vec[2]);
301                                 fp+= 3;
302                                 fp[0]= bezt->alfa;
303                                 fp+= 3; /* alphas */
304                                 bezt++;
305                         }
306                 }
307                 else {
308                         bp= nu->bp;
309                         a= nu->pntsu*nu->pntsv;
310                         while(a--) {
311                                 VECCOPY(fp, bp->vec);
312                                 fp[3]= bp->alfa;
313                                 
314                                 fp+= 4;
315                                 bp++;
316                         }
317                 }
318                 nu= nu->next;
319         }
320 }
321
322 void key_to_curve(KeyBlock *kb, Curve  *cu, ListBase *nurb)
323 {
324         Nurb *nu;
325         BezTriple *bezt;
326         BPoint *bp;
327         float *fp;
328         int a, tot;
329         
330         nu= nurb->first;
331         fp= kb->data;
332         
333         tot= count_curveverts(nurb);
334
335         tot= MIN2(kb->totelem, tot);
336         
337         while(nu && tot>0) {
338                 
339                 if(nu->bezt) {
340                         bezt= nu->bezt;
341                         a= nu->pntsu;
342                         while(a-- && tot>0) {
343                                 VECCOPY(bezt->vec[0], fp);
344                                 fp+= 3;
345                                 VECCOPY(bezt->vec[1], fp);
346                                 fp+= 3;
347                                 VECCOPY(bezt->vec[2], fp);
348                                 fp+= 3;
349                                 bezt->alfa= fp[0];
350                                 fp+= 3; /* alphas */
351                         
352                                 tot-= 3;
353                                 bezt++;
354                         }
355                 }
356                 else {
357                         bp= nu->bp;
358                         a= nu->pntsu*nu->pntsv;
359                         while(a-- && tot>0) {
360                                 VECCOPY(bp->vec, fp);
361                                 bp->alfa= fp[3];
362                                 
363                                 fp+= 4;
364                                 tot--;
365                                 bp++;
366                         }
367                 }
368                 nu= nu->next;
369         }
370 }
371
372
373 void insert_curvekey(Scene *scene, Curve *cu, short rel) 
374 {
375         Key *key;
376         KeyBlock *kb;
377         
378         if(cu->key==NULL) {
379                 cu->key= add_key( (ID *)cu);
380
381                 if (rel)
382                         cu->key->type = KEY_RELATIVE;
383 //              else
384 //                      default_key_ipo(scene, cu->key);        // XXX old animation system
385         }
386         key= cu->key;
387         
388         kb= add_keyblock(scene, key);
389         
390         if(cu->editnurb->first) curve_to_key(cu, kb, cu->editnurb);
391         else curve_to_key(cu, kb, &cu->nurb);
392 }
393
394
395 /* ******************** */
396
397 void insert_shapekey(Scene *scene, Object *ob)
398 {
399         if(get_mesh(ob) && get_mesh(ob)->mr) {
400                 error("Cannot create shape keys on a multires mesh.");
401         }
402         else {
403                 Key *key;
404         
405                 if(ob->type==OB_MESH) insert_meshkey(scene, ob->data, 1);
406                 else if ELEM(ob->type, OB_CURVE, OB_SURF) insert_curvekey(scene, ob->data, 1);
407                 else if(ob->type==OB_LATTICE) insert_lattkey(scene, ob->data, 1);
408         
409                 key= ob_get_key(ob);
410                 ob->shapenr= BLI_countlist(&key->block);
411         
412                 BIF_undo_push("Add Shapekey");
413         }
414 }
415
416 void delete_key(Scene *scene, Object *ob)
417 {
418         KeyBlock *kb, *rkb;
419         Key *key;
420         //IpoCurve *icu;
421         
422         key= ob_get_key(ob);
423         if(key==NULL) return;
424         
425         kb= BLI_findlink(&key->block, ob->shapenr-1);
426
427         if(kb) {
428                 for(rkb= key->block.first; rkb; rkb= rkb->next)
429                         if(rkb->relative == ob->shapenr-1)
430                                 rkb->relative= 0;
431
432                 BLI_remlink(&key->block, kb);
433                 key->totkey--;
434                 if(key->refkey== kb) key->refkey= key->block.first;
435                         
436                 if(kb->data) MEM_freeN(kb->data);
437                 MEM_freeN(kb);
438                 
439                 for(kb= key->block.first; kb; kb= kb->next) {
440                         if(kb->adrcode>=ob->shapenr)
441                                 kb->adrcode--;
442                 }
443                 
444 #if 0 // XXX old animation system
445                 if(key->ipo) {
446                         
447                         for(icu= key->ipo->curve.first; icu; icu= icu->next) {
448                                 if(icu->adrcode==ob->shapenr-1) {
449                                         BLI_remlink(&key->ipo->curve, icu);
450                                         free_ipo_curve(icu);
451                                         break;
452                                 }
453                         }
454                         for(icu= key->ipo->curve.first; icu; icu= icu->next) 
455                                 if(icu->adrcode>=ob->shapenr)
456                                         icu->adrcode--;
457                 }
458 #endif // XXX old animation system              
459                 
460                 if(ob->shapenr>1) ob->shapenr--;
461         }
462         
463         if(key->totkey==0) {
464                 if(GS(key->from->name)==ID_ME) ((Mesh *)key->from)->key= NULL;
465                 else if(GS(key->from->name)==ID_CU) ((Curve *)key->from)->key= NULL;
466                 else if(GS(key->from->name)==ID_LT) ((Lattice *)key->from)->key= NULL;
467
468                 free_libblock_us(&(G.main->key), key);
469         }
470         
471         DAG_object_flush_update(scene, OBACT, OB_RECALC_DATA);
472         
473         BIF_undo_push("Delete Shapekey");
474 }
475
476 void move_keys(Object *ob)
477 {
478 #if 0
479         /* XXX probably goes away entirely */
480         Key *key;
481         KeyBlock *kb;
482         float div, dy, oldpos, vec[3], dvec[3];
483         int afbreek=0, firsttime= 1;
484         unsigned short event = 0;
485         short mval[2], val, xo, yo;
486         char str[32];
487         
488         if(G.sipo->blocktype!=ID_KE) return;
489         
490         if(G.sipo->ipo && G.sipo->ipo->id.lib) return;
491         if(G.sipo->editipo==NULL) return;
492
493         key= ob_get_key(ob);
494         if(key==NULL) return;
495         
496         /* which kb is involved */
497         kb= BLI_findlink(&key->block, ob->shapenr-1);
498         if(kb==NULL) return;    
499         
500         oldpos= kb->pos;
501         
502         getmouseco_areawin(mval);
503         xo= mval[0];
504         yo= mval[1];
505         dvec[0]=dvec[1]=dvec[2]= 0.0; 
506
507         while(afbreek==0) {
508                 getmouseco_areawin(mval);
509                 if(mval[0]!=xo || mval[1]!=yo || firsttime) {
510                         firsttime= 0;
511                         
512                         dy= (float)(mval[1]- yo);
513
514                         div= (float)(G.v2d->mask.ymax-G.v2d->mask.ymin);
515                         dvec[1]+= (G.v2d->cur.ymax-G.v2d->cur.ymin)*(dy)/div;
516                         
517                         VECCOPY(vec, dvec);
518
519                         apply_keyb_grid(vec, 0.0, 1.0, 0.1, U.flag & USER_AUTOGRABGRID);
520                         apply_keyb_grid(vec+1, 0.0, 1.0, 0.1, U.flag & USER_AUTOGRABGRID);
521
522                         kb->pos= oldpos+vec[1];
523                         
524                         sprintf(str, "Y: %.3f  ", vec[1]);
525                         headerprint(str);
526                         
527                         xo= mval[0];
528                         yo= mval[1];
529                                 
530                         force_draw(0);
531                 }
532                 else BIF_wait_for_statechange();
533                 
534                 while(qtest()) {
535                         event= extern_qread(&val);
536                         if(val) {
537                                 switch(event) {
538                                 case ESCKEY:
539                                 case LEFTMOUSE:
540                                 case SPACEKEY:
541                                         afbreek= 1;
542                                         break;
543                                 default:
544                                         arrows_move_cursor(event);
545                                 }
546                         }
547                 }
548         }
549         
550         if(event==ESCKEY) {
551                 kb->pos= oldpos;
552         }
553         
554         sort_keys(key);
555         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
556         
557         /* for boundbox */
558         editipo_changed(G.sipo, 0);
559
560         BIF_undo_push("Move Shapekey(s)");
561 #endif
562 }