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