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