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