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