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