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