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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 *****
36 #include "MEM_guardedalloc.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_editVert.h"
41 #include "DNA_anim_types.h"
42 #include "DNA_curve_types.h"
43 #include "DNA_key_types.h"
44 #include "DNA_lattice_types.h"
45 #include "DNA_mesh_types.h"
46 #include "DNA_meshdata_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
50 #include "BKE_animsys.h"
51 #include "BKE_action.h"
52 #include "BKE_blender.h"
53 #include "BKE_curve.h"
54 #include "BKE_global.h"
56 #include "BKE_lattice.h"
57 #include "BKE_library.h"
60 #include "BKE_object.h"
61 #include "BKE_utildefines.h"
63 #include "RNA_access.h"
71 #define KEY_BEZTRIPLE 2
73 // old defines from DNA_ipo_types.h for data-type
75 #define IPO_BEZTRIPLE 100
76 #define IPO_BPOINT 101
81 void free_key(Key *key)
85 BKE_free_animdata((ID *)key);
87 while( (kb= key->block.first) ) {
89 if(kb->data) MEM_freeN(kb->data);
91 BLI_remlink(&key->block, kb);
97 /* GS reads the memory pointed at in a specific ordering. There are,
98 * however two definitions for it. I have jotted them down here, both,
99 * but I think the first one is actually used. The thing is that
100 * big-endian systems might read this the wrong way round. OTOH, we
101 * constructed the IDs that are read out with this macro explicitly as
102 * well. I expect we'll sort it out soon... */
105 #define GS(a) (*((short *)(a)))
107 /* from misc_util: flip the bytes from x */
108 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
110 Key *add_key(ID *id) /* common function */
115 key= alloc_libblock(&G.main->key, ID_KE, "Key");
117 key->type= KEY_NORMAL;
120 // XXX the code here uses some defines which will soon be depreceated...
121 if( GS(id->name)==ID_ME) {
130 else if( GS(id->name)==ID_LT) {
139 else if( GS(id->name)==ID_CU) {
152 Key *copy_key(Key *key)
159 keyn= copy_libblock(key);
161 #if 0 // XXX old animation system
162 keyn->ipo= copy_ipo(key->ipo);
163 #endif // XXX old animation system
165 BLI_duplicatelist(&keyn->block, &key->block);
167 kb= key->block.first;
168 kbn= keyn->block.first;
171 if(kbn->data) kbn->data= MEM_dupallocN(kbn->data);
172 if(kb==key->refkey) keyn->refkey= kbn;
181 void make_local_key(Key *key)
184 /* - only lib users: do nothing
185 * - only local users: set flag
191 new_id(0, (ID *)key, 0);
193 #if 0 // XXX old animation system
194 make_local_ipo(key->ipo);
195 #endif // XXX old animation system
198 /* Sort shape keys and Ipo curves after a change. This assumes that at most
199 * one key was moved, which is a valid assumption for the places it's
200 * currently being called.
203 void sort_keys(Key *key)
207 //IpoCurve *icu = NULL;
210 /* locate the key which is out of position */
211 for (kb= key->block.first; kb; kb= kb->next)
212 if ((kb->next) && (kb->pos > kb->next->pos))
215 /* if we find a key, move it */
217 kb = kb->next; /* next key is the out-of-order one */
218 BLI_remlink(&key->block, kb);
220 /* find the right location and insert before */
221 for (kb2=key->block.first; kb2; kb2= kb2->next) {
222 if (kb2->pos > kb->pos) {
223 BLI_insertlink(&key->block, kb2->prev, kb);
228 /* if more than one Ipo curve, see if this key had a curve */
229 #if 0 // XXX old animation system
230 if(key->ipo && key->ipo->curve.first != key->ipo->curve.last ) {
231 for(icu= key->ipo->curve.first; icu; icu= icu->next) {
232 /* if we find the curve, remove it and reinsert in the
234 if(icu->adrcode==kb->adrcode) {
236 BLI_remlink(&key->ipo->curve, icu);
237 for(icu2= key->ipo->curve.first; icu2; icu2= icu2->next) {
238 if(icu2->adrcode >= kb2->adrcode) {
239 BLI_insertlink(&key->ipo->curve, icu2->prev, icu);
248 /* kb points at the moved key, icu at the moved ipo (if it exists).
249 * go back now and renumber adrcodes */
252 adrcode = kb2->adrcode;
253 for (i = kb->adrcode - adrcode; i >= 0; i--, adrcode++) {
254 /* if the next ipo curve matches the current key, renumber it */
255 if(icu && icu->adrcode == kb->adrcode ) {
256 icu->adrcode = adrcode;
259 /* renumber the shape key */
260 kb->adrcode = adrcode;
263 #endif // XXX old animation system
266 /* new rule; first key is refkey, this to match drawing channels... */
267 key->refkey= key->block.first;
270 /**************** do the key ****************/
272 void key_curve_position_weights(float t, float *data, int type)
276 if(type==KEY_LINEAR) {
282 else if(type==KEY_CARDINAL) {
287 data[0]= -fc*t3 + 2.0f*fc*t2 - fc*t;
288 data[1]= (2.0f-fc)*t3 + (fc-3.0f)*t2 + 1.0f;
289 data[2]= (fc-2.0f)*t3 + (3.0f-2.0f*fc)*t2 + fc*t;
290 data[3]= fc*t3 - fc*t2;
292 else if(type==KEY_BSPLINE) {
296 data[0]= -0.16666666f*t3 + 0.5f*t2 - 0.5f*t + 0.16666666f;
297 data[1]= 0.5f*t3 - t2 + 0.6666666f;
298 data[2]= -0.5f*t3 + 0.5f*t2 + 0.5f*t + 0.16666666f;
299 data[3]= 0.16666666f*t3;
303 /* first derivative */
304 void key_curve_tangent_weights(float t, float *data, int type)
308 if(type==KEY_LINEAR) {
314 else if(type==KEY_CARDINAL) {
318 data[0]= -3.0f*fc*t2 +4.0f*fc*t - fc;
319 data[1]= 3.0f*(2.0f-fc)*t2 +2.0f*(fc-3.0f)*t;
320 data[2]= 3.0f*(fc-2.0f)*t2 +2.0f*(3.0f-2.0f*fc)*t + fc;
321 data[3]= 3.0f*fc*t2 -2.0f*fc*t;
323 else if(type==KEY_BSPLINE) {
326 data[0]= -0.5f*t2 + t - 0.5f;
327 data[1]= 1.5f*t2 - 2.0f*t;
328 data[2]= -1.5f*t2 + t + 0.5f;
333 /* second derivative */
334 void key_curve_normal_weights(float t, float *data, int type)
338 if(type==KEY_LINEAR) {
344 else if(type==KEY_CARDINAL) {
347 data[0]= -6.0f*fc*t + 4.0f*fc;
348 data[1]= 6.0f*(2.0f-fc)*t + 2.0f*(fc-3.0f);
349 data[2]= 6.0f*(fc-2.0f)*t + 2.0f*(3.0f-2.0f*fc);
350 data[3]= 6.0f*fc*t - 2.0f*fc;
352 else if(type==KEY_BSPLINE) {
353 data[0]= -1.0f*t + 1.0f;
354 data[1]= 3.0f*t - 2.0f;
355 data[2]= -3.0f*t + 1.0f;
360 static int setkeys(float fac, ListBase *lb, KeyBlock *k[], float *t, int cycl)
362 /* return 1 means k[2] is the position, return 0 means interpolate */
363 KeyBlock *k1, *firstkey;
364 float d, dpos, ofs=0, lastpos, temp, fval[4];
370 dpos= lastpos - firstkey->pos;
372 if(fac < firstkey->pos) fac= firstkey->pos;
373 else if(fac > k1->pos) fac= k1->pos;
375 k1=k[0]=k[1]=k[2]=k[3]= firstkey;
376 t[0]=t[1]=t[2]=t[3]= k1->pos;
378 /* if(fac<0.0 || fac>1.0) return 1; */
380 if(k1->next==0) return 1;
382 if(cycl) { /* pre-sort */
387 if(k1->next==0) k[0]=k1;
393 t[2]= k[2]->pos + dpos;
394 t[3]= k[3]->pos + dpos;
401 if(fac<t[1]) fac+= dpos;
404 else { /* pre-sort */
408 if(k[3]==0) k[3]= k[2];
413 while( t[2]<fac ) { /* find correct location */
419 else if(t[2]==t[3]) break;
432 if(ofs>2.1+lastpos) break;
436 if(k[1]->type==KEY_BSPLINE || k[2]->type==KEY_BSPLINE) bsplinetype= 1;
440 if(bsplinetype==0) { /* B spline doesn't go through the control points */
441 if(fac<=t[1]) { /* fac for 1st key */
446 if(fac>=t[2] ) { /* fac after 2nd key */
450 else if(fac>t[2]) { /* last key */
460 return 1; /* both keys equal */
463 else d= (fac-t[1])/d;
467 key_curve_position_weights(d, t, k[1]->type);
469 if(k[1]->type != k[2]->type) {
470 key_curve_position_weights(d, fval, k[2]->type);
473 t[0]= temp*t[0]+ d*fval[0];
474 t[1]= temp*t[1]+ d*fval[1];
475 t[2]= temp*t[2]+ d*fval[2];
476 t[3]= temp*t[3]+ d*fval[3];
483 static void flerp(int aantal, float *in, float *f0, float *f1, float *f2, float *f3, float *t)
487 for(a=0; a<aantal; a++) {
488 in[a]= t[0]*f0[a]+t[1]*f1[a]+t[2]*f2[a]+t[3]*f3[a];
492 static void rel_flerp(int aantal, float *in, float *ref, float *out, float fac)
496 for(a=0; a<aantal; a++) {
497 in[a]-= fac*(ref[a]-out[a]);
501 static char *key_block_get_data(Key *key, KeyBlock *actkb, KeyBlock *kb, char **freedata)
504 /* this hack makes it possible to edit shape keys in
505 edit mode with shape keys blending applied */
506 if(GS(key->from->name) == ID_ME) {
512 me= (Mesh*)key->from;
514 if(me->edit_mesh && me->edit_mesh->totvert == kb->totelem) {
516 co= MEM_callocN(sizeof(float)*3*me->edit_mesh->totvert, "key_block_get_data");
518 for(eve=me->edit_mesh->verts.first; eve; eve=eve->next, a++)
519 VECCOPY(co[a], eve->co);
521 *freedata= (char*)co;
531 static void cp_key(int start, int end, int tot, char *poin, Key *key, KeyBlock *actkb, KeyBlock *kb, float *weights, int mode)
533 float ktot = 0.0, kd = 0.0;
534 int elemsize, poinsize = 0, a, *ofsp, ofs[32], flagflo=0;
535 char *k1, *kref, *freek1, *freekref;
536 char *cp, elemstr[8];
538 if(key->from==NULL) return;
540 if( GS(key->from->name)==ID_ME ) {
541 ofs[0]= sizeof(float)*3;
545 else if( GS(key->from->name)==ID_LT ) {
546 ofs[0]= sizeof(float)*3;
550 else if( GS(key->from->name)==ID_CU ) {
551 if(mode==KEY_BPOINT) ofs[0]= sizeof(float)*4;
552 else ofs[0]= sizeof(float)*10;
558 if(end>tot) end= tot;
560 if(tot != kb->totelem) {
564 kd= kb->totelem/(float)tot;
569 k1= key_block_get_data(key, actkb, kb, &freek1);
570 kref= key_block_get_data(key, actkb, key->refkey, &freekref);
572 /* this exception is needed for slurphing */
575 poin+= poinsize*start;
582 k1+= a*key->elemsize;
585 else k1+= start*key->elemsize;
588 if(mode==KEY_BEZTRIPLE) {
590 elemstr[1]= IPO_BEZTRIPLE;
594 /* just do it here, not above! */
595 elemsize= key->elemsize;
596 if(mode==KEY_BEZTRIPLE) elemsize*= 3;
598 for(a=start; a<end; a++) {
600 if(mode==KEY_BEZTRIPLE) cp= elemstr;
609 memcpy(poin, kref, sizeof(float)*3);
611 rel_flerp(cp[0], (float *)poin, (float *)kref, (float *)k1, *weights);
615 memcpy(poin, k1, sizeof(float)*3);
618 memcpy(poin, k1, sizeof(float)*4);
621 memcpy(poin, k1, sizeof(float)*10);
629 /* are we going to be nasty? */
643 if(mode==KEY_BEZTRIPLE) a+=2;
646 if(freek1) MEM_freeN(freek1);
647 if(freekref) MEM_freeN(freekref);
650 static void cp_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock *kb, int start, int end, char *out, int tot)
656 for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
658 step= nu->pntsu*nu->pntsv;
660 /* exception because keys prefer to work with complete blocks */
661 poin= out - a*sizeof(float)*4;
663 a2= MIN2(a+step, end);
665 if(a1<a2) cp_key(a1, a2, tot, poin, key, actkb, kb, NULL, KEY_BPOINT);
670 poin= out - a*sizeof(float)*10;
672 a2= MIN2(a+step, end);
674 if(a1<a2) cp_key(a1, a2, tot, poin, key, actkb, kb, NULL, KEY_BEZTRIPLE);
682 void do_rel_key(int start, int end, int tot, char *basispoin, Key *key, KeyBlock *actkb, int mode)
685 int *ofsp, ofs[3], elemsize, b;
686 char *cp, *poin, *reffrom, *from, elemstr[8];
687 char *freefrom, *freereffrom;
689 if(key->from==NULL) return;
691 if( GS(key->from->name)==ID_ME ) {
692 ofs[0]= sizeof(float)*3;
695 else if( GS(key->from->name)==ID_LT ) {
696 ofs[0]= sizeof(float)*3;
699 else if( GS(key->from->name)==ID_CU ) {
700 if(mode==KEY_BPOINT) ofs[0]= sizeof(float)*4;
701 else ofs[0]= sizeof(float)*10;
706 if(end>tot) end= tot;
708 /* in case of beztriple */
709 elemstr[0]= 1; /* nr of ipofloats */
710 elemstr[1]= IPO_BEZTRIPLE;
713 /* just here, not above! */
714 elemsize= key->elemsize;
715 if(mode==KEY_BEZTRIPLE) elemsize*= 3;
718 cp_key(start, end, tot, basispoin, key, actkb, key->refkey, NULL, mode);
722 for(kb=key->block.first; kb; kb=kb->next) {
723 if(kb!=key->refkey) {
724 float icuval= kb->curval;
726 /* only with value, and no difference allowed */
727 if(!(kb->flag & KEYBLOCK_MUTE) && icuval!=0.0f && kb->totelem==tot) {
729 float weight, *weights= kb->weights;
731 /* reference now can be any block */
732 refb= BLI_findlink(&key->block, kb->relative);
733 if(refb==NULL) continue;
736 from= key_block_get_data(key, actkb, kb, &freefrom);
737 reffrom= key_block_get_data(key, actkb, refb, &freereffrom);
740 reffrom+= key->elemsize*start; // key elemsize yes!
741 from+= key->elemsize*start;
743 for(b=start; b<end; b++) {
746 weight= *weights * icuval;
751 if(mode==KEY_BEZTRIPLE) cp= elemstr;
755 while( cp[0] ) { /* cp[0]==amount */
759 rel_flerp(3, (float *)poin, (float *)reffrom, (float *)from, weight);
762 rel_flerp(4, (float *)poin, (float *)reffrom, (float *)from, weight);
765 rel_flerp(10, (float *)poin, (float *)reffrom, (float *)from, weight);
778 if(mode==KEY_BEZTRIPLE) b+= 2;
779 if(weights) weights++;
782 if(freefrom) MEM_freeN(freefrom);
783 if(freereffrom) MEM_freeN(freereffrom);
790 static void do_key(int start, int end, int tot, char *poin, Key *key, KeyBlock *actkb, KeyBlock **k, float *t, int mode)
792 float k1tot = 0.0, k2tot = 0.0, k3tot = 0.0, k4tot = 0.0;
793 float k1d = 0.0, k2d = 0.0, k3d = 0.0, k4d = 0.0;
794 int a, ofs[32], *ofsp;
795 int flagdo= 15, flagflo=0, elemsize, poinsize=0;
796 char *k1, *k2, *k3, *k4, *freek1, *freek2, *freek3, *freek4;
797 char *cp, elemstr[8];;
799 if(key->from==0) return;
801 if( GS(key->from->name)==ID_ME ) {
802 ofs[0]= sizeof(float)*3;
806 else if( GS(key->from->name)==ID_LT ) {
807 ofs[0]= sizeof(float)*3;
811 else if( GS(key->from->name)==ID_CU ) {
812 if(mode==KEY_BPOINT) ofs[0]= sizeof(float)*4;
813 else ofs[0]= sizeof(float)*10;
819 if(end>tot) end= tot;
821 k1= key_block_get_data(key, actkb, k[0], &freek1);
822 k2= key_block_get_data(key, actkb, k[1], &freek2);
823 k3= key_block_get_data(key, actkb, k[2], &freek3);
824 k4= key_block_get_data(key, actkb, k[3], &freek4);
826 /* test for more or less points (per key!) */
827 if(tot != k[0]->totelem) {
831 k1d= k[0]->totelem/(float)tot;
835 if(tot != k[1]->totelem) {
839 k2d= k[1]->totelem/(float)tot;
843 if(tot != k[2]->totelem) {
847 k3d= k[2]->totelem/(float)tot;
851 if(tot != k[3]->totelem) {
855 k4d= k[3]->totelem/(float)tot;
860 /* this exception needed for slurphing */
863 poin+= poinsize*start;
868 a= (int)floor(k1tot);
871 k1+= a*key->elemsize;
874 else k1+= start*key->elemsize;
879 a= (int)floor(k2tot);
882 k2+= a*key->elemsize;
885 else k2+= start*key->elemsize;
890 a= (int)floor(k3tot);
893 k3+= a*key->elemsize;
896 else k3+= start*key->elemsize;
901 a= (int)floor(k4tot);
904 k4+= a*key->elemsize;
907 else k4+= start*key->elemsize;
912 /* in case of beztriple */
913 elemstr[0]= 1; /* nr of ipofloats */
914 elemstr[1]= IPO_BEZTRIPLE;
917 /* only here, not above! */
918 elemsize= key->elemsize;
919 if(mode==KEY_BEZTRIPLE) elemsize*= 3;
921 for(a=start; a<end; a++) {
924 if(mode==KEY_BEZTRIPLE) cp= elemstr;
928 while( cp[0] ) { /* cp[0]==amount */
932 flerp(3, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
935 flerp(4, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
938 flerp(10, (void *)poin, (void *)k1, (void *)k2, (void *)k3, (void *)k4, t);
946 /* lets do it the difficult way: when keys have a different size */
988 if(mode==KEY_BEZTRIPLE) a+= 2;
991 if(freek1) MEM_freeN(freek1);
992 if(freek2) MEM_freeN(freek2);
993 if(freek3) MEM_freeN(freek3);
994 if(freek4) MEM_freeN(freek4);
997 static float *get_weights_array(Object *ob, char *vgroup)
999 bDeformGroup *curdef;
1000 MDeformVert *dvert= NULL;
1001 int totvert= 0, index= 0;
1003 /* no vgroup string set? */
1004 if(vgroup[0]==0) return NULL;
1006 /* gather dvert and totvert */
1007 if(ob->type==OB_MESH) {
1010 totvert= me->totvert;
1012 else if(ob->type==OB_LATTICE) {
1013 Lattice *lt= ob->data;
1015 totvert= lt->pntsu*lt->pntsv*lt->pntsw;
1018 if(dvert==NULL) return NULL;
1020 /* find the group (weak loop-in-loop) */
1021 for (curdef = ob->defbase.first; curdef; curdef=curdef->next, index++)
1022 if (!strcmp(curdef->name, vgroup))
1029 weights= MEM_callocN(totvert*sizeof(float), "weights");
1031 for (i=0; i < totvert; i++, dvert++) {
1032 for(j=0; j<dvert->totweight; j++) {
1033 if (dvert->dw[j].def_nr == index) {
1034 weights[i]= dvert->dw[j].weight;
1044 static void do_mesh_key(Scene *scene, Object *ob, Key *key, char *out, int tot)
1046 KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1047 float cfra, ctime, t[4], delta;
1048 int a, flag = 0, step;
1050 if(key->slurph && key->type!=KEY_RELATIVE ) {
1055 if(tot>100 && slurph_opt) {
1058 /* in do_key and cp_key the case a>tot is handled */
1061 cfra= (float)scene->r.cfra;
1063 for(a=0; a<tot; a+=step, cfra+= delta) {
1065 ctime= bsystem_time(scene, 0, cfra, 0.0); // xxx ugly cruft!
1066 #if 0 // XXX old animation system
1067 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1069 CLAMP(ctime, 0.0, 1.0);
1071 #endif // XXX old animation system
1072 // XXX for now... since speed curve cannot be directly ported yet
1074 CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1076 flag= setkeys(ctime, &key->block, k, t, 0);
1079 do_key(a, a+step, tot, (char *)out, key, actkb, k, t, 0);
1081 cp_key(a, a+step, tot, (char *)out, key, actkb, k[2], NULL, 0);
1085 if(key->type==KEY_RELATIVE) {
1088 for(kb= key->block.first; kb; kb= kb->next)
1089 kb->weights= get_weights_array(ob, kb->vgroup);
1091 do_rel_key(0, tot, tot, (char *)out, key, actkb, 0);
1093 for(kb= key->block.first; kb; kb= kb->next) {
1094 if(kb->weights) MEM_freeN(kb->weights);
1099 ctime= bsystem_time(scene, ob, (float)scene->r.cfra, 0.0f); // xxx old cruft
1101 #if 0 // XXX old animation system
1102 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1104 CLAMP(ctime, 0.0, 1.0);
1106 #endif // XXX old animation system
1107 // XXX for now... since speed curve cannot be directly ported yet
1109 CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1111 flag= setkeys(ctime, &key->block, k, t, 0);
1114 do_key(0, tot, tot, (char *)out, key, actkb, k, t, 0);
1116 cp_key(0, tot, tot, (char *)out, key, actkb, k[2], NULL, 0);
1121 static void do_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock **k, float *t, char *out, int tot)
1127 for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
1129 step= nu->pntsu*nu->pntsv;
1130 poin= out - a*sizeof(float)*4;
1131 do_key(a, a+step, tot, poin, key, actkb, k, t, KEY_BPOINT);
1135 poin= out - a*sizeof(float)*10;
1136 do_key(a, a+step, tot, poin, key, actkb, k, t, KEY_BEZTRIPLE);
1143 static void do_rel_cu_key(Curve *cu, Key *key, KeyBlock *actkb, float ctime, char *out, int tot)
1149 for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
1151 step= nu->pntsu*nu->pntsv;
1152 poin= out - a*sizeof(float)*3;
1153 do_rel_key(a, a+step, tot, out, key, actkb, KEY_BPOINT);
1157 poin= out - a*sizeof(float)*10;
1158 do_rel_key(a, a+step, tot, poin, key, actkb, KEY_BEZTRIPLE);
1165 static void do_curve_key(Scene *scene, Object *ob, Key *key, char *out, int tot)
1167 Curve *cu= ob->data;
1168 KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1169 float cfra, ctime, t[4], delta;
1170 int a, flag = 0, step = 0;
1177 if(tot>100 && slurph_opt) {
1180 /* in do_key and cp_key the case a>tot has been handled */
1183 cfra= (float)scene->r.cfra;
1185 for(a=0; a<tot; a+=step, cfra+= delta) {
1186 ctime= bsystem_time(scene, 0, cfra, 0.0f); // XXX old cruft
1187 #if 0 // XXX old animation system
1188 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1190 CLAMP(ctime, 0.0, 1.0);
1192 #endif // XXX old animation system
1194 flag= setkeys(ctime, &key->block, k, t, 0);
1197 ; /* do_key(a, a+step, tot, (char *)out, key, k, t, 0); */
1199 ; /* cp_key(a, a+step, tot, (char *)out, key, k[2],0); */
1204 ctime= bsystem_time(scene, NULL, (float)scene->r.cfra, 0.0);
1206 if(key->type==KEY_RELATIVE) {
1207 do_rel_cu_key(cu, cu->key, actkb, ctime, out, tot);
1210 #if 0 // XXX old animation system
1211 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1213 CLAMP(ctime, 0.0, 1.0);
1215 #endif // XXX old animation system
1217 flag= setkeys(ctime, &key->block, k, t, 0);
1219 if(flag==0) do_cu_key(cu, key, actkb, k, t, out, tot);
1220 else cp_cu_key(cu, key, actkb, k[2], 0, tot, out, tot);
1225 static void do_latt_key(Scene *scene, Object *ob, Key *key, char *out, int tot)
1227 Lattice *lt= ob->data;
1228 KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
1229 float delta, cfra, ctime, t[4];
1236 cfra= (float)scene->r.cfra;
1238 for(a=0; a<tot; a++, cfra+= delta) {
1240 ctime= bsystem_time(scene, 0, cfra, 0.0); // XXX old cruft
1241 #if 0 // XXX old animation system
1242 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1244 CLAMP(ctime, 0.0, 1.0);
1246 #endif // XXX old animation system
1248 flag= setkeys(ctime, &key->block, k, t, 0);
1251 do_key(a, a+1, tot, (char *)out, key, actkb, k, t, 0);
1253 cp_key(a, a+1, tot, (char *)out, key, actkb, k[2], NULL, 0);
1257 if(key->type==KEY_RELATIVE) {
1260 for(kb= key->block.first; kb; kb= kb->next)
1261 kb->weights= get_weights_array(ob, kb->vgroup);
1263 do_rel_key(0, tot, tot, (char *)out, key, actkb, 0);
1265 for(kb= key->block.first; kb; kb= kb->next) {
1266 if(kb->weights) MEM_freeN(kb->weights);
1271 ctime= bsystem_time(scene, NULL, (float)scene->r.cfra, 0.0);
1273 #if 0 // XXX old animation system
1274 if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1276 CLAMP(ctime, 0.0, 1.0);
1278 #endif // XXX old animation system
1280 flag= setkeys(ctime, &key->block, k, t, 0);
1283 do_key(0, tot, tot, (char *)out, key, actkb, k, t, 0);
1285 cp_key(0, tot, tot, (char *)out, key, actkb, k[2], NULL, 0);
1289 if(lt->flag & LT_OUTSIDE) outside_lattice(lt);
1292 /* returns key coordinates (+ tilt) when key applied, NULL otherwise */
1293 float *do_ob_key(Scene *scene, Object *ob)
1295 Key *key= ob_get_key(ob);
1296 KeyBlock *actkb= ob_get_keyblock(ob);
1298 int tot= 0, size= 0;
1300 if(key==NULL || key->block.first==NULL)
1303 /* compute size of output array */
1304 if(ob->type == OB_MESH) {
1308 size= tot*3*sizeof(float);
1310 else if(ob->type == OB_LATTICE) {
1311 Lattice *lt= ob->data;
1313 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1314 size= tot*3*sizeof(float);
1316 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1317 Curve *cu= ob->data;
1320 for(nu=cu->nurb.first; nu; nu=nu->next) {
1323 size += nu->pntsu*10*sizeof(float);
1326 tot += nu->pntsu*nu->pntsv;
1327 size += nu->pntsu*nu->pntsv*10*sizeof(float);
1332 /* if nothing to interpolate, cancel */
1333 if(tot == 0 || size == 0)
1336 /* allocate array */
1337 out= MEM_callocN(size, "do_ob_key out");
1339 /* prevent python from screwing this up? anyhoo, the from pointer could be dropped */
1340 key->from= (ID *)ob->data;
1342 if(ob->shapeflag & OB_SHAPE_LOCK) {
1343 /* shape locked, copy the locked shape instead of blending */
1344 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1346 if(kb && (kb->flag & KEYBLOCK_MUTE))
1350 kb= key->block.first;
1354 if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1355 float *weights= get_weights_array(ob, kb->vgroup);
1357 cp_key(0, tot, tot, (char*)out, key, actkb, kb, weights, 0);
1359 if(weights) MEM_freeN(weights);
1361 else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1362 cp_cu_key(ob->data, key, actkb, kb, 0, tot, out, tot);
1365 /* do shapekey local drivers */
1366 float ctime= (float)scene->r.cfra; // XXX this needs to be checked
1368 BKE_animsys_evaluate_animdata(&key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
1370 if(ob->type==OB_MESH) do_mesh_key(scene, ob, key, out, tot);
1371 else if(ob->type==OB_LATTICE) do_latt_key(scene, ob, key, out, tot);
1372 else if(ob->type==OB_CURVE) do_curve_key(scene, ob, key, out, tot);
1373 else if(ob->type==OB_SURF) do_curve_key(scene, ob, key, out, tot);
1379 Key *ob_get_key(Object *ob)
1381 if(ob==NULL) return NULL;
1383 if(ob->type==OB_MESH) {
1387 else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1388 Curve *cu= ob->data;
1391 else if(ob->type==OB_LATTICE) {
1392 Lattice *lt= ob->data;
1398 /* only the active keyblock */
1399 KeyBlock *ob_get_keyblock(Object *ob)
1401 Key *key= ob_get_key(ob);
1404 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1411 /* get the appropriate KeyBlock given an index */
1412 KeyBlock *key_get_keyblock(Key *key, int index)
1418 kb= key->block.first;
1420 for (i= 1; i < key->totkey; i++) {
1431 /* get the appropriate KeyBlock given a name to search for */
1432 KeyBlock *key_get_named_keyblock(Key *key, const char name[])
1437 for (kb= key->block.first; kb; kb= kb->next) {
1438 if (strcmp(name, kb->name)==0)
1446 /* Get RNA-Path for 'value' setting of the given ShapeKey
1447 * NOTE: the user needs to free the returned string once they're finishe with it
1449 char *key_get_curValue_rnaPath(Key *key, KeyBlock *kb)
1455 if ELEM(NULL, key, kb)
1458 /* create the RNA pointer */
1459 RNA_pointer_create(&key->id, &RNA_ShapeKey, kb, &ptr);
1460 /* get pointer to the property too */
1461 prop= RNA_struct_find_property(&ptr, "value");
1463 /* return the path */
1464 return RNA_path_from_ID_to_property(&ptr, prop);