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