Sculpt+shape keys:
[blender-staging.git] / source / blender / blenkernel / intern / key.c
1
2 /*  key.c      
3  *  
4  * 
5  * $Id$
6  *
7  * ***** BEGIN GPL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 #include <math.h>
34 #include <string.h>
35 #include <stddef.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_editVert.h"
41 #include "BLI_math_vector.h"
42
43 #include "DNA_anim_types.h"
44 #include "DNA_key_types.h"
45 #include "DNA_lattice_types.h"
46 #include "DNA_meshdata_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49
50 #include "BKE_animsys.h"
51 #include "BKE_curve.h"
52 #include "BKE_customdata.h"
53 #include "BKE_global.h"
54 #include "BKE_key.h"
55 #include "BKE_lattice.h"
56 #include "BKE_library.h"
57 #include "BKE_main.h"
58 #include "BKE_object.h"
59 #include "BKE_utildefines.h"
60
61 #include "RNA_access.h"
62
63
64 #define KEY_BPOINT              1
65 #define KEY_BEZTRIPLE   2
66
67         // old defines from DNA_ipo_types.h for data-type
68 #define IPO_FLOAT               4
69 #define IPO_BEZTRIPLE   100
70 #define IPO_BPOINT              101
71
72 int slurph_opt= 1;
73
74
75 void free_key(Key *key)
76 {
77         KeyBlock *kb;
78         
79         BKE_free_animdata((ID *)key);
80         
81         while( (kb= key->block.first) ) {
82                 
83                 if(kb->data) MEM_freeN(kb->data);
84                 
85                 BLI_remlink(&key->block, kb);
86                 MEM_freeN(kb);
87         }
88         
89 }
90
91 /* GS reads the memory pointed at in a specific ordering. There are,
92  * however two definitions for it. I have jotted them down here, both,
93  * but I think the first one is actually used. The thing is that
94  * big-endian systems might read this the wrong way round. OTOH, we
95  * constructed the IDs that are read out with this macro explicitly as
96  * well. I expect we'll sort it out soon... */
97
98 /* from blendef: */
99 #define GS(a)   (*((short *)(a)))
100
101 /* from misc_util: flip the bytes from x  */
102 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
103
104 Key *add_key(ID *id)    /* common function */
105 {
106         Key *key;
107         char *el;
108         
109         key= alloc_libblock(&G.main->key, ID_KE, "Key");
110         
111         key->type= KEY_NORMAL;
112         key->from= id;
113         
114         // XXX the code here uses some defines which will soon be depreceated...
115         if( GS(id->name)==ID_ME) {
116                 el= key->elemstr;
117                 
118                 el[0]= 3;
119                 el[1]= IPO_FLOAT;
120                 el[2]= 0;
121                 
122                 key->elemsize= 12;
123         }
124         else if( GS(id->name)==ID_LT) {
125                 el= key->elemstr;
126                 
127                 el[0]= 3;
128                 el[1]= IPO_FLOAT;
129                 el[2]= 0;
130                 
131                 key->elemsize= 12;
132         }
133         else if( GS(id->name)==ID_CU) {
134                 el= key->elemstr;
135                 
136                 el[0]= 4;
137                 el[1]= IPO_BPOINT;
138                 el[2]= 0;
139                 
140                 key->elemsize= 16;
141         }
142         
143         return key;
144 }
145
146 Key *copy_key(Key *key)
147 {
148         Key *keyn;
149         KeyBlock *kbn, *kb;
150         
151         if(key==0) return 0;
152         
153         keyn= copy_libblock(key);
154         
155 #if 0 // XXX old animation system
156         keyn->ipo= copy_ipo(key->ipo);
157 #endif // XXX old animation system
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==0) return;
183         
184         key->id.lib= 0;
185         new_id(0, (ID *)key, 0);
186
187 #if 0 // XXX old animation system
188         make_local_ipo(key->ipo);
189 #endif // XXX old animation system
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==0) return 1;
375
376         if(cycl) {      /* pre-sort */
377                 k[2]= k1->next;
378                 k[3]= k[2]->next;
379                 if(k[3]==0) k[3]=k1;
380                 while(k1) {
381                         if(k1->next==0) 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]==0) 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==0) {
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.1+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.0) {
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 aantal, float *in, float *f0, float *f1, float *f2, float *f3, float *t)  
478 {
479         int a;
480
481         for(a=0; a<aantal; 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 aantal, float *in, float *ref, float *out, float fac)
487 {
488         int a;
489         
490         for(a=0; a<aantal; 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 static void cp_key(int start, int end, int tot, char *poin, Key *key, KeyBlock *actkb, KeyBlock *kb, float *weights, int mode)
526 {
527         float ktot = 0.0, kd = 0.0;
528         int elemsize, poinsize = 0, a, *ofsp, ofs[32], flagflo=0;
529         char *k1, *kref, *freek1, *freekref;
530         char *cp, elemstr[8];
531
532         if(key->from==NULL) return;
533
534         if( GS(key->from->name)==ID_ME ) {
535                 ofs[0]= sizeof(float)*3;
536                 ofs[1]= 0;
537                 poinsize= ofs[0];
538         }
539         else if( GS(key->from->name)==ID_LT ) {
540                 ofs[0]= sizeof(float)*3;
541                 ofs[1]= 0;
542                 poinsize= ofs[0];
543         }
544         else if( GS(key->from->name)==ID_CU ) {
545                 if(mode==KEY_BPOINT) ofs[0]= sizeof(float)*4;
546                 else ofs[0]= sizeof(float)*10;
547                 
548                 ofs[1]= 0;
549                 poinsize= ofs[0];
550         }
551
552         if(end>tot) end= tot;
553         
554         if(tot != kb->totelem) {
555                 ktot= 0.0;
556                 flagflo= 1;
557                 if(kb->totelem) {
558                         kd= kb->totelem/(float)tot;
559                 }
560                 else return;
561         }
562
563         k1= key_block_get_data(key, actkb, kb, &freek1);
564         kref= key_block_get_data(key, actkb, key->refkey, &freekref);
565
566         /* this exception is needed for slurphing */
567         if(start!=0) {
568                 
569                 poin+= poinsize*start;
570                 
571                 if(flagflo) {
572                         ktot+= start*kd;
573                         a= (int)floor(ktot);
574                         if(a) {
575                                 ktot-= a;
576                                 k1+= a*key->elemsize;
577                         }
578                 }
579                 else k1+= start*key->elemsize;
580         }       
581         
582         if(mode==KEY_BEZTRIPLE) {
583                 elemstr[0]= 1;
584                 elemstr[1]= IPO_BEZTRIPLE;
585                 elemstr[2]= 0;
586         }
587         
588         /* just do it here, not above! */
589         elemsize= key->elemsize;
590         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
591
592         for(a=start; a<end; a++) {
593                 cp= key->elemstr;
594                 if(mode==KEY_BEZTRIPLE) cp= elemstr;
595
596                 ofsp= ofs;
597                 
598                 while( cp[0] ) {
599                         
600                         switch(cp[1]) {
601                         case IPO_FLOAT:
602                                 if(weights) {
603                                         memcpy(poin, kref, sizeof(float)*3);
604                                         if(*weights!=0.0f)
605                                                 rel_flerp(cp[0], (float *)poin, (float *)kref, (float *)k1, *weights);
606                                         weights++;
607                                 }
608                                 else 
609                                         memcpy(poin, k1, sizeof(float)*3);
610                                 break;
611                         case IPO_BPOINT:
612                                 memcpy(poin, k1, sizeof(float)*4);
613                                 break;
614                         case IPO_BEZTRIPLE:
615                                 memcpy(poin, k1, sizeof(float)*10);
616                                 break;
617                         }
618                         
619                         poin+= ofsp[0]; 
620                         cp+= 2; ofsp++;
621                 }
622                 
623                 /* are we going to be nasty? */
624                 if(flagflo) {
625                         ktot+= kd;
626                         while(ktot>=1.0) {
627                                 ktot-= 1.0;
628                                 k1+= elemsize;
629                                 kref+= elemsize;
630                         }
631                 }
632                 else {
633                         k1+= elemsize;
634                         kref+= elemsize;
635                 }
636                 
637                 if(mode==KEY_BEZTRIPLE) a+=2;
638         }
639
640         if(freek1) MEM_freeN(freek1);
641         if(freekref) MEM_freeN(freekref);
642 }
643
644 static void cp_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock *kb, int start, int end, char *out, int tot)
645 {
646         Nurb *nu;
647         char *poin;
648         int a, step, a1, a2;
649
650         for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
651                 if(nu->bp) {
652                         step= nu->pntsu*nu->pntsv;
653                         
654                         /* exception because keys prefer to work with complete blocks */
655                         poin= out - a*sizeof(float)*4;
656                         a1= MAX2(a, start);
657                         a2= MIN2(a+step, end);
658                         
659                         if(a1<a2) cp_key(a1, a2, tot, poin, key, actkb, kb, NULL, KEY_BPOINT);
660                 }
661                 else if(nu->bezt) {
662                         step= 3*nu->pntsu;
663                         
664                         poin= out - a*sizeof(float)*10;
665                         a1= MAX2(a, start);
666                         a2= MIN2(a+step, end);
667
668                         if(a1<a2) cp_key(a1, a2, tot, poin, key, actkb, kb, NULL, KEY_BEZTRIPLE);
669                 }
670                 else
671                         step= 0;
672         }
673 }
674
675
676 void do_rel_key(int start, int end, int tot, char *basispoin, Key *key, KeyBlock *actkb, int mode)
677 {
678         KeyBlock *kb;
679         int *ofsp, ofs[3], elemsize, b;
680         char *cp, *poin, *reffrom, *from, elemstr[8];
681         char *freefrom, *freereffrom;
682         
683         if(key->from==NULL) return;
684         
685         if( GS(key->from->name)==ID_ME ) {
686                 ofs[0]= sizeof(float)*3;
687                 ofs[1]= 0;
688         }
689         else if( GS(key->from->name)==ID_LT ) {
690                 ofs[0]= sizeof(float)*3;
691                 ofs[1]= 0;
692         }
693         else if( GS(key->from->name)==ID_CU ) {
694                 if(mode==KEY_BPOINT) ofs[0]= sizeof(float)*4;
695                 else ofs[0]= sizeof(float)*10;
696                 
697                 ofs[1]= 0;
698         }
699         
700         if(end>tot) end= tot;
701         
702         /* in case of beztriple */
703         elemstr[0]= 1;                          /* nr of ipofloats */
704         elemstr[1]= IPO_BEZTRIPLE;
705         elemstr[2]= 0;
706
707         /* just here, not above! */
708         elemsize= key->elemsize;
709         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
710
711         /* step 1 init */
712         cp_key(start, end, tot, basispoin, key, actkb, key->refkey, NULL, mode);
713         
714         /* step 2: do it */
715         
716         for(kb=key->block.first; kb; kb=kb->next) {
717                 if(kb!=key->refkey) {
718                         float icuval= kb->curval;
719                         
720                         /* only with value, and no difference allowed */
721                         if(!(kb->flag & KEYBLOCK_MUTE) && icuval!=0.0f && kb->totelem==tot) {
722                                 KeyBlock *refb;
723                                 float weight, *weights= kb->weights;
724
725                                 /* reference now can be any block */
726                                 refb= BLI_findlink(&key->block, kb->relative);
727                                 if(refb==NULL) continue;
728                                 
729                                 poin= basispoin;
730                                 from= key_block_get_data(key, actkb, kb, &freefrom);
731                                 reffrom= key_block_get_data(key, actkb, refb, &freereffrom);
732                                 
733                                 poin+= start*ofs[0];
734                                 reffrom+= key->elemsize*start;  // key elemsize yes!
735                                 from+= key->elemsize*start;
736                                 
737                                 for(b=start; b<end; b++) {
738                                 
739                                         if(weights) 
740                                                 weight= *weights * icuval;
741                                         else
742                                                 weight= icuval;
743                                         
744                                         cp= key->elemstr;       
745                                         if(mode==KEY_BEZTRIPLE) cp= elemstr;
746                                         
747                                         ofsp= ofs;
748                                         
749                                         while( cp[0] ) {        /* cp[0]==amount */
750                                                 
751                                                 switch(cp[1]) {
752                                                 case IPO_FLOAT:
753                                                         rel_flerp(3, (float *)poin, (float *)reffrom, (float *)from, weight);
754                                                         break;
755                                                 case IPO_BPOINT:
756                                                         rel_flerp(4, (float *)poin, (float *)reffrom, (float *)from, weight);
757                                                         break;
758                                                 case IPO_BEZTRIPLE:
759                                                         rel_flerp(10, (float *)poin, (float *)reffrom, (float *)from, weight);
760                                                         break;
761                                                 }
762                                                 
763                                                 poin+= ofsp[0];                         
764                                                 
765                                                 cp+= 2;
766                                                 ofsp++;
767                                         }
768                                         
769                                         reffrom+= elemsize;
770                                         from+= elemsize;
771                                         
772                                         if(mode==KEY_BEZTRIPLE) b+= 2;
773                                         if(weights) weights++;
774                                 }
775
776                                 if(freefrom) MEM_freeN(freefrom);
777                                 if(freereffrom) MEM_freeN(freereffrom);
778                         }
779                 }
780         }
781 }
782
783
784 static void do_key(int start, int end, int tot, char *poin, Key *key, KeyBlock *actkb, KeyBlock **k, float *t, int mode)
785 {
786         float k1tot = 0.0, k2tot = 0.0, k3tot = 0.0, k4tot = 0.0;
787         float k1d = 0.0, k2d = 0.0, k3d = 0.0, k4d = 0.0;
788         int a, ofs[32], *ofsp;
789         int flagdo= 15, flagflo=0, elemsize, poinsize=0;
790         char *k1, *k2, *k3, *k4, *freek1, *freek2, *freek3, *freek4;
791         char *cp, elemstr[8];;
792
793         if(key->from==0) return;
794
795         if( GS(key->from->name)==ID_ME ) {
796                 ofs[0]= sizeof(float)*3;
797                 ofs[1]= 0;
798                 poinsize= ofs[0];
799         }
800         else if( GS(key->from->name)==ID_LT ) {
801                 ofs[0]= sizeof(float)*3;
802                 ofs[1]= 0;
803                 poinsize= ofs[0];
804         }
805         else if( GS(key->from->name)==ID_CU ) {
806                 if(mode==KEY_BPOINT) ofs[0]= sizeof(float)*4;
807                 else ofs[0]= sizeof(float)*10;
808                 
809                 ofs[1]= 0;
810                 poinsize= ofs[0];
811         }
812         
813         if(end>tot) end= tot;
814
815         k1= key_block_get_data(key, actkb, k[0], &freek1);
816         k2= key_block_get_data(key, actkb, k[1], &freek2);
817         k3= key_block_get_data(key, actkb, k[2], &freek3);
818         k4= key_block_get_data(key, actkb, k[3], &freek4);
819
820         /*  test for more or less points (per key!) */
821         if(tot != k[0]->totelem) {
822                 k1tot= 0.0;
823                 flagflo |= 1;
824                 if(k[0]->totelem) {
825                         k1d= k[0]->totelem/(float)tot;
826                 }
827                 else flagdo -= 1;
828         }
829         if(tot != k[1]->totelem) {
830                 k2tot= 0.0;
831                 flagflo |= 2;
832                 if(k[0]->totelem) {
833                         k2d= k[1]->totelem/(float)tot;
834                 }
835                 else flagdo -= 2;
836         }
837         if(tot != k[2]->totelem) {
838                 k3tot= 0.0;
839                 flagflo |= 4;
840                 if(k[0]->totelem) {
841                         k3d= k[2]->totelem/(float)tot;
842                 }
843                 else flagdo -= 4;
844         }
845         if(tot != k[3]->totelem) {
846                 k4tot= 0.0;
847                 flagflo |= 8;
848                 if(k[0]->totelem) {
849                         k4d= k[3]->totelem/(float)tot;
850                 }
851                 else flagdo -= 8;
852         }
853
854                 /* this exception needed for slurphing */
855         if(start!=0) {
856
857                 poin+= poinsize*start;
858                 
859                 if(flagdo & 1) {
860                         if(flagflo & 1) {
861                                 k1tot+= start*k1d;
862                                 a= (int)floor(k1tot);
863                                 if(a) {
864                                         k1tot-= a;
865                                         k1+= a*key->elemsize;
866                                 }
867                         }
868                         else k1+= start*key->elemsize;
869                 }
870                 if(flagdo & 2) {
871                         if(flagflo & 2) {
872                                 k2tot+= start*k2d;
873                                 a= (int)floor(k2tot);
874                                 if(a) {
875                                         k2tot-= a;
876                                         k2+= a*key->elemsize;
877                                 }
878                         }
879                         else k2+= start*key->elemsize;
880                 }
881                 if(flagdo & 4) {
882                         if(flagflo & 4) {
883                                 k3tot+= start*k3d;
884                                 a= (int)floor(k3tot);
885                                 if(a) {
886                                         k3tot-= a;
887                                         k3+= a*key->elemsize;
888                                 }
889                         }
890                         else k3+= start*key->elemsize;
891                 }
892                 if(flagdo & 8) {
893                         if(flagflo & 8) {
894                                 k4tot+= start*k4d;
895                                 a= (int)floor(k4tot);
896                                 if(a) {
897                                         k4tot-= a;
898                                         k4+= a*key->elemsize;
899                                 }
900                         }
901                         else k4+= start*key->elemsize;
902                 }
903
904         }
905
906         /* in case of beztriple */
907         elemstr[0]= 1;                          /* nr of ipofloats */
908         elemstr[1]= IPO_BEZTRIPLE;
909         elemstr[2]= 0;
910
911         /* only here, not above! */
912         elemsize= key->elemsize;
913         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
914
915         for(a=start; a<end; a++) {
916         
917                 cp= key->elemstr;       
918                 if(mode==KEY_BEZTRIPLE) cp= elemstr;
919                 
920                 ofsp= ofs;
921                 
922                 while( cp[0] ) {        /* cp[0]==amount */
923                         
924                         switch(cp[1]) {
925                         case IPO_FLOAT:
926                                 flerp(3, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
927                                 break;
928                         case IPO_BPOINT:
929                                 flerp(4, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
930                                 break;
931                         case IPO_BEZTRIPLE:
932                                 flerp(10, (void *)poin, (void *)k1, (void *)k2, (void *)k3, (void *)k4, t);
933                                 break;
934                         }
935                         
936                         poin+= ofsp[0];                         
937                         cp+= 2;
938                         ofsp++;
939                 }
940                 /* lets do it the difficult way: when keys have a different size */
941                 if(flagdo & 1) {
942                         if(flagflo & 1) {
943                                 k1tot+= k1d;
944                                 while(k1tot>=1.0) {
945                                         k1tot-= 1.0;
946                                         k1+= elemsize;
947                                 }
948                         }
949                         else k1+= elemsize;
950                 }
951                 if(flagdo & 2) {
952                         if(flagflo & 2) {
953                                 k2tot+= k2d;
954                                 while(k2tot>=1.0) {
955                                         k2tot-= 1.0;
956                                         k2+= elemsize;
957                                 }
958                         }
959                         else k2+= elemsize;
960                 }
961                 if(flagdo & 4) {
962                         if(flagflo & 4) {
963                                 k3tot+= k3d;
964                                 while(k3tot>=1.0) {
965                                         k3tot-= 1.0;
966                                         k3+= elemsize;
967                                 }
968                         }
969                         else k3+= elemsize;
970                 }
971                 if(flagdo & 8) {
972                         if(flagflo & 8) {
973                                 k4tot+= k4d;
974                                 while(k4tot>=1.0) {
975                                         k4tot-= 1.0;
976                                         k4+= elemsize;
977                                 }
978                         }
979                         else k4+= elemsize;
980                 }
981                 
982                 if(mode==KEY_BEZTRIPLE) a+= 2;
983         }
984
985         if(freek1) MEM_freeN(freek1);
986         if(freek2) MEM_freeN(freek2);
987         if(freek3) MEM_freeN(freek3);
988         if(freek4) MEM_freeN(freek4);
989 }
990
991 static float *get_weights_array(Object *ob, char *vgroup)
992 {
993         bDeformGroup *curdef;
994         MDeformVert *dvert= NULL;
995         EditMesh *em= NULL;
996         EditVert *eve;
997         int totvert= 0, index= 0;
998         
999         /* no vgroup string set? */
1000         if(vgroup[0]==0) return NULL;
1001         
1002         /* gather dvert and totvert */
1003         if(ob->type==OB_MESH) {
1004                 Mesh *me= ob->data;
1005                 dvert= me->dvert;
1006                 totvert= me->totvert;
1007
1008                 if(me->edit_mesh && me->edit_mesh->totvert == totvert)
1009                         em= me->edit_mesh;
1010         }
1011         else if(ob->type==OB_LATTICE) {
1012                 Lattice *lt= ob->data;
1013                 dvert= lt->dvert;
1014                 totvert= lt->pntsu*lt->pntsv*lt->pntsw;
1015         }
1016         
1017         if(dvert==NULL) return NULL;
1018         
1019         /* find the group (weak loop-in-loop) */
1020         for (curdef = ob->defbase.first; curdef; curdef=curdef->next, index++)
1021                 if (!strcmp(curdef->name, vgroup))
1022                         break;
1023
1024         if(curdef) {
1025                 float *weights;
1026                 int i, j;
1027                 
1028                 weights= MEM_callocN(totvert*sizeof(float), "weights");
1029
1030                 if(em) {
1031                         for(i=0, eve=em->verts.first; eve; eve=eve->next, i++) {
1032                                 dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
1033
1034                                 if(dvert) {
1035                                         for(j=0; j<dvert->totweight; j++) {
1036                                                 if(dvert->dw[j].def_nr == index) {
1037                                                         weights[i]= dvert->dw[j].weight;
1038                                                         break;
1039                                                 }
1040                                         }
1041                                 }
1042                         }
1043                 }
1044                 else {
1045                         for(i=0; i < totvert; i++, dvert++) {
1046                                 for(j=0; j<dvert->totweight; j++) {
1047                                         if(dvert->dw[j].def_nr == index) {
1048                                                 weights[i]= dvert->dw[j].weight;
1049                                                 break;
1050                                         }
1051                                 }
1052                         }
1053                 }
1054
1055                 return weights;
1056         }
1057         return NULL;
1058 }
1059
1060 static void do_mesh_key(Scene *scene, Object *ob, Key *key, char *out, int tot)
1061 {
1062         KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1063         float cfra, ctime, t[4], delta;
1064         int a, flag = 0, step;
1065         
1066         if(key->slurph && key->type!=KEY_RELATIVE ) {
1067                 delta= key->slurph;
1068                 delta/= tot;
1069                 
1070                 step= 1;
1071                 if(tot>100 && slurph_opt) {
1072                         step= tot/50;
1073                         delta*= step;
1074                         /* in do_key and cp_key the case a>tot is handled */
1075                 }
1076                 
1077                 cfra= (float)scene->r.cfra;
1078                 
1079                 for(a=0; a<tot; a+=step, cfra+= delta) {
1080                         
1081                         ctime= bsystem_time(scene, 0, cfra, 0.0); // xxx  ugly cruft!
1082 #if 0 // XXX old animation system
1083                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1084                                 ctime /= 100.0;
1085                                 CLAMP(ctime, 0.0, 1.0);
1086                         }
1087 #endif // XXX old animation system
1088                         // XXX for now... since speed curve cannot be directly ported yet
1089                         ctime /= 100.0f;
1090                         CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1091                 
1092                         flag= setkeys(ctime, &key->block, k, t, 0);
1093
1094                         if(flag==0)
1095                                 do_key(a, a+step, tot, (char *)out, key, actkb, k, t, 0);
1096                         else
1097                                 cp_key(a, a+step, tot, (char *)out, key, actkb, k[2], NULL, 0);
1098                 }
1099         }
1100         else {
1101                 if(key->type==KEY_RELATIVE) {
1102                         KeyBlock *kb;
1103                         
1104                         for(kb= key->block.first; kb; kb= kb->next)
1105                                 kb->weights= get_weights_array(ob, kb->vgroup);
1106
1107                         do_rel_key(0, tot, tot, (char *)out, key, actkb, 0);
1108                         
1109                         for(kb= key->block.first; kb; kb= kb->next) {
1110                                 if(kb->weights) MEM_freeN(kb->weights);
1111                                 kb->weights= NULL;
1112                         }
1113                 }
1114                 else {
1115                         ctime= bsystem_time(scene, ob, (float)scene->r.cfra, 0.0f); // xxx old cruft
1116                         
1117 #if 0 // XXX old animation system
1118                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1119                                 ctime /= 100.0;
1120                                 CLAMP(ctime, 0.0, 1.0);
1121                         }
1122 #endif // XXX old animation system
1123                         // XXX for now... since speed curve cannot be directly ported yet
1124                         ctime /= 100.0f;
1125                         CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1126                         
1127                         flag= setkeys(ctime, &key->block, k, t, 0);
1128
1129                         if(flag==0)
1130                                 do_key(0, tot, tot, (char *)out, key, actkb, k, t, 0);
1131                         else
1132                                 cp_key(0, tot, tot, (char *)out, key, actkb, k[2], NULL, 0);
1133                 }
1134         }
1135 }
1136
1137 static void do_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock **k, float *t, char *out, int tot)
1138 {
1139         Nurb *nu;
1140         char *poin;
1141         int a, step;
1142         
1143         for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
1144                 if(nu->bp) {
1145                         step= nu->pntsu*nu->pntsv;
1146                         poin= out - a*sizeof(float)*4;
1147                         do_key(a, a+step, tot, poin, key, actkb, k, t, KEY_BPOINT);
1148                 }
1149                 else if(nu->bezt) {
1150                         step= 3*nu->pntsu;
1151                         poin= out - a*sizeof(float)*10;
1152                         do_key(a, a+step, tot, poin, key, actkb, k, t, KEY_BEZTRIPLE);
1153                 }
1154                 else
1155                         step= 0;
1156         }
1157 }
1158
1159 static void do_rel_cu_key(Curve *cu, Key *key, KeyBlock *actkb, float ctime, char *out, int tot)
1160 {
1161         Nurb *nu;
1162         char *poin;
1163         int a, step;
1164         
1165         for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
1166                 if(nu->bp) {
1167                         step= nu->pntsu*nu->pntsv;
1168                         poin= out - a*sizeof(float)*3;
1169                         do_rel_key(a, a+step, tot, out, key, actkb, KEY_BPOINT);
1170                 }
1171                 else if(nu->bezt) {
1172                         step= 3*nu->pntsu;
1173                         poin= out - a*sizeof(float)*10;
1174                         do_rel_key(a, a+step, tot, poin, key, actkb, KEY_BEZTRIPLE);
1175                 }
1176                 else
1177                         step= 0;
1178         }
1179 }
1180
1181 static void do_curve_key(Scene *scene, Object *ob, Key *key, char *out, int tot)
1182 {
1183         Curve *cu= ob->data;
1184         KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1185         float cfra, ctime, t[4], delta;
1186         int a, flag = 0, step = 0;
1187         
1188         if(key->slurph) {
1189                 delta= key->slurph;
1190                 delta/= tot;
1191                 
1192                 step= 1;
1193                 if(tot>100 && slurph_opt) {
1194                         step= tot/50;
1195                         delta*= step;
1196                         /* in do_key and cp_key the case a>tot has been handled */
1197                 }
1198                 
1199                 cfra= (float)scene->r.cfra;
1200                 
1201                 for(a=0; a<tot; a+=step, cfra+= delta) {
1202                         ctime= bsystem_time(scene, 0, cfra, 0.0f); // XXX old cruft
1203 #if 0 // XXX old animation system
1204                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1205                                 ctime /= 100.0;
1206                                 CLAMP(ctime, 0.0, 1.0);
1207                         }
1208 #endif // XXX old animation system
1209                 
1210                         flag= setkeys(ctime, &key->block, k, t, 0);
1211                         
1212                         if(flag==0)
1213                                 do_key(a, a+step, tot, (char *)out, key, actkb, k, t, 0);
1214                         else
1215                                 cp_key(a, a+step, tot, (char *)out, key, actkb, k[2], NULL, 0);
1216                 }
1217         }
1218         else {
1219                 
1220                 ctime= bsystem_time(scene, NULL, (float)scene->r.cfra, 0.0);
1221                 
1222                 if(key->type==KEY_RELATIVE) {
1223                         do_rel_cu_key(cu, cu->key, actkb, ctime, out, tot);
1224                 }
1225                 else {
1226 #if 0 // XXX old animation system
1227                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1228                                 ctime /= 100.0;
1229                                 CLAMP(ctime, 0.0, 1.0);
1230                         }
1231 #endif // XXX old animation system
1232                         
1233                         flag= setkeys(ctime, &key->block, k, t, 0);
1234                         
1235                         if(flag==0) do_cu_key(cu, key, actkb, k, t, out, tot);
1236                         else cp_cu_key(cu, key, actkb, k[2], 0, tot, out, tot);
1237                 }
1238         }
1239 }
1240
1241 static void do_latt_key(Scene *scene, Object *ob, Key *key, char *out, int tot)
1242 {
1243         Lattice *lt= ob->data;
1244         KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1245         float delta, cfra, ctime, t[4];
1246         int a, flag;
1247         
1248         if(key->slurph) {
1249                 delta= key->slurph;
1250                 delta/= (float)tot;
1251                 
1252                 cfra= (float)scene->r.cfra;
1253                 
1254                 for(a=0; a<tot; a++, cfra+= delta) {
1255                         
1256                         ctime= bsystem_time(scene, 0, cfra, 0.0); // XXX old cruft
1257 #if 0 // XXX old animation system
1258                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1259                                 ctime /= 100.0;
1260                                 CLAMP(ctime, 0.0, 1.0);
1261                         }
1262 #endif // XXX old animation system
1263                 
1264                         flag= setkeys(ctime, &key->block, k, t, 0);
1265
1266                         if(flag==0)
1267                                 do_key(a, a+1, tot, (char *)out, key, actkb, k, t, 0);
1268                         else
1269                                 cp_key(a, a+1, tot, (char *)out, key, actkb, k[2], NULL, 0);
1270                 }               
1271         }
1272         else {
1273                 if(key->type==KEY_RELATIVE) {
1274                         KeyBlock *kb;
1275                         
1276                         for(kb= key->block.first; kb; kb= kb->next)
1277                                 kb->weights= get_weights_array(ob, kb->vgroup);
1278                         
1279                         do_rel_key(0, tot, tot, (char *)out, key, actkb, 0);
1280                         
1281                         for(kb= key->block.first; kb; kb= kb->next) {
1282                                 if(kb->weights) MEM_freeN(kb->weights);
1283                                 kb->weights= NULL;
1284                         }
1285                 }
1286                 else {
1287                         ctime= bsystem_time(scene, NULL, (float)scene->r.cfra, 0.0);
1288
1289 #if 0 // XXX old animation system
1290                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1291                                 ctime /= 100.0;
1292                                 CLAMP(ctime, 0.0, 1.0);
1293                         }
1294 #endif // XXX old animation system
1295                         
1296                         flag= setkeys(ctime, &key->block, k, t, 0);
1297
1298                         if(flag==0)
1299                                 do_key(0, tot, tot, (char *)out, key, actkb, k, t, 0);
1300                         else
1301                                 cp_key(0, tot, tot, (char *)out, key, actkb, k[2], NULL, 0);
1302                 }
1303         }
1304         
1305         if(lt->flag & LT_OUTSIDE) outside_lattice(lt);
1306 }
1307
1308 /* returns key coordinates (+ tilt) when key applied, NULL otherwise */
1309 float *do_ob_key(Scene *scene, Object *ob)
1310 {
1311         Key *key= ob_get_key(ob);
1312         KeyBlock *actkb= ob_get_keyblock(ob);
1313         char *out;
1314         int tot= 0, size= 0;
1315         
1316         if(key==NULL || key->block.first==NULL)
1317                 return NULL;
1318
1319         /* compute size of output array */
1320         if(ob->type == OB_MESH) {
1321                 Mesh *me= ob->data;
1322
1323                 tot= me->totvert;
1324                 size= tot*3*sizeof(float);
1325         }
1326         else if(ob->type == OB_LATTICE) {
1327                 Lattice *lt= ob->data;
1328
1329                 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1330                 size= tot*3*sizeof(float);
1331         }
1332         else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1333                 Curve *cu= ob->data;
1334                 Nurb *nu;
1335
1336                 for(nu=cu->nurb.first; nu; nu=nu->next) {
1337                         if(nu->bezt) {
1338                                 tot += 3*nu->pntsu;
1339                                 size += nu->pntsu*10*sizeof(float);
1340                         }
1341                         else if(nu->bp) {
1342                                 tot += nu->pntsu*nu->pntsv;
1343                                 size += nu->pntsu*nu->pntsv*10*sizeof(float);
1344                         }
1345                 }
1346         }
1347
1348         /* if nothing to interpolate, cancel */
1349         if(tot == 0 || size == 0)
1350                 return NULL;
1351         
1352         /* allocate array */
1353         out= MEM_callocN(size, "do_ob_key out");
1354
1355         /* prevent python from screwing this up? anyhoo, the from pointer could be dropped */
1356         key->from= (ID *)ob->data;
1357                 
1358         if(ob->shapeflag & OB_SHAPE_LOCK) {
1359                 /* shape locked, copy the locked shape instead of blending */
1360                 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1361                 
1362                 if(kb && (kb->flag & KEYBLOCK_MUTE))
1363                         kb= key->refkey;
1364
1365                 if(kb==NULL) {
1366                         kb= key->block.first;
1367                         ob->shapenr= 1;
1368                 }
1369                 
1370                 if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1371                         float *weights= get_weights_array(ob, kb->vgroup);
1372
1373                         cp_key(0, tot, tot, (char*)out, key, actkb, kb, weights, 0);
1374
1375                         if(weights) MEM_freeN(weights);
1376                 }
1377                 else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1378                         cp_cu_key(ob->data, key, actkb, kb, 0, tot, out, tot);
1379         }
1380         else {
1381                 /* do shapekey local drivers */
1382                 float ctime= (float)scene->r.cfra; // XXX this needs to be checked
1383                 
1384                 BKE_animsys_evaluate_animdata(&key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
1385                 
1386                 if(ob->type==OB_MESH) do_mesh_key(scene, ob, key, out, tot);
1387                 else if(ob->type==OB_LATTICE) do_latt_key(scene, ob, key, out, tot);
1388                 else if(ob->type==OB_CURVE) do_curve_key(scene, ob, key, out, tot);
1389                 else if(ob->type==OB_SURF) do_curve_key(scene, ob, key, out, tot);
1390         }
1391         
1392         return (float*)out;
1393 }
1394
1395 Key *ob_get_key(Object *ob)
1396 {
1397         if(ob==NULL) return NULL;
1398         
1399         if(ob->type==OB_MESH) {
1400                 Mesh *me= ob->data;
1401                 return me->key;
1402         }
1403         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1404                 Curve *cu= ob->data;
1405                 return cu->key;
1406         }
1407         else if(ob->type==OB_LATTICE) {
1408                 Lattice *lt= ob->data;
1409                 return lt->key;
1410         }
1411         return NULL;
1412 }
1413
1414 KeyBlock *add_keyblock(Key *key, char *name)
1415 {
1416         KeyBlock *kb;
1417         float curpos= -0.1;
1418         int tot;
1419         
1420         kb= key->block.last;
1421         if(kb) curpos= kb->pos;
1422         
1423         kb= MEM_callocN(sizeof(KeyBlock), "Keyblock");
1424         BLI_addtail(&key->block, kb);
1425         kb->type= KEY_CARDINAL;
1426         
1427         tot= BLI_countlist(&key->block);
1428         if(name) {
1429                 strncpy(kb->name, name, sizeof(kb->name));
1430         } else {
1431                 if(tot==1) strcpy(kb->name, "Basis");
1432                 else sprintf(kb->name, "Key %d", tot-1);
1433         }
1434
1435         BLI_uniquename(&key->block, kb, "Key", '.', offsetof(KeyBlock, name), sizeof(kb->name));
1436
1437         // XXX this is old anim system stuff? (i.e. the 'index' of the shapekey)
1438         kb->adrcode= tot-1;
1439         
1440         key->totkey++;
1441         if(key->totkey==1) key->refkey= kb;
1442         
1443         kb->slidermin= 0.0f;
1444         kb->slidermax= 1.0f;
1445         
1446         // XXX kb->pos is the confusing old horizontal-line RVK crap in old IPO Editor...
1447         if(key->type == KEY_RELATIVE) 
1448                 kb->pos= curpos+0.1;
1449         else {
1450 #if 0 // XXX old animation system
1451                 curpos= bsystem_time(scene, 0, (float)CFRA, 0.0);
1452                 if(calc_ipo_spec(key->ipo, KEY_SPEED, &curpos)==0) {
1453                         curpos /= 100.0;
1454                 }
1455                 kb->pos= curpos;
1456                 
1457                 sort_keys(key);
1458 #endif // XXX old animation system
1459         }
1460         return kb;
1461 }
1462
1463 /* only the active keyblock */
1464 KeyBlock *ob_get_keyblock(Object *ob) 
1465 {
1466         Key *key= ob_get_key(ob);
1467         
1468         if (key) {
1469                 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1470                 return kb;
1471         }
1472
1473         return NULL;
1474 }
1475
1476 KeyBlock *ob_get_reference_keyblock(Object *ob)
1477 {
1478         Key *key= ob_get_key(ob);
1479         
1480         if (key)
1481                 return key->refkey;
1482
1483         return NULL;
1484 }
1485
1486 /* get the appropriate KeyBlock given an index */
1487 KeyBlock *key_get_keyblock(Key *key, int index)
1488 {
1489         KeyBlock *kb;
1490         int i;
1491         
1492         if (key) {
1493                 kb= key->block.first;
1494                 
1495                 for (i= 1; i < key->totkey; i++) {
1496                         kb= kb->next;
1497                         
1498                         if (index==i)
1499                                 return kb;
1500                 }
1501         }
1502         
1503         return NULL;
1504 }
1505
1506 /* get the appropriate KeyBlock given a name to search for */
1507 KeyBlock *key_get_named_keyblock(Key *key, const char name[])
1508 {
1509         KeyBlock *kb;
1510         
1511         if (key && name) {
1512                 for (kb= key->block.first; kb; kb= kb->next) {
1513                         if (strcmp(name, kb->name)==0)
1514                                 return kb;
1515                 }
1516         }
1517         
1518         return NULL;
1519 }
1520
1521 /* Get RNA-Path for 'value' setting of the given ShapeKey 
1522  * NOTE: the user needs to free the returned string once they're finishe with it
1523  */
1524 char *key_get_curValue_rnaPath(Key *key, KeyBlock *kb)
1525 {
1526         PointerRNA ptr;
1527         PropertyRNA *prop;
1528         
1529         /* sanity checks */
1530         if ELEM(NULL, key, kb)
1531                 return NULL;
1532         
1533         /* create the RNA pointer */
1534         RNA_pointer_create(&key->id, &RNA_ShapeKey, kb, &ptr);
1535         /* get pointer to the property too */
1536         prop= RNA_struct_find_property(&ptr, "value");
1537         
1538         /* return the path */
1539         return RNA_path_from_ID_to_property(&ptr, prop);
1540 }
1541
1542
1543 /* conversion functions */
1544
1545 /************************* Lattice ************************/
1546 void latt_to_key(Lattice *lt, KeyBlock *kb)
1547 {
1548         BPoint *bp;
1549         float *fp;
1550         int a, tot;
1551
1552         tot= lt->pntsu*lt->pntsv*lt->pntsw;
1553         if(tot==0) return;
1554
1555         if(kb->data) MEM_freeN(kb->data);
1556
1557         kb->data= MEM_callocN(lt->key->elemsize*tot, "kb->data");
1558         kb->totelem= tot;
1559
1560         bp= lt->def;
1561         fp= kb->data;
1562         for(a=0; a<kb->totelem; a++, fp+=3, bp++) {
1563                 VECCOPY(fp, bp->vec);
1564         }
1565 }
1566
1567 void key_to_latt(KeyBlock *kb, Lattice *lt)
1568 {
1569         BPoint *bp;
1570         float *fp;
1571         int a, tot;
1572
1573         bp= lt->def;
1574         fp= kb->data;
1575
1576         tot= lt->pntsu*lt->pntsv*lt->pntsw;
1577         tot= MIN2(kb->totelem, tot);
1578
1579         for(a=0; a<tot; a++, fp+=3, bp++) {
1580                 VECCOPY(bp->vec, fp);
1581         }
1582 }
1583
1584 /************************* Curve ************************/
1585 void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb)
1586 {
1587         Nurb *nu;
1588         BezTriple *bezt;
1589         BPoint *bp;
1590         float *fp;
1591         int a, tot;
1592
1593         /* count */
1594         tot= count_curveverts(nurb);
1595         if(tot==0) return;
1596
1597         if(kb->data) MEM_freeN(kb->data);
1598
1599         kb->data= MEM_callocN(cu->key->elemsize*tot, "kb->data");
1600         kb->totelem= tot;
1601
1602         nu= nurb->first;
1603         fp= kb->data;
1604         while(nu) {
1605
1606                 if(nu->bezt) {
1607                         bezt= nu->bezt;
1608                         a= nu->pntsu;
1609                         while(a--) {
1610                                 VECCOPY(fp, bezt->vec[0]);
1611                                 fp+= 3;
1612                                 VECCOPY(fp, bezt->vec[1]);
1613                                 fp+= 3;
1614                                 VECCOPY(fp, bezt->vec[2]);
1615                                 fp+= 3;
1616                                 fp[0]= bezt->alfa;
1617                                 fp+= 3; /* alphas */
1618                                 bezt++;
1619                         }
1620                 }
1621                 else {
1622                         bp= nu->bp;
1623                         a= nu->pntsu*nu->pntsv;
1624                         while(a--) {
1625                                 VECCOPY(fp, bp->vec);
1626                                 fp[3]= bp->alfa;
1627
1628                                 fp+= 4;
1629                                 bp++;
1630                         }
1631                 }
1632                 nu= nu->next;
1633         }
1634 }
1635
1636 void key_to_curve(KeyBlock *kb, Curve  *cu, ListBase *nurb)
1637 {
1638         Nurb *nu;
1639         BezTriple *bezt;
1640         BPoint *bp;
1641         float *fp;
1642         int a, tot;
1643
1644         nu= nurb->first;
1645         fp= kb->data;
1646
1647         tot= count_curveverts(nurb);
1648
1649         tot= MIN2(kb->totelem, tot);
1650
1651         while(nu && tot>0) {
1652
1653                 if(nu->bezt) {
1654                         bezt= nu->bezt;
1655                         a= nu->pntsu;
1656                         while(a-- && tot>0) {
1657                                 VECCOPY(bezt->vec[0], fp);
1658                                 fp+= 3;
1659                                 VECCOPY(bezt->vec[1], fp);
1660                                 fp+= 3;
1661                                 VECCOPY(bezt->vec[2], fp);
1662                                 fp+= 3;
1663                                 bezt->alfa= fp[0];
1664                                 fp+= 3; /* alphas */
1665
1666                                 tot-= 3;
1667                                 bezt++;
1668                         }
1669                 }
1670                 else {
1671                         bp= nu->bp;
1672                         a= nu->pntsu*nu->pntsv;
1673                         while(a-- && tot>0) {
1674                                 VECCOPY(bp->vec, fp);
1675                                 bp->alfa= fp[3];
1676
1677                                 fp+= 4;
1678                                 tot--;
1679                                 bp++;
1680                         }
1681                 }
1682                 nu= nu->next;
1683         }
1684 }
1685
1686 /************************* Mesh ************************/
1687 void mesh_to_key(Mesh *me, KeyBlock *kb)
1688 {
1689         MVert *mvert;
1690         float *fp;
1691         int a;
1692
1693         if(me->totvert==0) return;
1694
1695         if(kb->data) MEM_freeN(kb->data);
1696
1697         kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data");
1698         kb->totelem= me->totvert;
1699
1700         mvert= me->mvert;
1701         fp= kb->data;
1702         for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
1703                 VECCOPY(fp, mvert->co);
1704
1705         }
1706 }
1707
1708 void key_to_mesh(KeyBlock *kb, Mesh *me)
1709 {
1710         MVert *mvert;
1711         float *fp;
1712         int a, tot;
1713
1714         mvert= me->mvert;
1715         fp= kb->data;
1716
1717         tot= MIN2(kb->totelem, me->totvert);
1718
1719         for(a=0; a<tot; a++, fp+=3, mvert++) {
1720                 VECCOPY(mvert->co, fp);
1721         }
1722 }
1723
1724 /************************* vert coords ************************/
1725 float (*key_to_vertcos(Object *ob, KeyBlock *kb))[3]
1726 {
1727         float (*vertCos)[3], *co;
1728         float *fp= kb->data;
1729         int tot= 0, a;
1730
1731         /* Count of vertex coords in array */
1732         if(ob->type == OB_MESH) {
1733                 Mesh *me= (Mesh*)ob->data;
1734                 tot= me->totvert;
1735         } else if(ob->type == OB_LATTICE) {
1736                 Lattice *lt= (Lattice*)ob->data;
1737                 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1738         } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1739                 Curve *cu= (Curve*)ob->data;
1740                 tot= count_curveverts(&cu->nurb);
1741         }
1742
1743         if (tot == 0) return NULL;
1744
1745         vertCos= MEM_callocN(tot*sizeof(*vertCos), "key_to_vertcos vertCos");
1746
1747         /* Copy coords to array */
1748         co= (float*)vertCos;
1749
1750         if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1751                 for (a= 0; a<tot; a++, fp+=3, co+=3) {
1752                         copy_v3_v3(co, fp);
1753                 }
1754         } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1755                 Curve *cu= (Curve*)ob->data;
1756                 Nurb *nu= cu->nurb.first;
1757                 BezTriple *bezt;
1758                 BPoint *bp;
1759
1760                 while (nu) {
1761                         if(nu->bezt) {
1762                                 int i;
1763                                 bezt= nu->bezt;
1764                                 a= nu->pntsu;
1765
1766                                 while (a--) {
1767                                         for (i= 0; i<3; i++) {
1768                                                 copy_v3_v3(co, fp);
1769                                                 fp+= 3; co+= 3;
1770                                         }
1771
1772                                         fp+= 3; /* skip alphas */
1773
1774                                         bezt++;
1775                                 }
1776                         }
1777                         else {
1778                                 bp= nu->bp;
1779                                 a= nu->pntsu*nu->pntsv;
1780
1781                                 while (a--) {
1782                                         copy_v3_v3(co, fp);
1783
1784                                         fp+= 4;
1785                                         co+= 3;
1786
1787                                         bp++;
1788                                 }
1789                         }
1790
1791                         nu= nu->next;
1792                 }
1793         }
1794
1795         return vertCos;
1796 }
1797
1798 void vertcos_to_key(Object *ob, KeyBlock *kb, float (*vertCos)[3])
1799 {
1800         float *co= (float*)vertCos, *fp;
1801         int tot= 0, a, elemsize;
1802
1803         if (kb->data) MEM_freeN(kb->data);
1804
1805         /* Count of vertex coords in array */
1806         if(ob->type == OB_MESH) {
1807                 Mesh *me= (Mesh*)ob->data;
1808                 tot= me->totvert;
1809                 elemsize= me->key->elemsize;
1810         } else if(ob->type == OB_LATTICE) {
1811                 Lattice *lt= (Lattice*)ob->data;
1812                 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1813                 elemsize= lt->key->elemsize;
1814         } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1815                 Curve *cu= (Curve*)ob->data;
1816                 elemsize= cu->key->elemsize;
1817                 tot= count_curveverts(&cu->nurb);
1818         }
1819
1820         fp= kb->data= MEM_callocN(tot*elemsize, "key_to_vertcos vertCos");
1821
1822         if (tot == 0) return;
1823
1824         /* Copy coords to keyblock */
1825
1826         if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1827                 for (a= 0; a<tot; a++, fp+=3, co+=3) {
1828                         copy_v3_v3(fp, co);
1829                 }
1830         } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1831                 Curve *cu= (Curve*)ob->data;
1832                 Nurb *nu= cu->nurb.first;
1833                 BezTriple *bezt;
1834                 BPoint *bp;
1835
1836                 while (nu) {
1837                         if(nu->bezt) {
1838                                 int i;
1839                                 bezt= nu->bezt;
1840                                 a= nu->pntsu;
1841
1842                                 while (a--) {
1843                                         for (i= 0; i<3; i++) {
1844                                                 copy_v3_v3(fp, co);
1845                                                 fp+= 3; co+= 3;
1846                                         }
1847
1848                                         fp+= 3; /* skip alphas */
1849
1850                                         bezt++;
1851                                 }
1852                         }
1853                         else {
1854                                 bp= nu->bp;
1855                                 a= nu->pntsu*nu->pntsv;
1856
1857                                 while (a--) {
1858                                         copy_v3_v3(fp, co);
1859
1860                                         fp+= 4;
1861                                         co+= 3;
1862
1863                                         bp++;
1864                                 }
1865                         }
1866
1867                         nu= nu->next;
1868                 }
1869         }
1870 }
1871
1872 void offset_to_key(Object *ob, KeyBlock *kb, float (*ofs)[3])
1873 {
1874         int a;
1875         float *co= (float*)ofs, *fp= kb->data;
1876
1877         if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1878                 for (a= 0; a<kb->totelem; a++, fp+=3, co+=3) {
1879                         add_v3_v3(fp, co);
1880                 }
1881         } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1882                 Curve *cu= (Curve*)ob->data;
1883                 Nurb *nu= cu->nurb.first;
1884                 BezTriple *bezt;
1885                 BPoint *bp;
1886
1887                 while (nu) {
1888                         if(nu->bezt) {
1889                                 int i;
1890                                 bezt= nu->bezt;
1891                                 a= nu->pntsu;
1892
1893                                 while (a--) {
1894                                         for (i= 0; i<3; i++) {
1895                                                 add_v3_v3(fp, co);
1896                                                 fp+= 3; co+= 3;
1897                                         }
1898
1899                                         fp+= 3; /* skip alphas */
1900
1901                                         bezt++;
1902                                 }
1903                         }
1904                         else {
1905                                 bp= nu->bp;
1906                                 a= nu->pntsu*nu->pntsv;
1907
1908                                 while (a--) {
1909                                         add_v3_v3(fp, co);
1910
1911                                         fp+= 4;
1912                                         co+= 3;
1913
1914                                         bp++;
1915                                 }
1916                         }
1917
1918                         nu= nu->next;
1919                 }
1920         }
1921 }