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