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