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