Wednesday sync orange with bf-blender
[blender.git] / source / blender / blenkernel / intern / ipo.c
1 /* ipo.c
2  * 
3  * $Id$
4  *
5  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version. The Blender
11  * Foundation also sells licenses for use in proprietary software under
12  * the Blender License.  See http://www.blender.org/BL/ for information
13  * about this.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  *
24  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
25  * All rights reserved.
26  *
27  * The Original Code is: all of this file.
28  *
29  * Contributor(s): none yet.
30  *
31  * ***** END GPL/BL DUAL LICENSE BLOCK *****
32  */
33
34 #include <math.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include "MEM_guardedalloc.h"
43
44 #include "DNA_action_types.h"
45 #include "DNA_armature_types.h"
46 #include "DNA_curve_types.h"
47 #include "DNA_camera_types.h"
48 #include "DNA_lamp_types.h"
49 #include "DNA_ipo_types.h"
50 #include "DNA_key_types.h"
51 #include "DNA_material_types.h"
52 #include "DNA_mesh_types.h"
53 #include "DNA_object_types.h"
54 #include "DNA_object_force.h"
55 #include "DNA_sequence_types.h"
56 #include "DNA_scene_types.h"
57 #include "DNA_sound_types.h"
58 #include "DNA_texture_types.h"
59 #include "DNA_view3d_types.h"
60
61 #include "BLI_blenlib.h"
62 #include "BLI_arithb.h"
63
64 #include "BKE_bad_level_calls.h"
65 #include "BKE_utildefines.h"
66
67 #include "BKE_action.h"
68 #include "BKE_blender.h"
69 #include "BKE_curve.h"
70 #include "BKE_constraint.h"
71 #include "BKE_global.h"
72 #include "BKE_ipo.h"
73 #include "BKE_library.h"
74 #include "BKE_main.h"
75 #include "BKE_mesh.h"
76 #include "BKE_object.h"
77
78 #define SMALL -1.0e-10
79
80 /* This array concept was meant to make sure that defines such as OB_LOC_X
81    don't have to be enumerated, also for backward compatibility, future changes,
82    and to enable it all can be accessed with a for-next loop.
83 */
84
85 int co_ar[CO_TOTIPO]= {
86         CO_ENFORCE
87 };
88
89 int ob_ar[OB_TOTIPO]= {
90         OB_LOC_X, OB_LOC_Y, OB_LOC_Z, OB_DLOC_X, OB_DLOC_Y, OB_DLOC_Z, 
91         OB_ROT_X, OB_ROT_Y, OB_ROT_Z, OB_DROT_X, OB_DROT_Y, OB_DROT_Z, 
92         OB_SIZE_X, OB_SIZE_Y, OB_SIZE_Z, OB_DSIZE_X, OB_DSIZE_Y, OB_DSIZE_Z, 
93         OB_LAY, OB_TIME, OB_COL_R, OB_COL_G, OB_COL_B, OB_COL_A,
94         OB_PD_FSTR, OB_PD_FFALL, OB_PD_SDAMP, OB_PD_RDAMP, OB_PD_PERM
95 };
96
97 int ac_ar[AC_TOTIPO]= {
98         AC_LOC_X, AC_LOC_Y, AC_LOC_Z,  
99          AC_QUAT_W, AC_QUAT_X, AC_QUAT_Y, AC_QUAT_Z,
100         AC_SIZE_X, AC_SIZE_Y, AC_SIZE_Z
101 };
102
103 int ma_ar[MA_TOTIPO]= {
104         MA_COL_R, MA_COL_G, MA_COL_B, 
105         MA_SPEC_R, MA_SPEC_G, MA_SPEC_B, 
106         MA_MIR_R, MA_MIR_G, MA_MIR_B,
107         MA_REF, MA_ALPHA, MA_EMIT, MA_AMB, 
108         MA_SPEC, MA_HARD, MA_SPTR, MA_IOR, 
109         MA_MODE, MA_HASIZE, MA_TRANSLU, MA_RAYM,
110         MA_FRESMIR, MA_FRESMIRI, MA_FRESTRA, MA_FRESTRAI, MA_ADD,
111         
112         MA_MAP1+MAP_OFS_X, MA_MAP1+MAP_OFS_Y, MA_MAP1+MAP_OFS_Z, 
113         MA_MAP1+MAP_SIZE_X, MA_MAP1+MAP_SIZE_Y, MA_MAP1+MAP_SIZE_Z, 
114         MA_MAP1+MAP_R, MA_MAP1+MAP_G, MA_MAP1+MAP_B,
115         MA_MAP1+MAP_DVAR, MA_MAP1+MAP_COLF, MA_MAP1+MAP_NORF, MA_MAP1+MAP_VARF, MA_MAP1+MAP_DISP
116 };
117
118 int te_ar[TE_TOTIPO] ={
119         
120         TE_NSIZE, TE_NDEPTH, TE_NTYPE, TE_TURB,
121         
122         TE_VNW1, TE_VNW2, TE_VNW3, TE_VNW4,
123         TE_VNMEXP, TE_VN_COLT, TE_VN_DISTM,
124         
125         TE_ISCA, TE_DISTA,
126         
127         TE_MG_TYP, TE_MGH, TE_MG_LAC, TE_MG_OCT, TE_MG_OFF, TE_MG_GAIN,
128         
129         TE_N_BAS1, TE_N_BAS2
130 };
131
132 int seq_ar[SEQ_TOTIPO]= {
133         SEQ_FAC1
134 };
135
136 int cu_ar[CU_TOTIPO]= {
137         CU_SPEED
138 };
139
140 int wo_ar[WO_TOTIPO]= {
141         WO_HOR_R, WO_HOR_G, WO_HOR_B, WO_ZEN_R, WO_ZEN_G, WO_ZEN_B, 
142         WO_EXPOS, WO_MISI, WO_MISTDI, WO_MISTSTA, WO_MISTHI,
143         WO_STAR_R, WO_STAR_G, WO_STAR_B, WO_STARDIST, WO_STARSIZE, 
144
145         MA_MAP1+MAP_OFS_X, MA_MAP1+MAP_OFS_Y, MA_MAP1+MAP_OFS_Z, 
146         MA_MAP1+MAP_SIZE_X, MA_MAP1+MAP_SIZE_Y, MA_MAP1+MAP_SIZE_Z, 
147         MA_MAP1+MAP_R, MA_MAP1+MAP_G, MA_MAP1+MAP_B,
148         MA_MAP1+MAP_DVAR, MA_MAP1+MAP_COLF, MA_MAP1+MAP_NORF, MA_MAP1+MAP_VARF
149 };
150
151 int la_ar[LA_TOTIPO]= {
152         LA_ENERGY,  LA_COL_R, LA_COL_G,  LA_COL_B, 
153         LA_DIST, LA_SPOTSI, LA_SPOTBL, 
154         LA_QUAD1,  LA_QUAD2,  LA_HALOINT,  
155
156         MA_MAP1+MAP_OFS_X, MA_MAP1+MAP_OFS_Y, MA_MAP1+MAP_OFS_Z, 
157         MA_MAP1+MAP_SIZE_X, MA_MAP1+MAP_SIZE_Y, MA_MAP1+MAP_SIZE_Z, 
158         MA_MAP1+MAP_R, MA_MAP1+MAP_G, MA_MAP1+MAP_B,
159         MA_MAP1+MAP_DVAR, MA_MAP1+MAP_COLF
160 };
161
162 /* yafray: aperture & focal distance curves added */
163 int cam_ar[CAM_TOTIPO]= {
164         CAM_LENS, CAM_STA, CAM_END, CAM_YF_APERT, CAM_YF_FDIST
165 };
166
167 int snd_ar[SND_TOTIPO]= {
168         SND_VOLUME, SND_PITCH, SND_PANNING, SND_ATTEN
169 };
170
171
172
173 float frame_to_float(int cfra)          /* see also bsystem_time in object.c */
174 {
175         extern float bluroffs;  /* object.c */
176         float ctime;
177         
178         ctime= (float)cfra;
179         if(R.flag & R_SEC_FIELD) {
180                 if((R.r.mode & R_FIELDSTILL)==0) ctime+= 0.5;
181         }
182         ctime+= bluroffs;
183         ctime*= G.scene->r.framelen;
184         
185         return ctime;
186 }
187
188 /* includes ipo curve itself */
189 void free_ipo_curve(IpoCurve *icu) 
190 {
191         if(icu->bezt) MEM_freeN(icu->bezt);
192         if(icu->bp) MEM_freeN(icu->bp);
193         if(icu->driver) MEM_freeN(icu->driver);
194         MEM_freeN(icu);
195 }
196
197 /* do not free ipo itself */
198 void free_ipo(Ipo *ipo)
199 {
200         IpoCurve *icu;
201         
202         while( (icu= ipo->curve.first) ) {
203                 BLI_remlink(&ipo->curve, icu);
204                 free_ipo_curve(icu);
205         }
206 }
207
208 /* on adding new ipos, or for empty views */
209 void ipo_default_v2d_cur(int blocktype, rctf *cur)
210 {
211         if(blocktype==ID_CA) {
212                 cur->xmin= G.scene->r.sfra;
213                 cur->xmax= G.scene->r.efra;
214                 cur->ymin= 0.0;
215                 cur->ymax= 100.0;
216         }
217         else if ELEM5(blocktype, ID_MA, ID_CU, ID_WO, ID_LA, ID_CO) {
218                 cur->xmin= (float)G.scene->r.sfra-0.1;
219                 cur->xmax= G.scene->r.efra;
220                 cur->ymin= (float)-0.1;
221                 cur->ymax= (float)+1.1;
222         }
223         else if(blocktype==ID_TE) {
224                 cur->xmin= (float)G.scene->r.sfra-0.1;
225                 cur->xmax= G.scene->r.efra;
226                 cur->ymin= (float)-0.1;
227                 cur->ymax= (float)+1.1;
228         }
229         else if(blocktype==ID_SEQ) {
230                 cur->xmin= -5.0+G.scene->r.sfra;
231                 cur->xmax= 105.0;
232                 cur->ymin= (float)-0.1;
233                 cur->ymax= (float)+1.1;
234         }
235         else if(blocktype==ID_KE) {
236                 cur->xmin= (float)G.scene->r.sfra-0.1;
237                 cur->xmax= G.scene->r.efra;
238                 cur->ymin= (float)-0.1;
239                 cur->ymax= (float)+2.1;
240         }
241         else {  /* ID_OB and everything else */
242                 cur->xmin= G.scene->r.sfra;
243                 cur->xmax= G.scene->r.efra;
244                 cur->ymin= -5.0;
245                 cur->ymax= +5.0;
246         }
247 }
248
249
250 Ipo *add_ipo(char *name, int idcode)
251 {
252         Ipo *ipo;
253         
254         ipo= alloc_libblock(&G.main->ipo, ID_IP, name);
255         ipo->blocktype= idcode;
256         ipo_default_v2d_cur(idcode, &ipo->cur);
257
258         return ipo;
259 }
260
261 Ipo *copy_ipo(Ipo *ipo)
262 {
263         Ipo *ipon;
264         IpoCurve *icu;
265         
266         if(ipo==NULL) return 0;
267         
268         ipon= copy_libblock(ipo);
269         
270         duplicatelist(&(ipon->curve), &(ipo->curve));
271
272         for(icu= ipo->curve.first; icu; icu= icu->next) {
273                 icu->bezt= MEM_dupallocN(icu->bezt);
274                 if(icu->driver) icu->driver= MEM_dupallocN(icu->driver);
275         }
276         
277         return ipon;
278 }
279
280 void make_local_obipo(Ipo *ipo)
281 {
282         Object *ob;
283         Ipo *ipon;
284         int local=0, lib=0;
285         
286         /* - only lib users: do nothing
287          * - only local users: set flag
288          * - mixed: make copy
289          */
290
291         ob= G.main->object.first;
292         while(ob) {
293                 if(ob->ipo==ipo) {
294                         if(ob->id.lib) lib= 1;
295                         else local= 1;
296                 }
297                 ob= ob->id.next;
298         }
299         
300         if(local && lib==0) {
301                 ipo->id.lib= 0;
302                 ipo->id.flag= LIB_LOCAL;
303                 new_id(0, (ID *)ipo, 0);
304         }
305         else if(local && lib) {
306                 ipon= copy_ipo(ipo);
307                 ipon->id.us= 0;
308                 
309                 ob= G.main->object.first;
310                 while(ob) {
311                         if(ob->ipo==ipo) {
312                                 
313                                 if(ob->id.lib==NULL) {
314                                         ob->ipo= ipon;
315                                         ipon->id.us++;
316                                         ipo->id.us--;
317                                 }
318                         }
319                         ob= ob->id.next;
320                 }
321         }
322 }
323
324 void make_local_matipo(Ipo *ipo)
325 {
326         Material *ma;
327         Ipo *ipon;
328         int local=0, lib=0;
329
330         /* - only lib users: do nothing
331             * - only local users: set flag
332             * - mixed: make copy
333         */
334         
335         ma= G.main->mat.first;
336         while(ma) {
337                 if(ma->ipo==ipo) {
338                         if(ma->id.lib) lib= 1;
339                         else local= 1;
340                 }
341                 ma= ma->id.next;
342         }
343         
344         if(local && lib==0) {
345                 ipo->id.lib= 0;
346                 ipo->id.flag= LIB_LOCAL;
347                 new_id(0, (ID *)ipo, 0);
348         }
349         else if(local && lib) {
350                 ipon= copy_ipo(ipo);
351                 ipon->id.us= 0;
352                 
353                 ma= G.main->mat.first;
354                 while(ma) {
355                         if(ma->ipo==ipo) {
356                                 
357                                 if(ma->id.lib==NULL) {
358                                         ma->ipo= ipon;
359                                         ipon->id.us++;
360                                         ipo->id.us--;
361                                 }
362                         }
363                         ma= ma->id.next;
364                 }
365         }
366 }
367
368 void make_local_keyipo(Ipo *ipo)
369 {
370         Key *key;
371         Ipo *ipon;
372         int local=0, lib=0;
373
374         /* - only lib users: do nothing
375          * - only local users: set flag
376          * - mixed: make copy
377          */
378         
379         key= G.main->key.first;
380         while(key) {
381                 if(key->ipo==ipo) {
382                         if(key->id.lib) lib= 1;
383                         else local= 1;
384                 }
385                 key= key->id.next;
386         }
387         
388         if(local && lib==0) {
389                 ipo->id.lib= 0;
390                 ipo->id.flag= LIB_LOCAL;
391                 new_id(0, (ID *)ipo, 0);
392         }
393         else if(local && lib) {
394                 ipon= copy_ipo(ipo);
395                 ipon->id.us= 0;
396                 
397                 key= G.main->key.first;
398                 while(key) {
399                         if(key->ipo==ipo) {
400                                 
401                                 if(key->id.lib==NULL) {
402                                         key->ipo= ipon;
403                                         ipon->id.us++;
404                                         ipo->id.us--;
405                                 }
406                         }
407                         key= key->id.next;
408                 }
409         }
410 }
411
412
413 void make_local_ipo(Ipo *ipo)
414 {
415         
416         if(ipo->id.lib==NULL) return;
417         if(ipo->id.us==1) {
418                 ipo->id.lib= 0;
419                 ipo->id.flag= LIB_LOCAL;
420                 new_id(0, (ID *)ipo, 0);
421                 return;
422         }
423         
424         if(ipo->blocktype==ID_OB) make_local_obipo(ipo);
425         else if(ipo->blocktype==ID_MA) make_local_matipo(ipo);
426         else if(ipo->blocktype==ID_KE) make_local_keyipo(ipo);
427
428 }
429
430 IpoCurve *find_ipocurve(Ipo *ipo, int adrcode)
431 {
432         if(ipo) {
433                 IpoCurve *icu;
434                 for(icu= ipo->curve.first; icu; icu= icu->next) {
435                         if(icu->adrcode==adrcode) return icu;
436                 }
437         }
438         return NULL;
439 }
440
441 void calchandles_ipocurve(IpoCurve *icu)
442 {
443         BezTriple *bezt, *prev, *next;
444         int a;
445
446         a= icu->totvert;
447         if(a<2) return;
448         
449         bezt= icu->bezt;
450         prev= 0;
451         next= bezt+1;
452
453         while(a--) {
454
455                 if(bezt->vec[0][0]>bezt->vec[1][0]) bezt->vec[0][0]= bezt->vec[1][0];
456                 if(bezt->vec[2][0]<bezt->vec[1][0]) bezt->vec[2][0]= bezt->vec[1][0];
457
458                 if(icu->flag & IPO_AUTO_HORIZ) 
459                         calchandleNurb(bezt, prev, next, 2);    /* 2==special autohandle && keep extrema horizontal */
460                 else
461                         calchandleNurb(bezt, prev, next, 1);    /* 1==special autohandle */
462         
463                 prev= bezt;
464                 if(a==1) {
465                         next= 0;
466                 }
467                 else next++;
468                         
469                 /* for automatic ease in and out */
470                 if(bezt->h1==HD_AUTO && bezt->h2==HD_AUTO) {
471                         if(a==0 || a==icu->totvert-1) {
472                                 if(icu->extrap==IPO_HORIZ) {
473                                         bezt->vec[0][1]= bezt->vec[2][1]= bezt->vec[1][1];
474                                 }
475                         }
476                 }
477                 
478                 bezt++;
479         }
480 }
481
482 void testhandles_ipocurve(IpoCurve *icu)
483 {
484     /* use when something has changed with handles.
485     it treats all BezTriples with the following rules:
486     PHASE 1: do types have to be altered?
487      Auto handles: become aligned when selection status is NOT(000 || 111)
488      Vector handles: become 'nothing' when (one half selected AND other not)
489     PHASE 2: recalculate handles
490     */
491     BezTriple *bezt;
492         int flag, a;
493
494         bezt= icu->bezt;
495         if(bezt==NULL) return;
496         
497         a= icu->totvert;
498         while(a--) {
499                 flag= 0;
500                 if(bezt->f1 & 1) flag++;
501                 if(bezt->f2 & 1) flag += 2;
502                 if(bezt->f3 & 1) flag += 4;
503
504                 if( !(flag==0 || flag==7) ) {
505                         if(bezt->h1==HD_AUTO) {   /* auto */
506                                 bezt->h1= HD_ALIGN;
507                         }
508                         if(bezt->h2==HD_AUTO) {   /* auto */
509                                 bezt->h2= HD_ALIGN;
510                         }
511
512                         if(bezt->h1==HD_VECT) {   /* vector */
513                                 if(flag < 4) bezt->h1= 0;
514                         }
515                         if(bezt->h2==HD_VECT) {   /* vector */
516                                 if( flag > 3) bezt->h2= 0;
517                         }
518                 }
519                 bezt++;
520         }
521
522         calchandles_ipocurve(icu);
523 }
524
525
526 void sort_time_ipocurve(IpoCurve *icu)
527 {
528         BezTriple *bezt;
529         int a, ok= 1;
530         
531         while(ok) {
532                 ok= 0;
533
534                 if(icu->bezt) {
535                         bezt= icu->bezt;
536                         a= icu->totvert;
537                         while(a--) {
538                                 if(a>0) {
539                                         if( bezt->vec[1][0] > (bezt+1)->vec[1][0]) {
540                                                 SWAP(BezTriple, *bezt, *(bezt+1));
541                                                 ok= 1;
542                                         }
543                                 }
544                                 if(bezt->vec[0][0]>=bezt->vec[1][0] && bezt->vec[2][0]<=bezt->vec[1][0]) {
545                                         SWAP(float, bezt->vec[0][0], bezt->vec[2][0]);
546                                         SWAP(float, bezt->vec[0][1], bezt->vec[2][1]);
547                                 }
548                                 else {
549                                         if(bezt->vec[0][0]>bezt->vec[1][0]) bezt->vec[0][0]= bezt->vec[1][0];
550                                         if(bezt->vec[2][0]<bezt->vec[1][0]) bezt->vec[2][0]= bezt->vec[1][0];
551                                 }
552                                 bezt++;
553                         }
554                 }
555                 else {
556                         
557                 }
558         }
559 }
560
561 int test_time_ipocurve(IpoCurve *icu)
562 {
563         BezTriple *bezt;
564         int a;
565         
566         if(icu->bezt) {
567                 bezt= icu->bezt;
568                 a= icu->totvert-1;
569                 while(a--) {
570                         if( bezt->vec[1][0] > (bezt+1)->vec[1][0]) {
571                                 return 1;
572                         }
573                         bezt++;
574                 }       
575         }
576         else {
577                 
578         }
579
580         return 0;
581 }
582
583 void correct_bezpart(float *v1, float *v2, float *v3, float *v4)
584 {
585         /* the total length of the handles is not allowed to be more
586          * than the horizontal distance between (v1-v4)
587          * this to prevent curve loops
588          */
589         float h1[2], h2[2], len1, len2, len, fac;
590         
591         h1[0]= v1[0]-v2[0];
592         h1[1]= v1[1]-v2[1];
593         h2[0]= v4[0]-v3[0];
594         h2[1]= v4[1]-v3[1];
595         
596         len= v4[0]- v1[0];
597         len1= (float)fabs(h1[0]);
598         len2= (float)fabs(h2[0]);
599         
600         if(len1+len2==0.0) return;
601         if(len1+len2 > len) {
602                 fac= len/(len1+len2);
603                 
604                 v2[0]= (v1[0]-fac*h1[0]);
605                 v2[1]= (v1[1]-fac*h1[1]);
606                 
607                 v3[0]= (v4[0]-fac*h2[0]);
608                 v3[1]= (v4[1]-fac*h2[1]);
609                 
610         }
611 }
612
613 /* *********************** ARITH *********************** */
614
615 int findzero(float x, float q0, float q1, float q2, float q3, float *o)
616 {
617         double c0, c1, c2, c3, a, b, c, p, q, d, t, phi;
618         int nr= 0;
619
620         c0= q0-x;
621         c1= 3*(q1-q0);
622         c2= 3*(q0-2*q1+q2);
623         c3= q3-q0+3*(q1-q2);
624         
625         if(c3!=0.0) {
626                 a= c2/c3;
627                 b= c1/c3;
628                 c= c0/c3;
629                 a= a/3;
630
631                 p= b/3-a*a;
632                 q= (2*a*a*a-a*b+c)/2;
633                 d= q*q+p*p*p;
634
635                 if(d>0.0) {
636                         t= sqrt(d);
637                         o[0]= (float)(Sqrt3d(-q+t)+Sqrt3d(-q-t)-a);
638                         if(o[0]>= SMALL && o[0]<=1.000001) return 1;
639                         else return 0;
640                 }
641                 else if(d==0.0) {
642                         t= Sqrt3d(-q);
643                         o[0]= (float)(2*t-a);
644                         if(o[0]>=SMALL && o[0]<=1.000001) nr++;
645                         o[nr]= (float)(-t-a);
646                         if(o[nr]>=SMALL && o[nr]<=1.000001) return nr+1;
647                         else return nr;
648                 }
649                 else {
650                         phi= acos(-q/sqrt(-(p*p*p)));
651                         t= sqrt(-p);
652                         p= cos(phi/3);
653                         q= sqrt(3-3*p*p);
654                         o[0]= (float)(2*t*p-a);
655                         if(o[0]>=SMALL && o[0]<=1.000001) nr++;
656                         o[nr]= (float)(-t*(p+q)-a);
657                         if(o[nr]>=SMALL && o[nr]<=1.000001) nr++;
658                         o[nr]= (float)(-t*(p-q)-a);
659                         if(o[nr]>=SMALL && o[nr]<=1.000001) return nr+1;
660                         else return nr;
661                 }
662         }
663         else {
664                 a=c2;
665                 b=c1;
666                 c=c0;
667                 
668                 if(a!=0.0) {
669                         p=b*b-4*a*c;
670                         if(p>0) {
671                                 p= sqrt(p);
672                                 o[0]= (float)((-b-p)/(2*a));
673                                 if(o[0]>=SMALL && o[0]<=1.000001) nr++;
674                                 o[nr]= (float)((-b+p)/(2*a));
675                                 if(o[nr]>=SMALL && o[nr]<=1.000001) return nr+1;
676                                 else return nr;
677                         }
678                         else if(p==0) {
679                                 o[0]= (float)(-b/(2*a));
680                                 if(o[0]>=SMALL && o[0]<=1.000001) return 1;
681                                 else return 0;
682                         }
683                 }
684                 else if(b!=0.0) {
685                         o[0]= (float)(-c/b);
686                         if(o[0]>=SMALL && o[0]<=1.000001) return 1;
687                         else return 0;
688                 }
689                 else if(c==0.0) {
690                         o[0]= 0.0;
691                         return 1;
692                 }
693                 return 0;       
694         }
695 }
696
697 void berekeny(float f1, float f2, float f3, float f4, float *o, int b)
698 {
699         float t, c0, c1, c2, c3;
700         int a;
701
702         c0= f1;
703         c1= 3.0f*(f2 - f1);
704         c2= 3.0f*(f1 - 2.0f*f2 + f3);
705         c3= f4 - f1 + 3.0f*(f2-f3);
706         
707         for(a=0; a<b; a++) {
708                 t= o[a];
709                 o[a]= c0+t*c1+t*t*c2+t*t*t*c3;
710         }
711 }
712
713 void berekenx(float *f, float *o, int b)
714 {
715         float t, c0, c1, c2, c3;
716         int a;
717
718         c0= f[0];
719         c1= 3*(f[3]-f[0]);
720         c2= 3*(f[0]-2*f[3]+f[6]);
721         c3= f[9]-f[0]+3*(f[3]-f[6]);
722         for(a=0; a<b; a++) {
723                 t= o[a];
724                 o[a]= c0+t*c1+t*t*c2+t*t*t*c3;
725         }
726 }
727
728 /* has to return a float value */
729 static float eval_driver(IpoDriver *driver)
730 {
731         
732         if(driver->flag & IPO_DRIVER_PYTHON) {
733                 printf("Execute %s\n", driver->name);
734         }
735         else {
736                 Object *ob= driver->ob;
737                 
738                 if(ob==NULL) return 0.0f;
739                 
740                 if(driver->blocktype==ID_OB) {
741                         switch(driver->adrcode) {
742                         case OB_LOC_X:
743                                 return ob->loc[0];
744                         case OB_LOC_Y:
745                                 return ob->loc[1];
746                         case OB_LOC_Z:
747                                 return ob->loc[2];
748                         case OB_ROT_X:
749                                 return ob->rot[0]/(M_PI_2/9.0);
750                         case OB_ROT_Y:
751                                 return ob->rot[1]/(M_PI_2/9.0);
752                         case OB_ROT_Z:
753                                 return ob->rot[2]/(M_PI_2/9.0);
754                         case OB_SIZE_X:
755                                 return ob->size[0];
756                         case OB_SIZE_Y:
757                                 return ob->size[1];
758                         case OB_SIZE_Z:
759                                 return ob->size[2];
760                         }
761                 }
762                 else {  /* ID_AR */
763                         bPoseChannel *pchan= get_pose_channel(ob->pose, driver->name);
764                         if(pchan && pchan->bone) {
765                                 float pose_mat[3][3];
766                                 float diff_mat[3][3], par_mat[3][3], ipar_mat[3][3];
767                                 float eul[3], size[3];
768                                 
769                                 /* we need the local transform = current transform - (parent transform + bone transform) */
770                                 
771                                 Mat3CpyMat4(pose_mat, pchan->pose_mat);
772                                 
773                                 if (pchan->parent) {
774                                         Mat3CpyMat4(par_mat, pchan->parent->pose_mat);
775                                         Mat3MulMat3(diff_mat, par_mat, pchan->bone->bone_mat);
776                                         
777                                         Mat3Inv(ipar_mat, diff_mat);
778                                 }
779                                 else {
780                                         Mat3Inv(ipar_mat, pchan->bone->bone_mat);
781                                 }
782                                 
783                                 Mat3MulMat3(diff_mat, ipar_mat, pose_mat);
784                                 
785                                 Mat3ToEul(diff_mat, eul);
786                                 Mat3ToSize(diff_mat, size);
787
788                                 switch(driver->adrcode) {
789                                 case OB_LOC_X:
790                                         return pchan->loc[0];
791                                 case OB_LOC_Y:
792                                         return pchan->loc[1];
793                                 case OB_LOC_Z:
794                                         return pchan->loc[2];
795                                 case OB_ROT_X:
796                                         return eul[0]/(M_PI_2/9.0);
797                                 case OB_ROT_Y:
798                                         return eul[1]/(M_PI_2/9.0);
799                                 case OB_ROT_Z:
800                                         return eul[2]/(M_PI_2/9.0);
801                                 case OB_SIZE_X:
802                                         return size[0];
803                                 case OB_SIZE_Y:
804                                         return size[1];
805                                 case OB_SIZE_Z:
806                                         return size[2];
807                                 }
808                         }
809                 }
810         }       
811         return 0.0f;
812 }
813
814 float eval_icu(IpoCurve *icu, float ipotime) 
815 {
816         BezTriple *bezt, *prevbezt;
817         float v1[2], v2[2], v3[2], v4[2], opl[32], dx, fac;
818         float cycdx, cycdy, ofs, cycyofs, cvalue = 0.0;
819         int a, b;
820         
821         cycyofs= 0.0;
822         
823         if(icu->driver) {
824                 /* ipotime now serves as input for the curve */
825                 ipotime= cvalue= eval_driver(icu->driver);
826         }
827         if(icu->bezt) {
828                 prevbezt= icu->bezt;
829                 bezt= prevbezt+1;
830                 a= icu->totvert-1;
831                 
832                 /* cyclic? */
833                 if(icu->extrap & IPO_CYCL) {
834                         ofs= icu->bezt->vec[1][0];
835                         cycdx= (icu->bezt+icu->totvert-1)->vec[1][0] - ofs;
836                         cycdy= (icu->bezt+icu->totvert-1)->vec[1][1] - icu->bezt->vec[1][1];
837                         if(cycdx!=0.0) {
838                                 
839                                 if(icu->extrap & IPO_DIR) {
840                                         cycyofs= (float)floor((ipotime-ofs)/cycdx);
841                                         cycyofs*= cycdy;
842                                 }
843
844                                 ipotime= (float)(fmod(ipotime-ofs, cycdx)+ofs);
845                                 if(ipotime<ofs) ipotime+= cycdx;
846                         }
847                 }
848                 
849                 /* endpoints? */
850         
851                 if(prevbezt->vec[1][0]>=ipotime) {
852                         if( (icu->extrap & IPO_DIR) && icu->ipo!=IPO_CONST) {
853                                 dx= prevbezt->vec[1][0]-ipotime;
854                                 fac= prevbezt->vec[1][0]-prevbezt->vec[0][0];
855                                 if(fac!=0.0) {
856                                         fac= (prevbezt->vec[1][1]-prevbezt->vec[0][1])/fac;
857                                         cvalue= prevbezt->vec[1][1]-fac*dx;
858                                 }
859                                 else cvalue= prevbezt->vec[1][1];
860                         }
861                         else cvalue= prevbezt->vec[1][1];
862                         
863                         cvalue+= cycyofs;
864                 }
865                 else if( (prevbezt+a)->vec[1][0]<=ipotime) {
866                         if( (icu->extrap & IPO_DIR) && icu->ipo!=IPO_CONST) {
867                                 prevbezt+= a;
868                                 dx= ipotime-prevbezt->vec[1][0];
869                                 fac= prevbezt->vec[2][0]-prevbezt->vec[1][0];
870
871                                 if(fac!=0) {
872                                         fac= (prevbezt->vec[2][1]-prevbezt->vec[1][1])/fac;
873                                         cvalue= prevbezt->vec[1][1]+fac*dx;
874                                 }
875                                 else cvalue= prevbezt->vec[1][1];
876                         }
877                         else cvalue= (prevbezt+a)->vec[1][1];
878                         
879                         cvalue+= cycyofs;
880                 }
881                 else {
882                         while(a--) {
883                                 if(prevbezt->vec[1][0]<=ipotime && bezt->vec[1][0]>=ipotime) {
884                                         if(icu->ipo==IPO_CONST) {
885                                                 cvalue= prevbezt->vec[1][1]+cycyofs;
886                                         }
887                                         else if(icu->ipo==IPO_LIN) {
888                                                 fac= bezt->vec[1][0]-prevbezt->vec[1][0];
889                                                 if(fac==0) cvalue= cycyofs+prevbezt->vec[1][1];
890                                                 else {
891                                                         fac= (ipotime-prevbezt->vec[1][0])/fac;
892                                                         cvalue= cycyofs+prevbezt->vec[1][1]+ fac*(bezt->vec[1][1]-prevbezt->vec[1][1]);
893                                                 }
894                                         }
895                                         else {
896                                                 v1[0]= prevbezt->vec[1][0];
897                                                 v1[1]= prevbezt->vec[1][1];
898                                                 v2[0]= prevbezt->vec[2][0];
899                                                 v2[1]= prevbezt->vec[2][1];
900                                                 
901                                                 v3[0]= bezt->vec[0][0];
902                                                 v3[1]= bezt->vec[0][1];
903                                                 v4[0]= bezt->vec[1][0];
904                                                 v4[1]= bezt->vec[1][1];
905
906                                                 correct_bezpart(v1, v2, v3, v4);
907                                                 
908                                                 b= findzero(ipotime, v1[0], v2[0], v3[0], v4[0], opl);
909                                                 if(b) {
910                                                         berekeny(v1[1], v2[1], v3[1], v4[1], opl, 1);
911                                                         cvalue= opl[0]+cycyofs;
912                                                         break;
913                                                 }
914                                         }
915                                 }
916                                 prevbezt= bezt;
917                                 bezt++;
918                         }
919                 }
920         }
921
922         if(icu->ymin < icu->ymax) {
923                 if(cvalue < icu->ymin) cvalue= icu->ymin;
924                 else if(cvalue > icu->ymax) cvalue= icu->ymax;
925         }
926         
927         return cvalue;
928 }
929
930 void calc_icu(IpoCurve *icu, float ctime)
931 {
932         icu->curval= eval_icu(icu, ctime);
933 }
934
935 float calc_ipo_time(Ipo *ipo, float ctime)
936 {
937
938         if(ipo && ipo->blocktype==ID_OB) {
939                 IpoCurve *icu= ipo->curve.first;
940
941                 while(icu) {
942                         if (icu->adrcode==OB_TIME) {
943                                 calc_icu(icu, ctime);
944                                 return 10.0f*icu->curval;
945                         }
946                         icu= icu->next;
947                 }       
948         }
949         
950         return ctime;
951 }
952
953 void calc_ipo(Ipo *ipo, float ctime)
954 {
955         IpoCurve *icu;
956         
957         if(ipo==NULL) return;
958         
959         for(icu= ipo->curve.first; icu; icu= icu->next) {
960                 if(icu->driver || (icu->flag & IPO_LOCK)==0) 
961                         calc_icu(icu, ctime);
962         }
963 }
964
965 /* ************************************** */
966 /*              DO THE IPO!                                               */
967 /* ************************************** */
968
969 void write_ipo_poin(void *poin, int type, float val)
970 {
971
972         switch(type) {
973         case IPO_FLOAT:
974                 *( (float *)poin)= val;
975                 break;
976         case IPO_FLOAT_DEGR:
977                 *( (float *)poin)= (float)(val*M_PI_2/9.0);
978                 break;
979         case IPO_INT:
980         case IPO_INT_BIT:
981         case IPO_LONG:
982                 *( (int *)poin)= (int)val;
983                 break;
984         case IPO_SHORT:
985         case IPO_SHORT_BIT:
986                 *( (short *)poin)= (short)val;
987                 break;
988         case IPO_CHAR:
989         case IPO_CHAR_BIT:
990                 *( (char *)poin)= (char)val;
991                 break;
992         }
993 }
994
995 float read_ipo_poin(void *poin, int type)
996 {
997         float val = 0.0;
998         
999         switch(type) {
1000         case IPO_FLOAT:
1001                 val= *( (float *)poin);
1002                 break;
1003         case IPO_FLOAT_DEGR:
1004                 val= *( (float *)poin);
1005                 val = (float)(val/(M_PI_2/9.0));
1006                 break;
1007         case IPO_INT:
1008         case IPO_INT_BIT:
1009         case IPO_LONG:
1010                 val= (float)(*( (int *)poin));
1011                 break;
1012         case IPO_SHORT:
1013         case IPO_SHORT_BIT:
1014                 val= *( (short *)poin);
1015                 break;
1016         case IPO_CHAR:
1017         case IPO_CHAR_BIT:
1018                 val= *( (char *)poin);
1019                 break;
1020         }
1021         return val;
1022 }
1023
1024 static void *give_tex_poin(Tex *tex, int adrcode, int *type )
1025 {
1026         void *poin=0;
1027
1028         switch(adrcode) {
1029         case TE_NSIZE:
1030                 poin= &(tex->noisesize); break;
1031         case TE_TURB:
1032                 poin= &(tex->turbul); break;
1033         case TE_NDEPTH:
1034                 poin= &(tex->noisedepth); *type= IPO_SHORT; break;
1035         case TE_NTYPE:
1036                 poin= &(tex->noisetype); *type= IPO_SHORT; break;
1037         case TE_VNW1:
1038                 poin= &(tex->vn_w1); break;
1039         case TE_VNW2:
1040                 poin= &(tex->vn_w2); break;
1041         case TE_VNW3:
1042                 poin= &(tex->vn_w3); break;
1043         case TE_VNW4:
1044                 poin= &(tex->vn_w4); break;
1045         case TE_VNMEXP:
1046                 poin= &(tex->vn_mexp); break;
1047         case TE_ISCA:
1048                 poin= &(tex->ns_outscale); break;
1049         case TE_DISTA:
1050                 poin= &(tex->dist_amount); break;
1051         case TE_VN_COLT:
1052                 poin= &(tex->vn_coltype); *type= IPO_SHORT; break;
1053         case TE_VN_DISTM:
1054                 poin= &(tex->vn_distm); *type= IPO_SHORT; break;
1055         case TE_MG_TYP:
1056                 poin= &(tex->stype); *type= IPO_SHORT; break;
1057         case TE_MGH:
1058                 poin= &(tex->mg_H); break;
1059         case TE_MG_LAC:
1060                 poin= &(tex->mg_lacunarity); break;
1061         case TE_MG_OCT:
1062                 poin= &(tex->mg_octaves); break;
1063         case TE_MG_OFF:
1064                 poin= &(tex->mg_offset); break;
1065         case TE_MG_GAIN:
1066                 poin= &(tex->mg_gain); break;
1067         case TE_N_BAS1:
1068                 poin= &(tex->noisebasis); *type= IPO_SHORT; break;
1069         case TE_N_BAS2:
1070                 poin= &(tex->noisebasis2); *type= IPO_SHORT; break;
1071         }
1072         
1073         return poin;
1074 }
1075
1076 void *give_mtex_poin(MTex *mtex, int adrcode )
1077 {
1078         void *poin=0;
1079                 
1080         switch(adrcode) {
1081         case MAP_OFS_X:
1082                 poin= &(mtex->ofs[0]); break;
1083         case MAP_OFS_Y:
1084                 poin= &(mtex->ofs[1]); break;
1085         case MAP_OFS_Z:
1086                 poin= &(mtex->ofs[2]); break;
1087         case MAP_SIZE_X:
1088                 poin= &(mtex->size[0]); break;
1089         case MAP_SIZE_Y:
1090                 poin= &(mtex->size[1]); break;
1091         case MAP_SIZE_Z:
1092                 poin= &(mtex->size[2]); break;
1093         case MAP_R:
1094                 poin= &(mtex->r); break;
1095         case MAP_G:
1096                 poin= &(mtex->g); break;
1097         case MAP_B:
1098                 poin= &(mtex->b); break;
1099         case MAP_DVAR:
1100                 poin= &(mtex->def_var); break;
1101         case MAP_COLF:
1102                 poin= &(mtex->colfac); break;
1103         case MAP_NORF:
1104                 poin= &(mtex->norfac); break;
1105         case MAP_VARF:
1106                 poin= &(mtex->varfac); break;
1107         case MAP_DISP:
1108                 poin= &(mtex->dispfac); break;
1109         }
1110         
1111         return poin;
1112 }
1113
1114 /* GS reads the memory pointed at in a specific ordering. There are,
1115  * however two definitions for it. I have jotted them down here, both,
1116  * but I think the first one is actually used. The thing is that
1117  * big-endian systems might read this the wrong way round. OTOH, we
1118  * constructed the IDs that are read out with this macro explicitly as
1119  * well. I expect we'll sort it out soon... */
1120
1121 /* from blendef: */
1122 #define GS(a)   (*((short *)(a)))
1123
1124 /* from misc_util: flip the bytes from x  */
1125 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
1126
1127 void *get_ipo_poin(ID *id, IpoCurve *icu, int *type)
1128 {
1129         void *poin= NULL;
1130         Object *ob;
1131         Material *ma;
1132         MTex *mtex;
1133         Tex *tex;
1134         Lamp *la;
1135         Sequence *seq;
1136         World *wo;
1137
1138         *type= IPO_FLOAT;
1139
1140         if( GS(id->name)==ID_OB) {
1141                 
1142                 ob= (Object *)id;
1143
1144                 switch(icu->adrcode) {
1145                 case OB_LOC_X:
1146                         poin= &(ob->loc[0]); break;
1147                 case OB_LOC_Y:
1148                         poin= &(ob->loc[1]); break;
1149                 case OB_LOC_Z:
1150                         poin= &(ob->loc[2]); break;
1151                 case OB_DLOC_X:
1152                         poin= &(ob->dloc[0]); break;
1153                 case OB_DLOC_Y:
1154                         poin= &(ob->dloc[1]); break;
1155                 case OB_DLOC_Z:
1156                         poin= &(ob->dloc[2]); break;
1157         
1158                 case OB_ROT_X:
1159                         poin= &(ob->rot[0]); *type= IPO_FLOAT_DEGR; break;
1160                 case OB_ROT_Y:
1161                         poin= &(ob->rot[1]); *type= IPO_FLOAT_DEGR; break;
1162                 case OB_ROT_Z:
1163                         poin= &(ob->rot[2]); *type= IPO_FLOAT_DEGR; break;
1164                 case OB_DROT_X:
1165                         poin= &(ob->drot[0]); *type= IPO_FLOAT_DEGR; break;
1166                 case OB_DROT_Y:
1167                         poin= &(ob->drot[1]); *type= IPO_FLOAT_DEGR; break;
1168                 case OB_DROT_Z:
1169                         poin= &(ob->drot[2]); *type= IPO_FLOAT_DEGR; break;
1170                         
1171                 case OB_SIZE_X:
1172                         poin= &(ob->size[0]); break;
1173                 case OB_SIZE_Y:
1174                         poin= &(ob->size[1]); break;
1175                 case OB_SIZE_Z:
1176                         poin= &(ob->size[2]); break;
1177                 case OB_DSIZE_X:
1178                         poin= &(ob->dsize[0]); break;
1179                 case OB_DSIZE_Y:
1180                         poin= &(ob->dsize[1]); break;
1181                 case OB_DSIZE_Z:
1182                         poin= &(ob->dsize[2]); break;
1183
1184                 case OB_LAY:
1185                         poin= &(ob->lay); *type= IPO_INT_BIT; break;
1186                         
1187                 case OB_COL_R:  
1188                         poin= &(ob->col[0]);
1189                         break;
1190                 case OB_COL_G:
1191                         poin= &(ob->col[1]);
1192                         break;
1193                 case OB_COL_B:
1194                         poin= &(ob->col[2]);
1195                         break;
1196                 case OB_COL_A:
1197                         poin= &(ob->col[3]);
1198                         break;
1199                 case OB_PD_FSTR:
1200                         if(ob->pd) poin= &(ob->pd->f_strength);
1201                         break;
1202                 case OB_PD_FFALL:
1203                         if(ob->pd) poin= &(ob->pd->f_power);
1204                         break;
1205                 case OB_PD_SDAMP:
1206                         if(ob->pd) poin= &(ob->pd->pdef_damp);
1207                         break;
1208                 case OB_PD_RDAMP:
1209                         if(ob->pd) poin= &(ob->pd->pdef_rdamp);
1210                         break;
1211                 case OB_PD_PERM:
1212                         if(ob->pd) poin= &(ob->pd->pdef_perm);
1213                         break;
1214                 }
1215         }
1216         else if( GS(id->name)==ID_MA) {
1217                 
1218                 ma= (Material *)id;
1219                 
1220                 switch(icu->adrcode) {
1221                 case MA_COL_R:
1222                         poin= &(ma->r); break;
1223                 case MA_COL_G:
1224                         poin= &(ma->g); break;
1225                 case MA_COL_B:
1226                         poin= &(ma->b); break;
1227                 case MA_SPEC_R:
1228                         poin= &(ma->specr); break;
1229                 case MA_SPEC_G:
1230                         poin= &(ma->specg); break;
1231                 case MA_SPEC_B:
1232                         poin= &(ma->specb); break;
1233                 case MA_MIR_R:
1234                         poin= &(ma->mirr); break;
1235                 case MA_MIR_G:
1236                         poin= &(ma->mirg); break;
1237                 case MA_MIR_B:
1238                         poin= &(ma->mirb); break;
1239                 case MA_REF:
1240                         poin= &(ma->ref); break;
1241                 case MA_ALPHA:
1242                         poin= &(ma->alpha); break;
1243                 case MA_EMIT:
1244                         poin= &(ma->emit); break;
1245                 case MA_AMB:
1246                         poin= &(ma->amb); break;
1247                 case MA_SPEC:
1248                         poin= &(ma->spec); break;
1249                 case MA_HARD:
1250                         poin= &(ma->har); *type= IPO_SHORT; break;
1251                 case MA_SPTR:
1252                         poin= &(ma->spectra); break;
1253                 case MA_IOR:
1254                         poin= &(ma->ang); break;
1255                 case MA_MODE:
1256                         poin= &(ma->mode); *type= IPO_INT_BIT; break;
1257                 case MA_HASIZE:
1258                         poin= &(ma->hasize); break;
1259                 case MA_TRANSLU:
1260                         poin= &(ma->translucency); break;
1261                 case MA_RAYM:
1262                         poin= &(ma->ray_mirror); break;
1263                 case MA_FRESMIR:
1264                         poin= &(ma->fresnel_mir); break;
1265                 case MA_FRESMIRI:
1266                         poin= &(ma->fresnel_mir_i); break;
1267                 case MA_FRESTRA:
1268                         poin= &(ma->fresnel_tra); break;
1269                 case MA_FRESTRAI:
1270                         poin= &(ma->fresnel_tra_i); break;
1271                 case MA_ADD:
1272                         poin= &(ma->add); break;
1273                 }
1274                 
1275                 if(poin==NULL) {
1276                         mtex= 0;
1277                         if(icu->adrcode & MA_MAP1) mtex= ma->mtex[0];
1278                         else if(icu->adrcode & MA_MAP2) mtex= ma->mtex[1];
1279                         else if(icu->adrcode & MA_MAP3) mtex= ma->mtex[2];
1280                         else if(icu->adrcode & MA_MAP4) mtex= ma->mtex[3];
1281                         else if(icu->adrcode & MA_MAP5) mtex= ma->mtex[4];
1282                         else if(icu->adrcode & MA_MAP6) mtex= ma->mtex[5];
1283                         else if(icu->adrcode & MA_MAP7) mtex= ma->mtex[6];
1284                         else if(icu->adrcode & MA_MAP8) mtex= ma->mtex[7];
1285                         else if(icu->adrcode & MA_MAP9) mtex= ma->mtex[8];
1286                         else if(icu->adrcode & MA_MAP10) mtex= ma->mtex[9];
1287                         
1288                         if(mtex) {
1289                                 poin= give_mtex_poin(mtex, icu->adrcode & (MA_MAP1-1) );
1290                         }
1291                 }
1292         }
1293         else if( GS(id->name)==ID_TE) {
1294                 tex= (Tex *)id;
1295                 
1296                 if(tex) poin= give_tex_poin(tex, icu->adrcode, type);
1297         }
1298         else if( GS(id->name)==ID_SEQ) {
1299                 seq= (Sequence *)id;
1300                 
1301                 switch(icu->adrcode) {
1302                 case SEQ_FAC1:
1303                         poin= &(seq->facf0); break;
1304                 }
1305         }
1306         else if( GS(id->name)==ID_CU) {
1307                 
1308                 poin= &(icu->curval);
1309                 
1310         }
1311         else if( GS(id->name)==ID_KE) {
1312                 KeyBlock *kb= ((Key *)id)->block.first;
1313                 
1314                 for(; kb; kb= kb->next)
1315                         if(kb->adrcode==icu->adrcode)
1316                                 break;
1317                 if(kb)
1318                         poin= &(kb->curval);
1319                 
1320         }
1321         else if(GS(id->name)==ID_WO) {
1322                 
1323                 wo= (World *)id;
1324                 
1325                 switch(icu->adrcode) {
1326                 case WO_HOR_R:
1327                         poin= &(wo->horr); break;
1328                 case WO_HOR_G:
1329                         poin= &(wo->horg); break;
1330                 case WO_HOR_B:
1331                         poin= &(wo->horb); break;
1332                 case WO_ZEN_R:
1333                         poin= &(wo->zenr); break;
1334                 case WO_ZEN_G:
1335                         poin= &(wo->zeng); break;
1336                 case WO_ZEN_B:
1337                         poin= &(wo->zenb); break;
1338
1339                 case WO_EXPOS:
1340                         poin= &(wo->exposure); break;
1341
1342                 case WO_MISI:
1343                         poin= &(wo->misi); break;
1344                 case WO_MISTDI:
1345                         poin= &(wo->mistdist); break;
1346                 case WO_MISTSTA:
1347                         poin= &(wo->miststa); break;
1348                 case WO_MISTHI:
1349                         poin= &(wo->misthi); break;
1350
1351                 case WO_STAR_R:
1352                         poin= &(wo->starr); break;
1353                 case WO_STAR_G:
1354                         poin= &(wo->starg); break;
1355                 case WO_STAR_B:
1356                         poin= &(wo->starb); break;
1357
1358                 case WO_STARDIST:
1359                         poin= &(wo->stardist); break;
1360                 case WO_STARSIZE:
1361                         poin= &(wo->starsize); break;
1362                 }
1363
1364                 if(poin==NULL) {
1365                         mtex= 0;
1366                         if(icu->adrcode & MA_MAP1) mtex= wo->mtex[0];
1367                         else if(icu->adrcode & MA_MAP2) mtex= wo->mtex[1];
1368                         else if(icu->adrcode & MA_MAP3) mtex= wo->mtex[2];
1369                         else if(icu->adrcode & MA_MAP4) mtex= wo->mtex[3];
1370                         else if(icu->adrcode & MA_MAP5) mtex= wo->mtex[4];
1371                         else if(icu->adrcode & MA_MAP6) mtex= wo->mtex[5];
1372                         else if(icu->adrcode & MA_MAP7) mtex= wo->mtex[6];
1373                         else if(icu->adrcode & MA_MAP8) mtex= wo->mtex[7];
1374                         else if(icu->adrcode & MA_MAP9) mtex= wo->mtex[8];
1375                         else if(icu->adrcode & MA_MAP10) mtex= wo->mtex[9];
1376                         
1377                         if(mtex) {
1378                                 poin= give_mtex_poin(mtex, icu->adrcode & (MA_MAP1-1) );
1379                         }
1380                 }
1381         }
1382         else if( GS(id->name)==ID_LA) {
1383                 
1384                 la= (Lamp *)id;
1385         
1386                 switch(icu->adrcode) {
1387                 case LA_ENERGY:
1388                         poin= &(la->energy); break;             
1389                 case LA_COL_R:
1390                         poin= &(la->r); break;
1391                 case LA_COL_G:
1392                         poin= &(la->g); break;
1393                 case LA_COL_B:
1394                         poin= &(la->b); break;
1395                 case LA_DIST:
1396                         poin= &(la->dist); break;               
1397                 case LA_SPOTSI:
1398                         poin= &(la->spotsize); break;
1399                 case LA_SPOTBL:
1400                         poin= &(la->spotblend); break;
1401                 case LA_QUAD1:
1402                         poin= &(la->att1); break;
1403                 case LA_QUAD2:
1404                         poin= &(la->att2); break;
1405                 case LA_HALOINT:
1406                         poin= &(la->haint); break;
1407                 }
1408                 
1409                 if(poin==NULL) {
1410                         mtex= 0;
1411                         if(icu->adrcode & MA_MAP1) mtex= la->mtex[0];
1412                         else if(icu->adrcode & MA_MAP2) mtex= la->mtex[1];
1413                         else if(icu->adrcode & MA_MAP3) mtex= la->mtex[2];
1414                         else if(icu->adrcode & MA_MAP4) mtex= la->mtex[3];
1415                         else if(icu->adrcode & MA_MAP5) mtex= la->mtex[4];
1416                         else if(icu->adrcode & MA_MAP6) mtex= la->mtex[5];
1417                         else if(icu->adrcode & MA_MAP7) mtex= la->mtex[6];
1418                         else if(icu->adrcode & MA_MAP8) mtex= la->mtex[7];
1419                         else if(icu->adrcode & MA_MAP9) mtex= la->mtex[8];
1420                         else if(icu->adrcode & MA_MAP10) mtex= la->mtex[9];
1421                         
1422                         if(mtex) {
1423                                 poin= give_mtex_poin(mtex, icu->adrcode & (MA_MAP1-1) );
1424                         }
1425                 }
1426         }
1427         else if(GS(id->name)==ID_CA) {
1428                 Camera *ca= (Camera *)id;
1429                 
1430                 /* yafray: aperture & focal distance params */
1431                 switch(icu->adrcode) {
1432                 case CAM_LENS:
1433                         poin= &(ca->lens); break;
1434                 case CAM_STA:
1435                         poin= &(ca->clipsta); break;
1436                 case CAM_END:
1437                         poin= &(ca->clipend); break;
1438                 case CAM_YF_APERT:
1439                         poin= &(ca->YF_aperture); break;
1440                 case CAM_YF_FDIST:
1441                         poin= &(ca->YF_dofdist); break;
1442                 }
1443         }
1444         else if(GS(id->name)==ID_SO) {
1445                 bSound *snd= (bSound *)id;
1446                 
1447                 switch(icu->adrcode) {
1448                 case SND_VOLUME:
1449                         poin= &(snd->volume); break;
1450                 case SND_PITCH:
1451                         poin= &(snd->pitch); break;
1452                 case SND_PANNING:
1453                         poin= &(snd->panning); break;
1454                 case SND_ATTEN:
1455                         poin= &(snd->attenuation); break;
1456                 }
1457         }
1458         
1459         return poin;
1460 }
1461
1462 void set_icu_vars(IpoCurve *icu)
1463 {
1464         
1465         icu->ymin= icu->ymax= 0.0;
1466         icu->ipo= IPO_BEZ;
1467         
1468         if(icu->blocktype==ID_OB) {
1469         
1470                 if(icu->adrcode==OB_LAY) {
1471                         icu->ipo= IPO_CONST;
1472                         icu->vartype= IPO_BITS;
1473                 }
1474                 
1475         }
1476         else if(icu->blocktype==ID_MA) {
1477                 
1478                 if(icu->adrcode < MA_MAP1) {
1479                         switch(icu->adrcode) {
1480                         case MA_HASIZE:
1481                                 icu->ymax= 10000.0; break;
1482                         case MA_HARD:
1483                                 icu->ymax= 128.0; break;
1484                         case MA_SPEC:
1485                                 icu->ymax= 2.0; break;
1486                         case MA_MODE:
1487                                 icu->ipo= IPO_CONST;
1488                                 icu->vartype= IPO_BITS; break;
1489                         case MA_RAYM:
1490                                 icu->ymax= 1.0; break;
1491                         case MA_TRANSLU:
1492                                 icu->ymax= 1.0; break;
1493                         case MA_IOR:
1494                                 icu->ymin= 1.0;
1495                                 icu->ymax= 3.0; break;
1496                         case MA_FRESMIR:
1497                                 icu->ymax= 5.0; break;
1498                         case MA_FRESMIRI:
1499                                 icu->ymin= 1.0;
1500                                 icu->ymax= 5.0; break;
1501                         case MA_FRESTRA:
1502                                 icu->ymax= 5.0; break;
1503                         case MA_FRESTRAI:
1504                                 icu->ymin= 1.0;
1505                                 icu->ymax= 5.0; break;
1506                         case MA_ADD:
1507                                 icu->ymax= 1.0; break;
1508                         default:
1509                                 icu->ymax= 1.0; break;
1510                         }
1511                 }
1512                 else {
1513                         switch(icu->adrcode & (MA_MAP1-1)) {
1514                         case MAP_OFS_X:
1515                         case MAP_OFS_Y:
1516                         case MAP_OFS_Z:
1517                         case MAP_SIZE_X:
1518                         case MAP_SIZE_Y:
1519                         case MAP_SIZE_Z:
1520                                 icu->ymax= 1000.0;
1521                                 icu->ymin= -1000.0;
1522                         
1523                                 break;
1524                         case MAP_R:
1525                         case MAP_G:
1526                         case MAP_B:
1527                         case MAP_DVAR:
1528                         case MAP_COLF:
1529                         case MAP_VARF:
1530                         case MAP_DISP:
1531                                 icu->ymax= 1.0;
1532                                 break;
1533                         case MAP_NORF:
1534                                 icu->ymax= 25.0;
1535                                 break;
1536                         }
1537                 }
1538         }
1539         else if(icu->blocktype==ID_TE) {
1540                 switch(icu->adrcode & (MA_MAP1-1)) {
1541                         case TE_NSIZE:
1542                                 icu->ymin= 0.0001;
1543                                 icu->ymax= 2.0; break;
1544                         case TE_NDEPTH:
1545                                 icu->vartype= IPO_SHORT;
1546                                 icu->ipo= IPO_CONST;
1547                                 icu->ymax= 6.0; break;
1548                         case TE_NTYPE:
1549                                 icu->vartype= IPO_SHORT;
1550                                 icu->ipo= IPO_CONST;
1551                                 icu->ymax= 1.0; break;
1552                         case TE_TURB:
1553                                 icu->ymax= 200.0; break;
1554                         case TE_VNW1:
1555                         case TE_VNW2:
1556                         case TE_VNW3:
1557                         case TE_VNW4:
1558                                 icu->ymax= 2.0;
1559                                 icu->ymin= -2.0; break;
1560                         case TE_VNMEXP:
1561                                 icu->ymax= 10.0;
1562                                 icu->ymin= 0.01; break;
1563                         case TE_VN_DISTM:
1564                                 icu->vartype= IPO_SHORT;
1565                                 icu->ipo= IPO_CONST;
1566                                 icu->ymax= 6.0; break;
1567                         case TE_VN_COLT:
1568                                 icu->vartype= IPO_SHORT;
1569                                 icu->ipo= IPO_CONST;
1570                                 icu->ymax= 3.0; break;
1571                         case TE_ISCA:
1572                                 icu->ymax= 10.0;
1573                                 icu->ymin= 0.01; break;
1574                         case TE_DISTA:
1575                                 icu->ymax= 10.0; break;
1576                         case TE_MG_TYP:
1577                                 icu->vartype= IPO_SHORT;
1578                                 icu->ipo= IPO_CONST;
1579                                 icu->ymax= 4.0; break;
1580                         case TE_MGH:
1581                                 icu->ymin= 0.0001;
1582                                 icu->ymax= 2.0; break;
1583                         case TE_MG_LAC:
1584                         case TE_MG_OFF:
1585                         case TE_MG_GAIN:
1586                                 icu->ymax= 6.0; break;
1587                         case TE_MG_OCT:
1588                                 icu->ymax= 8.0; break;
1589                         case TE_N_BAS1:
1590                         case TE_N_BAS2:
1591                                 icu->vartype= IPO_SHORT;
1592                                 icu->ipo= IPO_CONST;
1593                                 icu->ymax= 8.0; break;
1594                 }
1595         }
1596         else if(icu->blocktype==ID_SEQ) {
1597         
1598                 icu->ymax= 1.0;
1599                 
1600         }
1601         else if(icu->blocktype==ID_CU) {
1602         
1603                 icu->ymax= 1.0;
1604                 
1605         }
1606         else if(icu->blocktype==ID_WO) {
1607                 
1608                 if(icu->adrcode < MA_MAP1) {
1609                         switch(icu->adrcode) {
1610                         case WO_EXPOS:
1611                                 icu->ymax= 5.0; break;
1612                         case WO_MISTDI:
1613                         case WO_MISTSTA:
1614                         case WO_MISTHI:
1615                         case WO_STARDIST:
1616                         case WO_STARSIZE:
1617                                 break;
1618                                 
1619                         default:
1620                                 icu->ymax= 1.0;
1621                                 break;
1622                         }
1623                 }
1624                 else {
1625                         switch(icu->adrcode & (MA_MAP1-1)) {
1626                         case MAP_OFS_X:
1627                         case MAP_OFS_Y:
1628                         case MAP_OFS_Z:
1629                         case MAP_SIZE_X:
1630                         case MAP_SIZE_Y:
1631                         case MAP_SIZE_Z:
1632                                 icu->ymax= 100.0;
1633                                 icu->ymin= -100.0;
1634                         
1635                                 break;
1636                         case MAP_R:
1637                         case MAP_G:
1638                         case MAP_B:
1639                         case MAP_DVAR:
1640                         case MAP_COLF:
1641                         case MAP_NORF:
1642                         case MAP_VARF:
1643                         case MAP_DISP:
1644                                 icu->ymax= 1.0;
1645                         }
1646                 }
1647         }
1648         else if(icu->blocktype==ID_LA) {
1649                 if(icu->adrcode < MA_MAP1) {
1650                         switch(icu->adrcode) {
1651                         case LA_ENERGY:
1652                         case LA_DIST:
1653                                 break;          
1654         
1655                         case LA_COL_R:
1656                         case LA_COL_G:
1657                         case LA_COL_B:
1658                         case LA_SPOTBL:
1659                         case LA_QUAD1:
1660                         case LA_QUAD2:
1661                                 icu->ymax= 1.0; break;
1662                         case LA_SPOTSI:
1663                                 icu->ymax= 180.0; break;
1664                         case LA_HALOINT:
1665                                 icu->ymax= 5.0; break;
1666                         }
1667                 }
1668                 else {
1669                         switch(icu->adrcode & (MA_MAP1-1)) {
1670                         case MAP_OFS_X:
1671                         case MAP_OFS_Y:
1672                         case MAP_OFS_Z:
1673                         case MAP_SIZE_X:
1674                         case MAP_SIZE_Y:
1675                         case MAP_SIZE_Z:
1676                                 icu->ymax= 100.0;
1677                                 icu->ymin= -100.0;
1678                                 break;
1679                         case MAP_R:
1680                         case MAP_G:
1681                         case MAP_B:
1682                         case MAP_DVAR:
1683                         case MAP_COLF:
1684                         case MAP_NORF:
1685                         case MAP_VARF:
1686                         case MAP_DISP:
1687                                 icu->ymax= 1.0;
1688                         }
1689                 }
1690         }       
1691         else if(icu->blocktype==ID_CA) {
1692
1693                 /* yafray: aperture & focal distance params */
1694                 switch(icu->adrcode) {
1695                 case CAM_LENS:
1696                         icu->ymin= 5.0;
1697                         icu->ymax= 1000.0;
1698                         break;
1699                 case CAM_STA:
1700                         icu->ymin= 0.001f;
1701                         break;
1702                 case CAM_END:
1703                         icu->ymin= 0.1f;
1704                         break;
1705                 case CAM_YF_APERT:
1706                         icu->ymin = 0.0;
1707                         icu->ymax = 2.0;
1708                         break;
1709                 case CAM_YF_FDIST:
1710                         icu->ymin = 0.0;
1711                         icu->ymax = 5000.0;
1712                 }
1713         }
1714         else if(icu->blocktype==ID_SO) {
1715
1716                 switch(icu->adrcode) {
1717                 case SND_VOLUME:
1718                         icu->ymin= 0.0;
1719                         icu->ymax= 1.0;
1720                         break;
1721                 case SND_PITCH:
1722                         icu->ymin= -12.0;
1723                         icu->ymin= 12.0;
1724                         break;
1725                 case SND_PANNING:
1726                         icu->ymin= 0.0;
1727                         icu->ymax= 1.0;
1728                         break;
1729                 case SND_ATTEN:
1730                         icu->ymin= 0.0;
1731                         icu->ymin= 1.0;
1732                         break;
1733                 }
1734         }
1735 }
1736
1737 /* not for actions or constraints! */
1738 void execute_ipo(ID *id, Ipo *ipo)
1739 {
1740         IpoCurve *icu;
1741         void *poin;
1742         int type;
1743         
1744         if(ipo==NULL) return;
1745         
1746         for(icu= ipo->curve.first; icu; icu= icu->next) {
1747                 poin= get_ipo_poin(id, icu, &type);
1748                 if(poin) write_ipo_poin(poin, type, icu->curval);
1749         }
1750 }
1751
1752 void *get_pchan_ipo_poin(bPoseChannel *pchan, int adrcode)
1753 {
1754         void *poin= NULL;
1755         
1756         switch (adrcode) {
1757                 case AC_QUAT_W:
1758                         poin= &(pchan->quat[0]); 
1759                         pchan->flag |= POSE_ROT;
1760                         break;
1761                 case AC_QUAT_X:
1762                         poin= &(pchan->quat[1]); 
1763                         pchan->flag |= POSE_ROT;
1764                         break;
1765                 case AC_QUAT_Y:
1766                         poin= &(pchan->quat[2]); 
1767                         pchan->flag |= POSE_ROT;
1768                         break;
1769                 case AC_QUAT_Z:
1770                         poin= &(pchan->quat[3]); 
1771                         pchan->flag |= POSE_ROT;
1772                         break;
1773                 case AC_LOC_X:
1774                         poin= &(pchan->loc[0]); 
1775                         pchan->flag |= POSE_LOC;
1776                         break;
1777                 case AC_LOC_Y:
1778                         poin= &(pchan->loc[1]); 
1779                         pchan->flag |= POSE_LOC;
1780                         break;
1781                 case AC_LOC_Z:
1782                         poin= &(pchan->loc[2]); 
1783                         pchan->flag |= POSE_LOC;
1784                         break;                  
1785                 case AC_SIZE_X:
1786                         poin= &(pchan->size[0]); 
1787                         pchan->flag |= POSE_SIZE;
1788                         break;
1789                 case AC_SIZE_Y:
1790                         poin= &(pchan->size[1]); 
1791                         pchan->flag |= POSE_SIZE;
1792                         break;
1793                 case AC_SIZE_Z:
1794                         poin= &(pchan->size[2]); 
1795                         pchan->flag |= POSE_SIZE;
1796                         break;
1797         }
1798         return poin;
1799 }
1800
1801 void execute_action_ipo(bActionChannel *achan, bPoseChannel *pchan)
1802 {
1803
1804         if(achan && achan->ipo) {
1805                 IpoCurve *icu;
1806                 for(icu= achan->ipo->curve.first; icu; icu= icu->next) {
1807                         void *poin= get_pchan_ipo_poin(pchan, icu->adrcode);
1808                         if(poin) {
1809                           write_ipo_poin(poin, IPO_FLOAT, icu->curval);
1810                           //printf("execute_action_ipo wrote_ipo_poin: %f\n", icu->curval);
1811                           //printf("%s has poin %p value %f\n", achan->name, poin, icu->curval);
1812                         }
1813                 }
1814         }
1815 }
1816
1817 /* exception: it does calc for objects...
1818  * now find out why this routine was used anyway!
1819  */
1820 void do_ipo_nocalc(Ipo *ipo)
1821 {
1822         Object *ob;
1823         Material *ma;
1824         Tex *tex;
1825         World *wo;
1826         Lamp *la;
1827         Camera *ca;
1828         bSound *snd;
1829         
1830         if(ipo==NULL) return;
1831         
1832         switch(ipo->blocktype) {
1833         case ID_OB:
1834                 ob= G.main->object.first;
1835                 while(ob) {
1836                         if(ob->ipo==ipo) {
1837                                 do_ob_ipo(ob);
1838                                 /* execute_ipo((ID *)ob, ipo); */
1839                         }
1840                         ob= ob->id.next;
1841                 }
1842                 break;
1843         case ID_MA:
1844                 ma= G.main->mat.first;
1845                 while(ma) {
1846                         if(ma->ipo==ipo) execute_ipo((ID *)ma, ipo);
1847                         ma= ma->id.next;
1848                 }
1849                 break;
1850         case ID_TE:
1851                 tex= G.main->tex.first;
1852                 while(tex) {
1853                         if(tex->ipo==ipo) execute_ipo((ID *)tex, ipo);
1854                         tex=tex->id.next;
1855                 }
1856                 break;
1857         case ID_WO:
1858                 wo= G.main->world.first;
1859                 while(wo) {
1860                         if(wo->ipo==ipo) execute_ipo((ID *)wo, ipo);
1861                         wo= wo->id.next;
1862                 }
1863                 break;
1864         case ID_LA:
1865                 la= G.main->lamp.first;
1866                 while(la) {
1867                         if(la->ipo==ipo) execute_ipo((ID *)la, ipo);
1868                         la= la->id.next;
1869                 }
1870                 break;
1871         case ID_CA:
1872                 ca= G.main->camera.first;
1873                 while(ca) {
1874                         if(ca->ipo==ipo) execute_ipo((ID *)ca, ipo);
1875                         ca= ca->id.next;
1876                 }
1877                 break;
1878         case ID_SO:
1879                 snd= G.main->sound.first;
1880                 while(snd) {
1881                         if(snd->ipo==ipo) execute_ipo((ID *)snd, ipo);
1882                         snd= snd->id.next;
1883                 }
1884                 break;
1885         }
1886 }
1887
1888 void do_ipo(Ipo *ipo)
1889 {
1890         if(ipo) {
1891                 float ctime= frame_to_float(G.scene->r.cfra);
1892                 calc_ipo(ipo, ctime);
1893         
1894                 do_ipo_nocalc(ipo);
1895         }
1896 }
1897
1898
1899
1900 void do_mat_ipo(Material *ma)
1901 {
1902         float ctime;
1903         
1904         if(ma==NULL || ma->ipo==NULL) return;
1905         
1906         ctime= frame_to_float(G.scene->r.cfra);
1907         /* if(ob->ipoflag & OB_OFFS_OB) ctime-= ob->sf; */
1908
1909         calc_ipo(ma->ipo, ctime);
1910         
1911         execute_ipo((ID *)ma, ma->ipo);
1912 }
1913
1914 void do_ob_ipo(Object *ob)
1915 {
1916         float ctime;
1917         unsigned int lay;
1918         
1919         if(ob->ipo==NULL) return;
1920
1921         /* do not set ob->ctime here: for example when parent in invisible layer */
1922         
1923         ctime= bsystem_time(ob, 0, (float) G.scene->r.cfra, 0.0);
1924
1925         calc_ipo(ob->ipo, ctime);
1926
1927         /* Patch: remember localview */
1928         lay= ob->lay & 0xFF000000;
1929         
1930         execute_ipo((ID *)ob, ob->ipo);
1931
1932         ob->lay |= lay;
1933         if(ob->id.name[2]=='S' && ob->id.name[3]=='C' && ob->id.name[4]=='E') {
1934                 if(strcmp(G.scene->id.name+2, ob->id.name+6)==0) {
1935                         G.scene->lay= ob->lay;
1936                         copy_view3d_lock(0);
1937                         /* no redraw here! creates too many calls */
1938                 }
1939         }
1940 }
1941
1942 void do_ob_ipodrivers(Object *ob, Ipo *ipo)
1943 {
1944         IpoCurve *icu;
1945         void *poin;
1946         int type;
1947         
1948         for(icu= ipo->curve.first; icu; icu= icu->next) {
1949                 if(icu->driver) {
1950                         icu->curval= eval_icu(icu, 0.0f);
1951                         poin= get_ipo_poin((ID *)ob, icu, &type);
1952                         if(poin) write_ipo_poin(poin, type, icu->curval);
1953                 }
1954         }
1955 }
1956
1957 void do_seq_ipo(Sequence *seq)
1958 {
1959         float ctime, div;
1960         
1961         /* seq_ipo has an exception: calc both fields immediately */
1962         
1963         if(seq->ipo) {
1964                 ctime= frame_to_float(G.scene->r.cfra - seq->startdisp);
1965                 div= (seq->enddisp - seq->startdisp)/100.0f;
1966                 if(div==0.0) return;
1967                 
1968                 /* 2nd field */
1969                 calc_ipo(seq->ipo, (ctime+0.5f)/div);
1970                 execute_ipo((ID *)seq, seq->ipo);
1971                 seq->facf1= seq->facf0;
1972
1973                 /* 1st field */
1974                 calc_ipo(seq->ipo, ctime/div);
1975                 execute_ipo((ID *)seq, seq->ipo);
1976
1977         }
1978         else seq->facf1= seq->facf0= 1.0f;
1979 }
1980
1981 int has_ipo_code(Ipo *ipo, int code)
1982 {
1983         IpoCurve *icu;
1984         
1985         if(ipo==NULL) return 0;
1986         
1987         for(icu= ipo->curve.first; icu; icu= icu->next) {
1988                 if(icu->adrcode==code) return 1;
1989         }
1990         return 0;
1991 }
1992
1993 void do_all_data_ipos()
1994 {
1995         Material *ma;
1996         Tex *tex;
1997         World *wo;
1998         Ipo *ipo;
1999         Lamp *la;
2000         Key *key;
2001         Camera *ca;
2002         bSound *snd;
2003         Sequence *seq;
2004         Editing *ed;
2005         Base *base;
2006         float ctime;
2007
2008         ctime= frame_to_float(G.scene->r.cfra);
2009         
2010         /* this exception cannot be depgraphed yet... what todo with objects in other layers?... */
2011         for(base= G.scene->base.first; base; base= base->next) {
2012                 /* only update layer when an ipo */
2013                 if( has_ipo_code(base->object->ipo, OB_LAY) ) {
2014                         do_ob_ipo(base->object);
2015                         base->lay= base->object->lay;
2016                 }
2017         }
2018         
2019         /* layers for the set...*/
2020         if(G.scene->set) {
2021                 for(base= G.scene->set->base.first; base; base= base->next) {
2022                         if( has_ipo_code(base->object->ipo, OB_LAY) ) {
2023                                 do_ob_ipo(base->object);
2024                                 base->lay= base->object->lay;
2025                         }
2026                 }
2027         }
2028         
2029         
2030         ipo= G.main->ipo.first;
2031         while(ipo) {
2032                 if(ipo->id.us && ipo->blocktype!=ID_OB) {
2033                         calc_ipo(ipo, ctime);
2034                 }
2035                 ipo= ipo->id.next;
2036         }
2037
2038         for(tex= G.main->tex.first; tex; tex= tex->id.next) {
2039                 if(tex->ipo) execute_ipo((ID *)tex, tex->ipo);
2040         }
2041
2042         for(ma= G.main->mat.first; ma; ma= ma->id.next) {
2043                 if(ma->ipo) execute_ipo((ID *)ma, ma->ipo);
2044         }
2045
2046         for(wo= G.main->world.first; wo; wo= wo->id.next) {
2047                 if(wo->ipo) execute_ipo((ID *)wo, wo->ipo);
2048         }
2049         
2050         for(key= G.main->key.first; key; key= key->id.next) {
2051                 if(key->ipo) execute_ipo((ID *)key, key->ipo);
2052         }
2053         
2054         la= G.main->lamp.first;
2055         while(la) {
2056                 if(la->ipo) execute_ipo((ID *)la, la->ipo);
2057                 la= la->id.next;
2058         }
2059
2060         ca= G.main->camera.first;
2061         while(ca) {
2062                 if(ca->ipo) execute_ipo((ID *)ca, ca->ipo);
2063                 ca= ca->id.next;
2064         }
2065
2066         snd= G.main->sound.first;
2067         while(snd) {
2068                 if(snd->ipo) execute_ipo((ID *)snd, snd->ipo);
2069                 snd= snd->id.next;
2070         }
2071
2072         /* process FAC Ipos used as volume envelopes */
2073         ed= G.scene->ed;
2074         if (ed) {
2075                 seq= ed->seqbasep->first;
2076                 while(seq) {
2077                         if ((seq->type == SEQ_SOUND) && (seq->ipo) && 
2078                                 (seq->startdisp<=G.scene->r.cfra+2) && (seq->enddisp>G.scene->r.cfra)) 
2079                                         do_seq_ipo(seq);
2080                         seq= seq->next;
2081                 }
2082         }
2083
2084 }
2085
2086
2087 int calc_ipo_spec(Ipo *ipo, int adrcode, float *ctime)
2088 {
2089         IpoCurve *icu;
2090
2091         if(ipo==NULL) return 0;
2092
2093         for(icu= ipo->curve.first; icu; icu= icu->next) {
2094                 if(icu->adrcode == adrcode) {
2095                         if(icu->flag & IPO_LOCK);
2096                         else calc_icu(icu, *ctime);
2097                         
2098                         *ctime= icu->curval;
2099                         return 1;
2100                 }
2101         }
2102         
2103         return 0;
2104 }
2105
2106
2107 /* ************************** */
2108
2109 void clear_delta_obipo(Ipo *ipo)
2110 {
2111         Object *ob;
2112         
2113         if(ipo==NULL) return;
2114         
2115         ob= G.main->object.first;
2116         while(ob) {
2117                 if(ob->id.lib==NULL) {
2118                         if(ob->ipo==ipo) {
2119                                 memset(&ob->dloc, 0, 12);
2120                                 memset(&ob->drot, 0, 12);
2121                                 memset(&ob->dsize, 0, 12);
2122                         }
2123                 }
2124                 ob= ob->id.next;
2125         }
2126 }
2127
2128 void add_to_cfra_elem(ListBase *lb, BezTriple *bezt)
2129 {
2130         CfraElem *ce, *cen;
2131         
2132         ce= lb->first;
2133         while(ce) {
2134                 
2135                 if( ce->cfra==bezt->vec[1][0] ) {
2136                         /* do because of double keys */
2137                         if(bezt->f2 & 1) ce->sel= bezt->f2;
2138                         return;
2139                 }
2140                 else if(ce->cfra > bezt->vec[1][0]) break;
2141                 
2142                 ce= ce->next;
2143         }       
2144         
2145         cen= MEM_callocN(sizeof(CfraElem), "add_to_cfra_elem"); 
2146         if(ce) BLI_insertlinkbefore(lb, ce, cen);
2147         else BLI_addtail(lb, cen);
2148
2149         cen->cfra= bezt->vec[1][0];
2150         cen->sel= bezt->f2;
2151 }
2152
2153
2154
2155 void make_cfra_list(Ipo *ipo, ListBase *elems)
2156 {
2157         IpoCurve *icu;
2158         CfraElem *ce;
2159         BezTriple *bezt;
2160         int a;
2161         
2162         if(ipo->blocktype==ID_OB) {
2163                 for(icu= ipo->curve.first; icu; icu= icu->next) {
2164                         if(icu->flag & IPO_VISIBLE) {
2165                                 switch(icu->adrcode) {
2166                                 case OB_DLOC_X:
2167                                 case OB_DLOC_Y:
2168                                 case OB_DLOC_Z:
2169                                 case OB_DROT_X:
2170                                 case OB_DROT_Y:
2171                                 case OB_DROT_Z:
2172                                 case OB_DSIZE_X:
2173                                 case OB_DSIZE_Y:
2174                                 case OB_DSIZE_Z:
2175
2176                                 case OB_LOC_X:
2177                                 case OB_LOC_Y:
2178                                 case OB_LOC_Z:
2179                                 case OB_ROT_X:
2180                                 case OB_ROT_Y:
2181                                 case OB_ROT_Z:
2182                                 case OB_SIZE_X:
2183                                 case OB_SIZE_Y:
2184                                 case OB_SIZE_Z:
2185                                 case OB_PD_FSTR:
2186                                 case OB_PD_FFALL:
2187                                 case OB_PD_SDAMP:
2188                                 case OB_PD_RDAMP:
2189                                 case OB_PD_PERM:
2190                                         bezt= icu->bezt;
2191                                         if(bezt) {
2192                                                 a= icu->totvert;
2193                                                 while(a--) {
2194                                                         add_to_cfra_elem(elems, bezt);
2195                                                         bezt++;
2196                                                 }
2197                                         }
2198                                         break;
2199                                 }
2200                         }
2201                 }
2202         }
2203         else if(ipo->blocktype==ID_AC) {
2204                 for(icu= ipo->curve.first; icu; icu= icu->next) {
2205                         if(icu->flag & IPO_VISIBLE) {
2206                                 switch(icu->adrcode) {
2207                                 case AC_LOC_X:
2208                                 case AC_LOC_Y:
2209                                 case AC_LOC_Z:
2210                                 case AC_SIZE_X:
2211                                 case AC_SIZE_Y:
2212                                 case AC_SIZE_Z:
2213                                 case AC_QUAT_W:
2214                                 case AC_QUAT_X:
2215                                 case AC_QUAT_Y:
2216                                 case AC_QUAT_Z:
2217                                         bezt= icu->bezt;
2218                                         if(bezt) {
2219                                                 a= icu->totvert;
2220                                                 while(a--) {
2221                                                         add_to_cfra_elem(elems, bezt);
2222                                                         bezt++;
2223                                                 }
2224                                         }
2225                                         break;
2226                                 }
2227                         }
2228                 }
2229         }
2230         else {
2231                 for(icu= ipo->curve.first; icu; icu= icu->next) {
2232                         if(icu->flag & IPO_VISIBLE) {
2233                                 bezt= icu->bezt;
2234                                 if(bezt) {
2235                                         a= icu->totvert;
2236                                         while(a--) {
2237                                                 add_to_cfra_elem(elems, bezt);
2238                                                 bezt++;
2239                                         }
2240                                 }
2241                         }
2242                 }
2243         }
2244         
2245         if(ipo->showkey==0) {
2246                 /* deselect all keys */
2247                 ce= elems->first;
2248                 while(ce) {
2249                         ce->sel= 0;
2250                         ce= ce->next;
2251                 }
2252         }
2253 }
2254
2255 /* *********************** INTERFACE FOR KETSJI ********** */
2256
2257
2258 int IPO_GetChannels(Ipo *ipo, IPO_Channel *channels)
2259 {
2260         /* channels is max 32 items, allocated by calling function */   
2261
2262         IpoCurve *icu;
2263         int total=0;
2264         
2265         if(ipo==NULL) return 0;
2266         
2267         for(icu= ipo->curve.first; icu; icu= icu->next) {
2268                 channels[total]= icu->adrcode;
2269                 total++;
2270                 if(total>31) break;
2271         }
2272         
2273         return total;
2274 }
2275
2276
2277
2278 /* Get the float value for channel 'channel' at time 'ctime' */
2279
2280 float IPO_GetFloatValue(Ipo *ipo, IPO_Channel channel, float ctime)
2281 {
2282         if(ipo==NULL) return 0;
2283         
2284         calc_ipo_spec(ipo, channel, &ctime);
2285         
2286         if (OB_ROT_X <= channel && channel <= OB_DROT_Z) {
2287                 ctime *= (float)(M_PI_2/9.0); 
2288         }
2289
2290         return ctime;
2291 }