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