2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/key.c
37 #include "MEM_guardedalloc.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_math_vector.h"
41 #include "BLI_utildefines.h"
43 #include "DNA_anim_types.h"
44 #include "DNA_key_types.h"
45 #include "DNA_lattice_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_scene_types.h"
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"
57 #include "BKE_lattice.h"
58 #include "BKE_library.h"
59 #include "BKE_tessmesh.h"
61 #include "BKE_object.h"
62 #include "BKE_deform.h"
63 #include "BKE_scene.h"
66 #include "RNA_access.h"
68 #define KEY_MODE_DUMMY 0 /* use where mode isn't checked for */
69 #define KEY_MODE_BPOINT 1
70 #define KEY_MODE_BEZTRIPLE 2
72 // old defines from DNA_ipo_types.h for data-type
74 #define IPO_BEZTRIPLE 100
75 #define IPO_BPOINT 101
80 void free_key(Key *key)
84 BKE_free_animdata((ID *)key);
86 while( (kb= key->block.first) ) {
88 if(kb->data) MEM_freeN(kb->data);
90 BLI_remlink(&key->block, kb);
96 void free_key_nolib(Key *key)
100 while( (kb= key->block.first) ) {
102 if(kb->data) MEM_freeN(kb->data);
104 BLI_remlink(&key->block, kb);
110 /* GS reads the memory pointed at in a specific ordering. There are,
111 * however two definitions for it. I have jotted them down here, both,
112 * but I think the first one is actually used. The thing is that
113 * big-endian systems might read this the wrong way round. OTOH, we
114 * constructed the IDs that are read out with this macro explicitly as
115 * well. I expect we'll sort it out soon... */
118 #define GS(a) (*((short *)(a)))
120 /* from misc_util: flip the bytes from x */
121 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
123 Key *add_key(ID *id) /* common function */
128 key= alloc_libblock(&G.main->key, ID_KE, "Key");
130 key->type= KEY_NORMAL;
135 // XXX the code here uses some defines which will soon be depreceated...
136 if( GS(id->name)==ID_ME) {
145 else if( GS(id->name)==ID_LT) {
154 else if( GS(id->name)==ID_CU) {
167 Key *copy_key(Key *key)
172 if(key==NULL) return NULL;
174 keyn= copy_libblock(&key->id);
176 BLI_duplicatelist(&keyn->block, &key->block);
178 kb= key->block.first;
179 kbn= keyn->block.first;
182 if(kbn->data) kbn->data= MEM_dupallocN(kbn->data);
183 if(kb==key->refkey) keyn->refkey= kbn;
193 Key *copy_key_nolib(Key *key)
200 keyn= MEM_dupallocN(key);
204 BLI_duplicatelist(&keyn->block, &key->block);
206 kb= key->block.first;
207 kbn= keyn->block.first;
210 if(kbn->data) kbn->data= MEM_dupallocN(kbn->data);
211 if(kb==key->refkey) keyn->refkey= kbn;
220 void make_local_key(Key *key)
223 /* - only lib users: do nothing
224 * - only local users: set flag
227 if(key==NULL) return;
230 new_id(NULL, &key->id, NULL);
233 /* Sort shape keys and Ipo curves after a change. This assumes that at most
234 * one key was moved, which is a valid assumption for the places it's
235 * currently being called.
238 void sort_keys(Key *key)
242 //IpoCurve *icu = NULL;
245 /* locate the key which is out of position */
246 for (kb= key->block.first; kb; kb= kb->next)
247 if ((kb->next) && (kb->pos > kb->next->pos))
250 /* if we find a key, move it */
252 kb = kb->next; /* next key is the out-of-order one */
253 BLI_remlink(&key->block, kb);
255 /* find the right location and insert before */
256 for (kb2=key->block.first; kb2; kb2= kb2->next) {
257 if (kb2->pos > kb->pos) {
258 BLI_insertlink(&key->block, kb2->prev, kb);
263 /* if more than one Ipo curve, see if this key had a curve */
264 #if 0 // XXX old animation system
265 if(key->ipo && key->ipo->curve.first != key->ipo->curve.last ) {
266 for(icu= key->ipo->curve.first; icu; icu= icu->next) {
267 /* if we find the curve, remove it and reinsert in the
269 if(icu->adrcode==kb->adrcode) {
271 BLI_remlink(&key->ipo->curve, icu);
272 for(icu2= key->ipo->curve.first; icu2; icu2= icu2->next) {
273 if(icu2->adrcode >= kb2->adrcode) {
274 BLI_insertlink(&key->ipo->curve, icu2->prev, icu);
283 /* kb points at the moved key, icu at the moved ipo (if it exists).
284 * go back now and renumber adrcodes */
287 adrcode = kb2->adrcode;
288 for (i = kb->adrcode - adrcode; i >= 0; i--, adrcode++) {
289 /* if the next ipo curve matches the current key, renumber it */
290 if(icu && icu->adrcode == kb->adrcode ) {
291 icu->adrcode = adrcode;
294 /* renumber the shape key */
295 kb->adrcode = adrcode;
298 #endif // XXX old animation system
301 /* new rule; first key is refkey, this to match drawing channels... */
302 key->refkey= key->block.first;
305 /**************** do the key ****************/
307 void key_curve_position_weights(float t, float *data, int type)
311 if(type==KEY_LINEAR) {
317 else if(type==KEY_CARDINAL) {
322 data[0]= -fc*t3 + 2.0f*fc*t2 - fc*t;
323 data[1]= (2.0f-fc)*t3 + (fc-3.0f)*t2 + 1.0f;
324 data[2]= (fc-2.0f)*t3 + (3.0f-2.0f*fc)*t2 + fc*t;
325 data[3]= fc*t3 - fc*t2;
327 else if(type==KEY_BSPLINE) {
331 data[0]= -0.16666666f*t3 + 0.5f*t2 - 0.5f*t + 0.16666666f;
332 data[1]= 0.5f*t3 - t2 + 0.6666666f;
333 data[2]= -0.5f*t3 + 0.5f*t2 + 0.5f*t + 0.16666666f;
334 data[3]= 0.16666666f*t3;
338 /* first derivative */
339 void key_curve_tangent_weights(float t, float *data, int type)
343 if(type==KEY_LINEAR) {
349 else if(type==KEY_CARDINAL) {
353 data[0]= -3.0f*fc*t2 +4.0f*fc*t - fc;
354 data[1]= 3.0f*(2.0f-fc)*t2 +2.0f*(fc-3.0f)*t;
355 data[2]= 3.0f*(fc-2.0f)*t2 +2.0f*(3.0f-2.0f*fc)*t + fc;
356 data[3]= 3.0f*fc*t2 -2.0f*fc*t;
358 else if(type==KEY_BSPLINE) {
361 data[0]= -0.5f*t2 + t - 0.5f;
362 data[1]= 1.5f*t2 - 2.0f*t;
363 data[2]= -1.5f*t2 + t + 0.5f;
368 /* second derivative */
369 void key_curve_normal_weights(float t, float *data, int type)
373 if(type==KEY_LINEAR) {
379 else if(type==KEY_CARDINAL) {
382 data[0]= -6.0f*fc*t + 4.0f*fc;
383 data[1]= 6.0f*(2.0f-fc)*t + 2.0f*(fc-3.0f);
384 data[2]= 6.0f*(fc-2.0f)*t + 2.0f*(3.0f-2.0f*fc);
385 data[3]= 6.0f*fc*t - 2.0f*fc;
387 else if(type==KEY_BSPLINE) {
388 data[0]= -1.0f*t + 1.0f;
389 data[1]= 3.0f*t - 2.0f;
390 data[2]= -3.0f*t + 1.0f;
395 static int setkeys(float fac, ListBase *lb, KeyBlock *k[], float *t, int cycl)
397 /* return 1 means k[2] is the position, return 0 means interpolate */
398 KeyBlock *k1, *firstkey;
399 float d, dpos, ofs=0, lastpos, temp, fval[4];
405 dpos= lastpos - firstkey->pos;
407 if(fac < firstkey->pos) fac= firstkey->pos;
408 else if(fac > k1->pos) fac= k1->pos;
410 k1=k[0]=k[1]=k[2]=k[3]= firstkey;
411 t[0]=t[1]=t[2]=t[3]= k1->pos;
413 /* if(fac<0.0 || fac>1.0) return 1; */
415 if(k1->next==NULL) return 1;
417 if(cycl) { /* pre-sort */
420 if(k[3]==NULL) k[3]=k1;
422 if(k1->next==NULL) k[0]=k1;
425 /* k1= k[1]; */ /* UNUSED */
428 t[2]= k[2]->pos + dpos;
429 t[3]= k[3]->pos + dpos;
436 if(fac<t[1]) fac+= dpos;
439 else { /* pre-sort */
443 if(k[3]==NULL) k[3]= k[2];
448 while( t[2]<fac ) { /* find correct location */
454 else if(t[2]==t[3]) break;
467 if(ofs > 2.1f + lastpos) break;
471 if(k[1]->type==KEY_BSPLINE || k[2]->type==KEY_BSPLINE) bsplinetype= 1;
475 if(bsplinetype==0) { /* B spline doesn't go through the control points */
476 if(fac<=t[1]) { /* fac for 1st key */
481 if(fac>=t[2] ) { /* fac after 2nd key */
485 else if(fac>t[2]) { /* last key */
495 return 1; /* both keys equal */
498 else d= (fac-t[1])/d;
502 key_curve_position_weights(d, t, k[1]->type);
504 if(k[1]->type != k[2]->type) {
505 key_curve_position_weights(d, fval, k[2]->type);
508 t[0]= temp*t[0]+ d*fval[0];
509 t[1]= temp*t[1]+ d*fval[1];
510 t[2]= temp*t[2]+ d*fval[2];
511 t[3]= temp*t[3]+ d*fval[3];
518 static void flerp(int tot, float *in, float *f0, float *f1, float *f2, float *f3, float *t)
522 for(a=0; a<tot; a++) {
523 in[a]= t[0]*f0[a]+t[1]*f1[a]+t[2]*f2[a]+t[3]*f3[a];
527 static void rel_flerp(int tot, float *in, float *ref, float *out, float fac)
531 for(a=0; a<tot; a++) {
532 in[a]-= fac*(ref[a]-out[a]);
536 static char *key_block_get_data(Key *key, KeyBlock *actkb, KeyBlock *kb, char **freedata)
539 /* this hack makes it possible to edit shape keys in
540 edit mode with shape keys blending applied */
541 if(GS(key->from->name) == ID_ME) {
548 me= (Mesh*)key->from;
550 if(me->edit_btmesh && me->edit_btmesh->bm->totvert == kb->totelem) {
552 co= MEM_callocN(sizeof(float)*3*me->edit_btmesh->bm->totvert, "key_block_get_data");
554 BM_ITER(eve, &iter, me->edit_btmesh->bm, BM_VERTS_OF_MESH, NULL) {
555 copy_v3_v3(co[a], eve->co);
559 *freedata= (char*)co;
570 /* currently only the first value of 'ofs' may be set. */
571 static short key_pointer_size(const Key *key, const int mode, int *poinsize, int *ofs)
573 if(key->from==NULL) {
577 switch(GS(key->from->name)) {
579 *ofs= sizeof(float)*3;
583 *ofs= sizeof(float)*3;
587 if(mode == KEY_MODE_BPOINT) {
588 *ofs= sizeof(float)*4;
591 ofs[0]= sizeof(float)*12;
592 *poinsize= (*ofs) / 3;
597 BLI_assert(!"invalid 'key->from' ID type");
604 static void cp_key(const int start, int end, const int tot, char *poin, Key *key, KeyBlock *actkb, KeyBlock *kb, float *weights, const int mode)
606 float ktot = 0.0, kd = 0.0;
607 int elemsize, poinsize = 0, a, *ofsp, ofs[32], flagflo=0;
608 char *k1, *kref, *freek1, *freekref;
609 char *cp, elemstr[8];
611 /* currently always 0, in future key_pointer_size may assign */
614 if(!key_pointer_size(key, mode, &poinsize, &ofs[0]))
617 if(end>tot) end= tot;
619 if(tot != kb->totelem) {
623 kd= kb->totelem/(float)tot;
628 k1= key_block_get_data(key, actkb, kb, &freek1);
629 kref= key_block_get_data(key, actkb, key->refkey, &freekref);
631 /* this exception is needed for slurphing */
634 poin+= poinsize*start;
641 k1+= a*key->elemsize;
644 else k1+= start*key->elemsize;
647 if(mode == KEY_MODE_BEZTRIPLE) {
649 elemstr[1]= IPO_BEZTRIPLE;
653 /* just do it here, not above! */
654 elemsize= key->elemsize;
655 if(mode == KEY_MODE_BEZTRIPLE) elemsize*= 3;
657 for(a=start; a<end; a++) {
659 if(mode == KEY_MODE_BEZTRIPLE) cp= elemstr;
668 memcpy(poin, kref, sizeof(float)*3);
670 rel_flerp(cp[0], (float *)poin, (float *)kref, (float *)k1, *weights);
674 memcpy(poin, k1, sizeof(float)*3);
677 memcpy(poin, k1, sizeof(float)*4);
680 memcpy(poin, k1, sizeof(float)*12);
683 /* should never happen */
684 if(freek1) MEM_freeN(freek1);
685 if(freekref) MEM_freeN(freekref);
686 BLI_assert(!"invalid 'cp[1]'");
694 /* are we going to be nasty? */
697 while(ktot >= 1.0f) {
708 if(mode == KEY_MODE_BEZTRIPLE) a+=2;
711 if(freek1) MEM_freeN(freek1);
712 if(freekref) MEM_freeN(freekref);
715 static void cp_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock *kb, const int start, int end, char *out, const int tot)
720 for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
722 step= nu->pntsu*nu->pntsv;
725 a2= MIN2(a+step, end);
727 if(a1<a2) cp_key(a1, a2, tot, out, key, actkb, kb, NULL, KEY_MODE_BPOINT);
732 /* exception because keys prefer to work with complete blocks */
734 a2= MIN2(a+step, end);
736 if(a1<a2) cp_key(a1, a2, tot, out, key, actkb, kb, NULL, KEY_MODE_BEZTRIPLE);
743 void do_rel_key(const int start, int end, const int tot, char *basispoin, Key *key, KeyBlock *actkb, const int mode)
746 int *ofsp, ofs[3], elemsize, b;
747 char *cp, *poin, *reffrom, *from, elemstr[8];
748 char *freefrom, *freereffrom;
751 /* currently always 0, in future key_pointer_size may assign */
754 if(!key_pointer_size(key, mode, &poinsize, &ofs[0]))
757 if(end>tot) end= tot;
759 /* in case of beztriple */
760 elemstr[0]= 1; /* nr of ipofloats */
761 elemstr[1]= IPO_BEZTRIPLE;
764 /* just here, not above! */
765 elemsize= key->elemsize;
766 if(mode == KEY_MODE_BEZTRIPLE) elemsize*= 3;
769 cp_key(start, end, tot, basispoin, key, actkb, key->refkey, NULL, mode);
773 for(kb=key->block.first; kb; kb=kb->next) {
774 if(kb!=key->refkey) {
775 float icuval= kb->curval;
777 /* only with value, and no difference allowed */
778 if(!(kb->flag & KEYBLOCK_MUTE) && icuval!=0.0f && kb->totelem==tot) {
780 float weight, *weights= kb->weights;
782 /* reference now can be any block */
783 refb= BLI_findlink(&key->block, kb->relative);
784 if(refb==NULL) continue;
787 from= key_block_get_data(key, actkb, kb, &freefrom);
788 reffrom= key_block_get_data(key, actkb, refb, &freereffrom);
790 poin+= start*poinsize;
791 reffrom+= key->elemsize*start; // key elemsize yes!
792 from+= key->elemsize*start;
794 for(b=start; b<end; b++) {
797 weight= *weights * icuval;
802 if(mode == KEY_MODE_BEZTRIPLE) cp= elemstr;
806 while( cp[0] ) { /* cp[0]==amount */
810 rel_flerp(3, (float *)poin, (float *)reffrom, (float *)from, weight);
813 rel_flerp(4, (float *)poin, (float *)reffrom, (float *)from, weight);
816 rel_flerp(12, (float *)poin, (float *)reffrom, (float *)from, weight);
819 /* should never happen */
820 if(freefrom) MEM_freeN(freefrom);
821 if(freereffrom) MEM_freeN(freereffrom);
822 BLI_assert(!"invalid 'cp[1]'");
835 if(mode == KEY_MODE_BEZTRIPLE) b+= 2;
836 if(weights) weights++;
839 if(freefrom) MEM_freeN(freefrom);
840 if(freereffrom) MEM_freeN(freereffrom);
847 static void do_key(const int start, int end, const int tot, char *poin, Key *key, KeyBlock *actkb, KeyBlock **k, float *t, const int mode)
849 float k1tot = 0.0, k2tot = 0.0, k3tot = 0.0, k4tot = 0.0;
850 float k1d = 0.0, k2d = 0.0, k3d = 0.0, k4d = 0.0;
851 int a, ofs[32], *ofsp;
852 int flagdo= 15, flagflo=0, elemsize, poinsize=0;
853 char *k1, *k2, *k3, *k4, *freek1, *freek2, *freek3, *freek4;
854 char *cp, elemstr[8];
856 /* currently always 0, in future key_pointer_size may assign */
859 if(!key_pointer_size(key, mode, &poinsize, &ofs[0]))
862 if(end>tot) end= tot;
864 k1= key_block_get_data(key, actkb, k[0], &freek1);
865 k2= key_block_get_data(key, actkb, k[1], &freek2);
866 k3= key_block_get_data(key, actkb, k[2], &freek3);
867 k4= key_block_get_data(key, actkb, k[3], &freek4);
869 /* test for more or less points (per key!) */
870 if(tot != k[0]->totelem) {
874 k1d= k[0]->totelem/(float)tot;
878 if(tot != k[1]->totelem) {
882 k2d= k[1]->totelem/(float)tot;
886 if(tot != k[2]->totelem) {
890 k3d= k[2]->totelem/(float)tot;
894 if(tot != k[3]->totelem) {
898 k4d= k[3]->totelem/(float)tot;
903 /* this exception needed for slurphing */
906 poin+= poinsize*start;
911 a= (int)floor(k1tot);
914 k1+= a*key->elemsize;
917 else k1+= start*key->elemsize;
922 a= (int)floor(k2tot);
925 k2+= a*key->elemsize;
928 else k2+= start*key->elemsize;
933 a= (int)floor(k3tot);
936 k3+= a*key->elemsize;
939 else k3+= start*key->elemsize;
944 a= (int)floor(k4tot);
947 k4+= a*key->elemsize;
950 else k4+= start*key->elemsize;
955 /* in case of beztriple */
956 elemstr[0]= 1; /* nr of ipofloats */
957 elemstr[1]= IPO_BEZTRIPLE;
960 /* only here, not above! */
961 elemsize= key->elemsize;
962 if(mode == KEY_MODE_BEZTRIPLE) elemsize*= 3;
964 for(a=start; a<end; a++) {
967 if(mode == KEY_MODE_BEZTRIPLE) cp= elemstr;
971 while( cp[0] ) { /* cp[0]==amount */
975 flerp(3, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
978 flerp(4, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
981 flerp(12, (void *)poin, (void *)k1, (void *)k2, (void *)k3, (void *)k4, t);
984 /* should never happen */
985 if(freek1) MEM_freeN(freek1);
986 if(freek2) MEM_freeN(freek2);
987 if(freek3) MEM_freeN(freek3);
988 if(freek4) MEM_freeN(freek4);
989 BLI_assert(!"invalid 'cp[1]'");
997 /* lets do it the difficult way: when keys have a different size */
1001 while(k1tot >= 1.0f) {
1011 while(k2tot >= 1.0f) {
1021 while(k3tot >= 1.0f) {
1031 while(k4tot >= 1.0f) {
1039 if(mode == KEY_MODE_BEZTRIPLE) a+= 2;
1042 if(freek1) MEM_freeN(freek1);
1043 if(freek2) MEM_freeN(freek2);
1044 if(freek3) MEM_freeN(freek3);
1045 if(freek4) MEM_freeN(freek4);
1048 static float *get_weights_array(Object *ob, char *vgroup)
1050 MDeformVert *dvert= NULL;
1051 BMEditMesh *em= NULL;
1054 int totvert= 0, defgrp_index= 0;
1056 /* no vgroup string set? */
1057 if(vgroup[0]==0) return NULL;
1059 /* gather dvert and totvert */
1060 if(ob->type==OB_MESH) {
1063 totvert= me->totvert;
1065 if(me->edit_btmesh && me->edit_btmesh->bm->totvert == totvert)
1066 em= me->edit_btmesh;
1068 else if(ob->type==OB_LATTICE) {
1069 Lattice *lt= ob->data;
1071 totvert= lt->pntsu*lt->pntsv*lt->pntsw;
1074 if(dvert==NULL) return NULL;
1076 /* find the group (weak loop-in-loop) */
1077 defgrp_index= defgroup_name_index(ob, vgroup);
1078 if(defgrp_index >= 0) {
1082 weights= MEM_callocN(totvert*sizeof(float), "weights");
1085 eve = BM_iter_new(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
1086 for (i=0; eve; eve=BM_iter_step(&iter), i++) {
1087 dvert= CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
1090 weights[i]= defvert_find_weight(dvert, defgrp_index);
1095 for(i=0; i < totvert; i++, dvert++) {
1096 weights[i]= defvert_find_weight(dvert, defgrp_index);
1105 static void do_mesh_key(Scene *scene, Object *ob, Key *key, char *out, const int tot)
1107 KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1108 float cfra, ctime, t[4], delta;
1109 int a, flag = 0, step;
1111 if(key->slurph && key->type!=KEY_RELATIVE ) {
1116 if(tot>100 && slurph_opt) {
1119 /* in do_key and cp_key the case a>tot is handled */
1122 cfra= (float)scene->r.cfra;
1124 for(a=0; a<tot; a+=step, cfra+= delta) {
1126 ctime= BKE_curframe(scene);
1127 #if 0 // XXX old animation system
1128 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1130 CLAMP(ctime, 0.0, 1.0);
1132 #endif // XXX old animation system
1133 // XXX for now... since speed curve cannot be directly ported yet
1135 CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1137 flag= setkeys(ctime, &key->block, k, t, 0);
1140 do_key(a, a+step, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
1142 cp_key(a, a+step, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1146 if(key->type==KEY_RELATIVE) {
1149 for(kb= key->block.first; kb; kb= kb->next)
1150 kb->weights= get_weights_array(ob, kb->vgroup);
1152 do_rel_key(0, tot, tot, (char *)out, key, actkb, KEY_MODE_DUMMY);
1154 for(kb= key->block.first; kb; kb= kb->next) {
1155 if(kb->weights) MEM_freeN(kb->weights);
1160 ctime= BKE_curframe(scene);
1162 #if 0 // XXX old animation system
1163 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1165 CLAMP(ctime, 0.0, 1.0);
1167 #endif // XXX old animation system
1168 // XXX for now... since speed curve cannot be directly ported yet
1170 CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1172 flag= setkeys(ctime, &key->block, k, t, 0);
1175 do_key(0, tot, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
1177 cp_key(0, tot, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1182 static void do_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock **k, float *t, char *out, const int tot)
1187 for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
1189 step= nu->pntsu*nu->pntsv;
1190 do_key(a, a+step, tot, out, key, actkb, k, t, KEY_MODE_BPOINT);
1194 do_key(a, a+step, tot, out, key, actkb, k, t, KEY_MODE_BEZTRIPLE);
1201 static void do_rel_cu_key(Curve *cu, Key *key, KeyBlock *actkb, float UNUSED(ctime), char *out, const int tot)
1206 for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
1208 step= nu->pntsu*nu->pntsv;
1209 do_rel_key(a, a+step, tot, out, key, actkb, KEY_MODE_BPOINT);
1213 do_rel_key(a, a+step, tot, out, key, actkb, KEY_MODE_BEZTRIPLE);
1220 static void do_curve_key(Scene *scene, Object *ob, Key *key, char *out, const int tot)
1222 Curve *cu= ob->data;
1223 KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1224 float cfra, ctime, t[4], delta;
1225 int a, flag = 0, step = 0;
1227 if(key->slurph && key->type!=KEY_RELATIVE) {
1229 int mode=0, i= 0, remain= 0, estep=0, count=0;
1231 delta= (float)key->slurph / tot;
1234 if(tot>100 && slurph_opt) {
1237 /* in do_key and cp_key the case a>tot has been handled */
1240 cfra= (float)scene->r.cfra;
1242 for(nu=cu->nurb.first; nu; nu=nu->next) {
1244 mode= KEY_MODE_BPOINT;
1245 estep= nu->pntsu*nu->pntsv;
1248 mode= KEY_MODE_BEZTRIPLE;
1258 ctime= BKE_curframe(scene);
1261 CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1262 flag= setkeys(ctime, &key->block, k, t, 0);
1267 count= MIN2(remain, estep);
1268 if (mode == KEY_MODE_BEZTRIPLE) {
1269 count += 3 - count % 3;
1273 do_key(i, i+count, tot, (char *)out, key, actkb, k, t, mode);
1275 cp_key(i, i+count, tot, (char *)out, key, actkb, k[2], NULL, mode);
1285 ctime= BKE_curframe(scene);
1287 if(key->type==KEY_RELATIVE) {
1288 do_rel_cu_key(cu, cu->key, actkb, ctime, out, tot);
1291 #if 0 // XXX old animation system
1292 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1294 CLAMP(ctime, 0.0, 1.0);
1296 #endif // XXX old animation system
1298 flag= setkeys(ctime, &key->block, k, t, 0);
1300 if(flag==0) do_cu_key(cu, key, actkb, k, t, out, tot);
1301 else cp_cu_key(cu, key, actkb, k[2], 0, tot, out, tot);
1306 static void do_latt_key(Scene *scene, Object *ob, Key *key, char *out, const int tot)
1308 Lattice *lt= ob->data;
1309 KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1310 float delta, cfra, ctime, t[4];
1317 cfra= (float)scene->r.cfra;
1319 for(a=0; a<tot; a++, cfra+= delta) {
1321 ctime= BKE_curframe(scene);
1322 #if 0 // XXX old animation system
1323 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1325 CLAMP(ctime, 0.0, 1.0);
1327 #endif // XXX old animation system
1329 flag= setkeys(ctime, &key->block, k, t, 0);
1332 do_key(a, a+1, tot, out, key, actkb, k, t, KEY_MODE_DUMMY);
1334 cp_key(a, a+1, tot, out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1338 if(key->type==KEY_RELATIVE) {
1341 for(kb= key->block.first; kb; kb= kb->next)
1342 kb->weights= get_weights_array(ob, kb->vgroup);
1344 do_rel_key(0, tot, tot, out, key, actkb, KEY_MODE_DUMMY);
1346 for(kb= key->block.first; kb; kb= kb->next) {
1347 if(kb->weights) MEM_freeN(kb->weights);
1352 ctime= BKE_curframe(scene);
1354 #if 0 // XXX old animation system
1355 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1357 CLAMP(ctime, 0.0, 1.0);
1359 #endif // XXX old animation system
1361 flag= setkeys(ctime, &key->block, k, t, 0);
1364 do_key(0, tot, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
1366 cp_key(0, tot, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1370 if(lt->flag & LT_OUTSIDE) outside_lattice(lt);
1373 /* returns key coordinates (+ tilt) when key applied, NULL otherwise */
1374 float *do_ob_key(Scene *scene, Object *ob)
1376 Key *key= ob_get_key(ob);
1377 KeyBlock *actkb= ob_get_keyblock(ob);
1379 int tot= 0, size= 0;
1381 if(key==NULL || key->block.first==NULL)
1384 /* compute size of output array */
1385 if(ob->type == OB_MESH) {
1389 size= tot*3*sizeof(float);
1391 else if(ob->type == OB_LATTICE) {
1392 Lattice *lt= ob->data;
1394 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1395 size= tot*3*sizeof(float);
1397 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1398 Curve *cu= ob->data;
1401 for(nu=cu->nurb.first; nu; nu=nu->next) {
1404 size += nu->pntsu*12*sizeof(float);
1407 tot += nu->pntsu*nu->pntsv;
1408 size += nu->pntsu*nu->pntsv*12*sizeof(float);
1413 /* if nothing to interpolate, cancel */
1414 if(tot == 0 || size == 0)
1417 /* allocate array */
1418 out= MEM_callocN(size, "do_ob_key out");
1420 /* prevent python from screwing this up? anyhoo, the from pointer could be dropped */
1421 key->from= (ID *)ob->data;
1423 if(ob->shapeflag & OB_SHAPE_LOCK) {
1424 /* shape locked, copy the locked shape instead of blending */
1425 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1427 if(kb && (kb->flag & KEYBLOCK_MUTE))
1431 kb= key->block.first;
1435 if (OB_TYPE_SUPPORT_VGROUP(ob->type)) {
1436 float *weights= get_weights_array(ob, kb->vgroup);
1438 cp_key(0, tot, tot, out, key, actkb, kb, weights, 0);
1440 if(weights) MEM_freeN(weights);
1442 else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1443 cp_cu_key(ob->data, key, actkb, kb, 0, tot, out, tot);
1446 /* do shapekey local drivers */
1447 float ctime= (float)scene->r.cfra; // XXX this needs to be checked
1449 BKE_animsys_evaluate_animdata(scene, &key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
1451 if(ob->type==OB_MESH) do_mesh_key(scene, ob, key, out, tot);
1452 else if(ob->type==OB_LATTICE) do_latt_key(scene, ob, key, out, tot);
1453 else if(ob->type==OB_CURVE) do_curve_key(scene, ob, key, out, tot);
1454 else if(ob->type==OB_SURF) do_curve_key(scene, ob, key, out, tot);
1460 Key *ob_get_key(Object *ob)
1462 if(ob==NULL) return NULL;
1464 if(ob->type==OB_MESH) {
1468 else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1469 Curve *cu= ob->data;
1472 else if(ob->type==OB_LATTICE) {
1473 Lattice *lt= ob->data;
1479 KeyBlock *add_keyblock(Key *key, const char *name)
1485 kb= key->block.last;
1486 if(kb) curpos= kb->pos;
1488 kb= MEM_callocN(sizeof(KeyBlock), "Keyblock");
1489 BLI_addtail(&key->block, kb);
1490 kb->type= KEY_CARDINAL;
1492 tot= BLI_countlist(&key->block);
1494 BLI_strncpy(kb->name, name, sizeof(kb->name));
1496 if(tot==1) BLI_strncpy(kb->name, "Basis", sizeof(kb->name));
1497 else BLI_snprintf(kb->name, sizeof(kb->name), "Key %d", tot-1);
1500 BLI_uniquename(&key->block, kb, "Key", '.', offsetof(KeyBlock, name), sizeof(kb->name));
1502 // XXX this is old anim system stuff? (i.e. the 'index' of the shapekey)
1504 kb->uid = key->uidgen++;
1507 if(key->totkey==1) key->refkey= kb;
1509 kb->slidermin= 0.0f;
1510 kb->slidermax= 1.0f;
1512 // XXX kb->pos is the confusing old horizontal-line RVK crap in old IPO Editor...
1513 if(key->type == KEY_RELATIVE)
1514 kb->pos= curpos + 0.1f;
1516 #if 0 // XXX old animation system
1517 curpos= BKE_curframe(scene);
1518 if(calc_ipo_spec(key->ipo, KEY_SPEED, &curpos)==0) {
1524 #endif // XXX old animation system
1529 /* only the active keyblock */
1530 KeyBlock *ob_get_keyblock(Object *ob)
1532 Key *key= ob_get_key(ob);
1535 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1542 KeyBlock *ob_get_reference_keyblock(Object *ob)
1544 Key *key= ob_get_key(ob);
1552 /* get the appropriate KeyBlock given an index */
1553 KeyBlock *key_get_keyblock(Key *key, int index)
1559 kb= key->block.first;
1561 for (i= 1; i < key->totkey; i++) {
1572 /* get the appropriate KeyBlock given a name to search for */
1573 KeyBlock *key_get_named_keyblock(Key *key, const char name[])
1576 return BLI_findstring(&key->block, name, offsetof(KeyBlock, name));
1581 /* Get RNA-Path for 'value' setting of the given ShapeKey
1582 * NOTE: the user needs to free the returned string once they're finishe with it
1584 char *key_get_curValue_rnaPath(Key *key, KeyBlock *kb)
1590 if ELEM(NULL, key, kb)
1593 /* create the RNA pointer */
1594 RNA_pointer_create(&key->id, &RNA_ShapeKey, kb, &ptr);
1595 /* get pointer to the property too */
1596 prop= RNA_struct_find_property(&ptr, "value");
1598 /* return the path */
1599 return RNA_path_from_ID_to_property(&ptr, prop);
1603 /* conversion functions */
1605 /************************* Lattice ************************/
1606 void latt_to_key(Lattice *lt, KeyBlock *kb)
1612 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1615 if(kb->data) MEM_freeN(kb->data);
1617 kb->data= MEM_callocN(lt->key->elemsize*tot, "kb->data");
1622 for(a=0; a<kb->totelem; a++, fp+=3, bp++) {
1623 copy_v3_v3(fp, bp->vec);
1627 void key_to_latt(KeyBlock *kb, Lattice *lt)
1636 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1637 tot= MIN2(kb->totelem, tot);
1639 for(a=0; a<tot; a++, fp+=3, bp++) {
1640 copy_v3_v3(bp->vec, fp);
1644 /************************* Curve ************************/
1645 void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb)
1654 tot= count_curveverts(nurb);
1657 if(kb->data) MEM_freeN(kb->data);
1659 kb->data= MEM_callocN(cu->key->elemsize*tot, "kb->data");
1670 copy_v3_v3(fp, bezt->vec[0]);
1672 copy_v3_v3(fp, bezt->vec[1]);
1674 copy_v3_v3(fp, bezt->vec[2]);
1677 fp+= 3; /* alphas */
1683 a= nu->pntsu*nu->pntsv;
1685 copy_v3_v3(fp, bp->vec);
1696 void key_to_curve(KeyBlock *kb, Curve *UNUSED(cu), ListBase *nurb)
1707 tot= count_curveverts(nurb);
1709 tot= MIN2(kb->totelem, tot);
1711 while(nu && tot>0) {
1716 while(a-- && tot>0) {
1717 copy_v3_v3(bezt->vec[0], fp);
1719 copy_v3_v3(bezt->vec[1], fp);
1721 copy_v3_v3(bezt->vec[2], fp);
1724 fp+= 3; /* alphas */
1732 a= nu->pntsu*nu->pntsv;
1733 while(a-- && tot>0) {
1734 copy_v3_v3(bp->vec, fp);
1746 /************************* Mesh ************************/
1747 void mesh_to_key(Mesh *me, KeyBlock *kb)
1753 if(me->totvert==0) return;
1755 if(kb->data) MEM_freeN(kb->data);
1757 kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data");
1758 kb->totelem= me->totvert;
1762 for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
1763 copy_v3_v3(fp, mvert->co);
1768 void key_to_mesh(KeyBlock *kb, Mesh *me)
1777 tot= MIN2(kb->totelem, me->totvert);
1779 for(a=0; a<tot; a++, fp+=3, mvert++) {
1780 copy_v3_v3(mvert->co, fp);
1784 /************************* vert coords ************************/
1785 float (*key_to_vertcos(Object *ob, KeyBlock *kb))[3]
1787 float (*vertCos)[3], *co;
1788 float *fp= kb->data;
1791 /* Count of vertex coords in array */
1792 if(ob->type == OB_MESH) {
1793 Mesh *me= (Mesh*)ob->data;
1795 } else if(ob->type == OB_LATTICE) {
1796 Lattice *lt= (Lattice*)ob->data;
1797 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1798 } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1799 Curve *cu= (Curve*)ob->data;
1800 tot= count_curveverts(&cu->nurb);
1803 if (tot == 0) return NULL;
1805 vertCos= MEM_callocN(tot*sizeof(*vertCos), "key_to_vertcos vertCos");
1807 /* Copy coords to array */
1808 co= (float*)vertCos;
1810 if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1811 for (a= 0; a<tot; a++, fp+=3, co+=3) {
1814 } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1815 Curve *cu= (Curve*)ob->data;
1816 Nurb *nu= cu->nurb.first;
1827 for (i= 0; i<3; i++) {
1832 fp+= 3; /* skip alphas */
1839 a= nu->pntsu*nu->pntsv;
1858 void vertcos_to_key(Object *ob, KeyBlock *kb, float (*vertCos)[3])
1860 float *co= (float*)vertCos, *fp;
1861 int tot= 0, a, elemsize;
1863 if (kb->data) MEM_freeN(kb->data);
1865 /* Count of vertex coords in array */
1866 if(ob->type == OB_MESH) {
1867 Mesh *me= (Mesh*)ob->data;
1869 elemsize= me->key->elemsize;
1870 } else if(ob->type == OB_LATTICE) {
1871 Lattice *lt= (Lattice*)ob->data;
1872 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1873 elemsize= lt->key->elemsize;
1874 } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1875 Curve *cu= (Curve*)ob->data;
1876 elemsize= cu->key->elemsize;
1877 tot= count_curveverts(&cu->nurb);
1885 fp= kb->data= MEM_callocN(tot*elemsize, "key_to_vertcos vertCos");
1887 /* Copy coords to keyblock */
1889 if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1890 for (a= 0; a<tot; a++, fp+=3, co+=3) {
1893 } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1894 Curve *cu= (Curve*)ob->data;
1895 Nurb *nu= cu->nurb.first;
1906 for (i= 0; i<3; i++) {
1911 fp+= 3; /* skip alphas */
1918 a= nu->pntsu*nu->pntsv;
1935 void offset_to_key(Object *ob, KeyBlock *kb, float (*ofs)[3])
1938 float *co= (float*)ofs, *fp= kb->data;
1940 if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1941 for (a= 0; a<kb->totelem; a++, fp+=3, co+=3) {
1944 } else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1945 Curve *cu= (Curve*)ob->data;
1946 Nurb *nu= cu->nurb.first;
1957 for (i= 0; i<3; i++) {
1962 fp+= 3; /* skip alphas */
1969 a= nu->pntsu*nu->pntsv;