2.5: Lists for vertex groups, shape keys, uvs, vertex colors.
[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_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 /* XXX */
82 static void BIF_undo_push() {}
83 /* XXX */
84
85 #if 0 // XXX old animation system
86 static void default_key_ipo(Scene *scene, Key *key)
87 {
88         IpoCurve *icu;
89         BezTriple *bezt;
90         
91         key->ipo= add_ipo(scene, "KeyIpo", ID_KE);
92         
93         icu= MEM_callocN(sizeof(IpoCurve), "ipocurve");
94                         
95         icu->blocktype= ID_KE;
96         icu->adrcode= KEY_SPEED;
97         icu->flag= IPO_VISIBLE|IPO_SELECT|IPO_AUTO_HORIZ;
98         set_icu_vars(icu);
99         
100         BLI_addtail( &(key->ipo->curve), icu);
101         
102         icu->bezt= bezt= MEM_callocN(2*sizeof(BezTriple), "defaultipo");
103         icu->totvert= 2;
104         
105         bezt->hide= IPO_BEZ;
106         bezt->f1=bezt->f2= bezt->f3= SELECT;
107         bezt->h1= bezt->h2= HD_AUTO;
108         bezt++;
109         bezt->vec[1][0]= 100.0;
110         bezt->vec[1][1]= 1.0;
111         bezt->hide= IPO_BEZ;
112         bezt->f1=bezt->f2= bezt->f3= SELECT;
113         bezt->h1= bezt->h2= HD_AUTO;
114         
115         calchandles_ipocurve(icu);
116 }
117 #endif // XXX old animation system
118         
119
120 /* **************************************** */
121
122 void mesh_to_key(Mesh *me, KeyBlock *kb)
123 {
124         MVert *mvert;
125         float *fp;
126         int a;
127         
128         if(me->totvert==0) return;
129         
130         if(kb->data) MEM_freeN(kb->data);
131         
132         kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data");
133         kb->totelem= me->totvert;
134         
135         mvert= me->mvert;
136         fp= kb->data;
137         for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
138                 VECCOPY(fp, mvert->co);
139                 
140         }
141 }
142
143 void key_to_mesh(KeyBlock *kb, Mesh *me)
144 {
145         MVert *mvert;
146         float *fp;
147         int a, tot;
148         
149         mvert= me->mvert;
150         fp= kb->data;
151         
152         tot= MIN2(kb->totelem, me->totvert);
153         
154         for(a=0; a<tot; a++, fp+=3, mvert++) {
155                 VECCOPY(mvert->co, fp);
156         }
157 }
158
159 static KeyBlock *add_keyblock(Scene *scene, Key *key)
160 {
161         KeyBlock *kb;
162         float curpos= -0.1;
163         int tot;
164         
165         kb= key->block.last;
166         if(kb) curpos= kb->pos;
167         
168         kb= MEM_callocN(sizeof(KeyBlock), "Keyblock");
169         BLI_addtail(&key->block, kb);
170         kb->type= KEY_CARDINAL;
171         tot= BLI_countlist(&key->block);
172         if(tot==1) strcpy(kb->name, "Basis");
173         else sprintf(kb->name, "Key %d", tot-1);
174         kb->adrcode= tot-1;
175         
176         key->totkey++;
177         if(key->totkey==1) key->refkey= kb;
178         
179         // XXX kb->pos is the confusing old horizontal-line RVK crap in old IPO Editor...
180         if(key->type == KEY_RELATIVE) 
181                 kb->pos= curpos+0.1;
182         else {
183 #if 0 // XXX old animation system
184                 curpos= bsystem_time(scene, 0, (float)CFRA, 0.0);
185                 if(calc_ipo_spec(key->ipo, KEY_SPEED, &curpos)==0) {
186                         curpos /= 100.0;
187                 }
188                 kb->pos= curpos;
189                 
190                 sort_keys(key);
191 #endif // XXX old animation system
192         }
193         return kb;
194 }
195
196 void insert_meshkey(Scene *scene, Mesh *me, short rel)
197 {
198         Key *key;
199         KeyBlock *kb;
200
201         if(me->key==NULL) {
202                 me->key= add_key( (ID *)me);
203
204                 if(rel)
205                         me->key->type = KEY_RELATIVE;
206 //              else
207 //                      default_key_ipo(scene, me->key); // XXX old animation system
208         }
209         key= me->key;
210         
211         kb= add_keyblock(scene, key);
212         
213         mesh_to_key(me, kb);
214 }
215
216 /* ******************** */
217
218 void latt_to_key(Lattice *lt, KeyBlock *kb)
219 {
220         BPoint *bp;
221         float *fp;
222         int a, tot;
223         
224         tot= lt->pntsu*lt->pntsv*lt->pntsw;
225         if(tot==0) return;
226         
227         if(kb->data) MEM_freeN(kb->data);
228         
229         kb->data= MEM_callocN(lt->key->elemsize*tot, "kb->data");
230         kb->totelem= tot;
231         
232         bp= lt->def;
233         fp= kb->data;
234         for(a=0; a<kb->totelem; a++, fp+=3, bp++) {
235                 VECCOPY(fp, bp->vec);
236         }
237 }
238
239 void key_to_latt(KeyBlock *kb, Lattice *lt)
240 {
241         BPoint *bp;
242         float *fp;
243         int a, tot;
244         
245         bp= lt->def;
246         fp= kb->data;
247         
248         tot= lt->pntsu*lt->pntsv*lt->pntsw;
249         tot= MIN2(kb->totelem, tot);
250         
251         for(a=0; a<tot; a++, fp+=3, bp++) {
252                 VECCOPY(bp->vec, fp);
253         }
254         
255 }
256
257 /* exported to python... hrms, should not, use object levels! (ton) */
258 void insert_lattkey(Scene *scene, Lattice *lt, short rel)
259 {
260         Key *key;
261         KeyBlock *kb;
262         
263         if(lt->key==NULL) {
264                 lt->key= add_key( (ID *)lt);
265 //              default_key_ipo(scene, lt->key); // XXX old animation system
266         }
267         key= lt->key;
268         
269         kb= add_keyblock(scene, key);
270         
271         latt_to_key(lt, kb);
272 }
273
274 /* ******************************** */
275
276 void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb)
277 {
278         Nurb *nu;
279         BezTriple *bezt;
280         BPoint *bp;
281         float *fp;
282         int a, tot;
283         
284         /* count */
285         tot= count_curveverts(nurb);
286         if(tot==0) return;
287         
288         if(kb->data) MEM_freeN(kb->data);
289         
290         kb->data= MEM_callocN(cu->key->elemsize*tot, "kb->data");
291         kb->totelem= tot;
292         
293         nu= nurb->first;
294         fp= kb->data;
295         while(nu) {
296                 
297                 if(nu->bezt) {
298                         bezt= nu->bezt;
299                         a= nu->pntsu;
300                         while(a--) {
301                                 VECCOPY(fp, bezt->vec[0]);
302                                 fp+= 3;
303                                 VECCOPY(fp, bezt->vec[1]);
304                                 fp+= 3;
305                                 VECCOPY(fp, bezt->vec[2]);
306                                 fp+= 3;
307                                 fp[0]= bezt->alfa;
308                                 fp+= 3; /* alphas */
309                                 bezt++;
310                         }
311                 }
312                 else {
313                         bp= nu->bp;
314                         a= nu->pntsu*nu->pntsv;
315                         while(a--) {
316                                 VECCOPY(fp, bp->vec);
317                                 fp[3]= bp->alfa;
318                                 
319                                 fp+= 4;
320                                 bp++;
321                         }
322                 }
323                 nu= nu->next;
324         }
325 }
326
327 void key_to_curve(KeyBlock *kb, Curve  *cu, ListBase *nurb)
328 {
329         Nurb *nu;
330         BezTriple *bezt;
331         BPoint *bp;
332         float *fp;
333         int a, tot;
334         
335         nu= nurb->first;
336         fp= kb->data;
337         
338         tot= count_curveverts(nurb);
339
340         tot= MIN2(kb->totelem, tot);
341         
342         while(nu && tot>0) {
343                 
344                 if(nu->bezt) {
345                         bezt= nu->bezt;
346                         a= nu->pntsu;
347                         while(a-- && tot>0) {
348                                 VECCOPY(bezt->vec[0], fp);
349                                 fp+= 3;
350                                 VECCOPY(bezt->vec[1], fp);
351                                 fp+= 3;
352                                 VECCOPY(bezt->vec[2], fp);
353                                 fp+= 3;
354                                 bezt->alfa= fp[0];
355                                 fp+= 3; /* alphas */
356                         
357                                 tot-= 3;
358                                 bezt++;
359                         }
360                 }
361                 else {
362                         bp= nu->bp;
363                         a= nu->pntsu*nu->pntsv;
364                         while(a-- && tot>0) {
365                                 VECCOPY(bp->vec, fp);
366                                 bp->alfa= fp[3];
367                                 
368                                 fp+= 4;
369                                 tot--;
370                                 bp++;
371                         }
372                 }
373                 nu= nu->next;
374         }
375 }
376
377
378 void insert_curvekey(Scene *scene, Curve *cu, short rel) 
379 {
380         Key *key;
381         KeyBlock *kb;
382         
383         if(cu->key==NULL) {
384                 cu->key= add_key( (ID *)cu);
385
386                 if (rel)
387                         cu->key->type = KEY_RELATIVE;
388 //              else
389 //                      default_key_ipo(scene, cu->key);        // XXX old animation system
390         }
391         key= cu->key;
392         
393         kb= add_keyblock(scene, key);
394         
395         if(cu->editnurb->first) curve_to_key(cu, kb, cu->editnurb);
396         else curve_to_key(cu, kb, &cu->nurb);
397 }
398
399
400 /* ******************** */
401
402 void delete_key(Scene *scene, Object *ob)
403 {
404         KeyBlock *kb, *rkb;
405         Key *key;
406         //IpoCurve *icu;
407         
408         key= ob_get_key(ob);
409         if(key==NULL) return;
410         
411         kb= BLI_findlink(&key->block, ob->shapenr-1);
412
413         if(kb) {
414                 for(rkb= key->block.first; rkb; rkb= rkb->next)
415                         if(rkb->relative == ob->shapenr-1)
416                                 rkb->relative= 0;
417
418                 BLI_remlink(&key->block, kb);
419                 key->totkey--;
420                 if(key->refkey== kb) key->refkey= key->block.first;
421                         
422                 if(kb->data) MEM_freeN(kb->data);
423                 MEM_freeN(kb);
424                 
425                 for(kb= key->block.first; kb; kb= kb->next) {
426                         if(kb->adrcode>=ob->shapenr)
427                                 kb->adrcode--;
428                 }
429                 
430 #if 0 // XXX old animation system
431                 if(key->ipo) {
432                         
433                         for(icu= key->ipo->curve.first; icu; icu= icu->next) {
434                                 if(icu->adrcode==ob->shapenr-1) {
435                                         BLI_remlink(&key->ipo->curve, icu);
436                                         free_ipo_curve(icu);
437                                         break;
438                                 }
439                         }
440                         for(icu= key->ipo->curve.first; icu; icu= icu->next) 
441                                 if(icu->adrcode>=ob->shapenr)
442                                         icu->adrcode--;
443                 }
444 #endif // XXX old animation system              
445                 
446                 if(ob->shapenr>1) ob->shapenr--;
447         }
448         
449         if(key->totkey==0) {
450                 if(GS(key->from->name)==ID_ME) ((Mesh *)key->from)->key= NULL;
451                 else if(GS(key->from->name)==ID_CU) ((Curve *)key->from)->key= NULL;
452                 else if(GS(key->from->name)==ID_LT) ((Lattice *)key->from)->key= NULL;
453
454                 free_libblock_us(&(G.main->key), key);
455         }
456         
457         DAG_object_flush_update(scene, OBACT, OB_RECALC_DATA);
458         
459         BIF_undo_push("Delete Shapekey");
460 }
461
462 /********************** shape key operators *********************/
463
464 static int shape_key_add_exec(bContext *C, wmOperator *op)
465 {
466         Scene *scene= CTX_data_scene(C);
467         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
468         Key *key;
469
470         if(!ob)
471                 return OPERATOR_CANCELLED;
472
473         if(ob->type==OB_MESH) insert_meshkey(scene, ob->data, 1);
474         else if ELEM(ob->type, OB_CURVE, OB_SURF) insert_curvekey(scene, ob->data, 1);
475         else if(ob->type==OB_LATTICE) insert_lattkey(scene, ob->data, 1);
476
477         key= ob_get_key(ob);
478         ob->shapenr= BLI_countlist(&key->block);
479
480         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
481         
482         return OPERATOR_FINISHED;
483 }
484
485 void OBJECT_OT_shape_key_add(wmOperatorType *ot)
486 {
487         /* identifiers */
488         ot->name= "Add Shape Key";
489         ot->idname= "OBJECT_OT_shape_key_add";
490         
491         /* api callbacks */
492         ot->exec= shape_key_add_exec;
493
494         /* flags */
495         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
496 }
497
498 static int shape_key_remove_exec(bContext *C, wmOperator *op)
499 {
500         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
501         Scene *scene= CTX_data_scene(C);
502         Main *bmain= CTX_data_main(C);
503         KeyBlock *kb, *rkb;
504         Key *key;
505         //IpoCurve *icu;
506
507         if(!ob)
508                 return OPERATOR_CANCELLED;
509         
510         key= ob_get_key(ob);
511         if(key==NULL)
512                 return OPERATOR_CANCELLED;
513         
514         kb= BLI_findlink(&key->block, ob->shapenr-1);
515
516         if(kb) {
517                 for(rkb= key->block.first; rkb; rkb= rkb->next)
518                         if(rkb->relative == ob->shapenr-1)
519                                 rkb->relative= 0;
520
521                 BLI_remlink(&key->block, kb);
522                 key->totkey--;
523                 if(key->refkey== kb)
524                         key->refkey= key->block.first;
525                         
526                 if(kb->data) MEM_freeN(kb->data);
527                 MEM_freeN(kb);
528                 
529                 for(kb= key->block.first; kb; kb= kb->next)
530                         if(kb->adrcode>=ob->shapenr)
531                                 kb->adrcode--;
532                 
533 #if 0 // XXX old animation system
534                 if(key->ipo) {
535                         
536                         for(icu= key->ipo->curve.first; icu; icu= icu->next) {
537                                 if(icu->adrcode==ob->shapenr-1) {
538                                         BLI_remlink(&key->ipo->curve, icu);
539                                         free_ipo_curve(icu);
540                                         break;
541                                 }
542                         }
543                         for(icu= key->ipo->curve.first; icu; icu= icu->next) 
544                                 if(icu->adrcode>=ob->shapenr)
545                                         icu->adrcode--;
546                 }
547 #endif // XXX old animation system              
548                 
549                 if(ob->shapenr>1) ob->shapenr--;
550         }
551         
552         if(key->totkey==0) {
553                 if(GS(key->from->name)==ID_ME) ((Mesh *)key->from)->key= NULL;
554                 else if(GS(key->from->name)==ID_CU) ((Curve *)key->from)->key= NULL;
555                 else if(GS(key->from->name)==ID_LT) ((Lattice *)key->from)->key= NULL;
556
557                 free_libblock_us(&(bmain->key), key);
558         }
559         
560         DAG_object_flush_update(scene, OBACT, OB_RECALC_DATA);
561         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
562         
563         return OPERATOR_FINISHED;
564 }
565
566 void OBJECT_OT_shape_key_remove(wmOperatorType *ot)
567 {
568         /* identifiers */
569         ot->name= "Remove Shape Key";
570         ot->idname= "OBJECT_OT_shape_key_remove";
571         
572         /* api callbacks */
573         ot->exec= shape_key_remove_exec;
574
575         /* flags */
576         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
577 }
578
579 void move_keys(Object *ob)
580 {
581 #if 0
582         /* XXX probably goes away entirely */
583         Key *key;
584         KeyBlock *kb;
585         float div, dy, oldpos, vec[3], dvec[3];
586         int afbreek=0, firsttime= 1;
587         unsigned short event = 0;
588         short mval[2], val, xo, yo;
589         char str[32];
590         
591         if(G.sipo->blocktype!=ID_KE) return;
592         
593         if(G.sipo->ipo && G.sipo->ipo->id.lib) return;
594         if(G.sipo->editipo==NULL) return;
595
596         key= ob_get_key(ob);
597         if(key==NULL) return;
598         
599         /* which kb is involved */
600         kb= BLI_findlink(&key->block, ob->shapenr-1);
601         if(kb==NULL) return;    
602         
603         oldpos= kb->pos;
604         
605         getmouseco_areawin(mval);
606         xo= mval[0];
607         yo= mval[1];
608         dvec[0]=dvec[1]=dvec[2]= 0.0; 
609
610         while(afbreek==0) {
611                 getmouseco_areawin(mval);
612                 if(mval[0]!=xo || mval[1]!=yo || firsttime) {
613                         firsttime= 0;
614                         
615                         dy= (float)(mval[1]- yo);
616
617                         div= (float)(G.v2d->mask.ymax-G.v2d->mask.ymin);
618                         dvec[1]+= (G.v2d->cur.ymax-G.v2d->cur.ymin)*(dy)/div;
619                         
620                         VECCOPY(vec, dvec);
621
622                         apply_keyb_grid(vec, 0.0, 1.0, 0.1, U.flag & USER_AUTOGRABGRID);
623                         apply_keyb_grid(vec+1, 0.0, 1.0, 0.1, U.flag & USER_AUTOGRABGRID);
624
625                         kb->pos= oldpos+vec[1];
626                         
627                         sprintf(str, "Y: %.3f  ", vec[1]);
628                         headerprint(str);
629                         
630                         xo= mval[0];
631                         yo= mval[1];
632                                 
633                         force_draw(0);
634                 }
635                 else BIF_wait_for_statechange();
636                 
637                 while(qtest()) {
638                         event= extern_qread(&val);
639                         if(val) {
640                                 switch(event) {
641                                 case ESCKEY:
642                                 case LEFTMOUSE:
643                                 case SPACEKEY:
644                                         afbreek= 1;
645                                         break;
646                                 default:
647                                         arrows_move_cursor(event);
648                                 }
649                         }
650                 }
651         }
652         
653         if(event==ESCKEY) {
654                 kb->pos= oldpos;
655         }
656         
657         sort_keys(key);
658         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
659         
660         /* for boundbox */
661         editipo_changed(G.sipo, 0);
662
663         BIF_undo_push("Move Shapekey(s)");
664 #endif
665 }
666