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