9e6691f55256593665527cff2e4d6e07a21baeda
[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 #ifdef WIN32
44 #include "BLI_winstuff.h"
45 #endif
46 #include "MEM_guardedalloc.h"
47
48 #include "DNA_action_types.h"
49 #include "DNA_armature_types.h"
50 #include "DNA_camera_types.h"
51 #include "DNA_constraint_types.h"
52 #include "DNA_curve_types.h"
53 #include "DNA_group_types.h"
54 #include "DNA_ika_types.h"
55 #include "DNA_ipo_types.h"
56 #include "DNA_lamp_types.h"
57 #include "DNA_lattice_types.h"
58 #include "DNA_material_types.h"
59 #include "DNA_mesh_types.h"
60 #include "DNA_meshdata_types.h"
61 #include "DNA_object_types.h"
62 #include "DNA_scene_types.h"
63 #include "DNA_screen_types.h"
64 #include "DNA_space_types.h"
65 #include "DNA_texture_types.h"
66 #include "DNA_userdef_types.h"
67 #include "DNA_view3d_types.h"
68 #include "DNA_world_types.h"
69
70 #include "BKE_armature.h"
71 #include "BKE_action.h"
72 #include "BKE_deform.h"
73 #include "BKE_nla.h"
74
75 #include "BLI_blenlib.h"
76 #include "BLI_arithb.h"
77 #include "BLI_editVert.h"
78
79 #include "BKE_utildefines.h"
80 #include "BKE_bad_level_calls.h"
81
82 #include "BKE_main.h"
83 #include "BKE_global.h"
84
85 #include "BKE_object.h"
86 #include "BKE_blender.h"
87 #include "BKE_screen.h"
88 #include "BKE_ipo.h"
89 #include "BKE_ika.h"
90 #include "BKE_library.h"
91 #include "BKE_mesh.h"
92 #include "BKE_curve.h"
93 #include "BKE_mball.h"
94 #include "BKE_effect.h"
95 #include "BKE_sca.h"
96 #include "BKE_displist.h"
97 #include "BKE_property.h"
98 #include "BKE_anim.h"
99 #include "BKE_group.h"
100 #include "BKE_lattice.h"
101 #include "BKE_constraint.h"
102 #include "BKE_scene.h"
103
104 #include "BPY_extern.h"
105
106 /* Local function protos */
107 static void solve_parenting (Object *ob, Object *par, float slowmat[][4], int simul);
108
109 float originmat[3][3];  /* after where_is_object(), can be used in other functions (bad!) */
110 Object workob;
111
112 void clear_workob(void)
113 {
114         memset(&workob, 0, sizeof(Object));
115         
116         workob.size[0]= workob.size[1]= workob.size[2]= 1.0;
117         
118 }
119
120 void copy_baseflags()
121 {
122         Base *base= G.scene->base.first;
123         
124         while(base) {
125                 base->object->flag= base->flag;
126                 base= base->next;
127         }
128 }
129
130 void copy_objectflags()
131 {
132         Base *base= G.scene->base.first;
133         
134         while(base) {
135                 base->flag= base->object->flag;
136                 base= base->next;
137         }
138 }
139
140 void update_base_layer(Object *ob)
141 {
142         Base *base= G.scene->base.first;
143
144         while (base) {
145                 if (base->object == ob) base->lay= ob->lay;
146                 base= base->next;
147         }
148 }
149
150 /* do not free object itself */
151 void free_object(Object *ob)
152 {
153         int a;
154         
155         /* disconnect specific data */
156         if(ob->data) {
157                 ID *id= ob->data;
158                 id->us--;
159                 if(id->us==0) {
160                         if(ob->type==OB_MESH) unlink_mesh(ob->data);
161                         else if(ob->type==OB_CURVE) unlink_curve(ob->data);
162                         else if(ob->type==OB_MBALL) unlink_mball(ob->data);
163                 }
164                 ob->data= 0;
165         }
166         
167         for(a=0; a<ob->totcol; a++) {
168                 if(ob->mat[a]) ob->mat[a]->id.us--;
169         }
170         if(ob->mat) MEM_freeN(ob->mat);
171         ob->mat= 0;
172         if(ob->bb) MEM_freeN(ob->bb); 
173         ob->bb= 0;
174         if(ob->path) free_path(ob->path); 
175         ob->path= 0;
176         if(ob->ipo) ob->ipo->id.us--;
177         if(ob->action) ob->action->id.us--;
178         if(ob->defbase.first)
179                 BLI_freelistN(&ob->defbase);
180         if(ob->pose) {
181                 clear_pose(ob->pose);
182                 MEM_freeN(ob->pose);
183         }
184         free_effects(&ob->effect);
185         BLI_freelistN(&ob->network);
186         free_properties(&ob->prop);
187         
188         free_sensors(&ob->sensors);
189         free_controllers(&ob->controllers);
190         free_actuators(&ob->actuators);
191         
192         free_constraints(&ob->constraints);
193         free_constraint_channels(&ob->constraintChannels);
194         free_nlastrips(&ob->nlastrips);
195
196         freedisplist(&ob->disp);
197         
198         BPY_free_scriptlink(&ob->scriptlink);
199 }
200
201 void unlink_object(Object *ob)
202 {
203         Object *obt;
204         Material *mat;
205         World *wrld;
206         bScreen *sc;
207         Scene *sce;
208         Curve *cu;
209         Tex *tex;
210         Group *group;
211         int a;
212
213         unlink_controllers(&ob->controllers);
214         unlink_actuators(&ob->actuators);
215         
216         /* check all objects: parents en bevels */
217         obt= G.main->object.first;
218         while(obt) {
219                 if(obt->id.lib==0) {
220                         if(obt->parent==ob) {
221                                 obt->parent= 0;
222                                 if(ob->type==OB_LATTICE) freedisplist(&obt->disp);
223                         }
224                         if(obt->track==ob) obt->track= 0;
225                         if ELEM(obt->type, OB_CURVE, OB_FONT) {
226                                 cu= obt->data;
227                                 if(cu->bevobj==ob) cu->bevobj= 0;
228                                 if(cu->textoncurve==ob) cu->textoncurve= 0;
229                         }
230                         if(obt->type==OB_IKA) {
231                                 Ika *ika= obt->data;
232                                 Deform *def= ika->def;
233                                 
234                                 if(ika->parent==ob) ika->parent= 0;
235                                 a= ika->totdef;
236                                 while(a--) {
237                                         if(def->ob==ob) {
238                                                 ika->totdef= 0;
239                                                 MEM_freeN(ika->def);
240                                                 ika->def= 0;
241                                                 break;
242                                         }
243                                         def++;
244                                 }
245                         }
246                         sca_remove_ob_poin(obt, ob);
247                 }
248                 obt= obt->id.next;
249         }
250         
251         /* materials */
252         mat= G.main->mat.first;
253         while(mat) {
254         
255                 for(a=0; a<8; a++) {
256                         if(mat->mtex[a] && ob==mat->mtex[a]->object) {
257                                 /* actually, test for lib here... to do */
258                                 mat->mtex[a]->object= 0;
259                         }
260                 }
261
262                 mat= mat->id.next;
263         }
264         
265         /* textures */
266         tex= G.main->tex.first;
267         while(tex) {
268                 if(tex->env) {
269                         if(tex->env->object == ob) tex->env->object= 0;
270                 }
271                 tex= tex->id.next;
272         }
273         
274         /* mballs */
275         if(ob->type==OB_MBALL) {
276                 obt= find_basis_mball(ob);
277                 if(obt) freedisplist(&obt->disp);
278         }
279         
280         /* worlds */
281         wrld= G.main->world.first;
282         while(wrld) {
283                 if(wrld->id.lib==0) {
284                         for(a=0; a<6; a++) {
285                                 if(wrld->mtex[a] && ob==wrld->mtex[a]->object)
286                                         wrld->mtex[a]->object =0;
287                         }
288                 }
289                 
290                 wrld= wrld->id.next;
291         }
292                 
293         /* scenes */
294         sce= G.main->scene.first;
295         while(sce) {
296                 if(sce->id.lib==0) {
297                         if(sce->camera==ob) sce->camera= 0;
298                 }
299                 sce= sce->id.next;
300         }
301         /* keys */
302         
303         /* screens */
304         sc= G.main->screen.first;
305         while(sc) {
306                 ScrArea *sa= sc->areabase.first;
307                 while(sa) {
308                         SpaceLink *sl;
309
310                         for (sl= sa->spacedata.first; sl; sl= sl->next) {
311                                 if(sl->spacetype==SPACE_VIEW3D) {
312                                         View3D *v3d= (View3D*) sl;
313
314                                         if(v3d->camera==ob) {
315                                                 v3d->camera= 0;
316                                                 if(v3d->persp>1) v3d->persp= 1;
317                                         }
318                                         if(v3d->localvd && v3d->localvd->camera==ob ) {
319                                                 v3d->localvd->camera= 0;
320                                                 if(v3d->localvd->persp>1) v3d->localvd->persp= 1;
321                                         }
322                                 }
323                         }
324
325                         sa= sa->next;
326                 }
327                 sc= sc->id.next;
328         }
329
330         /* groups */
331         group= G.main->group.first;
332         while(group) {
333                 rem_from_group(group, ob);
334                 group= group->id.next;
335         }
336 }
337
338 int exist_object(Object *obtest)
339 {
340         Object *ob;
341         
342         ob= G.main->object.first;
343         while(ob) {
344                 if(ob==obtest) return 1;
345                 ob= ob->id.next;
346         }
347         return 0;
348 }
349
350 void *add_camera()
351 {
352         Camera *cam;
353         
354         cam=  alloc_libblock(&G.main->camera, ID_CA, "Camera");
355
356         cam->lens= 35.0f;
357         cam->clipsta= 0.1f;
358         cam->clipend= 100.0f;
359         cam->drawsize= 0.5f;
360         
361         return cam;
362 }
363
364 Camera *copy_camera(Camera *cam)
365 {
366         Camera *camn;
367         
368         camn= copy_libblock(cam);
369         id_us_plus((ID *)camn->ipo);
370
371         BPY_copy_scriptlink(&camn->scriptlink);
372         
373         return camn;
374 }
375
376
377
378 void make_local_camera(Camera *cam)
379 {
380         Object *ob;
381         Camera *camn;
382         int local=0, lib=0;
383
384         /* - only lib users: do nothing
385             * - only local users: set flag
386             * - mixed: make copy
387             */
388         
389         if(cam->id.lib==0) return;
390         if(cam->id.us==1) {
391                 cam->id.lib= 0;
392                 cam->id.flag= LIB_LOCAL;
393                 new_id(0, (ID *)cam, 0);
394                 return;
395         }
396         
397         ob= G.main->object.first;
398         while(ob) {
399                 if(ob->data==cam) {
400                         if(ob->id.lib) lib= 1;
401                         else local= 1;
402                 }
403                 ob= ob->id.next;
404         }
405         
406         if(local && lib==0) {
407                 cam->id.lib= 0;
408                 cam->id.flag= LIB_LOCAL;
409                 new_id(0, (ID *)cam, 0);
410         }
411         else if(local && lib) {
412                 camn= copy_camera(cam);
413                 camn->id.us= 0;
414                 
415                 ob= G.main->object.first;
416                 while(ob) {
417                         if(ob->data==cam) {
418                                 
419                                 if(ob->id.lib==0) {
420                                         ob->data= camn;
421                                         camn->id.us++;
422                                         cam->id.us--;
423                                 }
424                         }
425                         ob= ob->id.next;
426                 }
427         }
428 }
429
430
431
432 void *add_lamp(void)
433 {
434         Lamp *la;
435         
436         la=  alloc_libblock(&G.main->lamp, ID_LA, "Lamp");
437         
438         la->r= la->g= la->b= la->k= 1.0;
439         la->haint= la->energy= 1.0;
440         la->dist= 20.0;
441         la->spotsize= 45.0;
442         la->spotblend= 0.15;
443         la->att2= 1.0;
444         la->mode= LA_SHAD;
445         la->bufsize= 512;
446         la->clipsta= 0.5;
447         la->clipend= 40.0;
448         la->shadspotsize= 45.0;
449         la->samp= 3;
450         la->bias= 1.0;
451         la->soft= 3.0;
452         la->ray_samp= la->ray_sampy= la->ray_sampz= 1; 
453         la->area_size=la->area_sizey=la->area_sizez= 1.0; 
454         
455         return la;
456 }
457
458 Lamp *copy_lamp(Lamp *la)
459 {
460         Lamp *lan;
461         int a;
462         
463         lan= copy_libblock(la);
464
465         for(a=0; a<8; a++) {
466                 if(lan->mtex[a]) {
467                         lan->mtex[a]= MEM_mallocN(sizeof(MTex), "copylamptex");
468                         memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
469                         id_us_plus((ID *)lan->mtex[a]->tex);
470                 }
471         }
472         
473         id_us_plus((ID *)lan->ipo);
474
475         BPY_copy_scriptlink(&la->scriptlink);
476         
477         return lan;
478 }
479
480 void make_local_lamp(Lamp *la)
481 {
482         Object *ob;
483         Lamp *lan;
484         int local=0, lib=0;
485
486         /* - only lib users: do nothing
487             * - only local users: set flag
488             * - mixed: make copy
489             */
490         
491         if(la->id.lib==0) return;
492         if(la->id.us==1) {
493                 la->id.lib= 0;
494                 la->id.flag= LIB_LOCAL;
495                 new_id(0, (ID *)la, 0);
496                 return;
497         }
498         
499         ob= G.main->object.first;
500         while(ob) {
501                 if(ob->data==la) {
502                         if(ob->id.lib) lib= 1;
503                         else local= 1;
504                 }
505                 ob= ob->id.next;
506         }
507         
508         if(local && lib==0) {
509                 la->id.lib= 0;
510                 la->id.flag= LIB_LOCAL;
511                 new_id(0, (ID *)la, 0);
512         }
513         else if(local && lib) {
514                 lan= copy_lamp(la);
515                 lan->id.us= 0;
516                 
517                 ob= G.main->object.first;
518                 while(ob) {
519                         if(ob->data==la) {
520                                 
521                                 if(ob->id.lib==0) {
522                                         ob->data= lan;
523                                         lan->id.us++;
524                                         la->id.us--;
525                                 }
526                         }
527                         ob= ob->id.next;
528                 }
529         }
530 }
531
532 void free_camera(Camera *ca)
533 {
534         BPY_free_scriptlink(&ca->scriptlink);
535 }
536
537 void free_lamp(Lamp *la)
538 {
539         MTex *mtex;
540         int a;
541
542         /* scriptlinks */
543                 
544         BPY_free_scriptlink(&la->scriptlink);
545         
546         for(a=0; a<8; a++) {
547                 mtex= la->mtex[a];
548                 if(mtex && mtex->tex) mtex->tex->id.us--;
549                 if(mtex) MEM_freeN(mtex);
550         }
551         la->ipo= 0;
552 }
553
554 void *add_wave()
555 {
556         return 0;
557 }
558
559
560 /* *************************************************** */
561
562 static void *add_obdata_from_type(int type)
563 {
564         switch (type) {
565         case OB_MESH: G.totmesh++; return add_mesh();
566         case OB_CURVE: G.totcurve++; return add_curve(OB_CURVE);
567         case OB_SURF: G.totcurve++; return add_curve(OB_SURF);
568         case OB_FONT: return add_curve(OB_FONT);
569         case OB_MBALL: return add_mball();
570         case OB_CAMERA: return add_camera();
571         case OB_LAMP: G.totlamp++; return add_lamp();
572         case OB_IKA: return add_ika();
573         case OB_LATTICE: return add_lattice();
574         case OB_WAVE: return add_wave();
575         case OB_ARMATURE: return add_armature();
576         case OB_EMPTY: return NULL;
577         default:
578                 printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
579                 return NULL;
580         }
581 }
582
583 static char *get_obdata_defname(int type)
584 {
585         switch (type) {
586         case OB_MESH: return "Mesh";
587         case OB_CURVE: return "Curve";
588         case OB_SURF: return "Surf";
589         case OB_FONT: return "Font";
590         case OB_MBALL: return "Mball";
591         case OB_CAMERA: return "Camera";
592         case OB_LAMP: return "Lamp";
593         case OB_IKA: return "Ika";
594         case OB_LATTICE: return "Lattice";
595         case OB_WAVE: return "Wave";
596         case OB_ARMATURE: return "Armature";
597         case OB_EMPTY: return "Empty";
598         default:
599                 printf("get_obdata_defname: Internal error, bad type: %d\n", type);
600                 return "Empty";
601         }
602 }
603
604 /* general add: to G.scene, with layer from area and default name */
605 /* creates minimum required data, but without vertices etc. */
606 Object *add_object(int type)
607 {
608         Object *ob;
609         Base *base;
610         char name[32];
611
612         if (G.obpose)
613                 exit_posemode(1);
614         
615         strcpy(name, get_obdata_defname(type));
616         
617         ob= alloc_libblock(&G.main->object, ID_OB, name);
618         G.totobj++;
619         
620         /* default object vars */
621         ob->type= type;
622         /* ob->transflag= OB_QUAT; */
623         
624         QuatOne(ob->quat);
625         QuatOne(ob->dquat);
626
627         ob->col[0]= ob->col[1]= ob->col[2]= 0.0;
628         ob->col[3]= 1.0;
629         
630         ob->loc[0]= ob->loc[1]= ob->loc[2]= 0.0;
631         ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
632         ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
633
634         Mat4One(ob->parentinv);
635         Mat4One(ob->obmat);
636         ob->dt= OB_SHADED;
637         if(U.flag & USER_MAT_ON_OB) ob->colbits= -1;
638         
639         if(type==OB_CAMERA || type==OB_LAMP) {
640                 ob->trackflag= OB_NEGZ;
641                 ob->upflag= OB_POSY;
642         }
643         else {
644                 ob->trackflag= OB_POSY;
645                 ob->upflag= OB_POSZ;
646         }
647         ob->ipoflag = OB_OFFS_OB+OB_OFFS_PARENT;
648         
649         ob->dupon= 1; ob->dupoff= 0;
650         ob->dupsta= 1; ob->dupend= 100;
651
652         /* Game engine defaults*/
653         ob->mass= ob->inertia= 1.0f;
654         ob->formfactor= 0.4f;
655         ob->damping= 0.04f;
656         ob->rdamping= 0.1f;
657         ob->anisotropicFriction[0] = 1.0f;
658         ob->anisotropicFriction[1] = 1.0f;
659         ob->anisotropicFriction[2] = 1.0f;
660         ob->gameflag= OB_PROP;
661         
662         ob->data= add_obdata_from_type(type);
663         
664         ob->lay= G.scene->lay;
665
666         base= scene_add_base(G.scene, ob);
667         scene_select_base(G.scene, base);
668
669         return ob;
670 }
671
672 void base_init_from_view3d(Base *base, View3D *v3d)
673 {
674         Object *ob= base->object;
675
676         if (v3d->localview) {
677                 base->lay= ob->lay= v3d->layact + v3d->lay;
678                 VECCOPY(ob->loc, v3d->cursor);
679         } else {
680                 base->lay= ob->lay= v3d->layact;
681                 VECCOPY(ob->loc, G.scene->cursor);
682         }
683
684         v3d->viewquat[0]= -v3d->viewquat[0];
685         if (ob->transflag & OB_QUAT) {
686                 QUATCOPY(ob->quat, v3d->viewquat);
687         } else {
688                 QuatToEul(v3d->viewquat, ob->rot);
689         }
690         v3d->viewquat[0]= -v3d->viewquat[0];
691 }
692
693 Object *copy_object(Object *ob)
694 {
695         Object *obn;
696         int a;
697         bConstraintChannel *actcon;
698
699         obn= copy_libblock(ob);
700         
701         if(ob->totcol) {
702                 obn->mat= MEM_dupallocN(ob->mat);
703         }
704         
705         if(ob->bb) obn->bb= MEM_dupallocN(ob->bb);
706         obn->path= 0;
707         obn->flag &= ~OB_FROMGROUP;
708         
709         copy_effects(&obn->effect, &ob->effect);
710         
711         obn->network.first= obn->network.last= 0;
712         
713         BPY_copy_scriptlink(&ob->scriptlink);
714         
715         copy_properties(&obn->prop, &ob->prop);
716         copy_sensors(&obn->sensors, &ob->sensors);
717         copy_controllers(&obn->controllers, &ob->controllers);
718         copy_actuators(&obn->actuators, &ob->actuators);
719         
720         copy_pose(&obn->pose, ob->pose, 1);
721         copy_defgroups(&obn->defbase, &ob->defbase);
722         copy_nlastrips(&obn->nlastrips, &ob->nlastrips);
723         copy_constraints (&obn->constraints, &ob->constraints);
724
725         actcon = clone_constraint_channels (&obn->constraintChannels, &ob->constraintChannels, ob->activecon);
726         /* If the active constraint channel was in this list, update it */
727         if (actcon)
728                 obn->activecon = actcon;
729
730         /* increase user numbers */
731         id_us_plus((ID *)obn->data);
732         id_us_plus((ID *)obn->ipo);
733         id_us_plus((ID *)obn->action);
734         for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
735         
736         obn->disp.first= obn->disp.last= 0;
737         
738         return obn;
739 }
740
741 void expand_local_object(Object *ob)
742 {
743         int a;
744         
745         id_lib_extern((ID *)ob->action);
746         id_lib_extern((ID *)ob->ipo);
747         id_lib_extern((ID *)ob->data);
748         
749         for(a=0; a<ob->totcol; a++) {
750                 id_lib_extern((ID *)ob->mat[a]);
751         }
752 }
753
754 void make_local_object(Object *ob)
755 {
756         Object *obn;
757         Scene *sce;
758         Base *base;
759         int local=0, lib=0;
760
761         /* - only lib users: do nothing
762             * - only local users: set flag
763             * - mixed: make copy
764             */
765         
766         if(ob->id.lib==0) return;
767         if(ob->id.us==1) {
768                 ob->id.lib= 0;
769                 ob->id.flag= LIB_LOCAL;
770                 new_id(0, (ID *)ob, 0);
771
772         }
773         else {
774                 sce= G.main->scene.first;
775                 while(sce) {
776                         base= sce->base.first;
777                         while(base) {
778                                 if(base->object==ob) {
779                                         if(sce->id.lib) lib++;
780                                         else local++;
781                                         break;
782                                 }
783                                 base= base->next;
784                         }
785                         sce= sce->id.next;
786                 }
787                 
788                 if(local && lib==0) {
789                         ob->id.lib= 0;
790                         ob->id.flag= LIB_LOCAL;
791                         new_id(0, (ID *)ob, 0);
792                 }
793                 else if(local && lib) {
794                         obn= copy_object(ob);
795                         obn->id.us= 0;
796                         
797                         sce= G.main->scene.first;
798                         while(sce) {
799                                 if(sce->id.lib==0) {
800                                         base= sce->base.first;
801                                         while(base) {
802                                                 if(base->object==ob) {
803                                                         base->object= obn;
804                                                         obn->id.us++;
805                                                         ob->id.us--;
806                                                 }
807                                                 base= base->next;
808                                         }
809                                 }
810                                 sce= sce->id.next;
811                         }
812                 }
813         }
814         
815         expand_local_object(ob);
816 }
817
818 /* *************** CALC ****************** */
819
820 /* there is also a timing calculation in drawobject() */
821
822 float bluroffs= 0.0;
823 int no_speed_curve= 0;
824
825 void set_mblur_offs(int blur)
826 {
827         bluroffs= R.r.blurfac*((float)blur);
828         bluroffs/= (float)R.r.osa;
829 }
830         
831 void disable_speed_curve(int val)
832 {
833         no_speed_curve= val;
834 }
835
836 float bsystem_time(Object *ob, Object *par, float cfra, float ofs)
837 {
838         /* returns float ( see frame_to_float in ipo.c) */
839
840         if(no_speed_curve==0) if(ob && ob->ipo) cfra= calc_ipo_time(ob->ipo, cfra);
841         
842         /* 2nd field */
843         if(R.flag & R_SEC_FIELD) {
844                 if(R.r.mode & R_FIELDSTILL); else cfra+= .5;
845         }
846
847         /* motion blur */
848         cfra+= bluroffs;
849
850         /* global time */
851         cfra*= G.scene->r.framelen;     
852         
853         /* ofset frames */
854         if(ob && (ob->ipoflag & OB_OFFS_PARENT)) {
855                 if((ob->partype & PARSLOW)==0) cfra-= ob->sf;
856         }
857         
858         cfra-= ofs;
859
860         return cfra;
861 }
862
863 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
864 {
865         float smat[3][3], vec[3];
866         float rmat[3][3];
867         float q1[4];
868         
869         /* size */
870         if(ob->ipo) {
871                 vec[0]= ob->size[0]+ob->dsize[0];
872                 vec[1]= ob->size[1]+ob->dsize[1];
873                 vec[2]= ob->size[2]+ob->dsize[2];
874                 SizeToMat3(vec, smat);
875         }
876         else {
877                 SizeToMat3(ob->size, smat);
878         }
879
880         /* rot */
881         if(ob->transflag & OB_QUAT) {
882                 if(ob->ipo) {
883                         QuatMul(q1, ob->quat, ob->dquat);
884                         QuatToMat3(q1, rmat);
885                 }
886                 else {
887                         QuatToMat3(ob->quat, rmat);
888                 }
889         }
890         else {
891                 if(ob->ipo) {
892                         vec[0]= ob->rot[0]+ob->drot[0];
893                         vec[1]= ob->rot[1]+ob->drot[1];
894                         vec[2]= ob->rot[2]+ob->drot[2];
895                         EulToMat3(vec, rmat);
896                 }
897                 else {
898                         EulToMat3(ob->rot, rmat);
899                 }
900         }
901         Mat3MulMat3(mat, rmat, smat);
902 }
903
904 void object_to_mat4(Object *ob, float mat[][4])
905 {
906         float tmat[3][3];
907         
908         object_to_mat3(ob, tmat);
909         
910         Mat4CpyMat3(mat, tmat);
911         
912         VECCOPY(mat[3], ob->loc);
913         if(ob->ipo) {
914                 mat[3][0]+= ob->dloc[0];
915                 mat[3][1]+= ob->dloc[1];
916                 mat[3][2]+= ob->dloc[2];
917         }
918 }
919
920 int enable_cu_speed= 1;
921
922 void ob_parcurve(Object *ob, Object *par, float mat[][4])
923 {
924         Curve *cu;
925         float q[4], vec[4], dir[3], *quat, x1, ctime;
926         
927         Mat4One(mat);
928         
929         cu= par->data;
930         if(cu->path==0 || cu->path->data==0) calc_curvepath(par);
931         if(cu->path==0) return;
932         
933         /* catch exceptions: curve paths used as a duplicator */
934         if(enable_cu_speed) {
935                 ctime= bsystem_time(ob, par, (float)G.scene->r.cfra, 0.0);
936                 
937                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
938                         ctime /= cu->pathlen;
939                         CLAMP(ctime, 0.0, 1.0);
940                 }
941         }
942         else {
943                 ctime= G.scene->r.cfra - ob->sf;
944                 ctime /= cu->pathlen;
945                 
946                 CLAMP(ctime, 0.0, 1.0);
947         }
948
949         if( where_on_path(par, ctime, vec, dir) ) {
950
951                 if(cu->flag & CU_FOLLOW) {
952                         quat= vectoquat(dir, ob->trackflag, ob->upflag);
953
954                         Normalise(dir);
955                         q[0]= (float)cos(0.5*vec[3]);
956                         x1= (float)sin(0.5*vec[3]);
957                         q[1]= -x1*dir[0];
958                         q[2]= -x1*dir[1];
959                         q[3]= -x1*dir[2];
960                         QuatMul(quat, q, quat);
961                         
962                         QuatToMat4(quat, mat);
963                 }
964                 
965                 VECCOPY(mat[3], vec);
966                 
967         }
968 }
969
970 void ob_parbone(Object *ob, Object *par, float mat[][4])
971 {       
972         Bone *bone;
973         bArmature *arm;
974
975         Mat4One(mat);
976         arm=get_armature(par);
977         if (!arm)
978                 return;
979
980         /* Make sure the bone is still valid */
981         bone = get_named_bone(arm, ob->parsubstr);
982         if (!bone){
983                 printf ("Lost bone %s\n", ob->parsubstr);
984                 return;
985         }
986
987         apply_pose_armature(arm, par->pose, 1); /* Hopefully can set doit parameter in the future */
988         where_is_bone (par, bone); 
989
990         /* Translate by negative bone */
991         get_objectspace_bone_matrix(bone, mat, 0, 1);
992
993 }
994
995 void ob_parlimb(Object *ob, Object *par, float mat[][4])
996 {       
997         Ika *ika;
998         Limb *li;
999         float ang=0.0;
1000         int cur=0;
1001         
1002         /* in local ob space */
1003         Mat4One(mat);
1004         
1005         ika= par->data;
1006         li= ika->limbbase.first;
1007         while(li) {
1008                 ang+= li->alpha;
1009                 if(cur==ob->par1 || li->next==0) break;
1010                 
1011                 cur++;
1012                 li= li->next;
1013         }
1014         
1015         mat[0][0]= (float)cos(ang);
1016         mat[1][0]= (float)-sin(ang);
1017         mat[0][1]= (float)sin(ang);
1018         mat[1][1]= (float)cos(ang);
1019         
1020         mat[3][0]= li->eff[0];
1021         mat[3][1]= li->eff[1];
1022         
1023 }
1024
1025 void give_parvert(Object *par, int nr, float *vec)
1026 {
1027         EditMesh *em = G.editMesh;
1028         Mesh *me;
1029         EditVert *eve;
1030 /*      extern ListBase editNurb; already in bad lev calls */
1031         Nurb *nu;
1032         Curve *cu;
1033         BPoint *bp;
1034         DispList *dl;
1035         BezTriple *bezt;
1036         float *fp;
1037         int a, count;
1038         
1039         vec[0]=vec[1]=vec[2]= 0.0;
1040         
1041         if(par->type==OB_MESH) {
1042                 if(par==G.obedit) {
1043                         if(nr >= G.totvert) nr= 0;
1044
1045                         count= 0;
1046                         eve= em->verts.first;
1047                         while(eve) {
1048                                 if(count==nr) {
1049                                         memcpy(vec, eve->co, 12);
1050                                         break;
1051                                 }
1052                                 eve= eve->next;
1053                                 count++;
1054                         }
1055                 }
1056                 else {
1057                         me= par->data;
1058                         if(me->totvert) {
1059                                 if(nr >= me->totvert) nr= 0;
1060                                 
1061                                 /* is there a deform */
1062                                 dl= find_displist(&par->disp, DL_VERTS);
1063                                 if(dl) {
1064                                         fp= dl->verts+3*nr;
1065                                         VECCOPY(vec, fp);
1066                                 }
1067                                 else {
1068                                         MVert *mvert= me->mvert + nr;
1069                                         VECCOPY(vec, mvert->co);
1070                                 }
1071                         }
1072                 }
1073         }
1074         else if ELEM(par->type, OB_CURVE, OB_SURF) {
1075
1076                 cu= par->data;
1077                 nu= cu->nurb.first;
1078                 if(par==G.obedit) nu= editNurb.first;
1079                 
1080                 count= 0;
1081                 while(nu) {
1082                         if((nu->type & 7)==CU_BEZIER) {
1083                                 bezt= nu->bezt;
1084                                 a= nu->pntsu;
1085                                 while(a--) {
1086                                         if(count==nr) {
1087                                                 VECCOPY(vec, bezt->vec[1]);
1088                                                 break;
1089                                         }
1090                                         count++;
1091                                         bezt++;
1092                                 }
1093                         }
1094                         else {
1095                                 bp= nu->bp;
1096                                 a= nu->pntsu*nu->pntsv;
1097                                 while(a--) {
1098                                         if(count==nr) {
1099                                                 memcpy(vec, bp->vec, 12);
1100                                                 break;
1101                                         }
1102                                         count++;
1103                                         bp++;
1104                                 }
1105                         }
1106                         nu= nu->next;
1107                 }
1108
1109         }
1110         else if(par->type==OB_IKA) {
1111                 Ika *ika= par->data;
1112                 Limb *li= ika->limbbase.first;
1113                 int cur= 1;
1114                 if(nr) {
1115                         while(li) {
1116                                 if(cur==nr || li->next==0) break;
1117                                 cur++;
1118                                 li= li->next;
1119                         }
1120                         vec[0]= li->eff[0];
1121                         vec[1]= li->eff[1];
1122                 }
1123         }
1124         else return;
1125 }
1126
1127 void ob_parvert3(Object *ob, Object *par, float mat[][4])
1128 {
1129         float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1130
1131         /* in local ob space */
1132         Mat4One(mat);
1133         
1134         if ELEM3(par->type, OB_MESH, OB_SURF, OB_CURVE) {
1135                 
1136                 give_parvert(par, ob->par1, v1);
1137                 give_parvert(par, ob->par2, v2);
1138                 give_parvert(par, ob->par3, v3);
1139                                 
1140                 triatoquat(v1, v2, v3, q);
1141                 QuatToMat3(q, cmat);
1142                 Mat4CpyMat3(mat, cmat);
1143                 
1144                 if(ob->type==OB_CURVE) {
1145                         VECCOPY(mat[3], v1);
1146                 }
1147                 else {
1148                         VecAddf(mat[3], v1, v2);
1149                         VecAddf(mat[3], mat[3], v3);
1150                         VecMulf(mat[3], 0.3333333f);
1151                 }
1152         }
1153 }
1154
1155 static int no_parent_ipo=0;
1156 void set_no_parent_ipo(int val)
1157 {
1158         no_parent_ipo= val;
1159 }
1160
1161 static float timefac= 1.0;              /* 50 Hz, dtime:2 */
1162 void set_dtime(int dtime)
1163 {
1164         timefac= ((float)(dtime-1))/2.0f;
1165 }
1166
1167 static int during_script_flag=0;
1168 void disable_where_script(short on)
1169 {
1170         during_script_flag= on; 
1171 }
1172
1173 int during_script(void) {
1174         return during_script_flag;
1175 }
1176
1177 void where_is_object_time(Object *ob, float ctime)
1178 {
1179         Object *par;
1180         float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
1181         float stime, fac1, fac2, vec[3];
1182         int a;
1183         int pop; 
1184         
1185         /* new version: correct parent+vertexparent and track+parent */
1186         /* this one only calculates direct attached parent and track */
1187         /* hij is sneller, maar moet wel de timeoffs in de gaten houden */
1188         
1189         if(ob==0) return;
1190
1191         if( ctime != ob->ctime) {
1192                 ob->ctime= ctime;
1193                 
1194                 if(ob->ipo) {
1195                         
1196                         stime= bsystem_time(ob, 0, ctime, 0.0);
1197
1198                         calc_ipo(ob->ipo, stime);
1199                         execute_ipo((ID *)ob, ob->ipo);
1200                 }                       
1201         }
1202
1203
1204         if(ob->type==OB_IKA) {
1205                 Ika *ika= ob->data;
1206                 if(ika->parent) where_is_object_time(ika->parent, ctime);
1207         }
1208
1209         if(ob->parent) {
1210                 par= ob->parent;
1211
1212                 if(ob->ipoflag & OB_OFFS_PARENT) ctime-= ob->sf;
1213                 
1214                 pop= 0;
1215                 if(no_parent_ipo==0 && ctime != par->ctime) {
1216                 
1217                         // alleen voor ipo systemen? 
1218                         pushdata(par, sizeof(Object));
1219                         pop= 1;
1220                         
1221                         where_is_object_time(par, ctime);
1222                 }
1223                 
1224                 solve_parenting(ob, par, slowmat, 0);
1225
1226                 if(pop) {
1227                         poplast(par);
1228                 }
1229                 
1230                 if(ob->partype & PARSLOW) {
1231                         // framerate meetellen 
1232
1233                         fac1= (float)(timefac/(1.0+ fabs(ob->sf)));
1234                         if(fac1>=1.0) return;
1235                         fac2= 1.0f-fac1;
1236                         
1237                         fp1= ob->obmat[0];
1238                         fp2= slowmat[0];
1239                         for(a=0; a<16; a++, fp1++, fp2++) {
1240                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1241                         }
1242                 }
1243         
1244         }
1245         else {
1246                 object_to_mat4(ob, ob->obmat);
1247         }
1248
1249         /* Handle tracking */
1250         if(ob->track) {
1251                 if( ctime != ob->track->ctime) where_is_object_time(ob->track, ctime);
1252                 solve_tracking (ob, ob->track->obmat);
1253                 
1254         }
1255
1256         solve_constraints (ob, TARGET_OBJECT, NULL, ctime);
1257
1258         if(ob->scriptlink.totscript && !during_script()) {
1259                 BPY_do_pyscript((ID *)ob, SCRIPT_REDRAW);
1260         }
1261         
1262         /* set negative scale flag in object */
1263         Crossf(vec, ob->obmat[0], ob->obmat[1]);
1264         if( Inpf(vec, ob->obmat[2]) < 0.0 ) ob->transflag |= OB_NEG_SCALE;
1265         else ob->transflag &= ~OB_NEG_SCALE;
1266 }
1267
1268 static void solve_parenting (Object *ob, Object *par, float slowmat[][4], int simul)
1269 {
1270         float totmat[4][4];
1271         float tmat[4][4];
1272         float obmat[4][4];
1273         float vec[3];
1274         int ok;
1275
1276         object_to_mat4(ob, obmat);
1277         
1278         if(ob->partype & PARSLOW) Mat4CpyMat4(slowmat, ob->obmat);
1279         
1280
1281         switch(ob->partype & PARTYPE) {
1282         case PAROBJECT:
1283                 ok= 0;
1284                 if(par->type==OB_CURVE) {
1285                         if( ((Curve *)par->data)->flag & CU_PATH ) {
1286                                 ob_parcurve(ob, par, tmat);
1287                                 ok= 1;
1288                         }
1289                 }
1290                 
1291                 if(ok) Mat4MulSerie(totmat, par->obmat, tmat, 
1292                         NULL, NULL, NULL, NULL, NULL, NULL);
1293                 else Mat4CpyMat4(totmat, par->obmat);
1294                 
1295                 break;
1296         case PARBONE:
1297                 ob_parbone(ob, par, tmat);
1298                 Mat4MulSerie(totmat, par->obmat, tmat,         
1299                         NULL, NULL, NULL, NULL, NULL, NULL);
1300                 break;
1301         case PARLIMB:   
1302                 ob_parlimb(ob, par, tmat);
1303                 Mat4MulSerie(totmat, par->obmat, tmat,         
1304                         NULL, NULL, NULL, NULL, NULL, NULL);
1305                 break;
1306                 
1307         case PARVERT1:
1308                 Mat4One(totmat);
1309                 if (simul){
1310                         VECCOPY(totmat[3], par->obmat[3]);
1311                 }
1312                 else{
1313                         give_parvert(par, ob->par1, vec);
1314                         VecMat4MulVecfl(totmat[3], par->obmat, vec);
1315                 }
1316                 break;
1317         case PARVERT3:
1318                 ob_parvert3(ob, par, tmat);
1319                 
1320                 Mat4MulSerie(totmat, par->obmat, tmat,         
1321                         NULL, NULL, NULL, NULL, NULL, NULL);
1322                 break;
1323                 
1324         case PARSKEL:
1325 #if 0
1326                 if (ob!=G.obedit)
1327                         Mat4One(totmat);
1328                 else
1329                         Mat4CpyMat4(totmat, par->obmat);
1330                 break;
1331 #else
1332                         Mat4CpyMat4(totmat, par->obmat);
1333 #endif
1334         }
1335         
1336         // totaal 
1337         Mat4MulSerie(tmat, totmat, ob->parentinv,         
1338                 NULL, NULL, NULL, NULL, NULL, NULL);
1339         Mat4MulSerie(ob->obmat, tmat, obmat,         
1340                 NULL, NULL, NULL, NULL, NULL, NULL);
1341         
1342         if (simul){
1343
1344         }
1345         else{
1346                 // >>>>>>>>>>>>>>>>>>
1347                 // dit is een extern bruikbare originmat 
1348                 Mat3CpyMat4(originmat, tmat);
1349                 
1350                 // origin, voor hulplijntje 
1351                 if( (ob->partype & 15)==PARSKEL ) {
1352                         VECCOPY(ob->orig, par->obmat[3]);
1353                 }
1354                 else {
1355                         VECCOPY(ob->orig, totmat[3]);
1356                 }
1357         }
1358
1359 }
1360 void solve_tracking (Object *ob, float targetmat[][4])
1361 {
1362         float *quat;
1363         float vec[3];
1364         float totmat[3][3];
1365         float tmat[4][4];
1366         
1367         VecSubf(vec, ob->obmat[3], targetmat[3]);
1368         quat= vectoquat(vec, ob->trackflag, ob->upflag);
1369         QuatToMat3(quat, totmat);
1370         
1371         if(ob->parent && (ob->transflag & OB_POWERTRACK)) {
1372                 /* 'temporal' : clear parent info */
1373                 object_to_mat4(ob, tmat);
1374                 tmat[0][3]= ob->obmat[0][3];
1375                 tmat[1][3]= ob->obmat[1][3];
1376                 tmat[2][3]= ob->obmat[2][3];
1377                 tmat[3][0]= ob->obmat[3][0];
1378                 tmat[3][1]= ob->obmat[3][1];
1379                 tmat[3][2]= ob->obmat[3][2];
1380                 tmat[3][3]= ob->obmat[3][3];
1381         }
1382         else Mat4CpyMat4(tmat, ob->obmat);
1383         
1384         Mat4MulMat34(ob->obmat, totmat, tmat);
1385
1386 }
1387
1388 void where_is_object(Object *ob)
1389 {
1390         
1391         /* these have been mem copied */
1392         if(ob->flag & OB_FROMDUPLI) return;
1393         
1394         where_is_object_time(ob, (float)G.scene->r.cfra);
1395 }
1396
1397
1398 void where_is_object_simul(Object *ob)
1399 /* was written for the old game engine (until 2.04) */
1400 /* It seems that this function is only called
1401 for a lamp that is the child of another object */
1402 {
1403         Object *par;
1404         Ipo *ipo;
1405         float *fp1, *fp2;
1406         float slowmat[4][4];
1407         float fac1, fac2;
1408         int a;
1409         
1410         /* NO TIMEOFFS */
1411         
1412         /* no ipo! (because of dloc and realtime-ipos) */
1413         ipo= ob->ipo;
1414         ob->ipo= NULL;
1415
1416         if(ob->parent) {
1417                 par= ob->parent;
1418                 
1419                 solve_parenting(ob, par, slowmat, 1);
1420
1421                 if(ob->partype & PARSLOW) {
1422
1423                         fac1= (float)(1.0/(1.0+ fabs(ob->sf)));
1424                         fac2= 1.0f-fac1;
1425                         fp1= ob->obmat[0];
1426                         fp2= slowmat[0];
1427                         for(a=0; a<16; a++, fp1++, fp2++) {
1428                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1429                         }
1430                 }
1431                 
1432         }
1433         else {
1434                 object_to_mat4(ob, ob->obmat);
1435         }
1436         
1437         if(ob->track) 
1438                 solve_tracking(ob, ob->track->obmat);
1439
1440         solve_constraints(ob, TARGET_OBJECT, NULL, G.scene->r.cfra);
1441         
1442         /*  WATCH IT!!! */
1443         ob->ipo= ipo;
1444         
1445 }
1446 extern void Mat4BlendMat4(float out[][4], float dst[][4], float src[][4], float srcweight);
1447
1448 void solve_constraints (Object *ob, short obtype, void *obdata, float ctime)
1449 {
1450         bConstraint *con;
1451         float tmat[4][4], focusmat[4][4], lastmat[4][4];
1452         int i, clear=1, tot=0;
1453         float   a=0;
1454         float   aquat[4], quat[4];
1455         float   aloc[3], loc[3];
1456         float   asize[3], size[3];
1457         float   oldmat[4][4];
1458         float   smat[3][3], rmat[3][3], mat[3][3];
1459         float enf;
1460
1461         for (con = ob->constraints.first; con; con=con->next){
1462                 /* Clear accumulators if necessary*/
1463                 if (clear){
1464                         clear=0;
1465                         a=0;
1466                         tot=0;
1467                         memset(aquat, 0, sizeof(float)*4);
1468                         memset(aloc, 0, sizeof(float)*3);
1469                         memset(asize, 0, sizeof(float)*3);
1470                 }
1471                 
1472                 /* Check this constraint only if it has some enforcement */
1473                 if (!(con->flag & CONSTRAINT_DISABLE))
1474                 {
1475                         if (con->enforce==0)
1476                                 enf = 0.001f;
1477                                 enf = con->enforce;
1478
1479                         /* Get the targetmat */
1480                         get_constraint_target(con, obtype, obdata, tmat, size, ctime);
1481                         
1482                         Mat4CpyMat4(focusmat, tmat);
1483                         
1484                         /* Extract the components & accumulate */
1485                         Mat4ToQuat(focusmat, quat);
1486                         VECCOPY(loc, focusmat[3]);
1487                         Mat3CpyMat4(mat, focusmat);
1488                         Mat3ToSize(mat, size);
1489                         
1490                         a+=enf;
1491                         tot++;
1492                         
1493                         for(i=0; i<3; i++){
1494                                 aquat[i+1]+=(quat[i+1]) * enf;
1495                                 aloc[i]+=(loc[i]) * enf;
1496                                 asize[i]+=(size[i]-1.0f) * enf;
1497                         }
1498                         aquat[0]+=(quat[0])*enf;
1499                         Mat4CpyMat4(lastmat, focusmat);
1500                 }
1501                 
1502                 /* If the next constraint is not the same type (or there isn't one),
1503                  *      then evaluate the accumulator & request a clear */
1504                 if ((!con->next)||(con->next && con->next->type!=con->type))
1505                 {
1506                         clear=1;
1507                         Mat4CpyMat4(oldmat, ob->obmat);
1508
1509                         /*      If we have several inputs, do a blend of them */
1510                         if (tot){
1511                                 if (tot>1){
1512                                         if (a){
1513                                                 for (i=0; i<3; i++){
1514                                                         asize[i]=1.0f + (asize[i]/(a));
1515                                                         aloc[i]=(aloc[i]/a);
1516                                                 }
1517                                                 
1518                                                 NormalQuat(aquat);
1519                                                 
1520                                                 QuatToMat3(aquat, rmat);
1521                                                 SizeToMat3(asize, smat);
1522                                                 Mat3MulMat3(mat, rmat, smat);
1523                                                 Mat4CpyMat3(focusmat, mat);
1524                                                 VECCOPY(focusmat[3], aloc);
1525
1526                                                 evaluate_constraint(con, ob, obtype, obdata, focusmat);
1527                                         }
1528                                         
1529                                 }       
1530                                 /* If we only have one, blend with the current obmat */
1531                                 else{
1532                                         float solution[4][4];
1533                                         float delta[4][4];
1534                                         float imat[4][4];
1535                                         float identity[4][4];
1536                                         float worldmat[4][4];
1537                                         
1538                                         if (con->type!=CONSTRAINT_TYPE_KINEMATIC){
1539                                                 /* If we're not an IK constraint, solve the constraint then blend it to the previous one */
1540                                                 evaluate_constraint(con, ob, obtype, obdata, lastmat);
1541                                                 
1542                                                 Mat4CpyMat4 (solution, ob->obmat);
1543
1544                                                 /* Interpolate the enforcement */                                       
1545                                                 Mat4Invert (imat, oldmat);
1546                                                 Mat4MulMat4 (delta, solution, imat);
1547                                                 
1548                                                 if (a<1.0) {
1549                                                         Mat4One(identity);
1550                                                         Mat4BlendMat4(delta, identity, delta, a);
1551                                                 }
1552                                                 Mat4MulMat4 (ob->obmat, delta, oldmat);
1553
1554                                         }
1555                                         else{
1556                                                 /* Interpolate the target between the chain's unconstrained endpoint and the effector loc */
1557                                                 if (obtype==TARGET_BONE){
1558                                                         get_objectspace_bone_matrix(obdata, oldmat, 1, 1);
1559                                                         
1560                                                         Mat4MulMat4(worldmat, oldmat, ob->parent->obmat);
1561
1562                                                         Mat4BlendMat4(focusmat, worldmat, lastmat, a);
1563                                                         
1564                                                         evaluate_constraint(con, ob, obtype, obdata, focusmat);
1565                                                 }
1566                                         }
1567                                 }
1568                         }
1569                 }
1570         }       
1571 }
1572
1573 void what_does_parent1(Object *par, int partype, int par1, int par2, int par3)
1574 {
1575
1576         clear_workob();
1577         Mat4One(workob.parentinv);
1578         workob.parent= par;
1579         if(par) 
1580                 workob.track= par->track;       /* WATCH IT: THATS NOT NICE CODE */
1581         workob.partype= partype;
1582         workob.par1= par1;
1583         workob.par2= par2;
1584         workob.par3= par3;
1585
1586         if (par){
1587                 workob.constraints.first = par->constraints.first;
1588                 workob.constraints.last = par->constraints.last;
1589         }
1590
1591         where_is_object(&workob);
1592 }
1593
1594
1595 void what_does_parent(Object *ob)
1596 {
1597
1598         clear_workob();
1599         Mat4One(workob.obmat);
1600         Mat4One(workob.parentinv);
1601         workob.parent= ob->parent;
1602         workob.track= ob->track;
1603
1604         workob.trackflag= ob->trackflag;
1605         workob.upflag= ob->upflag;
1606         
1607         workob.partype= ob->partype;
1608         workob.par1= ob->par1;
1609         workob.par2= ob->par2;
1610         workob.par3= ob->par3;
1611
1612         workob.constraints.first = ob->constraints.first;
1613         workob.constraints.last = ob->constraints.last;
1614
1615         strcpy (workob.parsubstr, ob->parsubstr); 
1616
1617         where_is_object(&workob);
1618 }
1619
1620 BoundBox *unit_boundbox()
1621 {
1622         BoundBox *bb;
1623         
1624         bb= MEM_mallocN(sizeof(BoundBox), "bb");
1625
1626         bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= -1.0;
1627         bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= 1.0;
1628         
1629         bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= -1.0;
1630         bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= 1.0;
1631
1632         bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= -1.0;
1633         bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= 1.0;
1634         
1635         return bb;
1636 }
1637
1638 void minmax_object(Object *ob, float *min, float *max)
1639 {
1640         BoundBox bb;
1641         Mesh *me;
1642         Curve *cu;
1643         float vec[3];
1644         int a;
1645         
1646         switch(ob->type) {
1647                 
1648         case OB_CURVE:
1649         case OB_FONT:
1650         case OB_SURF:
1651                 cu= ob->data;
1652                 
1653                 if(cu->bb==0) tex_space_curve(cu);
1654                 bb= *(cu->bb);
1655                 
1656                 for(a=0; a<8; a++) {
1657                         Mat4MulVecfl(ob->obmat, bb.vec[a]);
1658                         DO_MINMAX(bb.vec[a], min, max);
1659                 }
1660                 break;
1661
1662         case OB_MESH:
1663                 me= get_mesh(ob);
1664                 
1665                 if(me) {
1666                         if(me->bb==0) tex_space_mesh(me);
1667                         bb= *(me->bb);
1668                         
1669                         for(a=0; a<8; a++) {
1670                                 Mat4MulVecfl(ob->obmat, bb.vec[a]);
1671                                 DO_MINMAX(bb.vec[a], min, max);
1672                         }
1673                 }
1674                 if(min[0] < max[0] ) break;
1675                 
1676                 /* else here no break!!!, mesh can be zero sized */
1677                 
1678         default:
1679                 DO_MINMAX(ob->obmat[3], min, max);
1680
1681                 VECCOPY(vec, ob->obmat[3]);
1682                 VecAddf(vec, vec, ob->size);
1683                 DO_MINMAX(vec, min, max);
1684
1685                 VECCOPY(vec, ob->obmat[3]);
1686                 VecSubf(vec, vec, ob->size);
1687                 DO_MINMAX(vec, min, max);
1688                 break;
1689         }
1690 }
1691
1692