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