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