6 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
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
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.
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.
25 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26 * All rights reserved.
28 * The Original Code is: all of this file.
30 * Contributor(s): none yet.
32 * ***** END GPL/BL DUAL LICENSE BLOCK *****
44 #include "BLI_winstuff.h"
46 #include "MEM_guardedalloc.h"
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"
70 #include "BKE_armature.h"
71 #include "BKE_action.h"
72 #include "BKE_deform.h"
75 #include "BLI_blenlib.h"
76 #include "BLI_arithb.h"
77 #include "BLI_editVert.h"
79 #include "BKE_utildefines.h"
80 #include "BKE_bad_level_calls.h"
83 #include "BKE_global.h"
85 #include "BKE_object.h"
86 #include "BKE_blender.h"
87 #include "BKE_screen.h"
90 #include "BKE_library.h"
92 #include "BKE_curve.h"
93 #include "BKE_mball.h"
94 #include "BKE_effect.h"
96 #include "BKE_displist.h"
97 #include "BKE_property.h"
99 #include "BKE_group.h"
100 #include "BKE_lattice.h"
101 #include "BKE_constraint.h"
102 #include "BKE_scene.h"
104 #include "BPY_extern.h"
106 /* Local function protos */
107 static void solve_parenting (Object *ob, Object *par, float slowmat[][4], int simul);
109 float originmat[3][3]; /* after where_is_object(), can be used in other functions (bad!) */
112 void clear_workob(void)
114 memset(&workob, 0, sizeof(Object));
116 workob.size[0]= workob.size[1]= workob.size[2]= 1.0;
120 void copy_baseflags()
122 Base *base= G.scene->base.first;
125 base->object->flag= base->flag;
130 void copy_objectflags()
132 Base *base= G.scene->base.first;
135 base->flag= base->object->flag;
140 void update_base_layer(Object *ob)
142 Base *base= G.scene->base.first;
145 if (base->object == ob) base->lay= ob->lay;
150 /* do not free object itself */
151 void free_object(Object *ob)
155 /* disconnect specific data */
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);
167 for(a=0; a<ob->totcol; a++) {
168 if(ob->mat[a]) ob->mat[a]->id.us--;
170 if(ob->mat) MEM_freeN(ob->mat);
172 if(ob->bb) MEM_freeN(ob->bb);
174 if(ob->path) free_path(ob->path);
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);
181 clear_pose(ob->pose);
184 free_effects(&ob->effect);
185 BLI_freelistN(&ob->network);
186 free_properties(&ob->prop);
188 free_sensors(&ob->sensors);
189 free_controllers(&ob->controllers);
190 free_actuators(&ob->actuators);
192 free_constraints(&ob->constraints);
193 free_constraint_channels(&ob->constraintChannels);
194 free_nlastrips(&ob->nlastrips);
196 freedisplist(&ob->disp);
198 BPY_free_scriptlink(&ob->scriptlink);
201 void unlink_object(Object *ob)
213 unlink_controllers(&ob->controllers);
214 unlink_actuators(&ob->actuators);
216 /* check all objects: parents en bevels */
217 obt= G.main->object.first;
220 if(obt->parent==ob) {
222 if(ob->type==OB_LATTICE) freedisplist(&obt->disp);
224 if(obt->track==ob) obt->track= 0;
225 if ELEM(obt->type, OB_CURVE, OB_FONT) {
227 if(cu->bevobj==ob) cu->bevobj= 0;
228 if(cu->textoncurve==ob) cu->textoncurve= 0;
230 if(obt->type==OB_IKA) {
232 Deform *def= ika->def;
234 if(ika->parent==ob) ika->parent= 0;
246 sca_remove_ob_poin(obt, ob);
252 mat= G.main->mat.first;
256 if(mat->mtex[a] && ob==mat->mtex[a]->object) {
257 /* actually, test for lib here... to do */
258 mat->mtex[a]->object= 0;
266 tex= G.main->tex.first;
269 if(tex->env->object == ob) tex->env->object= 0;
275 if(ob->type==OB_MBALL) {
276 obt= find_basis_mball(ob);
277 if(obt) freedisplist(&obt->disp);
281 wrld= G.main->world.first;
283 if(wrld->id.lib==0) {
285 if(wrld->mtex[a] && ob==wrld->mtex[a]->object)
286 wrld->mtex[a]->object =0;
294 sce= G.main->scene.first;
297 if(sce->camera==ob) sce->camera= 0;
304 sc= G.main->screen.first;
306 ScrArea *sa= sc->areabase.first;
310 for (sl= sa->spacedata.first; sl; sl= sl->next) {
311 if(sl->spacetype==SPACE_VIEW3D) {
312 View3D *v3d= (View3D*) sl;
314 if(v3d->camera==ob) {
316 if(v3d->persp>1) v3d->persp= 1;
318 if(v3d->localvd && v3d->localvd->camera==ob ) {
319 v3d->localvd->camera= 0;
320 if(v3d->localvd->persp>1) v3d->localvd->persp= 1;
331 group= G.main->group.first;
333 rem_from_group(group, ob);
334 group= group->id.next;
338 int exist_object(Object *obtest)
342 ob= G.main->object.first;
344 if(ob==obtest) return 1;
354 cam= alloc_libblock(&G.main->camera, ID_CA, "Camera");
358 cam->clipend= 100.0f;
364 Camera *copy_camera(Camera *cam)
368 camn= copy_libblock(cam);
369 id_us_plus((ID *)camn->ipo);
371 BPY_copy_scriptlink(&camn->scriptlink);
378 void make_local_camera(Camera *cam)
384 /* - only lib users: do nothing
385 * - only local users: set flag
389 if(cam->id.lib==0) return;
392 cam->id.flag= LIB_LOCAL;
393 new_id(0, (ID *)cam, 0);
397 ob= G.main->object.first;
400 if(ob->id.lib) lib= 1;
406 if(local && lib==0) {
408 cam->id.flag= LIB_LOCAL;
409 new_id(0, (ID *)cam, 0);
411 else if(local && lib) {
412 camn= copy_camera(cam);
415 ob= G.main->object.first;
436 la= alloc_libblock(&G.main->lamp, ID_LA, "Lamp");
438 la->r= la->g= la->b= la->k= 1.0;
439 la->haint= la->energy= 1.0;
448 la->shadspotsize= 45.0;
452 la->ray_samp= la->ray_sampy= la->ray_sampz= 1;
453 la->area_size=la->area_sizey=la->area_sizez= 1.0;
458 Lamp *copy_lamp(Lamp *la)
463 lan= copy_libblock(la);
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);
473 id_us_plus((ID *)lan->ipo);
475 BPY_copy_scriptlink(&la->scriptlink);
480 void make_local_lamp(Lamp *la)
486 /* - only lib users: do nothing
487 * - only local users: set flag
491 if(la->id.lib==0) return;
494 la->id.flag= LIB_LOCAL;
495 new_id(0, (ID *)la, 0);
499 ob= G.main->object.first;
502 if(ob->id.lib) lib= 1;
508 if(local && lib==0) {
510 la->id.flag= LIB_LOCAL;
511 new_id(0, (ID *)la, 0);
513 else if(local && lib) {
517 ob= G.main->object.first;
532 void free_camera(Camera *ca)
534 BPY_free_scriptlink(&ca->scriptlink);
537 void free_lamp(Lamp *la)
544 BPY_free_scriptlink(&la->scriptlink);
548 if(mtex && mtex->tex) mtex->tex->id.us--;
549 if(mtex) MEM_freeN(mtex);
560 /* *************************************************** */
562 static void *add_obdata_from_type(int 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;
578 printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
583 static char *get_obdata_defname(int 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";
599 printf("get_obdata_defname: Internal error, bad type: %d\n", type);
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)
615 strcpy(name, get_obdata_defname(type));
617 ob= alloc_libblock(&G.main->object, ID_OB, name);
620 /* default object vars */
622 /* ob->transflag= OB_QUAT; */
627 ob->col[0]= ob->col[1]= ob->col[2]= 0.0;
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;
634 Mat4One(ob->parentinv);
637 if(U.flag & USER_MAT_ON_OB) ob->colbits= -1;
639 if(type==OB_CAMERA || type==OB_LAMP) {
640 ob->trackflag= OB_NEGZ;
644 ob->trackflag= OB_POSY;
647 ob->ipoflag = OB_OFFS_OB+OB_OFFS_PARENT;
649 ob->dupon= 1; ob->dupoff= 0;
650 ob->dupsta= 1; ob->dupend= 100;
652 /* Game engine defaults*/
653 ob->mass= ob->inertia= 1.0f;
654 ob->formfactor= 0.4f;
657 ob->anisotropicFriction[0] = 1.0f;
658 ob->anisotropicFriction[1] = 1.0f;
659 ob->anisotropicFriction[2] = 1.0f;
660 ob->gameflag= OB_PROP;
662 ob->data= add_obdata_from_type(type);
664 ob->lay= G.scene->lay;
666 base= scene_add_base(G.scene, ob);
667 scene_select_base(G.scene, base);
672 void base_init_from_view3d(Base *base, View3D *v3d)
674 Object *ob= base->object;
676 if (v3d->localview) {
677 base->lay= ob->lay= v3d->layact + v3d->lay;
678 VECCOPY(ob->loc, v3d->cursor);
680 base->lay= ob->lay= v3d->layact;
681 VECCOPY(ob->loc, G.scene->cursor);
684 v3d->viewquat[0]= -v3d->viewquat[0];
685 if (ob->transflag & OB_QUAT) {
686 QUATCOPY(ob->quat, v3d->viewquat);
688 QuatToEul(v3d->viewquat, ob->rot);
690 v3d->viewquat[0]= -v3d->viewquat[0];
693 Object *copy_object(Object *ob)
697 bConstraintChannel *actcon;
699 obn= copy_libblock(ob);
702 obn->mat= MEM_dupallocN(ob->mat);
705 if(ob->bb) obn->bb= MEM_dupallocN(ob->bb);
707 obn->flag &= ~OB_FROMGROUP;
709 copy_effects(&obn->effect, &ob->effect);
711 obn->network.first= obn->network.last= 0;
713 BPY_copy_scriptlink(&ob->scriptlink);
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);
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);
725 actcon = clone_constraint_channels (&obn->constraintChannels, &ob->constraintChannels, ob->activecon);
726 /* If the active constraint channel was in this list, update it */
728 obn->activecon = actcon;
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]);
736 obn->disp.first= obn->disp.last= 0;
741 void expand_local_object(Object *ob)
745 id_lib_extern((ID *)ob->action);
746 id_lib_extern((ID *)ob->ipo);
747 id_lib_extern((ID *)ob->data);
749 for(a=0; a<ob->totcol; a++) {
750 id_lib_extern((ID *)ob->mat[a]);
754 void make_local_object(Object *ob)
761 /* - only lib users: do nothing
762 * - only local users: set flag
766 if(ob->id.lib==0) return;
769 ob->id.flag= LIB_LOCAL;
770 new_id(0, (ID *)ob, 0);
774 sce= G.main->scene.first;
776 base= sce->base.first;
778 if(base->object==ob) {
779 if(sce->id.lib) lib++;
788 if(local && lib==0) {
790 ob->id.flag= LIB_LOCAL;
791 new_id(0, (ID *)ob, 0);
793 else if(local && lib) {
794 obn= copy_object(ob);
797 sce= G.main->scene.first;
800 base= sce->base.first;
802 if(base->object==ob) {
815 expand_local_object(ob);
818 /* *************** CALC ****************** */
820 /* there is also a timing calculation in drawobject() */
823 int no_speed_curve= 0;
825 void set_mblur_offs(int blur)
827 bluroffs= R.r.blurfac*((float)blur);
828 bluroffs/= (float)R.r.osa;
831 void disable_speed_curve(int val)
836 float bsystem_time(Object *ob, Object *par, float cfra, float ofs)
838 /* returns float ( see frame_to_float in ipo.c) */
840 if(no_speed_curve==0) if(ob && ob->ipo) cfra= calc_ipo_time(ob->ipo, cfra);
843 if(R.flag & R_SEC_FIELD) {
844 if(R.r.mode & R_FIELDSTILL); else cfra+= .5;
851 cfra*= G.scene->r.framelen;
854 if(ob && (ob->ipoflag & OB_OFFS_PARENT)) {
855 if((ob->partype & PARSLOW)==0) cfra-= ob->sf;
863 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
865 float smat[3][3], vec[3];
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);
877 SizeToMat3(ob->size, smat);
881 if(ob->transflag & OB_QUAT) {
883 QuatMul(q1, ob->quat, ob->dquat);
884 QuatToMat3(q1, rmat);
887 QuatToMat3(ob->quat, rmat);
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);
898 EulToMat3(ob->rot, rmat);
901 Mat3MulMat3(mat, rmat, smat);
904 void object_to_mat4(Object *ob, float mat[][4])
908 object_to_mat3(ob, tmat);
910 Mat4CpyMat3(mat, tmat);
912 VECCOPY(mat[3], ob->loc);
914 mat[3][0]+= ob->dloc[0];
915 mat[3][1]+= ob->dloc[1];
916 mat[3][2]+= ob->dloc[2];
920 int enable_cu_speed= 1;
922 void ob_parcurve(Object *ob, Object *par, float mat[][4])
925 float q[4], vec[4], dir[3], *quat, x1, ctime;
930 if(cu->path==0 || cu->path->data==0) calc_curvepath(par);
931 if(cu->path==0) return;
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);
937 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
938 ctime /= cu->pathlen;
939 CLAMP(ctime, 0.0, 1.0);
943 ctime= G.scene->r.cfra - ob->sf;
944 ctime /= cu->pathlen;
946 CLAMP(ctime, 0.0, 1.0);
949 if( where_on_path(par, ctime, vec, dir) ) {
951 if(cu->flag & CU_FOLLOW) {
952 quat= vectoquat(dir, ob->trackflag, ob->upflag);
955 q[0]= (float)cos(0.5*vec[3]);
956 x1= (float)sin(0.5*vec[3]);
960 QuatMul(quat, q, quat);
962 QuatToMat4(quat, mat);
965 VECCOPY(mat[3], vec);
970 void ob_parbone(Object *ob, Object *par, float mat[][4])
976 arm=get_armature(par);
980 /* Make sure the bone is still valid */
981 bone = get_named_bone(arm, ob->parsubstr);
983 printf ("Lost bone %s\n", ob->parsubstr);
987 apply_pose_armature(arm, par->pose, 1); /* Hopefully can set doit parameter in the future */
988 where_is_bone (par, bone);
990 /* Translate by negative bone */
991 get_objectspace_bone_matrix(bone, mat, 0, 1);
995 void ob_parlimb(Object *ob, Object *par, float mat[][4])
1002 /* in local ob space */
1006 li= ika->limbbase.first;
1009 if(cur==ob->par1 || li->next==0) break;
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);
1020 mat[3][0]= li->eff[0];
1021 mat[3][1]= li->eff[1];
1025 void give_parvert(Object *par, int nr, float *vec)
1027 EditMesh *em = G.editMesh;
1030 /* extern ListBase editNurb; already in bad lev calls */
1039 vec[0]=vec[1]=vec[2]= 0.0;
1041 if(par->type==OB_MESH) {
1043 if(nr >= G.totvert) nr= 0;
1046 eve= em->verts.first;
1049 memcpy(vec, eve->co, 12);
1059 if(nr >= me->totvert) nr= 0;
1061 /* is there a deform */
1062 dl= find_displist(&par->disp, DL_VERTS);
1068 MVert *mvert= me->mvert + nr;
1069 VECCOPY(vec, mvert->co);
1074 else if ELEM(par->type, OB_CURVE, OB_SURF) {
1078 if(par==G.obedit) nu= editNurb.first;
1082 if((nu->type & 7)==CU_BEZIER) {
1087 VECCOPY(vec, bezt->vec[1]);
1096 a= nu->pntsu*nu->pntsv;
1099 memcpy(vec, bp->vec, 12);
1110 else if(par->type==OB_IKA) {
1111 Ika *ika= par->data;
1112 Limb *li= ika->limbbase.first;
1116 if(cur==nr || li->next==0) break;
1127 void ob_parvert3(Object *ob, Object *par, float mat[][4])
1129 float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1131 /* in local ob space */
1134 if ELEM3(par->type, OB_MESH, OB_SURF, OB_CURVE) {
1136 give_parvert(par, ob->par1, v1);
1137 give_parvert(par, ob->par2, v2);
1138 give_parvert(par, ob->par3, v3);
1140 triatoquat(v1, v2, v3, q);
1141 QuatToMat3(q, cmat);
1142 Mat4CpyMat3(mat, cmat);
1144 if(ob->type==OB_CURVE) {
1145 VECCOPY(mat[3], v1);
1148 VecAddf(mat[3], v1, v2);
1149 VecAddf(mat[3], mat[3], v3);
1150 VecMulf(mat[3], 0.3333333f);
1155 static int no_parent_ipo=0;
1156 void set_no_parent_ipo(int val)
1161 static float timefac= 1.0; /* 50 Hz, dtime:2 */
1162 void set_dtime(int dtime)
1164 timefac= ((float)(dtime-1))/2.0f;
1167 static int during_script_flag=0;
1168 void disable_where_script(short on)
1170 during_script_flag= on;
1173 int during_script(void) {
1174 return during_script_flag;
1177 void where_is_object_time(Object *ob, float ctime)
1180 float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
1181 float stime, fac1, fac2, vec[3];
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 */
1191 if( ctime != ob->ctime) {
1196 stime= bsystem_time(ob, 0, ctime, 0.0);
1198 calc_ipo(ob->ipo, stime);
1199 execute_ipo((ID *)ob, ob->ipo);
1204 if(ob->type==OB_IKA) {
1206 if(ika->parent) where_is_object_time(ika->parent, ctime);
1212 if(ob->ipoflag & OB_OFFS_PARENT) ctime-= ob->sf;
1215 if(no_parent_ipo==0 && ctime != par->ctime) {
1217 // alleen voor ipo systemen?
1218 pushdata(par, sizeof(Object));
1221 where_is_object_time(par, ctime);
1224 solve_parenting(ob, par, slowmat, 0);
1230 if(ob->partype & PARSLOW) {
1231 // framerate meetellen
1233 fac1= (float)(timefac/(1.0+ fabs(ob->sf)));
1234 if(fac1>=1.0) return;
1239 for(a=0; a<16; a++, fp1++, fp2++) {
1240 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1246 object_to_mat4(ob, ob->obmat);
1249 /* Handle tracking */
1251 if( ctime != ob->track->ctime) where_is_object_time(ob->track, ctime);
1252 solve_tracking (ob, ob->track->obmat);
1256 solve_constraints (ob, TARGET_OBJECT, NULL, ctime);
1258 if(ob->scriptlink.totscript && !during_script()) {
1259 BPY_do_pyscript((ID *)ob, SCRIPT_REDRAW);
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;
1268 static void solve_parenting (Object *ob, Object *par, float slowmat[][4], int simul)
1276 object_to_mat4(ob, obmat);
1278 if(ob->partype & PARSLOW) Mat4CpyMat4(slowmat, ob->obmat);
1281 switch(ob->partype & PARTYPE) {
1284 if(par->type==OB_CURVE) {
1285 if( ((Curve *)par->data)->flag & CU_PATH ) {
1286 ob_parcurve(ob, par, tmat);
1291 if(ok) Mat4MulSerie(totmat, par->obmat, tmat,
1292 NULL, NULL, NULL, NULL, NULL, NULL);
1293 else Mat4CpyMat4(totmat, par->obmat);
1297 ob_parbone(ob, par, tmat);
1298 Mat4MulSerie(totmat, par->obmat, tmat,
1299 NULL, NULL, NULL, NULL, NULL, NULL);
1302 ob_parlimb(ob, par, tmat);
1303 Mat4MulSerie(totmat, par->obmat, tmat,
1304 NULL, NULL, NULL, NULL, NULL, NULL);
1310 VECCOPY(totmat[3], par->obmat[3]);
1313 give_parvert(par, ob->par1, vec);
1314 VecMat4MulVecfl(totmat[3], par->obmat, vec);
1318 ob_parvert3(ob, par, tmat);
1320 Mat4MulSerie(totmat, par->obmat, tmat,
1321 NULL, NULL, NULL, NULL, NULL, NULL);
1329 Mat4CpyMat4(totmat, par->obmat);
1332 Mat4CpyMat4(totmat, par->obmat);
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);
1346 // >>>>>>>>>>>>>>>>>>
1347 // dit is een extern bruikbare originmat
1348 Mat3CpyMat4(originmat, tmat);
1350 // origin, voor hulplijntje
1351 if( (ob->partype & 15)==PARSKEL ) {
1352 VECCOPY(ob->orig, par->obmat[3]);
1355 VECCOPY(ob->orig, totmat[3]);
1360 void solve_tracking (Object *ob, float targetmat[][4])
1367 VecSubf(vec, ob->obmat[3], targetmat[3]);
1368 quat= vectoquat(vec, ob->trackflag, ob->upflag);
1369 QuatToMat3(quat, totmat);
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];
1382 else Mat4CpyMat4(tmat, ob->obmat);
1384 Mat4MulMat34(ob->obmat, totmat, tmat);
1388 void where_is_object(Object *ob)
1391 /* these have been mem copied */
1392 if(ob->flag & OB_FROMDUPLI) return;
1394 where_is_object_time(ob, (float)G.scene->r.cfra);
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 */
1406 float slowmat[4][4];
1412 /* no ipo! (because of dloc and realtime-ipos) */
1419 solve_parenting(ob, par, slowmat, 1);
1421 if(ob->partype & PARSLOW) {
1423 fac1= (float)(1.0/(1.0+ fabs(ob->sf)));
1427 for(a=0; a<16; a++, fp1++, fp2++) {
1428 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1434 object_to_mat4(ob, ob->obmat);
1438 solve_tracking(ob, ob->track->obmat);
1440 solve_constraints(ob, TARGET_OBJECT, NULL, G.scene->r.cfra);
1446 extern void Mat4BlendMat4(float out[][4], float dst[][4], float src[][4], float srcweight);
1448 void solve_constraints (Object *ob, short obtype, void *obdata, float ctime)
1451 float tmat[4][4], focusmat[4][4], lastmat[4][4];
1452 int i, clear=1, tot=0;
1454 float aquat[4], quat[4];
1455 float aloc[3], loc[3];
1456 float asize[3], size[3];
1458 float smat[3][3], rmat[3][3], mat[3][3];
1461 for (con = ob->constraints.first; con; con=con->next){
1462 /* Clear accumulators if necessary*/
1467 memset(aquat, 0, sizeof(float)*4);
1468 memset(aloc, 0, sizeof(float)*3);
1469 memset(asize, 0, sizeof(float)*3);
1472 /* Check this constraint only if it has some enforcement */
1473 if (!(con->flag & CONSTRAINT_DISABLE))
1475 if (con->enforce==0)
1479 /* Get the targetmat */
1480 get_constraint_target(con, obtype, obdata, tmat, size, ctime);
1482 Mat4CpyMat4(focusmat, tmat);
1484 /* Extract the components & accumulate */
1485 Mat4ToQuat(focusmat, quat);
1486 VECCOPY(loc, focusmat[3]);
1487 Mat3CpyMat4(mat, focusmat);
1488 Mat3ToSize(mat, size);
1494 aquat[i+1]+=(quat[i+1]) * enf;
1495 aloc[i]+=(loc[i]) * enf;
1496 asize[i]+=(size[i]-1.0f) * enf;
1498 aquat[0]+=(quat[0])*enf;
1499 Mat4CpyMat4(lastmat, focusmat);
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))
1507 Mat4CpyMat4(oldmat, ob->obmat);
1509 /* If we have several inputs, do a blend of them */
1513 for (i=0; i<3; i++){
1514 asize[i]=1.0f + (asize[i]/(a));
1515 aloc[i]=(aloc[i]/a);
1520 QuatToMat3(aquat, rmat);
1521 SizeToMat3(asize, smat);
1522 Mat3MulMat3(mat, rmat, smat);
1523 Mat4CpyMat3(focusmat, mat);
1524 VECCOPY(focusmat[3], aloc);
1526 evaluate_constraint(con, ob, obtype, obdata, focusmat);
1530 /* If we only have one, blend with the current obmat */
1532 float solution[4][4];
1535 float identity[4][4];
1536 float worldmat[4][4];
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);
1542 Mat4CpyMat4 (solution, ob->obmat);
1544 /* Interpolate the enforcement */
1545 Mat4Invert (imat, oldmat);
1546 Mat4MulMat4 (delta, solution, imat);
1550 Mat4BlendMat4(delta, identity, delta, a);
1552 Mat4MulMat4 (ob->obmat, delta, oldmat);
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);
1560 Mat4MulMat4(worldmat, oldmat, ob->parent->obmat);
1562 Mat4BlendMat4(focusmat, worldmat, lastmat, a);
1564 evaluate_constraint(con, ob, obtype, obdata, focusmat);
1573 void what_does_parent1(Object *par, int partype, int par1, int par2, int par3)
1577 Mat4One(workob.parentinv);
1580 workob.track= par->track; /* WATCH IT: THATS NOT NICE CODE */
1581 workob.partype= partype;
1587 workob.constraints.first = par->constraints.first;
1588 workob.constraints.last = par->constraints.last;
1591 where_is_object(&workob);
1595 void what_does_parent(Object *ob)
1599 Mat4One(workob.obmat);
1600 Mat4One(workob.parentinv);
1601 workob.parent= ob->parent;
1602 workob.track= ob->track;
1604 workob.trackflag= ob->trackflag;
1605 workob.upflag= ob->upflag;
1607 workob.partype= ob->partype;
1608 workob.par1= ob->par1;
1609 workob.par2= ob->par2;
1610 workob.par3= ob->par3;
1612 workob.constraints.first = ob->constraints.first;
1613 workob.constraints.last = ob->constraints.last;
1615 strcpy (workob.parsubstr, ob->parsubstr);
1617 where_is_object(&workob);
1620 BoundBox *unit_boundbox()
1624 bb= MEM_mallocN(sizeof(BoundBox), "bb");
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;
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;
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;
1638 void minmax_object(Object *ob, float *min, float *max)
1653 if(cu->bb==0) tex_space_curve(cu);
1656 for(a=0; a<8; a++) {
1657 Mat4MulVecfl(ob->obmat, bb.vec[a]);
1658 DO_MINMAX(bb.vec[a], min, max);
1666 if(me->bb==0) tex_space_mesh(me);
1669 for(a=0; a<8; a++) {
1670 Mat4MulVecfl(ob->obmat, bb.vec[a]);
1671 DO_MINMAX(bb.vec[a], min, max);
1674 if(min[0] < max[0] ) break;
1676 /* else here no break!!!, mesh can be zero sized */
1679 DO_MINMAX(ob->obmat[3], min, max);
1681 VECCOPY(vec, ob->obmat[3]);
1682 VecAddf(vec, vec, ob->size);
1683 DO_MINMAX(vec, min, max);
1685 VECCOPY(vec, ob->obmat[3]);
1686 VecSubf(vec, vec, ob->size);
1687 DO_MINMAX(vec, min, max);