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