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