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