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