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