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