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