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