b6e4ffd6cc3861f7f501c4685893e02ad8c1af8c
[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 void key_curve_position_weights(float t, float *data, int type)
270 {
271         float t2, t3, fc;
272         
273         if(type==KEY_LINEAR) {
274                 data[0]=                  0.0f;
275                 data[1]= -t             + 1.0f;
276                 data[2]= t;
277                 data[3]=                  0.0f;
278         }
279         else if(type==KEY_CARDINAL) {
280                 t2= t*t;
281                 t3= t2*t;
282                 fc= 0.71f;
283                 
284                 data[0]= -fc*t3                 + 2.0f*fc*t2            - fc*t;
285                 data[1]= (2.0f-fc)*t3   + (fc-3.0f)*t2                                  + 1.0f;
286                 data[2]= (fc-2.0f)*t3   + (3.0f-2.0f*fc)*t2     + fc*t;
287                 data[3]= fc*t3                  - fc*t2;
288         }
289         else if(type==KEY_BSPLINE) {
290                 t2= t*t;
291                 t3= t2*t;
292
293                 data[0]= -0.16666666f*t3        + 0.5f*t2       - 0.5f*t        + 0.16666666f;
294                 data[1]= 0.5f*t3                        - t2                                    + 0.6666666f;
295                 data[2]= -0.5f*t3                       + 0.5f*t2       + 0.5f*t        + 0.16666666f;
296                 data[3]= 0.16666666f*t3;
297         }
298 }
299
300 /* first derivative */
301 void key_curve_tangent_weights(float t, float *data, int type)
302 {
303         float t2, fc;
304         
305         if(type==KEY_LINEAR) {
306                 data[0]= 0.0f;
307                 data[1]= -1.0f;
308                 data[2]= 1.0f;
309                 data[3]= 0.0f;
310         }
311         else if(type==KEY_CARDINAL) {
312                 t2= t*t;
313                 fc= 0.71f;
314                 
315                 data[0]= -3.0f*fc*t2            +4.0f*fc*t                              - fc;
316                 data[1]= 3.0f*(2.0f-fc)*t2      +2.0f*(fc-3.0f)*t;
317                 data[2]= 3.0f*(fc-2.0f)*t2      +2.0f*(3.0f-2.0f*fc)*t  + fc;
318                 data[3]= 3.0f*fc*t2                     -2.0f*fc*t;
319         }
320         else if(type==KEY_BSPLINE) {
321                 t2= t*t;
322
323                 data[0]= -0.5f*t2       + t                     - 0.5f;
324                 data[1]= 1.5f*t2        - 2.0f*t;
325                 data[2]= -1.5f*t2       + t                     + 0.5f;
326                 data[3]= 0.5f*t2;
327         }
328 }
329
330 /* second derivative */
331 void key_curve_normal_weights(float t, float *data, int type)
332 {
333         float fc;
334         
335         if(type==KEY_LINEAR) {
336                 data[0]= 0.0f;
337                 data[1]= 0.0f;
338                 data[2]= 0.0f;
339                 data[3]= 0.0f;
340         }
341         else if(type==KEY_CARDINAL) {
342                 fc= 0.71f;
343                 
344                 data[0]= -6.0f*fc*t                     + 4.0f*fc;
345                 data[1]= 6.0f*(2.0f-fc)*t       + 2.0f*(fc-3.0f);
346                 data[2]= 6.0f*(fc-2.0f)*t       + 2.0f*(3.0f-2.0f*fc);
347                 data[3]= 6.0f*fc*t                      - 2.0f*fc;
348         }
349         else if(type==KEY_BSPLINE) {
350                 data[0]= -1.0f*t        + 1.0f;
351                 data[1]= 3.0f*t         - 2.0f;
352                 data[2]= -3.0f*t        + 1.0f;
353                 data[3]= 1.0f*t;
354         }
355 }
356
357 static int setkeys(float fac, ListBase *lb, KeyBlock *k[], float *t, int cycl)
358 {
359         /* return 1 means k[2] is the position, return 0 means interpolate */
360         KeyBlock *k1, *firstkey;
361         float d, dpos, ofs=0, lastpos, temp, fval[4];
362         short bsplinetype;
363
364         firstkey= lb->first;
365         k1= lb->last;
366         lastpos= k1->pos;
367         dpos= lastpos - firstkey->pos;
368
369         if(fac < firstkey->pos) fac= firstkey->pos;
370         else if(fac > k1->pos) fac= k1->pos;
371
372         k1=k[0]=k[1]=k[2]=k[3]= firstkey;
373         t[0]=t[1]=t[2]=t[3]= k1->pos;
374
375         /* if(fac<0.0 || fac>1.0) return 1; */
376
377         if(k1->next==0) return 1;
378
379         if(cycl) {      /* pre-sort */
380                 k[2]= k1->next;
381                 k[3]= k[2]->next;
382                 if(k[3]==0) k[3]=k1;
383                 while(k1) {
384                         if(k1->next==0) k[0]=k1;
385                         k1=k1->next;
386                 }
387                 k1= k[1];
388                 t[0]= k[0]->pos;
389                 t[1]+= dpos;
390                 t[2]= k[2]->pos + dpos;
391                 t[3]= k[3]->pos + dpos;
392                 fac+= dpos;
393                 ofs= dpos;
394                 if(k[3]==k[1]) { 
395                         t[3]+= dpos; 
396                         ofs= 2.0f*dpos;
397                 }
398                 if(fac<t[1]) fac+= dpos;
399                 k1= k[3];
400         }
401         else {          /* pre-sort */
402                 k[2]= k1->next;
403                 t[2]= k[2]->pos;
404                 k[3]= k[2]->next;
405                 if(k[3]==0) k[3]= k[2];
406                 t[3]= k[3]->pos;
407                 k1= k[3];
408         }
409         
410         while( t[2]<fac ) {     /* find correct location */
411                 if(k1->next==0) {
412                         if(cycl) {
413                                 k1= firstkey;
414                                 ofs+= dpos;
415                         }
416                         else if(t[2]==t[3]) break;
417                 }
418                 else k1= k1->next;
419
420                 t[0]= t[1]; 
421                 k[0]= k[1];
422                 t[1]= t[2]; 
423                 k[1]= k[2];
424                 t[2]= t[3]; 
425                 k[2]= k[3];
426                 t[3]= k1->pos+ofs; 
427                 k[3]= k1;
428
429                 if(ofs>2.1+lastpos) break;
430         }
431         
432         bsplinetype= 0;
433         if(k[1]->type==KEY_BSPLINE || k[2]->type==KEY_BSPLINE) bsplinetype= 1;
434
435
436         if(cycl==0) {
437                 if(bsplinetype==0) {    /* B spline doesn't go through the control points */
438                         if(fac<=t[1]) {         /* fac for 1st key */
439                                 t[2]= t[1];
440                                 k[2]= k[1];
441                                 return 1;
442                         }
443                         if(fac>=t[2] ) {        /* fac after 2nd key */
444                                 return 1;
445                         }
446                 }
447                 else if(fac>t[2]) {     /* last key */
448                         fac= t[2];
449                         k[3]= k[2];
450                         t[3]= t[2];
451                 }
452         }
453
454         d= t[2]-t[1];
455         if(d==0.0) {
456                 if(bsplinetype==0) {
457                         return 1;       /* both keys equal */
458                 }
459         }
460         else d= (fac-t[1])/d;
461
462         /* interpolation */
463         
464         key_curve_position_weights(d, t, k[1]->type);
465
466         if(k[1]->type != k[2]->type) {
467                 key_curve_position_weights(d, fval, k[2]->type);
468                 
469                 temp= 1.0f-d;
470                 t[0]= temp*t[0]+ d*fval[0];
471                 t[1]= temp*t[1]+ d*fval[1];
472                 t[2]= temp*t[2]+ d*fval[2];
473                 t[3]= temp*t[3]+ d*fval[3];
474         }
475
476         return 0;
477
478 }
479
480 static void flerp(int aantal, float *in, float *f0, float *f1, float *f2, float *f3, float *t)  
481 {
482         int a;
483
484         for(a=0; a<aantal; a++) {
485                 in[a]= t[0]*f0[a]+t[1]*f1[a]+t[2]*f2[a]+t[3]*f3[a];
486         }
487 }
488
489 static void rel_flerp(int aantal, float *in, float *ref, float *out, float fac)
490 {
491         int a;
492         
493         for(a=0; a<aantal; a++) {
494                 in[a]-= fac*(ref[a]-out[a]);
495         }
496 }
497
498 static void cp_key(int start, int end, int tot, char *poin, Key *key, KeyBlock *k, float *weights, int mode)
499 {
500         float ktot = 0.0, kd = 0.0;
501         int elemsize, poinsize = 0, a, *ofsp, ofs[32], flagflo=0;
502         char *k1, *kref;
503         char *cp, elemstr[8];
504
505         if(key->from==NULL) return;
506
507         if( GS(key->from->name)==ID_ME ) {
508                 ofs[0]= sizeof(MVert);
509                 ofs[1]= 0;
510                 poinsize= ofs[0];
511         }
512         else if( GS(key->from->name)==ID_LT ) {
513                 ofs[0]= sizeof(BPoint);
514                 ofs[1]= 0;
515                 poinsize= ofs[0];
516         }
517         else if( GS(key->from->name)==ID_CU ) {
518                 if(mode==KEY_BPOINT) ofs[0]= sizeof(BPoint);
519                 else ofs[0]= sizeof(BezTriple);
520                 
521                 ofs[1]= 0;
522                 poinsize= ofs[0];
523         }
524
525
526         if(end>tot) end= tot;
527         
528         k1= k->data;
529         kref= key->refkey->data;
530         
531         if(tot != k->totelem) {
532                 ktot= 0.0;
533                 flagflo= 1;
534                 if(k->totelem) {
535                         kd= k->totelem/(float)tot;
536                 }
537                 else return;
538         }
539
540         /* this exception is needed for slurphing */
541         if(start!=0) {
542                 
543                 poin+= poinsize*start;
544                 
545                 if(flagflo) {
546                         ktot+= start*kd;
547                         a= (int)floor(ktot);
548                         if(a) {
549                                 ktot-= a;
550                                 k1+= a*key->elemsize;
551                         }
552                 }
553                 else k1+= start*key->elemsize;
554         }       
555         
556         if(mode==KEY_BEZTRIPLE) {
557                 elemstr[0]= 1;
558                 elemstr[1]= IPO_BEZTRIPLE;
559                 elemstr[2]= 0;
560         }
561         
562         /* just do it here, not above! */
563         elemsize= key->elemsize;
564         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
565
566         for(a=start; a<end; a++) {
567                 cp= key->elemstr;
568                 if(mode==KEY_BEZTRIPLE) cp= elemstr;
569
570                 ofsp= ofs;
571                 
572                 while( cp[0] ) {
573                         
574                         switch(cp[1]) {
575                         case IPO_FLOAT:
576                                 
577                                 if(weights) {
578                                         memcpy(poin, kref, sizeof(float)*cp[0]);
579                                         if(*weights!=0.0f)
580                                                 rel_flerp(cp[0], (float *)poin, (float *)kref, (float *)k1, *weights);
581                                         weights++;
582                                 }
583                                 else 
584                                         memcpy(poin, k1, sizeof(float)*cp[0]);
585
586                                 poin+= ofsp[0];
587
588                                 break;
589                         case IPO_BPOINT:
590                                 memcpy(poin, k1, 3*sizeof(float));
591                                 memcpy(poin+4*sizeof(float), k1+3*sizeof(float), sizeof(float));
592                                 
593                                 poin+= ofsp[0];                         
594
595                                 break;
596                         case IPO_BEZTRIPLE:
597                                 memcpy(poin, k1, sizeof(float)*10);
598                                 poin+= ofsp[0]; 
599
600                                 break;
601                         }
602                         
603                         cp+= 2; ofsp++;
604                 }
605                 
606                 /* are we going to be nasty? */
607                 if(flagflo) {
608                         ktot+= kd;
609                         while(ktot>=1.0) {
610                                 ktot-= 1.0;
611                                 k1+= elemsize;
612                                 kref+= elemsize;
613                         }
614                 }
615                 else {
616                         k1+= elemsize;
617                         kref+= elemsize;
618                 }
619                 
620                 if(mode==KEY_BEZTRIPLE) a+=2;
621         }
622 }
623
624 void cp_cu_key(Curve *cu, KeyBlock *kb, int start, int end)
625 {
626         Nurb *nu;
627         int a, step = 0, tot, a1, a2;
628         char *poin;
629
630         tot= count_curveverts(&cu->nurb);
631         nu= cu->nurb.first;
632         a= 0;
633         while(nu) {
634                 if(nu->bp) {
635                         
636                         step= nu->pntsu*nu->pntsv;
637                         
638                         /* exception because keys prefer to work with complete blocks */
639                         poin= (char *)nu->bp->vec;
640                         poin -= a*sizeof(BPoint);
641                         
642                         a1= MAX2(a, start);
643                         a2= MIN2(a+step, end);
644                         
645                         if(a1<a2) cp_key(a1, a2, tot, poin, cu->key, kb, NULL, KEY_BPOINT);
646                 }
647                 else if(nu->bezt) {
648                         
649                         step= 3*nu->pntsu;
650                         
651                         poin= (char *)nu->bezt->vec;
652                         poin -= a*sizeof(BezTriple);
653
654                         a1= MAX2(a, start);
655                         a2= MIN2(a+step, end);
656
657                         if(a1<a2) cp_key(a1, a2, tot, poin, cu->key, kb, NULL, KEY_BEZTRIPLE);
658                         
659                 }
660                 a+= step;
661                 nu=nu->next;
662         }
663 }
664
665
666 void do_rel_key(int start, int end, int tot, char *basispoin, Key *key, int mode)
667 {
668         KeyBlock *kb;
669         int *ofsp, ofs[3], elemsize, b;
670         char *cp, *poin, *reffrom, *from, elemstr[8];
671         
672         if(key->from==NULL) return;
673         
674         if (G.f & G_DEBUG) printf("do_rel_key() \n");
675         
676         if( GS(key->from->name)==ID_ME ) {
677                 ofs[0]= sizeof(MVert);
678                 ofs[1]= 0;
679         }
680         else if( GS(key->from->name)==ID_LT ) {
681                 ofs[0]= sizeof(BPoint);
682                 ofs[1]= 0;
683         }
684         else if( GS(key->from->name)==ID_CU ) {
685                 if(mode==KEY_BPOINT) ofs[0]= sizeof(BPoint);
686                 else ofs[0]= sizeof(BezTriple);
687                 
688                 ofs[1]= 0;
689         }
690         
691         if(end>tot) end= tot;
692         
693         /* in case of beztriple */
694         elemstr[0]= 1;                          /* nr of ipofloats */
695         elemstr[1]= IPO_BEZTRIPLE;
696         elemstr[2]= 0;
697
698         /* just here, not above! */
699         elemsize= key->elemsize;
700         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
701
702         /* step 1 init */
703         cp_key(start, end, tot, basispoin, key, key->refkey, NULL, mode);
704         
705         /* step 2: do it */
706         
707         for(kb=key->block.first; kb; kb=kb->next) {
708                 if(kb!=key->refkey) {
709                         float icuval= kb->curval;
710                         
711                         if (G.f & G_DEBUG) printf("\tdo rel key %s : %s = %f \n", key->id.name+2, kb->name, icuval);
712                         
713                         /* only with value, and no difference allowed */
714                         if(!(kb->flag & KEYBLOCK_MUTE) && icuval!=0.0f && kb->totelem==tot) {
715                                 KeyBlock *refb;
716                                 float weight, *weights= kb->weights;
717                                 
718                                 if (G.f & G_DEBUG) printf("\t\tnot skipped \n");
719                                 
720                                 poin= basispoin;
721                                 from= kb->data;
722                                 /* reference now can be any block */
723                                 refb= BLI_findlink(&key->block, kb->relative);
724                                 if(refb==NULL) continue;
725                                 reffrom= refb->data;
726                                 
727                                 poin+= start*ofs[0];
728                                 reffrom+= key->elemsize*start;  // key elemsize yes!
729                                 from+= key->elemsize*start;
730                                 
731                                 for(b=start; b<end; b++) {
732                                 
733                                         if(weights) 
734                                                 weight= *weights * icuval;
735                                         else
736                                                 weight= icuval;
737                                         
738                                         cp= key->elemstr;       
739                                         if(mode==KEY_BEZTRIPLE) cp= elemstr;
740                                         
741                                         ofsp= ofs;
742                                         
743                                         while( cp[0] ) {        /* cp[0]==amount */
744                                                 
745                                                 switch(cp[1]) {
746                                                 case IPO_FLOAT:
747                                                         rel_flerp(cp[0], (float *)poin, (float *)reffrom, (float *)from, weight);
748                                                         
749                                                         break;
750                                                 case IPO_BPOINT:
751                                                         rel_flerp(3, (float *)poin, (float *)reffrom, (float *)from, icuval);
752                                                         rel_flerp(1, (float *)(poin+16), (float *)(reffrom+16), (float *)(from+16), icuval);
753                         
754                                                         break;
755                                                 case IPO_BEZTRIPLE:
756                                                         rel_flerp(9, (float *)poin, (float *)reffrom, (float *)from, icuval);
757                         
758                                                         break;
759                                                 }
760                                                 
761                                                 poin+= ofsp[0];                         
762                                                 
763                                                 cp+= 2;
764                                                 ofsp++;
765                                         }
766                                         
767                                         reffrom+= elemsize;
768                                         from+= elemsize;
769                                         
770                                         if(mode==KEY_BEZTRIPLE) b+= 2;
771                                         if(weights) weights++;
772                                 }
773                         }
774                 }
775         }
776 }
777
778
779 static void do_key(int start, int end, int tot, char *poin, Key *key, KeyBlock **k, float *t, int mode)
780 {
781         float k1tot = 0.0, k2tot = 0.0, k3tot = 0.0, k4tot = 0.0;
782         float k1d = 0.0, k2d = 0.0, k3d = 0.0, k4d = 0.0;
783         int a, ofs[32], *ofsp;
784         int flagdo= 15, flagflo=0, elemsize, poinsize=0;
785         char *k1, *k2, *k3, *k4;
786         char *cp, elemstr[8];;
787
788         if(key->from==0) return;
789
790         if (G.f & G_DEBUG) printf("do_key() \n");
791         
792         if( GS(key->from->name)==ID_ME ) {
793                 ofs[0]= sizeof(MVert);
794                 ofs[1]= 0;
795                 poinsize= ofs[0];
796         }
797         else if( GS(key->from->name)==ID_LT ) {
798                 ofs[0]= sizeof(BPoint);
799                 ofs[1]= 0;
800                 poinsize= ofs[0];
801         }
802         else if( GS(key->from->name)==ID_CU ) {
803                 if(mode==KEY_BPOINT) ofs[0]= sizeof(BPoint);
804                 else ofs[0]= sizeof(BezTriple);
805                 
806                 ofs[1]= 0;
807                 poinsize= ofs[0];
808         }
809         
810         if(end>tot) end= tot;
811
812         k1= k[0]->data;
813         k2= k[1]->data;
814         k3= k[2]->data;
815         k4= k[3]->data;
816
817         /*  test for more or less points (per key!) */
818         if(tot != k[0]->totelem) {
819                 k1tot= 0.0;
820                 flagflo |= 1;
821                 if(k[0]->totelem) {
822                         k1d= k[0]->totelem/(float)tot;
823                 }
824                 else flagdo -= 1;
825         }
826         if(tot != k[1]->totelem) {
827                 k2tot= 0.0;
828                 flagflo |= 2;
829                 if(k[0]->totelem) {
830                         k2d= k[1]->totelem/(float)tot;
831                 }
832                 else flagdo -= 2;
833         }
834         if(tot != k[2]->totelem) {
835                 k3tot= 0.0;
836                 flagflo |= 4;
837                 if(k[0]->totelem) {
838                         k3d= k[2]->totelem/(float)tot;
839                 }
840                 else flagdo -= 4;
841         }
842         if(tot != k[3]->totelem) {
843                 k4tot= 0.0;
844                 flagflo |= 8;
845                 if(k[0]->totelem) {
846                         k4d= k[3]->totelem/(float)tot;
847                 }
848                 else flagdo -= 8;
849         }
850
851                 /* this exception needed for slurphing */
852         if(start!=0) {
853
854                 poin+= poinsize*start;
855                 
856                 if(flagdo & 1) {
857                         if(flagflo & 1) {
858                                 k1tot+= start*k1d;
859                                 a= (int)floor(k1tot);
860                                 if(a) {
861                                         k1tot-= a;
862                                         k1+= a*key->elemsize;
863                                 }
864                         }
865                         else k1+= start*key->elemsize;
866                 }
867                 if(flagdo & 2) {
868                         if(flagflo & 2) {
869                                 k2tot+= start*k2d;
870                                 a= (int)floor(k2tot);
871                                 if(a) {
872                                         k2tot-= a;
873                                         k2+= a*key->elemsize;
874                                 }
875                         }
876                         else k2+= start*key->elemsize;
877                 }
878                 if(flagdo & 4) {
879                         if(flagflo & 4) {
880                                 k3tot+= start*k3d;
881                                 a= (int)floor(k3tot);
882                                 if(a) {
883                                         k3tot-= a;
884                                         k3+= a*key->elemsize;
885                                 }
886                         }
887                         else k3+= start*key->elemsize;
888                 }
889                 if(flagdo & 8) {
890                         if(flagflo & 8) {
891                                 k4tot+= start*k4d;
892                                 a= (int)floor(k4tot);
893                                 if(a) {
894                                         k4tot-= a;
895                                         k4+= a*key->elemsize;
896                                 }
897                         }
898                         else k4+= start*key->elemsize;
899                 }
900
901         }
902
903         /* in case of beztriple */
904         elemstr[0]= 1;                          /* nr of ipofloats */
905         elemstr[1]= IPO_BEZTRIPLE;
906         elemstr[2]= 0;
907
908         /* only here, not above! */
909         elemsize= key->elemsize;
910         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
911
912         for(a=start; a<end; a++) {
913         
914                 cp= key->elemstr;       
915                 if(mode==KEY_BEZTRIPLE) cp= elemstr;
916                 
917                 ofsp= ofs;
918                 
919                 while( cp[0] ) {        /* cp[0]==amount */
920                         
921                         switch(cp[1]) {
922                         case IPO_FLOAT:
923                                 flerp(cp[0], (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
924                                 poin+= ofsp[0];                         
925
926                                 break;
927                         case IPO_BPOINT:
928                                 flerp(3, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
929                                 flerp(1, (float *)(poin+16), (float *)(k1+12), (float *)(k2+12), (float *)(k3+12), (float *)(k4+12), t);
930                                 
931                                 poin+= ofsp[0];                         
932
933                                 break;
934                         case IPO_BEZTRIPLE:
935                                 flerp(9, (void *)poin, (void *)k1, (void *)k2, (void *)k3, (void *)k4, t);
936                                 flerp(1, (float *)(poin+36), (float *)(k1+36), (float *)(k2+36), (float *)(k3+36), (float *)(k4+36), t);
937                                 poin+= ofsp[0];                         
938
939                                 break;
940                         }
941                         
942
943                         cp+= 2;
944                         ofsp++;
945                 }
946                 /* lets do it the difficult way: when keys have a different size */
947                 if(flagdo & 1) {
948                         if(flagflo & 1) {
949                                 k1tot+= k1d;
950                                 while(k1tot>=1.0) {
951                                         k1tot-= 1.0;
952                                         k1+= elemsize;
953                                 }
954                         }
955                         else k1+= elemsize;
956                 }
957                 if(flagdo & 2) {
958                         if(flagflo & 2) {
959                                 k2tot+= k2d;
960                                 while(k2tot>=1.0) {
961                                         k2tot-= 1.0;
962                                         k2+= elemsize;
963                                 }
964                         }
965                         else k2+= elemsize;
966                 }
967                 if(flagdo & 4) {
968                         if(flagflo & 4) {
969                                 k3tot+= k3d;
970                                 while(k3tot>=1.0) {
971                                         k3tot-= 1.0;
972                                         k3+= elemsize;
973                                 }
974                         }
975                         else k3+= elemsize;
976                 }
977                 if(flagdo & 8) {
978                         if(flagflo & 8) {
979                                 k4tot+= k4d;
980                                 while(k4tot>=1.0) {
981                                         k4tot-= 1.0;
982                                         k4+= elemsize;
983                                 }
984                         }
985                         else k4+= elemsize;
986                 }
987                 
988                 if(mode==KEY_BEZTRIPLE) a+= 2;
989         }
990 }
991
992 static float *get_weights_array(Object *ob, char *vgroup)
993 {
994         bDeformGroup *curdef;
995         MDeformVert *dvert= NULL;
996         int totvert= 0, index= 0;
997         
998         /* no vgroup string set? */
999         if(vgroup[0]==0) return NULL;
1000         
1001         /* gather dvert and totvert */
1002         if(ob->type==OB_MESH) {
1003                 Mesh *me= ob->data;
1004                 dvert= me->dvert;
1005                 totvert= me->totvert;
1006         }
1007         else if(ob->type==OB_LATTICE) {
1008                 Lattice *lt= ob->data;
1009                 dvert= lt->dvert;
1010                 totvert= lt->pntsu*lt->pntsv*lt->pntsw;
1011         }
1012         
1013         if(dvert==NULL) return NULL;
1014         
1015         /* find the group (weak loop-in-loop) */
1016         for (curdef = ob->defbase.first; curdef; curdef=curdef->next, index++)
1017                 if (!strcmp(curdef->name, vgroup))
1018                         break;
1019
1020         if(curdef) {
1021                 float *weights;
1022                 int i, j;
1023                 
1024                 weights= MEM_callocN(totvert*sizeof(float), "weights");
1025                 
1026                 for (i=0; i < totvert; i++, dvert++) {
1027                         for(j=0; j<dvert->totweight; j++) {
1028                                 if (dvert->dw[j].def_nr == index) {
1029                                         weights[i]= dvert->dw[j].weight;
1030                                         break;
1031                                 }
1032                         }
1033                 }
1034                 return weights;
1035         }
1036         return NULL;
1037 }
1038
1039 static int do_mesh_key(Scene *scene, Object *ob, Mesh *me)
1040 {
1041         KeyBlock *k[4];
1042         float cfra, ctime, t[4], delta, loc[3], size[3];
1043         int a, flag = 0, step;
1044         
1045         if(me->totvert==0) return 0;
1046         if(me->key==NULL) return 0;
1047         if(me->key->block.first==NULL) return 0;
1048         
1049         /* prevent python from screwing this up? anyhoo, the from pointer could be dropped */
1050         me->key->from= (ID *)me;
1051         
1052         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);
1053         
1054         if(me->key->slurph && me->key->type!=KEY_RELATIVE ) {
1055                 if (G.f & G_DEBUG) printf("\tslurph key\n");
1056                 
1057                 delta= me->key->slurph;
1058                 delta/= me->totvert;
1059                 
1060                 step= 1;
1061                 if(me->totvert>100 && slurph_opt) {
1062                         step= me->totvert/50;
1063                         delta*= step;
1064                         /* in do_key and cp_key the case a>tot is handled */
1065                 }
1066                 
1067                 cfra= (float)scene->r.cfra;
1068                 
1069                 for(a=0; a<me->totvert; a+=step, cfra+= delta) {
1070                         
1071                         ctime= bsystem_time(scene, 0, cfra, 0.0); // xxx  ugly cruft!
1072 #if 0 // XXX old animation system
1073                         if(calc_ipo_spec(me->key->ipo, KEY_SPEED, &ctime)==0) {
1074                                 ctime /= 100.0;
1075                                 CLAMP(ctime, 0.0, 1.0);
1076                         }
1077 #endif // XXX old animation system
1078                         // XXX for now... since speed curve cannot be directly ported yet
1079                         ctime /= 100.0f;
1080                         CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1081                 
1082                         flag= setkeys(ctime, &me->key->block, k, t, 0);
1083                         if(flag==0) {
1084                                 
1085                                 do_key(a, a+step, me->totvert, (char *)me->mvert->co, me->key, k, t, 0);
1086                         }
1087                         else {
1088                                 cp_key(a, a+step, me->totvert, (char *)me->mvert->co, me->key, k[2], NULL, 0);
1089                         }
1090                 }
1091                 
1092                 if(flag && k[2]==me->key->refkey) tex_space_mesh(me);
1093                 else boundbox_mesh(me, loc, size);
1094         }
1095         else {
1096                 if(me->key->type==KEY_RELATIVE) {
1097                         KeyBlock *kb;
1098                         
1099                         if (G.f & G_DEBUG) printf("\tdo relative \n");
1100                         
1101                         for(kb= me->key->block.first; kb; kb= kb->next)
1102                                 kb->weights= get_weights_array(ob, kb->vgroup);
1103
1104                         do_rel_key(0, me->totvert, me->totvert, (char *)me->mvert->co, me->key, 0);
1105                         
1106                         for(kb= me->key->block.first; kb; kb= kb->next) {
1107                                 if(kb->weights) MEM_freeN(kb->weights);
1108                                 kb->weights= NULL;
1109                         }
1110                 }
1111                 else {
1112                         if (G.f & G_DEBUG) printf("\tdo absolute \n");
1113                         
1114                         ctime= bsystem_time(scene, ob, (float)scene->r.cfra, 0.0f); // xxx old cruft
1115                         
1116 #if 0 // XXX old animation system
1117                         if(calc_ipo_spec(me->key->ipo, KEY_SPEED, &ctime)==0) {
1118                                 ctime /= 100.0;
1119                                 CLAMP(ctime, 0.0, 1.0);
1120                         }
1121 #endif // XXX old animation system
1122                         // XXX for now... since speed curve cannot be directly ported yet
1123                         ctime /= 100.0f;
1124                         CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1125                         
1126                         flag= setkeys(ctime, &me->key->block, k, t, 0);
1127                         if(flag==0) {
1128                                 do_key(0, me->totvert, me->totvert, (char *)me->mvert->co, me->key, k, t, 0);
1129                         }
1130                         else {
1131                                 cp_key(0, me->totvert, me->totvert, (char *)me->mvert->co, me->key, k[2], NULL, 0);
1132                         }
1133                         
1134                         if(flag && k[2]==me->key->refkey) tex_space_mesh(me);
1135                         else boundbox_mesh(me, loc, size);
1136                 }
1137         }
1138         return 1;
1139 }
1140
1141 static void do_cu_key(Curve *cu, KeyBlock **k, float *t)
1142 {
1143         Nurb *nu;
1144         int a, step = 0, tot;
1145         char *poin;
1146         
1147         tot= count_curveverts(&cu->nurb);
1148         nu= cu->nurb.first;
1149         a= 0;
1150         
1151         while(nu) {
1152                 if(nu->bp) {
1153                         
1154                         step= nu->pntsu*nu->pntsv;
1155                         
1156                         /* exception because keys prefer to work with complete blocks */
1157                         poin= (char *)nu->bp->vec;
1158                         poin -= a*sizeof(BPoint);
1159                         
1160                         do_key(a, a+step, tot, poin, cu->key, k, t, KEY_BPOINT);
1161                 }
1162                 else if(nu->bezt) {
1163                         
1164                         step= 3*nu->pntsu;
1165                         
1166                         poin= (char *)nu->bezt->vec;
1167                         poin -= a*sizeof(BezTriple);
1168
1169                         do_key(a, a+step, tot, poin, cu->key, k, t, KEY_BEZTRIPLE);
1170                         
1171                 }
1172                 a+= step;
1173                 nu=nu->next;
1174         }
1175 }
1176
1177 static void do_rel_cu_key(Curve *cu, float ctime)
1178 {
1179         Nurb *nu;
1180         int a, step = 0, tot;
1181         char *poin;
1182         
1183         tot= count_curveverts(&cu->nurb);
1184         nu= cu->nurb.first;
1185         a= 0;
1186         while(nu) {
1187                 if(nu->bp) {
1188                         
1189                         step= nu->pntsu*nu->pntsv;
1190                         
1191                         /* exception because keys prefer to work with complete blocks */
1192                         poin= (char *)nu->bp->vec;
1193                         poin -= a*sizeof(BPoint);
1194                         
1195                         do_rel_key(a, a+step, tot, poin, cu->key, KEY_BPOINT);
1196                 }
1197                 else if(nu->bezt) {
1198                         
1199                         step= 3*nu->pntsu;
1200                         
1201                         poin= (char *)nu->bezt->vec;
1202                         poin -= a*sizeof(BezTriple);
1203
1204                         do_rel_key(a, a+step, tot, poin, cu->key, KEY_BEZTRIPLE);
1205                 }
1206                 a+= step;
1207                 
1208                 nu=nu->next;
1209         }
1210 }
1211
1212 static int do_curve_key(Scene *scene, Curve *cu)
1213 {
1214         KeyBlock *k[4];
1215         float cfra, ctime, t[4], delta;
1216         int a, flag = 0, step = 0, tot;
1217         
1218         tot= count_curveverts(&cu->nurb);
1219         
1220         if(tot==0) return 0;
1221         if(cu->key==NULL) return 0;
1222         if(cu->key->block.first==NULL) return 0;
1223         
1224         if(cu->key->slurph) {
1225                 delta= cu->key->slurph;
1226                 delta/= tot;
1227                 
1228                 step= 1;
1229                 if(tot>100 && slurph_opt) {
1230                         step= tot/50;
1231                         delta*= step;
1232                         /* in do_key and cp_key the case a>tot has been handled */
1233                 }
1234                 
1235                 cfra= (float)scene->r.cfra;
1236                 
1237                 for(a=0; a<tot; a+=step, cfra+= delta) {
1238                         ctime= bsystem_time(scene, 0, cfra, 0.0f); // XXX old cruft
1239 #if 0 // XXX old animation system
1240                         if(calc_ipo_spec(cu->key->ipo, KEY_SPEED, &ctime)==0) {
1241                                 ctime /= 100.0;
1242                                 CLAMP(ctime, 0.0, 1.0);
1243                         }
1244 #endif // XXX old animation system
1245                 
1246                         flag= setkeys(ctime, &cu->key->block, k, t, 0);
1247                         if(flag==0) {
1248                                 
1249                                 /* do_key(a, a+step, tot, (char *)cu->mvert->co, cu->key, k, t, 0); */
1250                         }
1251                         else {
1252                                 /* cp_key(a, a+step, tot, (char *)cu->mvert->co, cu->key, k[2],0); */
1253                         }
1254                 }
1255
1256                 if(flag && k[2]==cu->key->refkey) tex_space_curve(cu);
1257                 
1258                 
1259         }
1260         else {
1261                 
1262                 ctime= bsystem_time(scene, NULL, (float)scene->r.cfra, 0.0);
1263                 
1264                 if(cu->key->type==KEY_RELATIVE) {
1265                         do_rel_cu_key(cu, ctime);
1266                 }
1267                 else {
1268 #if 0 // XXX old animation system
1269                         if(calc_ipo_spec(cu->key->ipo, KEY_SPEED, &ctime)==0) {
1270                                 ctime /= 100.0;
1271                                 CLAMP(ctime, 0.0, 1.0);
1272                         }
1273 #endif // XXX old animation system
1274                         
1275                         flag= setkeys(ctime, &cu->key->block, k, t, 0);
1276                         
1277                         if(flag==0) do_cu_key(cu, k, t);
1278                         else cp_cu_key(cu, k[2], 0, tot);
1279                                         
1280                         if(flag && k[2]==cu->key->refkey) tex_space_curve(cu);
1281                 }
1282         }
1283         
1284         return 1;
1285 }
1286
1287 static int do_latt_key(Scene *scene, Object *ob, Lattice *lt)
1288 {
1289         KeyBlock *k[4];
1290         float delta, cfra, ctime, t[4];
1291         int a, tot, flag;
1292         
1293         if(lt->key==NULL) return 0;
1294         if(lt->key->block.first==NULL) return 0;
1295
1296         tot= lt->pntsu*lt->pntsv*lt->pntsw;
1297
1298         if(lt->key->slurph) {
1299                 delta= lt->key->slurph;
1300                 delta/= (float)tot;
1301                 
1302                 cfra= (float)scene->r.cfra;
1303                 
1304                 for(a=0; a<tot; a++, cfra+= delta) {
1305                         
1306                         ctime= bsystem_time(scene, 0, cfra, 0.0); // XXX old cruft
1307 #if 0 // XXX old animation system
1308                         if(calc_ipo_spec(lt->key->ipo, KEY_SPEED, &ctime)==0) {
1309                                 ctime /= 100.0;
1310                                 CLAMP(ctime, 0.0, 1.0);
1311                         }
1312 #endif // XXX old animation system
1313                 
1314                         flag= setkeys(ctime, &lt->key->block, k, t, 0);
1315                         if(flag==0) {
1316                                 
1317                                 do_key(a, a+1, tot, (char *)lt->def->vec, lt->key, k, t, 0);
1318                         }
1319                         else {
1320                                 cp_key(a, a+1, tot, (char *)lt->def->vec, lt->key, k[2], NULL, 0);
1321                         }
1322                 }               
1323         }
1324         else {
1325                 ctime= bsystem_time(scene, NULL, (float)scene->r.cfra, 0.0);
1326         
1327                 if(lt->key->type==KEY_RELATIVE) {
1328                         KeyBlock *kb;
1329                         
1330                         for(kb= lt->key->block.first; kb; kb= kb->next)
1331                                 kb->weights= get_weights_array(ob, kb->vgroup);
1332                         
1333                         do_rel_key(0, tot, tot, (char *)lt->def->vec, lt->key, 0);
1334                         
1335                         for(kb= lt->key->block.first; kb; kb= kb->next) {
1336                                 if(kb->weights) MEM_freeN(kb->weights);
1337                                 kb->weights= NULL;
1338                         }
1339                 }
1340                 else {
1341 #if 0 // XXX old animation system
1342                         if(calc_ipo_spec(lt->key->ipo, KEY_SPEED, &ctime)==0) {
1343                                 ctime /= 100.0;
1344                                 CLAMP(ctime, 0.0, 1.0);
1345                         }
1346 #endif // XXX old animation system
1347                         
1348                         flag= setkeys(ctime, &lt->key->block, k, t, 0);
1349                         if(flag==0) {
1350                                 do_key(0, tot, tot, (char *)lt->def->vec, lt->key, k, t, 0);
1351                         }
1352                         else {
1353                                 cp_key(0, tot, tot, (char *)lt->def->vec, lt->key, k[2], NULL, 0);
1354                         }
1355                 }
1356         }
1357         
1358         if(lt->flag & LT_OUTSIDE) outside_lattice(lt);
1359         
1360         return 1;
1361 }
1362
1363 /* returns 1 when key applied */
1364 int do_ob_key(Scene *scene, Object *ob)
1365 {
1366         Key *key= ob_get_key(ob);
1367         
1368         if(key==NULL)
1369                 return 0;
1370                 
1371         if(ob->shapeflag & OB_SHAPE_LOCK) {
1372                 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1373                 
1374                 if (G.f & G_DEBUG) printf("ob %s, key %s locked \n", ob->id.name+2, key->id.name+2);
1375                 
1376                 if(kb && (kb->flag & KEYBLOCK_MUTE))
1377                         kb= key->refkey;
1378
1379                 if(kb==NULL) {
1380                         kb= key->block.first;
1381                         ob->shapenr= 1;
1382                 }
1383                 
1384                 if(ob->type==OB_MESH) {
1385                         Mesh *me= ob->data;
1386                         float *weights= get_weights_array(ob, kb->vgroup);
1387
1388                         cp_key(0, me->totvert, me->totvert, (char *)me->mvert->co, key, kb, weights, 0);
1389                         
1390                         if(weights) MEM_freeN(weights);
1391                 }
1392                 else if(ob->type==OB_LATTICE) {
1393                         Lattice *lt= ob->data;
1394                         float *weights= get_weights_array(ob, kb->vgroup);
1395                         int tot= lt->pntsu*lt->pntsv*lt->pntsw;
1396                         
1397                         cp_key(0, tot, tot, (char *)lt->def->vec, key, kb, weights, 0);
1398                         
1399                         if(weights) MEM_freeN(weights);
1400                 }
1401                 else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1402                         Curve *cu= ob->data;
1403                         int tot= count_curveverts(&cu->nurb);
1404                         
1405                         cp_cu_key(cu, kb, 0, tot);
1406                 }
1407                 return 1;
1408         }
1409         else {
1410                 /* do shapekey local drivers */
1411                 float ctime= (float)scene->r.cfra; // XXX this needs to be checked
1412                 
1413                 if (G.f & G_DEBUG) 
1414                         printf("ob %s - do shapekey (%s) drivers \n", ob->id.name+2, key->id.name+2);
1415                 BKE_animsys_evaluate_animdata(&key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
1416                 
1417                 if(ob->type==OB_MESH) return do_mesh_key(scene, ob, ob->data);
1418                 else if(ob->type==OB_CURVE) return do_curve_key(scene, ob->data);
1419                 else if(ob->type==OB_SURF) return do_curve_key(scene, ob->data);
1420                 else if(ob->type==OB_LATTICE) return do_latt_key(scene, ob, ob->data);
1421         }
1422         
1423         return 0;
1424 }
1425
1426 Key *ob_get_key(Object *ob)
1427 {
1428         if(ob==NULL) return NULL;
1429         
1430         if(ob->type==OB_MESH) {
1431                 Mesh *me= ob->data;
1432                 return me->key;
1433         }
1434         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1435                 Curve *cu= ob->data;
1436                 return cu->key;
1437         }
1438         else if(ob->type==OB_LATTICE) {
1439                 Lattice *lt= ob->data;
1440                 return lt->key;
1441         }
1442         return NULL;
1443 }
1444
1445 /* only the active keyblock */
1446 KeyBlock *ob_get_keyblock(Object *ob) 
1447 {
1448         Key *key= ob_get_key(ob);
1449         
1450         if (key) {
1451                 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1452                 return kb;
1453         }
1454
1455         return NULL;
1456 }
1457
1458 /* get the appropriate KeyBlock given an index */
1459 KeyBlock *key_get_keyblock(Key *key, int index)
1460 {
1461         KeyBlock *kb;
1462         int i;
1463         
1464         if (key) {
1465                 kb= key->block.first;
1466                 
1467                 for (i= 1; i < key->totkey; i++) {
1468                         kb= kb->next;
1469                         
1470                         if (index==i)
1471                                 return kb;
1472                 }
1473         }
1474         
1475         return NULL;
1476 }
1477
1478 /* get the appropriate KeyBlock given a name to search for */
1479 KeyBlock *key_get_named_keyblock(Key *key, const char name[])
1480 {
1481         KeyBlock *kb;
1482         
1483         if (key && name) {
1484                 for (kb= key->block.first; kb; kb= kb->next) {
1485                         if (strcmp(name, kb->name)==0)
1486                                 return kb;
1487                 }
1488         }
1489         
1490         return NULL;
1491 }