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