b6f6820ca738f60ac79aaa63affebf3cca9383a2
[blender-staging.git] / source / blender / blenkernel / intern / object.c
1 /* object.c
2  *
3  * 
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include <string.h>
33 #include <math.h>
34 #include <stdio.h>                      
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "MEM_guardedalloc.h"
41
42 #include "DNA_action_types.h"
43 #include "DNA_armature_types.h"
44 #include "DNA_camera_types.h"
45 #include "DNA_constraint_types.h"
46 #include "DNA_curve_types.h"
47 #include "DNA_group_types.h"
48 #include "DNA_ipo_types.h"
49 #include "DNA_lamp_types.h"
50 #include "DNA_lattice_types.h"
51 #include "DNA_material_types.h"
52 #include "DNA_mesh_types.h"
53 #include "DNA_meta_types.h"
54 #include "DNA_curve_types.h"
55 #include "DNA_meshdata_types.h"
56 #include "DNA_modifier_types.h"
57 #include "DNA_nla_types.h"
58 #include "DNA_object_types.h"
59 #include "DNA_object_force.h"
60 #include "DNA_object_fluidsim.h"
61 #include "DNA_oops_types.h"
62 #include "DNA_particle_types.h"
63 #include "DNA_scene_types.h"
64 #include "DNA_screen_types.h"
65 #include "DNA_space_types.h"
66 #include "DNA_texture_types.h"
67 #include "DNA_userdef_types.h"
68 #include "DNA_view3d_types.h"
69 #include "DNA_world_types.h"
70
71 #include "BKE_armature.h"
72 #include "BKE_action.h"
73 #include "BKE_bullet.h"
74 #include "BKE_colortools.h"
75 #include "BKE_deform.h"
76 #include "BKE_DerivedMesh.h"
77 #include "BKE_nla.h"
78
79 #include "BLI_blenlib.h"
80 #include "BLI_arithb.h"
81 #include "BLI_editVert.h"
82
83 #include "BKE_utildefines.h"
84 #include "BKE_bad_level_calls.h"
85
86 #include "BKE_main.h"
87 #include "BKE_global.h"
88
89 #include "BKE_anim.h"
90 #include "BKE_blender.h"
91 #include "BKE_constraint.h"
92 #include "BKE_curve.h"
93 #include "BKE_displist.h"
94 #include "BKE_group.h"
95 #include "BKE_icons.h"
96 #include "BKE_ipo.h"
97 #include "BKE_key.h"
98 #include "BKE_lattice.h"
99 #include "BKE_library.h"
100 #include "BKE_mesh.h"
101 #include "BKE_mball.h"
102 #include "BKE_modifier.h"
103 #include "BKE_object.h"
104 #include "BKE_particle.h"
105 #include "BKE_pointcache.h"
106 #include "BKE_property.h"
107 #include "BKE_sca.h"
108 #include "BKE_scene.h"
109 #include "BKE_screen.h"
110 #include "BKE_softbody.h"
111
112 #include "LBM_fluidsim.h"
113
114 #include "BPY_extern.h"
115
116 #include "GPU_material.h"
117
118 #include "blendef.h"
119
120 /* Local function protos */
121 static void solve_parenting (Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul);
122
123 float originmat[3][3];  /* after where_is_object(), can be used in other functions (bad!) */
124 Object workob;
125
126 void clear_workob(void)
127 {
128         memset(&workob, 0, sizeof(Object));
129         
130         workob.size[0]= workob.size[1]= workob.size[2]= 1.0;
131         
132 }
133
134 void copy_baseflags()
135 {
136         Base *base= G.scene->base.first;
137         
138         while(base) {
139                 base->object->flag= base->flag;
140                 base= base->next;
141         }
142 }
143
144 void copy_objectflags()
145 {
146         Base *base= G.scene->base.first;
147         
148         while(base) {
149                 base->flag= base->object->flag;
150                 base= base->next;
151         }
152 }
153
154 void update_base_layer(Object *ob)
155 {
156         Base *base= G.scene->base.first;
157
158         while (base) {
159                 if (base->object == ob) base->lay= ob->lay;
160                 base= base->next;
161         }
162 }
163
164 void object_free_particlesystems(Object *ob)
165 {
166         while(ob->particlesystem.first){
167                 ParticleSystem *psys = ob->particlesystem.first;
168
169                 BLI_remlink(&ob->particlesystem,psys);
170
171                 psys_free(ob,psys);
172         }
173 }
174
175 void object_free_softbody(Object *ob)
176 {
177         if(ob->soft) {
178                 sbFree(ob->soft);
179                 ob->soft= NULL;
180         }
181 }
182
183 void object_free_bulletsoftbody(Object *ob)
184 {
185         if(ob->bsoft) {
186                 bsbFree(ob->bsoft);
187                 ob->bsoft= NULL;
188         }
189 }
190
191 void object_free_modifiers(Object *ob)
192 {
193         while (ob->modifiers.first) {
194                 ModifierData *md = ob->modifiers.first;
195
196                 BLI_remlink(&ob->modifiers, md);
197
198                 modifier_free(md);
199         }
200
201         /* particle modifiers were freed, so free the particlesystems as well */
202         object_free_particlesystems(ob);
203
204         /* same for softbody */
205         object_free_softbody(ob);
206 }
207
208 /* here we will collect all local displist stuff */
209 /* also (ab)used in depsgraph */
210 void object_free_display(Object *ob)
211 {
212         if(ob->derivedDeform) {
213                 ob->derivedDeform->needsFree = 1;
214                 ob->derivedDeform->release(ob->derivedDeform);
215                 ob->derivedDeform= NULL;
216         }
217         if(ob->derivedFinal) {
218                 ob->derivedFinal->needsFree = 1;
219                 ob->derivedFinal->release(ob->derivedFinal);
220                 ob->derivedFinal= NULL;
221         }
222         
223         freedisplist(&ob->disp);
224 }
225
226 /* do not free object itself */
227 void free_object(Object *ob)
228 {
229         int a;
230         
231         object_free_display(ob);
232         
233         /* disconnect specific data */
234         if(ob->data) {
235                 ID *id= ob->data;
236                 id->us--;
237                 if(id->us==0) {
238                         if(ob->type==OB_MESH) unlink_mesh(ob->data);
239                         else if(ob->type==OB_CURVE) unlink_curve(ob->data);
240                         else if(ob->type==OB_MBALL) unlink_mball(ob->data);
241                 }
242                 ob->data= 0;
243         }
244         
245         for(a=0; a<ob->totcol; a++) {
246                 if(ob->mat[a]) ob->mat[a]->id.us--;
247         }
248         if(ob->mat) MEM_freeN(ob->mat);
249         ob->mat= 0;
250         if(ob->bb) MEM_freeN(ob->bb); 
251         ob->bb= 0;
252         if(ob->path) free_path(ob->path); 
253         ob->path= 0;
254         if(ob->ipo) ob->ipo->id.us--;
255         if(ob->action) ob->action->id.us--;
256         if(ob->poselib) ob->poselib->id.us--;
257         if(ob->dup_group) ob->dup_group->id.us--;
258         if(ob->defbase.first)
259                 BLI_freelistN(&ob->defbase);
260         if(ob->pose)
261                 free_pose(ob->pose);
262         free_properties(&ob->prop);
263         object_free_modifiers(ob);
264         
265         free_sensors(&ob->sensors);
266         free_controllers(&ob->controllers);
267         free_actuators(&ob->actuators);
268         
269         free_constraints(&ob->constraints);
270         free_constraint_channels(&ob->constraintChannels);
271         free_nlastrips(&ob->nlastrips);
272         
273         BPY_free_scriptlink(&ob->scriptlink);
274         
275         if(ob->pd){
276                 if(ob->pd->tex)
277                         ob->pd->tex->id.us--;
278                 MEM_freeN(ob->pd);
279         }
280         if(ob->soft) sbFree(ob->soft);
281         if(ob->bsoft) bsbFree(ob->bsoft);
282         if(ob->gpulamp.first) GPU_lamp_free(ob);
283 }
284
285 static void unlink_object__unlinkModifierLinks(void *userData, Object *ob, Object **obpoin)
286 {
287         Object *unlinkOb = userData;
288
289         if (*obpoin==unlinkOb) {
290                 *obpoin = NULL;
291                 ob->recalc |= OB_RECALC;
292         }
293 }
294 void unlink_object(Object *ob)
295 {
296         Object *obt;
297         Material *mat;
298         World *wrld;
299         bScreen *sc;
300         Scene *sce;
301         Curve *cu;
302         Tex *tex;
303         Ipo *ipo;
304         Group *group;
305         Camera *camera;
306         bConstraint *con;
307         bActionStrip *strip;
308         ModifierData *md;
309         int a;
310         
311         unlink_controllers(&ob->controllers);
312         unlink_actuators(&ob->actuators);
313         
314         /* check all objects: parents en bevels and fields, also from libraries */
315         obt= G.main->object.first;
316         while(obt) {
317                 if(obt->proxy==ob)
318                         obt->proxy= NULL;
319                 if(obt->proxy_from==ob) {
320                         obt->proxy_from= NULL;
321                         obt->recalc |= OB_RECALC_OB;
322                 }
323                 if(obt->proxy_group==ob)
324                         obt->proxy_group= NULL;
325                 
326                 if(obt->parent==ob) {
327                         obt->parent= NULL;
328                         obt->recalc |= OB_RECALC;
329                 }
330                 
331                 if(obt->track==ob) {
332                         obt->track= NULL;
333                         obt->recalc |= OB_RECALC_OB;
334                 }
335                 
336                 modifiers_foreachObjectLink(obt, unlink_object__unlinkModifierLinks, ob);
337                 
338                 if ELEM(obt->type, OB_CURVE, OB_FONT) {
339                         cu= obt->data;
340
341                         if(cu->bevobj==ob) {
342                                 cu->bevobj= NULL;
343                                 obt->recalc |= OB_RECALC;
344                         }
345                         if(cu->taperobj==ob) {
346                                 cu->taperobj= NULL;
347                                 obt->recalc |= OB_RECALC;
348                         }
349                         if(cu->textoncurve==ob) {
350                                 cu->textoncurve= NULL;
351                                 obt->recalc |= OB_RECALC;
352                         }
353                 }
354                 else if(obt->type==OB_ARMATURE && obt->pose) {
355                         bPoseChannel *pchan;
356                         for(pchan= obt->pose->chanbase.first; pchan; pchan= pchan->next) {
357                                 for (con = pchan->constraints.first; con; con=con->next) {
358                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
359                                         ListBase targets = {NULL, NULL};
360                                         bConstraintTarget *ct;
361                                         
362                                         if (cti && cti->get_constraint_targets) {
363                                                 cti->get_constraint_targets(con, &targets);
364                                                 
365                                                 for (ct= targets.first; ct; ct= ct->next) {
366                                                         if (ct->tar == ob) {
367                                                                 ct->tar = NULL;
368                                                                 strcpy(ct->subtarget, "");
369                                                                 obt->recalc |= OB_RECALC_DATA;
370                                                         }
371                                                 }
372                                                 
373                                                 if (cti->flush_constraint_targets)
374                                                         cti->flush_constraint_targets(con, &targets, 0);
375                                         }
376                                 }
377                                 if(pchan->custom==ob)
378                                         pchan->custom= NULL;
379                         }
380                 }
381                 
382                 sca_remove_ob_poin(obt, ob);
383                 
384                 for (con = obt->constraints.first; con; con=con->next) {
385                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
386                         ListBase targets = {NULL, NULL};
387                         bConstraintTarget *ct;
388                         
389                         if (cti && cti->get_constraint_targets) {
390                                 cti->get_constraint_targets(con, &targets);
391                                 
392                                 for (ct= targets.first; ct; ct= ct->next) {
393                                         if (ct->tar == ob) {
394                                                 ct->tar = NULL;
395                                                 strcpy(ct->subtarget, "");
396                                                 obt->recalc |= OB_RECALC_DATA;
397                                         }
398                                 }
399                                 
400                                 if (cti->flush_constraint_targets)
401                                         cti->flush_constraint_targets(con, &targets, 0);
402                         }
403                 }
404                 
405                 /* object is deflector or field */
406                 if(ob->pd) {
407                         if(obt->soft)
408                                 obt->recalc |= OB_RECALC_DATA;
409
410                         /* cloth */
411                         for(md=obt->modifiers.first; md; md=md->next)
412                                 if(md->type == eModifierType_Cloth)
413                                         obt->recalc |= OB_RECALC_DATA;
414                 }
415                 
416                 /* strips */
417                 for(strip= obt->nlastrips.first; strip; strip= strip->next) {
418                         if(strip->object==ob)
419                                 strip->object= NULL;
420                         
421                         if(strip->modifiers.first) {
422                                 bActionModifier *amod;
423                                 for(amod= strip->modifiers.first; amod; amod= amod->next)
424                                         if(amod->ob==ob)
425                                                 amod->ob= NULL;
426                         }
427                 }
428
429                 /* particle systems */
430                 if(obt->particlesystem.first) {
431                         ParticleSystem *tpsys= obt->particlesystem.first;
432                         for(; tpsys; tpsys=tpsys->next) {
433                                 if(tpsys->keyed_ob==ob) {
434                                         ParticleSystem *psys= BLI_findlink(&ob->particlesystem,tpsys->keyed_psys-1);
435
436                                         if(psys && psys->keyed_ob) {
437                                                 tpsys->keyed_ob= psys->keyed_ob;
438                                                 tpsys->keyed_psys= psys->keyed_psys;
439                                         }
440                                         else
441                                                 tpsys->keyed_ob= NULL;
442
443                                         obt->recalc |= OB_RECALC_DATA;
444                                 }
445
446                                 if(tpsys->target_ob==ob) {
447                                         tpsys->target_ob= NULL;
448                                         obt->recalc |= OB_RECALC_DATA;
449                                 }
450
451                                 if(tpsys->part->dup_ob==ob)
452                                         tpsys->part->dup_ob= NULL;
453
454                                 if(tpsys->part->flag&PART_STICKY) {
455                                         ParticleData *pa;
456                                         int p;
457
458                                         for(p=0,pa=tpsys->particles; p<tpsys->totpart; p++,pa++) {
459                                                 if(pa->stick_ob==ob) {
460                                                         pa->stick_ob= 0;
461                                                         pa->flag &= ~PARS_STICKY;
462                                                 }
463                                         }
464                                 }
465                         }
466                         if(ob->pd)
467                                 obt->recalc |= OB_RECALC_DATA;
468                 }
469
470                 obt= obt->id.next;
471         }
472         
473         /* materials */
474         mat= G.main->mat.first;
475         while(mat) {
476         
477                 for(a=0; a<MAX_MTEX; a++) {
478                         if(mat->mtex[a] && ob==mat->mtex[a]->object) {
479                                 /* actually, test for lib here... to do */
480                                 mat->mtex[a]->object= NULL;
481                         }
482                 }
483
484                 mat= mat->id.next;
485         }
486         
487         /* textures */
488         tex= G.main->tex.first;
489         while(tex) {
490                 if(tex->env) {
491                         if(tex->env->object == ob) tex->env->object= NULL;
492                 }
493                 tex= tex->id.next;
494         }
495         
496         /* mballs */
497         if(ob->type==OB_MBALL) {
498                 obt= find_basis_mball(ob);
499                 if(obt) freedisplist(&obt->disp);
500         }
501         
502         /* worlds */
503         wrld= G.main->world.first;
504         while(wrld) {
505                 if(wrld->id.lib==NULL) {
506                         for(a=0; a<MAX_MTEX; a++) {
507                                 if(wrld->mtex[a] && ob==wrld->mtex[a]->object)
508                                         wrld->mtex[a]->object= NULL;
509                         }
510                 }
511                 
512                 wrld= wrld->id.next;
513         }
514                 
515         /* scenes */
516         sce= G.main->scene.first;
517         while(sce) {
518                 if(sce->id.lib==NULL) {
519                         if(sce->camera==ob) sce->camera= NULL;
520                 }
521                 sce= sce->id.next;
522         }
523         /* ipos */
524         ipo= G.main->ipo.first;
525         while(ipo) {
526                 if(ipo->id.lib==NULL) {
527                         IpoCurve *icu;
528                         for(icu= ipo->curve.first; icu; icu= icu->next) {
529                                 if(icu->driver && icu->driver->ob==ob)
530                                         icu->driver->ob= NULL;
531                         }
532                 }
533                 ipo= ipo->id.next;
534         }
535         
536         /* screens */
537         sc= G.main->screen.first;
538         while(sc) {
539                 ScrArea *sa= sc->areabase.first;
540                 while(sa) {
541                         SpaceLink *sl;
542
543                         for (sl= sa->spacedata.first; sl; sl= sl->next) {
544                                 if(sl->spacetype==SPACE_VIEW3D) {
545                                         View3D *v3d= (View3D*) sl;
546
547                                         if(v3d->camera==ob) {
548                                                 v3d->camera= NULL;
549                                                 if(v3d->persp==V3D_CAMOB) v3d->persp= V3D_PERSP;
550                                         }
551                                         if(v3d->localvd && v3d->localvd->camera==ob ) {
552                                                 v3d->localvd->camera= NULL;
553                                                 if(v3d->localvd->persp==V3D_CAMOB) v3d->localvd->persp= V3D_PERSP;
554                                         }
555                                 }
556                                 else if(sl->spacetype==SPACE_IPO) {
557                                         SpaceIpo *sipo= (SpaceIpo *)sl;
558                                         if(sipo->from == (ID *)ob) sipo->from= NULL;
559                                 }
560                                 else if(sl->spacetype==SPACE_OOPS) {
561                                         SpaceOops *so= (SpaceOops *)sl;
562                                         Oops *oops;
563
564                                         oops= so->oops.first;
565                                         while(oops) {
566                                                 if(oops->id==(ID *)ob) oops->id= NULL;
567                                                 oops= oops->next;
568                                         }
569                                         if(so->treestore) {
570                                                 TreeStoreElem *tselem= so->treestore->data;
571                                                 int a;
572                                                 for(a=0; a<so->treestore->usedelem; a++, tselem++) {
573                                                         if(tselem->id==(ID *)ob) tselem->id= NULL;
574                                                 }
575                                         }
576                                         so->lockpoin= NULL;
577                                 }
578                         }
579
580                         sa= sa->next;
581                 }
582                 sc= sc->id.next;
583         }
584
585         /* groups */
586         group= G.main->group.first;
587         while(group) {
588                 rem_from_group(group, ob);
589                 group= group->id.next;
590         }
591         
592         /* cameras */
593         camera= G.main->camera.first;
594         while(camera) {
595                 if (camera->dof_ob==ob) {
596                         camera->dof_ob = NULL;
597                 }
598                 camera= camera->id.next;
599         }
600 }
601
602 int exist_object(Object *obtest)
603 {
604         Object *ob;
605         
606         if(obtest==NULL) return 0;
607         
608         ob= G.main->object.first;
609         while(ob) {
610                 if(ob==obtest) return 1;
611                 ob= ob->id.next;
612         }
613         return 0;
614 }
615
616 void *add_camera(char *name)
617 {
618         Camera *cam;
619         
620         cam=  alloc_libblock(&G.main->camera, ID_CA, name);
621
622         cam->lens= 35.0f;
623         cam->angle= 49.14f;
624         cam->clipsta= 0.1f;
625         cam->clipend= 100.0f;
626         cam->drawsize= 0.5f;
627         cam->ortho_scale= 6.0;
628         cam->flag |= CAM_SHOWTITLESAFE;
629         cam->passepartalpha = 0.2f;
630         
631         return cam;
632 }
633
634 Camera *copy_camera(Camera *cam)
635 {
636         Camera *camn;
637         
638         camn= copy_libblock(cam);
639         id_us_plus((ID *)camn->ipo);
640
641         BPY_copy_scriptlink(&camn->scriptlink);
642         
643         return camn;
644 }
645
646
647
648 void make_local_camera(Camera *cam)
649 {
650         Object *ob;
651         Camera *camn;
652         int local=0, lib=0;
653
654         /* - only lib users: do nothing
655             * - only local users: set flag
656             * - mixed: make copy
657             */
658         
659         if(cam->id.lib==0) return;
660         if(cam->id.us==1) {
661                 cam->id.lib= 0;
662                 cam->id.flag= LIB_LOCAL;
663                 new_id(0, (ID *)cam, 0);
664                 return;
665         }
666         
667         ob= G.main->object.first;
668         while(ob) {
669                 if(ob->data==cam) {
670                         if(ob->id.lib) lib= 1;
671                         else local= 1;
672                 }
673                 ob= ob->id.next;
674         }
675         
676         if(local && lib==0) {
677                 cam->id.lib= 0;
678                 cam->id.flag= LIB_LOCAL;
679                 new_id(0, (ID *)cam, 0);
680         }
681         else if(local && lib) {
682                 camn= copy_camera(cam);
683                 camn->id.us= 0;
684                 
685                 ob= G.main->object.first;
686                 while(ob) {
687                         if(ob->data==cam) {
688                                 
689                                 if(ob->id.lib==0) {
690                                         ob->data= camn;
691                                         camn->id.us++;
692                                         cam->id.us--;
693                                 }
694                         }
695                         ob= ob->id.next;
696                 }
697         }
698 }
699
700 /* get the camera's dof value, takes the dof object into account */
701 float dof_camera(Object *ob)
702 {
703         Camera *cam = (Camera *)ob->data; 
704         if (ob->type != OB_CAMERA)
705                 return 0.0;
706         if (cam->dof_ob) {      
707                 /* too simple, better to return the distance on the view axis only
708                  * return VecLenf(ob->obmat[3], cam->dof_ob->obmat[3]); */
709                 float mat[4][4], obmat[4][4];
710                 
711                 Mat4CpyMat4(obmat, ob->obmat);
712                 Mat4Ortho(obmat);
713                 Mat4Invert(ob->imat, obmat);
714                 Mat4MulMat4(mat, cam->dof_ob->obmat, ob->imat);
715                 return fabs(mat[3][2]);
716         }
717         return cam->YF_dofdist;
718 }
719
720 void *add_lamp(char *name)
721 {
722         Lamp *la;
723         
724         la=  alloc_libblock(&G.main->lamp, ID_LA, name);
725         
726         la->r= la->g= la->b= la->k= 1.0;
727         la->haint= la->energy= 1.0;
728         la->dist= 20.0;
729         la->spotsize= 45.0;
730         la->spotblend= 0.15;
731         la->att2= 1.0;
732         la->mode= LA_SHAD_BUF;
733         la->bufsize= 512;
734         la->clipsta= 0.5;
735         la->clipend= 40.0;
736         la->shadspotsize= 45.0;
737         la->samp= 3;
738         la->bias= 1.0;
739         la->soft= 3.0;
740         la->ray_samp= la->ray_sampy= la->ray_sampz= 1; 
741         la->area_size=la->area_sizey=la->area_sizez= 1.0; 
742         la->buffers= 1;
743         la->buftype= LA_SHADBUF_HALFWAY;
744         la->ray_samp_method = LA_SAMP_HALTON;
745         la->adapt_thresh = 0.001;
746         la->preview=NULL;
747         la->falloff_type = LA_FALLOFF_INVLINEAR;
748         la->curfalloff = curvemapping_add(1, 0.0f, 1.0f, 1.0f, 0.0f);
749         la->sun_effect_type = 0;
750         la->horizon_brightness = 1.0;
751         la->spread = 1.0;
752         la->sun_brightness = 1.0;
753         la->sun_size = 1.0;
754         la->backscattered_light = 1.0;
755         la->atm_turbidity = 2.0;
756         la->atm_inscattering_factor = 1.0;
757         la->atm_extinction_factor = 1.0;
758         la->atm_distance_factor = 1.0;
759         la->sun_intensity = 1.0;
760         la->skyblendtype= MA_RAMP_ADD;
761         la->skyblendfac= 1.0f;
762         la->sky_colorspace= BLI_CS_CIE;
763         la->sky_exposure= 1.0f;
764         
765         curvemapping_initialize(la->curfalloff);
766         return la;
767 }
768
769 Lamp *copy_lamp(Lamp *la)
770 {
771         Lamp *lan;
772         int a;
773         
774         lan= copy_libblock(la);
775
776         for(a=0; a<MAX_MTEX; a++) {
777                 if(lan->mtex[a]) {
778                         lan->mtex[a]= MEM_mallocN(sizeof(MTex), "copylamptex");
779                         memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
780                         id_us_plus((ID *)lan->mtex[a]->tex);
781                 }
782         }
783         
784         lan->curfalloff = curvemapping_copy(la->curfalloff);
785         
786         id_us_plus((ID *)lan->ipo);
787
788         if (la->preview) lan->preview = BKE_previewimg_copy(la->preview);
789
790         BPY_copy_scriptlink(&la->scriptlink);
791         
792         return lan;
793 }
794
795 void make_local_lamp(Lamp *la)
796 {
797         Object *ob;
798         Lamp *lan;
799         int local=0, lib=0;
800
801         /* - only lib users: do nothing
802             * - only local users: set flag
803             * - mixed: make copy
804             */
805         
806         if(la->id.lib==0) return;
807         if(la->id.us==1) {
808                 la->id.lib= 0;
809                 la->id.flag= LIB_LOCAL;
810                 new_id(0, (ID *)la, 0);
811                 return;
812         }
813         
814         ob= G.main->object.first;
815         while(ob) {
816                 if(ob->data==la) {
817                         if(ob->id.lib) lib= 1;
818                         else local= 1;
819                 }
820                 ob= ob->id.next;
821         }
822         
823         if(local && lib==0) {
824                 la->id.lib= 0;
825                 la->id.flag= LIB_LOCAL;
826                 new_id(0, (ID *)la, 0);
827         }
828         else if(local && lib) {
829                 lan= copy_lamp(la);
830                 lan->id.us= 0;
831                 
832                 ob= G.main->object.first;
833                 while(ob) {
834                         if(ob->data==la) {
835                                 
836                                 if(ob->id.lib==0) {
837                                         ob->data= lan;
838                                         lan->id.us++;
839                                         la->id.us--;
840                                 }
841                         }
842                         ob= ob->id.next;
843                 }
844         }
845 }
846
847 void free_camera(Camera *ca)
848 {
849         BPY_free_scriptlink(&ca->scriptlink);
850 }
851
852 void free_lamp(Lamp *la)
853 {
854         MTex *mtex;
855         int a;
856
857         /* scriptlinks */
858                 
859         BPY_free_scriptlink(&la->scriptlink);
860
861         for(a=0; a<MAX_MTEX; a++) {
862                 mtex= la->mtex[a];
863                 if(mtex && mtex->tex) mtex->tex->id.us--;
864                 if(mtex) MEM_freeN(mtex);
865         }
866         la->ipo= 0;
867
868         curvemapping_free(la->curfalloff);
869         
870         BKE_previewimg_free(&la->preview);
871         BKE_icon_delete(&la->id);
872         la->id.icon_id = 0;
873 }
874
875 void *add_wave()
876 {
877         return 0;
878 }
879
880
881 /* *************************************************** */
882
883 static void *add_obdata_from_type(int type)
884 {
885         switch (type) {
886         case OB_MESH: G.totmesh++; return add_mesh("Mesh");
887         case OB_CURVE: G.totcurve++; return add_curve("Curve", OB_CURVE);
888         case OB_SURF: G.totcurve++; return add_curve("Surf", OB_SURF);
889         case OB_FONT: return add_curve("Text", OB_FONT);
890         case OB_MBALL: return add_mball("Meta");
891         case OB_CAMERA: return add_camera("Camera");
892         case OB_LAMP: G.totlamp++; return add_lamp("Lamp");
893         case OB_LATTICE: return add_lattice("Lattice");
894         case OB_WAVE: return add_wave();
895         case OB_ARMATURE: return add_armature("Armature");
896         case OB_EMPTY: return NULL;
897         default:
898                 printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
899                 return NULL;
900         }
901 }
902
903 static char *get_obdata_defname(int type)
904 {
905         switch (type) {
906         case OB_MESH: return "Mesh";
907         case OB_CURVE: return "Curve";
908         case OB_SURF: return "Surf";
909         case OB_FONT: return "Font";
910         case OB_MBALL: return "Mball";
911         case OB_CAMERA: return "Camera";
912         case OB_LAMP: return "Lamp";
913         case OB_LATTICE: return "Lattice";
914         case OB_WAVE: return "Wave";
915         case OB_ARMATURE: return "Armature";
916         case OB_EMPTY: return "Empty";
917         default:
918                 printf("get_obdata_defname: Internal error, bad type: %d\n", type);
919                 return "Empty";
920         }
921 }
922
923 /* more general add: creates minimum required data, but without vertices etc. */
924 Object *add_only_object(int type, char *name)
925 {
926         Object *ob;
927
928         ob= alloc_libblock(&G.main->object, ID_OB, name);
929         G.totobj++;
930
931         /* default object vars */
932         ob->type= type;
933         /* ob->transflag= OB_QUAT; */
934
935 #if 0 /* not used yet */
936         QuatOne(ob->quat);
937         QuatOne(ob->dquat);
938 #endif 
939
940         ob->col[0]= ob->col[1]= ob->col[2]= 1.0;
941         ob->col[3]= 1.0;
942
943         ob->loc[0]= ob->loc[1]= ob->loc[2]= 0.0;
944         ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
945         ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
946
947         Mat4One(ob->constinv);
948         Mat4One(ob->parentinv);
949         Mat4One(ob->obmat);
950         ob->dt= OB_SHADED;
951         if(U.flag & USER_MAT_ON_OB) ob->colbits= -1;
952         ob->empty_drawtype= OB_ARROWS;
953         ob->empty_drawsize= 1.0;
954
955         if(type==OB_CAMERA || type==OB_LAMP) {
956                 ob->trackflag= OB_NEGZ;
957                 ob->upflag= OB_POSY;
958         }
959         else {
960                 ob->trackflag= OB_POSY;
961                 ob->upflag= OB_POSZ;
962         }
963         ob->ipoflag = OB_OFFS_OB+OB_OFFS_PARENT;
964         ob->ipowin= ID_OB;      /* the ipowin shown */
965         ob->dupon= 1; ob->dupoff= 0;
966         ob->dupsta= 1; ob->dupend= 100;
967         ob->dupfacesca = 1.0;
968
969         /* Game engine defaults*/
970         ob->mass= ob->inertia= 1.0f;
971         ob->formfactor= 0.4f;
972         ob->damping= 0.04f;
973         ob->rdamping= 0.1f;
974         ob->anisotropicFriction[0] = 1.0f;
975         ob->anisotropicFriction[1] = 1.0f;
976         ob->anisotropicFriction[2] = 1.0f;
977         ob->gameflag= OB_PROP|OB_COLLISION;
978         ob->margin = 0.0;
979         
980         /* NT fluid sim defaults */
981         ob->fluidsimFlag = 0;
982         ob->fluidsimSettings = NULL;
983
984         return ob;
985 }
986
987 /* general add: to G.scene, with layer from area and default name */
988 /* creates minimum required data, but without vertices etc. */
989 Object *add_object(int type)
990 {
991         Object *ob;
992         Base *base;
993         char name[32];
994
995         strcpy(name, get_obdata_defname(type));
996         ob = add_only_object(type, name);
997
998         ob->data= add_obdata_from_type(type);
999
1000         ob->lay= G.scene->lay;
1001
1002         base= scene_add_base(G.scene, ob);
1003         scene_select_base(G.scene, base);
1004         ob->recalc |= OB_RECALC;
1005
1006         return ob;
1007 }
1008
1009 void base_init_from_view3d(Base *base, View3D *v3d)
1010 {
1011         Object *ob= base->object;
1012         
1013         if (!v3d) {
1014                 /* no 3d view, this wont happen often */
1015                 base->lay = 1;
1016                 VECCOPY(ob->loc, G.scene->cursor);
1017                 
1018                 /* return now because v3d->viewquat isnt available */
1019                 return;
1020         } else if (v3d->localview) {
1021                 base->lay= ob->lay= v3d->layact + v3d->lay;
1022                 VECCOPY(ob->loc, v3d->cursor);
1023         } else {
1024                 base->lay= ob->lay= v3d->layact;
1025                 VECCOPY(ob->loc, G.scene->cursor);
1026         }
1027
1028         if (U.flag & USER_ADD_VIEWALIGNED) {
1029                 v3d->viewquat[0]= -v3d->viewquat[0];
1030
1031                 /* Quats arnt used yet */
1032                 /*if (ob->transflag & OB_QUAT) {
1033                         QUATCOPY(ob->quat, v3d->viewquat);
1034                 } else {*/
1035                         QuatToEul(v3d->viewquat, ob->rot);
1036                 /*}*/
1037                 v3d->viewquat[0]= -v3d->viewquat[0];
1038         }
1039 }
1040
1041 SoftBody *copy_softbody(SoftBody *sb)
1042 {
1043         SoftBody *sbn;
1044         
1045         if (sb==NULL) return(NULL);
1046         
1047         sbn= MEM_dupallocN(sb);
1048         sbn->totspring= sbn->totpoint= 0;
1049         sbn->bpoint= NULL;
1050         sbn->bspring= NULL;
1051         
1052         sbn->keys= NULL;
1053         sbn->totkey= sbn->totpointkey= 0;
1054         
1055         sbn->scratch= NULL;
1056
1057         sbn->pointcache= BKE_ptcache_copy(sb->pointcache);
1058
1059         return sbn;
1060 }
1061
1062 BulletSoftBody *copy_bulletsoftbody(BulletSoftBody *bsb)
1063 {
1064         BulletSoftBody *bsbn;
1065
1066         if (bsb == NULL)
1067                 return NULL;
1068         bsbn = MEM_dupallocN(bsb);
1069         /* no pointer in this structure yet */
1070         return bsbn;
1071 }
1072
1073 ParticleSystem *copy_particlesystem(ParticleSystem *psys)
1074 {
1075         ParticleSystem *psysn;
1076         ParticleData *pa;
1077         int a;
1078
1079         psysn= MEM_dupallocN(psys);
1080         psysn->particles= MEM_dupallocN(psys->particles);
1081         psysn->child= MEM_dupallocN(psys->child);
1082
1083         for(a=0, pa=psysn->particles; a<psysn->totpart; a++, pa++) {
1084                 if(pa->hair)
1085                         pa->hair= MEM_dupallocN(pa->hair);
1086                 if(pa->keys)
1087                         pa->keys= MEM_dupallocN(pa->keys);
1088         }
1089
1090         if(psys->soft) {
1091                 psysn->soft= copy_softbody(psys->soft);
1092                 psysn->soft->particles = psysn;
1093         }
1094         
1095         psysn->pathcache= NULL;
1096         psysn->childcache= NULL;
1097         psysn->edit= NULL;
1098         psysn->effectors.first= psysn->effectors.last= 0;
1099         
1100         psysn->pathcachebufs.first = psysn->pathcachebufs.last = NULL;
1101         psysn->childcachebufs.first = psysn->childcachebufs.last = NULL;
1102         psysn->reactevents.first = psysn->reactevents.last = NULL;
1103         psysn->renderdata = NULL;
1104         
1105         psysn->pointcache= BKE_ptcache_copy(psys->pointcache);
1106
1107         id_us_plus((ID *)psysn->part);
1108
1109         return psysn;
1110 }
1111
1112 void copy_object_particlesystems(Object *obn, Object *ob)
1113 {
1114         ParticleSystemModifierData *psmd;
1115         ParticleSystem *psys, *npsys;
1116         ModifierData *md;
1117
1118         obn->particlesystem.first= obn->particlesystem.last= NULL;
1119         for(psys=ob->particlesystem.first; psys; psys=psys->next) {
1120                 npsys= copy_particlesystem(psys);
1121
1122                 BLI_addtail(&obn->particlesystem, npsys);
1123
1124                 /* need to update particle modifiers too */
1125                 for(md=obn->modifiers.first; md; md=md->next) {
1126                         if(md->type==eModifierType_ParticleSystem) {
1127                                 psmd= (ParticleSystemModifierData*)md;
1128                                 if(psmd->psys==psys)
1129                                         psmd->psys= npsys;
1130                         }
1131                 }
1132         }
1133 }
1134
1135 void copy_object_softbody(Object *obn, Object *ob)
1136 {
1137         if(ob->soft)
1138                 obn->soft= copy_softbody(ob->soft);
1139 }
1140
1141 static void copy_object_pose(Object *obn, Object *ob)
1142 {
1143         bPoseChannel *chan;
1144         
1145         /* note: need to clear obn->pose pointer first, so that copy_pose works (otherwise there's a crash) */
1146         obn->pose= NULL;
1147         copy_pose(&obn->pose, ob->pose, 1);     /* 1 = copy constraints */
1148
1149         for (chan = obn->pose->chanbase.first; chan; chan=chan->next){
1150                 bConstraint *con;
1151                 
1152                 chan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
1153                 
1154                 for (con= chan->constraints.first; con; con= con->next) {
1155                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1156                         ListBase targets = {NULL, NULL};
1157                         bConstraintTarget *ct;
1158                         
1159                         if(con->ipo) {
1160                                 IpoCurve *icu;
1161                                 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
1162                                         if(icu->driver && icu->driver->ob==ob)
1163                                                 icu->driver->ob= obn;
1164                                 }
1165                         }
1166                         
1167                         if (cti && cti->get_constraint_targets) {
1168                                 cti->get_constraint_targets(con, &targets);
1169                                 
1170                                 for (ct= targets.first; ct; ct= ct->next) {
1171                                         if (ct->tar == ob)
1172                                                 ct->tar = obn;
1173                                 }
1174                                 
1175                                 if (cti->flush_constraint_targets)
1176                                         cti->flush_constraint_targets(con, &targets, 0);
1177                         }
1178                 }
1179         }
1180 }
1181
1182 Object *copy_object(Object *ob)
1183 {
1184         Object *obn;
1185         ModifierData *md;
1186         int a;
1187
1188         obn= copy_libblock(ob);
1189         
1190         if(ob->totcol) {
1191                 obn->mat= MEM_dupallocN(ob->mat);
1192         }
1193         
1194         if(ob->bb) obn->bb= MEM_dupallocN(ob->bb);
1195         obn->path= NULL;
1196         obn->flag &= ~OB_FROMGROUP;
1197         
1198         obn->modifiers.first = obn->modifiers.last= NULL;
1199         
1200         for (md=ob->modifiers.first; md; md=md->next) {
1201                 ModifierData *nmd = modifier_new(md->type);
1202                 modifier_copyData(md, nmd);
1203                 BLI_addtail(&obn->modifiers, nmd);
1204         }
1205         
1206         BPY_copy_scriptlink(&ob->scriptlink);
1207         
1208         obn->prop.first = obn->prop.last = NULL;
1209         copy_properties(&obn->prop, &ob->prop);
1210         
1211         copy_sensors(&obn->sensors, &ob->sensors);
1212         copy_controllers(&obn->controllers, &ob->controllers);
1213         copy_actuators(&obn->actuators, &ob->actuators);
1214         
1215         if(ob->pose) {
1216                 copy_object_pose(obn, ob);
1217                 /* backwards compat... non-armatures can get poses in older files? */
1218                 if(ob->type==OB_ARMATURE)
1219                         armature_rebuild_pose(obn, obn->data);
1220         }
1221         copy_defgroups(&obn->defbase, &ob->defbase);
1222         copy_nlastrips(&obn->nlastrips, &ob->nlastrips);
1223         copy_constraints (&obn->constraints, &ob->constraints);
1224
1225         clone_constraint_channels (&obn->constraintChannels, &ob->constraintChannels);
1226
1227         /* increase user numbers */
1228         id_us_plus((ID *)obn->data);
1229         id_us_plus((ID *)obn->ipo);
1230         id_us_plus((ID *)obn->action);
1231         id_us_plus((ID *)obn->dup_group);
1232
1233         for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
1234         
1235         obn->disp.first= obn->disp.last= NULL;
1236         
1237         if(ob->pd){
1238                 obn->pd= MEM_dupallocN(ob->pd);
1239                 if(obn->pd->tex)
1240                         id_us_plus(&(obn->pd->tex->id));
1241         }
1242         obn->soft= copy_softbody(ob->soft);
1243         obn->bsoft = copy_bulletsoftbody(ob->bsoft);
1244
1245         copy_object_particlesystems(obn, ob);
1246         
1247         obn->derivedDeform = NULL;
1248         obn->derivedFinal = NULL;
1249
1250 #ifdef WITH_VERSE
1251         obn->vnode = NULL;
1252 #endif
1253
1254         obn->gpulamp.first = obn->gpulamp.last = NULL;
1255
1256         return obn;
1257 }
1258
1259 void expand_local_object(Object *ob)
1260 {
1261         bActionStrip *strip;
1262         ParticleSystem *psys;
1263         int a;
1264         
1265         id_lib_extern((ID *)ob->action);
1266         id_lib_extern((ID *)ob->ipo);
1267         id_lib_extern((ID *)ob->data);
1268         id_lib_extern((ID *)ob->dup_group);
1269         
1270         for(a=0; a<ob->totcol; a++) {
1271                 id_lib_extern((ID *)ob->mat[a]);
1272         }
1273         for (strip=ob->nlastrips.first; strip; strip=strip->next) {
1274                 id_lib_extern((ID *)strip->act);
1275         }
1276         for(psys=ob->particlesystem.first; psys; psys=psys->next)
1277                 id_lib_extern((ID *)psys->part);
1278 }
1279
1280 void make_local_object(Object *ob)
1281 {
1282         Object *obn;
1283         Scene *sce;
1284         Base *base;
1285         int local=0, lib=0;
1286
1287         /* - only lib users: do nothing
1288             * - only local users: set flag
1289             * - mixed: make copy
1290             */
1291         
1292         if(ob->id.lib==NULL) return;
1293         
1294         ob->proxy= ob->proxy_from= NULL;
1295         
1296         if(ob->id.us==1) {
1297                 ob->id.lib= NULL;
1298                 ob->id.flag= LIB_LOCAL;
1299                 new_id(0, (ID *)ob, 0);
1300
1301         }
1302         else {
1303                 sce= G.main->scene.first;
1304                 while(sce) {
1305                         base= sce->base.first;
1306                         while(base) {
1307                                 if(base->object==ob) {
1308                                         if(sce->id.lib) lib++;
1309                                         else local++;
1310                                         break;
1311                                 }
1312                                 base= base->next;
1313                         }
1314                         sce= sce->id.next;
1315                 }
1316                 
1317                 if(local && lib==0) {
1318                         ob->id.lib= 0;
1319                         ob->id.flag= LIB_LOCAL;
1320                         new_id(0, (ID *)ob, 0);
1321                 }
1322                 else if(local && lib) {
1323                         obn= copy_object(ob);
1324                         obn->id.us= 0;
1325                         
1326                         sce= G.main->scene.first;
1327                         while(sce) {
1328                                 if(sce->id.lib==0) {
1329                                         base= sce->base.first;
1330                                         while(base) {
1331                                                 if(base->object==ob) {
1332                                                         base->object= obn;
1333                                                         obn->id.us++;
1334                                                         ob->id.us--;
1335                                                 }
1336                                                 base= base->next;
1337                                         }
1338                                 }
1339                                 sce= sce->id.next;
1340                         }
1341                 }
1342         }
1343         
1344         expand_local_object(ob);
1345 }
1346
1347 /* *************** PROXY **************** */
1348
1349 /* when you make proxy, ensure the exposed layers are extern */
1350 void armature_set_id_extern(Object *ob)
1351 {
1352         bArmature *arm= ob->data;
1353         bPoseChannel *pchan;
1354         int lay= arm->layer_protected;
1355         
1356         for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1357                 if(!(pchan->bone->layer & lay))
1358                         id_lib_extern((ID *)pchan->custom);
1359         }
1360                         
1361 }
1362
1363 /* proxy rule: lib_object->proxy_from == the one we borrow from, set temporally while object_update */
1364 /*             local_object->proxy == pointer to library object, saved in files and read */
1365 /*             local_object->proxy_group == pointer to group dupli-object, saved in files and read */
1366
1367 void object_make_proxy(Object *ob, Object *target, Object *gob)
1368 {
1369         /* paranoia checks */
1370         if(ob->id.lib || target->id.lib==NULL) {
1371                 printf("cannot make proxy\n");
1372                 return;
1373         }
1374         
1375         ob->proxy= target;
1376         ob->proxy_group= gob;
1377         id_lib_extern(&target->id);
1378         
1379         ob->recalc= target->recalc= OB_RECALC;
1380         
1381         /* copy transform */
1382         if(gob) {
1383                 VECCOPY(ob->loc, gob->loc);
1384                 VECCOPY(ob->rot, gob->rot);
1385                 VECCOPY(ob->size, gob->size);
1386                 
1387                 group_tag_recalc(gob->dup_group);
1388         }
1389         else {
1390                 VECCOPY(ob->loc, target->loc);
1391                 VECCOPY(ob->rot, target->rot);
1392                 VECCOPY(ob->size, target->size);
1393         }
1394         
1395         ob->parent= target->parent;     /* libdata */
1396         Mat4CpyMat4(ob->parentinv, target->parentinv);
1397         ob->ipo= target->ipo;           /* libdata */
1398         
1399         /* skip constraints, constraintchannels, nla? */
1400         
1401         /* set object type and link to data */
1402         ob->type= target->type;
1403         ob->data= target->data;
1404         id_us_plus((ID *)ob->data);             /* ensures lib data becomes LIB_EXTERN */
1405         
1406         /* copy material and index information */
1407         ob->actcol= ob->totcol= 0;
1408         if(ob->mat) MEM_freeN(ob->mat);
1409         ob->mat = NULL;
1410         if ((target->totcol) && (target->mat) && OB_SUPPORT_MATERIAL(ob)) {
1411                 int i;
1412                 ob->colbits = target->colbits;
1413                 
1414                 ob->actcol= target->actcol;
1415                 ob->totcol= target->totcol;
1416                 
1417                 ob->mat = MEM_dupallocN(target->mat);
1418                 for(i=0; i<target->totcol; i++) {
1419                         /* dont need to run test_object_materials since we know this object is new and not used elsewhere */
1420                         id_us_plus((ID *)ob->mat[i]); 
1421                 }
1422         }
1423         
1424         /* type conversions */
1425         if(target->type == OB_ARMATURE) {
1426                 copy_object_pose(ob, target);   /* data copy, object pointers in constraints */
1427                 rest_pose(ob->pose);                    /* clear all transforms in channels */
1428                 armature_rebuild_pose(ob, ob->data);    /* set all internal links */
1429                 
1430                 armature_set_id_extern(ob);
1431         }
1432 }
1433
1434
1435 /* *************** CALC ****************** */
1436
1437 /* there is also a timing calculation in drawobject() */
1438
1439 float bluroffs= 0.0f, fieldoffs= 0.0f;
1440 int no_speed_curve= 0;
1441
1442 /* ugly calls from render */
1443 void set_mblur_offs(float blur)
1444 {
1445         bluroffs= blur;
1446 }
1447
1448 void set_field_offs(float field)
1449 {
1450         fieldoffs= field;
1451 }
1452
1453 void disable_speed_curve(int val)
1454 {
1455         no_speed_curve= val;
1456 }
1457
1458 /* ob can be NULL */
1459 float bsystem_time(Object *ob, float cfra, float ofs)
1460 {
1461         /* returns float ( see frame_to_float in ipo.c) */
1462         
1463         /* bluroffs and fieldoffs are ugly globals that are set by render */
1464         cfra+= bluroffs+fieldoffs;
1465
1466         /* global time */
1467         cfra*= G.scene->r.framelen;     
1468         
1469         if (ob) {
1470                 if (no_speed_curve==0 && ob->ipo)
1471                         cfra= calc_ipo_time(ob->ipo, cfra);
1472                 
1473                 /* ofset frames */
1474                 if ((ob->ipoflag & OB_OFFS_PARENT) && (ob->partype & PARSLOW)==0) 
1475                         cfra-= give_timeoffset(ob);
1476         }
1477         
1478         cfra-= ofs;
1479
1480         return cfra;
1481 }
1482
1483 void object_scale_to_mat3(Object *ob, float mat[][3])
1484 {
1485         float vec[3];
1486         if(ob->ipo) {
1487                 vec[0]= ob->size[0]+ob->dsize[0];
1488                 vec[1]= ob->size[1]+ob->dsize[1];
1489                 vec[2]= ob->size[2]+ob->dsize[2];
1490                 SizeToMat3(vec, mat);
1491         }
1492         else {
1493                 SizeToMat3(ob->size, mat);
1494         }
1495 }
1496
1497 void object_rot_to_mat3(Object *ob, float mat[][3])
1498 {
1499         float vec[3];
1500         if(ob->ipo) {
1501                 vec[0]= ob->rot[0]+ob->drot[0];
1502                 vec[1]= ob->rot[1]+ob->drot[1];
1503                 vec[2]= ob->rot[2]+ob->drot[2];
1504                 EulToMat3(vec, mat);
1505         }
1506         else {
1507                 EulToMat3(ob->rot, mat);
1508         }
1509 }
1510
1511 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
1512 {
1513         float smat[3][3];
1514         float rmat[3][3];
1515         /*float q1[4];*/
1516         
1517         /* size */
1518         object_scale_to_mat3(ob, smat);
1519
1520         /* rot */
1521         /* Quats arnt used yet */
1522         /*if(ob->transflag & OB_QUAT) {
1523                 if(ob->ipo) {
1524                         QuatMul(q1, ob->quat, ob->dquat);
1525                         QuatToMat3(q1, rmat);
1526                 }
1527                 else {
1528                         QuatToMat3(ob->quat, rmat);
1529                 }
1530         }
1531         else {*/
1532                 object_rot_to_mat3(ob, rmat);
1533         /*}*/
1534         Mat3MulMat3(mat, rmat, smat);
1535 }
1536
1537 void object_to_mat4(Object *ob, float mat[][4])
1538 {
1539         float tmat[3][3];
1540         
1541         object_to_mat3(ob, tmat);
1542         
1543         Mat4CpyMat3(mat, tmat);
1544         
1545         VECCOPY(mat[3], ob->loc);
1546         if(ob->ipo) {
1547                 mat[3][0]+= ob->dloc[0];
1548                 mat[3][1]+= ob->dloc[1];
1549                 mat[3][2]+= ob->dloc[2];
1550         }
1551 }
1552
1553 int enable_cu_speed= 1;
1554
1555 static void ob_parcurve(Object *ob, Object *par, float mat[][4])
1556 {
1557         Curve *cu;
1558         float q[4], vec[4], dir[3], quat[4], x1, ctime;
1559         float timeoffs = 0.0, sf_orig = 0.0;
1560         
1561         Mat4One(mat);
1562         
1563         cu= par->data;
1564         if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1565                 makeDispListCurveTypes(par, 0);
1566         if(cu->path==NULL) return;
1567         
1568         /* exception, timeoffset is regarded as distance offset */
1569         if(cu->flag & CU_OFFS_PATHDIST) {
1570                 timeoffs = give_timeoffset(ob);
1571                 SWAP(float, sf_orig, ob->sf);
1572         }
1573         
1574         /* catch exceptions: feature for nla stride editing */
1575         if(ob->ipoflag & OB_DISABLE_PATH) {
1576                 ctime= 0.0f;
1577         }
1578         /* catch exceptions: curve paths used as a duplicator */
1579         else if(enable_cu_speed) {
1580                 ctime= bsystem_time(ob, (float)G.scene->r.cfra, 0.0);
1581                 
1582                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
1583                         ctime /= cu->pathlen;
1584                         CLAMP(ctime, 0.0, 1.0);
1585                 }
1586         }
1587         else {
1588                 ctime= G.scene->r.cfra - give_timeoffset(ob);
1589                 ctime /= cu->pathlen;
1590                 
1591                 CLAMP(ctime, 0.0, 1.0);
1592         }
1593         
1594         /* time calculus is correct, now apply distance offset */
1595         if(cu->flag & CU_OFFS_PATHDIST) {
1596                 ctime += timeoffs/cu->path->totdist;
1597
1598                 /* restore */
1599                 SWAP(float, sf_orig, ob->sf);
1600         }
1601         
1602         
1603         /* vec: 4 items! */
1604         if( where_on_path(par, ctime, vec, dir) ) {
1605
1606                 if(cu->flag & CU_FOLLOW) {
1607                         vectoquat(dir, ob->trackflag, ob->upflag, quat);
1608                         
1609                         /* the tilt */
1610                         Normalize(dir);
1611                         q[0]= (float)cos(0.5*vec[3]);
1612                         x1= (float)sin(0.5*vec[3]);
1613                         q[1]= -x1*dir[0];
1614                         q[2]= -x1*dir[1];
1615                         q[3]= -x1*dir[2];
1616                         QuatMul(quat, q, quat);
1617                         
1618                         QuatToMat4(quat, mat);
1619                 }
1620                 
1621                 VECCOPY(mat[3], vec);
1622                 
1623         }
1624 }
1625
1626 static void ob_parbone(Object *ob, Object *par, float mat[][4])
1627 {       
1628         bPoseChannel *pchan;
1629         bArmature *arm;
1630         float vec[3];
1631         
1632         arm=get_armature(par);
1633         if (!arm) {
1634                 Mat4One(mat);
1635                 return;
1636         }
1637         
1638         /* Make sure the bone is still valid */
1639         pchan= get_pose_channel(par->pose, ob->parsubstr);
1640         if (!pchan){
1641                 printf ("Object %s with Bone parent: bone %s doesn't exist\n", ob->id.name+2, ob->parsubstr);
1642                 Mat4One(mat);
1643                 return;
1644         }
1645
1646         /* get bone transform */
1647         Mat4CpyMat4(mat, pchan->pose_mat);
1648
1649         /* but for backwards compatibility, the child has to move to the tail */
1650         VECCOPY(vec, mat[1]);
1651         VecMulf(vec, pchan->bone->length);
1652         VecAddf(mat[3], mat[3], vec);
1653 }
1654
1655 static void give_parvert(Object *par, int nr, float *vec)
1656 {
1657         int a, count;
1658         
1659         vec[0]=vec[1]=vec[2]= 0.0f;
1660         
1661         if(par->type==OB_MESH) {
1662                 if(G.obedit && (par->data==G.obedit->data)) {
1663                         EditMesh *em = G.editMesh;
1664                         EditVert *eve;
1665                         
1666                         for(eve= em->verts.first; eve; eve= eve->next) {
1667                                 if(eve->keyindex==nr) {
1668                                         memcpy(vec, eve->co, sizeof(float)*3);
1669                                         break;
1670                                 }
1671                         }
1672                 }
1673                 else {
1674                         DerivedMesh *dm = par->derivedFinal;
1675                         
1676                         if(dm) {
1677                                 int i, count = 0, numVerts = dm->getNumVerts(dm);
1678                                 int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
1679                                 float co[3];
1680
1681                                 /* get the average of all verts with (original index == nr) */
1682                                 for(i = 0; i < numVerts; ++i, ++index) {
1683                                         if(*index == nr) {
1684                                                 dm->getVertCo(dm, i, co);
1685                                                 VecAddf(vec, vec, co);
1686                                                 count++;
1687                                         }
1688                                 }
1689
1690                                 if (count==0) {
1691                                         /* keep as 0,0,0 */
1692                                 } else if(count > 0) {
1693                                         VecMulf(vec, 1.0f / count);
1694                                 } else {
1695                                         /* use first index if its out of range */
1696                                         dm->getVertCo(dm, 0, vec);
1697                                 }
1698                         }
1699                 }
1700         }
1701         else if ELEM(par->type, OB_CURVE, OB_SURF) {
1702                 Nurb *nu;
1703                 Curve *cu;
1704                 BPoint *bp;
1705                 BezTriple *bezt;
1706                 int found= 0;
1707                 
1708                 cu= par->data;
1709                 nu= cu->nurb.first;
1710                 if(par==G.obedit) nu= editNurb.first;
1711                 
1712                 count= 0;
1713                 while(nu && !found) {
1714                         if((nu->type & 7)==CU_BEZIER) {
1715                                 bezt= nu->bezt;
1716                                 a= nu->pntsu;
1717                                 while(a--) {
1718                                         if(count==nr) {
1719                                                 found= 1;
1720                                                 VECCOPY(vec, bezt->vec[1]);
1721                                                 break;
1722                                         }
1723                                         count++;
1724                                         bezt++;
1725                                 }
1726                         }
1727                         else {
1728                                 bp= nu->bp;
1729                                 a= nu->pntsu*nu->pntsv;
1730                                 while(a--) {
1731                                         if(count==nr) {
1732                                                 found= 1;
1733                                                 memcpy(vec, bp->vec, sizeof(float)*3);
1734                                                 break;
1735                                         }
1736                                         count++;
1737                                         bp++;
1738                                 }
1739                         }
1740                         nu= nu->next;
1741                 }
1742
1743         }
1744         else if(par->type==OB_LATTICE) {
1745                 Lattice *latt= par->data;
1746                 BPoint *bp;
1747                 DispList *dl = find_displist(&par->disp, DL_VERTS);
1748                 float *co = dl?dl->verts:NULL;
1749                 
1750                 if(par==G.obedit) latt= editLatt;
1751                 
1752                 a= latt->pntsu*latt->pntsv*latt->pntsw;
1753                 count= 0;
1754                 bp= latt->def;
1755                 while(a--) {
1756                         if(count==nr) {
1757                                 if(co)
1758                                         memcpy(vec, co, 3*sizeof(float));
1759                                 else
1760                                         memcpy(vec, bp->vec, 3*sizeof(float));
1761                                 break;
1762                         }
1763                         count++;
1764                         if(co) co+= 3;
1765                         else bp++;
1766                 }
1767         }
1768 }
1769
1770 static void ob_parvert3(Object *ob, Object *par, float mat[][4])
1771 {
1772         float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1773
1774         /* in local ob space */
1775         Mat4One(mat);
1776         
1777         if ELEM4(par->type, OB_MESH, OB_SURF, OB_CURVE, OB_LATTICE) {
1778                 
1779                 give_parvert(par, ob->par1, v1);
1780                 give_parvert(par, ob->par2, v2);
1781                 give_parvert(par, ob->par3, v3);
1782                                 
1783                 triatoquat(v1, v2, v3, q);
1784                 QuatToMat3(q, cmat);
1785                 Mat4CpyMat3(mat, cmat);
1786                 
1787                 if(ob->type==OB_CURVE) {
1788                         VECCOPY(mat[3], v1);
1789                 }
1790                 else {
1791                         VecAddf(mat[3], v1, v2);
1792                         VecAddf(mat[3], mat[3], v3);
1793                         VecMulf(mat[3], 0.3333333f);
1794                 }
1795         }
1796 }
1797
1798 static int no_parent_ipo=0;
1799 void set_no_parent_ipo(int val)
1800 {
1801         no_parent_ipo= val;
1802 }
1803
1804 static int during_script_flag=0;
1805 void disable_where_script(short on)
1806 {
1807         during_script_flag= on;
1808 }
1809
1810 int during_script(void) {
1811         return during_script_flag;
1812 }
1813
1814 static int during_scriptlink_flag=0;
1815 void disable_where_scriptlink(short on)
1816 {
1817         during_scriptlink_flag= on;
1818 }
1819
1820 int during_scriptlink(void) {
1821         return during_scriptlink_flag;
1822 }
1823
1824 void where_is_object_time(Object *ob, float ctime)
1825 {
1826         float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
1827         float stime, fac1, fac2, vec[3];
1828         int a;
1829         int pop; 
1830         
1831         /* new version: correct parent+vertexparent and track+parent */
1832         /* this one only calculates direct attached parent and track */
1833         /* is faster, but should keep track of timeoffs */
1834         
1835         if(ob==NULL) return;
1836         
1837         /* this is needed to be able to grab objects with ipos, otherwise it always freezes them */
1838         stime= bsystem_time(ob, ctime, 0.0);
1839         if(stime != ob->ctime) {
1840                 
1841                 ob->ctime= stime;
1842                 
1843                 if(ob->ipo) {
1844                         calc_ipo(ob->ipo, stime);
1845                         execute_ipo((ID *)ob, ob->ipo);
1846                 }
1847                 else 
1848                         do_all_object_actions(ob);
1849                 
1850                 /* do constraint ipos ..., note it needs stime (0 = all ipos) */
1851                 do_constraint_channels(&ob->constraints, &ob->constraintChannels, stime, 0);
1852         }
1853         else {
1854                 /* but, the drivers have to be done */
1855                 if(ob->ipo) do_ob_ipodrivers(ob, ob->ipo, stime);
1856                 /* do constraint ipos ..., note it needs stime (1 = only drivers ipos) */
1857                 do_constraint_channels(&ob->constraints, &ob->constraintChannels, stime, 1);
1858         }
1859         
1860         if(ob->parent) {
1861                 Object *par= ob->parent;
1862                 
1863                 if(ob->ipoflag & OB_OFFS_PARENT) ctime-= give_timeoffset(ob);
1864                 
1865                 /* hurms, code below conflicts with depgraph... (ton) */
1866                 /* and even worse, it gives bad effects for NLA stride too (try ctime != par->ctime, with MBlur) */
1867                 pop= 0;
1868                 if(no_parent_ipo==0 && stime != par->ctime) {
1869                 
1870                         // only for ipo systems? 
1871                         pushdata(par, sizeof(Object));
1872                         pop= 1;
1873                         
1874                         if(par->proxy_from);    // was a copied matrix, no where_is! bad...
1875                         else where_is_object_time(par, ctime);
1876                 }
1877                 
1878                 solve_parenting(ob, par, ob->obmat, slowmat, 0);
1879
1880                 if(pop) {
1881                         poplast(par);
1882                 }
1883                 
1884                 if(ob->partype & PARSLOW) {
1885                         // include framerate
1886
1887                         fac1= (1.0f/(1.0f+ fabs(give_timeoffset(ob))));
1888                         if(fac1>=1.0) return;
1889                         fac2= 1.0f-fac1;
1890                         
1891                         fp1= ob->obmat[0];
1892                         fp2= slowmat[0];
1893                         for(a=0; a<16; a++, fp1++, fp2++) {
1894                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1895                         }
1896                 }
1897         
1898         }
1899         else {
1900                 object_to_mat4(ob, ob->obmat);
1901         }
1902
1903         /* Handle tracking */
1904         if(ob->track) {
1905                 if( ctime != ob->track->ctime) where_is_object_time(ob->track, ctime);
1906                 solve_tracking (ob, ob->track->obmat);
1907                 
1908         }
1909
1910         /* solve constraints */
1911         if (ob->constraints.first) {
1912                 bConstraintOb *cob;
1913                 
1914                 cob= constraints_make_evalob(ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
1915                 
1916                 /* constraints need ctime, not stime. Some call where_is_object_time and bsystem_time */
1917                 solve_constraints (&ob->constraints, cob, ctime);
1918                 
1919                 constraints_clear_evalob(cob);
1920         }
1921         
1922         if(ob->scriptlink.totscript && !during_script()) {
1923                 if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_REDRAW);
1924         }
1925         
1926         /* set negative scale flag in object */
1927         Crossf(vec, ob->obmat[0], ob->obmat[1]);
1928         if( Inpf(vec, ob->obmat[2]) < 0.0 ) ob->transflag |= OB_NEG_SCALE;
1929         else ob->transflag &= ~OB_NEG_SCALE;
1930 }
1931
1932 static void solve_parenting (Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul)
1933 {
1934         float totmat[4][4];
1935         float tmat[4][4];
1936         float locmat[4][4];
1937         float vec[3];
1938         int ok;
1939
1940         object_to_mat4(ob, locmat);
1941         
1942         if(ob->partype & PARSLOW) Mat4CpyMat4(slowmat, obmat);
1943         
1944
1945         switch(ob->partype & PARTYPE) {
1946         case PAROBJECT:
1947                 ok= 0;
1948                 if(par->type==OB_CURVE) {
1949                         if( ((Curve *)par->data)->flag & CU_PATH ) {
1950                                 ob_parcurve(ob, par, tmat);
1951                                 ok= 1;
1952                         }
1953                 }
1954                 
1955                 if(ok) Mat4MulSerie(totmat, par->obmat, tmat, 
1956                         NULL, NULL, NULL, NULL, NULL, NULL);
1957                 else Mat4CpyMat4(totmat, par->obmat);
1958                 
1959                 break;
1960         case PARBONE:
1961                 ob_parbone(ob, par, tmat);
1962                 Mat4MulSerie(totmat, par->obmat, tmat,         
1963                         NULL, NULL, NULL, NULL, NULL, NULL);
1964                 break;
1965                 
1966         case PARVERT1:
1967                 Mat4One(totmat);
1968                 if (simul){
1969                         VECCOPY(totmat[3], par->obmat[3]);
1970                 }
1971                 else{
1972                         give_parvert(par, ob->par1, vec);
1973                         VecMat4MulVecfl(totmat[3], par->obmat, vec);
1974                 }
1975                 break;
1976         case PARVERT3:
1977                 ob_parvert3(ob, par, tmat);
1978                 
1979                 Mat4MulSerie(totmat, par->obmat, tmat,         
1980                         NULL, NULL, NULL, NULL, NULL, NULL);
1981                 break;
1982                 
1983         case PARSKEL:
1984                 Mat4CpyMat4(totmat, par->obmat);
1985                 break;
1986         }
1987         
1988         // total 
1989         Mat4MulSerie(tmat, totmat, ob->parentinv,         
1990                 NULL, NULL, NULL, NULL, NULL, NULL);
1991         Mat4MulSerie(obmat, tmat, locmat,         
1992                 NULL, NULL, NULL, NULL, NULL, NULL);
1993         
1994         if (simul) {
1995
1996         }
1997         else{
1998                 // external usable originmat 
1999                 Mat3CpyMat4(originmat, tmat);
2000                 
2001                 // origin, voor help line
2002                 if( (ob->partype & 15)==PARSKEL ) {
2003                         VECCOPY(ob->orig, par->obmat[3]);
2004                 }
2005                 else {
2006                         VECCOPY(ob->orig, totmat[3]);
2007                 }
2008         }
2009
2010 }
2011 void solve_tracking (Object *ob, float targetmat[][4])
2012 {
2013         float quat[4];
2014         float vec[3];
2015         float totmat[3][3];
2016         float tmat[4][4];
2017         
2018         VecSubf(vec, ob->obmat[3], targetmat[3]);
2019         vectoquat(vec, ob->trackflag, ob->upflag, quat);
2020         QuatToMat3(quat, totmat);
2021         
2022         if(ob->parent && (ob->transflag & OB_POWERTRACK)) {
2023                 /* 'temporal' : clear parent info */
2024                 object_to_mat4(ob, tmat);
2025                 tmat[0][3]= ob->obmat[0][3];
2026                 tmat[1][3]= ob->obmat[1][3];
2027                 tmat[2][3]= ob->obmat[2][3];
2028                 tmat[3][0]= ob->obmat[3][0];
2029                 tmat[3][1]= ob->obmat[3][1];
2030                 tmat[3][2]= ob->obmat[3][2];
2031                 tmat[3][3]= ob->obmat[3][3];
2032         }
2033         else Mat4CpyMat4(tmat, ob->obmat);
2034         
2035         Mat4MulMat34(ob->obmat, totmat, tmat);
2036
2037 }
2038
2039 void where_is_object(Object *ob)
2040 {
2041         where_is_object_time(ob, (float)G.scene->r.cfra);
2042 }
2043
2044
2045 void where_is_object_simul(Object *ob)
2046 /* was written for the old game engine (until 2.04) */
2047 /* It seems that this function is only called
2048 for a lamp that is the child of another object */
2049 {
2050         Object *par;
2051         Ipo *ipo;
2052         float *fp1, *fp2;
2053         float slowmat[4][4];
2054         float fac1, fac2;
2055         int a;
2056         
2057         /* NO TIMEOFFS */
2058         
2059         /* no ipo! (because of dloc and realtime-ipos) */
2060         ipo= ob->ipo;
2061         ob->ipo= NULL;
2062
2063         if(ob->parent) {
2064                 par= ob->parent;
2065                 
2066                 solve_parenting(ob, par, ob->obmat, slowmat, 1);
2067
2068                 if(ob->partype & PARSLOW) {
2069
2070                         fac1= (float)(1.0/(1.0+ fabs(give_timeoffset(ob))));
2071                         fac2= 1.0f-fac1;
2072                         fp1= ob->obmat[0];
2073                         fp2= slowmat[0];
2074                         for(a=0; a<16; a++, fp1++, fp2++) {
2075                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
2076                         }
2077                 }
2078                 
2079         }
2080         else {
2081                 object_to_mat4(ob, ob->obmat);
2082         }
2083         
2084         if(ob->track) 
2085                 solve_tracking(ob, ob->track->obmat);
2086
2087         /* solve constraints */
2088         if (ob->constraints.first) {
2089                 bConstraintOb *cob;
2090                 
2091                 cob= constraints_make_evalob(ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2092                 solve_constraints (&ob->constraints, cob, G.scene->r.cfra);
2093                 constraints_clear_evalob(cob);
2094         }
2095         
2096         /*  WATCH IT!!! */
2097         ob->ipo= ipo;
2098 }
2099
2100 /* for calculation of the inverse parent transform, only used for editor */
2101 void what_does_parent(Object *ob)
2102 {
2103         clear_workob();
2104         Mat4One(workob.obmat);
2105         Mat4One(workob.parentinv);
2106         Mat4One(workob.constinv);
2107         workob.parent= ob->parent;
2108         workob.track= ob->track;
2109
2110         workob.trackflag= ob->trackflag;
2111         workob.upflag= ob->upflag;
2112         
2113         workob.partype= ob->partype;
2114         workob.par1= ob->par1;
2115         workob.par2= ob->par2;
2116         workob.par3= ob->par3;
2117
2118         workob.constraints.first = ob->constraints.first;
2119         workob.constraints.last = ob->constraints.last;
2120
2121         strcpy(workob.parsubstr, ob->parsubstr); 
2122
2123         where_is_object(&workob);
2124 }
2125
2126 BoundBox *unit_boundbox()
2127 {
2128         BoundBox *bb;
2129         float min[3] = {-1,-1,-1}, max[3] = {-1,-1,-1};
2130
2131         bb= MEM_callocN(sizeof(BoundBox), "bb");
2132         boundbox_set_from_min_max(bb, min, max);
2133         
2134         return bb;
2135 }
2136
2137 void boundbox_set_from_min_max(BoundBox *bb, float min[3], float max[3])
2138 {
2139         bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
2140         bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
2141         
2142         bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
2143         bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
2144
2145         bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
2146         bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
2147 }
2148
2149 BoundBox *object_get_boundbox(Object *ob)
2150 {
2151         BoundBox *bb= NULL;
2152         
2153         if(ob->type==OB_MESH) {
2154                 bb = mesh_get_bb(ob);
2155         }
2156         else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) {
2157                 bb= ( (Curve *)ob->data )->bb;
2158         }
2159         else if(ob->type==OB_MBALL) {
2160                 bb= ob->bb;
2161         }
2162         return bb;
2163 }
2164
2165 /* used to temporally disable/enable boundbox */
2166 void object_boundbox_flag(Object *ob, int flag, int set)
2167 {
2168         BoundBox *bb= object_get_boundbox(ob);
2169         if(bb) {
2170                 if(set) bb->flag |= flag;
2171                 else bb->flag &= ~flag;
2172         }
2173 }
2174
2175 void minmax_object(Object *ob, float *min, float *max)
2176 {
2177         BoundBox bb;
2178         Mesh *me;
2179         Curve *cu;
2180         float vec[3];
2181         int a;
2182         
2183         switch(ob->type) {
2184                 
2185         case OB_CURVE:
2186         case OB_FONT:
2187         case OB_SURF:
2188                 cu= ob->data;
2189                 
2190                 if(cu->bb==NULL) tex_space_curve(cu);
2191                 bb= *(cu->bb);
2192                 
2193                 for(a=0; a<8; a++) {
2194                         Mat4MulVecfl(ob->obmat, bb.vec[a]);
2195                         DO_MINMAX(bb.vec[a], min, max);
2196                 }
2197                 break;
2198         case OB_ARMATURE:
2199                 if(ob->pose) {
2200                         bPoseChannel *pchan;
2201                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2202                                 VECCOPY(vec, pchan->pose_head);
2203                                 Mat4MulVecfl(ob->obmat, vec);
2204                                 DO_MINMAX(vec, min, max);
2205                                 VECCOPY(vec, pchan->pose_tail);
2206                                 Mat4MulVecfl(ob->obmat, vec);
2207                                 DO_MINMAX(vec, min, max);
2208                         }
2209                         break;
2210                 }
2211                 /* no break, get_mesh will give NULL and it passes on to default */
2212         case OB_MESH:
2213                 me= get_mesh(ob);
2214                 
2215                 if(me) {
2216                         bb = *mesh_get_bb(ob);
2217                         
2218                         for(a=0; a<8; a++) {
2219                                 Mat4MulVecfl(ob->obmat, bb.vec[a]);
2220                                 DO_MINMAX(bb.vec[a], min, max);
2221                         }
2222                 }
2223                 if(min[0] < max[0] ) break;
2224                 
2225                 /* else here no break!!!, mesh can be zero sized */
2226                 
2227         default:
2228                 DO_MINMAX(ob->obmat[3], min, max);
2229
2230                 VECCOPY(vec, ob->obmat[3]);
2231                 VecAddf(vec, vec, ob->size);
2232                 DO_MINMAX(vec, min, max);
2233
2234                 VECCOPY(vec, ob->obmat[3]);
2235                 VecSubf(vec, vec, ob->size);
2236                 DO_MINMAX(vec, min, max);
2237                 break;
2238         }
2239 }
2240
2241 /* TODO - use dupli objects bounding boxes */
2242 void minmax_object_duplis(Object *ob, float *min, float *max)
2243 {
2244         if ((ob->transflag & OB_DUPLI)==0) {
2245                 return;
2246         } else {
2247                 ListBase *lb;
2248                 DupliObject *dob;
2249                 
2250                 lb= object_duplilist(G.scene, ob);
2251                 for(dob= lb->first; dob; dob= dob->next) {
2252                         if(dob->no_draw);
2253                         else {
2254                                 /* should really use bound box of dup object */
2255                                 DO_MINMAX(dob->mat[3], min, max);
2256                         }
2257                 }
2258                 free_object_duplilist(lb);      /* does restore */
2259         }
2260 }
2261
2262
2263 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
2264 /*           local_object->proxy      == pointer to library object, saved in files and read */
2265
2266 /* function below is polluted with proxy exceptions, cleanup will follow! */
2267
2268 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
2269 /* requires flags to be set! */
2270 void object_handle_update(Object *ob)
2271 {
2272         if(ob->recalc & OB_RECALC) {
2273                 
2274                 if(ob->recalc & OB_RECALC_OB) {
2275
2276                         // printf("recalcob %s\n", ob->id.name+2);
2277                         
2278                         /* handle proxy copy for target */
2279                         if(ob->id.lib && ob->proxy_from) {
2280                                 // printf("ob proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2281                                 if(ob->proxy_from->proxy_group) {/* transform proxy into group space */
2282                                         Object *obg= ob->proxy_from->proxy_group;
2283                                         Mat4Invert(obg->imat, obg->obmat);
2284                                         Mat4MulMat4(ob->obmat, ob->proxy_from->obmat, obg->imat);
2285                                 }
2286                                 else
2287                                         Mat4CpyMat4(ob->obmat, ob->proxy_from->obmat);
2288                         }
2289                         else
2290                                 where_is_object(ob);
2291                         if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_OBJECTUPDATE);
2292                 }
2293                 
2294                 if(ob->recalc & OB_RECALC_DATA) {
2295                         
2296                         // printf("recalcdata %s\n", ob->id.name+2);
2297                         
2298                         /* includes all keys and modifiers */
2299                         if(ob->type==OB_MESH) {
2300                                 makeDerivedMesh(ob, get_viewedit_datamask());
2301                         }
2302                         else if(ob->type==OB_MBALL) {
2303                                 makeDispListMBall(ob);
2304                         } 
2305                         else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2306                                 makeDispListCurveTypes(ob, 0);
2307                         }
2308                         else if(ob->type==OB_LATTICE) {
2309                                 lattice_calc_modifiers(ob);
2310                         }
2311                         else if(ob->type==OB_CAMERA) {
2312                                 Camera *cam = (Camera *)ob->data;
2313                                 calc_ipo(cam->ipo, frame_to_float(G.scene->r.cfra));
2314                                 execute_ipo(&cam->id, cam->ipo);
2315                         }
2316                         else if(ob->type==OB_LAMP) {
2317                                 Lamp *la = (Lamp *)ob->data;
2318                                 calc_ipo(la->ipo, frame_to_float(G.scene->r.cfra));
2319                                 execute_ipo(&la->id, la->ipo);
2320                         }
2321                         else if(ob->type==OB_ARMATURE) {
2322                                 /* this happens for reading old files and to match library armatures with poses */
2323                                 if(ob->pose==NULL || (ob->pose->flag & POSE_RECALC))
2324                                         armature_rebuild_pose(ob, ob->data);
2325                                 
2326                                 if(ob->id.lib && ob->proxy_from) {
2327                                         copy_pose_result(ob->pose, ob->proxy_from->pose);
2328                                         // printf("pose proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2329                                 }
2330                                 else {
2331                                         do_all_pose_actions(ob);
2332                                         where_is_pose(ob);
2333                                 }
2334                         }
2335
2336                         if(ob->particlesystem.first) {
2337                                 ParticleSystem *tpsys, *psys;
2338                                 DerivedMesh *dm;
2339                                 
2340                                 psys= ob->particlesystem.first;
2341                                 while(psys) {
2342                                         if(psys_check_enabled(ob, psys)) {
2343                                                 particle_system_update(ob, psys);
2344                                                 psys= psys->next;
2345                                         }
2346                                         else if(psys->flag & PSYS_DELETE) {
2347                                                 tpsys=psys->next;
2348                                                 BLI_remlink(&ob->particlesystem, psys);
2349                                                 psys_free(ob,psys);
2350                                                 psys= tpsys;
2351                                         }
2352                                         else
2353                                                 psys= psys->next;
2354                                 }
2355
2356                                 if(G.rendering && ob->transflag & OB_DUPLIPARTS) {
2357                                         /* this is to make sure we get render level duplis in groups:
2358                                          * the derivedmesh must be created before init_render_mesh,
2359                                          * since object_duplilist does dupliparticles before that */
2360                                         dm = mesh_create_derived_render(ob, CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL);
2361                                         dm->release(dm);
2362
2363                                         for(psys=ob->particlesystem.first; psys; psys=psys->next)
2364                                                 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
2365                                 }
2366                         }
2367                         if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_OBDATAUPDATE);
2368                 }
2369
2370                 /* the no-group proxy case, we call update */
2371                 if(ob->proxy && ob->proxy_group==NULL) {
2372                         /* set pointer in library proxy target, for copying, but restore it */
2373                         ob->proxy->proxy_from= ob;
2374                         // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
2375                         object_handle_update(ob->proxy);
2376                 }
2377         
2378                 ob->recalc &= ~OB_RECALC;
2379         }
2380
2381         /* the case when this is a group proxy, object_update is called in group.c */
2382         if(ob->proxy) {
2383                 ob->proxy->proxy_from= ob;
2384                 // printf("set proxy pointer for later group stuff %s\n", ob->id.name);
2385         }
2386 }
2387
2388 float give_timeoffset(Object *ob) {
2389         if ((ob->ipoflag & OB_OFFS_PARENTADD) && ob->parent) {
2390                 return ob->sf + give_timeoffset(ob->parent);
2391         } else {
2392                 return ob->sf;
2393         }
2394 }
2395
2396 int give_obdata_texspace(Object *ob, int **texflag, float **loc, float **size, float **rot) {
2397         
2398         if (ob->data==NULL)
2399                 return 0;
2400         
2401         switch (GS(((ID *)ob->data)->name)) {
2402         case ID_ME:
2403         {
2404                 Mesh *me= ob->data;
2405                 if (texflag)    *texflag = &me->texflag;
2406                 if (loc)                *loc = me->loc;
2407                 if (size)               *size = me->size;
2408                 if (rot)                *rot = me->rot;
2409                 break;
2410         }
2411         case ID_CU:
2412         {
2413                 Curve *cu= ob->data;
2414                 if (texflag)    *texflag = &cu->texflag;
2415                 if (loc)                *loc = cu->loc;
2416                 if (size)               *size = cu->size;
2417                 if (rot)                *rot = cu->rot;
2418                 break;
2419         }
2420         case ID_MB:
2421         {
2422                 MetaBall *mb= ob->data;
2423                 if (texflag)    *texflag = &mb->texflag;
2424                 if (loc)                *loc = mb->loc;
2425                 if (size)               *size = mb->size;
2426                 if (rot)                *rot = mb->rot;
2427                 break;
2428         }
2429         default:
2430                 return 0;
2431         }
2432         return 1;
2433 }
2434
2435 /*
2436  * Test a bounding box for ray intersection
2437  * assumes the ray is already local to the boundbox space
2438  */
2439 int ray_hit_boundbox(struct BoundBox *bb, float ray_start[3], float ray_normal[3])
2440 {
2441         static int triangle_indexes[12][3] = {{0, 1, 2}, {0, 2, 3},
2442                                                                                   {3, 2, 6}, {3, 6, 7},
2443                                                                                   {1, 2, 6}, {1, 6, 5}, 
2444                                                                                   {5, 6, 7}, {4, 5, 7},
2445                                                                                   {0, 3, 7}, {0, 4, 7},
2446                                                                                   {0, 1, 5}, {0, 4, 5}};
2447         int result = 0;
2448         int i;
2449         
2450         for (i = 0; i < 12 && result == 0; i++)
2451         {
2452                 float lambda;
2453                 int v1, v2, v3;
2454                 v1 = triangle_indexes[i][0];
2455                 v2 = triangle_indexes[i][1];
2456                 v3 = triangle_indexes[i][2];
2457                 result = RayIntersectsTriangle(ray_start, ray_normal, bb->vec[v1], bb->vec[v2], bb->vec[v3], &lambda, NULL);
2458         }
2459         
2460         return result;
2461 }