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