7 * ***** BEGIN GPL LICENSE BLOCK *****
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL LICENSE BLOCK *****
33 /** \file blender/blenkernel/intern/key.c
42 #include "MEM_guardedalloc.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_editVert.h"
46 #include "BLI_math_vector.h"
47 #include "BLI_utildefines.h"
49 #include "DNA_anim_types.h"
50 #include "DNA_key_types.h"
51 #include "DNA_lattice_types.h"
52 #include "DNA_meshdata_types.h"
53 #include "DNA_object_types.h"
54 #include "DNA_scene_types.h"
56 #include "BKE_animsys.h"
57 #include "BKE_curve.h"
58 #include "BKE_customdata.h"
59 #include "BKE_deform.h"
60 #include "BKE_global.h"
62 #include "BKE_lattice.h"
63 #include "BKE_library.h"
64 #include "BKE_tessmesh.h"
66 #include "BKE_object.h"
67 #include "BKE_deform.h"
70 #include "RNA_access.h"
72 #define KEY_MODE_DUMMY 0 /* use where mode isn't checked for */
73 #define KEY_MODE_BPOINT 1
74 #define KEY_MODE_BEZTRIPLE 2
76 // old defines from DNA_ipo_types.h for data-type
78 #define IPO_BEZTRIPLE 100
79 #define IPO_BPOINT 101
84 void free_key(Key *key)
88 BKE_free_animdata((ID *)key);
90 while( (kb= key->block.first) ) {
92 if(kb->data) MEM_freeN(kb->data);
94 BLI_remlink(&key->block, kb);
100 void free_key_nolib(Key *key)
104 while( (kb= key->block.first) ) {
106 if(kb->data) MEM_freeN(kb->data);
108 BLI_remlink(&key->block, kb);
114 /* GS reads the memory pointed at in a specific ordering. There are,
115 * however two definitions for it. I have jotted them down here, both,
116 * but I think the first one is actually used. The thing is that
117 * big-endian systems might read this the wrong way round. OTOH, we
118 * constructed the IDs that are read out with this macro explicitly as
119 * well. I expect we'll sort it out soon... */
122 #define GS(a) (*((short *)(a)))
124 /* from misc_util: flip the bytes from x */
125 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
127 Key *add_key(ID *id) /* common function */
132 key= alloc_libblock(&G.main->key, ID_KE, "Key");
134 key->type= KEY_NORMAL;
139 // XXX the code here uses some defines which will soon be depreceated...
140 if( GS(id->name)==ID_ME) {
149 else if( GS(id->name)==ID_LT) {
158 else if( GS(id->name)==ID_CU) {
171 Key *copy_key(Key *key)
176 if(key==NULL) return NULL;
178 keyn= copy_libblock(key);
180 BLI_duplicatelist(&keyn->block, &key->block);
182 kb= key->block.first;
183 kbn= keyn->block.first;
186 if(kbn->data) kbn->data= MEM_dupallocN(kbn->data);
187 if(kb==key->refkey) keyn->refkey= kbn;
197 Key *copy_key_nolib(Key *key)
204 keyn= MEM_dupallocN(key);
206 BLI_duplicatelist(&keyn->block, &key->block);
208 kb= key->block.first;
209 kbn= keyn->block.first;
212 if(kbn->data) kbn->data= MEM_dupallocN(kbn->data);
213 if(kb==key->refkey) keyn->refkey= kbn;
222 void make_local_key(Key *key)
225 /* - only lib users: do nothing
226 * - only local users: set flag
229 if(key==NULL) return;
232 new_id(NULL, (ID *)key, NULL);
235 /* Sort shape keys and Ipo curves after a change. This assumes that at most
236 * one key was moved, which is a valid assumption for the places it's
237 * currently being called.
240 void sort_keys(Key *key)
244 //IpoCurve *icu = NULL;
247 /* locate the key which is out of position */
248 for (kb= key->block.first; kb; kb= kb->next)
249 if ((kb->next) && (kb->pos > kb->next->pos))
252 /* if we find a key, move it */
254 kb = kb->next; /* next key is the out-of-order one */
255 BLI_remlink(&key->block, kb);
257 /* find the right location and insert before */
258 for (kb2=key->block.first; kb2; kb2= kb2->next) {
259 if (kb2->pos > kb->pos) {
260 BLI_insertlink(&key->block, kb2->prev, kb);
265 /* if more than one Ipo curve, see if this key had a curve */
266 #if 0 // XXX old animation system
267 if(key->ipo && key->ipo->curve.first != key->ipo->curve.last ) {
268 for(icu= key->ipo->curve.first; icu; icu= icu->next) {
269 /* if we find the curve, remove it and reinsert in the
271 if(icu->adrcode==kb->adrcode) {
273 BLI_remlink(&key->ipo->curve, icu);
274 for(icu2= key->ipo->curve.first; icu2; icu2= icu2->next) {
275 if(icu2->adrcode >= kb2->adrcode) {
276 BLI_insertlink(&key->ipo->curve, icu2->prev, icu);
285 /* kb points at the moved key, icu at the moved ipo (if it exists).
286 * go back now and renumber adrcodes */
289 adrcode = kb2->adrcode;
290 for (i = kb->adrcode - adrcode; i >= 0; i--, adrcode++) {
291 /* if the next ipo curve matches the current key, renumber it */
292 if(icu && icu->adrcode == kb->adrcode ) {
293 icu->adrcode = adrcode;
296 /* renumber the shape key */
297 kb->adrcode = adrcode;
300 #endif // XXX old animation system
303 /* new rule; first key is refkey, this to match drawing channels... */
304 key->refkey= key->block.first;
307 /**************** do the key ****************/
309 void key_curve_position_weights(float t, float *data, int type)
313 if(type==KEY_LINEAR) {
319 else if(type==KEY_CARDINAL) {
324 data[0]= -fc*t3 + 2.0f*fc*t2 - fc*t;
325 data[1]= (2.0f-fc)*t3 + (fc-3.0f)*t2 + 1.0f;
326 data[2]= (fc-2.0f)*t3 + (3.0f-2.0f*fc)*t2 + fc*t;
327 data[3]= fc*t3 - fc*t2;
329 else if(type==KEY_BSPLINE) {
333 data[0]= -0.16666666f*t3 + 0.5f*t2 - 0.5f*t + 0.16666666f;
334 data[1]= 0.5f*t3 - t2 + 0.6666666f;
335 data[2]= -0.5f*t3 + 0.5f*t2 + 0.5f*t + 0.16666666f;
336 data[3]= 0.16666666f*t3;
340 /* first derivative */
341 void key_curve_tangent_weights(float t, float *data, int type)
345 if(type==KEY_LINEAR) {
351 else if(type==KEY_CARDINAL) {
355 data[0]= -3.0f*fc*t2 +4.0f*fc*t - fc;
356 data[1]= 3.0f*(2.0f-fc)*t2 +2.0f*(fc-3.0f)*t;
357 data[2]= 3.0f*(fc-2.0f)*t2 +2.0f*(3.0f-2.0f*fc)*t + fc;
358 data[3]= 3.0f*fc*t2 -2.0f*fc*t;
360 else if(type==KEY_BSPLINE) {
363 data[0]= -0.5f*t2 + t - 0.5f;
364 data[1]= 1.5f*t2 - 2.0f*t;
365 data[2]= -1.5f*t2 + t + 0.5f;
370 /* second derivative */
371 void key_curve_normal_weights(float t, float *data, int type)
375 if(type==KEY_LINEAR) {
381 else if(type==KEY_CARDINAL) {
384 data[0]= -6.0f*fc*t + 4.0f*fc;
385 data[1]= 6.0f*(2.0f-fc)*t + 2.0f*(fc-3.0f);
386 data[2]= 6.0f*(fc-2.0f)*t + 2.0f*(3.0f-2.0f*fc);
387 data[3]= 6.0f*fc*t - 2.0f*fc;
389 else if(type==KEY_BSPLINE) {
390 data[0]= -1.0f*t + 1.0f;
391 data[1]= 3.0f*t - 2.0f;
392 data[2]= -3.0f*t + 1.0f;
397 static int setkeys(float fac, ListBase *lb, KeyBlock *k[], float *t, int cycl)
399 /* return 1 means k[2] is the position, return 0 means interpolate */
400 KeyBlock *k1, *firstkey;
401 float d, dpos, ofs=0, lastpos, temp, fval[4];
407 dpos= lastpos - firstkey->pos;
409 if(fac < firstkey->pos) fac= firstkey->pos;
410 else if(fac > k1->pos) fac= k1->pos;
412 k1=k[0]=k[1]=k[2]=k[3]= firstkey;
413 t[0]=t[1]=t[2]=t[3]= k1->pos;
415 /* if(fac<0.0 || fac>1.0) return 1; */
417 if(k1->next==NULL) return 1;
419 if(cycl) { /* pre-sort */
422 if(k[3]==NULL) k[3]=k1;
424 if(k1->next==NULL) k[0]=k1;
430 t[2]= k[2]->pos + dpos;
431 t[3]= k[3]->pos + dpos;
438 if(fac<t[1]) fac+= dpos;
441 else { /* pre-sort */
445 if(k[3]==NULL) k[3]= k[2];
450 while( t[2]<fac ) { /* find correct location */
456 else if(t[2]==t[3]) break;
469 if(ofs > 2.1f + lastpos) break;
473 if(k[1]->type==KEY_BSPLINE || k[2]->type==KEY_BSPLINE) bsplinetype= 1;
477 if(bsplinetype==0) { /* B spline doesn't go through the control points */
478 if(fac<=t[1]) { /* fac for 1st key */
483 if(fac>=t[2] ) { /* fac after 2nd key */
487 else if(fac>t[2]) { /* last key */
497 return 1; /* both keys equal */
500 else d= (fac-t[1])/d;
504 key_curve_position_weights(d, t, k[1]->type);
506 if(k[1]->type != k[2]->type) {
507 key_curve_position_weights(d, fval, k[2]->type);
510 t[0]= temp*t[0]+ d*fval[0];
511 t[1]= temp*t[1]+ d*fval[1];
512 t[2]= temp*t[2]+ d*fval[2];
513 t[3]= temp*t[3]+ d*fval[3];
520 static void flerp(int aantal, float *in, float *f0, float *f1, float *f2, float *f3, float *t)
524 for(a=0; a<aantal; a++) {
525 in[a]= t[0]*f0[a]+t[1]*f1[a]+t[2]*f2[a]+t[3]*f3[a];
529 static void rel_flerp(int aantal, float *in, float *ref, float *out, float fac)
533 for(a=0; a<aantal; a++) {
534 in[a]-= fac*(ref[a]-out[a]);
538 static char *key_block_get_data(Key *key, KeyBlock *actkb, KeyBlock *kb, char **freedata)
541 /* this hack makes it possible to edit shape keys in
542 edit mode with shape keys blending applied */
543 if(GS(key->from->name) == ID_ME) {
550 me= (Mesh*)key->from;
552 if(me->edit_btmesh && me->edit_btmesh->bm->totvert == kb->totelem) {
554 co= MEM_callocN(sizeof(float)*3*me->edit_btmesh->bm->totvert, "key_block_get_data");
556 BM_ITER(eve, &iter, me->edit_btmesh->bm, BM_VERTS_OF_MESH, NULL) {
557 VECCOPY(co[a], eve->co);
561 *freedata= (char*)co;
572 /* currently only the first value of 'ofs' may be set. */
573 static short key_pointer_size(const Key *key, const int mode, int *poinsize, int *ofs)
575 if(key->from==NULL) {
579 switch(GS(key->from->name)) {
581 *ofs= sizeof(float)*3;
585 *ofs= sizeof(float)*3;
589 if(mode == KEY_MODE_BPOINT) {
590 *ofs= sizeof(float)*4;
593 ofs[0]= sizeof(float)*12;
594 *poinsize= (*ofs) / 3;
599 BLI_assert(!"invalid 'key->from' ID type");
606 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)
608 float ktot = 0.0, kd = 0.0;
609 int elemsize, poinsize = 0, a, *ofsp, ofs[32], flagflo=0;
610 char *k1, *kref, *freek1, *freekref;
611 char *cp, elemstr[8];
613 /* currently always 0, in future key_pointer_size may assign */
616 if(!key_pointer_size(key, mode, &poinsize, &ofs[0]))
619 if(end>tot) end= tot;
621 if(tot != kb->totelem) {
625 kd= kb->totelem/(float)tot;
630 k1= key_block_get_data(key, actkb, kb, &freek1);
631 kref= key_block_get_data(key, actkb, key->refkey, &freekref);
633 /* this exception is needed for slurphing */
636 poin+= poinsize*start;
643 k1+= a*key->elemsize;
646 else k1+= start*key->elemsize;
649 if(mode == KEY_MODE_BEZTRIPLE) {
651 elemstr[1]= IPO_BEZTRIPLE;
655 /* just do it here, not above! */
656 elemsize= key->elemsize;
657 if(mode == KEY_MODE_BEZTRIPLE) elemsize*= 3;
659 for(a=start; a<end; a++) {
661 if(mode == KEY_MODE_BEZTRIPLE) cp= elemstr;
670 memcpy(poin, kref, sizeof(float)*3);
672 rel_flerp(cp[0], (float *)poin, (float *)kref, (float *)k1, *weights);
676 memcpy(poin, k1, sizeof(float)*3);
679 memcpy(poin, k1, sizeof(float)*4);
682 memcpy(poin, k1, sizeof(float)*12);
685 /* should never happen */
686 if(freek1) MEM_freeN(freek1);
687 if(freekref) MEM_freeN(freekref);
688 BLI_assert(!"invalid 'cp[1]'");
696 /* are we going to be nasty? */
699 while(ktot >= 1.0f) {
710 if(mode == KEY_MODE_BEZTRIPLE) a+=2;
713 if(freek1) MEM_freeN(freek1);
714 if(freekref) MEM_freeN(freekref);
717 static void cp_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock *kb, const int start, int end, char *out, const int tot)
722 for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
724 step= nu->pntsu*nu->pntsv;
727 a2= MIN2(a+step, end);
729 if(a1<a2) cp_key(a1, a2, tot, out, key, actkb, kb, NULL, KEY_MODE_BPOINT);
734 /* exception because keys prefer to work with complete blocks */
736 a2= MIN2(a+step, end);
738 if(a1<a2) cp_key(a1, a2, tot, out, key, actkb, kb, NULL, KEY_MODE_BEZTRIPLE);
745 void do_rel_key(const int start, int end, const int tot, char *basispoin, Key *key, KeyBlock *actkb, const int mode)
748 int *ofsp, ofs[3], elemsize, b;
749 char *cp, *poin, *reffrom, *from, elemstr[8];
750 char *freefrom, *freereffrom;
753 /* currently always 0, in future key_pointer_size may assign */
756 if(!key_pointer_size(key, mode, &poinsize, &ofs[0]))
759 if(end>tot) end= tot;
761 /* in case of beztriple */
762 elemstr[0]= 1; /* nr of ipofloats */
763 elemstr[1]= IPO_BEZTRIPLE;
766 /* just here, not above! */
767 elemsize= key->elemsize;
768 if(mode == KEY_MODE_BEZTRIPLE) elemsize*= 3;
771 cp_key(start, end, tot, basispoin, key, actkb, key->refkey, NULL, mode);
775 for(kb=key->block.first; kb; kb=kb->next) {
776 if(kb!=key->refkey) {
777 float icuval= kb->curval;
779 /* only with value, and no difference allowed */
780 if(!(kb->flag & KEYBLOCK_MUTE) && icuval!=0.0f && kb->totelem==tot) {
782 float weight, *weights= kb->weights;
784 /* reference now can be any block */
785 refb= BLI_findlink(&key->block, kb->relative);
786 if(refb==NULL) continue;
789 from= key_block_get_data(key, actkb, kb, &freefrom);
790 reffrom= key_block_get_data(key, actkb, refb, &freereffrom);
792 poin+= start*poinsize;
793 reffrom+= key->elemsize*start; // key elemsize yes!
794 from+= key->elemsize*start;
796 for(b=start; b<end; b++) {
799 weight= *weights * icuval;
804 if(mode == KEY_MODE_BEZTRIPLE) cp= elemstr;
808 while( cp[0] ) { /* cp[0]==amount */
812 rel_flerp(3, (float *)poin, (float *)reffrom, (float *)from, weight);
815 rel_flerp(4, (float *)poin, (float *)reffrom, (float *)from, weight);
818 rel_flerp(12, (float *)poin, (float *)reffrom, (float *)from, weight);
821 /* should never happen */
822 if(freefrom) MEM_freeN(freefrom);
823 if(freereffrom) MEM_freeN(freereffrom);
824 BLI_assert(!"invalid 'cp[1]'");
837 if(mode == KEY_MODE_BEZTRIPLE) b+= 2;
838 if(weights) weights++;
841 if(freefrom) MEM_freeN(freefrom);
842 if(freereffrom) MEM_freeN(freereffrom);
849 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)
851 float k1tot = 0.0, k2tot = 0.0, k3tot = 0.0, k4tot = 0.0;
852 float k1d = 0.0, k2d = 0.0, k3d = 0.0, k4d = 0.0;
853 int a, ofs[32], *ofsp;
854 int flagdo= 15, flagflo=0, elemsize, poinsize=0;
855 char *k1, *k2, *k3, *k4, *freek1, *freek2, *freek3, *freek4;
856 char *cp, elemstr[8];;
858 /* currently always 0, in future key_pointer_size may assign */
861 if(!key_pointer_size(key, mode, &poinsize, &ofs[0]))
864 if(end>tot) end= tot;
866 k1= key_block_get_data(key, actkb, k[0], &freek1);
867 k2= key_block_get_data(key, actkb, k[1], &freek2);
868 k3= key_block_get_data(key, actkb, k[2], &freek3);
869 k4= key_block_get_data(key, actkb, k[3], &freek4);
871 /* test for more or less points (per key!) */
872 if(tot != k[0]->totelem) {
876 k1d= k[0]->totelem/(float)tot;
880 if(tot != k[1]->totelem) {
884 k2d= k[1]->totelem/(float)tot;
888 if(tot != k[2]->totelem) {
892 k3d= k[2]->totelem/(float)tot;
896 if(tot != k[3]->totelem) {
900 k4d= k[3]->totelem/(float)tot;
905 /* this exception needed for slurphing */
908 poin+= poinsize*start;
913 a= (int)floor(k1tot);
916 k1+= a*key->elemsize;
919 else k1+= start*key->elemsize;
924 a= (int)floor(k2tot);
927 k2+= a*key->elemsize;
930 else k2+= start*key->elemsize;
935 a= (int)floor(k3tot);
938 k3+= a*key->elemsize;
941 else k3+= start*key->elemsize;
946 a= (int)floor(k4tot);
949 k4+= a*key->elemsize;
952 else k4+= start*key->elemsize;
957 /* in case of beztriple */
958 elemstr[0]= 1; /* nr of ipofloats */
959 elemstr[1]= IPO_BEZTRIPLE;
962 /* only here, not above! */
963 elemsize= key->elemsize;
964 if(mode == KEY_MODE_BEZTRIPLE) elemsize*= 3;
966 for(a=start; a<end; a++) {
969 if(mode == KEY_MODE_BEZTRIPLE) cp= elemstr;
973 while( cp[0] ) { /* cp[0]==amount */
977 flerp(3, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
980 flerp(4, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
983 flerp(12, (void *)poin, (void *)k1, (void *)k2, (void *)k3, (void *)k4, t);
986 /* should never happen */
987 if(freek1) MEM_freeN(freek1);
988 if(freek2) MEM_freeN(freek2);
989 if(freek3) MEM_freeN(freek3);
990 if(freek4) MEM_freeN(freek4);
991 BLI_assert(!"invalid 'cp[1]'");
999 /* lets do it the difficult way: when keys have a different size */
1003 while(k1tot >= 1.0f) {
1013 while(k2tot >= 1.0f) {
1023 while(k3tot >= 1.0f) {
1033 while(k4tot >= 1.0f) {
1041 if(mode == KEY_MODE_BEZTRIPLE) a+= 2;
1044 if(freek1) MEM_freeN(freek1);
1045 if(freek2) MEM_freeN(freek2);
1046 if(freek3) MEM_freeN(freek3);
1047 if(freek4) MEM_freeN(freek4);
1050 static float *get_weights_array(Object *ob, char *vgroup)
1052 MDeformVert *dvert= NULL;
1053 BMEditMesh *em= NULL;
1056 int totvert= 0, defgrp_index= 0;
1058 /* no vgroup string set? */
1059 if(vgroup[0]==0) return NULL;
1061 /* gather dvert and totvert */
1062 if(ob->type==OB_MESH) {
1065 totvert= me->totvert;
1067 if(me->edit_btmesh && me->edit_btmesh->bm->totvert == totvert)
1068 em= me->edit_btmesh;
1070 else if(ob->type==OB_LATTICE) {
1071 Lattice *lt= ob->data;
1073 totvert= lt->pntsu*lt->pntsv*lt->pntsw;
1076 if(dvert==NULL) return NULL;
1078 /* find the group (weak loop-in-loop) */
1079 defgrp_index= defgroup_name_index(ob, vgroup);
1080 if(defgrp_index >= 0) {
1084 weights= MEM_callocN(totvert*sizeof(float), "weights");
1087 eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
1088 for (i=0; eve; eve=BMIter_Step(&iter), i++) {
1089 dvert= CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
1092 weights[i]= defvert_find_weight(dvert, defgrp_index);
1097 for(i=0; i < totvert; i++, dvert++) {
1098 weights[i]= defvert_find_weight(dvert, defgrp_index);
1107 static void do_mesh_key(Scene *scene, Object *ob, Key *key, char *out, const int tot)
1109 KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1110 float cfra, ctime, t[4], delta;
1111 int a, flag = 0, step;
1113 if(key->slurph && key->type!=KEY_RELATIVE ) {
1118 if(tot>100 && slurph_opt) {
1121 /* in do_key and cp_key the case a>tot is handled */
1124 cfra= (float)scene->r.cfra;
1126 for(a=0; a<tot; a+=step, cfra+= delta) {
1128 ctime= bsystem_time(scene, NULL, cfra, 0.0); // xxx ugly cruft!
1129 #if 0 // XXX old animation system
1130 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1132 CLAMP(ctime, 0.0, 1.0);
1134 #endif // XXX old animation system
1135 // XXX for now... since speed curve cannot be directly ported yet
1137 CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1139 flag= setkeys(ctime, &key->block, k, t, 0);
1142 do_key(a, a+step, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
1144 cp_key(a, a+step, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1148 if(key->type==KEY_RELATIVE) {
1151 for(kb= key->block.first; kb; kb= kb->next)
1152 kb->weights= get_weights_array(ob, kb->vgroup);
1154 do_rel_key(0, tot, tot, (char *)out, key, actkb, KEY_MODE_DUMMY);
1156 for(kb= key->block.first; kb; kb= kb->next) {
1157 if(kb->weights) MEM_freeN(kb->weights);
1162 ctime= bsystem_time(scene, ob, (float)scene->r.cfra, 0.0f); // xxx old cruft
1164 #if 0 // XXX old animation system
1165 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1167 CLAMP(ctime, 0.0, 1.0);
1169 #endif // XXX old animation system
1170 // XXX for now... since speed curve cannot be directly ported yet
1172 CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1174 flag= setkeys(ctime, &key->block, k, t, 0);
1177 do_key(0, tot, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
1179 cp_key(0, tot, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1184 static void do_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock **k, float *t, char *out, const int tot)
1189 for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
1191 step= nu->pntsu*nu->pntsv;
1192 do_key(a, a+step, tot, out, key, actkb, k, t, KEY_MODE_BPOINT);
1196 do_key(a, a+step, tot, out, key, actkb, k, t, KEY_MODE_BEZTRIPLE);
1203 static void do_rel_cu_key(Curve *cu, Key *key, KeyBlock *actkb, float UNUSED(ctime), char *out, const int tot)
1208 for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
1210 step= nu->pntsu*nu->pntsv;
1211 do_rel_key(a, a+step, tot, out, key, actkb, KEY_MODE_BPOINT);
1215 do_rel_key(a, a+step, tot, out, key, actkb, KEY_MODE_BEZTRIPLE);
1222 static void do_curve_key(Scene *scene, Object *ob, Key *key, char *out, const int tot)
1224 Curve *cu= ob->data;
1225 KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1226 float cfra, ctime, t[4], delta;
1227 int a, flag = 0, step = 0;
1229 if(key->slurph && key->type!=KEY_RELATIVE) {
1231 int mode=0, i= 0, remain= 0, estep=0, count=0;
1233 delta= (float)key->slurph / tot;
1236 if(tot>100 && slurph_opt) {
1239 /* in do_key and cp_key the case a>tot has been handled */
1242 cfra= (float)scene->r.cfra;
1244 for(nu=cu->nurb.first; nu; nu=nu->next) {
1246 mode= KEY_MODE_BPOINT;
1247 estep= nu->pntsu*nu->pntsv;
1250 mode= KEY_MODE_BEZTRIPLE;
1260 ctime= bsystem_time(scene, NULL, cfra, 0.0f); // XXX old cruft
1263 CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1264 flag= setkeys(ctime, &key->block, k, t, 0);
1269 count= MIN2(remain, estep);
1270 if (mode == KEY_MODE_BEZTRIPLE) {
1271 count += 3 - count % 3;
1275 do_key(i, i+count, tot, (char *)out, key, actkb, k, t, mode);
1277 cp_key(i, i+count, tot, (char *)out, key, actkb, k[2], NULL, mode);
1287 ctime= bsystem_time(scene, NULL, (float)scene->r.cfra, 0.0);
1289 if(key->type==KEY_RELATIVE) {
1290 do_rel_cu_key(cu, cu->key, actkb, ctime, out, tot);
1293 #if 0 // XXX old animation system
1294 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1296 CLAMP(ctime, 0.0, 1.0);
1298 #endif // XXX old animation system
1300 flag= setkeys(ctime, &key->block, k, t, 0);
1302 if(flag==0) do_cu_key(cu, key, actkb, k, t, out, tot);
1303 else cp_cu_key(cu, key, actkb, k[2], 0, tot, out, tot);
1308 static void do_latt_key(Scene *scene, Object *ob, Key *key, char *out, const int tot)
1310 Lattice *lt= ob->data;
1311 KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1312 float delta, cfra, ctime, t[4];
1319 cfra= (float)scene->r.cfra;
1321 for(a=0; a<tot; a++, cfra+= delta) {
1323 ctime= bsystem_time(scene, NULL, cfra, 0.0); // XXX old cruft
1324 #if 0 // XXX old animation system
1325 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1327 CLAMP(ctime, 0.0, 1.0);
1329 #endif // XXX old animation system
1331 flag= setkeys(ctime, &key->block, k, t, 0);
1334 do_key(a, a+1, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
1336 cp_key(a, a+1, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1340 if(key->type==KEY_RELATIVE) {
1343 for(kb= key->block.first; kb; kb= kb->next)
1344 kb->weights= get_weights_array(ob, kb->vgroup);
1346 do_rel_key(0, tot, tot, (char *)out, key, actkb, KEY_MODE_DUMMY);
1348 for(kb= key->block.first; kb; kb= kb->next) {
1349 if(kb->weights) MEM_freeN(kb->weights);
1354 ctime= bsystem_time(scene, NULL, (float)scene->r.cfra, 0.0);
1356 #if 0 // XXX old animation system
1357 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1359 CLAMP(ctime, 0.0, 1.0);
1361 #endif // XXX old animation system
1363 flag= setkeys(ctime, &key->block, k, t, 0);
1366 do_key(0, tot, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
1368 cp_key(0, tot, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1372 if(lt->flag & LT_OUTSIDE) outside_lattice(lt);
1375 /* returns key coordinates (+ tilt) when key applied, NULL otherwise */
1376 float *do_ob_key(Scene *scene, Object *ob)
1378 Key *key= ob_get_key(ob);
1379 KeyBlock *actkb= ob_get_keyblock(ob);
1381 int tot= 0, size= 0;
1383 if(key==NULL || key->block.first==NULL)
1386 /* compute size of output array */
1387 if(ob->type == OB_MESH) {
1391 size= tot*3*sizeof(float);
1393 else if(ob->type == OB_LATTICE) {
1394 Lattice *lt= ob->data;
1396 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1397 size= tot*3*sizeof(float);
1399 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1400 Curve *cu= ob->data;
1403 for(nu=cu->nurb.first; nu; nu=nu->next) {
1406 size += nu->pntsu*12*sizeof(float);
1409 tot += nu->pntsu*nu->pntsv;
1410 size += nu->pntsu*nu->pntsv*12*sizeof(float);
1415 /* if nothing to interpolate, cancel */
1416 if(tot == 0 || size == 0)
1419 /* allocate array */
1420 out= MEM_callocN(size, "do_ob_key out");
1422 /* prevent python from screwing this up? anyhoo, the from pointer could be dropped */
1423 key->from= (ID *)ob->data;
1425 if(ob->shapeflag & OB_SHAPE_LOCK) {
1426 /* shape locked, copy the locked shape instead of blending */
1427 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1429 if(kb && (kb->flag & KEYBLOCK_MUTE))
1433 kb= key->block.first;
1437 if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1438 float *weights= get_weights_array(ob, kb->vgroup);
1440 cp_key(0, tot, tot, (char*)out, key, actkb, kb, weights, 0);
1442 if(weights) MEM_freeN(weights);
1444 else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1445 cp_cu_key(ob->data, key, actkb, kb, 0, tot, out, tot);
1448 /* do shapekey local drivers */
1449 float ctime= (float)scene->r.cfra; // XXX this needs to be checked
1451 BKE_animsys_evaluate_animdata(&key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
1453 if(ob->type==OB_MESH) do_mesh_key(scene, ob, key, out, tot);
1454 else if(ob->type==OB_LATTICE) do_latt_key(scene, ob, key, out, tot);
1455 else if(ob->type==OB_CURVE) do_curve_key(scene, ob, key, out, tot);
1456 else if(ob->type==OB_SURF) do_curve_key(scene, ob, key, out, tot);
1462 Key *ob_get_key(Object *ob)
1464 if(ob==NULL) return NULL;
1466 if(ob->type==OB_MESH) {
1470 else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1471 Curve *cu= ob->data;
1474 else if(ob->type==OB_LATTICE) {
1475 Lattice *lt= ob->data;
1481 KeyBlock *add_keyblock(Key *key, const char *name)
1487 kb= key->block.last;
1488 if(kb) curpos= kb->pos;
1490 kb= MEM_callocN(sizeof(KeyBlock), "Keyblock");
1491 BLI_addtail(&key->block, kb);
1492 kb->type= KEY_CARDINAL;
1494 tot= BLI_countlist(&key->block);
1496 strncpy(kb->name, name, sizeof(kb->name));
1498 if(tot==1) strcpy(kb->name, "Basis");
1499 else sprintf(kb->name, "Key %d", tot-1);
1502 BLI_uniquename(&key->block, kb, "Key", '.', offsetof(KeyBlock, name), sizeof(kb->name));
1504 // XXX this is old anim system stuff? (i.e. the 'index' of the shapekey)
1506 kb->uid = key->uidgen++;
1509 if(key->totkey==1) key->refkey= kb;
1511 kb->slidermin= 0.0f;
1512 kb->slidermax= 1.0f;
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;
1518 #if 0 // XXX old animation system
1519 curpos= bsystem_time(scene, 0, (float)CFRA, 0.0);
1520 if(calc_ipo_spec(key->ipo, KEY_SPEED, &curpos)==0) {
1526 #endif // XXX old animation system
1531 /* only the active keyblock */
1532 KeyBlock *ob_get_keyblock(Object *ob)
1534 Key *key= ob_get_key(ob);
1537 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1544 KeyBlock *ob_get_reference_keyblock(Object *ob)
1546 Key *key= ob_get_key(ob);
1554 /* get the appropriate KeyBlock given an index */
1555 KeyBlock *key_get_keyblock(Key *key, int index)
1561 kb= key->block.first;
1563 for (i= 1; i < key->totkey; i++) {
1574 /* get the appropriate KeyBlock given a name to search for */
1575 KeyBlock *key_get_named_keyblock(Key *key, const char name[])
1578 return BLI_findstring(&key->block, name, offsetof(KeyBlock, name));
1583 /* Get RNA-Path for 'value' setting of the given ShapeKey
1584 * NOTE: the user needs to free the returned string once they're finishe with it
1586 char *key_get_curValue_rnaPath(Key *key, KeyBlock *kb)
1592 if ELEM(NULL, key, kb)
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");
1600 /* return the path */
1601 return RNA_path_from_ID_to_property(&ptr, prop);
1605 /* conversion functions */
1607 /************************* Lattice ************************/
1608 void latt_to_key(Lattice *lt, KeyBlock *kb)
1614 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1617 if(kb->data) MEM_freeN(kb->data);
1619 kb->data= MEM_callocN(lt->key->elemsize*tot, "kb->data");
1624 for(a=0; a<kb->totelem; a++, fp+=3, bp++) {
1625 VECCOPY(fp, bp->vec);
1629 void key_to_latt(KeyBlock *kb, Lattice *lt)
1638 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1639 tot= MIN2(kb->totelem, tot);
1641 for(a=0; a<tot; a++, fp+=3, bp++) {
1642 VECCOPY(bp->vec, fp);
1646 /************************* Curve ************************/
1647 void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb)
1656 tot= count_curveverts(nurb);
1659 if(kb->data) MEM_freeN(kb->data);
1661 kb->data= MEM_callocN(cu->key->elemsize*tot, "kb->data");
1672 VECCOPY(fp, bezt->vec[0]);
1674 VECCOPY(fp, bezt->vec[1]);
1676 VECCOPY(fp, bezt->vec[2]);
1679 fp+= 3; /* alphas */
1685 a= nu->pntsu*nu->pntsv;
1687 VECCOPY(fp, bp->vec);
1698 void key_to_curve(KeyBlock *kb, Curve *UNUSED(cu), ListBase *nurb)
1709 tot= count_curveverts(nurb);
1711 tot= MIN2(kb->totelem, tot);
1713 while(nu && tot>0) {
1718 while(a-- && tot>0) {
1719 VECCOPY(bezt->vec[0], fp);
1721 VECCOPY(bezt->vec[1], fp);
1723 VECCOPY(bezt->vec[2], fp);
1726 fp+= 3; /* alphas */
1734 a= nu->pntsu*nu->pntsv;
1735 while(a-- && tot>0) {
1736 VECCOPY(bp->vec, fp);
1748 /************************* Mesh ************************/
1749 void mesh_to_key(Mesh *me, KeyBlock *kb)
1755 if(me->totvert==0) return;
1757 if(kb->data) MEM_freeN(kb->data);
1759 kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data");
1760 kb->totelem= me->totvert;
1764 for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
1765 VECCOPY(fp, mvert->co);
1770 void key_to_mesh(KeyBlock *kb, Mesh *me)
1779 tot= MIN2(kb->totelem, me->totvert);
1781 for(a=0; a<tot; a++, fp+=3, mvert++) {
1782 VECCOPY(mvert->co, fp);
1786 /************************* vert coords ************************/
1787 float (*key_to_vertcos(Object *ob, KeyBlock *kb))[3]
1789 float (*vertCos)[3], *co;
1790 float *fp= kb->data;
1793 /* Count of vertex coords in array */
1794 if(ob->type == OB_MESH) {
1795 Mesh *me= (Mesh*)ob->data;
1797 } else if(ob->type == OB_LATTICE) {
1798 Lattice *lt= (Lattice*)ob->data;
1799 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1800 } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1801 Curve *cu= (Curve*)ob->data;
1802 tot= count_curveverts(&cu->nurb);
1805 if (tot == 0) return NULL;
1807 vertCos= MEM_callocN(tot*sizeof(*vertCos), "key_to_vertcos vertCos");
1809 /* Copy coords to array */
1810 co= (float*)vertCos;
1812 if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1813 for (a= 0; a<tot; a++, fp+=3, co+=3) {
1816 } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1817 Curve *cu= (Curve*)ob->data;
1818 Nurb *nu= cu->nurb.first;
1829 for (i= 0; i<3; i++) {
1834 fp+= 3; /* skip alphas */
1841 a= nu->pntsu*nu->pntsv;
1860 void vertcos_to_key(Object *ob, KeyBlock *kb, float (*vertCos)[3])
1862 float *co= (float*)vertCos, *fp;
1863 int tot= 0, a, elemsize;
1865 if (kb->data) MEM_freeN(kb->data);
1867 /* Count of vertex coords in array */
1868 if(ob->type == OB_MESH) {
1869 Mesh *me= (Mesh*)ob->data;
1871 elemsize= me->key->elemsize;
1872 } else if(ob->type == OB_LATTICE) {
1873 Lattice *lt= (Lattice*)ob->data;
1874 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1875 elemsize= lt->key->elemsize;
1876 } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1877 Curve *cu= (Curve*)ob->data;
1878 elemsize= cu->key->elemsize;
1879 tot= count_curveverts(&cu->nurb);
1887 fp= kb->data= MEM_callocN(tot*elemsize, "key_to_vertcos vertCos");
1889 /* Copy coords to keyblock */
1891 if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1892 for (a= 0; a<tot; a++, fp+=3, co+=3) {
1895 } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1896 Curve *cu= (Curve*)ob->data;
1897 Nurb *nu= cu->nurb.first;
1908 for (i= 0; i<3; i++) {
1913 fp+= 3; /* skip alphas */
1920 a= nu->pntsu*nu->pntsv;
1937 void offset_to_key(Object *ob, KeyBlock *kb, float (*ofs)[3])
1940 float *co= (float*)ofs, *fp= kb->data;
1942 if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1943 for (a= 0; a<kb->totelem; a++, fp+=3, co+=3) {
1946 } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1947 Curve *cu= (Curve*)ob->data;
1948 Nurb *nu= cu->nurb.first;
1959 for (i= 0; i<3; i++) {
1964 fp+= 3; /* skip alphas */
1971 a= nu->pntsu*nu->pntsv;