Little code cleanup.
[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         if(ipo->muteipo) return;
999         
1000         for(icu= ipo->curve.first; icu; icu= icu->next) {
1001                 if(icu->driver || (icu->flag & IPO_LOCK)==0) { 
1002                         if((icu->flag & IPO_MUTE)==0)
1003                                 calc_icu(icu, ctime);
1004                 }
1005         }
1006 }
1007
1008 /* ************************************** */
1009 /*              DO THE IPO!                                               */
1010 /* ************************************** */
1011
1012 void write_ipo_poin(void *poin, int type, float val)
1013 {
1014
1015         switch(type) {
1016         case IPO_FLOAT:
1017                 *( (float *)poin)= val;
1018                 break;
1019         case IPO_FLOAT_DEGR:
1020                 *( (float *)poin)= (float)(val*M_PI_2/9.0);
1021                 break;
1022         case IPO_INT:
1023         case IPO_INT_BIT:
1024         case IPO_LONG:
1025                 *( (int *)poin)= (int)val;
1026                 break;
1027         case IPO_SHORT:
1028         case IPO_SHORT_BIT:
1029                 *( (short *)poin)= (short)val;
1030                 break;
1031         case IPO_CHAR:
1032         case IPO_CHAR_BIT:
1033                 *( (char *)poin)= (char)val;
1034                 break;
1035         }
1036 }
1037
1038 float read_ipo_poin(void *poin, int type)
1039 {
1040         float val = 0.0;
1041         
1042         switch(type) {
1043         case IPO_FLOAT:
1044                 val= *( (float *)poin);
1045                 break;
1046         case IPO_FLOAT_DEGR:
1047                 val= *( (float *)poin);
1048                 val = (float)(val/(M_PI_2/9.0));
1049                 break;
1050         case IPO_INT:
1051         case IPO_INT_BIT:
1052         case IPO_LONG:
1053                 val= (float)(*( (int *)poin));
1054                 break;
1055         case IPO_SHORT:
1056         case IPO_SHORT_BIT:
1057                 val= *( (short *)poin);
1058                 break;
1059         case IPO_CHAR:
1060         case IPO_CHAR_BIT:
1061                 val= *( (char *)poin);
1062                 break;
1063         }
1064         return val;
1065 }
1066
1067 static void *give_tex_poin(Tex *tex, int adrcode, int *type )
1068 {
1069         void *poin=0;
1070
1071         switch(adrcode) {
1072         case TE_NSIZE:
1073                 poin= &(tex->noisesize); break;
1074         case TE_TURB:
1075                 poin= &(tex->turbul); break;
1076         case TE_NDEPTH:
1077                 poin= &(tex->noisedepth); *type= IPO_SHORT; break;
1078         case TE_NTYPE:
1079                 poin= &(tex->noisetype); *type= IPO_SHORT; break;
1080         case TE_VNW1:
1081                 poin= &(tex->vn_w1); break;
1082         case TE_VNW2:
1083                 poin= &(tex->vn_w2); break;
1084         case TE_VNW3:
1085                 poin= &(tex->vn_w3); break;
1086         case TE_VNW4:
1087                 poin= &(tex->vn_w4); break;
1088         case TE_VNMEXP:
1089                 poin= &(tex->vn_mexp); break;
1090         case TE_ISCA:
1091                 poin= &(tex->ns_outscale); break;
1092         case TE_DISTA:
1093                 poin= &(tex->dist_amount); break;
1094         case TE_VN_COLT:
1095                 poin= &(tex->vn_coltype); *type= IPO_SHORT; break;
1096         case TE_VN_DISTM:
1097                 poin= &(tex->vn_distm); *type= IPO_SHORT; break;
1098         case TE_MG_TYP:
1099                 poin= &(tex->stype); *type= IPO_SHORT; break;
1100         case TE_MGH:
1101                 poin= &(tex->mg_H); break;
1102         case TE_MG_LAC:
1103                 poin= &(tex->mg_lacunarity); break;
1104         case TE_MG_OCT:
1105                 poin= &(tex->mg_octaves); break;
1106         case TE_MG_OFF:
1107                 poin= &(tex->mg_offset); break;
1108         case TE_MG_GAIN:
1109                 poin= &(tex->mg_gain); break;
1110         case TE_N_BAS1:
1111                 poin= &(tex->noisebasis); *type= IPO_SHORT; break;
1112         case TE_N_BAS2:
1113                 poin= &(tex->noisebasis2); *type= IPO_SHORT; break;
1114         case TE_COL_R:
1115                 poin= &(tex->rfac); break;
1116         case TE_COL_G:
1117                 poin= &(tex->gfac); break;
1118         case TE_COL_B:
1119                 poin= &(tex->bfac); break;
1120         case TE_BRIGHT:
1121                 poin= &(tex->bright); break;
1122         case TE_CONTRA:
1123                 poin= &(tex->contrast); break;
1124
1125         }
1126         
1127         return poin;
1128 }
1129
1130 void *give_mtex_poin(MTex *mtex, int adrcode )
1131 {
1132         void *poin=0;
1133                 
1134         switch(adrcode) {
1135         case MAP_OFS_X:
1136                 poin= &(mtex->ofs[0]); break;
1137         case MAP_OFS_Y:
1138                 poin= &(mtex->ofs[1]); break;
1139         case MAP_OFS_Z:
1140                 poin= &(mtex->ofs[2]); break;
1141         case MAP_SIZE_X:
1142                 poin= &(mtex->size[0]); break;
1143         case MAP_SIZE_Y:
1144                 poin= &(mtex->size[1]); break;
1145         case MAP_SIZE_Z:
1146                 poin= &(mtex->size[2]); break;
1147         case MAP_R:
1148                 poin= &(mtex->r); break;
1149         case MAP_G:
1150                 poin= &(mtex->g); break;
1151         case MAP_B:
1152                 poin= &(mtex->b); break;
1153         case MAP_DVAR:
1154                 poin= &(mtex->def_var); break;
1155         case MAP_COLF:
1156                 poin= &(mtex->colfac); break;
1157         case MAP_NORF:
1158                 poin= &(mtex->norfac); break;
1159         case MAP_VARF:
1160                 poin= &(mtex->varfac); break;
1161         case MAP_DISP:
1162                 poin= &(mtex->dispfac); break;
1163         }
1164         
1165         return poin;
1166 }
1167
1168 /* GS reads the memory pointed at in a specific ordering. There are,
1169  * however two definitions for it. I have jotted them down here, both,
1170  * but I think the first one is actually used. The thing is that
1171  * big-endian systems might read this the wrong way round. OTOH, we
1172  * constructed the IDs that are read out with this macro explicitly as
1173  * well. I expect we'll sort it out soon... */
1174
1175 /* from blendef: */
1176 #define GS(a)   (*((short *)(a)))
1177
1178 /* from misc_util: flip the bytes from x  */
1179 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
1180
1181 void *get_ipo_poin(ID *id, IpoCurve *icu, int *type)
1182 {
1183         void *poin= NULL;
1184         Object *ob;
1185         Material *ma;
1186         MTex *mtex;
1187         Tex *tex;
1188         Lamp *la;
1189         Sequence *seq;
1190         World *wo;
1191
1192         *type= IPO_FLOAT;
1193
1194         if( GS(id->name)==ID_OB) {
1195                 
1196                 ob= (Object *)id;
1197
1198                 switch(icu->adrcode) {
1199                 case OB_LOC_X:
1200                         poin= &(ob->loc[0]); break;
1201                 case OB_LOC_Y:
1202                         poin= &(ob->loc[1]); break;
1203                 case OB_LOC_Z:
1204                         poin= &(ob->loc[2]); break;
1205                 case OB_DLOC_X:
1206                         poin= &(ob->dloc[0]); break;
1207                 case OB_DLOC_Y:
1208                         poin= &(ob->dloc[1]); break;
1209                 case OB_DLOC_Z:
1210                         poin= &(ob->dloc[2]); break;
1211         
1212                 case OB_ROT_X:
1213                         poin= &(ob->rot[0]); *type= IPO_FLOAT_DEGR; break;
1214                 case OB_ROT_Y:
1215                         poin= &(ob->rot[1]); *type= IPO_FLOAT_DEGR; break;
1216                 case OB_ROT_Z:
1217                         poin= &(ob->rot[2]); *type= IPO_FLOAT_DEGR; break;
1218                 case OB_DROT_X:
1219                         poin= &(ob->drot[0]); *type= IPO_FLOAT_DEGR; break;
1220                 case OB_DROT_Y:
1221                         poin= &(ob->drot[1]); *type= IPO_FLOAT_DEGR; break;
1222                 case OB_DROT_Z:
1223                         poin= &(ob->drot[2]); *type= IPO_FLOAT_DEGR; break;
1224                         
1225                 case OB_SIZE_X:
1226                         poin= &(ob->size[0]); break;
1227                 case OB_SIZE_Y:
1228                         poin= &(ob->size[1]); break;
1229                 case OB_SIZE_Z:
1230                         poin= &(ob->size[2]); break;
1231                 case OB_DSIZE_X:
1232                         poin= &(ob->dsize[0]); break;
1233                 case OB_DSIZE_Y:
1234                         poin= &(ob->dsize[1]); break;
1235                 case OB_DSIZE_Z:
1236                         poin= &(ob->dsize[2]); break;
1237
1238                 case OB_LAY:
1239                         poin= &(ob->lay); *type= IPO_INT_BIT; break;
1240                         
1241                 case OB_COL_R:  
1242                         poin= &(ob->col[0]);
1243                         break;
1244                 case OB_COL_G:
1245                         poin= &(ob->col[1]);
1246                         break;
1247                 case OB_COL_B:
1248                         poin= &(ob->col[2]);
1249                         break;
1250                 case OB_COL_A:
1251                         poin= &(ob->col[3]);
1252                         break;
1253                 case OB_PD_FSTR:
1254                         if(ob->pd) poin= &(ob->pd->f_strength);
1255                         break;
1256                 case OB_PD_FFALL:
1257                         if(ob->pd) poin= &(ob->pd->f_power);
1258                         break;
1259                 case OB_PD_SDAMP:
1260                         if(ob->pd) poin= &(ob->pd->pdef_damp);
1261                         break;
1262                 case OB_PD_RDAMP:
1263                         if(ob->pd) poin= &(ob->pd->pdef_rdamp);
1264                         break;
1265                 case OB_PD_PERM:
1266                         if(ob->pd) poin= &(ob->pd->pdef_perm);
1267                         break;
1268                 }
1269         }
1270         else if( GS(id->name)==ID_MA) {
1271                 
1272                 ma= (Material *)id;
1273                 
1274                 switch(icu->adrcode) {
1275                 case MA_COL_R:
1276                         poin= &(ma->r); break;
1277                 case MA_COL_G:
1278                         poin= &(ma->g); break;
1279                 case MA_COL_B:
1280                         poin= &(ma->b); break;
1281                 case MA_SPEC_R:
1282                         poin= &(ma->specr); break;
1283                 case MA_SPEC_G:
1284                         poin= &(ma->specg); break;
1285                 case MA_SPEC_B:
1286                         poin= &(ma->specb); break;
1287                 case MA_MIR_R:
1288                         poin= &(ma->mirr); break;
1289                 case MA_MIR_G:
1290                         poin= &(ma->mirg); break;
1291                 case MA_MIR_B:
1292                         poin= &(ma->mirb); break;
1293                 case MA_REF:
1294                         poin= &(ma->ref); break;
1295                 case MA_ALPHA:
1296                         poin= &(ma->alpha); break;
1297                 case MA_EMIT:
1298                         poin= &(ma->emit); break;
1299                 case MA_AMB:
1300                         poin= &(ma->amb); break;
1301                 case MA_SPEC:
1302                         poin= &(ma->spec); break;
1303                 case MA_HARD:
1304                         poin= &(ma->har); *type= IPO_SHORT; break;
1305                 case MA_SPTR:
1306                         poin= &(ma->spectra); break;
1307                 case MA_IOR:
1308                         poin= &(ma->ang); break;
1309                 case MA_MODE:
1310                         poin= &(ma->mode); *type= IPO_INT_BIT; break;
1311                 case MA_HASIZE:
1312                         poin= &(ma->hasize); break;
1313                 case MA_TRANSLU:
1314                         poin= &(ma->translucency); break;
1315                 case MA_RAYM:
1316                         poin= &(ma->ray_mirror); break;
1317                 case MA_FRESMIR:
1318                         poin= &(ma->fresnel_mir); break;
1319                 case MA_FRESMIRI:
1320                         poin= &(ma->fresnel_mir_i); break;
1321                 case MA_FRESTRA:
1322                         poin= &(ma->fresnel_tra); break;
1323                 case MA_FRESTRAI:
1324                         poin= &(ma->fresnel_tra_i); break;
1325                 case MA_ADD:
1326                         poin= &(ma->add); break;
1327                 }
1328                 
1329                 if(poin==NULL) {
1330                         mtex= 0;
1331                         if(icu->adrcode & MA_MAP1) mtex= ma->mtex[0];
1332                         else if(icu->adrcode & MA_MAP2) mtex= ma->mtex[1];
1333                         else if(icu->adrcode & MA_MAP3) mtex= ma->mtex[2];
1334                         else if(icu->adrcode & MA_MAP4) mtex= ma->mtex[3];
1335                         else if(icu->adrcode & MA_MAP5) mtex= ma->mtex[4];
1336                         else if(icu->adrcode & MA_MAP6) mtex= ma->mtex[5];
1337                         else if(icu->adrcode & MA_MAP7) mtex= ma->mtex[6];
1338                         else if(icu->adrcode & MA_MAP8) mtex= ma->mtex[7];
1339                         else if(icu->adrcode & MA_MAP9) mtex= ma->mtex[8];
1340                         else if(icu->adrcode & MA_MAP10) mtex= ma->mtex[9];
1341                         
1342                         if(mtex) {
1343                                 poin= give_mtex_poin(mtex, icu->adrcode & (MA_MAP1-1) );
1344                         }
1345                 }
1346         }
1347         else if( GS(id->name)==ID_TE) {
1348                 tex= (Tex *)id;
1349                 
1350                 if(tex) poin= give_tex_poin(tex, icu->adrcode, type);
1351         }
1352         else if( GS(id->name)==ID_SEQ) {
1353                 seq= (Sequence *)id;
1354                 
1355                 switch(icu->adrcode) {
1356                 case SEQ_FAC1:
1357                         poin= &(seq->facf0); break;
1358                 }
1359         }
1360         else if( GS(id->name)==ID_CU) {
1361                 
1362                 poin= &(icu->curval);
1363                 
1364         }
1365         else if( GS(id->name)==ID_KE) {
1366                 KeyBlock *kb= ((Key *)id)->block.first;
1367                 
1368                 for(; kb; kb= kb->next)
1369                         if(kb->adrcode==icu->adrcode)
1370                                 break;
1371                 if(kb)
1372                         poin= &(kb->curval);
1373                 
1374         }
1375         else if(GS(id->name)==ID_WO) {
1376                 
1377                 wo= (World *)id;
1378                 
1379                 switch(icu->adrcode) {
1380                 case WO_HOR_R:
1381                         poin= &(wo->horr); break;
1382                 case WO_HOR_G:
1383                         poin= &(wo->horg); break;
1384                 case WO_HOR_B:
1385                         poin= &(wo->horb); break;
1386                 case WO_ZEN_R:
1387                         poin= &(wo->zenr); break;
1388                 case WO_ZEN_G:
1389                         poin= &(wo->zeng); break;
1390                 case WO_ZEN_B:
1391                         poin= &(wo->zenb); break;
1392
1393                 case WO_EXPOS:
1394                         poin= &(wo->exposure); break;
1395
1396                 case WO_MISI:
1397                         poin= &(wo->misi); break;
1398                 case WO_MISTDI:
1399                         poin= &(wo->mistdist); break;
1400                 case WO_MISTSTA:
1401                         poin= &(wo->miststa); break;
1402                 case WO_MISTHI:
1403                         poin= &(wo->misthi); break;
1404
1405                 case WO_STAR_R:
1406                         poin= &(wo->starr); break;
1407                 case WO_STAR_G:
1408                         poin= &(wo->starg); break;
1409                 case WO_STAR_B:
1410                         poin= &(wo->starb); break;
1411
1412                 case WO_STARDIST:
1413                         poin= &(wo->stardist); break;
1414                 case WO_STARSIZE:
1415                         poin= &(wo->starsize); break;
1416                 }
1417
1418                 if(poin==NULL) {
1419                         mtex= 0;
1420                         if(icu->adrcode & MA_MAP1) mtex= wo->mtex[0];
1421                         else if(icu->adrcode & MA_MAP2) mtex= wo->mtex[1];
1422                         else if(icu->adrcode & MA_MAP3) mtex= wo->mtex[2];
1423                         else if(icu->adrcode & MA_MAP4) mtex= wo->mtex[3];
1424                         else if(icu->adrcode & MA_MAP5) mtex= wo->mtex[4];
1425                         else if(icu->adrcode & MA_MAP6) mtex= wo->mtex[5];
1426                         else if(icu->adrcode & MA_MAP7) mtex= wo->mtex[6];
1427                         else if(icu->adrcode & MA_MAP8) mtex= wo->mtex[7];
1428                         else if(icu->adrcode & MA_MAP9) mtex= wo->mtex[8];
1429                         else if(icu->adrcode & MA_MAP10) mtex= wo->mtex[9];
1430                         
1431                         if(mtex) {
1432                                 poin= give_mtex_poin(mtex, icu->adrcode & (MA_MAP1-1) );
1433                         }
1434                 }
1435         }
1436         else if( GS(id->name)==ID_LA) {
1437                 
1438                 la= (Lamp *)id;
1439         
1440                 switch(icu->adrcode) {
1441                 case LA_ENERGY:
1442                         poin= &(la->energy); break;             
1443                 case LA_COL_R:
1444                         poin= &(la->r); break;
1445                 case LA_COL_G:
1446                         poin= &(la->g); break;
1447                 case LA_COL_B:
1448                         poin= &(la->b); break;
1449                 case LA_DIST:
1450                         poin= &(la->dist); break;               
1451                 case LA_SPOTSI:
1452                         poin= &(la->spotsize); break;
1453                 case LA_SPOTBL:
1454                         poin= &(la->spotblend); break;
1455                 case LA_QUAD1:
1456                         poin= &(la->att1); break;
1457                 case LA_QUAD2:
1458                         poin= &(la->att2); break;
1459                 case LA_HALOINT:
1460                         poin= &(la->haint); break;
1461                 }
1462                 
1463                 if(poin==NULL) {
1464                         mtex= 0;
1465                         if(icu->adrcode & MA_MAP1) mtex= la->mtex[0];
1466                         else if(icu->adrcode & MA_MAP2) mtex= la->mtex[1];
1467                         else if(icu->adrcode & MA_MAP3) mtex= la->mtex[2];
1468                         else if(icu->adrcode & MA_MAP4) mtex= la->mtex[3];
1469                         else if(icu->adrcode & MA_MAP5) mtex= la->mtex[4];
1470                         else if(icu->adrcode & MA_MAP6) mtex= la->mtex[5];
1471                         else if(icu->adrcode & MA_MAP7) mtex= la->mtex[6];
1472                         else if(icu->adrcode & MA_MAP8) mtex= la->mtex[7];
1473                         else if(icu->adrcode & MA_MAP9) mtex= la->mtex[8];
1474                         else if(icu->adrcode & MA_MAP10) mtex= la->mtex[9];
1475                         
1476                         if(mtex) {
1477                                 poin= give_mtex_poin(mtex, icu->adrcode & (MA_MAP1-1) );
1478                         }
1479                 }
1480         }
1481         else if(GS(id->name)==ID_CA) {
1482                 Camera *ca= (Camera *)id;
1483                 
1484                 /* yafray: aperture & focal distance params */
1485                 switch(icu->adrcode) {
1486                 case CAM_LENS:
1487                         if(ca->type==CAM_ORTHO)
1488                                 poin= &(ca->ortho_scale);
1489                         else
1490                                 poin= &(ca->lens); 
1491                         break;
1492                 case CAM_STA:
1493                         poin= &(ca->clipsta); break;
1494                 case CAM_END:
1495                         poin= &(ca->clipend); break;
1496                 case CAM_YF_APERT:
1497                         poin= &(ca->YF_aperture); break;
1498                 case CAM_YF_FDIST:
1499                         poin= &(ca->YF_dofdist); break;
1500                 case CAM_SHIFT_X:
1501                         poin= &(ca->shiftx); break;
1502                 case CAM_SHIFT_Y:
1503                         poin= &(ca->shifty); break;
1504                 }
1505         }
1506         else if(GS(id->name)==ID_SO) {
1507                 bSound *snd= (bSound *)id;
1508                 
1509                 switch(icu->adrcode) {
1510                 case SND_VOLUME:
1511                         poin= &(snd->volume); break;
1512                 case SND_PITCH:
1513                         poin= &(snd->pitch); break;
1514                 case SND_PANNING:
1515                         poin= &(snd->panning); break;
1516                 case SND_ATTEN:
1517                         poin= &(snd->attenuation); break;
1518                 }
1519         }
1520         
1521         return poin;
1522 }
1523
1524 void set_icu_vars(IpoCurve *icu)
1525 {
1526         
1527         icu->ymin= icu->ymax= 0.0;
1528         icu->ipo= IPO_BEZ;
1529         
1530         if(icu->blocktype==ID_OB) {
1531         
1532                 if(icu->adrcode==OB_LAY) {
1533                         icu->ipo= IPO_CONST;
1534                         icu->vartype= IPO_BITS;
1535                 }
1536                 
1537         }
1538         else if(icu->blocktype==ID_MA) {
1539                 
1540                 if(icu->adrcode < MA_MAP1) {
1541                         switch(icu->adrcode) {
1542                         case MA_HASIZE:
1543                                 icu->ymax= 10000.0; break;
1544                         case MA_HARD:
1545                                 icu->ymax= 511.0; break;
1546                         case MA_SPEC:
1547                                 icu->ymax= 2.0; break;
1548                         case MA_MODE:
1549                                 icu->ipo= IPO_CONST;
1550                                 icu->vartype= IPO_BITS; break;
1551                         case MA_RAYM:
1552                                 icu->ymax= 1.0; break;
1553                         case MA_TRANSLU:
1554                                 icu->ymax= 1.0; break;
1555                         case MA_IOR:
1556                                 icu->ymin= 1.0;
1557                                 icu->ymax= 3.0; break;
1558                         case MA_FRESMIR:
1559                                 icu->ymax= 5.0; break;
1560                         case MA_FRESMIRI:
1561                                 icu->ymin= 1.0;
1562                                 icu->ymax= 5.0; break;
1563                         case MA_FRESTRA:
1564                                 icu->ymax= 5.0; break;
1565                         case MA_FRESTRAI:
1566                                 icu->ymin= 1.0;
1567                                 icu->ymax= 5.0; break;
1568                         case MA_ADD:
1569                                 icu->ymax= 1.0; break;
1570                         default:
1571                                 icu->ymax= 1.0; break;
1572                         }
1573                 }
1574                 else {
1575                         switch(icu->adrcode & (MA_MAP1-1)) {
1576                         case MAP_OFS_X:
1577                         case MAP_OFS_Y:
1578                         case MAP_OFS_Z:
1579                         case MAP_SIZE_X:
1580                         case MAP_SIZE_Y:
1581                         case MAP_SIZE_Z:
1582                                 icu->ymax= 1000.0;
1583                                 icu->ymin= -1000.0;
1584                         
1585                                 break;
1586                         case MAP_R:
1587                         case MAP_G:
1588                         case MAP_B:
1589                         case MAP_DVAR:
1590                         case MAP_COLF:
1591                         case MAP_VARF:
1592                         case MAP_DISP:
1593                                 icu->ymax= 1.0;
1594                                 break;
1595                         case MAP_NORF:
1596                                 icu->ymax= 25.0;
1597                                 break;
1598                         }
1599                 }
1600         }
1601         else if(icu->blocktype==ID_TE) {
1602                 switch(icu->adrcode & (MA_MAP1-1)) {
1603                         case TE_NSIZE:
1604                                 icu->ymin= 0.0001;
1605                                 icu->ymax= 2.0; break;
1606                         case TE_NDEPTH:
1607                                 icu->vartype= IPO_SHORT;
1608                                 icu->ipo= IPO_CONST;
1609                                 icu->ymax= 6.0; break;
1610                         case TE_NTYPE:
1611                                 icu->vartype= IPO_SHORT;
1612                                 icu->ipo= IPO_CONST;
1613                                 icu->ymax= 1.0; break;
1614                         case TE_TURB:
1615                                 icu->ymax= 200.0; break;
1616                         case TE_VNW1:
1617                         case TE_VNW2:
1618                         case TE_VNW3:
1619                         case TE_VNW4:
1620                                 icu->ymax= 2.0;
1621                                 icu->ymin= -2.0; break;
1622                         case TE_VNMEXP:
1623                                 icu->ymax= 10.0;
1624                                 icu->ymin= 0.01; break;
1625                         case TE_VN_DISTM:
1626                                 icu->vartype= IPO_SHORT;
1627                                 icu->ipo= IPO_CONST;
1628                                 icu->ymax= 6.0; break;
1629                         case TE_VN_COLT:
1630                                 icu->vartype= IPO_SHORT;
1631                                 icu->ipo= IPO_CONST;
1632                                 icu->ymax= 3.0; break;
1633                         case TE_ISCA:
1634                                 icu->ymax= 10.0;
1635                                 icu->ymin= 0.01; break;
1636                         case TE_DISTA:
1637                                 icu->ymax= 10.0; break;
1638                         case TE_MG_TYP:
1639                                 icu->vartype= IPO_SHORT;
1640                                 icu->ipo= IPO_CONST;
1641                                 icu->ymax= 6.0; break;
1642                         case TE_MGH:
1643                                 icu->ymin= 0.0001;
1644                                 icu->ymax= 2.0; break;
1645                         case TE_MG_LAC:
1646                         case TE_MG_OFF:
1647                         case TE_MG_GAIN:
1648                                 icu->ymax= 6.0; break;
1649                         case TE_MG_OCT:
1650                                 icu->ymax= 8.0; break;
1651                         case TE_N_BAS1:
1652                         case TE_N_BAS2:
1653                                 icu->vartype= IPO_SHORT;
1654                                 icu->ipo= IPO_CONST;
1655                                 icu->ymax= 8.0; break;
1656                         case TE_COL_R:
1657                                 icu->ymax= 0.0; break;
1658                         case TE_COL_G:
1659                                 icu->ymax= 2.0; break;
1660                         case TE_COL_B:
1661                                 icu->ymax= 2.0; break;
1662                         case TE_BRIGHT:
1663                                 icu->ymax= 2.0; break;
1664                         case TE_CONTRA:
1665                                 icu->ymax= 5.0; break;                          
1666
1667                 }
1668         }
1669         else if(icu->blocktype==ID_SEQ) {
1670         
1671                 icu->ymax= 1.0;
1672                 
1673         }
1674         else if(icu->blocktype==ID_CU) {
1675         
1676                 icu->ymax= 1.0;
1677                 
1678         }
1679         else if(icu->blocktype==ID_WO) {
1680                 
1681                 if(icu->adrcode < MA_MAP1) {
1682                         switch(icu->adrcode) {
1683                         case WO_EXPOS:
1684                                 icu->ymax= 5.0; break;
1685                         case WO_MISTDI:
1686                         case WO_MISTSTA:
1687                         case WO_MISTHI:
1688                         case WO_STARDIST:
1689                         case WO_STARSIZE:
1690                                 break;
1691                                 
1692                         default:
1693                                 icu->ymax= 1.0;
1694                                 break;
1695                         }
1696                 }
1697                 else {
1698                         switch(icu->adrcode & (MA_MAP1-1)) {
1699                         case MAP_OFS_X:
1700                         case MAP_OFS_Y:
1701                         case MAP_OFS_Z:
1702                         case MAP_SIZE_X:
1703                         case MAP_SIZE_Y:
1704                         case MAP_SIZE_Z:
1705                                 icu->ymax= 100.0;
1706                                 icu->ymin= -100.0;
1707                         
1708                                 break;
1709                         case MAP_R:
1710                         case MAP_G:
1711                         case MAP_B:
1712                         case MAP_DVAR:
1713                         case MAP_COLF:
1714                         case MAP_NORF:
1715                         case MAP_VARF:
1716                         case MAP_DISP:
1717                                 icu->ymax= 1.0;
1718                         }
1719                 }
1720         }
1721         else if(icu->blocktype==ID_LA) {
1722                 if(icu->adrcode < MA_MAP1) {
1723                         switch(icu->adrcode) {
1724                         case LA_ENERGY:
1725                         case LA_DIST:
1726                                 break;          
1727         
1728                         case LA_COL_R:
1729                         case LA_COL_G:
1730                         case LA_COL_B:
1731                         case LA_SPOTBL:
1732                         case LA_QUAD1:
1733                         case LA_QUAD2:
1734                                 icu->ymax= 1.0; break;
1735                         case LA_SPOTSI:
1736                                 icu->ymax= 180.0; break;
1737                         case LA_HALOINT:
1738                                 icu->ymax= 5.0; break;
1739                         }
1740                 }
1741                 else {
1742                         switch(icu->adrcode & (MA_MAP1-1)) {
1743                         case MAP_OFS_X:
1744                         case MAP_OFS_Y:
1745                         case MAP_OFS_Z:
1746                         case MAP_SIZE_X:
1747                         case MAP_SIZE_Y:
1748                         case MAP_SIZE_Z:
1749                                 icu->ymax= 100.0;
1750                                 icu->ymin= -100.0;
1751                                 break;
1752                         case MAP_R:
1753                         case MAP_G:
1754                         case MAP_B:
1755                         case MAP_DVAR:
1756                         case MAP_COLF:
1757                         case MAP_NORF:
1758                         case MAP_VARF:
1759                         case MAP_DISP:
1760                                 icu->ymax= 1.0;
1761                         }
1762                 }
1763         }       
1764         else if(icu->blocktype==ID_CA) {
1765
1766                 /* yafray: aperture & focal distance params */
1767                 switch(icu->adrcode) {
1768                 case CAM_LENS:
1769                         icu->ymin= 5.0;
1770                         icu->ymax= 1000.0;
1771                         break;
1772                 case CAM_STA:
1773                         icu->ymin= 0.001f;
1774                         break;
1775                 case CAM_END:
1776                         icu->ymin= 0.1f;
1777                         break;
1778                 case CAM_YF_APERT:
1779                         icu->ymin = 0.0;
1780                         icu->ymax = 2.0;
1781                         break;
1782                 case CAM_YF_FDIST:
1783                         icu->ymin = 0.0;
1784                         icu->ymax = 5000.0;
1785                         break;
1786                         
1787                 case CAM_SHIFT_X:
1788                 case CAM_SHIFT_Y:
1789                         icu->ymin= -2.0f;
1790                         icu->ymax= 2.0f;
1791                         break;
1792                 }
1793         }
1794         else if(icu->blocktype==ID_SO) {
1795
1796                 switch(icu->adrcode) {
1797                 case SND_VOLUME:
1798                         icu->ymin= 0.0;
1799                         icu->ymax= 1.0;
1800                         break;
1801                 case SND_PITCH:
1802                         icu->ymin= -12.0;
1803                         icu->ymin= 12.0;
1804                         break;
1805                 case SND_PANNING:
1806                         icu->ymin= 0.0;
1807                         icu->ymax= 1.0;
1808                         break;
1809                 case SND_ATTEN:
1810                         icu->ymin= 0.0;
1811                         icu->ymin= 1.0;
1812                         break;
1813                 }
1814         }
1815 }
1816
1817 /* not for actions or constraints! */
1818 void execute_ipo(ID *id, Ipo *ipo)
1819 {
1820         IpoCurve *icu;
1821         void *poin;
1822         int type;
1823         
1824         if(ipo==NULL) return;
1825         
1826         for(icu= ipo->curve.first; icu; icu= icu->next) {
1827                 poin= get_ipo_poin(id, icu, &type);
1828                 if(poin) write_ipo_poin(poin, type, icu->curval);
1829         }
1830 }
1831
1832 void *get_pchan_ipo_poin(bPoseChannel *pchan, int adrcode)
1833 {
1834         void *poin= NULL;
1835         
1836         switch (adrcode) {
1837                 case AC_QUAT_W:
1838                         poin= &(pchan->quat[0]); 
1839                         pchan->flag |= POSE_ROT;
1840                         break;
1841                 case AC_QUAT_X:
1842                         poin= &(pchan->quat[1]); 
1843                         pchan->flag |= POSE_ROT;
1844                         break;
1845                 case AC_QUAT_Y:
1846                         poin= &(pchan->quat[2]); 
1847                         pchan->flag |= POSE_ROT;
1848                         break;
1849                 case AC_QUAT_Z:
1850                         poin= &(pchan->quat[3]); 
1851                         pchan->flag |= POSE_ROT;
1852                         break;
1853                 case AC_LOC_X:
1854                         poin= &(pchan->loc[0]); 
1855                         pchan->flag |= POSE_LOC;
1856                         break;
1857                 case AC_LOC_Y:
1858                         poin= &(pchan->loc[1]); 
1859                         pchan->flag |= POSE_LOC;
1860                         break;
1861                 case AC_LOC_Z:
1862                         poin= &(pchan->loc[2]); 
1863                         pchan->flag |= POSE_LOC;
1864                         break;                  
1865                 case AC_SIZE_X:
1866                         poin= &(pchan->size[0]); 
1867                         pchan->flag |= POSE_SIZE;
1868                         break;
1869                 case AC_SIZE_Y:
1870                         poin= &(pchan->size[1]); 
1871                         pchan->flag |= POSE_SIZE;
1872                         break;
1873                 case AC_SIZE_Z:
1874                         poin= &(pchan->size[2]); 
1875                         pchan->flag |= POSE_SIZE;
1876                         break;
1877         }
1878         return poin;
1879 }
1880
1881 void execute_action_ipo(bActionChannel *achan, bPoseChannel *pchan)
1882 {
1883
1884         if(achan && achan->ipo) {
1885                 IpoCurve *icu;
1886                 for(icu= achan->ipo->curve.first; icu; icu= icu->next) {
1887                         void *poin= get_pchan_ipo_poin(pchan, icu->adrcode);
1888                         if(poin) {
1889                                 write_ipo_poin(poin, IPO_FLOAT, icu->curval);
1890                                 //printf("execute_action_ipo wrote_ipo_poin: %f\n", icu->curval);
1891                                 //printf("%s has poin %p value %f\n", achan->name, poin, icu->curval);
1892                         }
1893                 }
1894         }
1895 }
1896
1897 /* exception: it does calc for objects...
1898  * now find out why this routine was used anyway!
1899  */
1900 void do_ipo_nocalc(Ipo *ipo)
1901 {
1902         Object *ob;
1903         Material *ma;
1904         Tex *tex;
1905         World *wo;
1906         Lamp *la;
1907         Camera *ca;
1908         bSound *snd;
1909         
1910         if(ipo==NULL) return;
1911         
1912         switch(ipo->blocktype) {
1913         case ID_OB:
1914                 ob= G.main->object.first;
1915                 while(ob) {
1916                         if(ob->ipo==ipo) {
1917                                 do_ob_ipo(ob);
1918                                 /* execute_ipo((ID *)ob, ipo); */
1919                         }
1920                         ob= ob->id.next;
1921                 }
1922                 break;
1923         case ID_MA:
1924                 ma= G.main->mat.first;
1925                 while(ma) {
1926                         if(ma->ipo==ipo) execute_ipo((ID *)ma, ipo);
1927                         ma= ma->id.next;
1928                 }
1929                 break;
1930         case ID_TE:
1931                 tex= G.main->tex.first;
1932                 while(tex) {
1933                         if(tex->ipo==ipo) execute_ipo((ID *)tex, ipo);
1934                         tex=tex->id.next;
1935                 }
1936                 break;
1937         case ID_WO:
1938                 wo= G.main->world.first;
1939                 while(wo) {
1940                         if(wo->ipo==ipo) execute_ipo((ID *)wo, ipo);
1941                         wo= wo->id.next;
1942                 }
1943                 break;
1944         case ID_LA:
1945                 la= G.main->lamp.first;
1946                 while(la) {
1947                         if(la->ipo==ipo) execute_ipo((ID *)la, ipo);
1948                         la= la->id.next;
1949                 }
1950                 break;
1951         case ID_CA:
1952                 ca= G.main->camera.first;
1953                 while(ca) {
1954                         if(ca->ipo==ipo) execute_ipo((ID *)ca, ipo);
1955                         ca= ca->id.next;
1956                 }
1957                 break;
1958         case ID_SO:
1959                 snd= G.main->sound.first;
1960                 while(snd) {
1961                         if(snd->ipo==ipo) execute_ipo((ID *)snd, ipo);
1962                         snd= snd->id.next;
1963                 }
1964                 break;
1965         }
1966 }
1967
1968 void do_ipo(Ipo *ipo)
1969 {
1970         if(ipo) {
1971                 float ctime= frame_to_float(G.scene->r.cfra);
1972                 calc_ipo(ipo, ctime);
1973         
1974                 do_ipo_nocalc(ipo);
1975         }
1976 }
1977
1978
1979
1980 void do_mat_ipo(Material *ma)
1981 {
1982         float ctime;
1983         
1984         if(ma==NULL || ma->ipo==NULL) return;
1985         
1986         ctime= frame_to_float(G.scene->r.cfra);
1987         /* if(ob->ipoflag & OB_OFFS_OB) ctime-= ob->sf; */
1988
1989         calc_ipo(ma->ipo, ctime);
1990         
1991         execute_ipo((ID *)ma, ma->ipo);
1992 }
1993
1994 void do_ob_ipo(Object *ob)
1995 {
1996         float ctime;
1997         unsigned int lay;
1998         
1999         if(ob->ipo==NULL) return;
2000
2001         /* do not set ob->ctime here: for example when parent in invisible layer */
2002         
2003         ctime= bsystem_time(ob, (float) G.scene->r.cfra, 0.0);
2004
2005         calc_ipo(ob->ipo, ctime);
2006
2007         /* Patch: remember localview */
2008         lay= ob->lay & 0xFF000000;
2009         
2010         execute_ipo((ID *)ob, ob->ipo);
2011
2012         ob->lay |= lay;
2013         if(ob->id.name[2]=='S' && ob->id.name[3]=='C' && ob->id.name[4]=='E') {
2014                 if(strcmp(G.scene->id.name+2, ob->id.name+6)==0) {
2015                         G.scene->lay= ob->lay;
2016                         copy_view3d_lock(0);
2017                         /* no redraw here! creates too many calls */
2018                 }
2019         }
2020 }
2021
2022 void do_ob_ipodrivers(Object *ob, Ipo *ipo, float ctime)
2023 {
2024         IpoCurve *icu;
2025         void *poin;
2026         int type;
2027         
2028         for(icu= ipo->curve.first; icu; icu= icu->next) {
2029                 if(icu->driver) {
2030                         icu->curval= eval_icu(icu, ctime);
2031                         poin= get_ipo_poin((ID *)ob, icu, &type);
2032                         if(poin) write_ipo_poin(poin, type, icu->curval);
2033                 }
2034         }
2035 }
2036
2037 void do_seq_ipo(Sequence *seq)
2038 {
2039         float ctime, div;
2040         
2041         /* seq_ipo has an exception: calc both fields immediately */
2042         
2043         if(seq->ipo) {
2044                 if((seq->flag & SEQ_IPO_FRAME_LOCKED) != 0) {
2045                         ctime = frame_to_float(G.scene->r.cfra);
2046                         div = 1.0;
2047                 } else {
2048                         ctime= frame_to_float(G.scene->r.cfra 
2049                                               - seq->startdisp);
2050                         div= (seq->enddisp - seq->startdisp)/100.0f;
2051                         if(div==0.0) return;
2052                 }
2053                 
2054                 /* 2nd field */
2055                 calc_ipo(seq->ipo, (ctime+0.5f)/div);
2056                 execute_ipo((ID *)seq, seq->ipo);
2057                 seq->facf1= seq->facf0;
2058
2059                 /* 1st field */
2060                 calc_ipo(seq->ipo, ctime/div);
2061                 execute_ipo((ID *)seq, seq->ipo);
2062
2063         }
2064         else seq->facf1= seq->facf0= 1.0f;
2065 }
2066
2067 int has_ipo_code(Ipo *ipo, int code)
2068 {
2069         IpoCurve *icu;
2070         
2071         if(ipo==NULL) return 0;
2072         
2073         for(icu= ipo->curve.first; icu; icu= icu->next) {
2074                 if(icu->adrcode==code) return 1;
2075         }
2076         return 0;
2077 }
2078
2079 void do_all_data_ipos()
2080 {
2081         Material *ma;
2082         Tex *tex;
2083         World *wo;
2084         Ipo *ipo;
2085         Lamp *la;
2086         Key *key;
2087         Camera *ca;
2088         bSound *snd;
2089         Sequence *seq;
2090         Editing *ed;
2091         Base *base;
2092         float ctime;
2093
2094         ctime= frame_to_float(G.scene->r.cfra);
2095         
2096         /* this exception cannot be depgraphed yet... what todo with objects in other layers?... */
2097         for(base= G.scene->base.first; base; base= base->next) {
2098                 /* only update layer when an ipo */
2099                 if( has_ipo_code(base->object->ipo, OB_LAY) ) {
2100                         do_ob_ipo(base->object);
2101                         base->lay= base->object->lay;
2102                 }
2103         }
2104         
2105         /* layers for the set...*/
2106         if(G.scene->set) {
2107                 for(base= G.scene->set->base.first; base; base= base->next) {
2108                         if( has_ipo_code(base->object->ipo, OB_LAY) ) {
2109                                 do_ob_ipo(base->object);
2110                                 base->lay= base->object->lay;
2111                         }
2112                 }
2113         }
2114         
2115         
2116         ipo= G.main->ipo.first;
2117         while(ipo) {
2118                 if(ipo->id.us && ipo->blocktype!=ID_OB) {
2119                         calc_ipo(ipo, ctime);
2120                 }
2121                 ipo= ipo->id.next;
2122         }
2123
2124         for(tex= G.main->tex.first; tex; tex= tex->id.next) {
2125                 if(tex->ipo) execute_ipo((ID *)tex, tex->ipo);
2126         }
2127
2128         for(ma= G.main->mat.first; ma; ma= ma->id.next) {
2129                 if(ma->ipo) execute_ipo((ID *)ma, ma->ipo);
2130         }
2131
2132         for(wo= G.main->world.first; wo; wo= wo->id.next) {
2133                 if(wo->ipo) execute_ipo((ID *)wo, wo->ipo);
2134         }
2135         
2136         for(key= G.main->key.first; key; key= key->id.next) {
2137                 if(key->ipo) execute_ipo((ID *)key, key->ipo);
2138         }
2139         
2140         la= G.main->lamp.first;
2141         while(la) {
2142                 if(la->ipo) execute_ipo((ID *)la, la->ipo);
2143                 la= la->id.next;
2144         }
2145
2146         ca= G.main->camera.first;
2147         while(ca) {
2148                 if(ca->ipo) execute_ipo((ID *)ca, ca->ipo);
2149                 ca= ca->id.next;
2150         }
2151
2152         snd= G.main->sound.first;
2153         while(snd) {
2154                 if(snd->ipo) execute_ipo((ID *)snd, snd->ipo);
2155                 snd= snd->id.next;
2156         }
2157
2158         /* process FAC Ipos used as volume envelopes */
2159         ed= G.scene->ed;
2160         if (ed) {
2161                 seq= ed->seqbasep->first;
2162                 while(seq) {
2163                         if ((seq->type == SEQ_RAM_SOUND
2164                              || seq->type == SEQ_HD_SOUND) && (seq->ipo) && 
2165                                 (seq->startdisp<=G.scene->r.cfra+2) && 
2166                             (seq->enddisp>G.scene->r.cfra)) 
2167                                         do_seq_ipo(seq);
2168                         seq= seq->next;
2169                 }
2170         }
2171
2172 }
2173
2174
2175 int calc_ipo_spec(Ipo *ipo, int adrcode, float *ctime)
2176 {
2177         IpoCurve *icu;
2178
2179         if(ipo==NULL) return 0;
2180
2181         for(icu= ipo->curve.first; icu; icu= icu->next) {
2182                 if(icu->adrcode == adrcode) {
2183                         if(icu->flag & IPO_LOCK);
2184                         else calc_icu(icu, *ctime);
2185                         
2186                         *ctime= icu->curval;
2187                         return 1;
2188                 }
2189         }
2190         
2191         return 0;
2192 }
2193
2194
2195 /* ************************** */
2196
2197 void clear_delta_obipo(Ipo *ipo)
2198 {
2199         Object *ob;
2200         
2201         if(ipo==NULL) return;
2202         
2203         ob= G.main->object.first;
2204         while(ob) {
2205                 if(ob->id.lib==NULL) {
2206                         if(ob->ipo==ipo) {
2207                                 memset(&ob->dloc, 0, 12);
2208                                 memset(&ob->drot, 0, 12);
2209                                 memset(&ob->dsize, 0, 12);
2210                         }
2211                 }
2212                 ob= ob->id.next;
2213         }
2214 }
2215
2216 void add_to_cfra_elem(ListBase *lb, BezTriple *bezt)
2217 {
2218         CfraElem *ce, *cen;
2219         
2220         ce= lb->first;
2221         while(ce) {
2222                 
2223                 if( ce->cfra==bezt->vec[1][0] ) {
2224                         /* do because of double keys */
2225                         if(bezt->f2 & 1) ce->sel= bezt->f2;
2226                         return;
2227                 }
2228                 else if(ce->cfra > bezt->vec[1][0]) break;
2229                 
2230                 ce= ce->next;
2231         }       
2232         
2233         cen= MEM_callocN(sizeof(CfraElem), "add_to_cfra_elem"); 
2234         if(ce) BLI_insertlinkbefore(lb, ce, cen);
2235         else BLI_addtail(lb, cen);
2236
2237         cen->cfra= bezt->vec[1][0];
2238         cen->sel= bezt->f2;
2239 }
2240
2241
2242
2243 void make_cfra_list(Ipo *ipo, ListBase *elems)
2244 {
2245         IpoCurve *icu;
2246         BezTriple *bezt;
2247         int a;
2248         
2249         if(ipo->blocktype==ID_OB) {
2250                 for(icu= ipo->curve.first; icu; icu= icu->next) {
2251                         if(icu->flag & IPO_VISIBLE) {
2252                                 switch(icu->adrcode) {
2253                                 case OB_DLOC_X:
2254                                 case OB_DLOC_Y:
2255                                 case OB_DLOC_Z:
2256                                 case OB_DROT_X:
2257                                 case OB_DROT_Y:
2258                                 case OB_DROT_Z:
2259                                 case OB_DSIZE_X:
2260                                 case OB_DSIZE_Y:
2261                                 case OB_DSIZE_Z:
2262
2263                                 case OB_LOC_X:
2264                                 case OB_LOC_Y:
2265                                 case OB_LOC_Z:
2266                                 case OB_ROT_X:
2267                                 case OB_ROT_Y:
2268                                 case OB_ROT_Z:
2269                                 case OB_SIZE_X:
2270                                 case OB_SIZE_Y:
2271                                 case OB_SIZE_Z:
2272                                 case OB_PD_FSTR:
2273                                 case OB_PD_FFALL:
2274                                 case OB_PD_SDAMP:
2275                                 case OB_PD_RDAMP:
2276                                 case OB_PD_PERM:
2277                                         bezt= icu->bezt;
2278                                         if(bezt) {
2279                                                 a= icu->totvert;
2280                                                 while(a--) {
2281                                                         add_to_cfra_elem(elems, bezt);
2282                                                         bezt++;
2283                                                 }
2284                                         }
2285                                         break;
2286                                 }
2287                         }
2288                 }
2289         }
2290         else if(ipo->blocktype==ID_AC) {
2291                 for(icu= ipo->curve.first; icu; icu= icu->next) {
2292                         if(icu->flag & IPO_VISIBLE) {
2293                                 switch(icu->adrcode) {
2294                                 case AC_LOC_X:
2295                                 case AC_LOC_Y:
2296                                 case AC_LOC_Z:
2297                                 case AC_SIZE_X:
2298                                 case AC_SIZE_Y:
2299                                 case AC_SIZE_Z:
2300                                 case AC_QUAT_W:
2301                                 case AC_QUAT_X:
2302                                 case AC_QUAT_Y:
2303                                 case AC_QUAT_Z:
2304                                         bezt= icu->bezt;
2305                                         if(bezt) {
2306                                                 a= icu->totvert;
2307                                                 while(a--) {
2308                                                         add_to_cfra_elem(elems, bezt);
2309                                                         bezt++;
2310                                                 }
2311                                         }
2312                                         break;
2313                                 }
2314                         }
2315                 }
2316         }
2317         else {
2318                 for(icu= ipo->curve.first; icu; icu= icu->next) {
2319                         if(icu->flag & IPO_VISIBLE) {
2320                                 bezt= icu->bezt;
2321                                 if(bezt) {
2322                                         a= icu->totvert;
2323                                         while(a--) {
2324                                                 add_to_cfra_elem(elems, bezt);
2325                                                 bezt++;
2326                                         }
2327                                 }
2328                         }
2329                 }
2330         }
2331
2332         /* what's the point of this little block of code?  */
2333 #if 0
2334         if(ipo->showkey==0) {
2335                 /* deselect all keys */
2336                 ce= elems->first;
2337                 while(ce) {
2338                         ce->sel= 0;
2339                         ce= ce->next;
2340                 }
2341         }
2342 #endif
2343 }
2344
2345 /* *********************** INTERFACE FOR KETSJI ********** */
2346
2347
2348 int IPO_GetChannels(Ipo *ipo, IPO_Channel *channels)
2349 {
2350         /* channels is max 32 items, allocated by calling function */   
2351
2352         IpoCurve *icu;
2353         int total=0;
2354         
2355         if(ipo==NULL) return 0;
2356         
2357         for(icu= ipo->curve.first; icu; icu= icu->next) {
2358                 channels[total]= icu->adrcode;
2359                 total++;
2360                 if(total>31) break;
2361         }
2362         
2363         return total;
2364 }
2365
2366
2367
2368 /* Get the float value for channel 'channel' at time 'ctime' */
2369
2370 float IPO_GetFloatValue(Ipo *ipo, IPO_Channel channel, float ctime)
2371 {
2372         if(ipo==NULL) return 0;
2373         
2374         calc_ipo_spec(ipo, channel, &ctime);
2375         
2376         if (OB_ROT_X <= channel && channel <= OB_DROT_Z) {
2377                 ctime *= (float)(M_PI_2/9.0); 
2378         }
2379
2380         return ctime;
2381 }