- added fix for fluidsim copying bug
[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                 /* backwards compat... non-armatures can get poses in older files? */
882                 if(ob->type==OB_ARMATURE)
883                         armature_rebuild_pose(obn, obn->data);
884         }
885         copy_defgroups(&obn->defbase, &ob->defbase);
886         copy_nlastrips(&obn->nlastrips, &ob->nlastrips);
887         copy_constraints (&obn->constraints, &ob->constraints);
888
889         clone_constraint_channels (&obn->constraintChannels, &ob->constraintChannels);
890
891         /* increase user numbers */
892         id_us_plus((ID *)obn->data);
893         id_us_plus((ID *)obn->ipo);
894         id_us_plus((ID *)obn->action);
895         id_us_plus((ID *)obn->dup_group);
896
897         for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
898         
899         obn->disp.first= obn->disp.last= NULL;
900         
901         if(ob->pd) obn->pd= MEM_dupallocN(ob->pd);
902         obn->soft= copy_softbody(ob->soft);
903
904         /* NT copy fluid sim setting memory */
905         //if(obn->fluidsimSettings) obn->fluidsimSettings = MEM_dupallocN(ob->fluidsimSettings);
906         //else obn->fluidsimSettings = NULL;
907         obn->fluidsimSettings = fluidsimSettingsCopy(ob->fluidsimSettings);
908         
909         obn->derivedDeform = NULL;
910         obn->derivedFinal = NULL;
911
912         return obn;
913 }
914
915 void expand_local_object(Object *ob)
916 {
917         bActionStrip *strip;
918         int a;
919         
920         id_lib_extern((ID *)ob->action);
921         id_lib_extern((ID *)ob->ipo);
922         id_lib_extern((ID *)ob->data);
923         
924         for(a=0; a<ob->totcol; a++) {
925                 id_lib_extern((ID *)ob->mat[a]);
926         }
927         for (strip=ob->nlastrips.first; strip; strip=strip->next) {
928                 id_lib_extern((ID *)strip->act);
929         }
930
931 }
932
933 void make_local_object(Object *ob)
934 {
935         Object *obn;
936         Scene *sce;
937         Base *base;
938         int local=0, lib=0;
939
940         /* - only lib users: do nothing
941             * - only local users: set flag
942             * - mixed: make copy
943             */
944         
945         if(ob->id.lib==0) return;
946         if(ob->id.us==1) {
947                 ob->id.lib= 0;
948                 ob->id.flag= LIB_LOCAL;
949                 new_id(0, (ID *)ob, 0);
950
951         }
952         else {
953                 sce= G.main->scene.first;
954                 while(sce) {
955                         base= sce->base.first;
956                         while(base) {
957                                 if(base->object==ob) {
958                                         if(sce->id.lib) lib++;
959                                         else local++;
960                                         break;
961                                 }
962                                 base= base->next;
963                         }
964                         sce= sce->id.next;
965                 }
966                 
967                 if(local && lib==0) {
968                         ob->id.lib= 0;
969                         ob->id.flag= LIB_LOCAL;
970                         new_id(0, (ID *)ob, 0);
971                 }
972                 else if(local && lib) {
973                         obn= copy_object(ob);
974                         obn->id.us= 0;
975                         
976                         sce= G.main->scene.first;
977                         while(sce) {
978                                 if(sce->id.lib==0) {
979                                         base= sce->base.first;
980                                         while(base) {
981                                                 if(base->object==ob) {
982                                                         base->object= obn;
983                                                         obn->id.us++;
984                                                         ob->id.us--;
985                                                 }
986                                                 base= base->next;
987                                         }
988                                 }
989                                 sce= sce->id.next;
990                         }
991                 }
992         }
993         
994         expand_local_object(ob);
995 }
996
997 /* *************** CALC ****************** */
998
999 /* there is also a timing calculation in drawobject() */
1000
1001 float bluroffs= 0.0f, fieldoffs= 0.0f;
1002 int no_speed_curve= 0;
1003
1004 /* ugly calls from render */
1005 void set_mblur_offs(float blur)
1006 {
1007         bluroffs= blur;
1008 }
1009
1010 void set_field_offs(float field)
1011 {
1012         fieldoffs= field;
1013 }
1014
1015 void disable_speed_curve(int val)
1016 {
1017         no_speed_curve= val;
1018 }
1019
1020 /* ob can be NULL */
1021 float bsystem_time(Object *ob, Object *par, float cfra, float ofs)
1022 {
1023         /* returns float ( see frame_to_float in ipo.c) */
1024
1025         cfra+= bluroffs+fieldoffs;
1026
1027         /* global time */
1028         cfra*= G.scene->r.framelen;     
1029         
1030         if(no_speed_curve==0) if(ob && ob->ipo) cfra= calc_ipo_time(ob->ipo, cfra);
1031         
1032         /* ofset frames */
1033         if(ob && (ob->ipoflag & OB_OFFS_PARENT)) {
1034                 if((ob->partype & PARSLOW)==0) cfra-= ob->sf;
1035         }
1036         
1037         cfra-= ofs;
1038
1039         return cfra;
1040 }
1041
1042 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
1043 {
1044         float smat[3][3], vec[3];
1045         float rmat[3][3];
1046         float q1[4];
1047         
1048         /* size */
1049         if(ob->ipo) {
1050                 vec[0]= ob->size[0]+ob->dsize[0];
1051                 vec[1]= ob->size[1]+ob->dsize[1];
1052                 vec[2]= ob->size[2]+ob->dsize[2];
1053                 SizeToMat3(vec, smat);
1054         }
1055         else {
1056                 SizeToMat3(ob->size, smat);
1057         }
1058
1059         /* rot */
1060         if(ob->transflag & OB_QUAT) {
1061                 if(ob->ipo) {
1062                         QuatMul(q1, ob->quat, ob->dquat);
1063                         QuatToMat3(q1, rmat);
1064                 }
1065                 else {
1066                         QuatToMat3(ob->quat, rmat);
1067                 }
1068         }
1069         else {
1070                 if(ob->ipo) {
1071                         vec[0]= ob->rot[0]+ob->drot[0];
1072                         vec[1]= ob->rot[1]+ob->drot[1];
1073                         vec[2]= ob->rot[2]+ob->drot[2];
1074                         EulToMat3(vec, rmat);
1075                 }
1076                 else {
1077                         EulToMat3(ob->rot, rmat);
1078                 }
1079         }
1080         Mat3MulMat3(mat, rmat, smat);
1081 }
1082
1083 void object_to_mat4(Object *ob, float mat[][4])
1084 {
1085         float tmat[3][3];
1086         
1087         object_to_mat3(ob, tmat);
1088         
1089         Mat4CpyMat3(mat, tmat);
1090         
1091         VECCOPY(mat[3], ob->loc);
1092         if(ob->ipo) {
1093                 mat[3][0]+= ob->dloc[0];
1094                 mat[3][1]+= ob->dloc[1];
1095                 mat[3][2]+= ob->dloc[2];
1096         }
1097 }
1098
1099 int enable_cu_speed= 1;
1100
1101 static void ob_parcurve(Object *ob, Object *par, float mat[][4])
1102 {
1103         Curve *cu;
1104         float q[4], vec[4], dir[3], *quat, x1, ctime;
1105         float timeoffs= 0.0;
1106         
1107         Mat4One(mat);
1108         
1109         cu= par->data;
1110         if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1111                 makeDispListCurveTypes(par, 0);
1112         if(cu->path==NULL) return;
1113         
1114         /* exception, timeoffset is regarded as distance offset */
1115         if(cu->flag & CU_OFFS_PATHDIST) {
1116                 SWAP(float, timeoffs, ob->sf);
1117         }
1118         
1119         /* catch exceptions: feature for nla stride editing */
1120         if(ob->ipoflag & OB_DISABLE_PATH) {
1121                 ctime= 0.0f;
1122         }
1123         /* catch exceptions: curve paths used as a duplicator */
1124         else if(enable_cu_speed) {
1125                 ctime= bsystem_time(ob, par, (float)G.scene->r.cfra, 0.0);
1126                 
1127                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
1128                         ctime /= cu->pathlen;
1129                         CLAMP(ctime, 0.0, 1.0);
1130                 }
1131         }
1132         else {
1133                 ctime= G.scene->r.cfra - ob->sf;
1134                 ctime /= cu->pathlen;
1135                 
1136                 CLAMP(ctime, 0.0, 1.0);
1137         }
1138         
1139         /* time calculus is correct, now apply distance offset */
1140         if(cu->flag & CU_OFFS_PATHDIST) {
1141                 ctime += timeoffs/cu->path->totdist;
1142
1143                 /* restore */
1144                 SWAP(float, timeoffs, ob->sf);
1145         }
1146         
1147         
1148         /* vec: 4 items! */
1149         if( where_on_path(par, ctime, vec, dir) ) {
1150
1151                 if(cu->flag & CU_FOLLOW) {
1152                         quat= vectoquat(dir, ob->trackflag, ob->upflag);
1153                         
1154                         /* the tilt */
1155                         Normalise(dir);
1156                         q[0]= (float)cos(0.5*vec[3]);
1157                         x1= (float)sin(0.5*vec[3]);
1158                         q[1]= -x1*dir[0];
1159                         q[2]= -x1*dir[1];
1160                         q[3]= -x1*dir[2];
1161                         QuatMul(quat, q, quat);
1162                         
1163                         QuatToMat4(quat, mat);
1164                 }
1165                 
1166                 VECCOPY(mat[3], vec);
1167                 
1168         }
1169 }
1170
1171 static void ob_parbone(Object *ob, Object *par, float mat[][4])
1172 {       
1173         bPoseChannel *pchan;
1174         bArmature *arm;
1175         float vec[3];
1176         
1177         arm=get_armature(par);
1178         if (!arm) {
1179                 Mat4One(mat);
1180                 return;
1181         }
1182         
1183         /* Make sure the bone is still valid */
1184         pchan= get_pose_channel(par->pose, ob->parsubstr);
1185         if (!pchan){
1186                 printf ("Bone parent: no bone %s\n", ob->parsubstr);
1187                 Mat4One(mat);
1188                 return;
1189         }
1190
1191         /* get bone transform */
1192         Mat4CpyMat4(mat, pchan->pose_mat);
1193
1194         /* but for backwards compatibility, the child has to move to the tail */
1195         VECCOPY(vec, mat[1]);
1196         VecMulf(vec, pchan->bone->length);
1197         VecAddf(mat[3], mat[3], vec);
1198 }
1199
1200 static void give_parvert(Object *par, int nr, float *vec)
1201 {
1202         EditMesh *em = G.editMesh;
1203         EditVert *eve;
1204 /*      extern ListBase editNurb; already in bad lev calls */
1205         Nurb *nu;
1206         Curve *cu;
1207         BPoint *bp;
1208         BezTriple *bezt;
1209         int a, count;
1210         
1211         vec[0]=vec[1]=vec[2]= 0.0;
1212         
1213         if(par->type==OB_MESH) {
1214                 if(G.obedit && (par->data==G.obedit->data)) {
1215                         if(nr >= G.totvert) nr= 0;
1216
1217                         count= 0;
1218                         eve= em->verts.first;
1219                         while(eve) {
1220                                 if(count==nr) {
1221                                         memcpy(vec, eve->co, 12);
1222                                         break;
1223                                 }
1224                                 eve= eve->next;
1225                                 count++;
1226                         }
1227                 }
1228                 else {
1229                         /* maybe this is against the derivedmesh philosphy, but where_is_object is called
1230                            by code that is called by build_mesh... (when ob->sf!=0.0) so it can cycle eternally */
1231                         DerivedMesh *dm = par->derivedDeform; //mesh_get_derived_deform(par, &needsFree);
1232                         
1233                         if(dm) {
1234                                 int tot= dm->getNumVerts(dm);
1235                                 if(tot) {
1236                                         if(nr >= tot) nr= 0;
1237                                         dm->getVertCo(dm, nr, vec);
1238                                 }
1239                         }
1240                 }
1241         }
1242         else if ELEM(par->type, OB_CURVE, OB_SURF) {
1243
1244                 cu= par->data;
1245                 nu= cu->nurb.first;
1246                 if(par==G.obedit) nu= editNurb.first;
1247                 
1248                 count= 0;
1249                 while(nu) {
1250                         if((nu->type & 7)==CU_BEZIER) {
1251                                 bezt= nu->bezt;
1252                                 a= nu->pntsu;
1253                                 while(a--) {
1254                                         if(count==nr) {
1255                                                 VECCOPY(vec, bezt->vec[1]);
1256                                                 break;
1257                                         }
1258                                         count++;
1259                                         bezt++;
1260                                 }
1261                         }
1262                         else {
1263                                 bp= nu->bp;
1264                                 a= nu->pntsu*nu->pntsv;
1265                                 while(a--) {
1266                                         if(count==nr) {
1267                                                 memcpy(vec, bp->vec, 12);
1268                                                 break;
1269                                         }
1270                                         count++;
1271                                         bp++;
1272                                 }
1273                         }
1274                         nu= nu->next;
1275                 }
1276
1277         }
1278         else return;
1279 }
1280
1281 static void ob_parvert3(Object *ob, Object *par, float mat[][4])
1282 {
1283         float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1284
1285         /* in local ob space */
1286         Mat4One(mat);
1287         
1288         if ELEM3(par->type, OB_MESH, OB_SURF, OB_CURVE) {
1289                 
1290                 give_parvert(par, ob->par1, v1);
1291                 give_parvert(par, ob->par2, v2);
1292                 give_parvert(par, ob->par3, v3);
1293                                 
1294                 triatoquat(v1, v2, v3, q);
1295                 QuatToMat3(q, cmat);
1296                 Mat4CpyMat3(mat, cmat);
1297                 
1298                 if(ob->type==OB_CURVE) {
1299                         VECCOPY(mat[3], v1);
1300                 }
1301                 else {
1302                         VecAddf(mat[3], v1, v2);
1303                         VecAddf(mat[3], mat[3], v3);
1304                         VecMulf(mat[3], 0.3333333f);
1305                 }
1306         }
1307 }
1308
1309 static int no_parent_ipo=0;
1310 void set_no_parent_ipo(int val)
1311 {
1312         no_parent_ipo= val;
1313 }
1314
1315 static int during_script_flag=0;
1316 void disable_where_script(short on)
1317 {
1318         during_script_flag= on;
1319 }
1320
1321 int during_script(void) {
1322         return during_script_flag;
1323 }
1324
1325 static int during_scriptlink_flag=0;
1326 void disable_where_scriptlink(short on)
1327 {
1328         during_scriptlink_flag= on;
1329 }
1330
1331 int during_scriptlink(void) {
1332         return during_scriptlink_flag;
1333 }
1334
1335 void where_is_object_time(Object *ob, float ctime)
1336 {
1337         Object *par;
1338         float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
1339         float stime, fac1, fac2, vec[3];
1340         int a;
1341         int pop; 
1342         
1343         /* new version: correct parent+vertexparent and track+parent */
1344         /* this one only calculates direct attached parent and track */
1345         /* is faster, but should keep track of timeoffs */
1346         
1347         if(ob==NULL) return;
1348         
1349         /* this is needed to be able to grab objects with ipos, otherwise it always freezes them */
1350         stime= bsystem_time(ob, 0, ctime, 0.0);
1351         if(stime != ob->ctime) {
1352                 ob->ctime= stime;
1353                 
1354                 if(ob->ipo) {
1355                         calc_ipo(ob->ipo, stime);
1356                         execute_ipo((ID *)ob, ob->ipo);
1357                 }
1358                 else 
1359                         do_all_object_actions(ob);
1360                 
1361                 /* do constraint ipos ... */
1362                 do_constraint_channels(&ob->constraints, &ob->constraintChannels, ctime);
1363         }
1364         else {
1365                 /* but, the drivers have to be done */
1366                 if(ob->ipo) do_ob_ipodrivers(ob, ob->ipo);
1367         }
1368         
1369         if(ob->parent) {
1370                 par= ob->parent;
1371
1372                 if(ob->ipoflag & OB_OFFS_PARENT) ctime-= ob->sf;
1373                 
1374                 /* hurms, code below conflicts with depgraph... (ton) */
1375                 pop= 0;
1376                 if(no_parent_ipo==0 && ctime != par->ctime) {
1377                 
1378                         // only for ipo systems? 
1379                         pushdata(par, sizeof(Object));
1380                         pop= 1;
1381                         
1382                         where_is_object_time(par, ctime);
1383                 }
1384                 
1385                 solve_parenting(ob, par, slowmat, 0);
1386
1387                 if(pop) {
1388                         poplast(par);
1389                 }
1390                 
1391                 if(ob->partype & PARSLOW) {
1392                         // include framerate
1393
1394                         fac1= (1.0f/(1.0f+ fabs(ob->sf)));
1395                         if(fac1>=1.0) return;
1396                         fac2= 1.0f-fac1;
1397                         
1398                         fp1= ob->obmat[0];
1399                         fp2= slowmat[0];
1400                         for(a=0; a<16; a++, fp1++, fp2++) {
1401                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1402                         }
1403                 }
1404         
1405         }
1406         else {
1407                 object_to_mat4(ob, ob->obmat);
1408         }
1409
1410         /* Handle tracking */
1411         if(ob->track) {
1412                 if( ctime != ob->track->ctime) where_is_object_time(ob->track, ctime);
1413                 solve_tracking (ob, ob->track->obmat);
1414                 
1415         }
1416
1417         solve_constraints (ob, TARGET_OBJECT, NULL, ctime);
1418
1419         if(ob->scriptlink.totscript && !during_script()) {
1420                 if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_REDRAW);
1421         }
1422         
1423         /* set negative scale flag in object */
1424         Crossf(vec, ob->obmat[0], ob->obmat[1]);
1425         if( Inpf(vec, ob->obmat[2]) < 0.0 ) ob->transflag |= OB_NEG_SCALE;
1426         else ob->transflag &= ~OB_NEG_SCALE;
1427 }
1428
1429 static void solve_parenting (Object *ob, Object *par, float slowmat[][4], int simul)
1430 {
1431         float totmat[4][4];
1432         float tmat[4][4];
1433         float obmat[4][4];
1434         float vec[3];
1435         int ok;
1436
1437         object_to_mat4(ob, obmat);
1438         
1439         if(ob->partype & PARSLOW) Mat4CpyMat4(slowmat, ob->obmat);
1440         
1441
1442         switch(ob->partype & PARTYPE) {
1443         case PAROBJECT:
1444                 ok= 0;
1445                 if(par->type==OB_CURVE) {
1446                         if( ((Curve *)par->data)->flag & CU_PATH ) {
1447                                 ob_parcurve(ob, par, tmat);
1448                                 ok= 1;
1449                         }
1450                 }
1451                 
1452                 if(ok) Mat4MulSerie(totmat, par->obmat, tmat, 
1453                         NULL, NULL, NULL, NULL, NULL, NULL);
1454                 else Mat4CpyMat4(totmat, par->obmat);
1455                 
1456                 break;
1457         case PARBONE:
1458                 ob_parbone(ob, par, tmat);
1459                 Mat4MulSerie(totmat, par->obmat, tmat,         
1460                         NULL, NULL, NULL, NULL, NULL, NULL);
1461                 break;
1462                 
1463         case PARVERT1:
1464                 Mat4One(totmat);
1465                 if (simul){
1466                         VECCOPY(totmat[3], par->obmat[3]);
1467                 }
1468                 else{
1469                         give_parvert(par, ob->par1, vec);
1470                         VecMat4MulVecfl(totmat[3], par->obmat, vec);
1471                 }
1472                 break;
1473         case PARVERT3:
1474                 ob_parvert3(ob, par, tmat);
1475                 
1476                 Mat4MulSerie(totmat, par->obmat, tmat,         
1477                         NULL, NULL, NULL, NULL, NULL, NULL);
1478                 break;
1479                 
1480         case PARSKEL:
1481                 Mat4CpyMat4(totmat, par->obmat);
1482                 break;
1483         }
1484         
1485         // total 
1486         Mat4MulSerie(tmat, totmat, ob->parentinv,         
1487                 NULL, NULL, NULL, NULL, NULL, NULL);
1488         Mat4MulSerie(ob->obmat, tmat, obmat,         
1489                 NULL, NULL, NULL, NULL, NULL, NULL);
1490         
1491         if (simul){
1492
1493         }
1494         else{
1495                 // external usable originmat 
1496                 Mat3CpyMat4(originmat, tmat);
1497                 
1498                 // origin, voor help line
1499                 if( (ob->partype & 15)==PARSKEL ) {
1500                         VECCOPY(ob->orig, par->obmat[3]);
1501                 }
1502                 else {
1503                         VECCOPY(ob->orig, totmat[3]);
1504                 }
1505         }
1506
1507 }
1508 void solve_tracking (Object *ob, float targetmat[][4])
1509 {
1510         float *quat;
1511         float vec[3];
1512         float totmat[3][3];
1513         float tmat[4][4];
1514         
1515         VecSubf(vec, ob->obmat[3], targetmat[3]);
1516         quat= vectoquat(vec, ob->trackflag, ob->upflag);
1517         QuatToMat3(quat, totmat);
1518         
1519         if(ob->parent && (ob->transflag & OB_POWERTRACK)) {
1520                 /* 'temporal' : clear parent info */
1521                 object_to_mat4(ob, tmat);
1522                 tmat[0][3]= ob->obmat[0][3];
1523                 tmat[1][3]= ob->obmat[1][3];
1524                 tmat[2][3]= ob->obmat[2][3];
1525                 tmat[3][0]= ob->obmat[3][0];
1526                 tmat[3][1]= ob->obmat[3][1];
1527                 tmat[3][2]= ob->obmat[3][2];
1528                 tmat[3][3]= ob->obmat[3][3];
1529         }
1530         else Mat4CpyMat4(tmat, ob->obmat);
1531         
1532         Mat4MulMat34(ob->obmat, totmat, tmat);
1533
1534 }
1535
1536 void where_is_object(Object *ob)
1537 {
1538         
1539         where_is_object_time(ob, (float)G.scene->r.cfra);
1540 }
1541
1542
1543 void where_is_object_simul(Object *ob)
1544 /* was written for the old game engine (until 2.04) */
1545 /* It seems that this function is only called
1546 for a lamp that is the child of another object */
1547 {
1548         Object *par;
1549         Ipo *ipo;
1550         float *fp1, *fp2;
1551         float slowmat[4][4];
1552         float fac1, fac2;
1553         int a;
1554         
1555         /* NO TIMEOFFS */
1556         
1557         /* no ipo! (because of dloc and realtime-ipos) */
1558         ipo= ob->ipo;
1559         ob->ipo= NULL;
1560
1561         if(ob->parent) {
1562                 par= ob->parent;
1563                 
1564                 solve_parenting(ob, par, slowmat, 1);
1565
1566                 if(ob->partype & PARSLOW) {
1567
1568                         fac1= (float)(1.0/(1.0+ fabs(ob->sf)));
1569                         fac2= 1.0f-fac1;
1570                         fp1= ob->obmat[0];
1571                         fp2= slowmat[0];
1572                         for(a=0; a<16; a++, fp1++, fp2++) {
1573                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1574                         }
1575                 }
1576                 
1577         }
1578         else {
1579                 object_to_mat4(ob, ob->obmat);
1580         }
1581         
1582         if(ob->track) 
1583                 solve_tracking(ob, ob->track->obmat);
1584
1585         solve_constraints(ob, TARGET_OBJECT, NULL, G.scene->r.cfra);
1586         
1587         /*  WATCH IT!!! */
1588         ob->ipo= ipo;
1589         
1590 }
1591 extern void Mat4BlendMat4(float out[][4], float dst[][4], float src[][4], float srcweight);
1592
1593 void solve_constraints (Object *ob, short obtype, void *obdata, float ctime)
1594 {
1595         bConstraint *con;
1596         float tmat[4][4], focusmat[4][4], lastmat[4][4];
1597         int i, clear=1, tot=0;
1598         float   a=0;
1599         float   aquat[4], quat[4];
1600         float   aloc[3], loc[3];
1601         float   asize[3], size[3];
1602         float   oldmat[4][4];
1603         float   smat[3][3], rmat[3][3], mat[3][3];
1604         float enf;
1605
1606         for (con = ob->constraints.first; con; con=con->next) {
1607                 // inverse kinematics is solved seperate 
1608                 if (con->type==CONSTRAINT_TYPE_KINEMATIC) continue;
1609                 // and this we can skip completely
1610                 if (con->flag & CONSTRAINT_DISABLE) continue;
1611                 // local constraints are handled in armature.c only 
1612                 if (con->flag & CONSTRAINT_LOCAL) continue;
1613
1614                 /* Clear accumulators if necessary*/
1615                 if (clear) {
1616                         clear= 0;
1617                         a= 0;
1618                         tot= 0;
1619                         memset(aquat, 0, sizeof(float)*4);
1620                         memset(aloc, 0, sizeof(float)*3);
1621                         memset(asize, 0, sizeof(float)*3);
1622                 }
1623                 
1624                 enf = con->enforce;     // value from ipos (from action channels)
1625
1626                 /* Get the targetmat */
1627                 get_constraint_target_matrix(con, obtype, obdata, tmat, size, ctime);
1628                 
1629                 Mat4CpyMat4(focusmat, tmat);
1630                 
1631                 /* Extract the components & accumulate */
1632                 Mat4ToQuat(focusmat, quat);
1633                 VECCOPY(loc, focusmat[3]);
1634                 Mat3CpyMat4(mat, focusmat);
1635                 Mat3ToSize(mat, size);
1636                 
1637                 a+= enf;
1638                 tot++;
1639                 
1640                 for(i=0; i<3; i++) {
1641                         aquat[i+1]+=(quat[i+1]) * enf;
1642                         aloc[i]+=(loc[i]) * enf;
1643                         asize[i]+=(size[i]-1.0f) * enf;
1644                 }
1645                 aquat[0]+=(quat[0])*enf;
1646                 Mat4CpyMat4(lastmat, focusmat);
1647                 
1648                 /* removed for now, probably becomes option? (ton) */
1649                 
1650                 /* If the next constraint is not the same type (or there isn't one),
1651                  *      then evaluate the accumulator & request a clear */
1652                 if (TRUE) { //(!con->next)||(con->next && con->next->type!=con->type)) {
1653                         clear= 1;
1654                         Mat4CpyMat4(oldmat, ob->obmat);
1655
1656                         /*      If we have several inputs, do a blend of them */
1657                         if (tot) {
1658                                 if (tot>1) {
1659                                         if (a) {
1660                                                 for (i=0; i<3; i++) {
1661                                                         asize[i]=1.0f + (asize[i]/(a));
1662                                                         aloc[i]=(aloc[i]/a);
1663                                                 }
1664                                                 
1665                                                 NormalQuat(aquat);
1666                                                 
1667                                                 QuatToMat3(aquat, rmat);
1668                                                 SizeToMat3(asize, smat);
1669                                                 Mat3MulMat3(mat, rmat, smat);
1670                                                 Mat4CpyMat3(focusmat, mat);
1671                                                 VECCOPY(focusmat[3], aloc);
1672
1673                                                 evaluate_constraint(con, ob, obtype, obdata, focusmat);
1674                                         }
1675                                         
1676                                 }       
1677                                 /* If we only have one, blend with the current obmat */
1678                                 else {
1679                                         float solution[4][4];
1680                                         float delta[4][4];
1681                                         float imat[4][4];
1682                                         float identity[4][4];
1683                                         
1684                                         if (con->type!=CONSTRAINT_TYPE_KINEMATIC) {
1685                                                 /* If we're not an IK constraint, solve the constraint then blend it to the previous one */
1686                                                 evaluate_constraint(con, ob, obtype, obdata, lastmat);
1687                                                 
1688                                                 Mat4CpyMat4 (solution, ob->obmat);
1689
1690                                                 /* Interpolate the enforcement */                                       
1691                                                 Mat4Invert (imat, oldmat);
1692                                                 Mat4MulMat4 (delta, solution, imat);
1693                                                 
1694                                                 if (a<1.0) {
1695                                                         Mat4One(identity);
1696                                                         Mat4BlendMat4(delta, identity, delta, a);
1697                                                 }
1698                                                 Mat4MulMat4 (ob->obmat, delta, oldmat);
1699
1700                                         }
1701                                 }
1702                         }
1703                 }
1704         }       
1705 }
1706
1707 /* for calculation of the inverse parent transform, only used for editor */
1708 void what_does_parent(Object *ob)
1709 {
1710
1711         clear_workob();
1712         Mat4One(workob.obmat);
1713         Mat4One(workob.parentinv);
1714         workob.parent= ob->parent;
1715         workob.track= ob->track;
1716
1717         workob.trackflag= ob->trackflag;
1718         workob.upflag= ob->upflag;
1719         
1720         workob.partype= ob->partype;
1721         workob.par1= ob->par1;
1722         workob.par2= ob->par2;
1723         workob.par3= ob->par3;
1724
1725         workob.constraints.first = ob->constraints.first;
1726         workob.constraints.last = ob->constraints.last;
1727
1728         strcpy (workob.parsubstr, ob->parsubstr); 
1729
1730         where_is_object(&workob);
1731 }
1732
1733 BoundBox *unit_boundbox()
1734 {
1735         BoundBox *bb;
1736         float min[3] = {-1,-1,-1}, max[3] = {-1,-1,-1};
1737
1738         bb= MEM_mallocN(sizeof(BoundBox), "bb");
1739         boundbox_set_from_min_max(bb, min, max);
1740         
1741         return bb;
1742 }
1743
1744 void boundbox_set_from_min_max(BoundBox *bb, float min[3], float max[3])
1745 {
1746         bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
1747         bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
1748         
1749         bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
1750         bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
1751
1752         bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
1753         bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
1754 }
1755
1756 void minmax_object(Object *ob, float *min, float *max)
1757 {
1758         BoundBox bb;
1759         Mesh *me;
1760         Curve *cu;
1761         float vec[3];
1762         int a;
1763         
1764         switch(ob->type) {
1765                 
1766         case OB_CURVE:
1767         case OB_FONT:
1768         case OB_SURF:
1769                 cu= ob->data;
1770                 
1771                 if(cu->bb==0) tex_space_curve(cu);
1772                 bb= *(cu->bb);
1773                 
1774                 for(a=0; a<8; a++) {
1775                         Mat4MulVecfl(ob->obmat, bb.vec[a]);
1776                         DO_MINMAX(bb.vec[a], min, max);
1777                 }
1778                 break;
1779         case OB_ARMATURE:
1780                 if(ob->pose) {
1781                         bPoseChannel *pchan;
1782                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1783                                 VECCOPY(vec, pchan->pose_head);
1784                                 Mat4MulVecfl(ob->obmat, vec);
1785                                 DO_MINMAX(vec, min, max);
1786                                 VECCOPY(vec, pchan->pose_tail);
1787                                 Mat4MulVecfl(ob->obmat, vec);
1788                                 DO_MINMAX(vec, min, max);
1789                         }
1790                         break;
1791                 }
1792                 /* no break, get_mesh will give NULL and it passes on to default */
1793         case OB_MESH:
1794                 me= get_mesh(ob);
1795                 
1796                 if(me) {
1797                         bb = *mesh_get_bb(me);
1798                         
1799                         for(a=0; a<8; a++) {
1800                                 Mat4MulVecfl(ob->obmat, bb.vec[a]);
1801                                 DO_MINMAX(bb.vec[a], min, max);
1802                         }
1803                 }
1804                 if(min[0] < max[0] ) break;
1805                 
1806                 /* else here no break!!!, mesh can be zero sized */
1807                 
1808         default:
1809                 DO_MINMAX(ob->obmat[3], min, max);
1810
1811                 VECCOPY(vec, ob->obmat[3]);
1812                 VecAddf(vec, vec, ob->size);
1813                 DO_MINMAX(vec, min, max);
1814
1815                 VECCOPY(vec, ob->obmat[3]);
1816                 VecSubf(vec, vec, ob->size);
1817                 DO_MINMAX(vec, min, max);
1818                 break;
1819         }
1820 }
1821
1822 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
1823 /* requires flags to be set! */
1824 void object_handle_update(Object *ob)
1825 {
1826         if(ob->recalc & OB_RECALC) {
1827                 
1828                 if(ob->recalc & OB_RECALC_OB) where_is_object(ob);
1829                 
1830                 if(ob->recalc & OB_RECALC_DATA) {
1831                         
1832 //                      printf("recalcdata %s\n", ob->id.name+2);
1833                         
1834                         /* includes all keys and modifiers */
1835                         if(ob->type==OB_MESH) {
1836                                 makeDispListMesh(ob);
1837                         }
1838                         else if(ob->type==OB_MBALL) {
1839                                 makeDispListMBall(ob);
1840                         } 
1841                         else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1842                                 makeDispListCurveTypes(ob, 0);
1843                         }
1844                         else if(ob->type==OB_LATTICE) {
1845                                 lattice_calc_modifiers(ob);
1846                         }
1847                         else if(ob->type==OB_ARMATURE) {
1848                                 /* this actually only happens for reading old files... */
1849                                 if(ob->pose==NULL || (ob->pose->flag & POSE_RECALC))
1850                                         armature_rebuild_pose(ob, ob->data);
1851                                 do_all_pose_actions(ob);
1852                                 where_is_pose(ob);
1853                         }
1854                 }
1855         
1856                 ob->recalc &= ~OB_RECALC;
1857         }
1858 }
1859
1860