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