78b9c04e4d2e34c6a229b1573debc63931ebc37c
[blender-staging.git] / source / blender / blenkernel / intern / key.c
1
2 /*  key.c      
3  *  
4  * 
5  * $Id$
6  *
7  * ***** BEGIN GPL LICENSE BLOCK *****
8  *
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.
13  *
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.
18  *
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.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 #include <math.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_blenlib.h"
39
40 #include "DNA_anim_types.h"
41 #include "DNA_curve_types.h"
42 #include "DNA_key_types.h"
43 #include "DNA_lattice_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_meshdata_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_scene_types.h"
48
49 #include "BKE_animsys.h"
50 #include "BKE_action.h"
51 #include "BKE_blender.h"
52 #include "BKE_curve.h"
53 #include "BKE_global.h"
54 #include "BKE_key.h"
55 #include "BKE_lattice.h"
56 #include "BKE_library.h"
57 #include "BKE_mesh.h"
58 #include "BKE_main.h"
59 #include "BKE_object.h"
60 #include "BKE_utildefines.h"
61
62 #include "RNA_access.h"
63
64
65 #ifdef HAVE_CONFIG_H
66 #include <config.h>
67 #endif
68
69 #define KEY_BPOINT              1
70 #define KEY_BEZTRIPLE   2
71
72         // old defines from DNA_ipo_types.h for data-type
73 #define IPO_FLOAT               4
74 #define IPO_BEZTRIPLE   100
75 #define IPO_BPOINT              101
76
77 int slurph_opt= 1;
78
79
80 void free_key(Key *key)
81 {
82         KeyBlock *kb;
83         
84         BKE_free_animdata((ID *)key);
85         
86         while( (kb= key->block.first) ) {
87                 
88                 if(kb->data) MEM_freeN(kb->data);
89                 
90                 BLI_remlink(&key->block, kb);
91                 MEM_freeN(kb);
92         }
93         
94 }
95
96 /* GS reads the memory pointed at in a specific ordering. There are,
97  * however two definitions for it. I have jotted them down here, both,
98  * but I think the first one is actually used. The thing is that
99  * big-endian systems might read this the wrong way round. OTOH, we
100  * constructed the IDs that are read out with this macro explicitly as
101  * well. I expect we'll sort it out soon... */
102
103 /* from blendef: */
104 #define GS(a)   (*((short *)(a)))
105
106 /* from misc_util: flip the bytes from x  */
107 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
108
109 Key *add_key(ID *id)    /* common function */
110 {
111         Key *key;
112         char *el;
113         
114         key= alloc_libblock(&G.main->key, ID_KE, "Key");
115         
116         key->type= KEY_NORMAL;
117         key->from= id;
118         
119         // XXX the code here uses some defines which will soon be depreceated...
120         if( GS(id->name)==ID_ME) {
121                 el= key->elemstr;
122                 
123                 el[0]= 3;
124                 el[1]= IPO_FLOAT;
125                 el[2]= 0;
126                 
127                 key->elemsize= 12;
128         }
129         else if( GS(id->name)==ID_LT) {
130                 el= key->elemstr;
131                 
132                 el[0]= 3;
133                 el[1]= IPO_FLOAT;
134                 el[2]= 0;
135                 
136                 key->elemsize= 12;
137         }
138         else if( GS(id->name)==ID_CU) {
139                 el= key->elemstr;
140                 
141                 el[0]= 4;
142                 el[1]= IPO_BPOINT;
143                 el[2]= 0;
144                 
145                 key->elemsize= 16;
146         }
147         
148         return key;
149 }
150
151 Key *copy_key(Key *key)
152 {
153         Key *keyn;
154         KeyBlock *kbn, *kb;
155         
156         if(key==0) return 0;
157         
158         keyn= copy_libblock(key);
159         
160 #if 0 // XXX old animation system
161         keyn->ipo= copy_ipo(key->ipo);
162 #endif // XXX old animation system
163         
164         BLI_duplicatelist(&keyn->block, &key->block);
165         
166         kb= key->block.first;
167         kbn= keyn->block.first;
168         while(kbn) {
169                 
170                 if(kbn->data) kbn->data= MEM_dupallocN(kbn->data);
171                 if(kb==key->refkey) keyn->refkey= kbn;
172                 
173                 kbn= kbn->next;
174                 kb= kb->next;
175         }
176         
177         return keyn;
178 }
179
180 void make_local_key(Key *key)
181 {
182
183     /* - only lib users: do nothing
184     * - only local users: set flag
185     * - mixed: make copy
186     */
187     if(key==0) return;
188         
189         key->id.lib= 0;
190         new_id(0, (ID *)key, 0);
191
192 #if 0 // XXX old animation system
193         make_local_ipo(key->ipo);
194 #endif // XXX old animation system
195 }
196
197 /* Sort shape keys and Ipo curves after a change.  This assumes that at most
198  * one key was moved, which is a valid assumption for the places it's
199  * currently being called.
200  */
201
202 void sort_keys(Key *key)
203 {
204         KeyBlock *kb;
205         //short i, adrcode;
206         //IpoCurve *icu = NULL;
207         KeyBlock *kb2;
208
209         /* locate the key which is out of position */ 
210         for (kb= key->block.first; kb; kb= kb->next)
211                 if ((kb->next) && (kb->pos > kb->next->pos))
212                         break;
213
214         /* if we find a key, move it */
215         if (kb) {
216                 kb = kb->next; /* next key is the out-of-order one */
217                 BLI_remlink(&key->block, kb);
218                 
219                 /* find the right location and insert before */
220                 for (kb2=key->block.first; kb2; kb2= kb2->next) {
221                         if (kb2->pos > kb->pos) {
222                                 BLI_insertlink(&key->block, kb2->prev, kb);
223                                 break;
224                         }
225                 }
226                 
227                 /* if more than one Ipo curve, see if this key had a curve */
228 #if 0 // XXX old animation system
229                 if(key->ipo && key->ipo->curve.first != key->ipo->curve.last ) {
230                         for(icu= key->ipo->curve.first; icu; icu= icu->next) {
231                                 /* if we find the curve, remove it and reinsert in the 
232                                  right place */
233                                 if(icu->adrcode==kb->adrcode) {
234                                         IpoCurve *icu2;
235                                         BLI_remlink(&key->ipo->curve, icu);
236                                         for(icu2= key->ipo->curve.first; icu2; icu2= icu2->next) {
237                                                 if(icu2->adrcode >= kb2->adrcode) {
238                                                         BLI_insertlink(&key->ipo->curve, icu2->prev, icu);
239                                                         break;
240                                                 }
241                                         }
242                                         break;
243                                 }
244                         }
245                 }
246                 
247                 /* kb points at the moved key, icu at the moved ipo (if it exists).
248                  * go back now and renumber adrcodes */
249
250                 /* first new code */
251                 adrcode = kb2->adrcode;
252                 for (i = kb->adrcode - adrcode; i >= 0; i--, adrcode++) {
253                         /* if the next ipo curve matches the current key, renumber it */
254                         if(icu && icu->adrcode == kb->adrcode ) {
255                                 icu->adrcode = adrcode;
256                                 icu = icu->next;
257                         }
258                         /* renumber the shape key */
259                         kb->adrcode = adrcode;
260                         kb = kb->next;
261                 }
262 #endif // XXX old animation system
263         }
264
265         /* new rule; first key is refkey, this to match drawing channels... */
266         key->refkey= key->block.first;
267 }
268
269 /**************** do the key ****************/
270
271 void key_curve_position_weights(float t, float *data, int type)
272 {
273         float t2, t3, fc;
274         
275         if(type==KEY_LINEAR) {
276                 data[0]=                  0.0f;
277                 data[1]= -t             + 1.0f;
278                 data[2]= t;
279                 data[3]=                  0.0f;
280         }
281         else if(type==KEY_CARDINAL) {
282                 t2= t*t;
283                 t3= t2*t;
284                 fc= 0.71f;
285                 
286                 data[0]= -fc*t3                 + 2.0f*fc*t2            - fc*t;
287                 data[1]= (2.0f-fc)*t3   + (fc-3.0f)*t2                                  + 1.0f;
288                 data[2]= (fc-2.0f)*t3   + (3.0f-2.0f*fc)*t2     + fc*t;
289                 data[3]= fc*t3                  - fc*t2;
290         }
291         else if(type==KEY_BSPLINE) {
292                 t2= t*t;
293                 t3= t2*t;
294
295                 data[0]= -0.16666666f*t3        + 0.5f*t2       - 0.5f*t        + 0.16666666f;
296                 data[1]= 0.5f*t3                        - t2                                    + 0.6666666f;
297                 data[2]= -0.5f*t3                       + 0.5f*t2       + 0.5f*t        + 0.16666666f;
298                 data[3]= 0.16666666f*t3;
299         }
300 }
301
302 /* first derivative */
303 void key_curve_tangent_weights(float t, float *data, int type)
304 {
305         float t2, fc;
306         
307         if(type==KEY_LINEAR) {
308                 data[0]= 0.0f;
309                 data[1]= -1.0f;
310                 data[2]= 1.0f;
311                 data[3]= 0.0f;
312         }
313         else if(type==KEY_CARDINAL) {
314                 t2= t*t;
315                 fc= 0.71f;
316                 
317                 data[0]= -3.0f*fc*t2            +4.0f*fc*t                              - fc;
318                 data[1]= 3.0f*(2.0f-fc)*t2      +2.0f*(fc-3.0f)*t;
319                 data[2]= 3.0f*(fc-2.0f)*t2      +2.0f*(3.0f-2.0f*fc)*t  + fc;
320                 data[3]= 3.0f*fc*t2                     -2.0f*fc*t;
321         }
322         else if(type==KEY_BSPLINE) {
323                 t2= t*t;
324
325                 data[0]= -0.5f*t2       + t                     - 0.5f;
326                 data[1]= 1.5f*t2        - 2.0f*t;
327                 data[2]= -1.5f*t2       + t                     + 0.5f;
328                 data[3]= 0.5f*t2;
329         }
330 }
331
332 /* second derivative */
333 void key_curve_normal_weights(float t, float *data, int type)
334 {
335         float fc;
336         
337         if(type==KEY_LINEAR) {
338                 data[0]= 0.0f;
339                 data[1]= 0.0f;
340                 data[2]= 0.0f;
341                 data[3]= 0.0f;
342         }
343         else if(type==KEY_CARDINAL) {
344                 fc= 0.71f;
345                 
346                 data[0]= -6.0f*fc*t                     + 4.0f*fc;
347                 data[1]= 6.0f*(2.0f-fc)*t       + 2.0f*(fc-3.0f);
348                 data[2]= 6.0f*(fc-2.0f)*t       + 2.0f*(3.0f-2.0f*fc);
349                 data[3]= 6.0f*fc*t                      - 2.0f*fc;
350         }
351         else if(type==KEY_BSPLINE) {
352                 data[0]= -1.0f*t        + 1.0f;
353                 data[1]= 3.0f*t         - 2.0f;
354                 data[2]= -3.0f*t        + 1.0f;
355                 data[3]= 1.0f*t;
356         }
357 }
358
359 static int setkeys(float fac, ListBase *lb, KeyBlock *k[], float *t, int cycl)
360 {
361         /* return 1 means k[2] is the position, return 0 means interpolate */
362         KeyBlock *k1, *firstkey;
363         float d, dpos, ofs=0, lastpos, temp, fval[4];
364         short bsplinetype;
365
366         firstkey= lb->first;
367         k1= lb->last;
368         lastpos= k1->pos;
369         dpos= lastpos - firstkey->pos;
370
371         if(fac < firstkey->pos) fac= firstkey->pos;
372         else if(fac > k1->pos) fac= k1->pos;
373
374         k1=k[0]=k[1]=k[2]=k[3]= firstkey;
375         t[0]=t[1]=t[2]=t[3]= k1->pos;
376
377         /* if(fac<0.0 || fac>1.0) return 1; */
378
379         if(k1->next==0) return 1;
380
381         if(cycl) {      /* pre-sort */
382                 k[2]= k1->next;
383                 k[3]= k[2]->next;
384                 if(k[3]==0) k[3]=k1;
385                 while(k1) {
386                         if(k1->next==0) k[0]=k1;
387                         k1=k1->next;
388                 }
389                 k1= k[1];
390                 t[0]= k[0]->pos;
391                 t[1]+= dpos;
392                 t[2]= k[2]->pos + dpos;
393                 t[3]= k[3]->pos + dpos;
394                 fac+= dpos;
395                 ofs= dpos;
396                 if(k[3]==k[1]) { 
397                         t[3]+= dpos; 
398                         ofs= 2.0f*dpos;
399                 }
400                 if(fac<t[1]) fac+= dpos;
401                 k1= k[3];
402         }
403         else {          /* pre-sort */
404                 k[2]= k1->next;
405                 t[2]= k[2]->pos;
406                 k[3]= k[2]->next;
407                 if(k[3]==0) k[3]= k[2];
408                 t[3]= k[3]->pos;
409                 k1= k[3];
410         }
411         
412         while( t[2]<fac ) {     /* find correct location */
413                 if(k1->next==0) {
414                         if(cycl) {
415                                 k1= firstkey;
416                                 ofs+= dpos;
417                         }
418                         else if(t[2]==t[3]) break;
419                 }
420                 else k1= k1->next;
421
422                 t[0]= t[1]; 
423                 k[0]= k[1];
424                 t[1]= t[2]; 
425                 k[1]= k[2];
426                 t[2]= t[3]; 
427                 k[2]= k[3];
428                 t[3]= k1->pos+ofs; 
429                 k[3]= k1;
430
431                 if(ofs>2.1+lastpos) break;
432         }
433         
434         bsplinetype= 0;
435         if(k[1]->type==KEY_BSPLINE || k[2]->type==KEY_BSPLINE) bsplinetype= 1;
436
437
438         if(cycl==0) {
439                 if(bsplinetype==0) {    /* B spline doesn't go through the control points */
440                         if(fac<=t[1]) {         /* fac for 1st key */
441                                 t[2]= t[1];
442                                 k[2]= k[1];
443                                 return 1;
444                         }
445                         if(fac>=t[2] ) {        /* fac after 2nd key */
446                                 return 1;
447                         }
448                 }
449                 else if(fac>t[2]) {     /* last key */
450                         fac= t[2];
451                         k[3]= k[2];
452                         t[3]= t[2];
453                 }
454         }
455
456         d= t[2]-t[1];
457         if(d==0.0) {
458                 if(bsplinetype==0) {
459                         return 1;       /* both keys equal */
460                 }
461         }
462         else d= (fac-t[1])/d;
463
464         /* interpolation */
465         
466         key_curve_position_weights(d, t, k[1]->type);
467
468         if(k[1]->type != k[2]->type) {
469                 key_curve_position_weights(d, fval, k[2]->type);
470                 
471                 temp= 1.0f-d;
472                 t[0]= temp*t[0]+ d*fval[0];
473                 t[1]= temp*t[1]+ d*fval[1];
474                 t[2]= temp*t[2]+ d*fval[2];
475                 t[3]= temp*t[3]+ d*fval[3];
476         }
477
478         return 0;
479
480 }
481
482 static void flerp(int aantal, float *in, float *f0, float *f1, float *f2, float *f3, float *t)  
483 {
484         int a;
485
486         for(a=0; a<aantal; a++) {
487                 in[a]= t[0]*f0[a]+t[1]*f1[a]+t[2]*f2[a]+t[3]*f3[a];
488         }
489 }
490
491 static void rel_flerp(int aantal, float *in, float *ref, float *out, float fac)
492 {
493         int a;
494         
495         for(a=0; a<aantal; a++) {
496                 in[a]-= fac*(ref[a]-out[a]);
497         }
498 }
499
500 static void *key_block_get_data(Key *key, KeyBlock *kb)
501 {
502         /* editmode shape key apply test */
503 #if 0
504         EditVert *eve;
505         Mesh *me;
506         float (*co)[3];
507         int a;
508
509         if(kb != key->refkey) {
510                 if(GS(key->from->name) == ID_ME) {
511                         me= (Mesh*)key->from;
512
513                         if(me->edit_mesh) {
514                                 a= 0;
515                                 co= kb->data;
516
517                                 for(eve=me->edit_mesh->verts.first; eve; eve=eve->next, a++)
518                                         VECCOPY(co[a], eve->co);
519                         }
520                 }
521         }
522 #endif
523
524         return kb->data;
525 }
526
527 static void cp_key(int start, int end, int tot, char *poin, Key *key, KeyBlock *kb, float *weights, int mode)
528 {
529         float ktot = 0.0, kd = 0.0;
530         int elemsize, poinsize = 0, a, *ofsp, ofs[32], flagflo=0;
531         char *k1, *kref;
532         char *cp, elemstr[8];
533
534         if(key->from==NULL) return;
535
536         if( GS(key->from->name)==ID_ME ) {
537                 ofs[0]= sizeof(float)*3;
538                 ofs[1]= 0;
539                 poinsize= ofs[0];
540         }
541         else if( GS(key->from->name)==ID_LT ) {
542                 ofs[0]= sizeof(float)*3;
543                 ofs[1]= 0;
544                 poinsize= ofs[0];
545         }
546         else if( GS(key->from->name)==ID_CU ) {
547                 if(mode==KEY_BPOINT) ofs[0]= sizeof(float)*4;
548                 else ofs[0]= sizeof(float)*10;
549                 
550                 ofs[1]= 0;
551                 poinsize= ofs[0];
552         }
553
554         if(end>tot) end= tot;
555         
556         k1= key_block_get_data(key, kb);
557         kref= key_block_get_data(key, key->refkey);
558         
559         if(tot != kb->totelem) {
560                 ktot= 0.0;
561                 flagflo= 1;
562                 if(kb->totelem) {
563                         kd= kb->totelem/(float)tot;
564                 }
565                 else return;
566         }
567
568         /* this exception is needed for slurphing */
569         if(start!=0) {
570                 
571                 poin+= poinsize*start;
572                 
573                 if(flagflo) {
574                         ktot+= start*kd;
575                         a= (int)floor(ktot);
576                         if(a) {
577                                 ktot-= a;
578                                 k1+= a*key->elemsize;
579                         }
580                 }
581                 else k1+= start*key->elemsize;
582         }       
583         
584         if(mode==KEY_BEZTRIPLE) {
585                 elemstr[0]= 1;
586                 elemstr[1]= IPO_BEZTRIPLE;
587                 elemstr[2]= 0;
588         }
589         
590         /* just do it here, not above! */
591         elemsize= key->elemsize;
592         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
593
594         for(a=start; a<end; a++) {
595                 cp= key->elemstr;
596                 if(mode==KEY_BEZTRIPLE) cp= elemstr;
597
598                 ofsp= ofs;
599                 
600                 while( cp[0] ) {
601                         
602                         switch(cp[1]) {
603                         case IPO_FLOAT:
604                                 if(weights) {
605                                         memcpy(poin, kref, sizeof(float)*3);
606                                         if(*weights!=0.0f)
607                                                 rel_flerp(cp[0], (float *)poin, (float *)kref, (float *)k1, *weights);
608                                         weights++;
609                                 }
610                                 else 
611                                         memcpy(poin, k1, sizeof(float)*3);
612                                 break;
613                         case IPO_BPOINT:
614                                 memcpy(poin, k1, sizeof(float)*4);
615                                 break;
616                         case IPO_BEZTRIPLE:
617                                 memcpy(poin, k1, sizeof(float)*10);
618                                 break;
619                         }
620                         
621                         poin+= ofsp[0]; 
622                         cp+= 2; ofsp++;
623                 }
624                 
625                 /* are we going to be nasty? */
626                 if(flagflo) {
627                         ktot+= kd;
628                         while(ktot>=1.0) {
629                                 ktot-= 1.0;
630                                 k1+= elemsize;
631                                 kref+= elemsize;
632                         }
633                 }
634                 else {
635                         k1+= elemsize;
636                         kref+= elemsize;
637                 }
638                 
639                 if(mode==KEY_BEZTRIPLE) a+=2;
640         }
641 }
642
643 static void cp_cu_key(Curve *cu, KeyBlock *kb, int start, int end, char *out, int tot)
644 {
645         Nurb *nu;
646         char *poin;
647         int a, step, a1, a2;
648
649         for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
650                 if(nu->bp) {
651                         step= nu->pntsu*nu->pntsv;
652                         
653                         /* exception because keys prefer to work with complete blocks */
654                         poin= out - a*sizeof(float)*4;
655                         a1= MAX2(a, start);
656                         a2= MIN2(a+step, end);
657                         
658                         if(a1<a2) cp_key(a1, a2, tot, poin, cu->key, kb, NULL, KEY_BPOINT);
659                 }
660                 else if(nu->bezt) {
661                         step= 3*nu->pntsu;
662                         
663                         poin= out - a*sizeof(float)*10;
664                         a1= MAX2(a, start);
665                         a2= MIN2(a+step, end);
666
667                         if(a1<a2) cp_key(a1, a2, tot, poin, cu->key, kb, NULL, KEY_BEZTRIPLE);
668                 }
669                 else
670                         step= 0;
671         }
672 }
673
674
675 void do_rel_key(int start, int end, int tot, char *basispoin, Key *key, int mode)
676 {
677         KeyBlock *kb;
678         int *ofsp, ofs[3], elemsize, b;
679         char *cp, *poin, *reffrom, *from, elemstr[8];
680         
681         if(key->from==NULL) return;
682         
683         if( GS(key->from->name)==ID_ME ) {
684                 ofs[0]= sizeof(float)*3;
685                 ofs[1]= 0;
686         }
687         else if( GS(key->from->name)==ID_LT ) {
688                 ofs[0]= sizeof(float)*3;
689                 ofs[1]= 0;
690         }
691         else if( GS(key->from->name)==ID_CU ) {
692                 if(mode==KEY_BPOINT) ofs[0]= sizeof(float)*4;
693                 else ofs[0]= sizeof(float)*10;
694                 
695                 ofs[1]= 0;
696         }
697         
698         if(end>tot) end= tot;
699         
700         /* in case of beztriple */
701         elemstr[0]= 1;                          /* nr of ipofloats */
702         elemstr[1]= IPO_BEZTRIPLE;
703         elemstr[2]= 0;
704
705         /* just here, not above! */
706         elemsize= key->elemsize;
707         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
708
709         /* step 1 init */
710         cp_key(start, end, tot, basispoin, key, key->refkey, NULL, mode);
711         
712         /* step 2: do it */
713         
714         for(kb=key->block.first; kb; kb=kb->next) {
715                 if(kb!=key->refkey) {
716                         float icuval= kb->curval;
717                         
718                         /* only with value, and no difference allowed */
719                         if(!(kb->flag & KEYBLOCK_MUTE) && icuval!=0.0f && kb->totelem==tot) {
720                                 KeyBlock *refb;
721                                 float weight, *weights= kb->weights;
722                                 
723                                 poin= basispoin;
724                                 from= key_block_get_data(key, kb);
725                                 /* reference now can be any block */
726                                 refb= BLI_findlink(&key->block, kb->relative);
727                                 if(refb==NULL) continue;
728                                 reffrom= key_block_get_data(key, refb);
729                                 
730                                 poin+= start*ofs[0];
731                                 reffrom+= key->elemsize*start;  // key elemsize yes!
732                                 from+= key->elemsize*start;
733                                 
734                                 for(b=start; b<end; b++) {
735                                 
736                                         if(weights) 
737                                                 weight= *weights * icuval;
738                                         else
739                                                 weight= icuval;
740                                         
741                                         cp= key->elemstr;       
742                                         if(mode==KEY_BEZTRIPLE) cp= elemstr;
743                                         
744                                         ofsp= ofs;
745                                         
746                                         while( cp[0] ) {        /* cp[0]==amount */
747                                                 
748                                                 switch(cp[1]) {
749                                                 case IPO_FLOAT:
750                                                         rel_flerp(3, (float *)poin, (float *)reffrom, (float *)from, weight);
751                                                         break;
752                                                 case IPO_BPOINT:
753                                                         rel_flerp(4, (float *)poin, (float *)reffrom, (float *)from, weight);
754                                                         break;
755                                                 case IPO_BEZTRIPLE:
756                                                         rel_flerp(10, (float *)poin, (float *)reffrom, (float *)from, weight);
757                                                         break;
758                                                 }
759                                                 
760                                                 poin+= ofsp[0];                         
761                                                 
762                                                 cp+= 2;
763                                                 ofsp++;
764                                         }
765                                         
766                                         reffrom+= elemsize;
767                                         from+= elemsize;
768                                         
769                                         if(mode==KEY_BEZTRIPLE) b+= 2;
770                                         if(weights) weights++;
771                                 }
772                         }
773                 }
774         }
775 }
776
777
778 static void do_key(int start, int end, int tot, char *poin, Key *key, KeyBlock **k, float *t, int mode)
779 {
780         float k1tot = 0.0, k2tot = 0.0, k3tot = 0.0, k4tot = 0.0;
781         float k1d = 0.0, k2d = 0.0, k3d = 0.0, k4d = 0.0;
782         int a, ofs[32], *ofsp;
783         int flagdo= 15, flagflo=0, elemsize, poinsize=0;
784         char *k1, *k2, *k3, *k4;
785         char *cp, elemstr[8];;
786
787         if(key->from==0) return;
788
789         if( GS(key->from->name)==ID_ME ) {
790                 ofs[0]= sizeof(float)*3;
791                 ofs[1]= 0;
792                 poinsize= ofs[0];
793         }
794         else if( GS(key->from->name)==ID_LT ) {
795                 ofs[0]= sizeof(float)*3;
796                 ofs[1]= 0;
797                 poinsize= ofs[0];
798         }
799         else if( GS(key->from->name)==ID_CU ) {
800                 if(mode==KEY_BPOINT) ofs[0]= sizeof(float)*4;
801                 else ofs[0]= sizeof(float)*10;
802                 
803                 ofs[1]= 0;
804                 poinsize= ofs[0];
805         }
806         
807         if(end>tot) end= tot;
808
809         k1= key_block_get_data(key, k[0]);
810         k2= key_block_get_data(key, k[1]);
811         k3= key_block_get_data(key, k[2]);
812         k4= key_block_get_data(key, k[3]);
813
814         /*  test for more or less points (per key!) */
815         if(tot != k[0]->totelem) {
816                 k1tot= 0.0;
817                 flagflo |= 1;
818                 if(k[0]->totelem) {
819                         k1d= k[0]->totelem/(float)tot;
820                 }
821                 else flagdo -= 1;
822         }
823         if(tot != k[1]->totelem) {
824                 k2tot= 0.0;
825                 flagflo |= 2;
826                 if(k[0]->totelem) {
827                         k2d= k[1]->totelem/(float)tot;
828                 }
829                 else flagdo -= 2;
830         }
831         if(tot != k[2]->totelem) {
832                 k3tot= 0.0;
833                 flagflo |= 4;
834                 if(k[0]->totelem) {
835                         k3d= k[2]->totelem/(float)tot;
836                 }
837                 else flagdo -= 4;
838         }
839         if(tot != k[3]->totelem) {
840                 k4tot= 0.0;
841                 flagflo |= 8;
842                 if(k[0]->totelem) {
843                         k4d= k[3]->totelem/(float)tot;
844                 }
845                 else flagdo -= 8;
846         }
847
848                 /* this exception needed for slurphing */
849         if(start!=0) {
850
851                 poin+= poinsize*start;
852                 
853                 if(flagdo & 1) {
854                         if(flagflo & 1) {
855                                 k1tot+= start*k1d;
856                                 a= (int)floor(k1tot);
857                                 if(a) {
858                                         k1tot-= a;
859                                         k1+= a*key->elemsize;
860                                 }
861                         }
862                         else k1+= start*key->elemsize;
863                 }
864                 if(flagdo & 2) {
865                         if(flagflo & 2) {
866                                 k2tot+= start*k2d;
867                                 a= (int)floor(k2tot);
868                                 if(a) {
869                                         k2tot-= a;
870                                         k2+= a*key->elemsize;
871                                 }
872                         }
873                         else k2+= start*key->elemsize;
874                 }
875                 if(flagdo & 4) {
876                         if(flagflo & 4) {
877                                 k3tot+= start*k3d;
878                                 a= (int)floor(k3tot);
879                                 if(a) {
880                                         k3tot-= a;
881                                         k3+= a*key->elemsize;
882                                 }
883                         }
884                         else k3+= start*key->elemsize;
885                 }
886                 if(flagdo & 8) {
887                         if(flagflo & 8) {
888                                 k4tot+= start*k4d;
889                                 a= (int)floor(k4tot);
890                                 if(a) {
891                                         k4tot-= a;
892                                         k4+= a*key->elemsize;
893                                 }
894                         }
895                         else k4+= start*key->elemsize;
896                 }
897
898         }
899
900         /* in case of beztriple */
901         elemstr[0]= 1;                          /* nr of ipofloats */
902         elemstr[1]= IPO_BEZTRIPLE;
903         elemstr[2]= 0;
904
905         /* only here, not above! */
906         elemsize= key->elemsize;
907         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
908
909         for(a=start; a<end; a++) {
910         
911                 cp= key->elemstr;       
912                 if(mode==KEY_BEZTRIPLE) cp= elemstr;
913                 
914                 ofsp= ofs;
915                 
916                 while( cp[0] ) {        /* cp[0]==amount */
917                         
918                         switch(cp[1]) {
919                         case IPO_FLOAT:
920                                 flerp(3, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
921                                 break;
922                         case IPO_BPOINT:
923                                 flerp(4, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
924                                 break;
925                         case IPO_BEZTRIPLE:
926                                 flerp(10, (void *)poin, (void *)k1, (void *)k2, (void *)k3, (void *)k4, t);
927                                 break;
928                         }
929                         
930                         poin+= ofsp[0];                         
931                         cp+= 2;
932                         ofsp++;
933                 }
934                 /* lets do it the difficult way: when keys have a different size */
935                 if(flagdo & 1) {
936                         if(flagflo & 1) {
937                                 k1tot+= k1d;
938                                 while(k1tot>=1.0) {
939                                         k1tot-= 1.0;
940                                         k1+= elemsize;
941                                 }
942                         }
943                         else k1+= elemsize;
944                 }
945                 if(flagdo & 2) {
946                         if(flagflo & 2) {
947                                 k2tot+= k2d;
948                                 while(k2tot>=1.0) {
949                                         k2tot-= 1.0;
950                                         k2+= elemsize;
951                                 }
952                         }
953                         else k2+= elemsize;
954                 }
955                 if(flagdo & 4) {
956                         if(flagflo & 4) {
957                                 k3tot+= k3d;
958                                 while(k3tot>=1.0) {
959                                         k3tot-= 1.0;
960                                         k3+= elemsize;
961                                 }
962                         }
963                         else k3+= elemsize;
964                 }
965                 if(flagdo & 8) {
966                         if(flagflo & 8) {
967                                 k4tot+= k4d;
968                                 while(k4tot>=1.0) {
969                                         k4tot-= 1.0;
970                                         k4+= elemsize;
971                                 }
972                         }
973                         else k4+= elemsize;
974                 }
975                 
976                 if(mode==KEY_BEZTRIPLE) a+= 2;
977         }
978 }
979
980 static float *get_weights_array(Object *ob, char *vgroup)
981 {
982         bDeformGroup *curdef;
983         MDeformVert *dvert= NULL;
984         int totvert= 0, index= 0;
985         
986         /* no vgroup string set? */
987         if(vgroup[0]==0) return NULL;
988         
989         /* gather dvert and totvert */
990         if(ob->type==OB_MESH) {
991                 Mesh *me= ob->data;
992                 dvert= me->dvert;
993                 totvert= me->totvert;
994         }
995         else if(ob->type==OB_LATTICE) {
996                 Lattice *lt= ob->data;
997                 dvert= lt->dvert;
998                 totvert= lt->pntsu*lt->pntsv*lt->pntsw;
999         }
1000         
1001         if(dvert==NULL) return NULL;
1002         
1003         /* find the group (weak loop-in-loop) */
1004         for (curdef = ob->defbase.first; curdef; curdef=curdef->next, index++)
1005                 if (!strcmp(curdef->name, vgroup))
1006                         break;
1007
1008         if(curdef) {
1009                 float *weights;
1010                 int i, j;
1011                 
1012                 weights= MEM_callocN(totvert*sizeof(float), "weights");
1013                 
1014                 for (i=0; i < totvert; i++, dvert++) {
1015                         for(j=0; j<dvert->totweight; j++) {
1016                                 if (dvert->dw[j].def_nr == index) {
1017                                         weights[i]= dvert->dw[j].weight;
1018                                         break;
1019                                 }
1020                         }
1021                 }
1022                 return weights;
1023         }
1024         return NULL;
1025 }
1026
1027 static void do_mesh_key(Scene *scene, Object *ob, Key *key, char *out, int tot)
1028 {
1029         KeyBlock *k[4];
1030         float cfra, ctime, t[4], delta;
1031         int a, flag = 0, step;
1032         
1033         if(key->slurph && key->type!=KEY_RELATIVE ) {
1034                 delta= key->slurph;
1035                 delta/= tot;
1036                 
1037                 step= 1;
1038                 if(tot>100 && slurph_opt) {
1039                         step= tot/50;
1040                         delta*= step;
1041                         /* in do_key and cp_key the case a>tot is handled */
1042                 }
1043                 
1044                 cfra= (float)scene->r.cfra;
1045                 
1046                 for(a=0; a<tot; a+=step, cfra+= delta) {
1047                         
1048                         ctime= bsystem_time(scene, 0, cfra, 0.0); // xxx  ugly cruft!
1049 #if 0 // XXX old animation system
1050                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1051                                 ctime /= 100.0;
1052                                 CLAMP(ctime, 0.0, 1.0);
1053                         }
1054 #endif // XXX old animation system
1055                         // XXX for now... since speed curve cannot be directly ported yet
1056                         ctime /= 100.0f;
1057                         CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1058                 
1059                         flag= setkeys(ctime, &key->block, k, t, 0);
1060
1061                         if(flag==0)
1062                                 do_key(a, a+step, tot, (char *)out, key, k, t, 0);
1063                         else
1064                                 cp_key(a, a+step, tot, (char *)out, key, k[2], NULL, 0);
1065                 }
1066         }
1067         else {
1068                 if(key->type==KEY_RELATIVE) {
1069                         KeyBlock *kb;
1070                         
1071                         for(kb= key->block.first; kb; kb= kb->next)
1072                                 kb->weights= get_weights_array(ob, kb->vgroup);
1073
1074                         do_rel_key(0, tot, tot, (char *)out, key, 0);
1075                         
1076                         for(kb= key->block.first; kb; kb= kb->next) {
1077                                 if(kb->weights) MEM_freeN(kb->weights);
1078                                 kb->weights= NULL;
1079                         }
1080                 }
1081                 else {
1082                         ctime= bsystem_time(scene, ob, (float)scene->r.cfra, 0.0f); // xxx old cruft
1083                         
1084 #if 0 // XXX old animation system
1085                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1086                                 ctime /= 100.0;
1087                                 CLAMP(ctime, 0.0, 1.0);
1088                         }
1089 #endif // XXX old animation system
1090                         // XXX for now... since speed curve cannot be directly ported yet
1091                         ctime /= 100.0f;
1092                         CLAMP(ctime, 0.0f, 1.0f); // XXX for compat, we use this, but this clamping was confusing
1093                         
1094                         flag= setkeys(ctime, &key->block, k, t, 0);
1095
1096                         if(flag==0)
1097                                 do_key(0, tot, tot, (char *)out, key, k, t, 0);
1098                         else
1099                                 cp_key(0, tot, tot, (char *)out, key, k[2], NULL, 0);
1100                 }
1101         }
1102 }
1103
1104 static void do_cu_key(Curve *cu, KeyBlock **k, float *t, char *out, int tot)
1105 {
1106         Nurb *nu;
1107         char *poin;
1108         int a, step;
1109         
1110         for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
1111                 if(nu->bp) {
1112                         step= nu->pntsu*nu->pntsv;
1113                         poin= out - a*sizeof(float)*4;
1114                         do_key(a, a+step, tot, poin, cu->key, k, t, KEY_BPOINT);
1115                 }
1116                 else if(nu->bezt) {
1117                         step= 3*nu->pntsu;
1118                         poin= out - a*sizeof(float)*10;
1119                         do_key(a, a+step, tot, poin, cu->key, k, t, KEY_BEZTRIPLE);
1120                 }
1121                 else
1122                         step= 0;
1123         }
1124 }
1125
1126 static void do_rel_cu_key(Curve *cu, float ctime, char *out, int tot)
1127 {
1128         Nurb *nu;
1129         char *poin;
1130         int a, step;
1131         
1132         for(a=0, nu=cu->nurb.first; nu; nu=nu->next, a+=step) {
1133                 if(nu->bp) {
1134                         step= nu->pntsu*nu->pntsv;
1135                         poin= out - a*sizeof(float)*3;
1136                         do_rel_key(a, a+step, tot, out, cu->key, KEY_BPOINT);
1137                 }
1138                 else if(nu->bezt) {
1139                         step= 3*nu->pntsu;
1140                         poin= out - a*sizeof(float)*10;
1141                         do_rel_key(a, a+step, tot, poin, cu->key, KEY_BEZTRIPLE);
1142                 }
1143                 else
1144                         step= 0;
1145         }
1146 }
1147
1148 static void do_curve_key(Scene *scene, Object *ob, Key *key, char *out, int tot)
1149 {
1150         Curve *cu= ob->data;
1151         KeyBlock *k[4];
1152         float cfra, ctime, t[4], delta;
1153         int a, flag = 0, step = 0;
1154         
1155         if(key->slurph) {
1156                 delta= key->slurph;
1157                 delta/= tot;
1158                 
1159                 step= 1;
1160                 if(tot>100 && slurph_opt) {
1161                         step= tot/50;
1162                         delta*= step;
1163                         /* in do_key and cp_key the case a>tot has been handled */
1164                 }
1165                 
1166                 cfra= (float)scene->r.cfra;
1167                 
1168                 for(a=0; a<tot; a+=step, cfra+= delta) {
1169                         ctime= bsystem_time(scene, 0, cfra, 0.0f); // XXX old cruft
1170 #if 0 // XXX old animation system
1171                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1172                                 ctime /= 100.0;
1173                                 CLAMP(ctime, 0.0, 1.0);
1174                         }
1175 #endif // XXX old animation system
1176                 
1177                         flag= setkeys(ctime, &key->block, k, t, 0);
1178
1179                         if(flag==0)
1180                                 ; /* do_key(a, a+step, tot, (char *)out, key, k, t, 0); */
1181                         else
1182                                 ; /* cp_key(a, a+step, tot, (char *)out, key, k[2],0); */
1183                 }
1184         }
1185         else {
1186                 
1187                 ctime= bsystem_time(scene, NULL, (float)scene->r.cfra, 0.0);
1188                 
1189                 if(key->type==KEY_RELATIVE) {
1190                         do_rel_cu_key(cu, ctime, out, tot);
1191                 }
1192                 else {
1193 #if 0 // XXX old animation system
1194                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1195                                 ctime /= 100.0;
1196                                 CLAMP(ctime, 0.0, 1.0);
1197                         }
1198 #endif // XXX old animation system
1199                         
1200                         flag= setkeys(ctime, &key->block, k, t, 0);
1201                         
1202                         if(flag==0) do_cu_key(cu, k, t, out, tot);
1203                         else cp_cu_key(cu, k[2], 0, tot, out, tot);
1204                 }
1205         }
1206 }
1207
1208 static void do_latt_key(Scene *scene, Object *ob, Key *key, char *out, int tot)
1209 {
1210         Lattice *lt= ob->data;
1211         KeyBlock *k[4];
1212         float delta, cfra, ctime, t[4];
1213         int a, flag;
1214         
1215         if(key->slurph) {
1216                 delta= key->slurph;
1217                 delta/= (float)tot;
1218                 
1219                 cfra= (float)scene->r.cfra;
1220                 
1221                 for(a=0; a<tot; a++, cfra+= delta) {
1222                         
1223                         ctime= bsystem_time(scene, 0, cfra, 0.0); // XXX old cruft
1224 #if 0 // XXX old animation system
1225                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1226                                 ctime /= 100.0;
1227                                 CLAMP(ctime, 0.0, 1.0);
1228                         }
1229 #endif // XXX old animation system
1230                 
1231                         flag= setkeys(ctime, &key->block, k, t, 0);
1232
1233                         if(flag==0)
1234                                 do_key(a, a+1, tot, (char *)out, key, k, t, 0);
1235                         else
1236                                 cp_key(a, a+1, tot, (char *)out, key, k[2], NULL, 0);
1237                 }               
1238         }
1239         else {
1240                 if(key->type==KEY_RELATIVE) {
1241                         KeyBlock *kb;
1242                         
1243                         for(kb= key->block.first; kb; kb= kb->next)
1244                                 kb->weights= get_weights_array(ob, kb->vgroup);
1245                         
1246                         do_rel_key(0, tot, tot, (char *)out, key, 0);
1247                         
1248                         for(kb= key->block.first; kb; kb= kb->next) {
1249                                 if(kb->weights) MEM_freeN(kb->weights);
1250                                 kb->weights= NULL;
1251                         }
1252                 }
1253                 else {
1254                         ctime= bsystem_time(scene, NULL, (float)scene->r.cfra, 0.0);
1255
1256 #if 0 // XXX old animation system
1257                         if(calc_ipo_spec(key->ipo, KEY_SPEED, &ctime)==0) {
1258                                 ctime /= 100.0;
1259                                 CLAMP(ctime, 0.0, 1.0);
1260                         }
1261 #endif // XXX old animation system
1262                         
1263                         flag= setkeys(ctime, &key->block, k, t, 0);
1264
1265                         if(flag==0)
1266                                 do_key(0, tot, tot, (char *)out, key, k, t, 0);
1267                         else
1268                                 cp_key(0, tot, tot, (char *)out, key, k[2], NULL, 0);
1269                 }
1270         }
1271         
1272         if(lt->flag & LT_OUTSIDE) outside_lattice(lt);
1273 }
1274
1275 /* returns key coordinates (+ tilt) when key applied, NULL otherwise */
1276 float *do_ob_key(Scene *scene, Object *ob)
1277 {
1278         Key *key= ob_get_key(ob);
1279         char *out;
1280         int tot= 0, size= 0;
1281         
1282         if(key==NULL || key->block.first==NULL)
1283                 return NULL;
1284
1285         /* compute size of output array */
1286         if(ob->type == OB_MESH) {
1287                 Mesh *me= ob->data;
1288
1289                 tot= me->totvert;
1290                 size= tot*3*sizeof(float);
1291         }
1292         else if(ob->type == OB_LATTICE) {
1293                 Lattice *lt= ob->data;
1294
1295                 tot= lt->pntsu*lt->pntsv*lt->pntsw;
1296                 size= tot*3*sizeof(float);
1297         }
1298         else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1299                 Curve *cu= ob->data;
1300                 Nurb *nu;
1301
1302                 for(nu=cu->nurb.first; nu; nu=nu->next) {
1303                         if(nu->bezt) {
1304                                 tot += 3*nu->pntsu;
1305                                 size += nu->pntsu*10*sizeof(float);
1306                         }
1307                         else if(nu->bp) {
1308                                 tot += nu->pntsu*nu->pntsv;
1309                                 size += nu->pntsu*nu->pntsv*10*sizeof(float);
1310                         }
1311                 }
1312         }
1313
1314         /* if nothing to interpolate, cancel */
1315         if(tot == 0 || size == 0)
1316                 return NULL;
1317         
1318         /* allocate array */
1319         out= MEM_callocN(size, "do_ob_key out");
1320
1321         /* prevent python from screwing this up? anyhoo, the from pointer could be dropped */
1322         key->from= (ID *)ob->data;
1323                 
1324         if(ob->shapeflag & OB_SHAPE_LOCK) {
1325                 /* shape locked, copy the locked shape instead of blending */
1326                 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1327                 
1328                 if(kb && (kb->flag & KEYBLOCK_MUTE))
1329                         kb= key->refkey;
1330
1331                 if(kb==NULL) {
1332                         kb= key->block.first;
1333                         ob->shapenr= 1;
1334                 }
1335                 
1336                 if(ELEM(ob->type, OB_MESH, OB_LATTICE)) {
1337                         float *weights= get_weights_array(ob, kb->vgroup);
1338
1339                         cp_key(0, tot, tot, (char*)out, key, kb, weights, 0);
1340
1341                         if(weights) MEM_freeN(weights);
1342                 }
1343                 else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1344                         cp_cu_key(ob->data, kb, 0, tot, out, tot);
1345         }
1346         else {
1347                 /* do shapekey local drivers */
1348                 float ctime= (float)scene->r.cfra; // XXX this needs to be checked
1349                 
1350                 BKE_animsys_evaluate_animdata(&key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
1351                 
1352                 if(ob->type==OB_MESH) do_mesh_key(scene, ob, key, out, tot);
1353                 else if(ob->type==OB_LATTICE) do_latt_key(scene, ob, key, out, tot);
1354                 else if(ob->type==OB_CURVE) do_curve_key(scene, ob, key, out, tot);
1355                 else if(ob->type==OB_SURF) do_curve_key(scene, ob, key, out, tot);
1356         }
1357         
1358         return (float*)out;
1359 }
1360
1361 Key *ob_get_key(Object *ob)
1362 {
1363         if(ob==NULL) return NULL;
1364         
1365         if(ob->type==OB_MESH) {
1366                 Mesh *me= ob->data;
1367                 return me->key;
1368         }
1369         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1370                 Curve *cu= ob->data;
1371                 return cu->key;
1372         }
1373         else if(ob->type==OB_LATTICE) {
1374                 Lattice *lt= ob->data;
1375                 return lt->key;
1376         }
1377         return NULL;
1378 }
1379
1380 /* only the active keyblock */
1381 KeyBlock *ob_get_keyblock(Object *ob) 
1382 {
1383         Key *key= ob_get_key(ob);
1384         
1385         if (key) {
1386                 KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1387                 return kb;
1388         }
1389
1390         return NULL;
1391 }
1392
1393 /* get the appropriate KeyBlock given an index */
1394 KeyBlock *key_get_keyblock(Key *key, int index)
1395 {
1396         KeyBlock *kb;
1397         int i;
1398         
1399         if (key) {
1400                 kb= key->block.first;
1401                 
1402                 for (i= 1; i < key->totkey; i++) {
1403                         kb= kb->next;
1404                         
1405                         if (index==i)
1406                                 return kb;
1407                 }
1408         }
1409         
1410         return NULL;
1411 }
1412
1413 /* get the appropriate KeyBlock given a name to search for */
1414 KeyBlock *key_get_named_keyblock(Key *key, const char name[])
1415 {
1416         KeyBlock *kb;
1417         
1418         if (key && name) {
1419                 for (kb= key->block.first; kb; kb= kb->next) {
1420                         if (strcmp(name, kb->name)==0)
1421                                 return kb;
1422                 }
1423         }
1424         
1425         return NULL;
1426 }
1427
1428 /* Get RNA-Path for 'value' setting of the given ShapeKey 
1429  * NOTE: the user needs to free the returned string once they're finishe with it
1430  */
1431 char *key_get_curValue_rnaPath(Key *key, KeyBlock *kb)
1432 {
1433         PointerRNA ptr;
1434         PropertyRNA *prop;
1435         
1436         /* sanity checks */
1437         if ELEM(NULL, key, kb)
1438                 return NULL;
1439         
1440         /* create the RNA pointer */
1441         RNA_pointer_create(&key->id, &RNA_ShapeKey, kb, &ptr);
1442         /* get pointer to the property too */
1443         prop= RNA_struct_find_property(&ptr, "value");
1444         
1445         /* return the path */
1446         return RNA_path_from_ID_to_property(&ptr, prop);
1447 }