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