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