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