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