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