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