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