synched with trunk at revision 34793
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include <stddef.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_editVert.h"
41 #include "BLI_math_vector.h"
42 #include "BLI_utildefines.h"
43
44 #include "DNA_anim_types.h"
45 #include "DNA_key_types.h"
46 #include "DNA_lattice_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_scene_types.h"
50
51 #include "BKE_animsys.h"
52 #include "BKE_curve.h"
53 #include "BKE_customdata.h"
54 #include "BKE_deform.h"
55 #include "BKE_global.h"
56 #include "BKE_key.h"
57 #include "BKE_lattice.h"
58 #include "BKE_library.h"
59 #include "BKE_main.h"
60 #include "BKE_object.h"
61
62
63 #include "RNA_access.h"
64
65 #define KEY_MODE_DUMMY          0 /* use where mode isn't checked for */
66 #define KEY_MODE_BPOINT         1
67 #define KEY_MODE_BEZTRIPLE      2
68
69         // old defines from DNA_ipo_types.h for data-type
70 #define IPO_FLOAT               4
71 #define IPO_BEZTRIPLE   100
72 #define IPO_BPOINT              101
73
74 int slurph_opt= 1;
75
76
77 void free_key(Key *key)
78 {
79         KeyBlock *kb;
80         
81         BKE_free_animdata((ID *)key);
82         
83         while( (kb= key->block.first) ) {
84                 
85                 if(kb->data) MEM_freeN(kb->data);
86                 
87                 BLI_remlink(&key->block, kb);
88                 MEM_freeN(kb);
89         }
90         
91 }
92
93 /* GS reads the memory pointed at in a specific ordering. There are,
94  * however two definitions for it. I have jotted them down here, both,
95  * but I think the first one is actually used. The thing is that
96  * big-endian systems might read this the wrong way round. OTOH, we
97  * constructed the IDs that are read out with this macro explicitly as
98  * well. I expect we'll sort it out soon... */
99
100 /* from blendef: */
101 #define GS(a)   (*((short *)(a)))
102
103 /* from misc_util: flip the bytes from x  */
104 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
105
106 Key *add_key(ID *id)    /* common function */
107 {
108         Key *key;
109         char *el;
110         
111         key= alloc_libblock(&G.main->key, ID_KE, "Key");
112         
113         key->type= KEY_NORMAL;
114         key->from= id;
115         
116         // XXX the code here uses some defines which will soon be depreceated...
117         if( GS(id->name)==ID_ME) {
118                 el= key->elemstr;
119                 
120                 el[0]= 3;
121                 el[1]= IPO_FLOAT;
122                 el[2]= 0;
123                 
124                 key->elemsize= 12;
125         }
126         else if( GS(id->name)==ID_LT) {
127                 el= key->elemstr;
128                 
129                 el[0]= 3;
130                 el[1]= IPO_FLOAT;
131                 el[2]= 0;
132                 
133                 key->elemsize= 12;
134         }
135         else if( GS(id->name)==ID_CU) {
136                 el= key->elemstr;
137                 
138                 el[0]= 4;
139                 el[1]= IPO_BPOINT;
140                 el[2]= 0;
141                 
142                 key->elemsize= 16;
143         }
144         
145         return key;
146 }
147
148 Key *copy_key(Key *key)
149 {
150         Key *keyn;
151         KeyBlock *kbn, *kb;
152         
153         if(key==0) return 0;
154         
155         keyn= copy_libblock(key);
156         
157         BLI_duplicatelist(&keyn->block, &key->block);
158         
159         kb= key->block.first;
160         kbn= keyn->block.first;
161         while(kbn) {
162                 
163                 if(kbn->data) kbn->data= MEM_dupallocN(kbn->data);
164                 if(kb==key->refkey) keyn->refkey= kbn;
165                 
166                 kbn= kbn->next;
167                 kb= kb->next;
168         }
169         
170         return keyn;
171 }
172
173 void make_local_key(Key *key)
174 {
175
176         /* - only lib users: do nothing
177         * - only local users: set flag
178         * - mixed: make copy
179         */
180         if(key==0) return;
181         
182         key->id.lib= 0;
183         new_id(0, (ID *)key, 0);
184 }
185
186 /* Sort shape keys and Ipo curves after a change.  This assumes that at most
187  * one key was moved, which is a valid assumption for the places it's
188  * currently being called.
189  */
190
191 void sort_keys(Key *key)
192 {
193         KeyBlock *kb;
194         //short i, adrcode;
195         //IpoCurve *icu = NULL;
196         KeyBlock *kb2;
197
198         /* locate the key which is out of position */ 
199         for (kb= key->block.first; kb; kb= kb->next)
200                 if ((kb->next) && (kb->pos > kb->next->pos))
201                         break;
202
203         /* if we find a key, move it */
204         if (kb) {
205                 kb = kb->next; /* next key is the out-of-order one */
206                 BLI_remlink(&key->block, kb);
207                 
208                 /* find the right location and insert before */
209                 for (kb2=key->block.first; kb2; kb2= kb2->next) {
210                         if (kb2->pos > kb->pos) {
211                                 BLI_insertlink(&key->block, kb2->prev, kb);
212                                 break;
213                         }
214                 }
215                 
216                 /* if more than one Ipo curve, see if this key had a curve */
217 #if 0 // XXX old animation system
218                 if(key->ipo && key->ipo->curve.first != key->ipo->curve.last ) {
219                         for(icu= key->ipo->curve.first; icu; icu= icu->next) {
220                                 /* if we find the curve, remove it and reinsert in the 
221                                  right place */
222                                 if(icu->adrcode==kb->adrcode) {
223                                         IpoCurve *icu2;
224                                         BLI_remlink(&key->ipo->curve, icu);
225                                         for(icu2= key->ipo->curve.first; icu2; icu2= icu2->next) {
226                                                 if(icu2->adrcode >= kb2->adrcode) {
227                                                         BLI_insertlink(&key->ipo->curve, icu2->prev, icu);
228                                                         break;
229                                                 }
230                                         }
231                                         break;
232                                 }
233                         }
234                 }
235                 
236                 /* kb points at the moved key, icu at the moved ipo (if it exists).
237                  * go back now and renumber adrcodes */
238
239                 /* first new code */
240                 adrcode = kb2->adrcode;
241                 for (i = kb->adrcode - adrcode; i >= 0; i--, adrcode++) {
242                         /* if the next ipo curve matches the current key, renumber it */
243                         if(icu && icu->adrcode == kb->adrcode ) {
244                                 icu->adrcode = adrcode;
245                                 icu = icu->next;
246                         }
247                         /* renumber the shape key */
248                         kb->adrcode = adrcode;
249                         kb = kb->next;
250                 }
251 #endif // XXX old animation system
252         }
253
254         /* new rule; first key is refkey, this to match drawing channels... */
255         key->refkey= key->block.first;
256 }
257
258 /**************** do the key ****************/
259
260 void key_curve_position_weights(float t, float *data, int type)
261 {
262         float t2, t3, fc;
263         
264         if(type==KEY_LINEAR) {
265                 data[0]=                  0.0f;
266                 data[1]= -t             + 1.0f;
267                 data[2]= t;
268                 data[3]=                  0.0f;
269         }
270         else if(type==KEY_CARDINAL) {
271                 t2= t*t;
272                 t3= t2*t;
273                 fc= 0.71f;
274                 
275                 data[0]= -fc*t3                 + 2.0f*fc*t2            - fc*t;
276                 data[1]= (2.0f-fc)*t3   + (fc-3.0f)*t2                                  + 1.0f;
277                 data[2]= (fc-2.0f)*t3   + (3.0f-2.0f*fc)*t2     + fc*t;
278                 data[3]= fc*t3                  - fc*t2;
279         }
280         else if(type==KEY_BSPLINE) {
281                 t2= t*t;
282                 t3= t2*t;
283
284                 data[0]= -0.16666666f*t3        + 0.5f*t2       - 0.5f*t        + 0.16666666f;
285                 data[1]= 0.5f*t3                        - t2                                    + 0.6666666f;
286                 data[2]= -0.5f*t3                       + 0.5f*t2       + 0.5f*t        + 0.16666666f;
287                 data[3]= 0.16666666f*t3;
288         }
289 }
290
291 /* first derivative */
292 void key_curve_tangent_weights(float t, float *data, int type)
293 {
294         float t2, fc;
295         
296         if(type==KEY_LINEAR) {
297                 data[0]= 0.0f;
298                 data[1]= -1.0f;
299                 data[2]= 1.0f;
300                 data[3]= 0.0f;
301         }
302         else if(type==KEY_CARDINAL) {
303                 t2= t*t;
304                 fc= 0.71f;
305                 
306                 data[0]= -3.0f*fc*t2            +4.0f*fc*t                              - fc;
307                 data[1]= 3.0f*(2.0f-fc)*t2      +2.0f*(fc-3.0f)*t;
308                 data[2]= 3.0f*(fc-2.0f)*t2      +2.0f*(3.0f-2.0f*fc)*t  + fc;
309                 data[3]= 3.0f*fc*t2                     -2.0f*fc*t;
310         }
311         else if(type==KEY_BSPLINE) {
312                 t2= t*t;
313
314                 data[0]= -0.5f*t2       + t                     - 0.5f;
315                 data[1]= 1.5f*t2        - 2.0f*t;
316                 data[2]= -1.5f*t2       + t                     + 0.5f;
317                 data[3]= 0.5f*t2;
318         }
319 }
320
321 /* second derivative */
322 void key_curve_normal_weights(float t, float *data, int type)
323 {
324         float fc;
325         
326         if(type==KEY_LINEAR) {
327                 data[0]= 0.0f;
328                 data[1]= 0.0f;
329                 data[2]= 0.0f;
330                 data[3]= 0.0f;
331         }
332         else if(type==KEY_CARDINAL) {
333                 fc= 0.71f;
334                 
335                 data[0]= -6.0f*fc*t                     + 4.0f*fc;
336                 data[1]= 6.0f*(2.0f-fc)*t       + 2.0f*(fc-3.0f);
337                 data[2]= 6.0f*(fc-2.0f)*t       + 2.0f*(3.0f-2.0f*fc);
338                 data[3]= 6.0f*fc*t                      - 2.0f*fc;
339         }
340         else if(type==KEY_BSPLINE) {
341                 data[0]= -1.0f*t        + 1.0f;
342                 data[1]= 3.0f*t         - 2.0f;
343                 data[2]= -3.0f*t        + 1.0f;
344                 data[3]= 1.0f*t;
345         }
346 }
347
348 static int setkeys(float fac, ListBase *lb, KeyBlock *k[], float *t, int cycl)
349 {
350         /* return 1 means k[2] is the position, return 0 means interpolate */
351         KeyBlock *k1, *firstkey;
352         float d, dpos, ofs=0, lastpos, temp, fval[4];
353         short bsplinetype;
354
355         firstkey= lb->first;
356         k1= lb->last;
357         lastpos= k1->pos;
358         dpos= lastpos - firstkey->pos;
359
360         if(fac < firstkey->pos) fac= firstkey->pos;
361         else if(fac > k1->pos) fac= k1->pos;
362
363         k1=k[0]=k[1]=k[2]=k[3]= firstkey;
364         t[0]=t[1]=t[2]=t[3]= k1->pos;
365
366         /* if(fac<0.0 || fac>1.0) return 1; */
367
368         if(k1->next==0) return 1;
369
370         if(cycl) {      /* pre-sort */
371                 k[2]= k1->next;
372                 k[3]= k[2]->next;
373                 if(k[3]==0) k[3]=k1;
374                 while(k1) {
375                         if(k1->next==0) k[0]=k1;
376                         k1=k1->next;
377                 }
378                 k1= k[1];
379                 t[0]= k[0]->pos;
380                 t[1]+= dpos;
381                 t[2]= k[2]->pos + dpos;
382                 t[3]= k[3]->pos + dpos;
383                 fac+= dpos;
384                 ofs= dpos;
385                 if(k[3]==k[1]) { 
386                         t[3]+= dpos; 
387                         ofs= 2.0f*dpos;
388                 }
389                 if(fac<t[1]) fac+= dpos;
390                 k1= k[3];
391         }
392         else {          /* pre-sort */
393                 k[2]= k1->next;
394                 t[2]= k[2]->pos;
395                 k[3]= k[2]->next;
396                 if(k[3]==0) k[3]= k[2];
397                 t[3]= k[3]->pos;
398                 k1= k[3];
399         }
400         
401         while( t[2]<fac ) {     /* find correct location */
402                 if(k1->next==0) {
403                         if(cycl) {
404                                 k1= firstkey;
405                                 ofs+= dpos;
406                         }
407                         else if(t[2]==t[3]) break;
408                 }
409                 else k1= k1->next;
410
411                 t[0]= t[1]; 
412                 k[0]= k[1];
413                 t[1]= t[2]; 
414                 k[1]= k[2];
415                 t[2]= t[3]; 
416                 k[2]= k[3];
417                 t[3]= k1->pos+ofs; 
418                 k[3]= k1;
419
420                 if(ofs>2.1+lastpos) break;
421         }
422         
423         bsplinetype= 0;
424         if(k[1]->type==KEY_BSPLINE || k[2]->type==KEY_BSPLINE) bsplinetype= 1;
425
426
427         if(cycl==0) {
428                 if(bsplinetype==0) {    /* B spline doesn't go through the control points */
429                         if(fac<=t[1]) {         /* fac for 1st key */
430                                 t[2]= t[1];
431                                 k[2]= k[1];
432                                 return 1;
433                         }
434                         if(fac>=t[2] ) {        /* fac after 2nd key */
435                                 return 1;
436                         }
437                 }
438                 else if(fac>t[2]) {     /* last key */
439                         fac= t[2];
440                         k[3]= k[2];
441                         t[3]= t[2];
442                 }
443         }
444
445         d= t[2]-t[1];
446         if(d==0.0) {
447                 if(bsplinetype==0) {
448                         return 1;       /* both keys equal */
449                 }
450         }
451         else d= (fac-t[1])/d;
452
453         /* interpolation */
454         
455         key_curve_position_weights(d, t, k[1]->type);
456
457         if(k[1]->type != k[2]->type) {
458                 key_curve_position_weights(d, fval, k[2]->type);
459                 
460                 temp= 1.0f-d;
461                 t[0]= temp*t[0]+ d*fval[0];
462                 t[1]= temp*t[1]+ d*fval[1];
463                 t[2]= temp*t[2]+ d*fval[2];
464                 t[3]= temp*t[3]+ d*fval[3];
465         }
466
467         return 0;
468
469 }
470
471 static void flerp(int aantal, float *in, float *f0, float *f1, float *f2, float *f3, float *t)  
472 {
473         int a;
474
475         for(a=0; a<aantal; a++) {
476                 in[a]= t[0]*f0[a]+t[1]*f1[a]+t[2]*f2[a]+t[3]*f3[a];
477         }
478 }
479
480 static void rel_flerp(int aantal, float *in, float *ref, float *out, float fac)
481 {
482         int a;
483         
484         for(a=0; a<aantal; a++) {
485                 in[a]-= fac*(ref[a]-out[a]);
486         }
487 }
488
489 static char *key_block_get_data(Key *key, KeyBlock *actkb, KeyBlock *kb, char **freedata)
490 {
491         if(kb == actkb) {
492                 /* this hack makes it possible to edit shape keys in
493                    edit mode with shape keys blending applied */
494                 if(GS(key->from->name) == ID_ME) {
495                         Mesh *me;
496                         EditVert *eve;
497                         float (*co)[3];
498                         int a;
499
500                         me= (Mesh*)key->from;
501
502                         if(me->edit_mesh && me->edit_mesh->totvert == kb->totelem) {
503                                 a= 0;
504                                 co= MEM_callocN(sizeof(float)*3*me->edit_mesh->totvert, "key_block_get_data");
505
506                                 for(eve=me->edit_mesh->verts.first; eve; eve=eve->next, a++)
507                                         VECCOPY(co[a], eve->co);
508
509                                 *freedata= (char*)co;
510                                 return (char*)co;
511                         }
512                 }
513         }
514
515         *freedata= NULL;
516         return kb->data;
517 }
518
519
520 /* currently only the first value of 'ofs' may be set. */
521 static short key_pointer_size(const Key *key, const int mode, int *poinsize, int *ofs)
522 {
523         if(key->from==NULL) {
524                 return FALSE;
525         }
526
527         switch(GS(key->from->name)) {
528         case ID_ME:
529                 *ofs= sizeof(float)*3;
530                 *poinsize= *ofs;
531                 break;
532         case ID_LT:
533                 *ofs= sizeof(float)*3;
534                 *poinsize= *ofs;
535                 break;
536         case ID_CU:
537                 if(mode == KEY_MODE_BPOINT) {
538                         *ofs= sizeof(float)*4;
539                         *poinsize= *ofs;
540                 } else {
541                         ofs[0]= sizeof(float)*12;
542                         *poinsize= (*ofs) / 3;
543                 }
544
545                 break;
546         default:
547                 BLI_assert(!"invalid 'key->from' ID type");
548                 return FALSE;
549         }
550
551         return TRUE;
552 }
553
554 static void cp_key(const int start, int end, const int tot, char *poin, Key *key, KeyBlock *actkb, KeyBlock *kb, float *weights, const int mode)
555 {
556         float ktot = 0.0, kd = 0.0;
557         int elemsize, poinsize = 0, a, *ofsp, ofs[32], flagflo=0;
558         char *k1, *kref, *freek1, *freekref;
559         char *cp, elemstr[8];
560
561         /* currently always 0, in future key_pointer_size may assign */
562         ofs[1]= 0;
563
564         if(!key_pointer_size(key, mode, &poinsize, &ofs[0]))
565                 return;
566
567         if(end>tot) end= tot;
568         
569         if(tot != kb->totelem) {
570                 ktot= 0.0;
571                 flagflo= 1;
572                 if(kb->totelem) {
573                         kd= kb->totelem/(float)tot;
574                 }
575                 else return;
576         }
577
578         k1= key_block_get_data(key, actkb, kb, &freek1);
579         kref= key_block_get_data(key, actkb, key->refkey, &freekref);
580
581         /* this exception is needed for slurphing */
582         if(start!=0) {
583                 
584                 poin+= poinsize*start;
585                 
586                 if(flagflo) {
587                         ktot+= start*kd;
588                         a= (int)floor(ktot);
589                         if(a) {
590                                 ktot-= a;
591                                 k1+= a*key->elemsize;
592                         }
593                 }
594                 else k1+= start*key->elemsize;
595         }       
596         
597         if(mode == KEY_MODE_BEZTRIPLE) {
598                 elemstr[0]= 1;
599                 elemstr[1]= IPO_BEZTRIPLE;
600                 elemstr[2]= 0;
601         }
602         
603         /* just do it here, not above! */
604         elemsize= key->elemsize;
605         if(mode == KEY_MODE_BEZTRIPLE) elemsize*= 3;
606
607         for(a=start; a<end; a++) {
608                 cp= key->elemstr;
609                 if(mode == KEY_MODE_BEZTRIPLE) cp= elemstr;
610
611                 ofsp= ofs;
612                 
613                 while( cp[0] ) {
614                         
615                         switch(cp[1]) {
616                         case IPO_FLOAT:
617                                 if(weights) {
618                                         memcpy(poin, kref, sizeof(float)*3);
619                                         if(*weights!=0.0f)
620                                                 rel_flerp(cp[0], (float *)poin, (float *)kref, (float *)k1, *weights);
621                                         weights++;
622                                 }
623                                 else 
624                                         memcpy(poin, k1, sizeof(float)*3);
625                                 break;
626                         case IPO_BPOINT:
627                                 memcpy(poin, k1, sizeof(float)*4);
628                                 break;
629                         case IPO_BEZTRIPLE:
630                                 memcpy(poin, k1, sizeof(float)*12);
631                                 break;
632                         default:
633                                 /* should never happen */
634                                 if(freek1) MEM_freeN(freek1);
635                                 if(freekref) MEM_freeN(freekref);
636                                 BLI_assert(!"invalid 'cp[1]'");
637                                 return;
638                         }
639
640                         poin+= ofsp[0]; 
641                         cp+= 2; ofsp++;
642                 }
643                 
644                 /* are we going to be nasty? */
645                 if(flagflo) {
646                         ktot+= kd;
647                         while(ktot>=1.0) {
648                                 ktot-= 1.0;
649                                 k1+= elemsize;
650                                 kref+= elemsize;
651                         }
652                 }
653                 else {
654                         k1+= elemsize;
655                         kref+= elemsize;
656                 }
657                 
658                 if(mode == KEY_MODE_BEZTRIPLE) a+=2;
659         }
660
661         if(freek1) MEM_freeN(freek1);
662         if(freekref) MEM_freeN(freekref);
663 }
664
665 static void cp_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock *kb, const int start, int end, char *out, const int tot)
666 {
667         Nurb *nu;
668         int a, step, a1, a2;
669
670         for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
671                 if(nu->bp) {
672                         step= nu->pntsu*nu->pntsv;
673
674                         a1= MAX2(a, start);
675                         a2= MIN2(a+step, end);
676
677                         if(a1<a2) cp_key(a1, a2, tot, out, key, actkb, kb, NULL, KEY_MODE_BPOINT);
678                 }
679                 else if(nu->bezt) {
680                         step= 3*nu->pntsu;
681
682                         /* exception because keys prefer to work with complete blocks */
683                         a1= MAX2(a, start);
684                         a2= MIN2(a+step, end);
685
686                         if(a1<a2) cp_key(a1, a2, tot, out, key, actkb, kb, NULL, KEY_MODE_BEZTRIPLE);
687                 }
688                 else
689                         step= 0;
690         }
691 }
692
693 void do_rel_key(const int start, int end, const int tot, char *basispoin, Key *key, KeyBlock *actkb, const int mode)
694 {
695         KeyBlock *kb;
696         int *ofsp, ofs[3], elemsize, b;
697         char *cp, *poin, *reffrom, *from, elemstr[8];
698         char *freefrom, *freereffrom;
699         int poinsize;
700
701         /* currently always 0, in future key_pointer_size may assign */
702         ofs[1]= 0;
703
704         if(!key_pointer_size(key, mode, &poinsize, &ofs[0]))
705                 return;
706
707         if(end>tot) end= tot;
708
709         /* in case of beztriple */
710         elemstr[0]= 1;                          /* nr of ipofloats */
711         elemstr[1]= IPO_BEZTRIPLE;
712         elemstr[2]= 0;
713
714         /* just here, not above! */
715         elemsize= key->elemsize;
716         if(mode == KEY_MODE_BEZTRIPLE) elemsize*= 3;
717
718         /* step 1 init */
719         cp_key(start, end, tot, basispoin, key, actkb, key->refkey, NULL, mode);
720         
721         /* step 2: do it */
722         
723         for(kb=key->block.first; kb; kb=kb->next) {
724                 if(kb!=key->refkey) {
725                         float icuval= kb->curval;
726                         
727                         /* only with value, and no difference allowed */
728                         if(!(kb->flag & KEYBLOCK_MUTE) && icuval!=0.0f && kb->totelem==tot) {
729                                 KeyBlock *refb;
730                                 float weight, *weights= kb->weights;
731
732                                 /* reference now can be any block */
733                                 refb= BLI_findlink(&key->block, kb->relative);
734                                 if(refb==NULL) continue;
735                                 
736                                 poin= basispoin;
737                                 from= key_block_get_data(key, actkb, kb, &freefrom);
738                                 reffrom= key_block_get_data(key, actkb, refb, &freereffrom);
739                                 
740                                 poin+= start*poinsize;
741                                 reffrom+= key->elemsize*start;  // key elemsize yes!
742                                 from+= key->elemsize*start;
743                                 
744                                 for(b=start; b<end; b++) {
745                                 
746                                         if(weights) 
747                                                 weight= *weights * icuval;
748                                         else
749                                                 weight= icuval;
750                                         
751                                         cp= key->elemstr;       
752                                         if(mode == KEY_MODE_BEZTRIPLE) cp= elemstr;
753                                         
754                                         ofsp= ofs;
755                                         
756                                         while( cp[0] ) {        /* cp[0]==amount */
757                                                 
758                                                 switch(cp[1]) {
759                                                 case IPO_FLOAT:
760                                                         rel_flerp(3, (float *)poin, (float *)reffrom, (float *)from, weight);
761                                                         break;
762                                                 case IPO_BPOINT:
763                                                         rel_flerp(4, (float *)poin, (float *)reffrom, (float *)from, weight);
764                                                         break;
765                                                 case IPO_BEZTRIPLE:
766                                                         rel_flerp(12, (float *)poin, (float *)reffrom, (float *)from, weight);
767                                                         break;
768                                                 default:
769                                                         /* should never happen */
770                                                         if(freefrom) MEM_freeN(freefrom);
771                                                         if(freereffrom) MEM_freeN(freereffrom);
772                                                         BLI_assert(!"invalid 'cp[1]'");
773                                                         return;
774                                                 }
775
776                                                 poin+= ofsp[0];                         
777                                                 
778                                                 cp+= 2;
779                                                 ofsp++;
780                                         }
781                                         
782                                         reffrom+= elemsize;
783                                         from+= elemsize;
784                                         
785                                         if(mode == KEY_MODE_BEZTRIPLE) b+= 2;
786                                         if(weights) weights++;
787                                 }
788
789                                 if(freefrom) MEM_freeN(freefrom);
790                                 if(freereffrom) MEM_freeN(freereffrom);
791                         }
792                 }
793         }
794 }
795
796
797 static void do_key(const int start, int end, const int tot, char *poin, Key *key, KeyBlock *actkb, KeyBlock **k, float *t, const int mode)
798 {
799         float k1tot = 0.0, k2tot = 0.0, k3tot = 0.0, k4tot = 0.0;
800         float k1d = 0.0, k2d = 0.0, k3d = 0.0, k4d = 0.0;
801         int a, ofs[32], *ofsp;
802         int flagdo= 15, flagflo=0, elemsize, poinsize=0;
803         char *k1, *k2, *k3, *k4, *freek1, *freek2, *freek3, *freek4;
804         char *cp, elemstr[8];;
805
806         /* currently always 0, in future key_pointer_size may assign */
807         ofs[1]= 0;
808
809         if(!key_pointer_size(key, mode, &poinsize, &ofs[0]))
810                 return;
811         
812         if(end>tot) end= tot;
813
814         k1= key_block_get_data(key, actkb, k[0], &freek1);
815         k2= key_block_get_data(key, actkb, k[1], &freek2);
816         k3= key_block_get_data(key, actkb, k[2], &freek3);
817         k4= key_block_get_data(key, actkb, k[3], &freek4);
818
819         /*  test for more or less points (per key!) */
820         if(tot != k[0]->totelem) {
821                 k1tot= 0.0;
822                 flagflo |= 1;
823                 if(k[0]->totelem) {
824                         k1d= k[0]->totelem/(float)tot;
825                 }
826                 else flagdo -= 1;
827         }
828         if(tot != k[1]->totelem) {
829                 k2tot= 0.0;
830                 flagflo |= 2;
831                 if(k[0]->totelem) {
832                         k2d= k[1]->totelem/(float)tot;
833                 }
834                 else flagdo -= 2;
835         }
836         if(tot != k[2]->totelem) {
837                 k3tot= 0.0;
838                 flagflo |= 4;
839                 if(k[0]->totelem) {
840                         k3d= k[2]->totelem/(float)tot;
841                 }
842                 else flagdo -= 4;
843         }
844         if(tot != k[3]->totelem) {
845                 k4tot= 0.0;
846                 flagflo |= 8;
847                 if(k[0]->totelem) {
848                         k4d= k[3]->totelem/(float)tot;
849                 }
850                 else flagdo -= 8;
851         }
852
853                 /* this exception needed for slurphing */
854         if(start!=0) {
855
856                 poin+= poinsize*start;
857                 
858                 if(flagdo & 1) {
859                         if(flagflo & 1) {
860                                 k1tot+= start*k1d;
861                                 a= (int)floor(k1tot);
862                                 if(a) {
863                                         k1tot-= a;
864                                         k1+= a*key->elemsize;
865                                 }
866                         }
867                         else k1+= start*key->elemsize;
868                 }
869                 if(flagdo & 2) {
870                         if(flagflo & 2) {
871                                 k2tot+= start*k2d;
872                                 a= (int)floor(k2tot);
873                                 if(a) {
874                                         k2tot-= a;
875                                         k2+= a*key->elemsize;
876                                 }
877                         }
878                         else k2+= start*key->elemsize;
879                 }
880                 if(flagdo & 4) {
881                         if(flagflo & 4) {
882                                 k3tot+= start*k3d;
883                                 a= (int)floor(k3tot);
884                                 if(a) {
885                                         k3tot-= a;
886                                         k3+= a*key->elemsize;
887                                 }
888                         }
889                         else k3+= start*key->elemsize;
890                 }
891                 if(flagdo & 8) {
892                         if(flagflo & 8) {
893                                 k4tot+= start*k4d;
894                                 a= (int)floor(k4tot);
895                                 if(a) {
896                                         k4tot-= a;
897                                         k4+= a*key->elemsize;
898                                 }
899                         }
900                         else k4+= start*key->elemsize;
901                 }
902
903         }
904
905         /* in case of beztriple */
906         elemstr[0]= 1;                          /* nr of ipofloats */
907         elemstr[1]= IPO_BEZTRIPLE;
908         elemstr[2]= 0;
909
910         /* only here, not above! */
911         elemsize= key->elemsize;
912         if(mode == KEY_MODE_BEZTRIPLE) elemsize*= 3;
913
914         for(a=start; a<end; a++) {
915         
916                 cp= key->elemstr;       
917                 if(mode == KEY_MODE_BEZTRIPLE) cp= elemstr;
918                 
919                 ofsp= ofs;
920                 
921                 while( cp[0] ) {        /* cp[0]==amount */
922                         
923                         switch(cp[1]) {
924                         case IPO_FLOAT:
925                                 flerp(3, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
926                                 break;
927                         case IPO_BPOINT:
928                                 flerp(4, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
929                                 break;
930                         case IPO_BEZTRIPLE:
931                                 flerp(12, (void *)poin, (void *)k1, (void *)k2, (void *)k3, (void *)k4, t);
932                                 break;
933                         default:
934                                 /* should never happen */
935                                 if(freek1) MEM_freeN(freek1);
936                                 if(freek2) MEM_freeN(freek2);
937                                 if(freek3) MEM_freeN(freek3);
938                                 if(freek4) MEM_freeN(freek4);
939                                 BLI_assert(!"invalid 'cp[1]'");
940                                 return;
941                         }
942                         
943                         poin+= ofsp[0];                         
944                         cp+= 2;
945                         ofsp++;
946                 }
947                 /* lets do it the difficult way: when keys have a different size */
948                 if(flagdo & 1) {
949                         if(flagflo & 1) {
950                                 k1tot+= k1d;
951                                 while(k1tot>=1.0) {
952                                         k1tot-= 1.0;
953                                         k1+= elemsize;
954                                 }
955                         }
956                         else k1+= elemsize;
957                 }
958                 if(flagdo & 2) {
959                         if(flagflo & 2) {
960                                 k2tot+= k2d;
961                                 while(k2tot>=1.0) {
962                                         k2tot-= 1.0;
963                                         k2+= elemsize;
964                                 }
965                         }
966                         else k2+= elemsize;
967                 }
968                 if(flagdo & 4) {
969                         if(flagflo & 4) {
970                                 k3tot+= k3d;
971                                 while(k3tot>=1.0) {
972                                         k3tot-= 1.0;
973                                         k3+= elemsize;
974                                 }
975                         }
976                         else k3+= elemsize;
977                 }
978                 if(flagdo & 8) {
979                         if(flagflo & 8) {
980                                 k4tot+= k4d;
981                                 while(k4tot>=1.0) {
982                                         k4tot-= 1.0;
983                                         k4+= elemsize;
984                                 }
985                         }
986                         else k4+= elemsize;
987                 }
988                 
989                 if(mode == KEY_MODE_BEZTRIPLE) a+= 2;
990         }
991
992         if(freek1) MEM_freeN(freek1);
993         if(freek2) MEM_freeN(freek2);
994         if(freek3) MEM_freeN(freek3);
995         if(freek4) MEM_freeN(freek4);
996 }
997
998 static float *get_weights_array(Object *ob, char *vgroup)
999 {
1000         MDeformVert *dvert= NULL;
1001         EditMesh *em= NULL;
1002         EditVert *eve;
1003         int totvert= 0, index= 0;
1004         
1005         /* no vgroup string set? */
1006         if(vgroup[0]==0) return NULL;
1007         
1008         /* gather dvert and totvert */
1009         if(ob->type==OB_MESH) {
1010                 Mesh *me= ob->data;
1011                 dvert= me->dvert;
1012                 totvert= me->totvert;
1013
1014                 if(me->edit_mesh && me->edit_mesh->totvert == totvert)
1015                         em= me->edit_mesh;
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         index= defgroup_name_index(ob, vgroup);
1027         if(index >= 0) {
1028                 float *weights;
1029                 int i, j;
1030                 
1031                 weights= MEM_callocN(totvert*sizeof(float), "weights");
1032
1033                 if(em) {
1034                         for(i=0, eve=em->verts.first; eve; eve=eve->next, i++) {
1035                                 dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
1036
1037                                 if(dvert) {
1038                                         for(j=0; j<dvert->totweight; j++) {
1039                                                 if(dvert->dw[j].def_nr == index) {
1040                                                         weights[i]= dvert->dw[j].weight;
1041                                                         break;
1042                                                 }
1043                                         }
1044                                 }
1045                         }
1046                 }
1047                 else {
1048                         for(i=0; i < totvert; i++, dvert++) {
1049                                 for(j=0; j<dvert->totweight; j++) {
1050                                         if(dvert->dw[j].def_nr == index) {
1051                                                 weights[i]= dvert->dw[j].weight;
1052                                                 break;
1053                                         }
1054                                 }
1055                         }
1056                 }
1057
1058                 return weights;
1059         }
1060         return NULL;
1061 }
1062
1063 static void do_mesh_key(Scene *scene, Object *ob, Key *key, char *out, const int tot)
1064 {
1065         KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1066         float cfra, ctime, t[4], delta;
1067         int a, flag = 0, step;
1068         
1069         if(key->slurph && key->type!=KEY_RELATIVE ) {
1070                 delta= key->slurph;
1071                 delta/= tot;
1072                 
1073                 step= 1;
1074                 if(tot>100 && slurph_opt) {
1075                         step= tot/50;
1076                         delta*= step;
1077                         /* in do_key and cp_key the case a>tot is handled */
1078                 }
1079                 
1080                 cfra= (float)scene->r.cfra;
1081                 
1082                 for(a=0; a<tot; a+=step, cfra+= delta) {
1083                         
1084                         ctime= bsystem_time(scene, 0, cfra, 0.0); // xxx  ugly cruft!
1085 #if 0 // XXX old animation system
1086                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1087                                 ctime /= 100.0;
1088                                 CLAMP(ctime, 0.0, 1.0);
1089                         }
1090 #endif // XXX old animation system
1091                         // XXX for now... since speed curve cannot be directly ported yet
1092                         ctime /= 100.0f;
1093                         CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1094                 
1095                         flag= setkeys(ctime, &key->block, k, t, 0);
1096
1097                         if(flag==0)
1098                                 do_key(a, a+step, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
1099                         else
1100                                 cp_key(a, a+step, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1101                 }
1102         }
1103         else {
1104                 if(key->type==KEY_RELATIVE) {
1105                         KeyBlock *kb;
1106                         
1107                         for(kb= key->block.first; kb; kb= kb->next)
1108                                 kb->weights= get_weights_array(ob, kb->vgroup);
1109
1110                         do_rel_key(0, tot, tot, (char *)out, key, actkb, KEY_MODE_DUMMY);
1111                         
1112                         for(kb= key->block.first; kb; kb= kb->next) {
1113                                 if(kb->weights) MEM_freeN(kb->weights);
1114                                 kb->weights= NULL;
1115                         }
1116                 }
1117                 else {
1118                         ctime= bsystem_time(scene, ob, (float)scene->r.cfra, 0.0f); // xxx old cruft
1119                         
1120 #if 0 // XXX old animation system
1121                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1122                                 ctime /= 100.0;
1123                                 CLAMP(ctime, 0.0, 1.0);
1124                         }
1125 #endif // XXX old animation system
1126                         // XXX for now... since speed curve cannot be directly ported yet
1127                         ctime /= 100.0f;
1128                         CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1129                         
1130                         flag= setkeys(ctime, &key->block, k, t, 0);
1131
1132                         if(flag==0)
1133                                 do_key(0, tot, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
1134                         else
1135                                 cp_key(0, tot, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1136                 }
1137         }
1138 }
1139
1140 static void do_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock **k, float *t, char *out, const int tot)
1141 {
1142         Nurb *nu;
1143         int a, step;
1144         
1145         for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
1146                 if(nu->bp) {
1147                         step= nu->pntsu*nu->pntsv;
1148                         do_key(a, a+step, tot, out, key, actkb, k, t, KEY_MODE_BPOINT);
1149                 }
1150                 else if(nu->bezt) {
1151                         step= 3*nu->pntsu;
1152                         do_key(a, a+step, tot, out, key, actkb, k, t, KEY_MODE_BEZTRIPLE);
1153                 }
1154                 else
1155                         step= 0;
1156         }
1157 }
1158
1159 static void do_rel_cu_key(Curve *cu, Key *key, KeyBlock *actkb, float UNUSED(ctime), char *out, const int tot)
1160 {
1161         Nurb *nu;
1162         int a, step;
1163         
1164         for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
1165                 if(nu->bp) {
1166                         step= nu->pntsu*nu->pntsv;
1167                         do_rel_key(a, a+step, tot, out, key, actkb, KEY_MODE_BPOINT);
1168                 }
1169                 else if(nu->bezt) {
1170                         step= 3*nu->pntsu;
1171                         do_rel_key(a, a+step, tot, out, key, actkb, KEY_MODE_BEZTRIPLE);
1172                 }
1173                 else
1174                         step= 0;
1175         }
1176 }
1177
1178 static void do_curve_key(Scene *scene, Object *ob, Key *key, char *out, const int tot)
1179 {
1180         Curve *cu= ob->data;
1181         KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1182         float cfra, ctime, t[4], delta;
1183         int a, flag = 0, step = 0;
1184
1185         if(key->slurph  && key->type!=KEY_RELATIVE) {
1186                 Nurb *nu;
1187                 int mode=0, i= 0, remain= 0, estep=0, count=0;
1188
1189                 delta= (float)key->slurph / tot;
1190
1191                 step= 1;
1192                 if(tot>100 && slurph_opt) {
1193                         step= tot/50;
1194                         delta*= step;
1195                         /* in do_key and cp_key the case a>tot has been handled */
1196                 }
1197
1198                 cfra= (float)scene->r.cfra;
1199
1200                 for(nu=cu->nurb.first; nu; nu=nu->next) {
1201                         if(nu->bp) {
1202                                 mode= KEY_MODE_BPOINT;
1203                                 estep= nu->pntsu*nu->pntsv;
1204                         }
1205                         else if(nu->bezt) {
1206                                 mode= KEY_MODE_BEZTRIPLE;
1207                                 estep= 3*nu->pntsu;
1208                         }
1209                         else
1210                                 step= 0;
1211
1212                         a= 0;
1213                         while (a < estep) {
1214                                 if (remain <= 0) {
1215                                         cfra+= delta;
1216                                         ctime= bsystem_time(scene, 0, cfra, 0.0f); // XXX old cruft
1217
1218                                         ctime /= 100.0f;
1219                                         CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1220                                         flag= setkeys(ctime, &key->block, k, t, 0);
1221
1222                                         remain= step;
1223                                 }
1224
1225                                 count= MIN2(remain, estep);
1226                                 if (mode == KEY_MODE_BEZTRIPLE) {
1227                                         count += 3 - count % 3;
1228                                 }
1229
1230                                 if(flag==0)
1231                                         do_key(i, i+count, tot, (char *)out, key, actkb, k, t, mode);
1232                                 else
1233                                         cp_key(i, i+count, tot, (char *)out, key, actkb, k[2], NULL, mode);
1234
1235                                 a += count;
1236                                 i += count;
1237                                 remain -= count;
1238                         }
1239                 }
1240         }
1241         else {
1242                 
1243                 ctime= bsystem_time(scene, NULL, (float)scene->r.cfra, 0.0);
1244                 
1245                 if(key->type==KEY_RELATIVE) {
1246                         do_rel_cu_key(cu, cu->key, actkb, ctime, out, tot);
1247                 }
1248                 else {
1249 #if 0 // XXX old animation system
1250                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1251                                 ctime /= 100.0;
1252                                 CLAMP(ctime, 0.0, 1.0);
1253                         }
1254 #endif // XXX old animation system
1255                         
1256                         flag= setkeys(ctime, &key->block, k, t, 0);
1257                         
1258                         if(flag==0) do_cu_key(cu, key, actkb, k, t, out, tot);
1259                         else cp_cu_key(cu, key, actkb, k[2], 0, tot, out, tot);
1260                 }
1261         }
1262 }
1263
1264 static void do_latt_key(Scene *scene, Object *ob, Key *key, char *out, const int tot)
1265 {
1266         Lattice *lt= ob->data;
1267         KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1268         float delta, cfra, ctime, t[4];
1269         int a, flag;
1270         
1271         if(key->slurph) {
1272                 delta= key->slurph;
1273                 delta/= (float)tot;
1274                 
1275                 cfra= (float)scene->r.cfra;
1276                 
1277                 for(a=0; a<tot; a++, cfra+= delta) {
1278                         
1279                         ctime= bsystem_time(scene, 0, cfra, 0.0); // XXX old cruft
1280 #if 0 // XXX old animation system
1281                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1282                                 ctime /= 100.0;
1283                                 CLAMP(ctime, 0.0, 1.0);
1284                         }
1285 #endif // XXX old animation system
1286                 
1287                         flag= setkeys(ctime, &key->block, k, t, 0);
1288
1289                         if(flag==0)
1290                                 do_key(a, a+1, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
1291                         else
1292                                 cp_key(a, a+1, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1293                 }               
1294         }
1295         else {
1296                 if(key->type==KEY_RELATIVE) {
1297                         KeyBlock *kb;
1298                         
1299                         for(kb= key->block.first; kb; kb= kb->next)
1300                                 kb->weights= get_weights_array(ob, kb->vgroup);
1301                         
1302                         do_rel_key(0, tot, tot, (char *)out, key, actkb, KEY_MODE_DUMMY);
1303                         
1304                         for(kb= key->block.first; kb; kb= kb->next) {
1305                                 if(kb->weights) MEM_freeN(kb->weights);
1306                                 kb->weights= NULL;
1307                         }
1308                 }
1309                 else {
1310                         ctime= bsystem_time(scene, NULL, (float)scene->r.cfra, 0.0);
1311
1312 #if 0 // XXX old animation system
1313                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1314                                 ctime /= 100.0;
1315                                 CLAMP(ctime, 0.0, 1.0);
1316                         }
1317 #endif // XXX old animation system
1318                         
1319                         flag= setkeys(ctime, &key->block, k, t, 0);
1320
1321                         if(flag==0)
1322                                 do_key(0, tot, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
1323                         else
1324                                 cp_key(0, tot, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1325                 }
1326         }
1327         
1328         if(lt->flag & LT_OUTSIDE) outside_lattice(lt);
1329 }
1330
1331 /* returns key coordinates (+ tilt) when key applied, NULL otherwise */
1332 float *do_ob_key(Scene *scene, Object *ob)
1333 {
1334         Key *key= ob_get_key(ob);
1335         KeyBlock *actkb= ob_get_keyblock(ob);
1336         char *out;
1337         int tot= 0, size= 0;
1338         
1339         if(key==NULL || key->block.first==NULL)
1340                 return NULL;
1341
1342         /* compute size of output array */
1343         if(ob->type == OB_MESH) {
1344                 Mesh *me= ob->data;
1345
1346                 tot= me->totvert;
1347                 size= tot*3*sizeof(float);
1348         }
1349         else if(ob->type == OB_LATTICE) {
1350                 Lattice *lt= ob->data;
1351
1352                 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1353                 size= tot*3*sizeof(float);
1354         }
1355         else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1356                 Curve *cu= ob->data;
1357                 Nurb *nu;
1358
1359                 for(nu=cu->nurb.first; nu; nu=nu->next) {
1360                         if(nu->bezt) {
1361                                 tot += 3*nu->pntsu;
1362                                 size += nu->pntsu*12*sizeof(float);
1363                         }
1364                         else if(nu->bp) {
1365                                 tot += nu->pntsu*nu->pntsv;
1366                                 size += nu->pntsu*nu->pntsv*12*sizeof(float);
1367                         }
1368                 }
1369         }
1370
1371         /* if nothing to interpolate, cancel */
1372         if(tot == 0 || size == 0)
1373                 return NULL;
1374         
1375         /* allocate array */
1376         out= MEM_callocN(size, "do_ob_key out");
1377
1378         /* prevent python from screwing this up? anyhoo, the from pointer could be dropped */
1379         key->from= (ID *)ob->data;
1380                 
1381         if(ob->shapeflag & OB_SHAPE_LOCK) {
1382                 /* shape locked, copy the locked shape instead of blending */
1383                 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1384                 
1385                 if(kb && (kb->flag & KEYBLOCK_MUTE))
1386                         kb= key->refkey;
1387
1388                 if(kb==NULL) {
1389                         kb= key->block.first;
1390                         ob->shapenr= 1;
1391                 }
1392                 
1393                 if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1394                         float *weights= get_weights_array(ob, kb->vgroup);
1395
1396                         cp_key(0, tot, tot, (char*)out, key, actkb, kb, weights, 0);
1397
1398                         if(weights) MEM_freeN(weights);
1399                 }
1400                 else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1401                         cp_cu_key(ob->data, key, actkb, kb, 0, tot, out, tot);
1402         }
1403         else {
1404                 /* do shapekey local drivers */
1405                 float ctime= (float)scene->r.cfra; // XXX this needs to be checked
1406                 
1407                 BKE_animsys_evaluate_animdata(&key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
1408                 
1409                 if(ob->type==OB_MESH) do_mesh_key(scene, ob, key, out, tot);
1410                 else if(ob->type==OB_LATTICE) do_latt_key(scene, ob, key, out, tot);
1411                 else if(ob->type==OB_CURVE) do_curve_key(scene, ob, key, out, tot);
1412                 else if(ob->type==OB_SURF) do_curve_key(scene, ob, key, out, tot);
1413         }
1414         
1415         return (float*)out;
1416 }
1417
1418 Key *ob_get_key(Object *ob)
1419 {
1420         if(ob==NULL) return NULL;
1421         
1422         if(ob->type==OB_MESH) {
1423                 Mesh *me= ob->data;
1424                 return me->key;
1425         }
1426         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1427                 Curve *cu= ob->data;
1428                 return cu->key;
1429         }
1430         else if(ob->type==OB_LATTICE) {
1431                 Lattice *lt= ob->data;
1432                 return lt->key;
1433         }
1434         return NULL;
1435 }
1436
1437 KeyBlock *add_keyblock(Key *key, const char *name)
1438 {
1439         KeyBlock *kb;
1440         float curpos= -0.1;
1441         int tot;
1442         
1443         kb= key->block.last;
1444         if(kb) curpos= kb->pos;
1445         
1446         kb= MEM_callocN(sizeof(KeyBlock), "Keyblock");
1447         BLI_addtail(&key->block, kb);
1448         kb->type= KEY_CARDINAL;
1449         
1450         tot= BLI_countlist(&key->block);
1451         if(name) {
1452                 strncpy(kb->name, name, sizeof(kb->name));
1453         } else {
1454                 if(tot==1) strcpy(kb->name, "Basis");
1455                 else sprintf(kb->name, "Key %d", tot-1);
1456         }
1457
1458         BLI_uniquename(&key->block, kb, "Key", '.', offsetof(KeyBlock, name), sizeof(kb->name));
1459
1460         // XXX this is old anim system stuff? (i.e. the 'index' of the shapekey)
1461         kb->adrcode= tot-1;
1462         
1463         key->totkey++;
1464         if(key->totkey==1) key->refkey= kb;
1465         
1466         kb->slidermin= 0.0f;
1467         kb->slidermax= 1.0f;
1468         
1469         // XXX kb->pos is the confusing old horizontal-line RVK crap in old IPO Editor...
1470         if(key->type == KEY_RELATIVE) 
1471                 kb->pos= curpos+0.1;
1472         else {
1473 #if 0 // XXX old animation system
1474                 curpos= bsystem_time(scene, 0, (float)CFRA, 0.0);
1475                 if(calc_ipo_spec(key->ipo, KEY_SPEED, &curpos)==0) {
1476                         curpos /= 100.0;
1477                 }
1478                 kb->pos= curpos;
1479                 
1480                 sort_keys(key);
1481 #endif // XXX old animation system
1482         }
1483         return kb;
1484 }
1485
1486 /* only the active keyblock */
1487 KeyBlock *ob_get_keyblock(Object *ob) 
1488 {
1489         Key *key= ob_get_key(ob);
1490         
1491         if (key) {
1492                 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1493                 return kb;
1494         }
1495
1496         return NULL;
1497 }
1498
1499 KeyBlock *ob_get_reference_keyblock(Object *ob)
1500 {
1501         Key *key= ob_get_key(ob);
1502         
1503         if (key)
1504                 return key->refkey;
1505
1506         return NULL;
1507 }
1508
1509 /* get the appropriate KeyBlock given an index */
1510 KeyBlock *key_get_keyblock(Key *key, int index)
1511 {
1512         KeyBlock *kb;
1513         int i;
1514         
1515         if (key) {
1516                 kb= key->block.first;
1517                 
1518                 for (i= 1; i < key->totkey; i++) {
1519                         kb= kb->next;
1520                         
1521                         if (index==i)
1522                                 return kb;
1523                 }
1524         }
1525         
1526         return NULL;
1527 }
1528
1529 /* get the appropriate KeyBlock given a name to search for */
1530 KeyBlock *key_get_named_keyblock(Key *key, const char name[])
1531 {
1532         if (key && name)
1533                 return BLI_findstring(&key->block, name, offsetof(KeyBlock, name));
1534         
1535         return NULL;
1536 }
1537
1538 /* Get RNA-Path for 'value' setting of the given ShapeKey 
1539  * NOTE: the user needs to free the returned string once they're finishe with it
1540  */
1541 char *key_get_curValue_rnaPath(Key *key, KeyBlock *kb)
1542 {
1543         PointerRNA ptr;
1544         PropertyRNA *prop;
1545         
1546         /* sanity checks */
1547         if ELEM(NULL, key, kb)
1548                 return NULL;
1549         
1550         /* create the RNA pointer */
1551         RNA_pointer_create(&key->id, &RNA_ShapeKey, kb, &ptr);
1552         /* get pointer to the property too */
1553         prop= RNA_struct_find_property(&ptr, "value");
1554         
1555         /* return the path */
1556         return RNA_path_from_ID_to_property(&ptr, prop);
1557 }
1558
1559
1560 /* conversion functions */
1561
1562 /************************* Lattice ************************/
1563 void latt_to_key(Lattice *lt, KeyBlock *kb)
1564 {
1565         BPoint *bp;
1566         float *fp;
1567         int a, tot;
1568
1569         tot= lt->pntsu*lt->pntsv*lt->pntsw;
1570         if(tot==0) return;
1571
1572         if(kb->data) MEM_freeN(kb->data);
1573
1574         kb->data= MEM_callocN(lt->key->elemsize*tot, "kb->data");
1575         kb->totelem= tot;
1576
1577         bp= lt->def;
1578         fp= kb->data;
1579         for(a=0; a<kb->totelem; a++, fp+=3, bp++) {
1580                 VECCOPY(fp, bp->vec);
1581         }
1582 }
1583
1584 void key_to_latt(KeyBlock *kb, Lattice *lt)
1585 {
1586         BPoint *bp;
1587         float *fp;
1588         int a, tot;
1589
1590         bp= lt->def;
1591         fp= kb->data;
1592
1593         tot= lt->pntsu*lt->pntsv*lt->pntsw;
1594         tot= MIN2(kb->totelem, tot);
1595
1596         for(a=0; a<tot; a++, fp+=3, bp++) {
1597                 VECCOPY(bp->vec, fp);
1598         }
1599 }
1600
1601 /************************* Curve ************************/
1602 void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb)
1603 {
1604         Nurb *nu;
1605         BezTriple *bezt;
1606         BPoint *bp;
1607         float *fp;
1608         int a, tot;
1609
1610         /* count */
1611         tot= count_curveverts(nurb);
1612         if(tot==0) return;
1613
1614         if(kb->data) MEM_freeN(kb->data);
1615
1616         kb->data= MEM_callocN(cu->key->elemsize*tot, "kb->data");
1617         kb->totelem= tot;
1618
1619         nu= nurb->first;
1620         fp= kb->data;
1621         while(nu) {
1622
1623                 if(nu->bezt) {
1624                         bezt= nu->bezt;
1625                         a= nu->pntsu;
1626                         while(a--) {
1627                                 VECCOPY(fp, bezt->vec[0]);
1628                                 fp+= 3;
1629                                 VECCOPY(fp, bezt->vec[1]);
1630                                 fp+= 3;
1631                                 VECCOPY(fp, bezt->vec[2]);
1632                                 fp+= 3;
1633                                 fp[0]= bezt->alfa;
1634                                 fp+= 3; /* alphas */
1635                                 bezt++;
1636                         }
1637                 }
1638                 else {
1639                         bp= nu->bp;
1640                         a= nu->pntsu*nu->pntsv;
1641                         while(a--) {
1642                                 VECCOPY(fp, bp->vec);
1643                                 fp[3]= bp->alfa;
1644
1645                                 fp+= 4;
1646                                 bp++;
1647                         }
1648                 }
1649                 nu= nu->next;
1650         }
1651 }
1652
1653 void key_to_curve(KeyBlock *kb, Curve *UNUSED(cu), ListBase *nurb)
1654 {
1655         Nurb *nu;
1656         BezTriple *bezt;
1657         BPoint *bp;
1658         float *fp;
1659         int a, tot;
1660
1661         nu= nurb->first;
1662         fp= kb->data;
1663
1664         tot= count_curveverts(nurb);
1665
1666         tot= MIN2(kb->totelem, tot);
1667
1668         while(nu && tot>0) {
1669
1670                 if(nu->bezt) {
1671                         bezt= nu->bezt;
1672                         a= nu->pntsu;
1673                         while(a-- && tot>0) {
1674                                 VECCOPY(bezt->vec[0], fp);
1675                                 fp+= 3;
1676                                 VECCOPY(bezt->vec[1], fp);
1677                                 fp+= 3;
1678                                 VECCOPY(bezt->vec[2], fp);
1679                                 fp+= 3;
1680                                 bezt->alfa= fp[0];
1681                                 fp+= 3; /* alphas */
1682
1683                                 tot-= 3;
1684                                 bezt++;
1685                         }
1686                 }
1687                 else {
1688                         bp= nu->bp;
1689                         a= nu->pntsu*nu->pntsv;
1690                         while(a-- && tot>0) {
1691                                 VECCOPY(bp->vec, fp);
1692                                 bp->alfa= fp[3];
1693
1694                                 fp+= 4;
1695                                 tot--;
1696                                 bp++;
1697                         }
1698                 }
1699                 nu= nu->next;
1700         }
1701 }
1702
1703 /************************* Mesh ************************/
1704 void mesh_to_key(Mesh *me, KeyBlock *kb)
1705 {
1706         MVert *mvert;
1707         float *fp;
1708         int a;
1709
1710         if(me->totvert==0) return;
1711
1712         if(kb->data) MEM_freeN(kb->data);
1713
1714         kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data");
1715         kb->totelem= me->totvert;
1716
1717         mvert= me->mvert;
1718         fp= kb->data;
1719         for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
1720                 VECCOPY(fp, mvert->co);
1721
1722         }
1723 }
1724
1725 void key_to_mesh(KeyBlock *kb, Mesh *me)
1726 {
1727         MVert *mvert;
1728         float *fp;
1729         int a, tot;
1730
1731         mvert= me->mvert;
1732         fp= kb->data;
1733
1734         tot= MIN2(kb->totelem, me->totvert);
1735
1736         for(a=0; a<tot; a++, fp+=3, mvert++) {
1737                 VECCOPY(mvert->co, fp);
1738         }
1739 }
1740
1741 /************************* vert coords ************************/
1742 float (*key_to_vertcos(Object *ob, KeyBlock *kb))[3]
1743 {
1744         float (*vertCos)[3], *co;
1745         float *fp= kb->data;
1746         int tot= 0, a;
1747
1748         /* Count of vertex coords in array */
1749         if(ob->type == OB_MESH) {
1750                 Mesh *me= (Mesh*)ob->data;
1751                 tot= me->totvert;
1752         } else if(ob->type == OB_LATTICE) {
1753                 Lattice *lt= (Lattice*)ob->data;
1754                 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1755         } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1756                 Curve *cu= (Curve*)ob->data;
1757                 tot= count_curveverts(&cu->nurb);
1758         }
1759
1760         if (tot == 0) return NULL;
1761
1762         vertCos= MEM_callocN(tot*sizeof(*vertCos), "key_to_vertcos vertCos");
1763
1764         /* Copy coords to array */
1765         co= (float*)vertCos;
1766
1767         if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1768                 for (a= 0; a<tot; a++, fp+=3, co+=3) {
1769                         copy_v3_v3(co, fp);
1770                 }
1771         } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1772                 Curve *cu= (Curve*)ob->data;
1773                 Nurb *nu= cu->nurb.first;
1774                 BezTriple *bezt;
1775                 BPoint *bp;
1776
1777                 while (nu) {
1778                         if(nu->bezt) {
1779                                 int i;
1780                                 bezt= nu->bezt;
1781                                 a= nu->pntsu;
1782
1783                                 while (a--) {
1784                                         for (i= 0; i<3; i++) {
1785                                                 copy_v3_v3(co, fp);
1786                                                 fp+= 3; co+= 3;
1787                                         }
1788
1789                                         fp+= 3; /* skip alphas */
1790
1791                                         bezt++;
1792                                 }
1793                         }
1794                         else {
1795                                 bp= nu->bp;
1796                                 a= nu->pntsu*nu->pntsv;
1797
1798                                 while (a--) {
1799                                         copy_v3_v3(co, fp);
1800
1801                                         fp+= 4;
1802                                         co+= 3;
1803
1804                                         bp++;
1805                                 }
1806                         }
1807
1808                         nu= nu->next;
1809                 }
1810         }
1811
1812         return vertCos;
1813 }
1814
1815 void vertcos_to_key(Object *ob, KeyBlock *kb, float (*vertCos)[3])
1816 {
1817         float *co= (float*)vertCos, *fp;
1818         int tot= 0, a, elemsize;
1819
1820         if (kb->data) MEM_freeN(kb->data);
1821
1822         /* Count of vertex coords in array */
1823         if(ob->type == OB_MESH) {
1824                 Mesh *me= (Mesh*)ob->data;
1825                 tot= me->totvert;
1826                 elemsize= me->key->elemsize;
1827         } else if(ob->type == OB_LATTICE) {
1828                 Lattice *lt= (Lattice*)ob->data;
1829                 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1830                 elemsize= lt->key->elemsize;
1831         } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1832                 Curve *cu= (Curve*)ob->data;
1833                 elemsize= cu->key->elemsize;
1834                 tot= count_curveverts(&cu->nurb);
1835         }
1836
1837         if (tot == 0) {
1838                 kb->data= NULL;
1839                 return;
1840         }
1841
1842         fp= kb->data= MEM_callocN(tot*elemsize, "key_to_vertcos vertCos");
1843
1844         /* Copy coords to keyblock */
1845
1846         if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1847                 for (a= 0; a<tot; a++, fp+=3, co+=3) {
1848                         copy_v3_v3(fp, co);
1849                 }
1850         } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1851                 Curve *cu= (Curve*)ob->data;
1852                 Nurb *nu= cu->nurb.first;
1853                 BezTriple *bezt;
1854                 BPoint *bp;
1855
1856                 while (nu) {
1857                         if(nu->bezt) {
1858                                 int i;
1859                                 bezt= nu->bezt;
1860                                 a= nu->pntsu;
1861
1862                                 while (a--) {
1863                                         for (i= 0; i<3; i++) {
1864                                                 copy_v3_v3(fp, co);
1865                                                 fp+= 3; co+= 3;
1866                                         }
1867
1868                                         fp+= 3; /* skip alphas */
1869
1870                                         bezt++;
1871                                 }
1872                         }
1873                         else {
1874                                 bp= nu->bp;
1875                                 a= nu->pntsu*nu->pntsv;
1876
1877                                 while (a--) {
1878                                         copy_v3_v3(fp, co);
1879
1880                                         fp+= 4;
1881                                         co+= 3;
1882
1883                                         bp++;
1884                                 }
1885                         }
1886
1887                         nu= nu->next;
1888                 }
1889         }
1890 }
1891
1892 void offset_to_key(Object *ob, KeyBlock *kb, float (*ofs)[3])
1893 {
1894         int a;
1895         float *co= (float*)ofs, *fp= kb->data;
1896
1897         if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1898                 for (a= 0; a<kb->totelem; a++, fp+=3, co+=3) {
1899                         add_v3_v3(fp, co);
1900                 }
1901         } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1902                 Curve *cu= (Curve*)ob->data;
1903                 Nurb *nu= cu->nurb.first;
1904                 BezTriple *bezt;
1905                 BPoint *bp;
1906
1907                 while (nu) {
1908                         if(nu->bezt) {
1909                                 int i;
1910                                 bezt= nu->bezt;
1911                                 a= nu->pntsu;
1912
1913                                 while (a--) {
1914                                         for (i= 0; i<3; i++) {
1915                                                 add_v3_v3(fp, co);
1916                                                 fp+= 3; co+= 3;
1917                                         }
1918
1919                                         fp+= 3; /* skip alphas */
1920
1921                                         bezt++;
1922                                 }
1923                         }
1924                         else {
1925                                 bp= nu->bp;
1926                                 a= nu->pntsu*nu->pntsv;
1927
1928                                 while (a--) {
1929                                         add_v3_v3(fp, co);
1930
1931                                         fp+= 4;
1932                                         co+= 3;
1933
1934                                         bp++;
1935                                 }
1936                         }
1937
1938                         nu= nu->next;
1939                 }
1940         }
1941 }