Cleanup of blender/ module; Makefiles now compile this warning free.
[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         
583         return la;
584 }
585
586 Lamp *copy_lamp(Lamp *la)
587 {
588         Lamp *lan;
589         int a;
590         
591         lan= copy_libblock(la);
592
593         for(a=0; a<MAX_MTEX; a++) {
594                 if(lan->mtex[a]) {
595                         lan->mtex[a]= MEM_mallocN(sizeof(MTex), "copylamptex");
596                         memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
597                         id_us_plus((ID *)lan->mtex[a]->tex);
598                 }
599         }
600         
601         id_us_plus((ID *)lan->ipo);
602
603         BPY_copy_scriptlink(&la->scriptlink);
604         
605         return lan;
606 }
607
608 void make_local_lamp(Lamp *la)
609 {
610         Object *ob;
611         Lamp *lan;
612         int local=0, lib=0;
613
614         /* - only lib users: do nothing
615             * - only local users: set flag
616             * - mixed: make copy
617             */
618         
619         if(la->id.lib==0) return;
620         if(la->id.us==1) {
621                 la->id.lib= 0;
622                 la->id.flag= LIB_LOCAL;
623                 new_id(0, (ID *)la, 0);
624                 return;
625         }
626         
627         ob= G.main->object.first;
628         while(ob) {
629                 if(ob->data==la) {
630                         if(ob->id.lib) lib= 1;
631                         else local= 1;
632                 }
633                 ob= ob->id.next;
634         }
635         
636         if(local && lib==0) {
637                 la->id.lib= 0;
638                 la->id.flag= LIB_LOCAL;
639                 new_id(0, (ID *)la, 0);
640         }
641         else if(local && lib) {
642                 lan= copy_lamp(la);
643                 lan->id.us= 0;
644                 
645                 ob= G.main->object.first;
646                 while(ob) {
647                         if(ob->data==la) {
648                                 
649                                 if(ob->id.lib==0) {
650                                         ob->data= lan;
651                                         lan->id.us++;
652                                         la->id.us--;
653                                 }
654                         }
655                         ob= ob->id.next;
656                 }
657         }
658 }
659
660 void free_camera(Camera *ca)
661 {
662         BPY_free_scriptlink(&ca->scriptlink);
663 }
664
665 void free_lamp(Lamp *la)
666 {
667         MTex *mtex;
668         int a;
669
670         /* scriptlinks */
671                 
672         BPY_free_scriptlink(&la->scriptlink);
673         
674         for(a=0; a<MAX_MTEX; a++) {
675                 mtex= la->mtex[a];
676                 if(mtex && mtex->tex) mtex->tex->id.us--;
677                 if(mtex) MEM_freeN(mtex);
678         }
679         la->ipo= 0;
680
681         BKE_icon_delete(&la->id);
682         la->id.icon_id = 0;
683 }
684
685 void *add_wave()
686 {
687         return 0;
688 }
689
690
691 /* *************************************************** */
692
693 static void *add_obdata_from_type(int type)
694 {
695         switch (type) {
696         case OB_MESH: G.totmesh++; return add_mesh();
697         case OB_CURVE: G.totcurve++; return add_curve(OB_CURVE);
698         case OB_SURF: G.totcurve++; return add_curve(OB_SURF);
699         case OB_FONT: return add_curve(OB_FONT);
700         case OB_MBALL: return add_mball();
701         case OB_CAMERA: return add_camera();
702         case OB_LAMP: G.totlamp++; return add_lamp();
703         case OB_LATTICE: return add_lattice();
704         case OB_WAVE: return add_wave();
705         case OB_ARMATURE: return add_armature();
706         case OB_EMPTY: return NULL;
707         default:
708                 printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
709                 return NULL;
710         }
711 }
712
713 static char *get_obdata_defname(int type)
714 {
715         switch (type) {
716         case OB_MESH: return "Mesh";
717         case OB_CURVE: return "Curve";
718         case OB_SURF: return "Surf";
719         case OB_FONT: return "Font";
720         case OB_MBALL: return "Mball";
721         case OB_CAMERA: return "Camera";
722         case OB_LAMP: return "Lamp";
723         case OB_LATTICE: return "Lattice";
724         case OB_WAVE: return "Wave";
725         case OB_ARMATURE: return "Armature";
726         case OB_EMPTY: return "Empty";
727         default:
728                 printf("get_obdata_defname: Internal error, bad type: %d\n", type);
729                 return "Empty";
730         }
731 }
732
733 /* general add: to G.scene, with layer from area and default name */
734 /* creates minimum required data, but without vertices etc. */
735 Object *add_object(int type)
736 {
737         Object *ob;
738         Base *base;
739         char name[32];
740
741         strcpy(name, get_obdata_defname(type));
742         
743         ob= alloc_libblock(&G.main->object, ID_OB, name);
744         G.totobj++;
745         
746         /* default object vars */
747         ob->type= type;
748         /* ob->transflag= OB_QUAT; */
749         
750         QuatOne(ob->quat);
751         QuatOne(ob->dquat);
752
753         ob->col[0]= ob->col[1]= ob->col[2]= 0.0;
754         ob->col[3]= 1.0;
755         
756         ob->loc[0]= ob->loc[1]= ob->loc[2]= 0.0;
757         ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
758         ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
759
760         Mat4One(ob->parentinv);
761         Mat4One(ob->obmat);
762         ob->dt= OB_SHADED;
763         if(U.flag & USER_MAT_ON_OB) ob->colbits= -1;
764         ob->empty_drawtype= OB_ARROWS;
765         ob->empty_drawsize= 1.0;
766         
767         if(type==OB_CAMERA || type==OB_LAMP) {
768                 ob->trackflag= OB_NEGZ;
769                 ob->upflag= OB_POSY;
770         }
771         else {
772                 ob->trackflag= OB_POSY;
773                 ob->upflag= OB_POSZ;
774         }
775         ob->ipoflag = OB_OFFS_OB+OB_OFFS_PARENT;
776         ob->ipowin= ID_OB;      /* the ipowin shown */
777         ob->dupon= 1; ob->dupoff= 0;
778         ob->dupsta= 1; ob->dupend= 100;
779
780         /* Game engine defaults*/
781         ob->mass= ob->inertia= 1.0f;
782         ob->formfactor= 0.4f;
783         ob->damping= 0.04f;
784         ob->rdamping= 0.1f;
785         ob->anisotropicFriction[0] = 1.0f;
786         ob->anisotropicFriction[1] = 1.0f;
787         ob->anisotropicFriction[2] = 1.0f;
788         ob->gameflag= OB_PROP;
789
790         /* NT fluid sim defaults */
791         ob->fluidsimFlag = 0;
792         ob->fluidsimSettings = NULL;
793         
794         ob->data= add_obdata_from_type(type);
795         
796         ob->lay= G.scene->lay;
797
798         base= scene_add_base(G.scene, ob);
799         scene_select_base(G.scene, base);
800         ob->recalc |= OB_RECALC;
801         
802         return ob;
803 }
804
805 void base_init_from_view3d(Base *base, View3D *v3d)
806 {
807         Object *ob= base->object;
808
809         if (v3d->localview) {
810                 base->lay= ob->lay= v3d->layact + v3d->lay;
811                 VECCOPY(ob->loc, v3d->cursor);
812         } else {
813                 base->lay= ob->lay= v3d->layact;
814                 VECCOPY(ob->loc, G.scene->cursor);
815         }
816
817         v3d->viewquat[0]= -v3d->viewquat[0];
818         if (ob->transflag & OB_QUAT) {
819                 QUATCOPY(ob->quat, v3d->viewquat);
820         } else {
821                 QuatToEul(v3d->viewquat, ob->rot);
822         }
823         v3d->viewquat[0]= -v3d->viewquat[0];
824 }
825
826 SoftBody *copy_softbody(SoftBody *sb)
827 {
828         SoftBody *sbn;
829         
830         if (sb==NULL) return(NULL);
831         
832         sbn= MEM_dupallocN(sb);
833         sbn->totspring= sbn->totpoint= 0;
834         sbn->bpoint= NULL;
835         sbn->bspring= NULL;
836         sbn->ctime= 0.0f;
837         
838         sbn->keys= NULL;
839         sbn->totkey= sbn->totpointkey= 0;
840         
841         return sbn;
842 }
843
844 Object *copy_object(Object *ob)
845 {
846         Object *obn;
847         ModifierData *md;
848         int a;
849
850         obn= copy_libblock(ob);
851         
852         if(ob->totcol) {
853                 obn->mat= MEM_dupallocN(ob->mat);
854         }
855         
856         if(ob->bb) obn->bb= MEM_dupallocN(ob->bb);
857         obn->path= 0;
858         obn->flag &= ~OB_FROMGROUP;
859         
860         copy_effects(&obn->effect, &ob->effect);
861         obn->modifiers.first = obn->modifiers.last= NULL;
862         
863         for (md=ob->modifiers.first; md; md=md->next) {
864                 ModifierData *nmd = modifier_new(md->type);
865                 modifier_copyData(md, nmd);
866                 BLI_addtail(&obn->modifiers, nmd);
867         }
868         
869         obn->network.first= obn->network.last= 0;
870         
871         BPY_copy_scriptlink(&ob->scriptlink);
872         
873         copy_properties(&obn->prop, &ob->prop);
874         copy_sensors(&obn->sensors, &ob->sensors);
875         copy_controllers(&obn->controllers, &ob->controllers);
876         copy_actuators(&obn->actuators, &ob->actuators);
877         
878         if(ob->pose) {
879                 copy_pose(&obn->pose, ob->pose, 1);
880                 armature_rebuild_pose(obn, obn->data);
881         }
882         copy_defgroups(&obn->defbase, &ob->defbase);
883         copy_nlastrips(&obn->nlastrips, &ob->nlastrips);
884         copy_constraints (&obn->constraints, &ob->constraints);
885
886         clone_constraint_channels (&obn->constraintChannels, &ob->constraintChannels);
887
888         /* increase user numbers */
889         id_us_plus((ID *)obn->data);
890         id_us_plus((ID *)obn->ipo);
891         id_us_plus((ID *)obn->action);
892         id_us_plus((ID *)obn->dup_group);
893
894         for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
895         
896         obn->disp.first= obn->disp.last= NULL;
897         
898         if(ob->pd) obn->pd= MEM_dupallocN(ob->pd);
899         obn->soft= copy_softbody(ob->soft);
900
901         /* NT copy fluid sim setting memory */
902         if(obn->fluidsimSettings) obn->fluidsimSettings = MEM_dupallocN(ob->fluidsimSettings);
903         else obn->fluidsimSettings = NULL;
904         
905         obn->derivedDeform = NULL;
906         obn->derivedFinal = NULL;
907
908         return obn;
909 }
910
911 void expand_local_object(Object *ob)
912 {
913         bActionStrip *strip;
914         int a;
915         
916         id_lib_extern((ID *)ob->action);
917         id_lib_extern((ID *)ob->ipo);
918         id_lib_extern((ID *)ob->data);
919         
920         for(a=0; a<ob->totcol; a++) {
921                 id_lib_extern((ID *)ob->mat[a]);
922         }
923         for (strip=ob->nlastrips.first; strip; strip=strip->next) {
924                 id_lib_extern((ID *)strip->act);
925         }
926
927 }
928
929 void make_local_object(Object *ob)
930 {
931         Object *obn;
932         Scene *sce;
933         Base *base;
934         int local=0, lib=0;
935
936         /* - only lib users: do nothing
937             * - only local users: set flag
938             * - mixed: make copy
939             */
940         
941         if(ob->id.lib==0) return;
942         if(ob->id.us==1) {
943                 ob->id.lib= 0;
944                 ob->id.flag= LIB_LOCAL;
945                 new_id(0, (ID *)ob, 0);
946
947         }
948         else {
949                 sce= G.main->scene.first;
950                 while(sce) {
951                         base= sce->base.first;
952                         while(base) {
953                                 if(base->object==ob) {
954                                         if(sce->id.lib) lib++;
955                                         else local++;
956                                         break;
957                                 }
958                                 base= base->next;
959                         }
960                         sce= sce->id.next;
961                 }
962                 
963                 if(local && lib==0) {
964                         ob->id.lib= 0;
965                         ob->id.flag= LIB_LOCAL;
966                         new_id(0, (ID *)ob, 0);
967                 }
968                 else if(local && lib) {
969                         obn= copy_object(ob);
970                         obn->id.us= 0;
971                         
972                         sce= G.main->scene.first;
973                         while(sce) {
974                                 if(sce->id.lib==0) {
975                                         base= sce->base.first;
976                                         while(base) {
977                                                 if(base->object==ob) {
978                                                         base->object= obn;
979                                                         obn->id.us++;
980                                                         ob->id.us--;
981                                                 }
982                                                 base= base->next;
983                                         }
984                                 }
985                                 sce= sce->id.next;
986                         }
987                 }
988         }
989         
990         expand_local_object(ob);
991 }
992
993 /* *************** CALC ****************** */
994
995 /* there is also a timing calculation in drawobject() */
996
997 float bluroffs= 0.0;
998 int no_speed_curve= 0;
999
1000 /* ugly call from render */
1001 void set_mblur_offs(float blur)
1002 {
1003         bluroffs= blur;
1004 }
1005         
1006 void disable_speed_curve(int val)
1007 {
1008         no_speed_curve= val;
1009 }
1010
1011 /* ob can be NULL */
1012 float bsystem_time(Object *ob, Object *par, float cfra, float ofs)
1013 {
1014         /* returns float ( see frame_to_float in ipo.c) */
1015
1016         /* 2nd field */
1017 //      if(R.flag & R_SEC_FIELD) {
1018 //              if(R.r.mode & R_FIELDSTILL); else cfra+= .5;
1019 //      }
1020
1021         cfra+= bluroffs;
1022
1023         /* global time */
1024         cfra*= G.scene->r.framelen;     
1025         
1026         if(no_speed_curve==0) if(ob && ob->ipo) cfra= calc_ipo_time(ob->ipo, cfra);
1027         
1028         /* ofset frames */
1029         if(ob && (ob->ipoflag & OB_OFFS_PARENT)) {
1030                 if((ob->partype & PARSLOW)==0) cfra-= ob->sf;
1031         }
1032         
1033         cfra-= ofs;
1034
1035         return cfra;
1036 }
1037
1038 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
1039 {
1040         float smat[3][3], vec[3];
1041         float rmat[3][3];
1042         float q1[4];
1043         
1044         /* size */
1045         if(ob->ipo) {
1046                 vec[0]= ob->size[0]+ob->dsize[0];
1047                 vec[1]= ob->size[1]+ob->dsize[1];
1048                 vec[2]= ob->size[2]+ob->dsize[2];
1049                 SizeToMat3(vec, smat);
1050         }
1051         else {
1052                 SizeToMat3(ob->size, smat);
1053         }
1054
1055         /* rot */
1056         if(ob->transflag & OB_QUAT) {
1057                 if(ob->ipo) {
1058                         QuatMul(q1, ob->quat, ob->dquat);
1059                         QuatToMat3(q1, rmat);
1060                 }
1061                 else {
1062                         QuatToMat3(ob->quat, rmat);
1063                 }
1064         }
1065         else {
1066                 if(ob->ipo) {
1067                         vec[0]= ob->rot[0]+ob->drot[0];
1068                         vec[1]= ob->rot[1]+ob->drot[1];
1069                         vec[2]= ob->rot[2]+ob->drot[2];
1070                         EulToMat3(vec, rmat);
1071                 }
1072                 else {
1073                         EulToMat3(ob->rot, rmat);
1074                 }
1075         }
1076         Mat3MulMat3(mat, rmat, smat);
1077 }
1078
1079 void object_to_mat4(Object *ob, float mat[][4])
1080 {
1081         float tmat[3][3];
1082         
1083         object_to_mat3(ob, tmat);
1084         
1085         Mat4CpyMat3(mat, tmat);
1086         
1087         VECCOPY(mat[3], ob->loc);
1088         if(ob->ipo) {
1089                 mat[3][0]+= ob->dloc[0];
1090                 mat[3][1]+= ob->dloc[1];
1091                 mat[3][2]+= ob->dloc[2];
1092         }
1093 }
1094
1095 int enable_cu_speed= 1;
1096
1097 void ob_parcurve(Object *ob, Object *par, float mat[][4])
1098 {
1099         Curve *cu;
1100         float q[4], vec[4], dir[3], *quat, x1, ctime;
1101         float timeoffs= 0.0;
1102         
1103         Mat4One(mat);
1104         
1105         cu= par->data;
1106         if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1107                 makeDispListCurveTypes(par, 0);
1108         if(cu->path==NULL) return;
1109         
1110         /* exception, timeoffset is regarded as distance offset */
1111         if(cu->flag & CU_OFFS_PATHDIST) {
1112                 SWAP(float, timeoffs, ob->sf);
1113         }
1114         
1115         /* catch exceptions: feature for nla stride editing */
1116         if(ob->ipoflag & OB_DISABLE_PATH) {
1117                 ctime= 0.0f;
1118         }
1119         /* catch exceptions: curve paths used as a duplicator */
1120         else if(enable_cu_speed) {
1121                 ctime= bsystem_time(ob, par, (float)G.scene->r.cfra, 0.0);
1122                 
1123                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
1124                         ctime /= cu->pathlen;
1125                         CLAMP(ctime, 0.0, 1.0);
1126                 }
1127         }
1128         else {
1129                 ctime= G.scene->r.cfra - ob->sf;
1130                 ctime /= cu->pathlen;
1131                 
1132                 CLAMP(ctime, 0.0, 1.0);
1133         }
1134         
1135         /* time calculus is correct, now apply distance offset */
1136         if(cu->flag & CU_OFFS_PATHDIST) {
1137                 ctime += timeoffs/cu->path->totdist;
1138
1139                 /* restore */
1140                 SWAP(float, timeoffs, ob->sf);
1141         }
1142         
1143         
1144         /* vec: 4 items! */
1145         if( where_on_path(par, ctime, vec, dir) ) {
1146
1147                 if(cu->flag & CU_FOLLOW) {
1148                         quat= vectoquat(dir, ob->trackflag, ob->upflag);
1149                         
1150                         /* the tilt */
1151                         Normalise(dir);
1152                         q[0]= (float)cos(0.5*vec[3]);
1153                         x1= (float)sin(0.5*vec[3]);
1154                         q[1]= -x1*dir[0];
1155                         q[2]= -x1*dir[1];
1156                         q[3]= -x1*dir[2];
1157                         QuatMul(quat, q, quat);
1158                         
1159                         QuatToMat4(quat, mat);
1160                 }
1161                 
1162                 VECCOPY(mat[3], vec);
1163                 
1164         }
1165 }
1166
1167 void ob_parbone(Object *ob, Object *par, float mat[][4])
1168 {       
1169         bPoseChannel *pchan;
1170         bArmature *arm;
1171         float vec[3];
1172         
1173         arm=get_armature(par);
1174         if (!arm) {
1175                 Mat4One(mat);
1176                 return;
1177         }
1178         
1179         /* Make sure the bone is still valid */
1180         pchan= get_pose_channel(par->pose, ob->parsubstr);
1181         if (!pchan){
1182                 printf ("Bone parent: no bone %s\n", ob->parsubstr);
1183                 Mat4One(mat);
1184                 return;
1185         }
1186
1187         /* get bone transform */
1188         Mat4CpyMat4(mat, pchan->pose_mat);
1189
1190         /* but for backwards compatibility, the child has to move to the tail */
1191         VECCOPY(vec, mat[1]);
1192         VecMulf(vec, pchan->bone->length);
1193         VecAddf(mat[3], mat[3], vec);
1194 }
1195
1196 void give_parvert(Object *par, int nr, float *vec)
1197 {
1198         EditMesh *em = G.editMesh;
1199         EditVert *eve;
1200 /*      extern ListBase editNurb; already in bad lev calls */
1201         Nurb *nu;
1202         Curve *cu;
1203         BPoint *bp;
1204         BezTriple *bezt;
1205         int a, count;
1206         
1207         vec[0]=vec[1]=vec[2]= 0.0;
1208         
1209         if(par->type==OB_MESH) {
1210                 if(G.obedit && (par->data==G.obedit->data)) {
1211                         if(nr >= G.totvert) nr= 0;
1212
1213                         count= 0;
1214                         eve= em->verts.first;
1215                         while(eve) {
1216                                 if(count==nr) {
1217                                         memcpy(vec, eve->co, 12);
1218                                         break;
1219                                 }
1220                                 eve= eve->next;
1221                                 count++;
1222                         }
1223                 }
1224                 else {
1225                         /* maybe this is against the derivedmesh philosphy, but where_is_object is called
1226                            by code that is called by build_mesh... (when ob->sf!=0.0) so it can cycle eternally */
1227                         DerivedMesh *dm = par->derivedDeform; //mesh_get_derived_deform(par, &needsFree);
1228                         
1229                         if(dm) {
1230                                 if(nr >= dm->getNumVerts(dm)) nr= 0;
1231
1232                                 dm->getVertCo(dm, nr, vec);
1233                         }
1234                 }
1235         }
1236         else if ELEM(par->type, OB_CURVE, OB_SURF) {
1237
1238                 cu= par->data;
1239                 nu= cu->nurb.first;
1240                 if(par==G.obedit) nu= editNurb.first;
1241                 
1242                 count= 0;
1243                 while(nu) {
1244                         if((nu->type & 7)==CU_BEZIER) {
1245                                 bezt= nu->bezt;
1246                                 a= nu->pntsu;
1247                                 while(a--) {
1248                                         if(count==nr) {
1249                                                 VECCOPY(vec, bezt->vec[1]);
1250                                                 break;
1251                                         }
1252                                         count++;
1253                                         bezt++;
1254                                 }
1255                         }
1256                         else {
1257                                 bp= nu->bp;
1258                                 a= nu->pntsu*nu->pntsv;
1259                                 while(a--) {
1260                                         if(count==nr) {
1261                                                 memcpy(vec, bp->vec, 12);
1262                                                 break;
1263                                         }
1264                                         count++;
1265                                         bp++;
1266                                 }
1267                         }
1268                         nu= nu->next;
1269                 }
1270
1271         }
1272         else return;
1273 }
1274
1275 void ob_parvert3(Object *ob, Object *par, float mat[][4])
1276 {
1277         float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1278
1279         /* in local ob space */
1280         Mat4One(mat);
1281         
1282         if ELEM3(par->type, OB_MESH, OB_SURF, OB_CURVE) {
1283                 
1284                 give_parvert(par, ob->par1, v1);
1285                 give_parvert(par, ob->par2, v2);
1286                 give_parvert(par, ob->par3, v3);
1287                                 
1288                 triatoquat(v1, v2, v3, q);
1289                 QuatToMat3(q, cmat);
1290                 Mat4CpyMat3(mat, cmat);
1291                 
1292                 if(ob->type==OB_CURVE) {
1293                         VECCOPY(mat[3], v1);
1294                 }
1295                 else {
1296                         VecAddf(mat[3], v1, v2);
1297                         VecAddf(mat[3], mat[3], v3);
1298                         VecMulf(mat[3], 0.3333333f);
1299                 }
1300         }
1301 }
1302
1303 static int no_parent_ipo=0;
1304 void set_no_parent_ipo(int val)
1305 {
1306         no_parent_ipo= val;
1307 }
1308
1309 static float timefac= 1.0;              /* 50 Hz, dtime:2 */
1310 void set_dtime(int dtime)
1311 {
1312         timefac= ((float)(dtime-1))/2.0f;
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= (float)(timefac/(1.0+ 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