Second try at committing IPO code cleanups...
[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): 2008, Joshua Leung (IPO System cleanup)
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 /* ***************************** Adrcode Blocktype Defines ********************************* */
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    This should whole adrcode system should eventually be replaced by a proper Data API
87 */
88
89
90 int co_ar[CO_TOTIPO]= {
91         CO_ENFORCE, CO_HEADTAIL
92 };
93
94 int ob_ar[OB_TOTIPO]= {
95         OB_LOC_X, OB_LOC_Y, OB_LOC_Z, OB_DLOC_X, OB_DLOC_Y, OB_DLOC_Z, 
96         OB_ROT_X, OB_ROT_Y, OB_ROT_Z, OB_DROT_X, OB_DROT_Y, OB_DROT_Z, 
97         OB_SIZE_X, OB_SIZE_Y, OB_SIZE_Z, OB_DSIZE_X, OB_DSIZE_Y, OB_DSIZE_Z, 
98         OB_LAY, OB_TIME, OB_COL_R, OB_COL_G, OB_COL_B, OB_COL_A,
99         OB_PD_FSTR, OB_PD_FFALL, OB_PD_SDAMP, OB_PD_RDAMP, OB_PD_PERM, OB_PD_FMAXD
100 };
101
102 int ac_ar[AC_TOTIPO]= {
103         AC_LOC_X, AC_LOC_Y, AC_LOC_Z,  
104          AC_QUAT_W, AC_QUAT_X, AC_QUAT_Y, AC_QUAT_Z,
105         AC_SIZE_X, AC_SIZE_Y, AC_SIZE_Z
106 };
107
108 int ma_ar[MA_TOTIPO]= {
109         MA_COL_R, MA_COL_G, MA_COL_B, 
110         MA_SPEC_R, MA_SPEC_G, MA_SPEC_B, 
111         MA_MIR_R, MA_MIR_G, MA_MIR_B,
112         MA_REF, MA_ALPHA, MA_EMIT, MA_AMB, 
113         MA_SPEC, MA_HARD, MA_SPTR, MA_IOR, 
114         MA_MODE, MA_HASIZE, MA_TRANSLU, MA_RAYM,
115         MA_FRESMIR, MA_FRESMIRI, MA_FRESTRA, MA_FRESTRAI, MA_ADD,
116         
117         MA_MAP1+MAP_OFS_X, MA_MAP1+MAP_OFS_Y, MA_MAP1+MAP_OFS_Z, 
118         MA_MAP1+MAP_SIZE_X, MA_MAP1+MAP_SIZE_Y, MA_MAP1+MAP_SIZE_Z, 
119         MA_MAP1+MAP_R, MA_MAP1+MAP_G, MA_MAP1+MAP_B,
120         MA_MAP1+MAP_DVAR, MA_MAP1+MAP_COLF, MA_MAP1+MAP_NORF, MA_MAP1+MAP_VARF, MA_MAP1+MAP_DISP
121 };
122
123 int te_ar[TE_TOTIPO] ={
124         
125         TE_NSIZE, TE_NDEPTH, TE_NTYPE, TE_TURB,
126         
127         TE_VNW1, TE_VNW2, TE_VNW3, TE_VNW4,
128         TE_VNMEXP, TE_VN_COLT, TE_VN_DISTM,
129         
130         TE_ISCA, TE_DISTA,
131         
132         TE_MG_TYP, TE_MGH, TE_MG_LAC, TE_MG_OCT, TE_MG_OFF, TE_MG_GAIN,
133         
134         TE_N_BAS1, TE_N_BAS2,
135         
136         TE_COL_R, TE_COL_G, TE_COL_B, TE_BRIGHT, TE_CONTRA
137 };
138
139 int seq_ar[SEQ_TOTIPO]= {
140         SEQ_FAC1
141 };
142
143 int cu_ar[CU_TOTIPO]= {
144         CU_SPEED
145 };
146
147 int wo_ar[WO_TOTIPO]= {
148         WO_HOR_R, WO_HOR_G, WO_HOR_B, WO_ZEN_R, WO_ZEN_G, WO_ZEN_B, 
149         WO_EXPOS, WO_MISI, WO_MISTDI, WO_MISTSTA, WO_MISTHI,
150         WO_STAR_R, WO_STAR_G, WO_STAR_B, WO_STARDIST, WO_STARSIZE, 
151
152         MA_MAP1+MAP_OFS_X, MA_MAP1+MAP_OFS_Y, MA_MAP1+MAP_OFS_Z, 
153         MA_MAP1+MAP_SIZE_X, MA_MAP1+MAP_SIZE_Y, MA_MAP1+MAP_SIZE_Z, 
154         MA_MAP1+MAP_R, MA_MAP1+MAP_G, MA_MAP1+MAP_B,
155         MA_MAP1+MAP_DVAR, MA_MAP1+MAP_COLF, MA_MAP1+MAP_NORF, MA_MAP1+MAP_VARF
156 };
157
158 int la_ar[LA_TOTIPO]= {
159         LA_ENERGY, LA_COL_R, LA_COL_G, LA_COL_B, 
160         LA_DIST, LA_SPOTSI, LA_SPOTBL, 
161         LA_QUAD1, LA_QUAD2, LA_HALOINT,  
162
163         MA_MAP1+MAP_OFS_X, MA_MAP1+MAP_OFS_Y, MA_MAP1+MAP_OFS_Z, 
164         MA_MAP1+MAP_SIZE_X, MA_MAP1+MAP_SIZE_Y, MA_MAP1+MAP_SIZE_Z, 
165         MA_MAP1+MAP_R, MA_MAP1+MAP_G, MA_MAP1+MAP_B,
166         MA_MAP1+MAP_DVAR, MA_MAP1+MAP_COLF
167 };
168
169 /* yafray: aperture & focal distance curves added */
170 /* qdn: FDIST now available to Blender as well for defocus node */
171 int cam_ar[CAM_TOTIPO]= {
172         CAM_LENS, CAM_STA, CAM_END, CAM_YF_APERT, CAM_YF_FDIST, CAM_SHIFT_X, CAM_SHIFT_Y
173 };
174
175 int snd_ar[SND_TOTIPO]= {
176         SND_VOLUME, SND_PITCH, SND_PANNING, SND_ATTEN
177 };
178
179 int fluidsim_ar[FLUIDSIM_TOTIPO]= {
180         FLUIDSIM_VISC, FLUIDSIM_TIME,
181         FLUIDSIM_GRAV_X , FLUIDSIM_GRAV_Y , FLUIDSIM_GRAV_Z ,
182         FLUIDSIM_VEL_X  , FLUIDSIM_VEL_Y  , FLUIDSIM_VEL_Z  ,
183         FLUIDSIM_ACTIVE,
184         FLUIDSIM_ATTR_FORCE_STR, FLUIDSIM_ATTR_FORCE_RADIUS,
185         FLUIDSIM_VEL_FORCE_STR, FLUIDSIM_VEL_FORCE_RADIUS,
186 };
187
188 int part_ar[PART_TOTIPO]= {
189         PART_EMIT_FREQ, PART_EMIT_LIFE, PART_EMIT_VEL, PART_EMIT_AVE, PART_EMIT_SIZE,
190         PART_AVE, PART_SIZE, PART_DRAG, PART_BROWN, PART_DAMP, PART_LENGTH, PART_CLUMP,
191     PART_GRAV_X, PART_GRAV_Y, PART_GRAV_Z, PART_KINK_AMP, PART_KINK_FREQ, PART_KINK_SHAPE,
192         PART_BB_TILT, PART_PD_FSTR, PART_PD_FFALL, PART_PD_FMAXD, PART_PD2_FSTR, PART_PD2_FFALL, PART_PD2_FMAXD
193 };
194
195 /* ************************** Data-Level Functions ************************* */
196
197 /* ---------------------- Freeing --------------------------- */
198
199 /* frees the ipo curve itself too */
200 void free_ipo_curve (IpoCurve *icu) 
201 {
202         if (icu == NULL) 
203                 return;
204         
205         if (icu->bezt) 
206                 MEM_freeN(icu->bezt);
207         if (icu->driver) 
208                 MEM_freeN(icu->driver);
209         
210         MEM_freeN(icu);
211 }
212
213 /* do not free ipo itself */
214 void free_ipo (Ipo *ipo)
215 {
216         IpoCurve *icu, *icn;
217         
218         if (ipo == NULL) 
219                 return;
220         
221         for (icu= ipo->curve.first; icu; icu= icn) {
222                 icn= icu->next;
223                 
224                 /* must remove the link before freeing, as the curve is freed too */
225                 BLI_remlink(&ipo->curve, icu);
226                 free_ipo_curve(icu);
227         }
228 }
229
230 /* ---------------------- Init --------------------------- */
231
232 /* on adding new ipos, or for empty views */
233 void ipo_default_v2d_cur (int blocktype, rctf *cur)
234 {
235         switch (blocktype) {
236         case ID_CA:
237                 cur->xmin= G.scene->r.sfra;
238                 cur->xmax= G.scene->r.efra;
239                 cur->ymin= 0.0;
240                 cur->ymax= 100.0;
241                 break;
242                 
243         case ID_MA: case ID_WO: case ID_LA: 
244         case ID_CU: case ID_CO:
245                 cur->xmin= (float)G.scene->r.sfra-0.1;
246                 cur->xmax= G.scene->r.efra;
247                 cur->ymin= (float)-0.1;
248                 cur->ymax= (float)+1.1;
249                 break;
250                 
251         case ID_TE:
252                 cur->xmin= (float)G.scene->r.sfra-0.1;
253                 cur->xmax= G.scene->r.efra;
254                 cur->ymin= (float)-0.1;
255                 cur->ymax= (float)+1.1;
256                 break;
257                 
258         case ID_SEQ:
259                 cur->xmin= -5.0;
260                 cur->xmax= 105.0;
261                 cur->ymin= (float)-0.1;
262                 cur->ymax= (float)+1.1;
263                 break;
264                 
265         case ID_KE:
266                 cur->xmin= (float)G.scene->r.sfra-0.1;
267                 cur->xmax= G.scene->r.efra;
268                 cur->ymin= (float)-0.1;
269                 cur->ymax= (float)+2.1;
270                 break;
271                 
272         default:        /* ID_OB and everything else */
273                 cur->xmin= G.scene->r.sfra;
274                 cur->xmax= G.scene->r.efra;
275                 cur->ymin= -5.0;
276                 cur->ymax= +5.0;
277                 break;
278         }
279 }
280
281 /* create a new IPO block (allocates the block) */
282 Ipo *add_ipo (char name[], int blocktype)
283 {
284         Ipo *ipo;
285         
286         ipo= alloc_libblock(&G.main->ipo, ID_IP, name);
287         ipo->blocktype= blocktype;
288         ipo_default_v2d_cur(blocktype, &ipo->cur);
289
290         return ipo;
291 }
292
293 /* ---------------------- Copy --------------------------- */
294
295 /* duplicate an IPO block and all its data  */
296 Ipo *copy_ipo (Ipo *src)
297 {
298         Ipo *dst;
299         IpoCurve *icu;
300         
301         if (src == NULL) 
302                 return NULL;
303         
304         dst= copy_libblock(src);
305         duplicatelist(&dst->curve, &src->curve);
306
307         for (icu= src->curve.first; icu; icu= icu->next) {
308                 icu->bezt= MEM_dupallocN(icu->bezt);
309                 
310                 if (icu->driver) 
311                         icu->driver= MEM_dupallocN(icu->driver);
312         }
313         
314         return dst;
315 }
316
317 /* ---------------------- Relink --------------------------- */
318
319 /* uses id->newid to match pointers with other copied data 
320  *      - called after single-user or other such
321  */
322 void ipo_idnew (Ipo *ipo)
323 {
324         if (ipo) {
325                 IpoCurve *icu;
326                 
327                 for (icu= ipo->curve.first; icu; icu= icu->next) {
328                         if (icu->driver)
329                                 ID_NEW(icu->driver->ob);
330                 }
331         }
332 }
333
334 /* --------------------- Find + Check ----------------------- */
335
336 /* find the IPO-curve within a given IPO-block with the adrcode of interest */
337 IpoCurve *find_ipocurve (Ipo *ipo, int adrcode)
338 {
339         if (ipo) {
340                 IpoCurve *icu;
341                 
342                 for (icu= ipo->curve.first; icu; icu= icu->next) {
343                         if (icu->adrcode == adrcode) 
344                                 return icu;
345                 }
346         }
347         return NULL;
348 }
349
350 /* return whether the given IPO block has a IPO-curve with the given adrcode */
351 short has_ipo_code(Ipo *ipo, int adrcode)
352 {
353         /* return success of faliure from trying to find such an IPO-curve */
354         return (find_ipocurve(ipo, adrcode) != NULL);
355 }
356
357 /* ---------------------- Make Local --------------------------- */
358
359
360 /* make the given IPO local (for Objects)
361  * - only lib users: do nothing
362  * - only local users: set flag
363  * - mixed: make copy
364  */
365 void make_local_obipo (Ipo *src)
366 {
367         Object *ob;
368         Ipo *dst;
369         int local=0, lib=0;
370         
371         /* check if only local and/or lib */
372         for (ob= G.main->object.first; ob; ob= ob->id.next) {
373                 if (ob->ipo == src) {
374                         if (ob->id.lib) lib= 1;
375                         else local= 1;
376                 }
377         }
378         
379         /* only local - set flag */
380         if (local && lib==0) {
381                 src->id.lib= 0;
382                 src->id.flag= LIB_LOCAL;
383                 new_id(0, (ID *)src, 0);
384         }
385         /* mixed: make copy */
386         else if (local && lib) {
387                 dst= copy_ipo(src);
388                 dst->id.us= 0;
389                 
390                 for (ob= G.main->object.first; ob; ob= ob->id.next) {
391                         if (ob->ipo == src) {
392                                 if (ob->id.lib == NULL) {
393                                         ob->ipo= dst;
394                                         dst->id.us++;
395                                         src->id.us--;
396                                 }
397                         }
398                 }
399         }
400 }
401
402 /* make the given IPO local (for Materials)
403  * - only lib users: do nothing
404  * - only local users: set flag
405  * - mixed: make copy
406  */
407 void make_local_matipo (Ipo *src)
408 {
409         Material *ma;
410         Ipo *dst;
411         int local=0, lib=0;
412         
413         /* check if only local and/or lib */
414         for (ma= G.main->mat.first; ma; ma= ma->id.next) {
415                 if (ma->ipo == src) {
416                         if (ma->id.lib) lib= 1;
417                         else local= 1;
418                 }
419         }
420         
421         /* only local - set flag */
422         if (local && lib==0) {
423                 src->id.lib= 0;
424                 src->id.flag= LIB_LOCAL;
425                 new_id(0, (ID *)src, 0);
426         }
427         /* mixed: make copy */
428         else if (local && lib) {
429                 dst= copy_ipo(src);
430                 dst->id.us= 0;
431                 
432                 for (ma= G.main->mat.first; ma; ma= ma->id.next) {
433                         if (ma->ipo == src) {
434                                 if (ma->id.lib == NULL) {
435                                         ma->ipo= dst;
436                                         dst->id.us++;
437                                         src->id.us--;
438                                 }
439                         }
440                 }
441         }
442 }
443
444 /* make the given IPO local (for ShapeKeys)
445  * - only lib users: do nothing
446  * - only local users: set flag
447  * - mixed: make copy
448  */
449 void make_local_keyipo (Ipo *src)
450 {
451         Key *key;
452         Ipo *dst;
453         int local=0, lib=0;
454         
455         /* check if only local and/or lib */
456         for (key= G.main->key.first; key; key= key->id.next) {
457                 if (key->ipo == src) {
458                         if (key->id.lib) lib= 1;
459                         else local= 1;
460                 }
461         }
462         
463         /* only local - set flag */
464         if (local && lib==0) {
465                 src->id.lib= 0;
466                 src->id.flag= LIB_LOCAL;
467                 new_id(0, (ID *)src, 0);
468         }
469         /* mixed: make copy */
470         else if (local && lib) {
471                 dst= copy_ipo(src);
472                 dst->id.us= 0;
473                 
474                 for (key= G.main->key.first; key; key= key->id.next) {
475                         if (key->ipo == src) {
476                                 if (key->id.lib == NULL) {
477                                         key->ipo= dst;
478                                         dst->id.us++;
479                                         src->id.us--;
480                                 }
481                         }
482                 }
483         }
484 }
485
486
487 /* generic call to make IPO's local */
488 void make_local_ipo (Ipo *ipo)
489 {
490         /* can't touch lib-linked data */
491         if (ipo->id.lib == NULL) 
492                 return;
493                 
494         /* with only one user, just set local flag */
495         if (ipo->id.us == 1) {
496                 ipo->id.lib= 0;
497                 ipo->id.flag= LIB_LOCAL;
498                 new_id(0, (ID *)ipo, 0);
499                 return;
500         }
501         
502         /* when more than 1 user, can only make local for certain blocktypes */
503         switch (ipo->blocktype) {
504                 case ID_OB:
505                         make_local_obipo(ipo);
506                         break;
507                 case ID_MA:
508                         make_local_matipo(ipo);
509                         break;
510                 case ID_KE:
511                         make_local_keyipo(ipo);
512                         break;
513         }
514 }
515
516 /* ***************************** Keyframe Column Tools ********************************* */
517
518 /* add a BezTriple to a column */
519 void add_to_cfra_elem(ListBase *lb, BezTriple *bezt)
520 {
521         CfraElem *ce, *cen;
522         
523         for (ce= lb->first; ce; ce= ce->next) {
524                 /* double key? */
525                 if (ce->cfra == bezt->vec[1][0]) {
526                         if (bezt->f2 & SELECT) ce->sel= bezt->f2;
527                         return;
528                 }
529                 /* should key be inserted before this column? */
530                 else if (ce->cfra > bezt->vec[1][0]) break;
531         }
532         
533         /* create a new column */
534         cen= MEM_callocN(sizeof(CfraElem), "add_to_cfra_elem"); 
535         if (ce) BLI_insertlinkbefore(lb, ce, cen);
536         else BLI_addtail(lb, cen);
537
538         cen->cfra= bezt->vec[1][0];
539         cen->sel= bezt->f2;
540 }
541
542 /* make a list of keyframe 'columns' in an IPO block */
543 void make_cfra_list (Ipo *ipo, ListBase *elems)
544 {
545         IpoCurve *icu;
546         BezTriple *bezt;
547         int a;
548         
549         for (icu= ipo->curve.first; icu; icu= icu->next) {
550                 if (icu->flag & IPO_VISIBLE) {
551                         /* ... removed old checks for adrcode types from here ...
552                          *      - (was this used for IpoKeys in the past?)
553                          */
554                         
555                         bezt= icu->bezt;
556                         if (bezt) {
557                                 for (a=0; a < icu->totvert; a++, bezt++) {
558                                         add_to_cfra_elem(elems, bezt);
559                                 }
560                         }
561                 }
562         }
563 }
564
565 /* ***************************** Timing Stuff ********************************* */
566
567 /* This (evil) function is needed to cope with two legacy Blender rendering features
568  * mblur (motion blur that renders 'subframes' and blurs them together), and fields 
569  * rendering. Thus, the use of ugly globals from object.c
570  */
571 // BAD... EVIL... JUJU...!!!!
572 float frame_to_float (int cfra)         /* see also bsystem_time in object.c */
573 {
574         extern float bluroffs;  /* bad stuff borrowed from object.c */
575         extern float fieldoffs;
576         float ctime;
577         
578         ctime= (float)cfra;
579         ctime+= bluroffs+fieldoffs;
580         ctime*= G.scene->r.framelen;
581         
582         return ctime;
583 }
584
585 /* ***************************** IPO Curve Sanity ********************************* */
586 /* The functions here are used in various parts of Blender, usually after some editing
587  * of keyframe data has occurred. They ensure that keyframe data is properly ordered and
588  * that the handles are correctly 
589  */
590
591 /* This function recalculates the handles of an IPO-Curve 
592  * If the BezTriples have been rearranged, sort them first before using this.
593  */
594 void calchandles_ipocurve (IpoCurve *icu)
595 {
596         BezTriple *bezt, *prev, *next;
597         int a= icu->totvert;
598
599         /* Error checking:
600          *      - need at least two points
601          *      - need bezier keys
602          *      - only bezier-interpolation has handles (for now)
603          */
604         if (ELEM(NULL, icu, icu->bezt) || (a < 2) || ELEM(icu->ipo, IPO_CONST, IPO_LIN)) 
605                 return;
606         
607         /* get initial pointers */
608         bezt= icu->bezt;
609         prev= NULL;
610         next= (bezt + 1);
611         
612         /* loop over all beztriples, adjusting handles */
613         while (a--) {
614                 /* clamp timing of handles to be on either side of beztriple */
615                 if (bezt->vec[0][0] > bezt->vec[1][0]) bezt->vec[0][0]= bezt->vec[1][0];
616                 if (bezt->vec[2][0] < bezt->vec[1][0]) bezt->vec[2][0]= bezt->vec[1][0];
617                 
618                 /* calculate autohandles */
619                 if (icu->flag & IPO_AUTO_HORIZ) 
620                         calchandleNurb(bezt, prev, next, 2);    /* 2==special autohandle && keep extrema horizontal */
621                 else
622                         calchandleNurb(bezt, prev, next, 1);    /* 1==special autohandle */
623                 
624                 /* for automatic ease in and out */
625                 if ((bezt->h1==HD_AUTO) && (bezt->h2==HD_AUTO)) {
626                         /* only do this on first or last beztriple */
627                         if ((a==0) || (a==icu->totvert-1)) {
628                                 /* set both handles to have same horizontal value as keyframe */
629                                 if (icu->extrap==IPO_HORIZ) {
630                                         bezt->vec[0][1]= bezt->vec[2][1]= bezt->vec[1][1];
631                                 }
632                         }
633                 }
634                 
635                 /* advance pointers for next iteration */
636                 prev= bezt;
637                 if (a == 1) next= NULL;
638                 else next++;
639                 bezt++;
640         }
641 }
642
643 /* Use when IPO-Curve with handles has changed
644  * It treats all BezTriples with the following rules:
645  *  - PHASE 1: do types have to be altered?
646  *              -> Auto handles: become aligned when selection status is NOT(000 || 111)
647  *              -> Vector handles: become 'nothing' when (one half selected AND other not)
648  *  - PHASE 2: recalculate handles
649 */
650 void testhandles_ipocurve (IpoCurve *icu)
651 {
652         BezTriple *bezt;
653         int a;
654
655         /* only beztriples have handles (bpoints don't though) */
656         if (ELEM(NULL, icu, icu->bezt))
657                 return;
658         
659         /* loop over beztriples */
660         for (a=0, bezt=icu->bezt; a < icu->totvert; a++, bezt++) {
661                 short flag= 0;
662                 
663                 /* flag is initialised as selection status
664                  * of beztriple control-points (labelled 0,1,2)
665                  */
666                 if (bezt->f1 & SELECT) flag |= (1<<0); // == 1
667                 if (bezt->f2 & SELECT) flag |= (1<<1); // == 2
668                 if (bezt->f3 & SELECT) flag |= (1<<2); // == 4
669                 
670                 /* one or two handles selected only */
671                 if (ELEM(flag, 0, 7)==0) {
672                         /* auto handles become aligned */
673                         if (bezt->h1==HD_AUTO)
674                                 bezt->h1= HD_ALIGN;
675                         if(bezt->h2==HD_AUTO)
676                                 bezt->h2= HD_ALIGN;
677                         
678                         /* vector handles become 'free' when only one half selected */
679                         if(bezt->h1==HD_VECT) {
680                                 /* only left half (1 or 2 or 1+2) */
681                                 if (flag < 4) 
682                                         bezt->h1= 0;
683                         }
684                         if(bezt->h2==HD_VECT) {
685                                 /* only right half (4 or 2+4) */
686                                 if (flag > 3) 
687                                         bezt->h2= 0;
688                         }
689                 }
690         }
691
692         /* recalculate handles */
693         calchandles_ipocurve(icu);
694 }
695
696 /* This function sorts BezTriples so that they are arranged in chronological order,
697  * as tools working on IPO-Curves expect that the BezTriples are in order.
698  */
699 void sort_time_ipocurve(IpoCurve *icu)
700 {
701         short ok= 1;
702         
703         /* keep adjusting order of beztriples until nothing moves (bubble-sort) */
704         while (ok) {
705                 ok= 0;
706                 
707                 /* currently, will only be needed when there are beztriples */
708                 if (icu->bezt) {
709                         BezTriple *bezt;
710                         int a;
711                         
712                         /* loop over ALL points to adjust position in array and recalculate handles */
713                         for (a=0, bezt=icu->bezt; a < icu->totvert; a++, bezt++) {
714                                 /* check if thee's a next beztriple which we could try to swap with current */
715                                 if (a < (icu->totvert-1)) {
716                                         /* swap if one is after the other (and indicate that order has changed) */
717                                         if (bezt->vec[1][0] > (bezt+1)->vec[1][0]) {
718                                                 SWAP(BezTriple, *bezt, *(bezt+1));
719                                                 ok= 1;
720                                         }
721                                         
722                                         /* if either one of both of the points exceeds crosses over the keyframe time... */
723                                         if ( (bezt->vec[0][0] > bezt->vec[1][0]) && (bezt->vec[2][0] < bezt->vec[1][0]) ) {
724                                                 /* swap handles if they have switched sides for some reason */
725                                                 SWAP(float, bezt->vec[0][0], bezt->vec[2][0]);
726                                                 SWAP(float, bezt->vec[0][1], bezt->vec[2][1]);
727                                         }
728                                         else {
729                                                 /* clamp handles */
730                                                 if (bezt->vec[0][0] > bezt->vec[1][0]) 
731                                                         bezt->vec[0][0]= bezt->vec[1][0];
732                                                 if (bezt->vec[2][0] < bezt->vec[1][0]) 
733                                                         bezt->vec[2][0]= bezt->vec[1][0];
734                                         }
735                                 }
736                         }
737                 }
738         }
739 }
740
741 /* This function tests if any BezTriples are out of order, thus requiring a sort */
742 int test_time_ipocurve (IpoCurve *icu)
743 {
744         int a;
745         
746         /* currently, only need to test beztriples */
747         if (icu->bezt) {
748                 BezTriple *bezt;
749                 
750                 /* loop through all beztriples, stopping when one exceeds the one after it */
751                 for (a=0, bezt= icu->bezt; a < (icu->totvert - 1); a++, bezt++) {
752                         if (bezt->vec[1][0] > (bezt+1)->vec[1][0])
753                                 return 1;
754                 }
755         }
756         
757         /* none need any swapping */
758         return 0;
759 }
760
761 /* --------- */
762
763 /* The total length of the handles is not allowed to be more
764  * than the horizontal distance between (v1-v4).
765  * This is to prevent curve loops.
766 */
767 void correct_bezpart (float *v1, float *v2, float *v3, float *v4)
768 {
769         float h1[2], h2[2], len1, len2, len, fac;
770         
771         /* calculate handle deltas */
772         h1[0]= v1[0]-v2[0];
773         h1[1]= v1[1]-v2[1];
774         h2[0]= v4[0]-v3[0];
775         h2[1]= v4[1]-v3[1];
776         
777         /* calculate distances: 
778          *      - len   = span of time between keyframes 
779          *      - len1  = length of handle of start key
780          *      - len2  = length of handle of end key
781          */
782         len= v4[0]- v1[0];
783         len1= (float)fabs(h1[0]);
784         len2= (float)fabs(h2[0]);
785         
786         /* if the handles have no length, no need to do any corrections */
787         if ((len1+len2) == 0.0) 
788                 return;
789                 
790         /* the two handles cross over each other, so force them
791          * apart using the proportion they overlap 
792          */
793         if ((len1+len2) > len) {
794                 fac= len/(len1+len2);
795                 
796                 v2[0]= (v1[0]-fac*h1[0]);
797                 v2[1]= (v1[1]-fac*h1[1]);
798                 
799                 v3[0]= (v4[0]-fac*h2[0]);
800                 v3[1]= (v4[1]-fac*h2[1]);
801         }
802 }
803
804 #if 0 // TODO: enable when we have per-segment interpolation
805 /* This function sets the interpolation mode for an entire Ipo-Curve. 
806  * It is primarily used for patching old files, but is also used in the interface
807  * to make sure that all segments of the curve use the same interpolation.
808  */
809 void set_interpolation_ipocurve (IpoCurve *icu, short ipo)
810 {
811         BezTriple *bezt;
812         int a;
813         
814         /* validate arguments */
815         if (icu == NULL) return;
816         if (ELEM3(ipo, IPO_CONST, IPO_LIN, IPO_BEZ)==0) return;
817
818         /* set interpolation mode for whole curve */
819         icu->ipo= ipo;
820         
821         /* set interpolation mode of all beztriples */
822         for (a=0, bezt=icu->bezt; a<icu->totvert; a++, bezt++)
823                 bezt->ipo= ipo;
824 }
825 #endif // TODO: enable when we have per-segment interpolation
826
827 /* ***************************** Curve Calculations ********************************* */
828
829 /* find root/zero */
830 int findzero (float x, float q0, float q1, float q2, float q3, float *o)
831 {
832         double c0, c1, c2, c3, a, b, c, p, q, d, t, phi;
833         int nr= 0;
834
835         c0= q0 - x;
836         c1= 3 * (q1 - q0);
837         c2= 3 * (q0 - 2*q1 + q2);
838         c3= q3 - q0 + 3 * (q1 - q2);
839         
840         if (c3 != 0.0) {
841                 a= c2/c3;
842                 b= c1/c3;
843                 c= c0/c3;
844                 a= a/3;
845                 
846                 p= b/3 - a*a;
847                 q= (2*a*a*a - a*b + c) / 2;
848                 d= q*q + p*p*p;
849                 
850                 if (d > 0.0) {
851                         t= sqrt(d);
852                         o[0]= (float)(Sqrt3d(-q+t) + Sqrt3d(-q-t) - a);
853                         
854                         if ((o[0] >= SMALL) && (o[0] <= 1.000001)) return 1;
855                         else return 0;
856                 }
857                 else if (d == 0.0) {
858                         t= Sqrt3d(-q);
859                         o[0]= (float)(2*t - a);
860                         
861                         if ((o[0] >= SMALL) && (o[0] <= 1.000001)) nr++;
862                         o[nr]= (float)(-t-a);
863                         
864                         if ((o[nr] >= SMALL) && (o[nr] <= 1.000001)) return nr+1;
865                         else return nr;
866                 }
867                 else {
868                         phi= acos(-q / sqrt(-(p*p*p)));
869                         t= sqrt(-p);
870                         p= cos(phi/3);
871                         q= sqrt(3 - 3*p*p);
872                         o[0]= (float)(2*t*p - a);
873                         
874                         if ((o[0] >= SMALL) && (o[0] <= 1.000001)) nr++;
875                         o[nr]= (float)(-t * (p + q) - a);
876                         
877                         if ((o[nr] >= SMALL) && (o[nr] <= 1.000001)) nr++;
878                         o[nr]= (float)(-t * (p - q) - a);
879                         
880                         if ((o[nr] >= SMALL) && (o[nr] <= 1.000001)) return nr+1;
881                         else return nr;
882                 }
883         }
884         else {
885                 a=c2;
886                 b=c1;
887                 c=c0;
888                 
889                 if (a != 0.0) {
890                         // discriminant
891                         p= b*b - 4*a*c;
892                         
893                         if (p > 0) {
894                                 p= sqrt(p);
895                                 o[0]= (float)((-b-p) / (2 * a));
896                                 
897                                 if ((o[0] >= SMALL) && (o[0] <= 1.000001)) nr++;
898                                 o[nr]= (float)((-b+p)/(2*a));
899                                 
900                                 if ((o[nr] >= SMALL) && (o[nr] <= 1.000001)) return nr+1;
901                                 else return nr;
902                         }
903                         else if (p == 0) {
904                                 o[0]= (float)(-b / (2 * a));
905                                 if ((o[0] >= SMALL) && (o[0] <= 1.000001)) return 1;
906                                 else return 0;
907                         }
908                 }
909                 else if (b != 0.0) {
910                         o[0]= (float)(-c/b);
911                         
912                         if ((o[0] >= SMALL) && (o[0] <= 1.000001)) return 1;
913                         else return 0;
914                 }
915                 else if (c == 0.0) {
916                         o[0]= 0.0;
917                         return 1;
918                 }
919                 
920                 return 0;       
921         }
922 }
923
924 void berekeny (float f1, float f2, float f3, float f4, float *o, int b)
925 {
926         float t, c0, c1, c2, c3;
927         int a;
928
929         c0= f1;
930         c1= 3.0f * (f2 - f1);
931         c2= 3.0f * (f1 - 2.0f*f2 + f3);
932         c3= f4 - f1 + 3.0f * (f2 - f3);
933         
934         for (a=0; a < b; a++) {
935                 t= o[a];
936                 o[a]= c0 + t*c1 + t*t*c2 + t*t*t*c3;
937         }
938 }
939
940 void berekenx (float *f, float *o, int b)
941 {
942         float t, c0, c1, c2, c3;
943         int a;
944
945         c0= f[0];
946         c1= 3 * (f[3] - f[0]);
947         c2= 3 * (f[0] - 2*f[3] + f[6]);
948         c3= f[9] - f[0] + 3 * (f[3] - f[6]);
949         
950         for (a=0; a < b; a++) {
951                 t= o[a];
952                 o[a]= c0 + t*c1 + t*t*c2 + t*t*t*c3;
953         }
954 }
955
956 /* ***************************** IPO - Calculations ********************************* */
957
958 /* ---------------------- Curve Evaluation --------------------------- */
959
960 /* helper function for evaluating drivers: 
961  *      - we need the local transform = current transform - (parent transform + bone transform)
962  *      - (local transform is on action channel level)
963  */
964 static void posechannel_get_local_transform (bPoseChannel *pchan, float loc[], float eul[], float size[])
965 {
966         float parmat[4][4], offs_bone[4][4], imat[4][4];
967         float diff_mat[4][4];
968         
969         /* get first the parent + bone transform in parmat */
970         if (pchan->parent) {
971                 /* bone transform itself */
972                 Mat4CpyMat3(offs_bone, pchan->bone->bone_mat);
973                 
974                 /* The bone's root offset (is in the parent's coordinate system) */
975                 VECCOPY(offs_bone[3], pchan->bone->head);
976                 
977                 /* Get the length translation of parent (length along y axis) */
978                 offs_bone[3][1]+= pchan->parent->bone->length;
979                 
980                 Mat4MulSerie(parmat, pchan->parent->pose_mat, offs_bone, NULL, NULL, NULL, NULL, NULL, NULL);
981                 
982                 /* invert it */
983                 Mat4Invert(imat, parmat);
984         }
985         else {
986                 Mat4CpyMat3(offs_bone, pchan->bone->bone_mat);
987                 VECCOPY(offs_bone[3], pchan->bone->head);
988                 
989                 /* invert it */
990                 Mat4Invert(imat, offs_bone);
991         }
992         
993         /* difference: current transform - (parent transform + bone transform)  */
994         Mat4MulMat4(diff_mat, pchan->pose_mat, imat);
995
996         /* extract relevant components */
997         if (loc)
998                 VECCOPY(loc, diff_mat[3]);
999         if (eul)
1000                 Mat4ToEul(diff_mat, eul);
1001         if (size)
1002                 Mat4ToSize(diff_mat, size);
1003 }
1004
1005 /* evaluate an IPO-driver to get a 'time' value to use instead of "ipotime"
1006  *      - "ipotime" is the frame at which IPO-curve is being evaluated
1007  *      - has to return a float value 
1008  */
1009 static float eval_driver (IpoDriver *driver, float ipotime)
1010 {
1011         /* currently, drivers are either PyDrivers (evaluating a PyExpression, or Object/Pose-Channel transforms) */
1012         if (driver->type == IPO_DRIVER_TYPE_PYTHON) {
1013                 /* check for empty or invalid expression */
1014                 if ( (driver->name[0] == '\0') ||
1015                          (driver->flag & IPO_DRIVER_FLAG_INVALID) )
1016                 {
1017                         return 0.0f;
1018                 }
1019                 
1020                 /* this evaluates the expression using Python,and returns its result:
1021                  *      - on errors it reports, then returns 0.0f
1022                  */
1023                 return BPY_pydriver_eval(driver);
1024         }
1025         else {
1026                 Object *ob= driver->ob;
1027                 
1028                 /* must have an object to evaluate */
1029                 if (ob == NULL) 
1030                         return 0.0f;
1031                         
1032                 /* if a proxy, use the proxy source*/
1033                 if (ob->proxy_from)
1034                         ob= ob->proxy_from;
1035                 
1036                 /* use given object as driver */
1037                 if (driver->blocktype == ID_OB) {
1038                         /* depsgraph failure: ob ipos are calculated in where_is_object, this might get called too late */
1039                         if ((ob->ipo) && (ob->ctime != ipotime)) {
1040                                 /* calculate the value of relevant channel on the Object, but do not write the value
1041                                  * calculated on to the Object but onto "ipotime" instead
1042                                  */
1043                                 calc_ipo_spec(ob->ipo, driver->adrcode, &ipotime);
1044                                 return ipotime;
1045                         }
1046                         
1047                         /* return the value of the relevant channel */
1048                         switch (driver->adrcode) {
1049                         case OB_LOC_X:
1050                                 return ob->loc[0];
1051                         case OB_LOC_Y:
1052                                 return ob->loc[1];
1053                         case OB_LOC_Z:
1054                                 return ob->loc[2];
1055                         case OB_ROT_X:  /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */
1056                                 return ob->rot[0]/(M_PI_2/9.0);
1057                         case OB_ROT_Y:  /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */
1058                                 return ob->rot[1]/(M_PI_2/9.0);
1059                         case OB_ROT_Z:  /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */
1060                                 return ob->rot[2]/(M_PI_2/9.0);
1061                         case OB_SIZE_X:
1062                                 return ob->size[0];
1063                         case OB_SIZE_Y:
1064                                 return ob->size[1];
1065                         case OB_SIZE_Z:
1066                                 return ob->size[2];
1067                         }
1068                 }
1069                 
1070                 /* use given pose-channel as driver */
1071                 else {  /* ID_AR */
1072                         bPoseChannel *pchan= get_pose_channel(ob->pose, driver->name);
1073                         
1074                         /* must have at least 1 bone to use */
1075                         if (pchan && pchan->bone) {
1076                                 /* rotation difference is not a simple driver (i.e. value drives value), but the angle between 2 bones is driving stuff... 
1077                                  *      - the name of the second pchan is also stored in driver->name, but packed after the other one by DRIVER_NAME_OFFS chars
1078                                  */
1079                                 if (driver->adrcode == OB_ROT_DIFF) {
1080                                         bPoseChannel *pchan2= get_pose_channel(ob->pose, driver->name+DRIVER_NAME_OFFS);
1081                                         
1082                                         if (pchan2 && pchan2->bone) {
1083                                                 float q1[4], q2[4], quat[4], angle;
1084                                                 
1085                                                 Mat4ToQuat(pchan->pose_mat, q1);
1086                                                 Mat4ToQuat(pchan2->pose_mat, q2);
1087                                                 
1088                                                 QuatInv(q1);
1089                                                 QuatMul(quat, q1, q2);
1090                                                 angle = 2.0f * (saacos(quat[0]));
1091                                                 angle= ABS(angle);
1092                                                 
1093                                                 return (angle > M_PI) ? ((2.0f * M_PI) - angle) : (angle);
1094                                         }
1095                                 }
1096                                 
1097                                 /* standard driver */
1098                                 else {
1099                                         float loc[3], eul[3], size[3];
1100                                         
1101                                         /* retrieve local transforms to return 
1102                                          *      - we use eulers here NOT quats, so that Objects can be driven by bones easily
1103                                          *        also, this way is more understandable for users
1104                                          */
1105                                         posechannel_get_local_transform(pchan, loc, eul, size);
1106                                         
1107                                         switch (driver->adrcode) {
1108                                         case OB_LOC_X:
1109                                                 return loc[0];
1110                                         case OB_LOC_Y:
1111                                                 return loc[1];
1112                                         case OB_LOC_Z:
1113                                                 return loc[2];
1114                                         case OB_ROT_X: /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */
1115                                                 return eul[0]/(M_PI_2/9.0);
1116                                         case OB_ROT_Y: /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */
1117                                                 return eul[1]/(M_PI_2/9.0);
1118                                         case OB_ROT_Z: /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */
1119                                                 return eul[2]/(M_PI_2/9.0);
1120                                         case OB_SIZE_X:
1121                                                 return size[0];
1122                                         case OB_SIZE_Y:
1123                                                 return size[1];
1124                                         case OB_SIZE_Z:
1125                                                 return size[2];
1126                                         }
1127                                 }
1128                         }
1129                 }
1130         }       
1131         
1132         /* return 0.0f, as couldn't find relevant data to use */
1133         return 0.0f;
1134 }
1135
1136 /* evaluate and return the value of the given IPO-curve at the specified frame ("evaltime") */
1137 float eval_icu(IpoCurve *icu, float evaltime) 
1138 {
1139         float cvalue = 0.0f;
1140         
1141         /* if there is a driver, evaluate it to find value to use as "evaltime" 
1142          *      - this value will also be returned as the value of the 'curve', if there are no keyframes
1143          */
1144         if (icu->driver) {
1145                 /* ipotime now serves as input for the curve */
1146                 evaltime= cvalue= eval_driver(icu->driver, evaltime);
1147         }
1148         
1149         /* there are keyframes (in the form of BezTriples) which can be interpolated between */
1150         if (icu->bezt) {
1151                 /* get pointers */
1152                 BezTriple *bezt, *prevbezt, *lastbezt;
1153                 float v1[2], v2[2], v3[2], v4[2], opl[32], dx, fac;
1154                 float cycdx, cycdy, ofs, cycyofs= 0.0;
1155                 int a, b;
1156                 
1157                 /* get pointers */
1158                 a= icu->totvert-1;
1159                 prevbezt= icu->bezt;
1160                 bezt= prevbezt+1;
1161                 lastbezt= prevbezt + a;
1162                 
1163                 /* extrapolation mode is 'cyclic' - find relative place within a cycle */
1164                 if (icu->extrap & IPO_CYCL) {
1165                         /* ofs is start frame of cycle */
1166                         ofs= prevbezt->vec[1][0];
1167                         
1168                         /* calculate period and amplitude (total height) of a cycle */
1169                         cycdx= lastbezt->vec[1][0] - prevbezt->vec[1][0];
1170                         cycdy= lastbezt->vec[1][1] - prevbezt->vec[1][1];
1171                         
1172                         /* cycle occurs over some period of time (cycdx should be positive all the time) */
1173                         if (cycdx) {
1174                                 /* check if 'cyclic extrapolation', and thus calculate y-offset for this cycle
1175                                  *      - IPO_CYCLX = (IPO_CYCL + IPO_DIR)
1176                                  */
1177                                 if (icu->extrap & IPO_DIR) {
1178                                         cycyofs = (float)floor((evaltime - ofs) / cycdx);
1179                                         cycyofs *= cycdy;
1180                                 }
1181                                 
1182                                 /* calculate where in the cycle we are (overwrite evaltime to reflect this) */
1183                                 evaltime= (float)(fmod(evaltime-ofs, cycdx) + ofs);
1184                                 if (evaltime < ofs) evaltime += cycdx;
1185                         }
1186                 }
1187                 
1188                 /* evaluation time at or past endpoints? */
1189                 // TODO: for per-bezt interpolation, replace all icu->ipo with (bezt)->ipo
1190                 if (prevbezt->vec[1][0] >= evaltime) {
1191                         /* before or on first keyframe */
1192                         if ((icu->extrap & IPO_DIR) && (icu->ipo != IPO_CONST)) {
1193                                 /* linear or bezier interpolation */
1194                                 if (icu->ipo==IPO_LIN) {
1195                                         /* Use the next center point instead of our own handle for
1196                                          * linear interpolated extrapolate 
1197                                          */
1198                                         if (icu->totvert == 1) 
1199                                                 cvalue= prevbezt->vec[1][1];
1200                                         else {
1201                                                 bezt = prevbezt+1;
1202                                                 dx= prevbezt->vec[1][0] - evaltime;
1203                                                 fac= bezt->vec[1][0] - prevbezt->vec[1][0];
1204                                                 
1205                                                 /* prevent division by zero */
1206                                                 if (fac) {
1207                                                         fac= (bezt->vec[1][1] - prevbezt->vec[1][1]) / fac;
1208                                                         cvalue= prevbezt->vec[1][1] - (fac * dx);
1209                                                 }
1210                                                 else 
1211                                                         cvalue= prevbezt->vec[1][1];
1212                                         }
1213                                 } 
1214                                 else {
1215                                         /* Use the first handle (earlier) of first BezTriple to calculate the
1216                                          * gradient and thus the value of the curve at evaltime
1217                                          */
1218                                         dx= prevbezt->vec[1][0] - evaltime;
1219                                         fac= prevbezt->vec[1][0] - prevbezt->vec[0][0];
1220                                         
1221                                         /* prevent division by zero */
1222                                         if (fac) {
1223                                                 fac= (prevbezt->vec[1][1] - prevbezt->vec[0][1]) / fac;
1224                                                 cvalue= prevbezt->vec[1][1] - (fac * dx);
1225                                         }
1226                                         else 
1227                                                 cvalue= prevbezt->vec[1][1];
1228                                 }
1229                         }
1230                         else {
1231                                 /* constant (IPO_HORIZ) extrapolation or constant interpolation, 
1232                                  * so just extend first keyframe's value 
1233                                  */
1234                                 cvalue= prevbezt->vec[1][1];
1235                         }
1236                 }
1237                 else if (lastbezt->vec[1][0] <= evaltime) {
1238                         /* after or on last keyframe */
1239                         if( (icu->extrap & IPO_DIR) && (icu->ipo != IPO_CONST)) {
1240                                 /* linear or bezier interpolation */
1241                                 if (icu->ipo==IPO_LIN) {
1242                                         /* Use the next center point instead of our own handle for
1243                                          * linear interpolated extrapolate 
1244                                          */
1245                                         if (icu->totvert == 1) 
1246                                                 cvalue= lastbezt->vec[1][1];
1247                                         else {
1248                                                 prevbezt = lastbezt - 1;
1249                                                 dx= evaltime - lastbezt->vec[1][0];
1250                                                 fac= lastbezt->vec[1][0] - prevbezt->vec[1][0];
1251                                                 
1252                                                 /* prevent division by zero */
1253                                                 if (fac) {
1254                                                         fac= (lastbezt->vec[1][1] - prevbezt->vec[1][1]) / fac;
1255                                                         cvalue= lastbezt->vec[1][1] + (fac * dx);
1256                                                 }
1257                                                 else 
1258                                                         cvalue= lastbezt->vec[1][1];
1259                                         }
1260                                 } 
1261                                 else {
1262                                         /* Use the gradient of the second handle (later) of last BezTriple to calculate the
1263                                          * gradient and thus the value of the curve at evaltime
1264                                          */
1265                                         dx= evaltime - lastbezt->vec[1][0];
1266                                         fac= lastbezt->vec[2][0] - lastbezt->vec[1][0];
1267                                         
1268                                         /* prevent division by zero */
1269                                         if (fac) {
1270                                                 fac= (lastbezt->vec[2][1] - lastbezt->vec[1][1]) / fac;
1271                                                 cvalue= lastbezt->vec[1][1] + (fac * dx);
1272                                         }
1273                                         else 
1274                                                 cvalue= lastbezt->vec[1][1];
1275                                 }
1276                         }
1277                         else {
1278                                 /* constant (IPO_HORIZ) extrapolation or constant interpolation, 
1279                                  * so just extend last keyframe's value 
1280                                  */
1281                                 cvalue= lastbezt->vec[1][1];
1282                         }
1283                 }
1284                 else {
1285                         /* evaltime occurs somewhere in the middle of the curve */
1286                         // TODO: chould be optimised by using a binary search instead???
1287                         for (a=0; prevbezt && bezt && (a < icu->totvert-1); a++, prevbezt=bezt, bezt++) {  
1288                                 /* evaltime occurs within the interval defined by these two keyframes */
1289                                 if ((prevbezt->vec[1][0] <= evaltime) && (bezt->vec[1][0] >= evaltime)) {
1290                                         /* value depends on interpolation mode */
1291                                         if (icu->ipo == IPO_CONST) {
1292                                                 /* constant (evaltime not relevant, as no interpolation needed) */
1293                                                 cvalue= prevbezt->vec[1][1];
1294                                                 
1295                                                 /* value found already, so no need to keep looping */
1296                                                 break;
1297                                         }
1298                                         else if (icu->ipo == IPO_LIN) {
1299                                                 /* linear - interpolate between values of the two keyframes */
1300                                                 fac= bezt->vec[1][0] - prevbezt->vec[1][0];
1301                                                 
1302                                                 /* prevent division by zero */
1303                                                 if (fac) {
1304                                                         fac= (evaltime - prevbezt->vec[1][0])/fac;
1305                                                         cvalue= prevbezt->vec[1][1] + (fac * (bezt->vec[1][1] - prevbezt->vec[1][1]));
1306                                                 }
1307                                                 else
1308                                                         cvalue= prevbezt->vec[1][1];
1309                                                         
1310                                                 /* value found already, so no need to keep looping */
1311                                                 break;
1312                                         }
1313                                         else {
1314                                                 /* bezier interpolation */
1315                                                         /* v1,v2 are the first keyframe and its 2nd handle */
1316                                                 v1[0]= prevbezt->vec[1][0];
1317                                                 v1[1]= prevbezt->vec[1][1];
1318                                                 v2[0]= prevbezt->vec[2][0];
1319                                                 v2[1]= prevbezt->vec[2][1];
1320                                                         /* v3,v4 are the last keyframe's 1st handle + the last keyframe */
1321                                                 v3[0]= bezt->vec[0][0];
1322                                                 v3[1]= bezt->vec[0][1];
1323                                                 v4[0]= bezt->vec[1][0];
1324                                                 v4[1]= bezt->vec[1][1];
1325                                                 
1326                                                 /* adjust handles so that they don't overlap (forming a loop) */
1327                                                 correct_bezpart(v1, v2, v3, v4);
1328                                                 
1329                                                 /* try to get a value for this position - if failure, try another set of points */
1330                                                 b= findzero(evaltime, v1[0], v2[0], v3[0], v4[0], opl);
1331                                                 if (b) {
1332                                                         berekeny(v1[1], v2[1], v3[1], v4[1], opl, 1);
1333                                                         cvalue= opl[0];
1334                                                         break;
1335                                                 }
1336                                         }
1337                                 }
1338                         }
1339                 }
1340                 
1341                 /* apply y-offset (for 'cyclic extrapolation') to calculated value */
1342                 cvalue+= cycyofs;
1343         }
1344         
1345         /* clamp evaluated value to lie within allowable value range for this channel */
1346         if (icu->ymin < icu->ymax) {
1347                 CLAMP(cvalue, icu->ymin, icu->ymax);
1348         }
1349         
1350         /* return evaluated value */
1351         return cvalue;
1352 }
1353
1354 /* ------------------- IPO-Block/Curve Calculation - General API ----------------------- */
1355
1356 /* calculate the value of the given IPO-curve at the current frame, and set its curval */
1357 void calc_icu (IpoCurve *icu, float ctime)
1358 {
1359         /* calculate and set curval (evaluates driver too) */
1360         icu->curval= eval_icu(icu, ctime);
1361 }
1362
1363 /* calculate for the current frame, all IPO-curves in IPO-block that can be evaluated 
1364  *      - icu->curval is set for all IPO-curves which are evaluated!
1365  */
1366 void calc_ipo (Ipo *ipo, float ctime)
1367 {
1368         IpoCurve *icu;
1369         
1370         /* if there is no IPO block to evaluate, or whole block is "muted" */
1371         if (ipo == NULL) return;
1372         if (ipo->muteipo) return;
1373         
1374         /* loop over all curves */
1375         for (icu= ipo->curve.first; icu; icu= icu->next) {
1376                 /* only evaluated curve if allowed to:
1377                  *      - Muted channels should not be evaluated as they shouldn't have any effect 
1378                  *              --> user explictly turned them off!
1379                  *      - Drivers should be evaluated at all updates
1380                  *              --> TODO Note: drivers should be separated from standard channels
1381                  *      - IPO_LOCK is not set, as it is set by some internal mechanisms to prevent
1382                  *              IPO-curve from overwriting data (currently only used for IPO-Record). 
1383                  */
1384                 if ((icu->driver) || (icu->flag & IPO_LOCK)==0) { 
1385                         if ((icu->flag & IPO_MUTE)==0)
1386                                 calc_icu(icu, ctime);
1387                 }
1388         }
1389 }
1390
1391 /* ------------------- IPO-Block/Curve Calculation - Special Hacks ----------------------- */
1392
1393 /* Calculate and return the value of the 'Time' Ipo-Curve from an Object,
1394  * OR return the current time if not found
1395  *      - used in object.c -> bsystem_time() 
1396  */
1397 float calc_ipo_time (Ipo *ipo, float ctime)
1398 {
1399         /* only Time IPO from Object IPO-blocks are relevant */
1400         if ((ipo) && (ipo->blocktype == ID_OB)) {
1401                 IpoCurve *icu= find_ipocurve(ipo, OB_TIME);
1402                 
1403                 /* only calculate (and set icu->curval) for time curve */
1404                 if (icu) {
1405                         calc_icu(icu, ctime);
1406                         return (10.0f * icu->curval);
1407                 }
1408         }
1409         
1410         /* no appropriate time-curve found */
1411         return ctime;
1412 }
1413
1414 /* Evaluate the specified channel in the given IPO block on the specified frame (ctime),
1415  * writing the value into that channel's icu->curval, but ALSO dumping it in ctime.
1416  *      - Returns success and modifies ctime! 
1417  */
1418 short calc_ipo_spec (Ipo *ipo, int adrcode, float *ctime)
1419 {
1420         IpoCurve *icu= find_ipocurve(ipo, adrcode);
1421         
1422         /* only evaluate if found */
1423         if (icu) {
1424                 /* only calculate if allowed to (not locked and not muted) 
1425                  *      - drivers not taken into account, because this may be called when calculating a driver
1426                  */
1427                 if ((icu->flag & (IPO_LOCK|IPO_MUTE))==0) 
1428                         calc_icu(icu, *ctime);
1429                 
1430                 /* value resulting from calculations is written into ctime! */
1431                 *ctime= icu->curval;
1432                 return 1;
1433         }
1434         
1435         /* couldn't evaluate */
1436         return 0;
1437 }
1438
1439 /* ***************************** IPO - DataAPI ********************************* */
1440
1441 /* --------------------- Flush/Execute IPO Values ----------------------------- */
1442
1443 /* Flush IpoCurve->curvals to the data they affect (defined by ID)
1444  *       - not for Actions or Constraints!  (those have their own special handling)
1445  */
1446 void execute_ipo (ID *id, Ipo *ipo)
1447 {
1448         IpoCurve *icu;
1449         void *poin;
1450         int type;
1451         
1452         /* don't do anything without an IPO block */
1453         if (ipo == NULL) 
1454                 return;
1455         
1456         /* loop over IPO Curves, getting pointer to var to affect, and write into that pointer */
1457         for (icu= ipo->curve.first; icu; icu= icu->next) {
1458                 poin= get_ipo_poin(id, icu, &type);
1459                 if (poin) write_ipo_poin(poin, type, icu->curval);
1460         }
1461 }
1462
1463 /* Flush Action-Channel IPO data to Pose Channel */
1464 void execute_action_ipo (bActionChannel *achan, bPoseChannel *pchan)
1465 {
1466         /* only do this if there's an Action Channel and Pose Channel to use */
1467         if (achan && achan->ipo && pchan) {
1468                 IpoCurve *icu;
1469                 
1470                 /* loop over IPO-curves, getting a pointer to pchan var to write to
1471                  *      - assume for now that only 'float' channels will ever get written into
1472                  */
1473                 for (icu= achan->ipo->curve.first; icu; icu= icu->next) {
1474                         void *poin= get_pchan_ipo_poin(pchan, icu->adrcode);
1475                         if (poin) write_ipo_poin(poin, IPO_FLOAT, icu->curval);
1476                 }
1477         }
1478 }
1479
1480
1481 /* --------------------- Force Calculation + Flush IPO Values ----------------------------- */
1482
1483 /* Calculate values for given IPO block, then flush to all of block's users
1484  *       - for general usage 
1485  */
1486 void do_ipo (Ipo *ipo)
1487 {
1488         if (ipo) {
1489                 float ctime= frame_to_float(G.scene->r.cfra);
1490                 
1491                 /* calculate values, then flush to all users of this IPO block */
1492                 calc_ipo(ipo, ctime);
1493                 do_ipo_nocalc(ipo);
1494         }
1495 }
1496
1497 /* Calculate values for given Material's IPO block, then flush to given Material only */
1498 void do_mat_ipo (Material *ma)
1499 {
1500         float ctime;
1501         
1502         if (ELEM(NULL, ma, ma->ipo)) 
1503                 return;
1504         
1505         ctime= frame_to_float(G.scene->r.cfra);
1506         /* if(ob->ipoflag & OB_OFFS_OB) ctime-= ob->sf; */
1507         
1508         /* calculate values for current time, then flush values to given material only */
1509         calc_ipo(ma->ipo, ctime);
1510         execute_ipo((ID *)ma, ma->ipo);
1511 }
1512
1513 /* Calculate values for given Object's IPO block, then flush to given Object only
1514  *      - there's also some funky stuff that looks like it's for scene layers
1515  */
1516 void do_ob_ipo (Object *ob)
1517 {
1518         float ctime;
1519         unsigned int lay;
1520         
1521         if (ob->ipo == NULL) 
1522                 return;
1523         
1524         /* do not set ob->ctime here: for example when parent in invisible layer */
1525         ctime= bsystem_time(ob, (float) G.scene->r.cfra, 0.0);
1526         
1527         /* calculate values of */
1528         calc_ipo(ob->ipo, ctime);
1529         
1530         /* Patch: remember localview */
1531         lay= ob->lay & 0xFF000000;
1532         
1533         /* flush IPO values to this object only */
1534         execute_ipo((ID *)ob, ob->ipo);
1535         
1536         /* hack: for layer animation??? - is this what this is? (Aligorith, 28Sep2008) */
1537         ob->lay |= lay;
1538         if ((ob->id.name[2]=='S') && (ob->id.name[3]=='C') && (ob->id.name[4]=='E')) {
1539                 if (strcmp(G.scene->id.name+2, ob->id.name+6)==0) {
1540                         G.scene->lay= ob->lay;
1541                         copy_view3d_lock(0);
1542                         /* no redraw here! creates too many calls */
1543                 }
1544         }
1545 }
1546
1547 /* Only execute those IPO-Curves with drivers, on the current frame, for the given Object
1548  *      - TODO: Drivers should really be made separate from standard anim channels
1549  */
1550 void do_ob_ipodrivers (Object *ob, Ipo *ipo, float ctime)
1551 {
1552         IpoCurve *icu;
1553         void *poin;
1554         int type;
1555         
1556         for (icu= ipo->curve.first; icu; icu= icu->next) {
1557                 if (icu->driver) {
1558                         icu->curval= eval_icu(icu, ctime);
1559                         
1560                         poin= get_ipo_poin((ID *)ob, icu, &type);
1561                         if (poin) write_ipo_poin(poin, type, icu->curval);
1562                 }
1563         }
1564 }
1565
1566 /* Special variation to calculate IPO values for Sequence + perform other stuff */
1567 void do_seq_ipo (Sequence *seq, int cfra)
1568 {
1569         float ctime, div;
1570         
1571         /* seq_ipo has an exception: calc both fields immediately */
1572         if (seq->ipo) {
1573                 if ((seq->flag & SEQ_IPO_FRAME_LOCKED) != 0) {
1574                         ctime = frame_to_float(cfra);
1575                         div = 1.0;
1576                 } 
1577                 else {
1578                         ctime= frame_to_float(cfra - seq->startdisp);
1579                         div= (seq->enddisp - seq->startdisp) / 100.0f;
1580                         if (div == 0.0) return;
1581                 }
1582                 
1583                 /* 2nd field */
1584                 calc_ipo(seq->ipo, (ctime+0.5f)/div);
1585                 execute_ipo((ID *)seq, seq->ipo);
1586                 seq->facf1= seq->facf0;
1587                 
1588                 /* 1st field */
1589                 calc_ipo(seq->ipo, ctime/div);
1590                 execute_ipo((ID *)seq, seq->ipo);
1591         }
1592         else 
1593                 seq->facf1= seq->facf0= 1.0f;
1594 }
1595
1596 /* --------- */
1597
1598
1599 /* exception: it does calc for objects...
1600  * now find out why this routine was used anyway!
1601  */
1602 void do_ipo_nocalc (Ipo *ipo)
1603 {
1604         Object *ob;
1605         Material *ma;
1606         Tex *tex;
1607         World *wo;
1608         Lamp *la;
1609         Camera *ca;
1610         bSound *snd;
1611         
1612         if (ipo == NULL) 
1613                 return;
1614         
1615         /* only flush IPO values (without calculating first/again) on 
1616          * to the datablocks that use the given IPO block 
1617          */
1618         switch (ipo->blocktype) {
1619         case ID_OB:
1620                 for (ob= G.main->object.first; ob; ob= ob->id.next) {
1621                         if (ob->ipo == ipo) do_ob_ipo(ob);
1622                 }
1623                 break;
1624         case ID_MA:
1625                 for (ma= G.main->mat.first; ma; ma= ma->id.next) {
1626                         if (ma->ipo == ipo) execute_ipo((ID *)ma, ipo);
1627                 }
1628                 break;
1629         case ID_TE:
1630                 for (tex= G.main->tex.first; tex; tex= tex->id.next) {
1631                         if (tex->ipo == ipo) execute_ipo((ID *)tex, ipo);
1632                 }
1633                 break;
1634         case ID_WO:
1635                 for (wo= G.main->world.first; wo; wo= wo->id.next) {
1636                         if (wo->ipo == ipo) execute_ipo((ID *)wo, ipo);
1637                 }
1638                 break;
1639         case ID_LA:
1640                 for (la= G.main->lamp.first; la; la= la->id.next) {
1641                         if (la->ipo == ipo) execute_ipo((ID *)la, ipo);
1642                 }
1643                 break;
1644         case ID_CA:
1645                 for (ca= G.main->camera.first; ca; ca= ca->id.next) {
1646                         if (ca->ipo == ipo) execute_ipo((ID *)ca, ipo);
1647                 }
1648                 break;
1649         case ID_SO:
1650                 for (snd= G.main->sound.first; snd; snd= snd->id.next) {
1651                         if (snd->ipo == ipo) execute_ipo((ID *)snd, ipo);
1652                 }
1653                 break;
1654         }
1655 }
1656
1657 /* Executes IPO's for whole database on frame change, in a specified order,
1658  * with datablocks being calculated in alphabetical order
1659  *      - called on scene_update_for_newframe() only 
1660  */
1661 void do_all_data_ipos ()
1662 {
1663         Material *ma;
1664         Tex *tex;
1665         World *wo;
1666         Ipo *ipo;
1667         Lamp *la;
1668         Key *key;
1669         Camera *ca;
1670         bSound *snd;
1671         Sequence *seq;
1672         Editing *ed;
1673         Base *base;
1674         float ctime;
1675
1676         ctime= frame_to_float(G.scene->r.cfra);
1677         
1678         /* this exception cannot be depgraphed yet... what todo with objects in other layers?... */
1679         for (base= G.scene->base.first; base; base= base->next) {
1680                 /* only update layer when an ipo */
1681                 if (has_ipo_code(base->object->ipo, OB_LAY)) {
1682                         do_ob_ipo(base->object);
1683                         base->lay= base->object->lay;
1684                 }
1685         }
1686         
1687         /* layers for the set...*/
1688         if (G.scene->set) {
1689                 for (base= G.scene->set->base.first; base; base= base->next) {
1690                         if (has_ipo_code(base->object->ipo, OB_LAY)) {
1691                                 do_ob_ipo(base->object);
1692                                 base->lay= base->object->lay;
1693                         }
1694                 }
1695         }
1696         
1697         /* Calculate all IPO blocks in use, execept those for Objects */
1698         for (ipo= G.main->ipo.first; ipo; ipo= ipo->id.next) {
1699                 if ((ipo->id.us) && (ipo->blocktype != ID_OB)) {
1700                         calc_ipo(ipo, ctime);
1701                 }
1702         }
1703
1704         /* Texture Blocks */
1705         for (tex= G.main->tex.first; tex; tex= tex->id.next) {
1706                 if (tex->ipo) execute_ipo((ID *)tex, tex->ipo);
1707         }
1708         
1709         /* Material Blocks */
1710         for (ma= G.main->mat.first; ma; ma= ma->id.next) {
1711                 if (ma->ipo) execute_ipo((ID *)ma, ma->ipo);
1712         }
1713         
1714         /* World Blocks */
1715         for (wo= G.main->world.first; wo; wo= wo->id.next) {
1716                 if (wo->ipo) execute_ipo((ID *)wo, wo->ipo);
1717         }
1718         
1719         /* ShapeKey Blocks */
1720         for (key= G.main->key.first; key; key= key->id.next) {
1721                 if (key->ipo) execute_ipo((ID *)key, key->ipo);
1722         }
1723         
1724         /* Lamp Blocks */
1725         for (la= G.main->lamp.first; la; la= la->id.next) {
1726                 if (la->ipo) execute_ipo((ID *)la, la->ipo);
1727         }
1728         
1729         /* Camera Blocks */
1730         for (ca= G.main->camera.first; ca; ca= ca->id.next) {
1731                 if (ca->ipo) execute_ipo((ID *)ca, ca->ipo);
1732         }
1733         
1734         /* Sound Blocks (Old + Unused) */
1735         for (snd= G.main->sound.first; snd; snd= snd->id.next) {
1736                 if (snd->ipo) execute_ipo((ID *)snd, snd->ipo);
1737         }
1738
1739         /* Sequencer: process FAC Ipos used as volume envelopes */
1740         ed= G.scene->ed;
1741         if (ed) {
1742                 for (seq= ed->seqbasep->first; seq; seq= seq->next) {
1743                         if ( ((seq->type == SEQ_RAM_SOUND) || (seq->type == SEQ_HD_SOUND)) &&
1744                                  (seq->startdisp <= G.scene->r.cfra+2) && 
1745                              (seq->enddisp>G.scene->r.cfra) &&
1746                                  (seq->ipo) ) 
1747                         {
1748                                         do_seq_ipo(seq, G.scene->r.cfra);
1749                         }
1750                 }
1751         }
1752 }
1753
1754
1755 /* --------------------- Assorted ----------------------------- */ 
1756
1757 /* clear delta-transforms on all Objects which use the given IPO block */
1758 void clear_delta_obipo(Ipo *ipo)
1759 {
1760         Object *ob;
1761         
1762         /* only search if there's an IPO */
1763         if (ipo == NULL) 
1764                 return;
1765         
1766         /* search through all objects in database */
1767         for (ob= G.main->object.first; ob; ob= ob->id.next) {
1768                 /* can only update if not a library */
1769                 if (ob->id.lib == NULL) {
1770                         if (ob->ipo == ipo)  {
1771                                 memset(&ob->dloc, 0, 12);
1772                                 memset(&ob->drot, 0, 12);
1773                                 memset(&ob->dsize, 0, 12);
1774                         }
1775                 }
1776         }
1777 }
1778
1779 /* ***************************** IPO - DataAPI ********************************* */
1780
1781 // !!!!!!!!!!!!!!!!!!!!!!!!!!!! FIXME - BAD CRUFT WARNING !!!!!!!!!!!!!!!!!!!!!!!
1782
1783 /* These functions here should be replaced eventually by the Data API, as this is 
1784  * inflexible duplication...
1785  */
1786
1787 /* --------------------- Get Pointer API ----------------------------- */ 
1788
1789 /* get pointer to pose-channel's channel, but set appropriate flags first */
1790 void *get_pchan_ipo_poin (bPoseChannel *pchan, int adrcode)
1791 {
1792         void *poin= NULL;
1793         
1794         switch (adrcode) {
1795                 case AC_QUAT_W:
1796                         poin= &(pchan->quat[0]); 
1797                         pchan->flag |= POSE_ROT;
1798                         break;
1799                 case AC_QUAT_X:
1800                         poin= &(pchan->quat[1]); 
1801                         pchan->flag |= POSE_ROT;
1802                         break;
1803                 case AC_QUAT_Y:
1804                         poin= &(pchan->quat[2]); 
1805                         pchan->flag |= POSE_ROT;
1806                         break;
1807                 case AC_QUAT_Z:
1808                         poin= &(pchan->quat[3]); 
1809                         pchan->flag |= POSE_ROT;
1810                         break;
1811                         
1812                 case AC_LOC_X:
1813                         poin= &(pchan->loc[0]); 
1814                         pchan->flag |= POSE_LOC;
1815                         break;
1816                 case AC_LOC_Y:
1817                         poin= &(pchan->loc[1]); 
1818                         pchan->flag |= POSE_LOC;
1819                         break;
1820                 case AC_LOC_Z:
1821                         poin= &(pchan->loc[2]); 
1822                         pchan->flag |= POSE_LOC;
1823                         break;
1824                 
1825                 case AC_SIZE_X:
1826                         poin= &(pchan->size[0]); 
1827                         pchan->flag |= POSE_SIZE;
1828                         break;
1829                 case AC_SIZE_Y:
1830                         poin= &(pchan->size[1]); 
1831                         pchan->flag |= POSE_SIZE;
1832                         break;
1833                 case AC_SIZE_Z:
1834                         poin= &(pchan->size[2]); 
1835                         pchan->flag |= POSE_SIZE;
1836                         break;
1837         }
1838         
1839         /* return pointer */
1840         return poin;
1841 }
1842
1843 /* get texture channel */
1844 static void *give_tex_poin (Tex *tex, int adrcode, int *type )
1845 {
1846         void *poin= NULL;
1847
1848         switch (adrcode) {
1849         case TE_NSIZE:
1850                 poin= &(tex->noisesize); break;
1851         case TE_TURB:
1852                 poin= &(tex->turbul); break;
1853         case TE_NDEPTH:
1854                 poin= &(tex->noisedepth); *type= IPO_SHORT; break;
1855         case TE_NTYPE:
1856                 poin= &(tex->noisetype); *type= IPO_SHORT; break;
1857         case TE_VNW1:
1858                 poin= &(tex->vn_w1); break;
1859         case TE_VNW2:
1860                 poin= &(tex->vn_w2); break;
1861         case TE_VNW3:
1862                 poin= &(tex->vn_w3); break;
1863         case TE_VNW4:
1864                 poin= &(tex->vn_w4); break;
1865         case TE_VNMEXP:
1866                 poin= &(tex->vn_mexp); break;
1867         case TE_ISCA:
1868                 poin= &(tex->ns_outscale); break;
1869         case TE_DISTA:
1870                 poin= &(tex->dist_amount); break;
1871         case TE_VN_COLT:
1872                 poin= &(tex->vn_coltype); *type= IPO_SHORT; break;
1873         case TE_VN_DISTM:
1874                 poin= &(tex->vn_distm); *type= IPO_SHORT; break;
1875         case TE_MG_TYP:
1876                 poin= &(tex->stype); *type= IPO_SHORT; break;
1877         case TE_MGH:
1878                 poin= &(tex->mg_H); break;
1879         case TE_MG_LAC:
1880                 poin= &(tex->mg_lacunarity); break;
1881         case TE_MG_OCT:
1882                 poin= &(tex->mg_octaves); break;
1883         case TE_MG_OFF:
1884                 poin= &(tex->mg_offset); break;
1885         case TE_MG_GAIN:
1886                 poin= &(tex->mg_gain); break;
1887         case TE_N_BAS1:
1888                 poin= &(tex->noisebasis); *type= IPO_SHORT; break;
1889         case TE_N_BAS2:
1890                 poin= &(tex->noisebasis2); *type= IPO_SHORT; break;
1891         case TE_COL_R:
1892                 poin= &(tex->rfac); break;
1893         case TE_COL_G:
1894                 poin= &(tex->gfac); break;
1895         case TE_COL_B:
1896                 poin= &(tex->bfac); break;
1897         case TE_BRIGHT:
1898                 poin= &(tex->bright); break;
1899         case TE_CONTRA:
1900                 poin= &(tex->contrast); break;
1901         }
1902         
1903         /* return pointer */
1904         return poin;
1905 }
1906
1907 /* get texture-slot/mapping channel */
1908 void *give_mtex_poin (MTex *mtex, int adrcode )
1909 {
1910         void *poin= NULL;
1911         
1912         switch (adrcode) {
1913         case MAP_OFS_X:
1914                 poin= &(mtex->ofs[0]); break;
1915         case MAP_OFS_Y:
1916                 poin= &(mtex->ofs[1]); break;
1917         case MAP_OFS_Z:
1918                 poin= &(mtex->ofs[2]); break;
1919         case MAP_SIZE_X:
1920                 poin= &(mtex->size[0]); break;
1921         case MAP_SIZE_Y:
1922                 poin= &(mtex->size[1]); break;
1923         case MAP_SIZE_Z:
1924                 poin= &(mtex->size[2]); break;
1925         case MAP_R:
1926                 poin= &(mtex->r); break;
1927         case MAP_G:
1928                 poin= &(mtex->g); break;
1929         case MAP_B:
1930                 poin= &(mtex->b); break;
1931         case MAP_DVAR:
1932                 poin= &(mtex->def_var); break;
1933         case MAP_COLF:
1934                 poin= &(mtex->colfac); break;
1935         case MAP_NORF:
1936                 poin= &(mtex->norfac); break;
1937         case MAP_VARF:
1938                 poin= &(mtex->varfac); break;
1939         case MAP_DISP:
1940                 poin= &(mtex->dispfac); break;
1941         }
1942         
1943         /* return pointer */
1944         return poin;
1945 }
1946
1947 /* GS reads the memory pointed at in a specific ordering. There are,
1948  * however two definitions for it. I have jotted them down here, both,
1949  * but I think the first one is actually used. The thing is that
1950  * big-endian systems might read this the wrong way round. OTOH, we
1951  * constructed the IDs that are read out with this macro explicitly as
1952  * well. I expect we'll sort it out soon... */
1953
1954 /* from blendef: */
1955 #define GS(a)   (*((short *)(a)))
1956
1957 /* from misc_util: flip the bytes from x  */
1958 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
1959
1960
1961 /* general function to get pointer to source/destination data  */
1962 void *get_ipo_poin (ID *id, IpoCurve *icu, int *type)
1963 {
1964         void *poin= NULL;
1965         MTex *mtex= NULL;
1966
1967         /* most channels will have float data, but those with other types will override this */
1968         *type= IPO_FLOAT;
1969
1970         /* data is divided into 'blocktypes' based on ID-codes */
1971         switch (GS(id->name)) {
1972                 case ID_OB: /* object channels -----------------------------  */
1973                 {
1974                         Object *ob= (Object *)id;
1975                         
1976                         switch (icu->adrcode) {
1977                         case OB_LOC_X:
1978                                 poin= &(ob->loc[0]); break;
1979                         case OB_LOC_Y:
1980                                 poin= &(ob->loc[1]); break;
1981                         case OB_LOC_Z:
1982                                 poin= &(ob->loc[2]); break;
1983                         case OB_DLOC_X:
1984                                 poin= &(ob->dloc[0]); break;
1985                         case OB_DLOC_Y:
1986                                 poin= &(ob->dloc[1]); break;
1987                         case OB_DLOC_Z:
1988                                 poin= &(ob->dloc[2]); break;
1989                         
1990                         case OB_ROT_X:
1991                                 poin= &(ob->rot[0]); *type= IPO_FLOAT_DEGR; break;
1992                         case OB_ROT_Y:
1993                                 poin= &(ob->rot[1]); *type= IPO_FLOAT_DEGR; break;
1994                         case OB_ROT_Z:
1995                                 poin= &(ob->rot[2]); *type= IPO_FLOAT_DEGR; break;
1996                         case OB_DROT_X:
1997                                 poin= &(ob->drot[0]); *type= IPO_FLOAT_DEGR; break;
1998                         case OB_DROT_Y:
1999                                 poin= &(ob->drot[1]); *type= IPO_FLOAT_DEGR; break;
2000                         case OB_DROT_Z:
2001                                 poin= &(ob->drot[2]); *type= IPO_FLOAT_DEGR; break;
2002                                 
2003                         case OB_SIZE_X:
2004                                 poin= &(ob->size[0]); break;
2005                         case OB_SIZE_Y:
2006                                 poin= &(ob->size[1]); break;
2007                         case OB_SIZE_Z:
2008                                 poin= &(ob->size[2]); break;
2009                         case OB_DSIZE_X:
2010                                 poin= &(ob->dsize[0]); break;
2011                         case OB_DSIZE_Y:
2012                                 poin= &(ob->dsize[1]); break;
2013                         case OB_DSIZE_Z:
2014                                 poin= &(ob->dsize[2]); break;
2015                         
2016                         case OB_LAY:
2017                                 poin= &(ob->lay); *type= IPO_INT_BIT; break;
2018                                 
2019                         case OB_COL_R:  
2020                                 poin= &(ob->col[0]); break;
2021                         case OB_COL_G:
2022                                 poin= &(ob->col[1]); break;
2023                         case OB_COL_B:
2024                                 poin= &(ob->col[2]); break;
2025                         case OB_COL_A:
2026                                 poin= &(ob->col[3]); break;
2027                                 
2028                         case OB_PD_FSTR:
2029                                 if (ob->pd) poin= &(ob->pd->f_strength);
2030                                 break;
2031                         case OB_PD_FFALL:
2032                                 if (ob->pd) poin= &(ob->pd->f_power);
2033                                 break;
2034                         case OB_PD_SDAMP:
2035                                 if (ob->pd) poin= &(ob->pd->pdef_damp);
2036                                 break;
2037                         case OB_PD_RDAMP:
2038                                 if (ob->pd) poin= &(ob->pd->pdef_rdamp);
2039                                 break;
2040                         case OB_PD_PERM:
2041                                 if (ob->pd) poin= &(ob->pd->pdef_perm);
2042                                 break;
2043                         case OB_PD_FMAXD:
2044                                 if (ob->pd) poin= &(ob->pd->maxdist);
2045                                 break;
2046                         }
2047                 }
2048                         break;
2049                 case ID_MA: /* material channels -----------------------------  */
2050                 {
2051                         Material *ma= (Material *)id;
2052                         
2053                         switch (icu->adrcode) {
2054                         case MA_COL_R:
2055                                 poin= &(ma->r); break;
2056                         case MA_COL_G:
2057                                 poin= &(ma->g); break;
2058                         case MA_COL_B:
2059                                 poin= &(ma->b); break;
2060                         case MA_SPEC_R:
2061                                 poin= &(ma->specr); break;
2062                         case MA_SPEC_G:
2063                                 poin= &(ma->specg); break;
2064                         case MA_SPEC_B:
2065                                 poin= &(ma->specb); break;
2066                         case MA_MIR_R:
2067                                 poin= &(ma->mirr); break;
2068                         case MA_MIR_G:
2069                                 poin= &(ma->mirg); break;
2070                         case MA_MIR_B:
2071                                 poin= &(ma->mirb); break;
2072                         case MA_REF:
2073                                 poin= &(ma->ref); break;
2074                         case MA_ALPHA:
2075                                 poin= &(ma->alpha); break;
2076                         case MA_EMIT:
2077                                 poin= &(ma->emit); break;
2078                         case MA_AMB:
2079                                 poin= &(ma->amb); break;
2080                         case MA_SPEC:
2081                                 poin= &(ma->spec); break;
2082                         case MA_HARD:
2083                                 poin= &(ma->har); *type= IPO_SHORT; break;
2084                         case MA_SPTR:
2085                                 poin= &(ma->spectra); break;
2086                         case MA_IOR:
2087                                 poin= &(ma->ang); break;
2088                         case MA_MODE:
2089                                 poin= &(ma->mode); *type= IPO_INT_BIT; break; // evil... dumping bitflags directly to user!
2090                         case MA_HASIZE:
2091                                 poin= &(ma->hasize); break;
2092                         case MA_TRANSLU:
2093                                 poin= &(ma->translucency); break;
2094                         case MA_RAYM:
2095                                 poin= &(ma->ray_mirror); break;
2096                         case MA_FRESMIR:
2097                                 poin= &(ma->fresnel_mir); break;
2098                         case MA_FRESMIRI:
2099                                 poin= &(ma->fresnel_mir_i); break;
2100                         case MA_FRESTRA:
2101                                 poin= &(ma->fresnel_tra); break;
2102                         case MA_FRESTRAI:
2103                                 poin= &(ma->fresnel_tra_i); break;
2104                         case MA_ADD:
2105                                 poin= &(ma->add); break;
2106                         }
2107                         
2108                         if (poin == NULL) {
2109                                 if (icu->adrcode & MA_MAP1) mtex= ma->mtex[0];
2110                                 else if (icu->adrcode & MA_MAP2) mtex= ma->mtex[1];
2111                                 else if (icu->adrcode & MA_MAP3) mtex= ma->mtex[2];
2112                                 else if (icu->adrcode & MA_MAP4) mtex= ma->mtex[3];
2113                                 else if (icu->adrcode & MA_MAP5) mtex= ma->mtex[4];
2114                                 else if (icu->adrcode & MA_MAP6) mtex= ma->mtex[5];
2115                                 else if (icu->adrcode & MA_MAP7) mtex= ma->mtex[6];
2116                                 else if (icu->adrcode & MA_MAP8) mtex= ma->mtex[7];
2117                                 else if (icu->adrcode & MA_MAP9) mtex= ma->mtex[8];
2118                                 else if (icu->adrcode & MA_MAP10) mtex= ma->mtex[9];
2119                                 else if (icu->adrcode & MA_MAP12) mtex= ma->mtex[11];
2120                                 else if (icu->adrcode & MA_MAP11) mtex= ma->mtex[10];
2121                                 else if (icu->adrcode & MA_MAP13) mtex= ma->mtex[12];
2122                                 else if (icu->adrcode & MA_MAP14) mtex= ma->mtex[13];
2123                                 else if (icu->adrcode & MA_MAP15) mtex= ma->mtex[14];
2124                                 else if (icu->adrcode & MA_MAP16) mtex= ma->mtex[15];
2125                                 else if (icu->adrcode & MA_MAP17) mtex= ma->mtex[16];
2126                                 else if (icu->adrcode & MA_MAP18) mtex= ma->mtex[17];
2127                                 
2128                                 if (mtex)
2129                                         poin= give_mtex_poin(mtex, (icu->adrcode & (MA_MAP1-1)));
2130                         }
2131                 }
2132                         break;
2133                 case ID_TE: /* texture channels -----------------------------  */
2134                 {
2135                         Tex *tex= (Tex *)id;
2136                         
2137                         if (tex) 
2138                                 poin= give_tex_poin(tex, icu->adrcode, type);
2139                 }
2140                         break;
2141                 case ID_SEQ: /* sequence channels -----------------------------  */
2142                 {
2143                         Sequence *seq= (Sequence *)id;
2144                         
2145                         switch (icu->adrcode) {
2146                         case SEQ_FAC1:
2147                                 poin= &(seq->facf0); break;
2148                         }
2149                 }
2150                         break;
2151                 case ID_CU: /* curve channels -----------------------------  */
2152                 {
2153                         poin= &(icu->curval);
2154                 }
2155                         break;
2156                 case ID_KE: /* shapekey channels -----------------------------  */
2157                 {
2158                         Key *key= (Key *)id;
2159                         KeyBlock *kb;
2160                         
2161                         for(kb= key->block.first; kb; kb= kb->next) {
2162                                 if (kb->adrcode == icu->adrcode)
2163                                         break;
2164                         }
2165                         
2166                         if (kb)
2167                                 poin= &(kb->curval);
2168                 }
2169                         break;
2170                 case ID_WO: /* world channels -----------------------------  */
2171                 {
2172                         World *wo= (World *)id;
2173                         
2174                         switch (icu->adrcode) {
2175                         case WO_HOR_R:
2176                                 poin= &(wo->horr); break;
2177                         case WO_HOR_G:
2178                                 poin= &(wo->horg); break;
2179                         case WO_HOR_B:
2180                                 poin= &(wo->horb); break;
2181                         case WO_ZEN_R:
2182                                 poin= &(wo->zenr); break;
2183                         case WO_ZEN_G:
2184                                 poin= &(wo->zeng); break;
2185                         case WO_ZEN_B:
2186                                 poin= &(wo->zenb); break;
2187                         
2188                         case WO_EXPOS:
2189                                 poin= &(wo->exposure); break;
2190                         
2191                         case WO_MISI:
2192                                 poin= &(wo->misi); break;
2193                         case WO_MISTDI:
2194                                 poin= &(wo->mistdist); break;
2195                         case WO_MISTSTA:
2196                                 poin= &(wo->miststa); break;
2197                         case WO_MISTHI:
2198                                 poin= &(wo->misthi); break;
2199                         
2200                         case WO_STAR_R:
2201                                 poin= &(wo->starr); break;
2202                         case WO_STAR_G:
2203                                 poin= &(wo->starg); break;
2204                         case WO_STAR_B:
2205                                 poin= &(wo->starb); break;
2206                         
2207                         case WO_STARDIST:
2208                                 poin= &(wo->stardist); break;
2209                         case WO_STARSIZE:
2210                                 poin= &(wo->starsize); break;
2211                         }
2212                         
2213                         if (poin == NULL) {
2214                                 if (icu->adrcode & MA_MAP1) mtex= wo->mtex[0];
2215                                 else if (icu->adrcode & MA_MAP2) mtex= wo->mtex[1];
2216                                 else if (icu->adrcode & MA_MAP3) mtex= wo->mtex[2];
2217                                 else if (icu->adrcode & MA_MAP4) mtex= wo->mtex[3];
2218                                 else if (icu->adrcode & MA_MAP5) mtex= wo->mtex[4];
2219                                 else if (icu->adrcode & MA_MAP6) mtex= wo->mtex[5];
2220                                 else if (icu->adrcode & MA_MAP7) mtex= wo->mtex[6];
2221                                 else if (icu->adrcode & MA_MAP8) mtex= wo->mtex[7];
2222                                 else if (icu->adrcode & MA_MAP9) mtex= wo->mtex[8];
2223                                 else if (icu->adrcode & MA_MAP10) mtex= wo->mtex[9];
2224                                 else if (icu->adrcode & MA_MAP11) mtex= wo->mtex[10];
2225                                 else if (icu->adrcode & MA_MAP12) mtex= wo->mtex[11];
2226                                 else if (icu->adrcode & MA_MAP13) mtex= wo->mtex[12];
2227                                 else if (icu->adrcode & MA_MAP14) mtex= wo->mtex[13];
2228                                 else if (icu->adrcode & MA_MAP15) mtex= wo->mtex[14];
2229                                 else if (icu->adrcode & MA_MAP16) mtex= wo->mtex[15];
2230                                 else if (icu->adrcode & MA_MAP17) mtex= wo->mtex[16];
2231                                 else if (icu->adrcode & MA_MAP18) mtex= wo->mtex[17];
2232                                 
2233                                 if (mtex)
2234                                         poin= give_mtex_poin(mtex, (icu->adrcode & (MA_MAP1-1)));
2235                         }
2236                 }
2237                         break;
2238                 case ID_LA: /* lamp channels -----------------------------  */
2239                 {
2240                         Lamp *la= (Lamp *)id;
2241                         
2242                         switch (icu->adrcode) {
2243                         case LA_ENERGY:
2244                                 poin= &(la->energy); break;             
2245                         case LA_COL_R:
2246                                 poin= &(la->r); break;
2247                         case LA_COL_G:
2248                                 poin= &(la->g); break;
2249                         case LA_COL_B:
2250                                 poin= &(la->b); break;
2251                         case LA_DIST:
2252                                 poin= &(la->dist); break;               
2253                         case LA_SPOTSI:
2254                                 poin= &(la->spotsize); break;
2255                         case LA_SPOTBL:
2256                                 poin= &(la->spotblend); break;
2257                         case LA_QUAD1:
2258                                 poin= &(la->att1); break;
2259                         case LA_QUAD2:
2260                                 poin= &(la->att2); break;
2261                         case LA_HALOINT:
2262                                 poin= &(la->haint); break;
2263                         }
2264                         
2265                         if (poin == NULL) {
2266                                 if (icu->adrcode & MA_MAP1) mtex= la->mtex[0];
2267                                 else if (icu->adrcode & MA_MAP2) mtex= la->mtex[1];
2268                                 else if (icu->adrcode & MA_MAP3) mtex= la->mtex[2];
2269                                 else if (icu->adrcode & MA_MAP4) mtex= la->mtex[3];
2270                                 else if (icu->adrcode & MA_MAP5) mtex= la->mtex[4];
2271                                 else if (icu->adrcode & MA_MAP6) mtex= la->mtex[5];
2272                                 else if (icu->adrcode & MA_MAP7) mtex= la->mtex[6];
2273                                 else if (icu->adrcode & MA_MAP8) mtex= la->mtex[7];
2274                                 else if (icu->adrcode & MA_MAP9) mtex= la->mtex[8];
2275                                 else if (icu->adrcode & MA_MAP10) mtex= la->mtex[9];
2276                                 else if (icu->adrcode & MA_MAP11) mtex= la->mtex[10];
2277                                 else if (icu->adrcode & MA_MAP12) mtex= la->mtex[11];
2278                                 else if (icu->adrcode & MA_MAP13) mtex= la->mtex[12];
2279                                 else if (icu->adrcode & MA_MAP14) mtex= la->mtex[13];
2280                                 else if (icu->adrcode & MA_MAP15) mtex= la->mtex[14];
2281                                 else if (icu->adrcode & MA_MAP16) mtex= la->mtex[15];
2282                                 else if (icu->adrcode & MA_MAP17) mtex= la->mtex[16];
2283                                 else if (icu->adrcode & MA_MAP18) mtex= la->mtex[17];
2284                                 
2285                                 if (mtex)
2286                                         poin= give_mtex_poin(mtex, (icu->adrcode & (MA_MAP1-1)));
2287                         }
2288                 }
2289                         break;
2290                 case ID_CA: /* camera channels -----------------------------  */
2291                 {
2292                         Camera *ca= (Camera *)id;
2293                         
2294                         switch (icu->adrcode) {
2295                         case CAM_LENS:
2296                                 if (ca->type == CAM_ORTHO)
2297                                         poin= &(ca->ortho_scale);
2298                                 else
2299                                         poin= &(ca->lens); 
2300                                 break;
2301                         case CAM_STA:
2302                                 poin= &(ca->clipsta); break;
2303                         case CAM_END:
2304                                 poin= &(ca->clipend); break;
2305                                 
2306                         case CAM_YF_APERT:
2307                                 poin= &(ca->YF_aperture); break;
2308                         case CAM_YF_FDIST:
2309                                 poin= &(ca->YF_dofdist); break;
2310                                 
2311                         case CAM_SHIFT_X:
2312                                 poin= &(ca->shiftx); break;
2313                         case CAM_SHIFT_Y:
2314                                 poin= &(ca->shifty); break;
2315                         }
2316                 }
2317                         break;
2318                 case ID_SO: /* sound channels -----------------------------  */
2319                 {
2320                         bSound *snd= (bSound *)id;
2321                         
2322                         switch (icu->adrcode) {
2323                         case SND_VOLUME:
2324                                 poin= &(snd->volume); break;
2325                         case SND_PITCH:
2326                                 poin= &(snd->pitch); break;
2327                         case SND_PANNING:
2328                                 poin= &(snd->panning); break;
2329                         case SND_ATTEN:
2330                                 poin= &(snd->attenuation); break;
2331                         }
2332                 }
2333                         break;
2334                 case ID_PA: /* particle channels -----------------------------  */
2335                 {
2336                         ParticleSettings *part= (ParticleSettings *)id;
2337                         
2338                         switch (icu->adrcode) {
2339                         case PART_EMIT_FREQ:
2340                         case PART_EMIT_LIFE:
2341                         case PART_EMIT_VEL:
2342                         case PART_EMIT_AVE:
2343                         case PART_EMIT_SIZE:
2344                                 poin= NULL; 
2345                                 break;
2346                         
2347                         case PART_CLUMP:
2348                                 poin= &(part->clumpfac); break;
2349                         case PART_AVE:
2350                                 poin= &(part->avefac); break;
2351                         case PART_SIZE:
2352                                 poin= &(part->size); break;
2353                         case PART_DRAG:
2354                                 poin= &(part->dragfac); break;
2355                         case PART_BROWN:
2356                                 poin= &(part->brownfac); break;
2357                         case PART_DAMP:
2358                                 poin= &(part->dampfac); break;
2359                         case PART_LENGTH:
2360                                 poin= &(part->length); break;
2361                         case PART_GRAV_X:
2362                                 poin= &(part->acc[0]); break;
2363                         case PART_GRAV_Y:
2364                                 poin= &(part->acc[1]); break;
2365                         case PART_GRAV_Z:
2366                                 poin= &(part->acc[2]); break;
2367                         case PART_KINK_AMP:
2368                                 poin= &(part->kink_amp); break;
2369                         case PART_KINK_FREQ:
2370                                 poin= &(part->kink_freq); break;
2371                         case PART_KINK_SHAPE:
2372                                 poin= &(part->kink_shape); break;
2373                         case PART_BB_TILT:
2374                                 poin= &(part->bb_tilt); break;
2375                                 
2376                         case PART_PD_FSTR:
2377                                 if (part->pd) poin= &(part->pd->f_strength);
2378                                 break;
2379                         case PART_PD_FFALL:
2380                                 if (part->pd) poin= &(part->pd->f_power);
2381                                 break;
2382                         case PART_PD_FMAXD:
2383                                 if (part->pd) poin= &(part->pd->maxdist);
2384                                 break;
2385                         case PART_PD2_FSTR:
2386                                 if (part->pd2) poin= &(part->pd2->f_strength);
2387                                 break;
2388                         case PART_PD2_FFALL:
2389                                 if (part->pd2) poin= &(part->pd2->f_power);
2390                                 break;
2391                         case PART_PD2_FMAXD:
2392                                 if (part->pd2) poin= &(part->pd2->maxdist);
2393                                 break;
2394                         }
2395                 }
2396                         break;
2397         }
2398
2399         /* return pointer */
2400         return poin;
2401 }
2402
2403 /* --------------------- IPO-Curve Limits ----------------------------- */
2404
2405 /* set limits for IPO-curve 
2406  * Note: must be synced with UI and PyAPI
2407  */
2408 void set_icu_vars (IpoCurve *icu)
2409 {
2410         /* defaults. 0.0 for y-extents makes these ignored */
2411         icu->ymin= icu->ymax= 0.0;
2412         icu->ipo= IPO_BEZ;
2413         
2414         switch (icu->blocktype) {
2415                 case ID_OB: /* object channels -----------------------------  */
2416                 {
2417                         if (icu->adrcode == OB_LAY) {
2418                                 icu->ipo= IPO_CONST;
2419                                 icu->vartype= IPO_BITS;
2420                         }
2421                 }
2422                         break;
2423                 case ID_MA: /* material channels -----------------------------  */
2424                 {
2425                         if (icu->adrcode < MA_MAP1) {
2426                                 switch (icu->adrcode) {
2427                                 case MA_HASIZE:
2428                                         icu->ymax= 10000.0; break;
2429                                 case MA_HARD:
2430                                         icu->ymax= 511.0; break;
2431                                 case MA_SPEC:
2432                                         icu->ymax= 2.0; break;
2433                                 case MA_MODE:
2434                                         icu->ipo= IPO_CONST;
2435                                         icu->vartype= IPO_BITS; break;
2436                                 case MA_RAYM:
2437                                         icu->ymax= 1.0; break;
2438                                 case MA_TRANSLU:
2439                                         icu->ymax= 1.0; break;
2440                                 case MA_IOR:
2441                                         icu->ymin= 1.0;
2442                                         icu->ymax= 3.0; break;
2443                                 case MA_FRESMIR:
2444                                         icu->ymax= 5.0; break;
2445                                 case MA_FRESMIRI:
2446                                         icu->ymin= 1.0;
2447                                         icu->ymax= 5.0; break;
2448                                 case MA_FRESTRA:
2449                                         icu->ymax= 5.0; break;
2450                                 case MA_FRESTRAI:
2451                                         icu->ymin= 1.0;
2452                                         icu->ymax= 5.0; break;
2453                                 case MA_ADD:
2454                                         icu->ymax= 1.0; break;
2455                                 case MA_EMIT:
2456                                         icu->ymax= 2.0; break;
2457                                 default:
2458                                         icu->ymax= 1.0; break;
2459                                 }
2460                         }
2461                         else {
2462                                 switch (icu->adrcode & (MA_MAP1-1)) {
2463                                 case MAP_OFS_X:
2464                                 case MAP_OFS_Y:
2465                                 case MAP_OFS_Z:
2466                                 case MAP_SIZE_X:
2467                                 case MAP_SIZE_Y:
2468                                 case MAP_SIZE_Z:
2469                                         icu->ymax= 1000.0;
2470                                         icu->ymin= -1000.0;
2471                                         break;
2472                                 case MAP_R:
2473                                 case MAP_G:
2474                                 case MAP_B:
2475                                 case MAP_DVAR:
2476                                 case MAP_COLF:
2477                                 case MAP_VARF:
2478                                 case MAP_DISP:
2479                                         icu->ymax= 1.0;
2480                                         break;
2481                                 case MAP_NORF:
2482                                         icu->ymax= 25.0;
2483                                         break;
2484                                 }
2485                         }
2486                 }
2487                         break;
2488                 case ID_TE: /* texture channels -----------------------------  */
2489                 {
2490                         switch (icu->adrcode & (MA_MAP1-1)) {
2491                                 case TE_NSIZE:
2492                                         icu->ymin= 0.0001;
2493                                         icu->ymax= 2.0; break;
2494                                 case TE_NDEPTH:
2495                                         icu->vartype= IPO_SHORT;
2496                                         icu->ipo= IPO_CONST;
2497                                         icu->ymax= 6.0; break;
2498                                 case TE_NTYPE:
2499                                         icu->vartype= IPO_SHORT;
2500                                         icu->ipo= IPO_CONST;
2501                                         icu->ymax= 1.0; break;
2502                                 case TE_TURB:
2503                                         icu->ymax= 200.0; break;
2504                                 case TE_VNW1:
2505                                 case TE_VNW2:
2506                                 case TE_VNW3:
2507                                 case TE_VNW4:
2508                                         icu->ymax= 2.0;
2509                                         icu->ymin= -2.0; break;
2510                                 case TE_VNMEXP:
2511                                         icu->ymax= 10.0;
2512                                         icu->ymin= 0.01; break;
2513                                 case TE_VN_DISTM:
2514                                         icu->vartype= IPO_SHORT;
2515                                         icu->ipo= IPO_CONST;
2516                                         icu->ymax= 6.0; break;
2517                                 case TE_VN_COLT:
2518                                         icu->vartype= IPO_SHORT;
2519                                         icu->ipo= IPO_CONST;
2520                                         icu->ymax= 3.0; break;
2521                                 case TE_ISCA:
2522                                         icu->ymax= 10.0;
2523                                         icu->ymin= 0.01; break;
2524                                 case TE_DISTA:
2525                                         icu->ymax= 10.0; break;
2526                                 case TE_MG_TYP:
2527                                         icu->vartype= IPO_SHORT;
2528                                         icu->ipo= IPO_CONST;
2529                                         icu->ymax= 6.0; break;
2530                                 case TE_MGH:
2531                                         icu->ymin= 0.0001;
2532                                         icu->ymax= 2.0; break;
2533                                 case TE_MG_LAC:
2534                                 case TE_MG_OFF:
2535                                 case TE_MG_GAIN:
2536                                         icu->ymax= 6.0; break;
2537                                 case TE_MG_OCT:
2538                                         icu->ymax= 8.0; break;
2539                                 case TE_N_BAS1:
2540                                 case TE_N_BAS2:
2541                                         icu->vartype= IPO_SHORT;
2542                                         icu->ipo= IPO_CONST;
2543                                         icu->ymax= 8.0; break;
2544                                 case TE_COL_R:
2545                                         icu->ymax= 0.0; break;
2546                                 case TE_COL_G:
2547                                         icu->ymax= 2.0; break;
2548                                 case TE_COL_B:
2549                                         icu->ymax= 2.0; break;
2550                                 case TE_BRIGHT:
2551                                         icu->ymax= 2.0; break;
2552                                 case TE_CONTRA:
2553                                         icu->ymax= 5.0; break;  
2554                         }
2555                 }
2556                         break;
2557                 case ID_SEQ: /* sequence channels -----------------------------  */
2558                 {
2559                         icu->ymax= 1.0;
2560                 }
2561                         break;
2562                 case ID_CU: /* curve channels -----------------------------  */
2563                 {
2564                         icu->ymax= 1.0;
2565                 }
2566                         break;
2567                 case ID_WO: /* world channels -----------------------------  */
2568                 {
2569                         if (icu->adrcode < MA_MAP1) {
2570                                 switch (icu->adrcode) {
2571                                 case WO_EXPOS:
2572                                         icu->ymax= 5.0; break;
2573                                 case WO_MISTDI:
2574                                 case WO_MISTSTA:
2575                                 case WO_MISTHI:
2576                                 case WO_STARDIST:
2577                                 case WO_STARSIZE:
2578                                         break;
2579                                         
2580                                 default:
2581                                         icu->ymax= 1.0;
2582                                         break;
2583                                 }
2584                         }
2585                         else {
2586                                 switch (icu->adrcode & (MA_MAP1-1)) {
2587                                 case MAP_OFS_X:
2588                                 case MAP_OFS_Y:
2589                                 case MAP_OFS_Z:
2590                                 case MAP_SIZE_X:
2591                                 case MAP_SIZE_Y:
2592                                 case MAP_SIZE_Z:
2593                                         icu->ymax= 100.0;
2594                                         icu->ymin= -100.0;
2595                                         break;
2596                                 case MAP_R:
2597                                 case MAP_G:
2598                                 case MAP_B:
2599                                 case MAP_DVAR:
2600                                 case MAP_COLF:
2601                                 case MAP_NORF:
2602                                 case MAP_VARF:
2603                                 case MAP_DISP:
2604                                         icu->ymax= 1.0;
2605                                 }
2606                         }
2607                 }
2608                         break;
2609                 case ID_LA: /* lamp channels -----------------------------  */
2610                 {
2611                         if (icu->adrcode < MA_MAP1) {
2612                                 switch (icu->adrcode) {
2613                                 case LA_ENERGY:
2614                                 case LA_DIST:
2615                                         break;          
2616                                 
2617                                 case LA_COL_R:
2618                                 case LA_COL_G:
2619                                 case LA_COL_B:
2620                                 case LA_SPOTBL:
2621                                 case LA_QUAD1:
2622                                 case LA_QUAD2:
2623                                         icu->ymax= 1.0; break;
2624                                         
2625                                 case LA_SPOTSI:
2626                                         icu->ymax= 180.0; break;
2627                                 
2628                                 case LA_HALOINT:
2629                                         icu->ymax= 5.0; break;
2630                                 }
2631                         }
2632                         else {
2633                                 switch (icu->adrcode & (MA_MAP1-1)) {
2634                                 case MAP_OFS_X:
2635                                 case MAP_OFS_Y:
2636                                 case MAP_OFS_Z:
2637                                 case MAP_SIZE_X:
2638                                 case MAP_SIZE_Y:
2639                                 case MAP_SIZE_Z:
2640                                         icu->ymax= 100.0;
2641                                         icu->ymin= -100.0;
2642                                         break;
2643                                 case MAP_R:
2644                                 case MAP_G:
2645                                 case MAP_B:
2646                                 case MAP_DVAR:
2647                                 case MAP_COLF:
2648                                 case MAP_NORF:
2649                                 case MAP_VARF:
2650                                 case MAP_DISP:
2651                                         icu->ymax= 1.0;
2652                                 }
2653                         }
2654                 }       
2655                         break;
2656                 case ID_CA: /* camera channels -----------------------------  */
2657                 {
2658                         switch (icu->adrcode) {
2659                         case CAM_LENS:
2660                                 icu->ymin= 1.0;
2661                                 icu->ymax= 1000.0;
2662                                 break;
2663                         case CAM_STA:
2664                                 icu->ymin= 0.001f;
2665                                 break;
2666                         case CAM_END:
2667                                 icu->ymin= 0.1f;
2668                                 break;
2669                                 
2670                         case CAM_YF_APERT:
2671                                 icu->ymin = 0.0;
2672                                 icu->ymax = 2.0;
2673                                 break;
2674                         case CAM_YF_FDIST:
2675                                 icu->ymin = 0.0;
2676                                 icu->ymax = 5000.0;
2677                                 break;
2678                                 
2679                         case CAM_SHIFT_X:
2680                         case CAM_SHIFT_Y:
2681                                 icu->ymin= -2.0f;
2682                                 icu->ymax= 2.0f;
2683                                 break;
2684                         }
2685                 }
2686                         break;
2687                 case ID_SO: /* sound channels -----------------------------  */
2688                 {
2689                         switch (icu->adrcode) {
2690                         case SND_VOLUME:
2691                                 icu->ymin= 0.0;
2692                                 icu->ymax= 1.0;
2693                                 break;
2694                         case SND_PITCH:
2695                                 icu->ymin= -12.0;
2696                                 icu->ymin= 12.0;
2697                                 break;
2698                         case SND_PANNING:
2699                                 icu->ymin= 0.0;
2700                                 icu->ymax= 1.0;
2701                                 break;
2702                         case SND_ATTEN:
2703                                 icu->ymin= 0.0;
2704                                 icu->ymin= 1.0;
2705                                 break;
2706                         }
2707                 }
2708                         break;
2709                 case ID_PA: /* particle channels -----------------------------  */
2710                 {
2711                         switch (icu->adrcode) {
2712                         case PART_EMIT_LIFE:
2713                         case PART_SIZE:
2714                         case PART_KINK_FREQ:
2715                         case PART_EMIT_VEL:
2716                         case PART_EMIT_AVE:
2717                         case PART_EMIT_SIZE:
2718                                 icu->ymin= 0.0;
2719                                 break;
2720                         case PART_CLUMP:
2721                                 icu->ymin= -1.0;
2722                                 icu->ymax= 1.0;
2723                                 break;
2724                         case PART_DRAG:
2725                         case PART_DAMP:
2726                         case PART_LENGTH:
2727                                 icu->ymin= 0.0;
2728                                 icu->ymax= 1.0;
2729                                 break;
2730                         case PART_KINK_SHAPE:
2731                                 icu->ymin= -0.999;
2732                                 icu->ymax= 0.999;
2733                                 break;
2734                         }
2735                 }
2736                         break;
2737                 case ID_CO: /* constraint channels -----------------------------  */
2738                 {
2739                         icu->ymin= 0.0;
2740                         icu->ymax= 1.0f;
2741                 }
2742                         break;
2743         }
2744         
2745         /* by default, slider limits will be icu->ymin and icu->ymax */
2746         icu->slide_min= icu->ymin;
2747         icu->slide_max= icu->ymax;
2748 }
2749
2750 /* --------------------- Pointer I/O API ----------------------------- */
2751  
2752 /* write the given value directly into the given pointer */
2753 void write_ipo_poin (void *poin, int type, float val)
2754 {
2755         /* Note: we only support a limited number of types, with the value
2756          * to set needing to be cast to the appropriate type first
2757          *      -> (float to integer conversions could be slow)
2758          */
2759         switch(type) {
2760         case IPO_FLOAT:
2761                 *((float *)poin)= val;
2762                 break;
2763                 
2764         case IPO_FLOAT_DEGR: /* special hack for rotation so that it fits on same axis as other transforms */
2765                 *((float *)poin)= (float)(val * M_PI_2 / 9.0);
2766                 break;
2767                 
2768         case IPO_INT:
2769         case IPO_INT_BIT: // fixme... directly revealing bitflag combinations is evil!
2770         case IPO_LONG:
2771                 *((int *)poin)= (int)val;
2772                 break;
2773                 
2774         case IPO_SHORT:
2775         case IPO_SHORT_BIT: // fixme... directly revealing bitflag combinations is evil!
2776                 *((short *)poin)= (short)val;
2777                 break;
2778                 
2779         case IPO_CHAR:
2780         case IPO_CHAR_BIT: // fixme... directly revealing bitflag combinations is evil!
2781                 *((char *)poin)= (char)val;
2782                 break;
2783         }
2784 }
2785
2786 /* read the value from the pointer that was obtained */
2787 float read_ipo_poin (void *poin, int type)
2788 {
2789         float val = 0.0;
2790         
2791         /* Note: we only support a limited number of types, with the value
2792          * to set needing to be cast to the appropriate type first
2793          *      -> (int to float conversions may loose accuracy in rare cases)
2794          */
2795         switch (type) {
2796         case IPO_FLOAT:
2797                 val= *((float *)poin);
2798                 break;
2799                 
2800         case IPO_FLOAT_DEGR: /* special hack for rotation so that it fits on same axis as other transforms */
2801                 val= *( (float *)poin);
2802                 val = (float)(val / (M_PI_2/9.0));
2803                 break;
2804         
2805         case IPO_INT:
2806         case IPO_INT_BIT: // fixme... directly revealing bitflag combinations is evil!
2807         case IPO_LONG:
2808                 val= (float)( *((int *)poin) );
2809                 break;
2810                 
2811         case IPO_SHORT:
2812         case IPO_SHORT_BIT: // fixme... directly revealing bitflag combinations is evil!
2813                 val= *((short *)poin);
2814                 break;
2815         
2816         case IPO_CHAR:
2817         case IPO_CHAR_BIT: // fixme... directly revealing bitflag combinations is evil
2818                 val= *((char *)poin);
2819                 break;
2820         }
2821         
2822         /* return value */
2823         return val;
2824 }
2825
2826 // !!!!!!!!!!!!!!!!!!!!!!!!!!!! FIXME - BAD CRUFT WARNING !!!!!!!!!!!!!!!!!!!!!!!
2827
2828
2829 /* ***************************** IPO <--> GameEngine Interface ********************************* */
2830
2831 /* channels is max 32 items, allocated by calling function */
2832 short IPO_GetChannels (Ipo *ipo, IPO_Channel *channels)
2833 {
2834         IpoCurve *icu;
2835         int total = 0;
2836         
2837         /* don't do anything with no IPO-block */
2838         if (ipo == NULL) 
2839                 return 0;
2840         
2841         /* store the IPO-curve's adrcode in the relevant channel slot */
2842         for (icu=ipo->curve.first; (icu) && (total < 31); icu=icu->next, total++)
2843                 channels[total]= icu->adrcode;
2844         
2845         /* return the number of channels stored */
2846         return total;
2847 }
2848
2849 /* Get the float value for channel 'channel' at time 'ctime' */
2850 float IPO_GetFloatValue (Ipo *ipo, IPO_Channel channel, float ctime)
2851 {
2852         /* don't evaluate if no IPO to use */
2853         if (ipo == NULL) 
2854                 return 0;
2855         
2856         /* only calculate the specified channel */
2857         calc_ipo_spec(ipo, channel, &ctime);
2858         
2859         /* unapply rotation hack, as gameengine doesn't use it */
2860         if ((OB_ROT_X <= channel) && (channel <= OB_DROT_Z))
2861                 ctime *= (float)(M_PI_2/9.0); 
2862
2863         /* return the value of this channel */
2864         return ctime;
2865 }