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