Bugfix #4051
[blender.git] / source / blender / blenkernel / intern / object.c
1 /* object.c
2  *
3  * 
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 #include <string.h>
36 #include <math.h>
37 #include <stdio.h>                      
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #include "MEM_guardedalloc.h"
44
45 #include "DNA_action_types.h"
46 #include "DNA_armature_types.h"
47 #include "DNA_camera_types.h"
48 #include "DNA_constraint_types.h"
49 #include "DNA_curve_types.h"
50 #include "DNA_group_types.h"
51 #include "DNA_ipo_types.h"
52 #include "DNA_lamp_types.h"
53 #include "DNA_lattice_types.h"
54 #include "DNA_material_types.h"
55 #include "DNA_mesh_types.h"
56 #include "DNA_meshdata_types.h"
57 #include "DNA_modifier_types.h"
58 #include "DNA_nla_types.h"
59 #include "DNA_object_types.h"
60 #include "DNA_object_force.h"
61 #include "DNA_object_fluidsim.h"
62 #include "DNA_oops_types.h"
63 #include "DNA_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_deform.h"
74 #include "BKE_DerivedMesh.h"
75 #include "BKE_nla.h"
76
77 #include "BLI_blenlib.h"
78 #include "BLI_arithb.h"
79 #include "BLI_editVert.h"
80
81 #include "BKE_utildefines.h"
82 #include "BKE_bad_level_calls.h"
83
84 #include "BKE_main.h"
85 #include "BKE_global.h"
86
87 #include "BKE_anim.h"
88 #include "BKE_blender.h"
89 #include "BKE_constraint.h"
90 #include "BKE_curve.h"
91 #include "BKE_displist.h"
92 #include "BKE_effect.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_property.h"
104 #include "BKE_sca.h"
105 #include "BKE_scene.h"
106 #include "BKE_screen.h"
107 #include "BKE_softbody.h"
108
109 #include "LBM_fluidsim.h"
110
111 #include "BPY_extern.h"
112
113 /* Local function protos */
114 static void solve_parenting (Object *ob, Object *par, float slowmat[][4], int simul);
115
116 float originmat[3][3];  /* after where_is_object(), can be used in other functions (bad!) */
117 Object workob;
118
119 void clear_workob(void)
120 {
121         memset(&workob, 0, sizeof(Object));
122         
123         workob.size[0]= workob.size[1]= workob.size[2]= 1.0;
124         
125 }
126
127 void copy_baseflags()
128 {
129         Base *base= G.scene->base.first;
130         
131         while(base) {
132                 base->object->flag= base->flag;
133                 base= base->next;
134         }
135 }
136
137 void copy_objectflags()
138 {
139         Base *base= G.scene->base.first;
140         
141         while(base) {
142                 base->flag= base->object->flag;
143                 base= base->next;
144         }
145 }
146
147 void update_base_layer(Object *ob)
148 {
149         Base *base= G.scene->base.first;
150
151         while (base) {
152                 if (base->object == ob) base->lay= ob->lay;
153                 base= base->next;
154         }
155 }
156
157 void object_free_modifiers(Object *ob)
158 {
159         while (ob->modifiers.first) {
160                 ModifierData *md = ob->modifiers.first;
161
162                 BLI_remlink(&ob->modifiers, md);
163
164                 modifier_free(md);
165         }
166 }
167
168 /* here we will collect all local displist stuff */
169 /* also (ab)used in depsgraph */
170 void object_free_display(Object *ob)
171 {
172         if(ob->derivedDeform) {
173                 ob->derivedDeform->release(ob->derivedDeform);
174                 ob->derivedDeform= NULL;
175         }
176         if(ob->derivedFinal) {
177                 ob->derivedFinal->release(ob->derivedFinal);
178                 ob->derivedFinal= NULL;
179         }
180         
181         freedisplist(&ob->disp);
182 }
183
184 /* do not free object itself */
185 void free_object(Object *ob)
186 {
187         int a;
188         
189         object_free_display(ob);
190         
191         /* disconnect specific data */
192         if(ob->data) {
193                 ID *id= ob->data;
194                 id->us--;
195                 if(id->us==0) {
196                         if(ob->type==OB_MESH) unlink_mesh(ob->data);
197                         else if(ob->type==OB_CURVE) unlink_curve(ob->data);
198                         else if(ob->type==OB_MBALL) unlink_mball(ob->data);
199                 }
200                 ob->data= 0;
201         }
202         
203         for(a=0; a<ob->totcol; a++) {
204                 if(ob->mat[a]) ob->mat[a]->id.us--;
205         }
206         if(ob->mat) MEM_freeN(ob->mat);
207         ob->mat= 0;
208         if(ob->bb) MEM_freeN(ob->bb); 
209         ob->bb= 0;
210         if(ob->path) free_path(ob->path); 
211         ob->path= 0;
212         if(ob->ipo) ob->ipo->id.us--;
213         if(ob->action) ob->action->id.us--;
214         if(ob->dup_group) ob->dup_group->id.us--;
215         if(ob->defbase.first)
216                 BLI_freelistN(&ob->defbase);
217         if(ob->pose) {
218                 free_pose_channels(ob->pose);
219                 MEM_freeN(ob->pose);
220         }
221         free_effects(&ob->effect);
222         BLI_freelistN(&ob->network);
223         free_properties(&ob->prop);
224         object_free_modifiers(ob);
225         
226         free_sensors(&ob->sensors);
227         free_controllers(&ob->controllers);
228         free_actuators(&ob->actuators);
229         
230         free_constraints(&ob->constraints);
231         free_constraint_channels(&ob->constraintChannels);
232         free_nlastrips(&ob->nlastrips);
233         
234         BPY_free_scriptlink(&ob->scriptlink);
235         
236         if(ob->pd) MEM_freeN(ob->pd);
237         if(ob->soft) sbFree(ob->soft);
238         if(ob->fluidsimSettings) fluidsimSettingsFree(ob->fluidsimSettings);
239 }
240
241 static void unlink_object__unlinkModifierLinks(void *userData, Object *ob, Object **obpoin)
242 {
243         Object *unlinkOb = userData;
244
245         if (*obpoin==unlinkOb) {
246                 *obpoin = NULL;
247                 ob->recalc |= OB_RECALC;
248         }
249 }
250 void unlink_object(Object *ob)
251 {
252         Object *obt;
253         Material *mat;
254         World *wrld;
255         bScreen *sc;
256         Scene *sce;
257         Curve *cu;
258         Tex *tex;
259         Ipo *ipo;
260         Group *group;
261         bConstraint *con;
262         bActionStrip *strip;
263         int a;
264         char *str;
265         
266         unlink_controllers(&ob->controllers);
267         unlink_actuators(&ob->actuators);
268         
269         /* check all objects: parents en bevels and fields */
270         obt= G.main->object.first;
271         while(obt) {
272                 if(obt->id.lib==NULL) {
273                         
274                         if(obt->parent==ob) {
275                                 obt->parent= NULL;
276                                 obt->recalc |= OB_RECALC;
277                         }
278                         
279                         if(obt->track==ob) {
280                                 obt->track= NULL;
281                                 obt->recalc |= OB_RECALC_OB;
282                         }
283                         
284                         modifiers_foreachObjectLink(obt, unlink_object__unlinkModifierLinks, ob);
285                         
286                         if ELEM(obt->type, OB_CURVE, OB_FONT) {
287                                 cu= obt->data;
288                                 if(cu->bevobj==ob) {
289                                         cu->bevobj= NULL;
290                                         obt->recalc |= OB_RECALC;
291                                 }
292                                 if(cu->taperobj==ob) {
293                                         cu->taperobj= NULL;
294                                         obt->recalc |= OB_RECALC;
295                                 }
296                                 if(cu->textoncurve==ob) {
297                                         cu->textoncurve= NULL;
298                                         obt->recalc |= OB_RECALC;
299                                 }
300                         }
301                         else if(obt->type==OB_ARMATURE && obt->pose) {
302                                 bPoseChannel *pchan;
303                                 for(pchan= obt->pose->chanbase.first; pchan; pchan= pchan->next) {
304                                         for (con = pchan->constraints.first; con; con=con->next) {
305                                                 if(ob==get_constraint_target(con, &str)) {
306                                                         set_constraint_target(con, NULL, NULL);
307                                                         obt->recalc |= OB_RECALC_DATA;
308                                                 }
309                                         }
310                                         if(pchan->custom==ob)
311                                                 pchan->custom= NULL;
312                                 }
313                         }
314                         
315                         sca_remove_ob_poin(obt, ob);
316                         
317                         for (con = obt->constraints.first; con; con=con->next) {
318                                 if(ob==get_constraint_target(con, &str)) {
319                                         set_constraint_target(con, NULL, NULL);
320                                         obt->recalc |= OB_RECALC_OB;
321                                 }
322                         }
323                         
324                         /* object is deflector or field */
325                         if(ob->pd) {
326                                 if(give_parteff(obt))
327                                         obt->recalc |= OB_RECALC_DATA;
328                                 else if(obt->soft)
329                                         obt->recalc |= OB_RECALC_DATA;
330                         }
331                         
332                         /* strips */
333                         for(strip= ob->nlastrips.first; strip; strip= strip->next) {
334                                 if(strip->object==ob)
335                                         strip->object= NULL;
336                         }
337                 }
338                 obt= obt->id.next;
339         }
340         
341         /* materials */
342         mat= G.main->mat.first;
343         while(mat) {
344         
345                 for(a=0; a<MAX_MTEX; a++) {
346                         if(mat->mtex[a] && ob==mat->mtex[a]->object) {
347                                 /* actually, test for lib here... to do */
348                                 mat->mtex[a]->object= NULL;
349                         }
350                 }
351
352                 mat= mat->id.next;
353         }
354         
355         /* textures */
356         tex= G.main->tex.first;
357         while(tex) {
358                 if(tex->env) {
359                         if(tex->env->object == ob) tex->env->object= NULL;
360                 }
361                 tex= tex->id.next;
362         }
363         
364         /* mballs */
365         if(ob->type==OB_MBALL) {
366                 obt= find_basis_mball(ob);
367                 if(obt) freedisplist(&obt->disp);
368         }
369         
370         /* worlds */
371         wrld= G.main->world.first;
372         while(wrld) {
373                 if(wrld->id.lib==NULL) {
374                         for(a=0; a<MAX_MTEX; a++) {
375                                 if(wrld->mtex[a] && ob==wrld->mtex[a]->object)
376                                         wrld->mtex[a]->object= NULL;
377                         }
378                 }
379                 
380                 wrld= wrld->id.next;
381         }
382                 
383         /* scenes */
384         sce= G.main->scene.first;
385         while(sce) {
386                 if(sce->id.lib==NULL) {
387                         if(sce->camera==ob) sce->camera= NULL;
388                 }
389                 sce= sce->id.next;
390         }
391         /* ipos */
392         ipo= G.main->ipo.first;
393         while(ipo) {
394                 if(ipo->id.lib==NULL) {
395                         IpoCurve *icu;
396                         for(icu= ipo->curve.first; icu; icu= icu->next) {
397                                 if(icu->driver && icu->driver->ob==ob)
398                                         icu->driver->ob= NULL;
399                         }
400                 }
401                 ipo= ipo->id.next;
402         }
403         
404         /* screens */
405         sc= G.main->screen.first;
406         while(sc) {
407                 ScrArea *sa= sc->areabase.first;
408                 while(sa) {
409                         SpaceLink *sl;
410
411                         for (sl= sa->spacedata.first; sl; sl= sl->next) {
412                                 if(sl->spacetype==SPACE_VIEW3D) {
413                                         View3D *v3d= (View3D*) sl;
414
415                                         if(v3d->camera==ob) {
416                                                 v3d->camera= NULL;
417                                                 if(v3d->persp>1) v3d->persp= 1;
418                                         }
419                                         if(v3d->localvd && v3d->localvd->camera==ob ) {
420                                                 v3d->localvd->camera= NULL;
421                                                 if(v3d->localvd->persp>1) v3d->localvd->persp= 1;
422                                         }
423                                 }
424                                 else if(sl->spacetype==SPACE_IPO) {
425                                         SpaceIpo *sipo= (SpaceIpo *)sl;
426                                         if(sipo->from == (ID *)ob) sipo->from= NULL;
427                                 }
428                                 else if(sl->spacetype==SPACE_OOPS) {
429                                         SpaceOops *so= (SpaceOops *)sl;
430                                         Oops *oops;
431
432                                         oops= so->oops.first;
433                                         while(oops) {
434                                                 if(oops->id==(ID *)ob) oops->id= NULL;
435                                                 oops= oops->next;
436                                         }
437                                         if(so->treestore) {
438                                                 TreeStoreElem *tselem= so->treestore->data;
439                                                 int a;
440                                                 for(a=0; a<so->treestore->usedelem; a++, tselem++) {
441                                                         if(tselem->id==(ID *)ob) tselem->id= NULL;
442                                                 }
443                                         }
444                                         so->lockpoin= NULL;
445                                 }
446                         }
447
448                         sa= sa->next;
449                 }
450                 sc= sc->id.next;
451         }
452
453         /* groups */
454         group= G.main->group.first;
455         while(group) {
456                 rem_from_group(group, ob);
457                 group= group->id.next;
458         }
459 }
460
461 int exist_object(Object *obtest)
462 {
463         Object *ob;
464         
465         if(obtest==NULL) return 0;
466         
467         ob= G.main->object.first;
468         while(ob) {
469                 if(ob==obtest) return 1;
470                 ob= ob->id.next;
471         }
472         return 0;
473 }
474
475 void *add_camera()
476 {
477         Camera *cam;
478         
479         cam=  alloc_libblock(&G.main->camera, ID_CA, "Camera");
480
481         cam->lens= 35.0f;
482         cam->clipsta= 0.1f;
483         cam->clipend= 100.0f;
484         cam->drawsize= 0.5f;
485         cam->ortho_scale= 6.0;
486         cam->flag |= CAM_SHOWTITLESAFE;
487         cam->passepartalpha = 0.2f;
488         
489         return cam;
490 }
491
492 Camera *copy_camera(Camera *cam)
493 {
494         Camera *camn;
495         
496         camn= copy_libblock(cam);
497         id_us_plus((ID *)camn->ipo);
498
499         BPY_copy_scriptlink(&camn->scriptlink);
500         
501         return camn;
502 }
503
504
505
506 void make_local_camera(Camera *cam)
507 {
508         Object *ob;
509         Camera *camn;
510         int local=0, lib=0;
511
512         /* - only lib users: do nothing
513             * - only local users: set flag
514             * - mixed: make copy
515             */
516         
517         if(cam->id.lib==0) return;
518         if(cam->id.us==1) {
519                 cam->id.lib= 0;
520                 cam->id.flag= LIB_LOCAL;
521                 new_id(0, (ID *)cam, 0);
522                 return;
523         }
524         
525         ob= G.main->object.first;
526         while(ob) {
527                 if(ob->data==cam) {
528                         if(ob->id.lib) lib= 1;
529                         else local= 1;
530                 }
531                 ob= ob->id.next;
532         }
533         
534         if(local && lib==0) {
535                 cam->id.lib= 0;
536                 cam->id.flag= LIB_LOCAL;
537                 new_id(0, (ID *)cam, 0);
538         }
539         else if(local && lib) {
540                 camn= copy_camera(cam);
541                 camn->id.us= 0;
542                 
543                 ob= G.main->object.first;
544                 while(ob) {
545                         if(ob->data==cam) {
546                                 
547                                 if(ob->id.lib==0) {
548                                         ob->data= camn;
549                                         camn->id.us++;
550                                         cam->id.us--;
551                                 }
552                         }
553                         ob= ob->id.next;
554                 }
555         }
556 }
557
558
559
560 void *add_lamp(void)
561 {
562         Lamp *la;
563         
564         la=  alloc_libblock(&G.main->lamp, ID_LA, "Lamp");
565         
566         la->r= la->g= la->b= la->k= 1.0;
567         la->haint= la->energy= 1.0;
568         la->dist= 20.0;
569         la->spotsize= 45.0;
570         la->spotblend= 0.15;
571         la->att2= 1.0;
572         la->mode= LA_SHAD;
573         la->bufsize= 512;
574         la->clipsta= 0.5;
575         la->clipend= 40.0;
576         la->shadspotsize= 45.0;
577         la->samp= 3;
578         la->bias= 1.0;
579         la->soft= 3.0;
580         la->ray_samp= la->ray_sampy= la->ray_sampz= 1; 
581         la->area_size=la->area_sizey=la->area_sizez= 1.0; 
582         la->buffers= 1;
583         
584         return la;
585 }
586
587 Lamp *copy_lamp(Lamp *la)
588 {
589         Lamp *lan;
590         int a;
591         
592         lan= copy_libblock(la);
593
594         for(a=0; a<MAX_MTEX; a++) {
595                 if(lan->mtex[a]) {
596                         lan->mtex[a]= MEM_mallocN(sizeof(MTex), "copylamptex");
597                         memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
598                         id_us_plus((ID *)lan->mtex[a]->tex);
599                 }
600         }
601         
602         id_us_plus((ID *)lan->ipo);
603
604         BPY_copy_scriptlink(&la->scriptlink);
605         
606         return lan;
607 }
608
609 void make_local_lamp(Lamp *la)
610 {
611         Object *ob;
612         Lamp *lan;
613         int local=0, lib=0;
614
615         /* - only lib users: do nothing
616             * - only local users: set flag
617             * - mixed: make copy
618             */
619         
620         if(la->id.lib==0) return;
621         if(la->id.us==1) {
622                 la->id.lib= 0;
623                 la->id.flag= LIB_LOCAL;
624                 new_id(0, (ID *)la, 0);
625                 return;
626         }
627         
628         ob= G.main->object.first;
629         while(ob) {
630                 if(ob->data==la) {
631                         if(ob->id.lib) lib= 1;
632                         else local= 1;
633                 }
634                 ob= ob->id.next;
635         }
636         
637         if(local && lib==0) {
638                 la->id.lib= 0;
639                 la->id.flag= LIB_LOCAL;
640                 new_id(0, (ID *)la, 0);
641         }
642         else if(local && lib) {
643                 lan= copy_lamp(la);
644                 lan->id.us= 0;
645                 
646                 ob= G.main->object.first;
647                 while(ob) {
648                         if(ob->data==la) {
649                                 
650                                 if(ob->id.lib==0) {
651                                         ob->data= lan;
652                                         lan->id.us++;
653                                         la->id.us--;
654                                 }
655                         }
656                         ob= ob->id.next;
657                 }
658         }
659 }
660
661 void free_camera(Camera *ca)
662 {
663         BPY_free_scriptlink(&ca->scriptlink);
664 }
665
666 void free_lamp(Lamp *la)
667 {
668         MTex *mtex;
669         int a;
670
671         /* scriptlinks */
672                 
673         BPY_free_scriptlink(&la->scriptlink);
674         
675         for(a=0; a<MAX_MTEX; a++) {
676                 mtex= la->mtex[a];
677                 if(mtex && mtex->tex) mtex->tex->id.us--;
678                 if(mtex) MEM_freeN(mtex);
679         }
680         la->ipo= 0;
681
682         BKE_icon_delete(&la->id);
683         la->id.icon_id = 0;
684 }
685
686 void *add_wave()
687 {
688         return 0;
689 }
690
691
692 /* *************************************************** */
693
694 static void *add_obdata_from_type(int type)
695 {
696         switch (type) {
697         case OB_MESH: G.totmesh++; return add_mesh();
698         case OB_CURVE: G.totcurve++; return add_curve(OB_CURVE);
699         case OB_SURF: G.totcurve++; return add_curve(OB_SURF);
700         case OB_FONT: return add_curve(OB_FONT);
701         case OB_MBALL: return add_mball();
702         case OB_CAMERA: return add_camera();
703         case OB_LAMP: G.totlamp++; return add_lamp();
704         case OB_LATTICE: return add_lattice();
705         case OB_WAVE: return add_wave();
706         case OB_ARMATURE: return add_armature();
707         case OB_EMPTY: return NULL;
708         default:
709                 printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
710                 return NULL;
711         }
712 }
713
714 static char *get_obdata_defname(int type)
715 {
716         switch (type) {
717         case OB_MESH: return "Mesh";
718         case OB_CURVE: return "Curve";
719         case OB_SURF: return "Surf";
720         case OB_FONT: return "Font";
721         case OB_MBALL: return "Mball";
722         case OB_CAMERA: return "Camera";
723         case OB_LAMP: return "Lamp";
724         case OB_LATTICE: return "Lattice";
725         case OB_WAVE: return "Wave";
726         case OB_ARMATURE: return "Armature";
727         case OB_EMPTY: return "Empty";
728         default:
729                 printf("get_obdata_defname: Internal error, bad type: %d\n", type);
730                 return "Empty";
731         }
732 }
733
734 /* general add: to G.scene, with layer from area and default name */
735 /* creates minimum required data, but without vertices etc. */
736 Object *add_object(int type)
737 {
738         Object *ob;
739         Base *base;
740         char name[32];
741
742         strcpy(name, get_obdata_defname(type));
743         
744         ob= alloc_libblock(&G.main->object, ID_OB, name);
745         G.totobj++;
746         
747         /* default object vars */
748         ob->type= type;
749         /* ob->transflag= OB_QUAT; */
750         
751         QuatOne(ob->quat);
752         QuatOne(ob->dquat);
753
754         ob->col[0]= ob->col[1]= ob->col[2]= 0.0;
755         ob->col[3]= 1.0;
756         
757         ob->loc[0]= ob->loc[1]= ob->loc[2]= 0.0;
758         ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
759         ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
760
761         Mat4One(ob->parentinv);
762         Mat4One(ob->obmat);
763         ob->dt= OB_SHADED;
764         if(U.flag & USER_MAT_ON_OB) ob->colbits= -1;
765         ob->empty_drawtype= OB_ARROWS;
766         ob->empty_drawsize= 1.0;
767         
768         if(type==OB_CAMERA || type==OB_LAMP) {
769                 ob->trackflag= OB_NEGZ;
770                 ob->upflag= OB_POSY;
771         }
772         else {
773                 ob->trackflag= OB_POSY;
774                 ob->upflag= OB_POSZ;
775         }
776         ob->ipoflag = OB_OFFS_OB+OB_OFFS_PARENT;
777         ob->ipowin= ID_OB;      /* the ipowin shown */
778         ob->dupon= 1; ob->dupoff= 0;
779         ob->dupsta= 1; ob->dupend= 100;
780
781         /* Game engine defaults*/
782         ob->mass= ob->inertia= 1.0f;
783         ob->formfactor= 0.4f;
784         ob->damping= 0.04f;
785         ob->rdamping= 0.1f;
786         ob->anisotropicFriction[0] = 1.0f;
787         ob->anisotropicFriction[1] = 1.0f;
788         ob->anisotropicFriction[2] = 1.0f;
789         ob->gameflag= OB_PROP;
790
791         /* NT fluid sim defaults */
792         ob->fluidsimFlag = 0;
793         ob->fluidsimSettings = NULL;
794         
795         ob->data= add_obdata_from_type(type);
796         
797         ob->lay= G.scene->lay;
798
799         base= scene_add_base(G.scene, ob);
800         scene_select_base(G.scene, base);
801         ob->recalc |= OB_RECALC;
802         
803         return ob;
804 }
805
806 void base_init_from_view3d(Base *base, View3D *v3d)
807 {
808         Object *ob= base->object;
809
810         if (v3d->localview) {
811                 base->lay= ob->lay= v3d->layact + v3d->lay;
812                 VECCOPY(ob->loc, v3d->cursor);
813         } else {
814                 base->lay= ob->lay= v3d->layact;
815                 VECCOPY(ob->loc, G.scene->cursor);
816         }
817
818         v3d->viewquat[0]= -v3d->viewquat[0];
819         if (ob->transflag & OB_QUAT) {
820                 QUATCOPY(ob->quat, v3d->viewquat);
821         } else {
822                 QuatToEul(v3d->viewquat, ob->rot);
823         }
824         v3d->viewquat[0]= -v3d->viewquat[0];
825 }
826
827 SoftBody *copy_softbody(SoftBody *sb)
828 {
829         SoftBody *sbn;
830         
831         if (sb==NULL) return(NULL);
832         
833         sbn= MEM_dupallocN(sb);
834         sbn->totspring= sbn->totpoint= 0;
835         sbn->bpoint= NULL;
836         sbn->bspring= NULL;
837         sbn->ctime= 0.0f;
838         
839         sbn->keys= NULL;
840         sbn->totkey= sbn->totpointkey= 0;
841         
842         return sbn;
843 }
844
845 Object *copy_object(Object *ob)
846 {
847         Object *obn;
848         ModifierData *md;
849         int a;
850
851         obn= copy_libblock(ob);
852         
853         if(ob->totcol) {
854                 obn->mat= MEM_dupallocN(ob->mat);
855         }
856         
857         if(ob->bb) obn->bb= MEM_dupallocN(ob->bb);
858         obn->path= 0;
859         obn->flag &= ~OB_FROMGROUP;
860         
861         copy_effects(&obn->effect, &ob->effect);
862         obn->modifiers.first = obn->modifiers.last= NULL;
863         
864         for (md=ob->modifiers.first; md; md=md->next) {
865                 ModifierData *nmd = modifier_new(md->type);
866                 modifier_copyData(md, nmd);
867                 BLI_addtail(&obn->modifiers, nmd);
868         }
869         
870         obn->network.first= obn->network.last= 0;
871         
872         BPY_copy_scriptlink(&ob->scriptlink);
873         
874         copy_properties(&obn->prop, &ob->prop);
875         copy_sensors(&obn->sensors, &ob->sensors);
876         copy_controllers(&obn->controllers, &ob->controllers);
877         copy_actuators(&obn->actuators, &ob->actuators);
878         
879         if(ob->pose) {
880                 copy_pose(&obn->pose, ob->pose, 1);
881                 armature_rebuild_pose(obn, obn->data);
882         }
883         copy_defgroups(&obn->defbase, &ob->defbase);
884         copy_nlastrips(&obn->nlastrips, &ob->nlastrips);
885         copy_constraints (&obn->constraints, &ob->constraints);
886
887         clone_constraint_channels (&obn->constraintChannels, &ob->constraintChannels);
888
889         /* increase user numbers */
890         id_us_plus((ID *)obn->data);
891         id_us_plus((ID *)obn->ipo);
892         id_us_plus((ID *)obn->action);
893         id_us_plus((ID *)obn->dup_group);
894
895         for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
896         
897         obn->disp.first= obn->disp.last= NULL;
898         
899         if(ob->pd) obn->pd= MEM_dupallocN(ob->pd);
900         obn->soft= copy_softbody(ob->soft);
901
902         /* NT copy fluid sim setting memory */
903         if(obn->fluidsimSettings) obn->fluidsimSettings = MEM_dupallocN(ob->fluidsimSettings);
904         else obn->fluidsimSettings = NULL;
905         
906         obn->derivedDeform = NULL;
907         obn->derivedFinal = NULL;
908
909         return obn;
910 }
911
912 void expand_local_object(Object *ob)
913 {
914         bActionStrip *strip;
915         int a;
916         
917         id_lib_extern((ID *)ob->action);
918         id_lib_extern((ID *)ob->ipo);
919         id_lib_extern((ID *)ob->data);
920         
921         for(a=0; a<ob->totcol; a++) {
922                 id_lib_extern((ID *)ob->mat[a]);
923         }
924         for (strip=ob->nlastrips.first; strip; strip=strip->next) {
925                 id_lib_extern((ID *)strip->act);
926         }
927
928 }
929
930 void make_local_object(Object *ob)
931 {
932         Object *obn;
933         Scene *sce;
934         Base *base;
935         int local=0, lib=0;
936
937         /* - only lib users: do nothing
938             * - only local users: set flag
939             * - mixed: make copy
940             */
941         
942         if(ob->id.lib==0) return;
943         if(ob->id.us==1) {
944                 ob->id.lib= 0;
945                 ob->id.flag= LIB_LOCAL;
946                 new_id(0, (ID *)ob, 0);
947
948         }
949         else {
950                 sce= G.main->scene.first;
951                 while(sce) {
952                         base= sce->base.first;
953                         while(base) {
954                                 if(base->object==ob) {
955                                         if(sce->id.lib) lib++;
956                                         else local++;
957                                         break;
958                                 }
959                                 base= base->next;
960                         }
961                         sce= sce->id.next;
962                 }
963                 
964                 if(local && lib==0) {
965                         ob->id.lib= 0;
966                         ob->id.flag= LIB_LOCAL;
967                         new_id(0, (ID *)ob, 0);
968                 }
969                 else if(local && lib) {
970                         obn= copy_object(ob);
971                         obn->id.us= 0;
972                         
973                         sce= G.main->scene.first;
974                         while(sce) {
975                                 if(sce->id.lib==0) {
976                                         base= sce->base.first;
977                                         while(base) {
978                                                 if(base->object==ob) {
979                                                         base->object= obn;
980                                                         obn->id.us++;
981                                                         ob->id.us--;
982                                                 }
983                                                 base= base->next;
984                                         }
985                                 }
986                                 sce= sce->id.next;
987                         }
988                 }
989         }
990         
991         expand_local_object(ob);
992 }
993
994 /* *************** CALC ****************** */
995
996 /* there is also a timing calculation in drawobject() */
997
998 float bluroffs= 0.0;
999 int no_speed_curve= 0;
1000
1001 /* ugly call from render */
1002 void set_mblur_offs(float blur)
1003 {
1004         bluroffs= blur;
1005 }
1006         
1007 void disable_speed_curve(int val)
1008 {
1009         no_speed_curve= val;
1010 }
1011
1012 /* ob can be NULL */
1013 float bsystem_time(Object *ob, Object *par, float cfra, float ofs)
1014 {
1015         /* returns float ( see frame_to_float in ipo.c) */
1016
1017         /* 2nd field */
1018 //      if(R.flag & R_SEC_FIELD) {
1019 //              if(R.r.mode & R_FIELDSTILL); else cfra+= .5;
1020 //      }
1021
1022         cfra+= bluroffs;
1023
1024         /* global time */
1025         cfra*= G.scene->r.framelen;     
1026         
1027         if(no_speed_curve==0) if(ob && ob->ipo) cfra= calc_ipo_time(ob->ipo, cfra);
1028         
1029         /* ofset frames */
1030         if(ob && (ob->ipoflag & OB_OFFS_PARENT)) {
1031                 if((ob->partype & PARSLOW)==0) cfra-= ob->sf;
1032         }
1033         
1034         cfra-= ofs;
1035
1036         return cfra;
1037 }
1038
1039 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
1040 {
1041         float smat[3][3], vec[3];
1042         float rmat[3][3];
1043         float q1[4];
1044         
1045         /* size */
1046         if(ob->ipo) {
1047                 vec[0]= ob->size[0]+ob->dsize[0];
1048                 vec[1]= ob->size[1]+ob->dsize[1];
1049                 vec[2]= ob->size[2]+ob->dsize[2];
1050                 SizeToMat3(vec, smat);
1051         }
1052         else {
1053                 SizeToMat3(ob->size, smat);
1054         }
1055
1056         /* rot */
1057         if(ob->transflag & OB_QUAT) {
1058                 if(ob->ipo) {
1059                         QuatMul(q1, ob->quat, ob->dquat);
1060                         QuatToMat3(q1, rmat);
1061                 }
1062                 else {
1063                         QuatToMat3(ob->quat, rmat);
1064                 }
1065         }
1066         else {
1067                 if(ob->ipo) {
1068                         vec[0]= ob->rot[0]+ob->drot[0];
1069                         vec[1]= ob->rot[1]+ob->drot[1];
1070                         vec[2]= ob->rot[2]+ob->drot[2];
1071                         EulToMat3(vec, rmat);
1072                 }
1073                 else {
1074                         EulToMat3(ob->rot, rmat);
1075                 }
1076         }
1077         Mat3MulMat3(mat, rmat, smat);
1078 }
1079
1080 void object_to_mat4(Object *ob, float mat[][4])
1081 {
1082         float tmat[3][3];
1083         
1084         object_to_mat3(ob, tmat);
1085         
1086         Mat4CpyMat3(mat, tmat);
1087         
1088         VECCOPY(mat[3], ob->loc);
1089         if(ob->ipo) {
1090                 mat[3][0]+= ob->dloc[0];
1091                 mat[3][1]+= ob->dloc[1];
1092                 mat[3][2]+= ob->dloc[2];
1093         }
1094 }
1095
1096 int enable_cu_speed= 1;
1097
1098 static void ob_parcurve(Object *ob, Object *par, float mat[][4])
1099 {
1100         Curve *cu;
1101         float q[4], vec[4], dir[3], *quat, x1, ctime;
1102         float timeoffs= 0.0;
1103         
1104         Mat4One(mat);
1105         
1106         cu= par->data;
1107         if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1108                 makeDispListCurveTypes(par, 0);
1109         if(cu->path==NULL) return;
1110         
1111         /* exception, timeoffset is regarded as distance offset */
1112         if(cu->flag & CU_OFFS_PATHDIST) {
1113                 SWAP(float, timeoffs, ob->sf);
1114         }
1115         
1116         /* catch exceptions: feature for nla stride editing */
1117         if(ob->ipoflag & OB_DISABLE_PATH) {
1118                 ctime= 0.0f;
1119         }
1120         /* catch exceptions: curve paths used as a duplicator */
1121         else if(enable_cu_speed) {
1122                 ctime= bsystem_time(ob, par, (float)G.scene->r.cfra, 0.0);
1123                 
1124                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
1125                         ctime /= cu->pathlen;
1126                         CLAMP(ctime, 0.0, 1.0);
1127                 }
1128         }
1129         else {
1130                 ctime= G.scene->r.cfra - ob->sf;
1131                 ctime /= cu->pathlen;
1132                 
1133                 CLAMP(ctime, 0.0, 1.0);
1134         }
1135         
1136         /* time calculus is correct, now apply distance offset */
1137         if(cu->flag & CU_OFFS_PATHDIST) {
1138                 ctime += timeoffs/cu->path->totdist;
1139
1140                 /* restore */
1141                 SWAP(float, timeoffs, ob->sf);
1142         }
1143         
1144         
1145         /* vec: 4 items! */
1146         if( where_on_path(par, ctime, vec, dir) ) {
1147
1148                 if(cu->flag & CU_FOLLOW) {
1149                         quat= vectoquat(dir, ob->trackflag, ob->upflag);
1150                         
1151                         /* the tilt */
1152                         Normalise(dir);
1153                         q[0]= (float)cos(0.5*vec[3]);
1154                         x1= (float)sin(0.5*vec[3]);
1155                         q[1]= -x1*dir[0];
1156                         q[2]= -x1*dir[1];
1157                         q[3]= -x1*dir[2];
1158                         QuatMul(quat, q, quat);
1159                         
1160                         QuatToMat4(quat, mat);
1161                 }
1162                 
1163                 VECCOPY(mat[3], vec);
1164                 
1165         }
1166 }
1167
1168 static void ob_parbone(Object *ob, Object *par, float mat[][4])
1169 {       
1170         bPoseChannel *pchan;
1171         bArmature *arm;
1172         float vec[3];
1173         
1174         arm=get_armature(par);
1175         if (!arm) {
1176                 Mat4One(mat);
1177                 return;
1178         }
1179         
1180         /* Make sure the bone is still valid */
1181         pchan= get_pose_channel(par->pose, ob->parsubstr);
1182         if (!pchan){
1183                 printf ("Bone parent: no bone %s\n", ob->parsubstr);
1184                 Mat4One(mat);
1185                 return;
1186         }
1187
1188         /* get bone transform */
1189         Mat4CpyMat4(mat, pchan->pose_mat);
1190
1191         /* but for backwards compatibility, the child has to move to the tail */
1192         VECCOPY(vec, mat[1]);
1193         VecMulf(vec, pchan->bone->length);
1194         VecAddf(mat[3], mat[3], vec);
1195 }
1196
1197 static void give_parvert(Object *par, int nr, float *vec)
1198 {
1199         EditMesh *em = G.editMesh;
1200         EditVert *eve;
1201 /*      extern ListBase editNurb; already in bad lev calls */
1202         Nurb *nu;
1203         Curve *cu;
1204         BPoint *bp;
1205         BezTriple *bezt;
1206         int a, count;
1207         
1208         vec[0]=vec[1]=vec[2]= 0.0;
1209         
1210         if(par->type==OB_MESH) {
1211                 if(G.obedit && (par->data==G.obedit->data)) {
1212                         if(nr >= G.totvert) nr= 0;
1213
1214                         count= 0;
1215                         eve= em->verts.first;
1216                         while(eve) {
1217                                 if(count==nr) {
1218                                         memcpy(vec, eve->co, 12);
1219                                         break;
1220                                 }
1221                                 eve= eve->next;
1222                                 count++;
1223                         }
1224                 }
1225                 else {
1226                         /* maybe this is against the derivedmesh philosphy, but where_is_object is called
1227                            by code that is called by build_mesh... (when ob->sf!=0.0) so it can cycle eternally */
1228                         DerivedMesh *dm = par->derivedDeform; //mesh_get_derived_deform(par, &needsFree);
1229                         
1230                         if(dm) {
1231                                 if(nr >= dm->getNumVerts(dm)) nr= 0;
1232
1233                                 dm->getVertCo(dm, nr, vec);
1234                         }
1235                 }
1236         }
1237         else if ELEM(par->type, OB_CURVE, OB_SURF) {
1238
1239                 cu= par->data;
1240                 nu= cu->nurb.first;
1241                 if(par==G.obedit) nu= editNurb.first;
1242                 
1243                 count= 0;
1244                 while(nu) {
1245                         if((nu->type & 7)==CU_BEZIER) {
1246                                 bezt= nu->bezt;
1247                                 a= nu->pntsu;
1248                                 while(a--) {
1249                                         if(count==nr) {
1250                                                 VECCOPY(vec, bezt->vec[1]);
1251                                                 break;
1252                                         }
1253                                         count++;
1254                                         bezt++;
1255                                 }
1256                         }
1257                         else {
1258                                 bp= nu->bp;
1259                                 a= nu->pntsu*nu->pntsv;
1260                                 while(a--) {
1261                                         if(count==nr) {
1262                                                 memcpy(vec, bp->vec, 12);
1263                                                 break;
1264                                         }
1265                                         count++;
1266                                         bp++;
1267                                 }
1268                         }
1269                         nu= nu->next;
1270                 }
1271
1272         }
1273         else return;
1274 }
1275
1276 static void ob_parvert3(Object *ob, Object *par, float mat[][4])
1277 {
1278         float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1279
1280         /* in local ob space */
1281         Mat4One(mat);
1282         
1283         if ELEM3(par->type, OB_MESH, OB_SURF, OB_CURVE) {
1284                 
1285                 give_parvert(par, ob->par1, v1);
1286                 give_parvert(par, ob->par2, v2);
1287                 give_parvert(par, ob->par3, v3);
1288                                 
1289                 triatoquat(v1, v2, v3, q);
1290                 QuatToMat3(q, cmat);
1291                 Mat4CpyMat3(mat, cmat);
1292                 
1293                 if(ob->type==OB_CURVE) {
1294                         VECCOPY(mat[3], v1);
1295                 }
1296                 else {
1297                         VecAddf(mat[3], v1, v2);
1298                         VecAddf(mat[3], mat[3], v3);
1299                         VecMulf(mat[3], 0.3333333f);
1300                 }
1301         }
1302 }
1303
1304 static int no_parent_ipo=0;
1305 void set_no_parent_ipo(int val)
1306 {
1307         no_parent_ipo= val;
1308 }
1309
1310 static int during_script_flag=0;
1311 void disable_where_script(short on)
1312 {
1313         during_script_flag= on;
1314 }
1315
1316 int during_script(void) {
1317         return during_script_flag;
1318 }
1319
1320 static int during_scriptlink_flag=0;
1321 void disable_where_scriptlink(short on)
1322 {
1323         during_scriptlink_flag= on;
1324 }
1325
1326 int during_scriptlink(void) {
1327         return during_scriptlink_flag;
1328 }
1329
1330 void where_is_object_time(Object *ob, float ctime)
1331 {
1332         Object *par;
1333         float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
1334         float stime, fac1, fac2, vec[3];
1335         int a;
1336         int pop; 
1337         
1338         /* new version: correct parent+vertexparent and track+parent */
1339         /* this one only calculates direct attached parent and track */
1340         /* is faster, but should keep track of timeoffs */
1341         
1342         if(ob==NULL) return;
1343         
1344         /* this is needed to be able to grab objects with ipos, otherwise it always freezes them */
1345         stime= bsystem_time(ob, 0, ctime, 0.0);
1346         if(stime != ob->ctime) {
1347                 ob->ctime= stime;
1348                 
1349                 if(ob->ipo) {
1350                         calc_ipo(ob->ipo, stime);
1351                         execute_ipo((ID *)ob, ob->ipo);
1352                 }
1353                 else 
1354                         do_all_object_actions(ob);
1355                 
1356                 /* do constraint ipos ... */
1357                 do_constraint_channels(&ob->constraints, &ob->constraintChannels, ctime);
1358         }
1359         else {
1360                 /* but, the drivers have to be done */
1361                 if(ob->ipo) do_ob_ipodrivers(ob, ob->ipo);
1362         }
1363         
1364         if(ob->parent) {
1365                 par= ob->parent;
1366
1367                 if(ob->ipoflag & OB_OFFS_PARENT) ctime-= ob->sf;
1368                 
1369                 /* hurms, code below conflicts with depgraph... (ton) */
1370                 pop= 0;
1371                 if(no_parent_ipo==0 && ctime != par->ctime) {
1372                 
1373                         // only for ipo systems? 
1374                         pushdata(par, sizeof(Object));
1375                         pop= 1;
1376                         
1377                         where_is_object_time(par, ctime);
1378                 }
1379                 
1380                 solve_parenting(ob, par, slowmat, 0);
1381
1382                 if(pop) {
1383                         poplast(par);
1384                 }
1385                 
1386                 if(ob->partype & PARSLOW) {
1387                         // include framerate
1388
1389                         fac1= (1.0f/(1.0f+ fabs(ob->sf)));
1390                         if(fac1>=1.0) return;
1391                         fac2= 1.0f-fac1;
1392                         
1393                         fp1= ob->obmat[0];
1394                         fp2= slowmat[0];
1395                         for(a=0; a<16; a++, fp1++, fp2++) {
1396                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1397                         }
1398                 }
1399         
1400         }
1401         else {
1402                 object_to_mat4(ob, ob->obmat);
1403         }
1404
1405         /* Handle tracking */
1406         if(ob->track) {
1407                 if( ctime != ob->track->ctime) where_is_object_time(ob->track, ctime);
1408                 solve_tracking (ob, ob->track->obmat);
1409                 
1410         }
1411
1412         solve_constraints (ob, TARGET_OBJECT, NULL, ctime);
1413
1414         if(ob->scriptlink.totscript && !during_script()) {
1415                 if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_REDRAW);
1416         }
1417         
1418         /* set negative scale flag in object */
1419         Crossf(vec, ob->obmat[0], ob->obmat[1]);
1420         if( Inpf(vec, ob->obmat[2]) < 0.0 ) ob->transflag |= OB_NEG_SCALE;
1421         else ob->transflag &= ~OB_NEG_SCALE;
1422 }
1423
1424 static void solve_parenting (Object *ob, Object *par, float slowmat[][4], int simul)
1425 {
1426         float totmat[4][4];
1427         float tmat[4][4];
1428         float obmat[4][4];
1429         float vec[3];
1430         int ok;
1431
1432         object_to_mat4(ob, obmat);
1433         
1434         if(ob->partype & PARSLOW) Mat4CpyMat4(slowmat, ob->obmat);
1435         
1436
1437         switch(ob->partype & PARTYPE) {
1438         case PAROBJECT:
1439                 ok= 0;
1440                 if(par->type==OB_CURVE) {
1441                         if( ((Curve *)par->data)->flag & CU_PATH ) {
1442                                 ob_parcurve(ob, par, tmat);
1443                                 ok= 1;
1444                         }
1445                 }
1446                 
1447                 if(ok) Mat4MulSerie(totmat, par->obmat, tmat, 
1448                         NULL, NULL, NULL, NULL, NULL, NULL);
1449                 else Mat4CpyMat4(totmat, par->obmat);
1450                 
1451                 break;
1452         case PARBONE:
1453                 ob_parbone(ob, par, tmat);
1454                 Mat4MulSerie(totmat, par->obmat, tmat,         
1455                         NULL, NULL, NULL, NULL, NULL, NULL);
1456                 break;
1457                 
1458         case PARVERT1:
1459                 Mat4One(totmat);
1460                 if (simul){
1461                         VECCOPY(totmat[3], par->obmat[3]);
1462                 }
1463                 else{
1464                         give_parvert(par, ob->par1, vec);
1465                         VecMat4MulVecfl(totmat[3], par->obmat, vec);
1466                 }
1467                 break;
1468         case PARVERT3:
1469                 ob_parvert3(ob, par, tmat);
1470                 
1471                 Mat4MulSerie(totmat, par->obmat, tmat,         
1472                         NULL, NULL, NULL, NULL, NULL, NULL);
1473                 break;
1474                 
1475         case PARSKEL:
1476                 Mat4CpyMat4(totmat, par->obmat);
1477                 break;
1478         }
1479         
1480         // total 
1481         Mat4MulSerie(tmat, totmat, ob->parentinv,         
1482                 NULL, NULL, NULL, NULL, NULL, NULL);
1483         Mat4MulSerie(ob->obmat, tmat, obmat,         
1484                 NULL, NULL, NULL, NULL, NULL, NULL);
1485         
1486         if (simul){
1487
1488         }
1489         else{
1490                 // external usable originmat 
1491                 Mat3CpyMat4(originmat, tmat);
1492                 
1493                 // origin, voor help line
1494                 if( (ob->partype & 15)==PARSKEL ) {
1495                         VECCOPY(ob->orig, par->obmat[3]);
1496                 }
1497                 else {
1498                         VECCOPY(ob->orig, totmat[3]);
1499                 }
1500         }
1501
1502 }
1503 void solve_tracking (Object *ob, float targetmat[][4])
1504 {
1505         float *quat;
1506         float vec[3];
1507         float totmat[3][3];
1508         float tmat[4][4];
1509         
1510         VecSubf(vec, ob->obmat[3], targetmat[3]);
1511         quat= vectoquat(vec, ob->trackflag, ob->upflag);
1512         QuatToMat3(quat, totmat);
1513         
1514         if(ob->parent && (ob->transflag & OB_POWERTRACK)) {
1515                 /* 'temporal' : clear parent info */
1516                 object_to_mat4(ob, tmat);
1517                 tmat[0][3]= ob->obmat[0][3];
1518                 tmat[1][3]= ob->obmat[1][3];
1519                 tmat[2][3]= ob->obmat[2][3];
1520                 tmat[3][0]= ob->obmat[3][0];
1521                 tmat[3][1]= ob->obmat[3][1];
1522                 tmat[3][2]= ob->obmat[3][2];
1523                 tmat[3][3]= ob->obmat[3][3];
1524         }
1525         else Mat4CpyMat4(tmat, ob->obmat);
1526         
1527         Mat4MulMat34(ob->obmat, totmat, tmat);
1528
1529 }
1530
1531 void where_is_object(Object *ob)
1532 {
1533         
1534         where_is_object_time(ob, (float)G.scene->r.cfra);
1535 }
1536
1537
1538 void where_is_object_simul(Object *ob)
1539 /* was written for the old game engine (until 2.04) */
1540 /* It seems that this function is only called
1541 for a lamp that is the child of another object */
1542 {
1543         Object *par;
1544         Ipo *ipo;
1545         float *fp1, *fp2;
1546         float slowmat[4][4];
1547         float fac1, fac2;
1548         int a;
1549         
1550         /* NO TIMEOFFS */
1551         
1552         /* no ipo! (because of dloc and realtime-ipos) */
1553         ipo= ob->ipo;
1554         ob->ipo= NULL;
1555
1556         if(ob->parent) {
1557                 par= ob->parent;
1558                 
1559                 solve_parenting(ob, par, slowmat, 1);
1560
1561                 if(ob->partype & PARSLOW) {
1562
1563                         fac1= (float)(1.0/(1.0+ fabs(ob->sf)));
1564                         fac2= 1.0f-fac1;
1565                         fp1= ob->obmat[0];
1566                         fp2= slowmat[0];
1567                         for(a=0; a<16; a++, fp1++, fp2++) {
1568                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1569                         }
1570                 }
1571                 
1572         }
1573         else {
1574                 object_to_mat4(ob, ob->obmat);
1575         }
1576         
1577         if(ob->track) 
1578                 solve_tracking(ob, ob->track->obmat);
1579
1580         solve_constraints(ob, TARGET_OBJECT, NULL, G.scene->r.cfra);
1581         
1582         /*  WATCH IT!!! */
1583         ob->ipo= ipo;
1584         
1585 }
1586 extern void Mat4BlendMat4(float out[][4], float dst[][4], float src[][4], float srcweight);
1587
1588 void solve_constraints (Object *ob, short obtype, void *obdata, float ctime)
1589 {
1590         bConstraint *con;
1591         float tmat[4][4], focusmat[4][4], lastmat[4][4];
1592         int i, clear=1, tot=0;
1593         float   a=0;
1594         float   aquat[4], quat[4];
1595         float   aloc[3], loc[3];
1596         float   asize[3], size[3];
1597         float   oldmat[4][4];
1598         float   smat[3][3], rmat[3][3], mat[3][3];
1599         float enf;
1600
1601         for (con = ob->constraints.first; con; con=con->next) {
1602                 // inverse kinematics is solved seperate 
1603                 if (con->type==CONSTRAINT_TYPE_KINEMATIC) continue;
1604                 // and this we can skip completely
1605                 if (con->flag & CONSTRAINT_DISABLE) continue;
1606                 // local constraints are handled in armature.c only 
1607                 if (con->flag & CONSTRAINT_LOCAL) continue;
1608
1609                 /* Clear accumulators if necessary*/
1610                 if (clear) {
1611                         clear= 0;
1612                         a= 0;
1613                         tot= 0;
1614                         memset(aquat, 0, sizeof(float)*4);
1615                         memset(aloc, 0, sizeof(float)*3);
1616                         memset(asize, 0, sizeof(float)*3);
1617                 }
1618                 
1619                 enf = con->enforce;     // value from ipos (from action channels)
1620
1621                 /* Get the targetmat */
1622                 get_constraint_target_matrix(con, obtype, obdata, tmat, size, ctime);
1623                 
1624                 Mat4CpyMat4(focusmat, tmat);
1625                 
1626                 /* Extract the components & accumulate */
1627                 Mat4ToQuat(focusmat, quat);
1628                 VECCOPY(loc, focusmat[3]);
1629                 Mat3CpyMat4(mat, focusmat);
1630                 Mat3ToSize(mat, size);
1631                 
1632                 a+= enf;
1633                 tot++;
1634                 
1635                 for(i=0; i<3; i++) {
1636                         aquat[i+1]+=(quat[i+1]) * enf;
1637                         aloc[i]+=(loc[i]) * enf;
1638                         asize[i]+=(size[i]-1.0f) * enf;
1639                 }
1640                 aquat[0]+=(quat[0])*enf;
1641                 Mat4CpyMat4(lastmat, focusmat);
1642                 
1643                 /* removed for now, probably becomes option? (ton) */
1644                 
1645                 /* If the next constraint is not the same type (or there isn't one),
1646                  *      then evaluate the accumulator & request a clear */
1647                 if (TRUE) { //(!con->next)||(con->next && con->next->type!=con->type)) {
1648                         clear= 1;
1649                         Mat4CpyMat4(oldmat, ob->obmat);
1650
1651                         /*      If we have several inputs, do a blend of them */
1652                         if (tot) {
1653                                 if (tot>1) {
1654                                         if (a) {
1655                                                 for (i=0; i<3; i++) {
1656                                                         asize[i]=1.0f + (asize[i]/(a));
1657                                                         aloc[i]=(aloc[i]/a);
1658                                                 }
1659                                                 
1660                                                 NormalQuat(aquat);
1661                                                 
1662                                                 QuatToMat3(aquat, rmat);
1663                                                 SizeToMat3(asize, smat);
1664                                                 Mat3MulMat3(mat, rmat, smat);
1665                                                 Mat4CpyMat3(focusmat, mat);
1666                                                 VECCOPY(focusmat[3], aloc);
1667
1668                                                 evaluate_constraint(con, ob, obtype, obdata, focusmat);
1669                                         }
1670                                         
1671                                 }       
1672                                 /* If we only have one, blend with the current obmat */
1673                                 else {
1674                                         float solution[4][4];
1675                                         float delta[4][4];
1676                                         float imat[4][4];
1677                                         float identity[4][4];
1678                                         
1679                                         if (con->type!=CONSTRAINT_TYPE_KINEMATIC) {
1680                                                 /* If we're not an IK constraint, solve the constraint then blend it to the previous one */
1681                                                 evaluate_constraint(con, ob, obtype, obdata, lastmat);
1682                                                 
1683                                                 Mat4CpyMat4 (solution, ob->obmat);
1684
1685                                                 /* Interpolate the enforcement */                                       
1686                                                 Mat4Invert (imat, oldmat);
1687                                                 Mat4MulMat4 (delta, solution, imat);
1688                                                 
1689                                                 if (a<1.0) {
1690                                                         Mat4One(identity);
1691                                                         Mat4BlendMat4(delta, identity, delta, a);
1692                                                 }
1693                                                 Mat4MulMat4 (ob->obmat, delta, oldmat);
1694
1695                                         }
1696                                 }
1697                         }
1698                 }
1699         }       
1700 }
1701
1702 /* for calculation of the inverse parent transform, only used for editor */
1703 void what_does_parent(Object *ob)
1704 {
1705
1706         clear_workob();
1707         Mat4One(workob.obmat);
1708         Mat4One(workob.parentinv);
1709         workob.parent= ob->parent;
1710         workob.track= ob->track;
1711
1712         workob.trackflag= ob->trackflag;
1713         workob.upflag= ob->upflag;
1714         
1715         workob.partype= ob->partype;
1716         workob.par1= ob->par1;
1717         workob.par2= ob->par2;
1718         workob.par3= ob->par3;
1719
1720         workob.constraints.first = ob->constraints.first;
1721         workob.constraints.last = ob->constraints.last;
1722
1723         strcpy (workob.parsubstr, ob->parsubstr); 
1724
1725         where_is_object(&workob);
1726 }
1727
1728 BoundBox *unit_boundbox()
1729 {
1730         BoundBox *bb;
1731         float min[3] = {-1,-1,-1}, max[3] = {-1,-1,-1};
1732
1733         bb= MEM_mallocN(sizeof(BoundBox), "bb");
1734         boundbox_set_from_min_max(bb, min, max);
1735         
1736         return bb;
1737 }
1738
1739 void boundbox_set_from_min_max(BoundBox *bb, float min[3], float max[3])
1740 {
1741         bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
1742         bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
1743         
1744         bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
1745         bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
1746
1747         bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
1748         bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
1749 }
1750
1751 void minmax_object(Object *ob, float *min, float *max)
1752 {
1753         BoundBox bb;
1754         Mesh *me;
1755         Curve *cu;
1756         float vec[3];
1757         int a;
1758         
1759         switch(ob->type) {
1760                 
1761         case OB_CURVE:
1762         case OB_FONT:
1763         case OB_SURF:
1764                 cu= ob->data;
1765                 
1766                 if(cu->bb==0) tex_space_curve(cu);
1767                 bb= *(cu->bb);
1768                 
1769                 for(a=0; a<8; a++) {
1770                         Mat4MulVecfl(ob->obmat, bb.vec[a]);
1771                         DO_MINMAX(bb.vec[a], min, max);
1772                 }
1773                 break;
1774         case OB_ARMATURE:
1775                 if(ob->pose) {
1776                         bPoseChannel *pchan;
1777                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1778                                 VECCOPY(vec, pchan->pose_head);
1779                                 Mat4MulVecfl(ob->obmat, vec);
1780                                 DO_MINMAX(vec, min, max);
1781                                 VECCOPY(vec, pchan->pose_tail);
1782                                 Mat4MulVecfl(ob->obmat, vec);
1783                                 DO_MINMAX(vec, min, max);
1784                         }
1785                         break;
1786                 }
1787                 /* no break, get_mesh will give NULL and it passes on to default */
1788         case OB_MESH:
1789                 me= get_mesh(ob);
1790                 
1791                 if(me) {
1792                         bb = *mesh_get_bb(me);
1793                         
1794                         for(a=0; a<8; a++) {
1795                                 Mat4MulVecfl(ob->obmat, bb.vec[a]);
1796                                 DO_MINMAX(bb.vec[a], min, max);
1797                         }
1798                 }
1799                 if(min[0] < max[0] ) break;
1800                 
1801                 /* else here no break!!!, mesh can be zero sized */
1802                 
1803         default:
1804                 DO_MINMAX(ob->obmat[3], min, max);
1805
1806                 VECCOPY(vec, ob->obmat[3]);
1807                 VecAddf(vec, vec, ob->size);
1808                 DO_MINMAX(vec, min, max);
1809
1810                 VECCOPY(vec, ob->obmat[3]);
1811                 VecSubf(vec, vec, ob->size);
1812                 DO_MINMAX(vec, min, max);
1813                 break;
1814         }
1815 }
1816
1817 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
1818 /* requires flags to be set! */
1819 void object_handle_update(Object *ob)
1820 {
1821         if(ob->recalc & OB_RECALC) {
1822                 
1823                 if(ob->recalc & OB_RECALC_OB) where_is_object(ob);
1824                 
1825                 if(ob->recalc & OB_RECALC_DATA) {
1826                         
1827 //                      printf("recalcdata %s\n", ob->id.name+2);
1828                         
1829                         /* includes all keys and modifiers */
1830                         if(ob->type==OB_MESH) {
1831                                 makeDispListMesh(ob);
1832                         }
1833                         else if(ob->type==OB_MBALL) {
1834                                 makeDispListMBall(ob);
1835                         } 
1836                         else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1837                                 makeDispListCurveTypes(ob, 0);
1838                         }
1839                         else if(ob->type==OB_LATTICE) {
1840                                 lattice_calc_modifiers(ob);
1841                         }
1842                         else if(ob->type==OB_ARMATURE) {
1843                                 /* this actually only happens for reading old files... */
1844                                 if(ob->pose==NULL || (ob->pose->flag & POSE_RECALC))
1845                                         armature_rebuild_pose(ob, ob->data);
1846                                 do_all_pose_actions(ob);
1847                                 where_is_pose(ob);
1848                         }
1849                 }
1850                 
1851                 ob->recalc &= ~OB_RECALC;
1852         }
1853 }
1854
1855