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, stored in DNA - don't modify! */
74 #define IPO_BEZTRIPLE 100
75 #define IPO_BPOINT 101
77 /* extern, not threadsafe */
81 void BKE_key_free(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 void free_key_nolib(Key *key)
101 while ( (kb = key->block.first) ) {
103 if (kb->data) MEM_freeN(kb->data);
105 BLI_remlink(&key->block, kb);
111 /* GS reads the memory pointed at in a specific ordering. There are,
112 * however two definitions for it. I have jotted them down here, both,
113 * but I think the first one is actually used. The thing is that
114 * big-endian systems might read this the wrong way round. OTOH, we
115 * constructed the IDs that are read out with this macro explicitly as
116 * well. I expect we'll sort it out soon... */
119 #define GS(a) (*((short *)(a)))
121 /* from misc_util: flip the bytes from x */
122 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
124 Key *add_key(ID *id) /* common function */
129 key = BKE_libblock_alloc(&G.main->key, ID_KE, "Key");
131 key->type = KEY_NORMAL;
136 /* XXX the code here uses some defines which will soon be depreceated... */
137 switch (GS(id->name)) {
173 Key *BKE_key_copy(Key *key)
178 if (key == NULL) return NULL;
180 keyn = BKE_libblock_copy(&key->id);
182 BLI_duplicatelist(&keyn->block, &key->block);
184 kb = key->block.first;
185 kbn = keyn->block.first;
188 if (kbn->data) kbn->data = MEM_dupallocN(kbn->data);
189 if (kb == key->refkey) keyn->refkey = kbn;
199 Key *copy_key_nolib(Key *key)
204 if (key == 0) return 0;
206 keyn = MEM_dupallocN(key);
210 BLI_duplicatelist(&keyn->block, &key->block);
212 kb = key->block.first;
213 kbn = keyn->block.first;
216 if (kbn->data) kbn->data = MEM_dupallocN(kbn->data);
217 if (kb == key->refkey) keyn->refkey = kbn;
226 void BKE_key_make_local(Key *key)
229 /* - only lib users: do nothing
230 * - only local users: set flag
233 if (key == NULL) return;
236 new_id(NULL, &key->id, NULL);
239 /* Sort shape keys and Ipo curves after a change. This assumes that at most
240 * one key was moved, which is a valid assumption for the places it's
241 * currently being called.
244 void sort_keys(Key *key)
249 /* locate the key which is out of position */
250 for (kb = key->block.first; kb; kb = kb->next)
251 if ((kb->next) && (kb->pos > kb->next->pos))
254 /* if we find a key, move it */
256 kb = kb->next; /* next key is the out-of-order one */
257 BLI_remlink(&key->block, kb);
259 /* find the right location and insert before */
260 for (kb2 = key->block.first; kb2; kb2 = kb2->next) {
261 if (kb2->pos > kb->pos) {
262 BLI_insertlink(&key->block, kb2->prev, kb);
268 /* new rule; first key is refkey, this to match drawing channels... */
269 key->refkey = key->block.first;
272 /**************** do the key ****************/
274 void key_curve_position_weights(float t, float *data, int type)
278 if (type == KEY_LINEAR) {
284 else if (type == KEY_CARDINAL) {
289 data[0] = -fc * t3 + 2.0f * fc * t2 - fc * t;
290 data[1] = (2.0f - fc) * t3 + (fc - 3.0f) * t2 + 1.0f;
291 data[2] = (fc - 2.0f) * t3 + (3.0f - 2.0f * fc) * t2 + fc * t;
292 data[3] = fc * t3 - fc * t2;
294 else if (type == KEY_BSPLINE) {
298 data[0] = -0.16666666f * t3 + 0.5f * t2 - 0.5f * t + 0.16666666f;
299 data[1] = 0.5f * t3 - t2 + 0.66666666f;
300 data[2] = -0.5f * t3 + 0.5f * t2 + 0.5f * t + 0.16666666f;
301 data[3] = 0.16666666f * t3;
305 /* first derivative */
306 void key_curve_tangent_weights(float t, float *data, int type)
310 if (type == KEY_LINEAR) {
316 else if (type == KEY_CARDINAL) {
320 data[0] = -3.0f * fc * t2 + 4.0f * fc * t - fc;
321 data[1] = 3.0f * (2.0f - fc) * t2 + 2.0f * (fc - 3.0f) * t;
322 data[2] = 3.0f * (fc - 2.0f) * t2 + 2.0f * (3.0f - 2.0f * fc) * t + fc;
323 data[3] = 3.0f * fc * t2 - 2.0f * fc * t;
325 else if (type == KEY_BSPLINE) {
328 data[0] = -0.5f * t2 + t - 0.5f;
329 data[1] = 1.5f * t2 - t * 2.0f;
330 data[2] = -1.5f * t2 + t + 0.5f;
335 /* second derivative */
336 void key_curve_normal_weights(float t, float *data, int type)
340 if (type == KEY_LINEAR) {
346 else if (type == KEY_CARDINAL) {
349 data[0] = -6.0f * fc * t + 4.0f * fc;
350 data[1] = 6.0f * (2.0f - fc) * t + 2.0f * (fc - 3.0f);
351 data[2] = 6.0f * (fc - 2.0f) * t + 2.0f * (3.0f - 2.0f * fc);
352 data[3] = 6.0f * fc * t - 2.0f * fc;
354 else if (type == KEY_BSPLINE) {
355 data[0] = -1.0f * t + 1.0f;
356 data[1] = 3.0f * t - 2.0f;
357 data[2] = -3.0f * t + 1.0f;
362 static int setkeys(float fac, ListBase *lb, KeyBlock *k[], float *t, int cycl)
364 /* return 1 means k[2] is the position, return 0 means interpolate */
365 KeyBlock *k1, *firstkey;
366 float d, dpos, ofs = 0, lastpos, temp, fval[4];
369 firstkey = lb->first;
372 dpos = lastpos - firstkey->pos;
374 if (fac < firstkey->pos) fac = firstkey->pos;
375 else if (fac > k1->pos) fac = k1->pos;
377 k1 = k[0] = k[1] = k[2] = k[3] = firstkey;
378 t[0] = t[1] = t[2] = t[3] = k1->pos;
380 /* if (fac<0.0 || fac>1.0) return 1; */
382 if (k1->next == NULL) return 1;
384 if (cycl) { /* pre-sort */
387 if (k[3] == NULL) k[3] = k1;
389 if (k1->next == NULL) k[0] = k1;
392 /* k1= k[1]; */ /* UNUSED */
395 t[2] = k[2]->pos + dpos;
396 t[3] = k[3]->pos + dpos;
403 if (fac < t[1]) fac += dpos;
406 else { /* pre-sort */
410 if (k[3] == NULL) k[3] = k[2];
415 while (t[2] < fac) { /* find correct location */
416 if (k1->next == NULL) {
421 else if (t[2] == t[3]) break;
431 t[3] = k1->pos + ofs;
434 if (ofs > 2.1f + lastpos) break;
438 if (k[1]->type == KEY_BSPLINE || k[2]->type == KEY_BSPLINE) bsplinetype = 1;
442 if (bsplinetype == 0) { /* B spline doesn't go through the control points */
443 if (fac <= t[1]) { /* fac for 1st key */
448 if (fac >= t[2]) { /* fac after 2nd key */
452 else if (fac > t[2]) { /* last key */
461 if (bsplinetype == 0) {
462 return 1; /* both keys equal */
466 d = (fac - t[1]) / d;
471 key_curve_position_weights(d, t, k[1]->type);
473 if (k[1]->type != k[2]->type) {
474 key_curve_position_weights(d, fval, k[2]->type);
477 t[0] = temp * t[0] + d * fval[0];
478 t[1] = temp * t[1] + d * fval[1];
479 t[2] = temp * t[2] + d * fval[2];
480 t[3] = temp * t[3] + d * fval[3];
487 static void flerp(int tot, float *in, float *f0, float *f1, float *f2, float *f3, float *t)
491 for (a = 0; a < tot; a++) {
492 in[a] = t[0] * f0[a] + t[1] * f1[a] + t[2] * f2[a] + t[3] * f3[a];
496 static void rel_flerp(int tot, float *in, float *ref, float *out, float fac)
500 for (a = 0; a < tot; a++) {
501 in[a] -= fac * (ref[a] - out[a]);
505 static char *key_block_get_data(Key *key, KeyBlock *actkb, KeyBlock *kb, char **freedata)
508 /* this hack makes it possible to edit shape keys in
509 * edit mode with shape keys blending applied */
510 if (GS(key->from->name) == ID_ME) {
517 me = (Mesh *)key->from;
519 if (me->edit_btmesh && me->edit_btmesh->bm->totvert == kb->totelem) {
521 co = MEM_callocN(sizeof(float) * 3 * me->edit_btmesh->bm->totvert, "key_block_get_data");
523 BM_ITER_MESH (eve, &iter, me->edit_btmesh->bm, BM_VERTS_OF_MESH) {
524 copy_v3_v3(co[a], eve->co);
528 *freedata = (char *)co;
539 /* currently only the first value of 'ofs' may be set. */
540 static short key_pointer_size(const Key *key, const int mode, int *poinsize, int *ofs)
542 if (key->from == NULL) {
546 switch (GS(key->from->name)) {
548 *ofs = sizeof(float) * 3;
552 *ofs = sizeof(float) * 3;
556 if (mode == KEY_MODE_BPOINT) {
557 *ofs = sizeof(float) * 4;
561 ofs[0] = sizeof(float) * 12;
562 *poinsize = (*ofs) / 3;
567 BLI_assert(!"invalid 'key->from' ID type");
574 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)
576 float ktot = 0.0, kd = 0.0;
577 int elemsize, poinsize = 0, a, *ofsp, ofs[32], flagflo = 0;
578 char *k1, *kref, *freek1, *freekref;
579 char *cp, elemstr[8];
581 /* currently always 0, in future key_pointer_size may assign */
584 if (!key_pointer_size(key, mode, &poinsize, &ofs[0]))
587 if (end > tot) end = tot;
589 if (tot != kb->totelem) {
593 kd = kb->totelem / (float)tot;
600 k1 = key_block_get_data(key, actkb, kb, &freek1);
601 kref = key_block_get_data(key, actkb, key->refkey, &freekref);
603 /* this exception is needed for slurphing */
606 poin += poinsize * start;
610 a = (int)floor(ktot);
613 k1 += a * key->elemsize;
616 else k1 += start * key->elemsize;
619 if (mode == KEY_MODE_BEZTRIPLE) {
621 elemstr[1] = IPO_BEZTRIPLE;
625 /* just do it here, not above! */
626 elemsize = key->elemsize;
627 if (mode == KEY_MODE_BEZTRIPLE) elemsize *= 3;
629 for (a = start; a < end; a++) {
631 if (mode == KEY_MODE_BEZTRIPLE) cp = elemstr;
640 memcpy(poin, kref, sizeof(float) * 3);
641 if (*weights != 0.0f)
642 rel_flerp(cp[0], (float *)poin, (float *)kref, (float *)k1, *weights);
646 memcpy(poin, k1, sizeof(float) * 3);
650 memcpy(poin, k1, sizeof(float) * 4);
653 memcpy(poin, k1, sizeof(float) * 12);
656 /* should never happen */
657 if (freek1) MEM_freeN(freek1);
658 if (freekref) MEM_freeN(freekref);
659 BLI_assert(!"invalid 'cp[1]'");
667 /* are we going to be nasty? */
670 while (ktot >= 1.0f) {
681 if (mode == KEY_MODE_BEZTRIPLE) {
686 if (freek1) MEM_freeN(freek1);
687 if (freekref) MEM_freeN(freekref);
690 static void cp_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock *kb, const int start, int end, char *out, const int tot)
695 for (a = 0, nu = cu->nurb.first; nu; nu = nu->next, a += step) {
697 step = nu->pntsu * nu->pntsv;
700 a2 = MIN2(a + step, end);
702 if (a1 < a2) cp_key(a1, a2, tot, out, key, actkb, kb, NULL, KEY_MODE_BPOINT);
705 step = 3 * nu->pntsu;
707 /* exception because keys prefer to work with complete blocks */
709 a2 = MIN2(a + step, end);
711 if (a1 < a2) cp_key(a1, a2, tot, out, key, actkb, kb, NULL, KEY_MODE_BEZTRIPLE);
719 void do_rel_key(const int start, int end, const int tot, char *basispoin, Key *key, KeyBlock *actkb, const int mode)
722 int *ofsp, ofs[3], elemsize, b;
723 char *cp, *poin, *reffrom, *from, elemstr[8];
724 char *freefrom, *freereffrom;
727 /* currently always 0, in future key_pointer_size may assign */
730 if (!key_pointer_size(key, mode, &poinsize, &ofs[0]))
733 if (end > tot) end = tot;
735 /* in case of beztriple */
736 elemstr[0] = 1; /* nr of ipofloats */
737 elemstr[1] = IPO_BEZTRIPLE;
740 /* just here, not above! */
741 elemsize = key->elemsize;
742 if (mode == KEY_MODE_BEZTRIPLE) elemsize *= 3;
745 cp_key(start, end, tot, basispoin, key, actkb, key->refkey, NULL, mode);
749 for (kb = key->block.first; kb; kb = kb->next) {
750 if (kb != key->refkey) {
751 float icuval = kb->curval;
753 /* only with value, and no difference allowed */
754 if (!(kb->flag & KEYBLOCK_MUTE) && icuval != 0.0f && kb->totelem == tot) {
756 float weight, *weights = kb->weights;
758 /* reference now can be any block */
759 refb = BLI_findlink(&key->block, kb->relative);
760 if (refb == NULL) continue;
763 from = key_block_get_data(key, actkb, kb, &freefrom);
764 reffrom = key_block_get_data(key, actkb, refb, &freereffrom);
766 poin += start * poinsize;
767 reffrom += key->elemsize * start; // key elemsize yes!
768 from += key->elemsize * start;
770 for (b = start; b < end; b++) {
772 weight = weights ? (*weights * icuval) : icuval;
775 if (mode == KEY_MODE_BEZTRIPLE) cp = elemstr;
779 while (cp[0]) { /* cp[0]==amount */
783 rel_flerp(3, (float *)poin, (float *)reffrom, (float *)from, weight);
786 rel_flerp(4, (float *)poin, (float *)reffrom, (float *)from, weight);
789 rel_flerp(12, (float *)poin, (float *)reffrom, (float *)from, weight);
792 /* should never happen */
793 if (freefrom) MEM_freeN(freefrom);
794 if (freereffrom) MEM_freeN(freereffrom);
795 BLI_assert(!"invalid 'cp[1]'");
808 if (mode == KEY_MODE_BEZTRIPLE) b += 2;
809 if (weights) weights++;
812 if (freefrom) MEM_freeN(freefrom);
813 if (freereffrom) MEM_freeN(freereffrom);
820 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)
822 float k1tot = 0.0, k2tot = 0.0, k3tot = 0.0, k4tot = 0.0;
823 float k1d = 0.0, k2d = 0.0, k3d = 0.0, k4d = 0.0;
824 int a, ofs[32], *ofsp;
825 int flagdo = 15, flagflo = 0, elemsize, poinsize = 0;
826 char *k1, *k2, *k3, *k4, *freek1, *freek2, *freek3, *freek4;
827 char *cp, elemstr[8];
829 /* currently always 0, in future key_pointer_size may assign */
832 if (!key_pointer_size(key, mode, &poinsize, &ofs[0]))
835 if (end > tot) end = tot;
837 k1 = key_block_get_data(key, actkb, k[0], &freek1);
838 k2 = key_block_get_data(key, actkb, k[1], &freek2);
839 k3 = key_block_get_data(key, actkb, k[2], &freek3);
840 k4 = key_block_get_data(key, actkb, k[3], &freek4);
842 /* test for more or less points (per key!) */
843 if (tot != k[0]->totelem) {
847 k1d = k[0]->totelem / (float)tot;
851 if (tot != k[1]->totelem) {
855 k2d = k[1]->totelem / (float)tot;
859 if (tot != k[2]->totelem) {
863 k3d = k[2]->totelem / (float)tot;
867 if (tot != k[3]->totelem) {
871 k4d = k[3]->totelem / (float)tot;
876 /* this exception needed for slurphing */
879 poin += poinsize * start;
883 k1tot += start * k1d;
884 a = (int)floor(k1tot);
887 k1 += a * key->elemsize;
890 else k1 += start * key->elemsize;
894 k2tot += start * k2d;
895 a = (int)floor(k2tot);
898 k2 += a * key->elemsize;
901 else k2 += start * key->elemsize;
905 k3tot += start * k3d;
906 a = (int)floor(k3tot);
909 k3 += a * key->elemsize;
912 else k3 += start * key->elemsize;
916 k4tot += start * k4d;
917 a = (int)floor(k4tot);
920 k4 += a * key->elemsize;
923 else k4 += start * key->elemsize;
928 /* in case of beztriple */
929 elemstr[0] = 1; /* nr of ipofloats */
930 elemstr[1] = IPO_BEZTRIPLE;
933 /* only here, not above! */
934 elemsize = key->elemsize;
935 if (mode == KEY_MODE_BEZTRIPLE) elemsize *= 3;
937 for (a = start; a < end; a++) {
940 if (mode == KEY_MODE_BEZTRIPLE) cp = elemstr;
944 while (cp[0]) { /* cp[0]==amount */
948 flerp(3, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
951 flerp(4, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
954 flerp(12, (void *)poin, (void *)k1, (void *)k2, (void *)k3, (void *)k4, t);
957 /* should never happen */
958 if (freek1) MEM_freeN(freek1);
959 if (freek2) MEM_freeN(freek2);
960 if (freek3) MEM_freeN(freek3);
961 if (freek4) MEM_freeN(freek4);
962 BLI_assert(!"invalid 'cp[1]'");
970 /* lets do it the difficult way: when keys have a different size */
974 while (k1tot >= 1.0f) {
984 while (k2tot >= 1.0f) {
994 while (k3tot >= 1.0f) {
1004 while (k4tot >= 1.0f) {
1009 else k4 += elemsize;
1012 if (mode == KEY_MODE_BEZTRIPLE) a += 2;
1015 if (freek1) MEM_freeN(freek1);
1016 if (freek2) MEM_freeN(freek2);
1017 if (freek3) MEM_freeN(freek3);
1018 if (freek4) MEM_freeN(freek4);
1021 static float *get_weights_array(Object *ob, char *vgroup)
1023 MDeformVert *dvert = NULL;
1024 BMEditMesh *em = NULL;
1027 int totvert = 0, defgrp_index = 0;
1029 /* no vgroup string set? */
1030 if (vgroup[0] == 0) return NULL;
1032 /* gather dvert and totvert */
1033 if (ob->type == OB_MESH) {
1034 Mesh *me = ob->data;
1036 totvert = me->totvert;
1038 if (me->edit_btmesh && me->edit_btmesh->bm->totvert == totvert)
1039 em = me->edit_btmesh;
1041 else if (ob->type == OB_LATTICE) {
1042 Lattice *lt = ob->data;
1044 totvert = lt->pntsu * lt->pntsv * lt->pntsw;
1047 if (dvert == NULL) return NULL;
1049 /* find the group (weak loop-in-loop) */
1050 defgrp_index = defgroup_name_index(ob, vgroup);
1051 if (defgrp_index >= 0) {
1055 weights = MEM_callocN(totvert * sizeof(float), "weights");
1058 BM_ITER_MESH_INDEX (eve, &iter, em->bm, BM_VERTS_OF_MESH, i) {
1059 dvert = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
1062 weights[i] = defvert_find_weight(dvert, defgrp_index);
1067 for (i = 0; i < totvert; i++, dvert++) {
1068 weights[i] = defvert_find_weight(dvert, defgrp_index);
1077 static void do_mesh_key(Scene *scene, Object *ob, Key *key, char *out, const int tot)
1079 KeyBlock *k[4], *actkb = ob_get_keyblock(ob);
1083 if (key->slurph && key->type != KEY_RELATIVE) {
1084 const float ctime_scaled = key->ctime / 100.0f;
1085 float delta = (float)key->slurph / tot;
1086 float cfra = (float)scene->r.cfra;
1089 if (tot > 100 && slurph_opt) {
1092 /* in do_key and cp_key the case a>tot is handled */
1098 for (a = 0; a < tot; a += step, cfra += delta) {
1099 flag = setkeys(ctime_scaled, &key->block, k, t, 0);
1102 do_key(a, a + step, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
1104 cp_key(a, a + step, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1108 if (key->type == KEY_RELATIVE) {
1110 for (kb = key->block.first; kb; kb = kb->next) {
1111 kb->weights = get_weights_array(ob, kb->vgroup);
1114 do_rel_key(0, tot, tot, (char *)out, key, actkb, KEY_MODE_DUMMY);
1116 for (kb = key->block.first; kb; kb = kb->next) {
1117 if (kb->weights) MEM_freeN(kb->weights);
1122 const float ctime_scaled = key->ctime / 100.0f;
1124 flag = setkeys(ctime_scaled, &key->block, k, t, 0);
1127 do_key(0, tot, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
1129 cp_key(0, tot, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1134 static void do_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock **k, float *t, char *out, const int tot)
1139 for (a = 0, nu = cu->nurb.first; nu; nu = nu->next, a += step) {
1141 step = nu->pntsu * nu->pntsv;
1142 do_key(a, a + step, tot, out, key, actkb, k, t, KEY_MODE_BPOINT);
1144 else if (nu->bezt) {
1145 step = 3 * nu->pntsu;
1146 do_key(a, a + step, tot, out, key, actkb, k, t, KEY_MODE_BEZTRIPLE);
1154 static void do_rel_cu_key(Curve *cu, Key *key, KeyBlock *actkb, char *out, const int tot)
1159 for (a = 0, nu = cu->nurb.first; nu; nu = nu->next, a += step) {
1161 step = nu->pntsu * nu->pntsv;
1162 do_rel_key(a, a + step, tot, out, key, actkb, KEY_MODE_BPOINT);
1164 else if (nu->bezt) {
1165 step = 3 * nu->pntsu;
1166 do_rel_key(a, a + step, tot, out, key, actkb, KEY_MODE_BEZTRIPLE);
1174 static void do_curve_key(Scene *scene, Object *ob, Key *key, char *out, const int tot)
1176 Curve *cu = ob->data;
1177 KeyBlock *k[4], *actkb = ob_get_keyblock(ob);
1181 if (key->slurph && key->type != KEY_RELATIVE) {
1182 const float ctime_scaled = key->ctime / 100.0f;
1183 float delta = (float)key->slurph / tot;
1184 float cfra = (float)scene->r.cfra;
1186 int i = 0, remain = 0;
1189 if (tot > 100 && slurph_opt) {
1192 /* in do_key and cp_key the case a>tot has been handled */
1198 for (nu = cu->nurb.first; nu; nu = nu->next) {
1202 mode = KEY_MODE_BPOINT;
1203 estep = nu->pntsu * nu->pntsv;
1205 else if (nu->bezt) {
1206 mode = KEY_MODE_BEZTRIPLE;
1207 estep = 3 * nu->pntsu;
1220 flag = setkeys(ctime_scaled, &key->block, k, t, 0);
1225 count = MIN2(remain, estep);
1226 if (mode == KEY_MODE_BEZTRIPLE) {
1227 count += 3 - count % 3;
1231 do_key(i, i + count, tot, (char *)out, key, actkb, k, t, mode);
1233 cp_key(i, i + count, tot, (char *)out, key, actkb, k[2], NULL, mode);
1242 if (key->type == KEY_RELATIVE) {
1243 do_rel_cu_key(cu, cu->key, actkb, out, tot);
1246 const float ctime_scaled = key->ctime / 100.0f;
1248 flag = setkeys(ctime_scaled, &key->block, k, t, 0);
1250 if (flag == 0) do_cu_key(cu, key, actkb, k, t, out, tot);
1251 else cp_cu_key(cu, key, actkb, k[2], 0, tot, out, tot);
1256 static void do_latt_key(Scene *scene, Object *ob, Key *key, char *out, const int tot)
1258 Lattice *lt = ob->data;
1259 KeyBlock *k[4], *actkb = ob_get_keyblock(ob);
1263 if (key->slurph && key->type != KEY_RELATIVE) {
1264 const float ctime_scaled = key->ctime / 100.0f;
1265 float delta = (float)key->slurph / tot;
1266 float cfra = (float)scene->r.cfra;
1269 for (a = 0; a < tot; a++, cfra += delta) {
1270 flag = setkeys(ctime_scaled, &key->block, k, t, 0);
1273 do_key(a, a + 1, tot, out, key, actkb, k, t, KEY_MODE_DUMMY);
1275 cp_key(a, a + 1, tot, out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1279 if (key->type == KEY_RELATIVE) {
1282 for (kb = key->block.first; kb; kb = kb->next)
1283 kb->weights = get_weights_array(ob, kb->vgroup);
1285 do_rel_key(0, tot, tot, out, key, actkb, KEY_MODE_DUMMY);
1287 for (kb = key->block.first; kb; kb = kb->next) {
1288 if (kb->weights) MEM_freeN(kb->weights);
1293 const float ctime_scaled = key->ctime / 100.0f;
1295 flag = setkeys(ctime_scaled, &key->block, k, t, 0);
1298 do_key(0, tot, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
1300 cp_key(0, tot, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
1304 if (lt->flag & LT_OUTSIDE) outside_lattice(lt);
1307 /* returns key coordinates (+ tilt) when key applied, NULL otherwise */
1308 float *do_ob_key(Scene *scene, Object *ob)
1310 Key *key = ob_get_key(ob);
1311 KeyBlock *actkb = ob_get_keyblock(ob);
1313 int tot = 0, size = 0;
1315 if (key == NULL || key->block.first == NULL)
1318 /* compute size of output array */
1319 if (ob->type == OB_MESH) {
1320 Mesh *me = ob->data;
1323 size = tot * 3 * sizeof(float);
1325 else if (ob->type == OB_LATTICE) {
1326 Lattice *lt = ob->data;
1328 tot = lt->pntsu * lt->pntsv * lt->pntsw;
1329 size = tot * 3 * sizeof(float);
1331 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1332 Curve *cu = ob->data;
1335 for (nu = cu->nurb.first; nu; nu = nu->next) {
1337 tot += 3 * nu->pntsu;
1338 size += nu->pntsu * 12 * sizeof(float);
1341 tot += nu->pntsu * nu->pntsv;
1342 size += nu->pntsu * nu->pntsv * 12 * sizeof(float);
1347 /* if nothing to interpolate, cancel */
1348 if (tot == 0 || size == 0)
1351 /* allocate array */
1352 out = MEM_callocN(size, "do_ob_key out");
1354 /* prevent python from screwing this up? anyhoo, the from pointer could be dropped */
1355 key->from = (ID *)ob->data;
1357 if (ob->shapeflag & OB_SHAPE_LOCK) {
1358 /* shape locked, copy the locked shape instead of blending */
1359 KeyBlock *kb = BLI_findlink(&key->block, ob->shapenr - 1);
1361 if (kb && (kb->flag & KEYBLOCK_MUTE))
1365 kb = key->block.first;
1369 if (OB_TYPE_SUPPORT_VGROUP(ob->type)) {
1370 float *weights = get_weights_array(ob, kb->vgroup);
1372 cp_key(0, tot, tot, out, key, actkb, kb, weights, 0);
1374 if (weights) MEM_freeN(weights);
1376 else if (ELEM(ob->type, OB_CURVE, OB_SURF))
1377 cp_cu_key(ob->data, key, actkb, kb, 0, tot, out, tot);
1380 /* do shapekey local drivers */
1381 float ctime = (float)scene->r.cfra; // XXX this needs to be checked
1383 BKE_animsys_evaluate_animdata(scene, &key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
1385 if (ob->type == OB_MESH) do_mesh_key(scene, ob, key, out, tot);
1386 else if (ob->type == OB_LATTICE) do_latt_key(scene, ob, key, out, tot);
1387 else if (ob->type == OB_CURVE) do_curve_key(scene, ob, key, out, tot);
1388 else if (ob->type == OB_SURF) do_curve_key(scene, ob, key, out, tot);
1391 return (float *)out;
1394 Key *ob_get_key(Object *ob)
1396 if (ob == NULL) return NULL;
1398 if (ob->type == OB_MESH) {
1399 Mesh *me = ob->data;
1402 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1403 Curve *cu = ob->data;
1406 else if (ob->type == OB_LATTICE) {
1407 Lattice *lt = ob->data;
1413 KeyBlock *add_keyblock(Key *key, const char *name)
1416 float curpos = -0.1;
1419 kb = key->block.last;
1420 if (kb) curpos = kb->pos;
1422 kb = MEM_callocN(sizeof(KeyBlock), "Keyblock");
1423 BLI_addtail(&key->block, kb);
1424 kb->type = KEY_CARDINAL;
1426 tot = BLI_countlist(&key->block);
1428 BLI_strncpy(kb->name, name, sizeof(kb->name));
1431 if (tot == 1) BLI_strncpy(kb->name, "Basis", sizeof(kb->name));
1432 else BLI_snprintf(kb->name, sizeof(kb->name), "Key %d", tot - 1);
1435 BLI_uniquename(&key->block, kb, "Key", '.', offsetof(KeyBlock, name), sizeof(kb->name));
1437 kb->uid = key->uidgen++;
1440 if (key->totkey == 1) key->refkey = kb;
1442 kb->slidermin = 0.0f;
1443 kb->slidermax = 1.0f;
1446 * \note caller may want to set this to current time, but don't do it here since we need to sort
1447 * which could cause problems in some cases, see #add_keyblock_ctime */
1448 kb->pos = curpos + 0.1f; /* only used for absolute shape keys */
1454 * \note sorting is a problematic side effect in some cases,
1455 * better only do this explicitly by having its own function,
1457 * \param do_force always use ctime even for relative keys.
1459 KeyBlock *add_keyblock_ctime(Key *key, const char *name, const short do_force)
1461 KeyBlock *kb = add_keyblock(key, name);
1463 if (do_force || (key->type != KEY_RELATIVE)) {
1464 kb->pos = key->ctime / 100.0f;
1471 /* only the active keyblock */
1472 KeyBlock *ob_get_keyblock(Object *ob)
1474 Key *key = ob_get_key(ob);
1477 KeyBlock *kb = BLI_findlink(&key->block, ob->shapenr - 1);
1484 KeyBlock *ob_get_reference_keyblock(Object *ob)
1486 Key *key = ob_get_key(ob);
1494 /* get the appropriate KeyBlock given an index */
1495 KeyBlock *key_get_keyblock(Key *key, int index)
1501 kb = key->block.first;
1503 for (i = 1; i < key->totkey; i++) {
1514 /* get the appropriate KeyBlock given a name to search for */
1515 KeyBlock *key_get_named_keyblock(Key *key, const char name[])
1518 return BLI_findstring(&key->block, name, offsetof(KeyBlock, name));
1523 /* Get RNA-Path for 'value' setting of the given ShapeKey
1524 * NOTE: the user needs to free the returned string once they're finish with it
1526 char *key_get_curValue_rnaPath(Key *key, KeyBlock *kb)
1532 if (ELEM(NULL, key, kb))
1535 /* create the RNA pointer */
1536 RNA_pointer_create(&key->id, &RNA_ShapeKey, kb, &ptr);
1537 /* get pointer to the property too */
1538 prop = RNA_struct_find_property(&ptr, "value");
1540 /* return the path */
1541 return RNA_path_from_ID_to_property(&ptr, prop);
1545 /* conversion functions */
1547 /************************* Lattice ************************/
1548 void latt_to_key(Lattice *lt, KeyBlock *kb)
1554 tot = lt->pntsu * lt->pntsv * lt->pntsw;
1555 if (tot == 0) return;
1557 if (kb->data) MEM_freeN(kb->data);
1559 kb->data = MEM_callocN(lt->key->elemsize * tot, "kb->data");
1564 for (a = 0; a < kb->totelem; a++, fp += 3, bp++) {
1565 copy_v3_v3(fp, bp->vec);
1569 void key_to_latt(KeyBlock *kb, Lattice *lt)
1578 tot = lt->pntsu * lt->pntsv * lt->pntsw;
1579 tot = MIN2(kb->totelem, tot);
1581 for (a = 0; a < tot; a++, fp += 3, bp++) {
1582 copy_v3_v3(bp->vec, fp);
1586 /************************* Curve ************************/
1587 void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb)
1596 tot = BKE_nurbList_verts_count(nurb);
1597 if (tot == 0) return;
1599 if (kb->data) MEM_freeN(kb->data);
1601 kb->data = MEM_callocN(cu->key->elemsize * tot, "kb->data");
1612 copy_v3_v3(fp, bezt->vec[0]);
1614 copy_v3_v3(fp, bezt->vec[1]);
1616 copy_v3_v3(fp, bezt->vec[2]);
1619 fp += 3; /* alphas */
1625 a = nu->pntsu * nu->pntsv;
1627 copy_v3_v3(fp, bp->vec);
1638 void key_to_curve(KeyBlock *kb, Curve *UNUSED(cu), ListBase *nurb)
1649 tot = BKE_nurbList_verts_count(nurb);
1651 tot = MIN2(kb->totelem, tot);
1653 while (nu && tot > 0) {
1658 while (a-- && tot > 0) {
1659 copy_v3_v3(bezt->vec[0], fp);
1661 copy_v3_v3(bezt->vec[1], fp);
1663 copy_v3_v3(bezt->vec[2], fp);
1666 fp += 3; /* alphas */
1674 a = nu->pntsu * nu->pntsv;
1675 while (a-- && tot > 0) {
1676 copy_v3_v3(bp->vec, fp);
1688 /************************* Mesh ************************/
1689 void mesh_to_key(Mesh *me, KeyBlock *kb)
1695 if (me->totvert == 0) return;
1697 if (kb->data) MEM_freeN(kb->data);
1699 kb->data = MEM_callocN(me->key->elemsize * me->totvert, "kb->data");
1700 kb->totelem = me->totvert;
1704 for (a = 0; a < kb->totelem; a++, fp += 3, mvert++) {
1705 copy_v3_v3(fp, mvert->co);
1710 void key_to_mesh(KeyBlock *kb, Mesh *me)
1719 tot = MIN2(kb->totelem, me->totvert);
1721 for (a = 0; a < tot; a++, fp += 3, mvert++) {
1722 copy_v3_v3(mvert->co, fp);
1726 /************************* vert coords ************************/
1727 float (*key_to_vertcos(Object * ob, KeyBlock * kb))[3]
1729 float (*vertCos)[3], *co;
1730 float *fp = kb->data;
1733 /* Count of vertex coords in array */
1734 if (ob->type == OB_MESH) {
1735 Mesh *me = (Mesh *)ob->data;
1738 else if (ob->type == OB_LATTICE) {
1739 Lattice *lt = (Lattice *)ob->data;
1740 tot = lt->pntsu * lt->pntsv * lt->pntsw;
1742 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1743 Curve *cu = (Curve *)ob->data;
1744 tot = BKE_nurbList_verts_count(&cu->nurb);
1747 if (tot == 0) return NULL;
1749 vertCos = MEM_callocN(tot * sizeof(*vertCos), "key_to_vertcos vertCos");
1751 /* Copy coords to array */
1752 co = (float *)vertCos;
1754 if (ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1755 for (a = 0; a < tot; a++, fp += 3, co += 3) {
1759 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1760 Curve *cu = (Curve *)ob->data;
1761 Nurb *nu = cu->nurb.first;
1772 for (i = 0; i < 3; i++) {
1777 fp += 3; /* skip alphas */
1784 a = nu->pntsu * nu->pntsv;
1803 void vertcos_to_key(Object *ob, KeyBlock *kb, float (*vertCos)[3])
1805 float *co = (float *)vertCos, *fp;
1806 int tot = 0, a, elemsize;
1808 if (kb->data) MEM_freeN(kb->data);
1810 /* Count of vertex coords in array */
1811 if (ob->type == OB_MESH) {
1812 Mesh *me = (Mesh *)ob->data;
1814 elemsize = me->key->elemsize;
1816 else if (ob->type == OB_LATTICE) {
1817 Lattice *lt = (Lattice *)ob->data;
1818 tot = lt->pntsu * lt->pntsv * lt->pntsw;
1819 elemsize = lt->key->elemsize;
1821 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1822 Curve *cu = (Curve *)ob->data;
1823 elemsize = cu->key->elemsize;
1824 tot = BKE_nurbList_verts_count(&cu->nurb);
1832 fp = kb->data = MEM_callocN(tot * elemsize, "key_to_vertcos vertCos");
1834 /* Copy coords to keyblock */
1836 if (ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1837 for (a = 0; a < tot; a++, fp += 3, co += 3) {
1841 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1842 Curve *cu = (Curve *)ob->data;
1843 Nurb *nu = cu->nurb.first;
1854 for (i = 0; i < 3; i++) {
1859 fp += 3; /* skip alphas */
1866 a = nu->pntsu * nu->pntsv;
1883 void offset_to_key(Object *ob, KeyBlock *kb, float (*ofs)[3])
1886 float *co = (float *)ofs, *fp = kb->data;
1888 if (ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1889 for (a = 0; a < kb->totelem; 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;