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