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