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