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