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