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