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