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