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