svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22130:22205
[blender.git] / source / blender / blenkernel / intern / key.c
1
2 /*  key.c      
3  *  
4  * 
5  * $Id$
6  *
7  * ***** BEGIN GPL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 #include <math.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_anim_types.h"
39 #include "DNA_curve_types.h"
40 #include "DNA_key_types.h"
41 #include "DNA_lattice_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46
47 #include "BKE_animsys.h"
48 #include "BKE_action.h"
49 #include "BKE_blender.h"
50 #include "BKE_curve.h"
51 #include "BKE_global.h"
52 #include "BKE_key.h"
53 #include "BKE_lattice.h"
54 #include "BKE_library.h"
55 #include "BKE_mesh.h"
56 #include "BKE_main.h"
57 #include "BKE_object.h"
58 #include "BKE_utildefines.h"
59
60 #include "BLI_blenlib.h"
61
62
63 #ifdef HAVE_CONFIG_H
64 #include <config.h>
65 #endif
66
67 #define KEY_BPOINT              1
68 #define KEY_BEZTRIPLE   2
69
70         // old defines from DNA_ipo_types.h for data-type
71 #define IPO_FLOAT               4
72 #define IPO_BEZTRIPLE   100
73 #define IPO_BPOINT              101
74
75 int slurph_opt= 1;
76
77
78 void free_key(Key *key)
79 {
80         KeyBlock *kb;
81         
82         BKE_free_animdata((ID *)key);
83         
84         while( (kb= key->block.first) ) {
85                 
86                 if(kb->data) MEM_freeN(kb->data);
87                 
88                 BLI_remlink(&key->block, kb);
89                 MEM_freeN(kb);
90         }
91         
92 }
93
94 /* GS reads the memory pointed at in a specific ordering. There are,
95  * however two definitions for it. I have jotted them down here, both,
96  * but I think the first one is actually used. The thing is that
97  * big-endian systems might read this the wrong way round. OTOH, we
98  * constructed the IDs that are read out with this macro explicitly as
99  * well. I expect we'll sort it out soon... */
100
101 /* from blendef: */
102 #define GS(a)   (*((short *)(a)))
103
104 /* from misc_util: flip the bytes from x  */
105 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
106
107 Key *add_key(ID *id)    /* common function */
108 {
109         Key *key;
110         char *el;
111         
112         key= alloc_libblock(&G.main->key, ID_KE, "Key");
113         
114         key->type= KEY_NORMAL;
115         key->from= id;
116         
117         // XXX the code here uses some defines which will soon be depreceated...
118         if( GS(id->name)==ID_ME) {
119                 el= key->elemstr;
120                 
121                 el[0]= 3;
122                 el[1]= IPO_FLOAT;
123                 el[2]= 0;
124                 
125                 key->elemsize= 12;
126         }
127         else if( GS(id->name)==ID_LT) {
128                 el= key->elemstr;
129                 
130                 el[0]= 3;
131                 el[1]= IPO_FLOAT;
132                 el[2]= 0;
133                 
134                 key->elemsize= 12;
135         }
136         else if( GS(id->name)==ID_CU) {
137                 el= key->elemstr;
138                 
139                 el[0]= 4;
140                 el[1]= IPO_BPOINT;
141                 el[2]= 0;
142                 
143                 key->elemsize= 16;
144         }
145         
146         return key;
147 }
148
149 Key *copy_key(Key *key)
150 {
151         Key *keyn;
152         KeyBlock *kbn, *kb;
153         
154         if(key==0) return 0;
155         
156         keyn= copy_libblock(key);
157         
158 #if 0 // XXX old animation system
159         keyn->ipo= copy_ipo(key->ipo);
160 #endif // XXX old animation system
161         
162         BLI_duplicatelist(&keyn->block, &key->block);
163         
164         kb= key->block.first;
165         kbn= keyn->block.first;
166         while(kbn) {
167                 
168                 if(kbn->data) kbn->data= MEM_dupallocN(kbn->data);
169                 if( kb==key->refkey ) keyn->refkey= kbn;
170                 
171                 kbn= kbn->next;
172                 kb= kb->next;
173         }
174         
175         return keyn;
176 }
177
178 void make_local_key(Key *key)
179 {
180
181     /* - only lib users: do nothing
182     * - only local users: set flag
183     * - mixed: make copy
184     */
185     if(key==0) return;
186         
187         key->id.lib= 0;
188         new_id(0, (ID *)key, 0);
189
190 #if 0 // XXX old animation system
191         make_local_ipo(key->ipo);
192 #endif // XXX old animation system
193 }
194
195 /* Sort shape keys and Ipo curves after a change.  This assumes that at most
196  * one key was moved, which is a valid assumption for the places it's
197  * currently being called.
198  */
199
200 void sort_keys(Key *key)
201 {
202         KeyBlock *kb;
203         //short i, adrcode;
204         //IpoCurve *icu = NULL;
205         KeyBlock *kb2;
206
207         /* locate the key which is out of position */ 
208         for (kb= key->block.first; kb; kb= kb->next)
209                 if ((kb->next) && (kb->pos > kb->next->pos))
210                         break;
211
212         /* if we find a key, move it */
213         if (kb) {
214                 kb = kb->next; /* next key is the out-of-order one */
215                 BLI_remlink(&key->block, kb);
216                 
217                 /* find the right location and insert before */
218                 for (kb2=key->block.first; kb2; kb2= kb2->next) {
219                         if (kb2->pos > kb->pos) {
220                                 BLI_insertlink(&key->block, kb2->prev, kb);
221                                 break;
222                         }
223                 }
224                 
225                 /* if more than one Ipo curve, see if this key had a curve */
226 #if 0 // XXX old animation system
227                 if(key->ipo && key->ipo->curve.first != key->ipo->curve.last ) {
228                         for(icu= key->ipo->curve.first; icu; icu= icu->next) {
229                                 /* if we find the curve, remove it and reinsert in the 
230                                  right place */
231                                 if(icu->adrcode==kb->adrcode) {
232                                         IpoCurve *icu2;
233                                         BLI_remlink(&key->ipo->curve, icu);
234                                         for(icu2= key->ipo->curve.first; icu2; icu2= icu2->next) {
235                                                 if(icu2->adrcode >= kb2->adrcode) {
236                                                         BLI_insertlink(&key->ipo->curve, icu2->prev, icu);
237                                                         break;
238                                                 }
239                                         }
240                                         break;
241                                 }
242                         }
243                 }
244                 
245                 /* kb points at the moved key, icu at the moved ipo (if it exists).
246                  * go back now and renumber adrcodes */
247
248                 /* first new code */
249                 adrcode = kb2->adrcode;
250                 for (i = kb->adrcode - adrcode; i >= 0; i--, adrcode++) {
251                         /* if the next ipo curve matches the current key, renumber it */
252                         if(icu && icu->adrcode == kb->adrcode ) {
253                                 icu->adrcode = adrcode;
254                                 icu = icu->next;
255                         }
256                         /* renumber the shape key */
257                         kb->adrcode = adrcode;
258                         kb = kb->next;
259                 }
260 #endif // XXX old animation system
261         }
262
263         /* new rule; first key is refkey, this to match drawing channels... */
264         key->refkey= key->block.first;
265 }
266
267 /**************** do the key ****************/
268
269
270 void set_four_ipo(float d, float *data, int type)
271 {
272         float d2, d3, fc;
273         
274         if(type==KEY_LINEAR) {
275                 data[0]= 0.0f;
276                 data[1]= 1.0f-d;
277                 data[2]= d;
278                 data[3]= 0.0f;
279         }
280         else {
281                 d2= d*d;
282                 d3= d2*d;
283                 
284                 if(type==KEY_CARDINAL) {
285
286                         fc= 0.71f;
287                         
288                         data[0]= -fc*d3         +2.0f*fc*d2             -fc*d;
289                         data[1]= (2.0f-fc)*d3   +(fc-3.0f)*d2                           +1.0f;
290                         data[2]= (fc-2.0f)*d3   +(3.0f-2.0f*fc)*d2 +fc*d;
291                         data[3]= fc*d3                  -fc*d2;
292                 }
293                 else if(type==KEY_BSPLINE) {
294
295                         data[0]= -0.16666666f*d3        +0.5f*d2        -0.5f*d +0.16666666f;
296                         data[1]= 0.5f*d3                -d2                             +0.6666666f;
297                         data[2]= -0.5f*d3               +0.5f*d2        +0.5f*d +0.16666666f;
298                         data[3]= 0.16666666f*d3                 ;
299                 }
300         }
301 }
302
303 void set_afgeleide_four_ipo(float d, float *data, int type)
304 {
305         float d2, fc;
306         
307         if(type==KEY_LINEAR) {
308
309         }
310         else {
311                 d2= d*d;
312                 
313                 if(type==KEY_CARDINAL) {
314
315                         fc= 0.71f;
316                         
317                         data[0]= -3.0f*fc*d2            +4.0f*fc*d              -fc;
318                         data[1]= 3.0f*(2.0f-fc)*d2      +2.0f*(fc-3.0f)*d;
319                         data[2]= 3.0f*(fc-2.0f)*d2      +2.0f*(3.0f-2.0f*fc)*d +fc;
320                         data[3]= 3.0f*fc*d2                     -2.0f*fc*d;
321                 }
322                 else if(type==KEY_BSPLINE) {
323
324                         data[0]= -0.16666666f*3.0f*d2   +d      -0.5f;
325                         data[1]= 1.5f*d2                -2.0f*d;
326                         data[2]= -1.5f*d2               +d      +0.5f;
327                         data[3]= 0.16666666f*3.0f*d2                    ;
328                 }
329         }
330 }
331
332 static int setkeys(float fac, ListBase *lb, KeyBlock *k[], float *t, int cycl)
333 {
334         /* return 1 means k[2] is the position, return 0 means interpolate */
335         KeyBlock *k1, *firstkey;
336         float d, dpos, ofs=0, lastpos, temp, fval[4];
337         short bsplinetype;
338
339         firstkey= lb->first;
340         k1= lb->last;
341         lastpos= k1->pos;
342         dpos= lastpos - firstkey->pos;
343
344         if(fac < firstkey->pos) fac= firstkey->pos;
345         else if(fac > k1->pos) fac= k1->pos;
346
347         k1=k[0]=k[1]=k[2]=k[3]= firstkey;
348         t[0]=t[1]=t[2]=t[3]= k1->pos;
349
350         /* if(fac<0.0 || fac>1.0) return 1; */
351
352         if(k1->next==0) return 1;
353
354         if(cycl) {      /* pre-sort */
355                 k[2]= k1->next;
356                 k[3]= k[2]->next;
357                 if(k[3]==0) k[3]=k1;
358                 while(k1) {
359                         if(k1->next==0) k[0]=k1;
360                         k1=k1->next;
361                 }
362                 k1= k[1];
363                 t[0]= k[0]->pos;
364                 t[1]+= dpos;
365                 t[2]= k[2]->pos + dpos;
366                 t[3]= k[3]->pos + dpos;
367                 fac+= dpos;
368                 ofs= dpos;
369                 if(k[3]==k[1]) { 
370                         t[3]+= dpos; 
371                         ofs= 2.0f*dpos;
372                 }
373                 if(fac<t[1]) fac+= dpos;
374                 k1= k[3];
375         }
376         else {          /* pre-sort */
377                 k[2]= k1->next;
378                 t[2]= k[2]->pos;
379                 k[3]= k[2]->next;
380                 if(k[3]==0) k[3]= k[2];
381                 t[3]= k[3]->pos;
382                 k1= k[3];
383         }
384         
385         while( t[2]<fac ) {     /* find correct location */
386                 if(k1->next==0) {
387                         if(cycl) {
388                                 k1= firstkey;
389                                 ofs+= dpos;
390                         }
391                         else if(t[2]==t[3]) break;
392                 }
393                 else k1= k1->next;
394
395                 t[0]= t[1]; 
396                 k[0]= k[1];
397                 t[1]= t[2]; 
398                 k[1]= k[2];
399                 t[2]= t[3]; 
400                 k[2]= k[3];
401                 t[3]= k1->pos+ofs; 
402                 k[3]= k1;
403
404                 if(ofs>2.1+lastpos) break;
405         }
406         
407         bsplinetype= 0;
408         if(k[1]->type==KEY_BSPLINE || k[2]->type==KEY_BSPLINE) bsplinetype= 1;
409
410
411         if(cycl==0) {
412                 if(bsplinetype==0) {    /* B spline doesn't go through the control points */
413                         if(fac<=t[1]) {         /* fac for 1st key */
414                                 t[2]= t[1];
415                                 k[2]= k[1];
416                                 return 1;
417                         }
418                         if(fac>=t[2] ) {        /* fac after 2nd key */
419                                 return 1;
420                         }
421                 }
422                 else if(fac>t[2]) {     /* last key */
423                         fac= t[2];
424                         k[3]= k[2];
425                         t[3]= t[2];
426                 }
427         }
428
429         d= t[2]-t[1];
430         if(d==0.0) {
431                 if(bsplinetype==0) {
432                         return 1;       /* both keys equal */
433                 }
434         }
435         else d= (fac-t[1])/d;
436
437         /* interpolation */
438         
439         set_four_ipo(d, t, k[1]->type);
440
441         if(k[1]->type != k[2]->type) {
442                 set_four_ipo(d, fval, k[2]->type);
443                 
444                 temp= 1.0f-d;
445                 t[0]= temp*t[0]+ d*fval[0];
446                 t[1]= temp*t[1]+ d*fval[1];
447                 t[2]= temp*t[2]+ d*fval[2];
448                 t[3]= temp*t[3]+ d*fval[3];
449         }
450
451         return 0;
452
453 }
454
455 static void flerp(int aantal, float *in, float *f0, float *f1, float *f2, float *f3, float *t)  
456 {
457         int a;
458
459         for(a=0; a<aantal; a++) {
460                 in[a]= t[0]*f0[a]+t[1]*f1[a]+t[2]*f2[a]+t[3]*f3[a];
461         }
462 }
463
464 static void rel_flerp(int aantal, float *in, float *ref, float *out, float fac)
465 {
466         int a;
467         
468         for(a=0; a<aantal; a++) {
469                 in[a]-= fac*(ref[a]-out[a]);
470         }
471 }
472
473 static void cp_key(int start, int end, int tot, char *poin, Key *key, KeyBlock *k, float *weights, int mode)
474 {
475         float ktot = 0.0, kd = 0.0;
476         int elemsize, poinsize = 0, a, *ofsp, ofs[32], flagflo=0;
477         char *k1, *kref;
478         char *cp, elemstr[8];
479
480         if(key->from==NULL) return;
481
482         if( GS(key->from->name)==ID_ME ) {
483                 ofs[0]= sizeof(MVert);
484                 ofs[1]= 0;
485                 poinsize= ofs[0];
486         }
487         else if( GS(key->from->name)==ID_LT ) {
488                 ofs[0]= sizeof(BPoint);
489                 ofs[1]= 0;
490                 poinsize= ofs[0];
491         }
492         else if( GS(key->from->name)==ID_CU ) {
493                 if(mode==KEY_BPOINT) ofs[0]= sizeof(BPoint);
494                 else ofs[0]= sizeof(BezTriple);
495                 
496                 ofs[1]= 0;
497                 poinsize= ofs[0];
498         }
499
500
501         if(end>tot) end= tot;
502         
503         k1= k->data;
504         kref= key->refkey->data;
505         
506         if(tot != k->totelem) {
507                 ktot= 0.0;
508                 flagflo= 1;
509                 if(k->totelem) {
510                         kd= k->totelem/(float)tot;
511                 }
512                 else return;
513         }
514
515         /* this exception is needed for slurphing */
516         if(start!=0) {
517                 
518                 poin+= poinsize*start;
519                 
520                 if(flagflo) {
521                         ktot+= start*kd;
522                         a= (int)floor(ktot);
523                         if(a) {
524                                 ktot-= a;
525                                 k1+= a*key->elemsize;
526                         }
527                 }
528                 else k1+= start*key->elemsize;
529         }       
530         
531         if(mode==KEY_BEZTRIPLE) {
532                 elemstr[0]= 1;
533                 elemstr[1]= IPO_BEZTRIPLE;
534                 elemstr[2]= 0;
535         }
536         
537         /* just do it here, not above! */
538         elemsize= key->elemsize;
539         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
540
541         for(a=start; a<end; a++) {
542                 cp= key->elemstr;
543                 if(mode==KEY_BEZTRIPLE) cp= elemstr;
544
545                 ofsp= ofs;
546                 
547                 while( cp[0] ) {
548                         
549                         switch(cp[1]) {
550                         case IPO_FLOAT:
551                                 
552                                 if(weights) {
553                                         memcpy(poin, kref, sizeof(float)*cp[0]);
554                                         if(*weights!=0.0f)
555                                                 rel_flerp(cp[0], (float *)poin, (float *)kref, (float *)k1, *weights);
556                                         weights++;
557                                 }
558                                 else 
559                                         memcpy(poin, k1, sizeof(float)*cp[0]);
560
561                                 poin+= ofsp[0];
562
563                                 break;
564                         case IPO_BPOINT:
565                                 memcpy(poin, k1, 3*sizeof(float));
566                                 memcpy(poin+4*sizeof(float), k1+3*sizeof(float), sizeof(float));
567                                 
568                                 poin+= ofsp[0];                         
569
570                                 break;
571                         case IPO_BEZTRIPLE:
572                                 memcpy(poin, k1, sizeof(float)*10);
573                                 poin+= ofsp[0]; 
574
575                                 break;
576                         }
577                         
578                         cp+= 2; ofsp++;
579                 }
580                 
581                 /* are we going to be nasty? */
582                 if(flagflo) {
583                         ktot+= kd;
584                         while(ktot>=1.0) {
585                                 ktot-= 1.0;
586                                 k1+= elemsize;
587                                 kref+= elemsize;
588                         }
589                 }
590                 else {
591                         k1+= elemsize;
592                         kref+= elemsize;
593                 }
594                 
595                 if(mode==KEY_BEZTRIPLE) a+=2;
596         }
597 }
598
599 void cp_cu_key(Curve *cu, KeyBlock *kb, int start, int end)
600 {
601         Nurb *nu;
602         int a, step = 0, tot, a1, a2;
603         char *poin;
604
605         tot= count_curveverts(&cu->nurb);
606         nu= cu->nurb.first;
607         a= 0;
608         while(nu) {
609                 if(nu->bp) {
610                         
611                         step= nu->pntsu*nu->pntsv;
612                         
613                         /* exception because keys prefer to work with complete blocks */
614                         poin= (char *)nu->bp->vec;
615                         poin -= a*sizeof(BPoint);
616                         
617                         a1= MAX2(a, start);
618                         a2= MIN2(a+step, end);
619                         
620                         if(a1<a2) cp_key(a1, a2, tot, poin, cu->key, kb, NULL, KEY_BPOINT);
621                 }
622                 else if(nu->bezt) {
623                         
624                         step= 3*nu->pntsu;
625                         
626                         poin= (char *)nu->bezt->vec;
627                         poin -= a*sizeof(BezTriple);
628
629                         a1= MAX2(a, start);
630                         a2= MIN2(a+step, end);
631
632                         if(a1<a2) cp_key(a1, a2, tot, poin, cu->key, kb, NULL, KEY_BEZTRIPLE);
633                         
634                 }
635                 a+= step;
636                 nu=nu->next;
637         }
638 }
639
640
641 void do_rel_key(int start, int end, int tot, char *basispoin, Key *key, int mode)
642 {
643         KeyBlock *kb;
644         int *ofsp, ofs[3], elemsize, b;
645         char *cp, *poin, *reffrom, *from, elemstr[8];
646         
647         if(key->from==NULL) return;
648         
649         if (G.f & G_DEBUG) printf("do_rel_key() \n");
650         
651         if( GS(key->from->name)==ID_ME ) {
652                 ofs[0]= sizeof(MVert);
653                 ofs[1]= 0;
654         }
655         else if( GS(key->from->name)==ID_LT ) {
656                 ofs[0]= sizeof(BPoint);
657                 ofs[1]= 0;
658         }
659         else if( GS(key->from->name)==ID_CU ) {
660                 if(mode==KEY_BPOINT) ofs[0]= sizeof(BPoint);
661                 else ofs[0]= sizeof(BezTriple);
662                 
663                 ofs[1]= 0;
664         }
665         
666         if(end>tot) end= tot;
667         
668         /* in case of beztriple */
669         elemstr[0]= 1;                          /* nr of ipofloats */
670         elemstr[1]= IPO_BEZTRIPLE;
671         elemstr[2]= 0;
672
673         /* just here, not above! */
674         elemsize= key->elemsize;
675         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
676
677         /* step 1 init */
678         cp_key(start, end, tot, basispoin, key, key->refkey, NULL, mode);
679         
680         /* step 2: do it */
681         
682         for(kb=key->block.first; kb; kb=kb->next) {
683                 if(kb!=key->refkey) {
684                         float icuval= kb->curval;
685                         
686                         if (G.f & G_DEBUG) printf("\tdo rel key %s : %s = %f \n", key->id.name+2, kb->name, icuval);
687                         
688                         /* only with value, and no difference allowed */
689                         if(!(kb->flag & KEYBLOCK_MUTE) && icuval!=0.0f && kb->totelem==tot) {
690                                 KeyBlock *refb;
691                                 float weight, *weights= kb->weights;
692                                 
693                                 if (G.f & G_DEBUG) printf("\t\tnot skipped \n");
694                                 
695                                 poin= basispoin;
696                                 from= kb->data;
697                                 /* reference now can be any block */
698                                 refb= BLI_findlink(&key->block, kb->relative);
699                                 if(refb==NULL) continue;
700                                 reffrom= refb->data;
701                                 
702                                 poin+= start*ofs[0];
703                                 reffrom+= key->elemsize*start;  // key elemsize yes!
704                                 from+= key->elemsize*start;
705                                 
706                                 for(b=start; b<end; b++) {
707                                 
708                                         if(weights) 
709                                                 weight= *weights * icuval;
710                                         else
711                                                 weight= icuval;
712                                         
713                                         cp= key->elemstr;       
714                                         if(mode==KEY_BEZTRIPLE) cp= elemstr;
715                                         
716                                         ofsp= ofs;
717                                         
718                                         while( cp[0] ) {        /* cp[0]==amount */
719                                                 
720                                                 switch(cp[1]) {
721                                                 case IPO_FLOAT:
722                                                         rel_flerp(cp[0], (float *)poin, (float *)reffrom, (float *)from, weight);
723                                                         
724                                                         break;
725                                                 case IPO_BPOINT:
726                                                         rel_flerp(3, (float *)poin, (float *)reffrom, (float *)from, icuval);
727                                                         rel_flerp(1, (float *)(poin+16), (float *)(reffrom+16), (float *)(from+16), icuval);
728                         
729                                                         break;
730                                                 case IPO_BEZTRIPLE:
731                                                         rel_flerp(9, (float *)poin, (float *)reffrom, (float *)from, icuval);
732                         
733                                                         break;
734                                                 }
735                                                 
736                                                 poin+= ofsp[0];                         
737                                                 
738                                                 cp+= 2;
739                                                 ofsp++;
740                                         }
741                                         
742                                         reffrom+= elemsize;
743                                         from+= elemsize;
744                                         
745                                         if(mode==KEY_BEZTRIPLE) b+= 2;
746                                         if(weights) weights++;
747                                 }
748                         }
749                 }
750         }
751 }
752
753
754 static void do_key(int start, int end, int tot, char *poin, Key *key, KeyBlock **k, float *t, int mode)
755 {
756         float k1tot = 0.0, k2tot = 0.0, k3tot = 0.0, k4tot = 0.0;
757         float k1d = 0.0, k2d = 0.0, k3d = 0.0, k4d = 0.0;
758         int a, ofs[32], *ofsp;
759         int flagdo= 15, flagflo=0, elemsize, poinsize=0;
760         char *k1, *k2, *k3, *k4;
761         char *cp, elemstr[8];;
762
763         if(key->from==0) return;
764
765         if (G.f & G_DEBUG) printf("do_key() \n");
766         
767         if( GS(key->from->name)==ID_ME ) {
768                 ofs[0]= sizeof(MVert);
769                 ofs[1]= 0;
770                 poinsize= ofs[0];
771         }
772         else if( GS(key->from->name)==ID_LT ) {
773                 ofs[0]= sizeof(BPoint);
774                 ofs[1]= 0;
775                 poinsize= ofs[0];
776         }
777         else if( GS(key->from->name)==ID_CU ) {
778                 if(mode==KEY_BPOINT) ofs[0]= sizeof(BPoint);
779                 else ofs[0]= sizeof(BezTriple);
780                 
781                 ofs[1]= 0;
782                 poinsize= ofs[0];
783         }
784         
785         if(end>tot) end= tot;
786
787         k1= k[0]->data;
788         k2= k[1]->data;
789         k3= k[2]->data;
790         k4= k[3]->data;
791
792         /*  test for more or less points (per key!) */
793         if(tot != k[0]->totelem) {
794                 k1tot= 0.0;
795                 flagflo |= 1;
796                 if(k[0]->totelem) {
797                         k1d= k[0]->totelem/(float)tot;
798                 }
799                 else flagdo -= 1;
800         }
801         if(tot != k[1]->totelem) {
802                 k2tot= 0.0;
803                 flagflo |= 2;
804                 if(k[0]->totelem) {
805                         k2d= k[1]->totelem/(float)tot;
806                 }
807                 else flagdo -= 2;
808         }
809         if(tot != k[2]->totelem) {
810                 k3tot= 0.0;
811                 flagflo |= 4;
812                 if(k[0]->totelem) {
813                         k3d= k[2]->totelem/(float)tot;
814                 }
815                 else flagdo -= 4;
816         }
817         if(tot != k[3]->totelem) {
818                 k4tot= 0.0;
819                 flagflo |= 8;
820                 if(k[0]->totelem) {
821                         k4d= k[3]->totelem/(float)tot;
822                 }
823                 else flagdo -= 8;
824         }
825
826                 /* this exception needed for slurphing */
827         if(start!=0) {
828
829                 poin+= poinsize*start;
830                 
831                 if(flagdo & 1) {
832                         if(flagflo & 1) {
833                                 k1tot+= start*k1d;
834                                 a= (int)floor(k1tot);
835                                 if(a) {
836                                         k1tot-= a;
837                                         k1+= a*key->elemsize;
838                                 }
839                         }
840                         else k1+= start*key->elemsize;
841                 }
842                 if(flagdo & 2) {
843                         if(flagflo & 2) {
844                                 k2tot+= start*k2d;
845                                 a= (int)floor(k2tot);
846                                 if(a) {
847                                         k2tot-= a;
848                                         k2+= a*key->elemsize;
849                                 }
850                         }
851                         else k2+= start*key->elemsize;
852                 }
853                 if(flagdo & 4) {
854                         if(flagflo & 4) {
855                                 k3tot+= start*k3d;
856                                 a= (int)floor(k3tot);
857                                 if(a) {
858                                         k3tot-= a;
859                                         k3+= a*key->elemsize;
860                                 }
861                         }
862                         else k3+= start*key->elemsize;
863                 }
864                 if(flagdo & 8) {
865                         if(flagflo & 8) {
866                                 k4tot+= start*k4d;
867                                 a= (int)floor(k4tot);
868                                 if(a) {
869                                         k4tot-= a;
870                                         k4+= a*key->elemsize;
871                                 }
872                         }
873                         else k4+= start*key->elemsize;
874                 }
875
876         }
877
878         /* in case of beztriple */
879         elemstr[0]= 1;                          /* nr of ipofloats */
880         elemstr[1]= IPO_BEZTRIPLE;
881         elemstr[2]= 0;
882
883         /* only here, not above! */
884         elemsize= key->elemsize;
885         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
886
887         for(a=start; a<end; a++) {
888         
889                 cp= key->elemstr;       
890                 if(mode==KEY_BEZTRIPLE) cp= elemstr;
891                 
892                 ofsp= ofs;
893                 
894                 while( cp[0] ) {        /* cp[0]==amount */
895                         
896                         switch(cp[1]) {
897                         case IPO_FLOAT:
898                                 flerp(cp[0], (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
899                                 poin+= ofsp[0];                         
900
901                                 break;
902                         case IPO_BPOINT:
903                                 flerp(3, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
904                                 flerp(1, (float *)(poin+16), (float *)(k1+12), (float *)(k2+12), (float *)(k3+12), (float *)(k4+12), t);
905                                 
906                                 poin+= ofsp[0];                         
907
908                                 break;
909                         case IPO_BEZTRIPLE:
910                                 flerp(9, (void *)poin, (void *)k1, (void *)k2, (void *)k3, (void *)k4, t);
911                                 flerp(1, (float *)(poin+36), (float *)(k1+36), (float *)(k2+36), (float *)(k3+36), (float *)(k4+36), t);
912                                 poin+= ofsp[0];                         
913
914                                 break;
915                         }
916                         
917
918                         cp+= 2;
919                         ofsp++;
920                 }
921                 /* lets do it the difficult way: when keys have a different size */
922                 if(flagdo & 1) {
923                         if(flagflo & 1) {
924                                 k1tot+= k1d;
925                                 while(k1tot>=1.0) {
926                                         k1tot-= 1.0;
927                                         k1+= elemsize;
928                                 }
929                         }
930                         else k1+= elemsize;
931                 }
932                 if(flagdo & 2) {
933                         if(flagflo & 2) {
934                                 k2tot+= k2d;
935                                 while(k2tot>=1.0) {
936                                         k2tot-= 1.0;
937                                         k2+= elemsize;
938                                 }
939                         }
940                         else k2+= elemsize;
941                 }
942                 if(flagdo & 4) {
943                         if(flagflo & 4) {
944                                 k3tot+= k3d;
945                                 while(k3tot>=1.0) {
946                                         k3tot-= 1.0;
947                                         k3+= elemsize;
948                                 }
949                         }
950                         else k3+= elemsize;
951                 }
952                 if(flagdo & 8) {
953                         if(flagflo & 8) {
954                                 k4tot+= k4d;
955                                 while(k4tot>=1.0) {
956                                         k4tot-= 1.0;
957                                         k4+= elemsize;
958                                 }
959                         }
960                         else k4+= elemsize;
961                 }
962                 
963                 if(mode==KEY_BEZTRIPLE) a+= 2;
964         }
965 }
966
967 static float *get_weights_array(Object *ob, char *vgroup)
968 {
969         bDeformGroup *curdef;
970         MDeformVert *dvert= NULL;
971         int totvert= 0, index= 0;
972         
973         /* no vgroup string set? */
974         if(vgroup[0]==0) return NULL;
975         
976         /* gather dvert and totvert */
977         if(ob->type==OB_MESH) {
978                 Mesh *me= ob->data;
979                 dvert= me->dvert;
980                 totvert= me->totvert;
981         }
982         else if(ob->type==OB_LATTICE) {
983                 Lattice *lt= ob->data;
984                 dvert= lt->dvert;
985                 totvert= lt->pntsu*lt->pntsv*lt->pntsw;
986         }
987         
988         if(dvert==NULL) return NULL;
989         
990         /* find the group (weak loop-in-loop) */
991         for (curdef = ob->defbase.first; curdef; curdef=curdef->next, index++)
992                 if (!strcmp(curdef->name, vgroup))
993                         break;
994
995         if(curdef) {
996                 float *weights;
997                 int i, j;
998                 
999                 weights= MEM_callocN(totvert*sizeof(float), "weights");
1000                 
1001                 for (i=0; i < totvert; i++, dvert++) {
1002                         for(j=0; j<dvert->totweight; j++) {
1003                                 if (dvert->dw[j].def_nr == index) {
1004                                         weights[i]= dvert->dw[j].weight;
1005                                         break;
1006                                 }
1007                         }
1008                 }
1009                 return weights;
1010         }
1011         return NULL;
1012 }
1013
1014 static int do_mesh_key(Scene *scene, Object *ob, Mesh *me)
1015 {
1016         KeyBlock *k[4];
1017         float cfra, ctime, t[4], delta, loc[3], size[3];
1018         int a, flag = 0, step;
1019         
1020         if(me->totvert==0) return 0;
1021         if(me->key==NULL) return 0;
1022         if(me->key->block.first==NULL) return 0;
1023         
1024         /* prevent python from screwing this up? anyhoo, the from pointer could be dropped */
1025         me->key->from= (ID *)me;
1026         
1027         if (G.f & G_DEBUG) printf("do mesh key ob:%s me:%s ke:%s \n", ob->id.name+2, me->id.name+2, me->key->id.name+2);
1028         
1029         if(me->key->slurph && me->key->type!=KEY_RELATIVE ) {
1030                 if (G.f & G_DEBUG) printf("\tslurph key\n");
1031                 
1032                 delta= me->key->slurph;
1033                 delta/= me->totvert;
1034                 
1035                 step= 1;
1036                 if(me->totvert>100 && slurph_opt) {
1037                         step= me->totvert/50;
1038                         delta*= step;
1039                         /* in do_key and cp_key the case a>tot is handled */
1040                 }
1041                 
1042                 cfra= (float)scene->r.cfra;
1043                 
1044                 for(a=0; a<me->totvert; a+=step, cfra+= delta) {
1045                         
1046                         ctime= bsystem_time(scene, 0, cfra, 0.0); // xxx  ugly cruft!
1047 #if 0 // XXX old animation system
1048                         if(calc_ipo_spec(me->key->ipo, KEY_SPEED, &ctime)==0) {
1049                                 ctime /= 100.0;
1050                                 CLAMP(ctime, 0.0, 1.0);
1051                         }
1052 #endif // XXX old animation system
1053                         // XXX for now... since speed curve cannot be directly ported yet
1054                         ctime /= 100.0f;
1055                         CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1056                 
1057                         flag= setkeys(ctime, &me->key->block, k, t, 0);
1058                         if(flag==0) {
1059                                 
1060                                 do_key(a, a+step, me->totvert, (char *)me->mvert->co, me->key, k, t, 0);
1061                         }
1062                         else {
1063                                 cp_key(a, a+step, me->totvert, (char *)me->mvert->co, me->key, k[2], NULL, 0);
1064                         }
1065                 }
1066                 
1067                 if(flag && k[2]==me->key->refkey) tex_space_mesh(me);
1068                 else boundbox_mesh(me, loc, size);
1069         }
1070         else {
1071                 if(me->key->type==KEY_RELATIVE) {
1072                         KeyBlock *kb;
1073                         
1074                         if (G.f & G_DEBUG) printf("\tdo relative \n");
1075                         
1076                         for(kb= me->key->block.first; kb; kb= kb->next)
1077                                 kb->weights= get_weights_array(ob, kb->vgroup);
1078
1079                         do_rel_key(0, me->totvert, me->totvert, (char *)me->mvert->co, me->key, 0);
1080                         
1081                         for(kb= me->key->block.first; kb; kb= kb->next) {
1082                                 if(kb->weights) MEM_freeN(kb->weights);
1083                                 kb->weights= NULL;
1084                         }
1085                 }
1086                 else {
1087                         if (G.f & G_DEBUG) printf("\tdo absolute \n");
1088                         
1089                         ctime= bsystem_time(scene, ob, (float)scene->r.cfra, 0.0f); // xxx old cruft
1090                         
1091 #if 0 // XXX old animation system
1092                         if(calc_ipo_spec(me->key->ipo, KEY_SPEED, &ctime)==0) {
1093                                 ctime /= 100.0;
1094                                 CLAMP(ctime, 0.0, 1.0);
1095                         }
1096 #endif // XXX old animation system
1097                         // XXX for now... since speed curve cannot be directly ported yet
1098                         ctime /= 100.0f;
1099                         CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1100                         
1101                         flag= setkeys(ctime, &me->key->block, k, t, 0);
1102                         if(flag==0) {
1103                                 do_key(0, me->totvert, me->totvert, (char *)me->mvert->co, me->key, k, t, 0);
1104                         }
1105                         else {
1106                                 cp_key(0, me->totvert, me->totvert, (char *)me->mvert->co, me->key, k[2], NULL, 0);
1107                         }
1108                         
1109                         if(flag && k[2]==me->key->refkey) tex_space_mesh(me);
1110                         else boundbox_mesh(me, loc, size);
1111                 }
1112         }
1113         return 1;
1114 }
1115
1116 static void do_cu_key(Curve *cu, KeyBlock **k, float *t)
1117 {
1118         Nurb *nu;
1119         int a, step = 0, tot;
1120         char *poin;
1121         
1122         tot= count_curveverts(&cu->nurb);
1123         nu= cu->nurb.first;
1124         a= 0;
1125         
1126         while(nu) {
1127                 if(nu->bp) {
1128                         
1129                         step= nu->pntsu*nu->pntsv;
1130                         
1131                         /* exception because keys prefer to work with complete blocks */
1132                         poin= (char *)nu->bp->vec;
1133                         poin -= a*sizeof(BPoint);
1134                         
1135                         do_key(a, a+step, tot, poin, cu->key, k, t, KEY_BPOINT);
1136                 }
1137                 else if(nu->bezt) {
1138                         
1139                         step= 3*nu->pntsu;
1140                         
1141                         poin= (char *)nu->bezt->vec;
1142                         poin -= a*sizeof(BezTriple);
1143
1144                         do_key(a, a+step, tot, poin, cu->key, k, t, KEY_BEZTRIPLE);
1145                         
1146                 }
1147                 a+= step;
1148                 nu=nu->next;
1149         }
1150 }
1151
1152 static void do_rel_cu_key(Curve *cu, float ctime)
1153 {
1154         Nurb *nu;
1155         int a, step = 0, tot;
1156         char *poin;
1157         
1158         tot= count_curveverts(&cu->nurb);
1159         nu= cu->nurb.first;
1160         a= 0;
1161         while(nu) {
1162                 if(nu->bp) {
1163                         
1164                         step= nu->pntsu*nu->pntsv;
1165                         
1166                         /* exception because keys prefer to work with complete blocks */
1167                         poin= (char *)nu->bp->vec;
1168                         poin -= a*sizeof(BPoint);
1169                         
1170                         do_rel_key(a, a+step, tot, poin, cu->key, KEY_BPOINT);
1171                 }
1172                 else if(nu->bezt) {
1173                         
1174                         step= 3*nu->pntsu;
1175                         
1176                         poin= (char *)nu->bezt->vec;
1177                         poin -= a*sizeof(BezTriple);
1178
1179                         do_rel_key(a, a+step, tot, poin, cu->key, KEY_BEZTRIPLE);
1180                 }
1181                 a+= step;
1182                 
1183                 nu=nu->next;
1184         }
1185 }
1186
1187 static int do_curve_key(Scene *scene, Curve *cu)
1188 {
1189         KeyBlock *k[4];
1190         float cfra, ctime, t[4], delta;
1191         int a, flag = 0, step = 0, tot;
1192         
1193         tot= count_curveverts(&cu->nurb);
1194         
1195         if(tot==0) return 0;
1196         if(cu->key==NULL) return 0;
1197         if(cu->key->block.first==NULL) return 0;
1198         
1199         if(cu->key->slurph) {
1200                 delta= cu->key->slurph;
1201                 delta/= tot;
1202                 
1203                 step= 1;
1204                 if(tot>100 && slurph_opt) {
1205                         step= tot/50;
1206                         delta*= step;
1207                         /* in do_key and cp_key the case a>tot has been handled */
1208                 }
1209                 
1210                 cfra= (float)scene->r.cfra;
1211                 
1212                 for(a=0; a<tot; a+=step, cfra+= delta) {
1213                         ctime= bsystem_time(scene, 0, cfra, 0.0f); // XXX old cruft
1214 #if 0 // XXX old animation system
1215                         if(calc_ipo_spec(cu->key->ipo, KEY_SPEED, &ctime)==0) {
1216                                 ctime /= 100.0;
1217                                 CLAMP(ctime, 0.0, 1.0);
1218                         }
1219 #endif // XXX old animation system
1220                 
1221                         flag= setkeys(ctime, &cu->key->block, k, t, 0);
1222                         if(flag==0) {
1223                                 
1224                                 /* do_key(a, a+step, tot, (char *)cu->mvert->co, cu->key, k, t, 0); */
1225                         }
1226                         else {
1227                                 /* cp_key(a, a+step, tot, (char *)cu->mvert->co, cu->key, k[2],0); */
1228                         }
1229                 }
1230
1231                 if(flag && k[2]==cu->key->refkey) tex_space_curve(cu);
1232                 
1233                 
1234         }
1235         else {
1236                 
1237                 ctime= bsystem_time(scene, NULL, (float)scene->r.cfra, 0.0);
1238                 
1239                 if(cu->key->type==KEY_RELATIVE) {
1240                         do_rel_cu_key(cu, ctime);
1241                 }
1242                 else {
1243 #if 0 // XXX old animation system
1244                         if(calc_ipo_spec(cu->key->ipo, KEY_SPEED, &ctime)==0) {
1245                                 ctime /= 100.0;
1246                                 CLAMP(ctime, 0.0, 1.0);
1247                         }
1248 #endif // XXX old animation system
1249                         
1250                         flag= setkeys(ctime, &cu->key->block, k, t, 0);
1251                         
1252                         if(flag==0) do_cu_key(cu, k, t);
1253                         else cp_cu_key(cu, k[2], 0, tot);
1254                                         
1255                         if(flag && k[2]==cu->key->refkey) tex_space_curve(cu);
1256                 }
1257         }
1258         
1259         return 1;
1260 }
1261
1262 static int do_latt_key(Scene *scene, Object *ob, Lattice *lt)
1263 {
1264         KeyBlock *k[4];
1265         float delta, cfra, ctime, t[4];
1266         int a, tot, flag;
1267         
1268         if(lt->key==NULL) return 0;
1269         if(lt->key->block.first==NULL) return 0;
1270
1271         tot= lt->pntsu*lt->pntsv*lt->pntsw;
1272
1273         if(lt->key->slurph) {
1274                 delta= lt->key->slurph;
1275                 delta/= (float)tot;
1276                 
1277                 cfra= (float)scene->r.cfra;
1278                 
1279                 for(a=0; a<tot; a++, cfra+= delta) {
1280                         
1281                         ctime= bsystem_time(scene, 0, cfra, 0.0); // XXX old cruft
1282 #if 0 // XXX old animation system
1283                         if(calc_ipo_spec(lt->key->ipo, KEY_SPEED, &ctime)==0) {
1284                                 ctime /= 100.0;
1285                                 CLAMP(ctime, 0.0, 1.0);
1286                         }
1287 #endif // XXX old animation system
1288                 
1289                         flag= setkeys(ctime, &lt->key->block, k, t, 0);
1290                         if(flag==0) {
1291                                 
1292                                 do_key(a, a+1, tot, (char *)lt->def->vec, lt->key, k, t, 0);
1293                         }
1294                         else {
1295                                 cp_key(a, a+1, tot, (char *)lt->def->vec, lt->key, k[2], NULL, 0);
1296                         }
1297                 }               
1298         }
1299         else {
1300                 ctime= bsystem_time(scene, NULL, (float)scene->r.cfra, 0.0);
1301         
1302                 if(lt->key->type==KEY_RELATIVE) {
1303                         KeyBlock *kb;
1304                         
1305                         for(kb= lt->key->block.first; kb; kb= kb->next)
1306                                 kb->weights= get_weights_array(ob, kb->vgroup);
1307                         
1308                         do_rel_key(0, tot, tot, (char *)lt->def->vec, lt->key, 0);
1309                         
1310                         for(kb= lt->key->block.first; kb; kb= kb->next) {
1311                                 if(kb->weights) MEM_freeN(kb->weights);
1312                                 kb->weights= NULL;
1313                         }
1314                 }
1315                 else {
1316 #if 0 // XXX old animation system
1317                         if(calc_ipo_spec(lt->key->ipo, KEY_SPEED, &ctime)==0) {
1318                                 ctime /= 100.0;
1319                                 CLAMP(ctime, 0.0, 1.0);
1320                         }
1321 #endif // XXX old animation system
1322                         
1323                         flag= setkeys(ctime, &lt->key->block, k, t, 0);
1324                         if(flag==0) {
1325                                 do_key(0, tot, tot, (char *)lt->def->vec, lt->key, k, t, 0);
1326                         }
1327                         else {
1328                                 cp_key(0, tot, tot, (char *)lt->def->vec, lt->key, k[2], NULL, 0);
1329                         }
1330                 }
1331         }
1332         
1333         if(lt->flag & LT_OUTSIDE) outside_lattice(lt);
1334         
1335         return 1;
1336 }
1337
1338 /* returns 1 when key applied */
1339 int do_ob_key(Scene *scene, Object *ob)
1340 {
1341         Key *key= ob_get_key(ob);
1342         
1343         if(key==NULL)
1344                 return 0;
1345                 
1346         if(ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)) {
1347                 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1348                 
1349                 if (G.f & G_DEBUG) printf("ob %s, key %s locked \n", ob->id.name+2, key->id.name+2);
1350                 
1351                 if(kb && (kb->flag & KEYBLOCK_MUTE))
1352                         kb= key->refkey;
1353
1354                 if(kb==NULL) {
1355                         kb= key->block.first;
1356                         ob->shapenr= 1;
1357                 }
1358                 
1359                 if(ob->type==OB_MESH) {
1360                         Mesh *me= ob->data;
1361                         float *weights= get_weights_array(ob, kb->vgroup);
1362
1363                         cp_key(0, me->totvert, me->totvert, (char *)me->mvert->co, key, kb, weights, 0);
1364                         
1365                         if(weights) MEM_freeN(weights);
1366                 }
1367                 else if(ob->type==OB_LATTICE) {
1368                         Lattice *lt= ob->data;
1369                         float *weights= get_weights_array(ob, kb->vgroup);
1370                         int tot= lt->pntsu*lt->pntsv*lt->pntsw;
1371                         
1372                         cp_key(0, tot, tot, (char *)lt->def->vec, key, kb, weights, 0);
1373                         
1374                         if(weights) MEM_freeN(weights);
1375                 }
1376                 else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1377                         Curve *cu= ob->data;
1378                         int tot= count_curveverts(&cu->nurb);
1379                         
1380                         cp_cu_key(cu, kb, 0, tot);
1381                 }
1382                 return 1;
1383         }
1384         else {
1385                 /* do shapekey local drivers */
1386                 float ctime= (float)scene->r.cfra; // XXX this needs to be checked
1387                 
1388                 if (G.f & G_DEBUG) 
1389                         printf("ob %s - do shapekey (%s) drivers \n", ob->id.name+2, key->id.name+2);
1390                 BKE_animsys_evaluate_animdata(&key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
1391                 
1392                 if(ob->type==OB_MESH) return do_mesh_key(scene, ob, ob->data);
1393                 else if(ob->type==OB_CURVE) return do_curve_key(scene, ob->data);
1394                 else if(ob->type==OB_SURF) return do_curve_key(scene, ob->data);
1395                 else if(ob->type==OB_LATTICE) return do_latt_key(scene, ob, ob->data);
1396         }
1397         
1398         return 0;
1399 }
1400
1401 Key *ob_get_key(Object *ob)
1402 {
1403         if(ob==NULL) return NULL;
1404         
1405         if(ob->type==OB_MESH) {
1406                 Mesh *me= ob->data;
1407                 return me->key;
1408         }
1409         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1410                 Curve *cu= ob->data;
1411                 return cu->key;
1412         }
1413         else if(ob->type==OB_LATTICE) {
1414                 Lattice *lt= ob->data;
1415                 return lt->key;
1416         }
1417         return NULL;
1418 }
1419
1420 /* only the active keyblock */
1421 KeyBlock *ob_get_keyblock(Object *ob) 
1422 {
1423         Key *key= ob_get_key(ob);
1424         
1425         if (key) {
1426                 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1427                 return kb;
1428         }
1429
1430         return NULL;
1431 }
1432
1433 /* get the appropriate KeyBlock given an index */
1434 KeyBlock *key_get_keyblock(Key *key, int index)
1435 {
1436         KeyBlock *kb;
1437         int i;
1438         
1439         if (key) {
1440                 kb= key->block.first;
1441                 
1442                 for (i= 1; i < key->totkey; i++) {
1443                         kb= kb->next;
1444                         
1445                         if (index==i)
1446                                 return kb;
1447                 }
1448         }
1449         
1450         return NULL;
1451 }
1452
1453 /* get the appropriate KeyBlock given a name to search for */
1454 KeyBlock *key_get_named_keyblock(Key *key, const char name[])
1455 {
1456         KeyBlock *kb;
1457         
1458         if (key && name) {
1459                 for (kb= key->block.first; kb; kb= kb->next) {
1460                         if (strcmp(name, kb->name)==0)
1461                                 return kb;
1462                 }
1463         }
1464         
1465         return NULL;
1466 }