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