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