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