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