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