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