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