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