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