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 *****
43 #include "MEM_guardedalloc.h"
45 #include "DNA_action_types.h"
46 #include "DNA_armature_types.h"
47 #include "DNA_camera_types.h"
48 #include "DNA_constraint_types.h"
49 #include "DNA_curve_types.h"
50 #include "DNA_group_types.h"
51 #include "DNA_ipo_types.h"
52 #include "DNA_lamp_types.h"
53 #include "DNA_lattice_types.h"
54 #include "DNA_material_types.h"
55 #include "DNA_mesh_types.h"
56 #include "DNA_meshdata_types.h"
57 #include "DNA_modifier_types.h"
58 #include "DNA_nla_types.h"
59 #include "DNA_object_types.h"
60 #include "DNA_object_force.h"
61 #include "DNA_object_fluidsim.h"
62 #include "DNA_oops_types.h"
63 #include "DNA_scene_types.h"
64 #include "DNA_screen_types.h"
65 #include "DNA_space_types.h"
66 #include "DNA_texture_types.h"
67 #include "DNA_userdef_types.h"
68 #include "DNA_view3d_types.h"
69 #include "DNA_world_types.h"
71 #include "BKE_armature.h"
72 #include "BKE_action.h"
73 #include "BKE_deform.h"
74 #include "BKE_DerivedMesh.h"
77 #include "BLI_blenlib.h"
78 #include "BLI_arithb.h"
79 #include "BLI_editVert.h"
81 #include "BKE_utildefines.h"
82 #include "BKE_bad_level_calls.h"
85 #include "BKE_global.h"
88 #include "BKE_blender.h"
89 #include "BKE_constraint.h"
90 #include "BKE_curve.h"
91 #include "BKE_displist.h"
92 #include "BKE_effect.h"
93 #include "BKE_group.h"
94 #include "BKE_icons.h"
97 #include "BKE_lattice.h"
98 #include "BKE_library.h"
100 #include "BKE_mball.h"
101 #include "BKE_modifier.h"
102 #include "BKE_object.h"
103 #include "BKE_property.h"
105 #include "BKE_scene.h"
106 #include "BKE_screen.h"
107 #include "BKE_softbody.h"
109 #include "LBM_fluidsim.h"
111 #include "BPY_extern.h"
113 /* Local function protos */
114 static void solve_parenting (Object *ob, Object *par, float slowmat[][4], int simul);
116 float originmat[3][3]; /* after where_is_object(), can be used in other functions (bad!) */
119 void clear_workob(void)
121 memset(&workob, 0, sizeof(Object));
123 workob.size[0]= workob.size[1]= workob.size[2]= 1.0;
127 void copy_baseflags()
129 Base *base= G.scene->base.first;
132 base->object->flag= base->flag;
137 void copy_objectflags()
139 Base *base= G.scene->base.first;
142 base->flag= base->object->flag;
147 void update_base_layer(Object *ob)
149 Base *base= G.scene->base.first;
152 if (base->object == ob) base->lay= ob->lay;
157 void object_free_modifiers(Object *ob)
159 while (ob->modifiers.first) {
160 ModifierData *md = ob->modifiers.first;
162 BLI_remlink(&ob->modifiers, md);
168 /* here we will collect all local displist stuff */
169 /* also (ab)used in depsgraph */
170 void object_free_display(Object *ob)
172 if(ob->derivedDeform) {
173 ob->derivedDeform->release(ob->derivedDeform);
174 ob->derivedDeform= NULL;
176 if(ob->derivedFinal) {
177 ob->derivedFinal->release(ob->derivedFinal);
178 ob->derivedFinal= NULL;
181 freedisplist(&ob->disp);
184 /* do not free object itself */
185 void free_object(Object *ob)
189 object_free_display(ob);
191 /* disconnect specific data */
196 if(ob->type==OB_MESH) unlink_mesh(ob->data);
197 else if(ob->type==OB_CURVE) unlink_curve(ob->data);
198 else if(ob->type==OB_MBALL) unlink_mball(ob->data);
203 for(a=0; a<ob->totcol; a++) {
204 if(ob->mat[a]) ob->mat[a]->id.us--;
206 if(ob->mat) MEM_freeN(ob->mat);
208 if(ob->bb) MEM_freeN(ob->bb);
210 if(ob->path) free_path(ob->path);
212 if(ob->ipo) ob->ipo->id.us--;
213 if(ob->action) ob->action->id.us--;
214 if(ob->dup_group) ob->dup_group->id.us--;
215 if(ob->defbase.first)
216 BLI_freelistN(&ob->defbase);
218 free_pose_channels(ob->pose);
221 free_effects(&ob->effect);
222 BLI_freelistN(&ob->network);
223 free_properties(&ob->prop);
224 object_free_modifiers(ob);
226 free_sensors(&ob->sensors);
227 free_controllers(&ob->controllers);
228 free_actuators(&ob->actuators);
230 free_constraints(&ob->constraints);
231 free_constraint_channels(&ob->constraintChannels);
232 free_nlastrips(&ob->nlastrips);
234 BPY_free_scriptlink(&ob->scriptlink);
236 if(ob->pd) MEM_freeN(ob->pd);
237 if(ob->soft) sbFree(ob->soft);
238 if(ob->fluidsimSettings) fluidsimSettingsFree(ob->fluidsimSettings);
241 static void unlink_object__unlinkModifierLinks(void *userData, Object *ob, Object **obpoin)
243 Object *unlinkOb = userData;
245 if (*obpoin==unlinkOb) {
247 ob->recalc |= OB_RECALC;
250 void unlink_object(Object *ob)
266 unlink_controllers(&ob->controllers);
267 unlink_actuators(&ob->actuators);
269 /* check all objects: parents en bevels and fields */
270 obt= G.main->object.first;
272 if(obt->id.lib==NULL) {
274 if(obt->parent==ob) {
276 obt->recalc |= OB_RECALC;
281 obt->recalc |= OB_RECALC_OB;
284 modifiers_foreachObjectLink(obt, unlink_object__unlinkModifierLinks, ob);
286 if ELEM(obt->type, OB_CURVE, OB_FONT) {
290 obt->recalc |= OB_RECALC;
292 if(cu->taperobj==ob) {
294 obt->recalc |= OB_RECALC;
296 if(cu->textoncurve==ob) {
297 cu->textoncurve= NULL;
298 obt->recalc |= OB_RECALC;
301 else if(obt->type==OB_ARMATURE && obt->pose) {
303 for(pchan= obt->pose->chanbase.first; pchan; pchan= pchan->next) {
304 for (con = pchan->constraints.first; con; con=con->next) {
305 if(ob==get_constraint_target(con, &str)) {
306 set_constraint_target(con, NULL, NULL);
307 obt->recalc |= OB_RECALC_DATA;
310 if(pchan->custom==ob)
315 sca_remove_ob_poin(obt, ob);
317 for (con = obt->constraints.first; con; con=con->next) {
318 if(ob==get_constraint_target(con, &str)) {
319 set_constraint_target(con, NULL, NULL);
320 obt->recalc |= OB_RECALC_OB;
324 /* object is deflector or field */
326 if(give_parteff(obt))
327 obt->recalc |= OB_RECALC_DATA;
329 obt->recalc |= OB_RECALC_DATA;
333 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
334 if(strip->object==ob)
342 mat= G.main->mat.first;
345 for(a=0; a<MAX_MTEX; a++) {
346 if(mat->mtex[a] && ob==mat->mtex[a]->object) {
347 /* actually, test for lib here... to do */
348 mat->mtex[a]->object= NULL;
356 tex= G.main->tex.first;
359 if(tex->env->object == ob) tex->env->object= NULL;
365 if(ob->type==OB_MBALL) {
366 obt= find_basis_mball(ob);
367 if(obt) freedisplist(&obt->disp);
371 wrld= G.main->world.first;
373 if(wrld->id.lib==NULL) {
374 for(a=0; a<MAX_MTEX; a++) {
375 if(wrld->mtex[a] && ob==wrld->mtex[a]->object)
376 wrld->mtex[a]->object= NULL;
384 sce= G.main->scene.first;
386 if(sce->id.lib==NULL) {
387 if(sce->camera==ob) sce->camera= NULL;
392 ipo= G.main->ipo.first;
394 if(ipo->id.lib==NULL) {
396 for(icu= ipo->curve.first; icu; icu= icu->next) {
397 if(icu->driver && icu->driver->ob==ob)
398 icu->driver->ob= NULL;
405 sc= G.main->screen.first;
407 ScrArea *sa= sc->areabase.first;
411 for (sl= sa->spacedata.first; sl; sl= sl->next) {
412 if(sl->spacetype==SPACE_VIEW3D) {
413 View3D *v3d= (View3D*) sl;
415 if(v3d->camera==ob) {
417 if(v3d->persp>1) v3d->persp= 1;
419 if(v3d->localvd && v3d->localvd->camera==ob ) {
420 v3d->localvd->camera= NULL;
421 if(v3d->localvd->persp>1) v3d->localvd->persp= 1;
424 else if(sl->spacetype==SPACE_IPO) {
425 SpaceIpo *sipo= (SpaceIpo *)sl;
426 if(sipo->from == (ID *)ob) sipo->from= NULL;
428 else if(sl->spacetype==SPACE_OOPS) {
429 SpaceOops *so= (SpaceOops *)sl;
432 oops= so->oops.first;
434 if(oops->id==(ID *)ob) oops->id= NULL;
438 TreeStoreElem *tselem= so->treestore->data;
440 for(a=0; a<so->treestore->usedelem; a++, tselem++) {
441 if(tselem->id==(ID *)ob) tselem->id= NULL;
454 group= G.main->group.first;
456 rem_from_group(group, ob);
457 group= group->id.next;
461 int exist_object(Object *obtest)
465 if(obtest==NULL) return 0;
467 ob= G.main->object.first;
469 if(ob==obtest) return 1;
479 cam= alloc_libblock(&G.main->camera, ID_CA, "Camera");
483 cam->clipend= 100.0f;
485 cam->ortho_scale= 6.0;
486 cam->flag |= CAM_SHOWTITLESAFE;
487 cam->passepartalpha = 0.2f;
492 Camera *copy_camera(Camera *cam)
496 camn= copy_libblock(cam);
497 id_us_plus((ID *)camn->ipo);
499 BPY_copy_scriptlink(&camn->scriptlink);
506 void make_local_camera(Camera *cam)
512 /* - only lib users: do nothing
513 * - only local users: set flag
517 if(cam->id.lib==0) return;
520 cam->id.flag= LIB_LOCAL;
521 new_id(0, (ID *)cam, 0);
525 ob= G.main->object.first;
528 if(ob->id.lib) lib= 1;
534 if(local && lib==0) {
536 cam->id.flag= LIB_LOCAL;
537 new_id(0, (ID *)cam, 0);
539 else if(local && lib) {
540 camn= copy_camera(cam);
543 ob= G.main->object.first;
564 la= alloc_libblock(&G.main->lamp, ID_LA, "Lamp");
566 la->r= la->g= la->b= la->k= 1.0;
567 la->haint= la->energy= 1.0;
576 la->shadspotsize= 45.0;
580 la->ray_samp= la->ray_sampy= la->ray_sampz= 1;
581 la->area_size=la->area_sizey=la->area_sizez= 1.0;
587 Lamp *copy_lamp(Lamp *la)
592 lan= copy_libblock(la);
594 for(a=0; a<MAX_MTEX; a++) {
596 lan->mtex[a]= MEM_mallocN(sizeof(MTex), "copylamptex");
597 memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
598 id_us_plus((ID *)lan->mtex[a]->tex);
602 id_us_plus((ID *)lan->ipo);
604 BPY_copy_scriptlink(&la->scriptlink);
609 void make_local_lamp(Lamp *la)
615 /* - only lib users: do nothing
616 * - only local users: set flag
620 if(la->id.lib==0) return;
623 la->id.flag= LIB_LOCAL;
624 new_id(0, (ID *)la, 0);
628 ob= G.main->object.first;
631 if(ob->id.lib) lib= 1;
637 if(local && lib==0) {
639 la->id.flag= LIB_LOCAL;
640 new_id(0, (ID *)la, 0);
642 else if(local && lib) {
646 ob= G.main->object.first;
661 void free_camera(Camera *ca)
663 BPY_free_scriptlink(&ca->scriptlink);
666 void free_lamp(Lamp *la)
673 BPY_free_scriptlink(&la->scriptlink);
675 for(a=0; a<MAX_MTEX; a++) {
677 if(mtex && mtex->tex) mtex->tex->id.us--;
678 if(mtex) MEM_freeN(mtex);
682 BKE_icon_delete(&la->id);
692 /* *************************************************** */
694 static void *add_obdata_from_type(int type)
697 case OB_MESH: G.totmesh++; return add_mesh();
698 case OB_CURVE: G.totcurve++; return add_curve(OB_CURVE);
699 case OB_SURF: G.totcurve++; return add_curve(OB_SURF);
700 case OB_FONT: return add_curve(OB_FONT);
701 case OB_MBALL: return add_mball();
702 case OB_CAMERA: return add_camera();
703 case OB_LAMP: G.totlamp++; return add_lamp();
704 case OB_LATTICE: return add_lattice();
705 case OB_WAVE: return add_wave();
706 case OB_ARMATURE: return add_armature();
707 case OB_EMPTY: return NULL;
709 printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
714 static char *get_obdata_defname(int type)
717 case OB_MESH: return "Mesh";
718 case OB_CURVE: return "Curve";
719 case OB_SURF: return "Surf";
720 case OB_FONT: return "Font";
721 case OB_MBALL: return "Mball";
722 case OB_CAMERA: return "Camera";
723 case OB_LAMP: return "Lamp";
724 case OB_LATTICE: return "Lattice";
725 case OB_WAVE: return "Wave";
726 case OB_ARMATURE: return "Armature";
727 case OB_EMPTY: return "Empty";
729 printf("get_obdata_defname: Internal error, bad type: %d\n", type);
734 /* general add: to G.scene, with layer from area and default name */
735 /* creates minimum required data, but without vertices etc. */
736 Object *add_object(int type)
742 strcpy(name, get_obdata_defname(type));
744 ob= alloc_libblock(&G.main->object, ID_OB, name);
747 /* default object vars */
749 /* ob->transflag= OB_QUAT; */
754 ob->col[0]= ob->col[1]= ob->col[2]= 0.0;
757 ob->loc[0]= ob->loc[1]= ob->loc[2]= 0.0;
758 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
759 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
761 Mat4One(ob->parentinv);
764 if(U.flag & USER_MAT_ON_OB) ob->colbits= -1;
765 ob->empty_drawtype= OB_ARROWS;
766 ob->empty_drawsize= 1.0;
768 if(type==OB_CAMERA || type==OB_LAMP) {
769 ob->trackflag= OB_NEGZ;
773 ob->trackflag= OB_POSY;
776 ob->ipoflag = OB_OFFS_OB+OB_OFFS_PARENT;
777 ob->ipowin= ID_OB; /* the ipowin shown */
778 ob->dupon= 1; ob->dupoff= 0;
779 ob->dupsta= 1; ob->dupend= 100;
781 /* Game engine defaults*/
782 ob->mass= ob->inertia= 1.0f;
783 ob->formfactor= 0.4f;
786 ob->anisotropicFriction[0] = 1.0f;
787 ob->anisotropicFriction[1] = 1.0f;
788 ob->anisotropicFriction[2] = 1.0f;
789 ob->gameflag= OB_PROP;
791 /* NT fluid sim defaults */
792 ob->fluidsimFlag = 0;
793 ob->fluidsimSettings = NULL;
795 ob->data= add_obdata_from_type(type);
797 ob->lay= G.scene->lay;
799 base= scene_add_base(G.scene, ob);
800 scene_select_base(G.scene, base);
801 ob->recalc |= OB_RECALC;
806 void base_init_from_view3d(Base *base, View3D *v3d)
808 Object *ob= base->object;
810 if (v3d->localview) {
811 base->lay= ob->lay= v3d->layact + v3d->lay;
812 VECCOPY(ob->loc, v3d->cursor);
814 base->lay= ob->lay= v3d->layact;
815 VECCOPY(ob->loc, G.scene->cursor);
818 v3d->viewquat[0]= -v3d->viewquat[0];
819 if (ob->transflag & OB_QUAT) {
820 QUATCOPY(ob->quat, v3d->viewquat);
822 QuatToEul(v3d->viewquat, ob->rot);
824 v3d->viewquat[0]= -v3d->viewquat[0];
827 SoftBody *copy_softbody(SoftBody *sb)
831 if (sb==NULL) return(NULL);
833 sbn= MEM_dupallocN(sb);
834 sbn->totspring= sbn->totpoint= 0;
840 sbn->totkey= sbn->totpointkey= 0;
845 Object *copy_object(Object *ob)
851 obn= copy_libblock(ob);
854 obn->mat= MEM_dupallocN(ob->mat);
857 if(ob->bb) obn->bb= MEM_dupallocN(ob->bb);
859 obn->flag &= ~OB_FROMGROUP;
861 copy_effects(&obn->effect, &ob->effect);
862 obn->modifiers.first = obn->modifiers.last= NULL;
864 for (md=ob->modifiers.first; md; md=md->next) {
865 ModifierData *nmd = modifier_new(md->type);
866 modifier_copyData(md, nmd);
867 BLI_addtail(&obn->modifiers, nmd);
870 obn->network.first= obn->network.last= 0;
872 BPY_copy_scriptlink(&ob->scriptlink);
874 copy_properties(&obn->prop, &ob->prop);
875 copy_sensors(&obn->sensors, &ob->sensors);
876 copy_controllers(&obn->controllers, &ob->controllers);
877 copy_actuators(&obn->actuators, &ob->actuators);
880 copy_pose(&obn->pose, ob->pose, 1);
881 armature_rebuild_pose(obn, obn->data);
883 copy_defgroups(&obn->defbase, &ob->defbase);
884 copy_nlastrips(&obn->nlastrips, &ob->nlastrips);
885 copy_constraints (&obn->constraints, &ob->constraints);
887 clone_constraint_channels (&obn->constraintChannels, &ob->constraintChannels);
889 /* increase user numbers */
890 id_us_plus((ID *)obn->data);
891 id_us_plus((ID *)obn->ipo);
892 id_us_plus((ID *)obn->action);
893 id_us_plus((ID *)obn->dup_group);
895 for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
897 obn->disp.first= obn->disp.last= NULL;
899 if(ob->pd) obn->pd= MEM_dupallocN(ob->pd);
900 obn->soft= copy_softbody(ob->soft);
902 /* NT copy fluid sim setting memory */
903 if(obn->fluidsimSettings) obn->fluidsimSettings = MEM_dupallocN(ob->fluidsimSettings);
904 else obn->fluidsimSettings = NULL;
906 obn->derivedDeform = NULL;
907 obn->derivedFinal = NULL;
912 void expand_local_object(Object *ob)
917 id_lib_extern((ID *)ob->action);
918 id_lib_extern((ID *)ob->ipo);
919 id_lib_extern((ID *)ob->data);
921 for(a=0; a<ob->totcol; a++) {
922 id_lib_extern((ID *)ob->mat[a]);
924 for (strip=ob->nlastrips.first; strip; strip=strip->next) {
925 id_lib_extern((ID *)strip->act);
930 void make_local_object(Object *ob)
937 /* - only lib users: do nothing
938 * - only local users: set flag
942 if(ob->id.lib==0) return;
945 ob->id.flag= LIB_LOCAL;
946 new_id(0, (ID *)ob, 0);
950 sce= G.main->scene.first;
952 base= sce->base.first;
954 if(base->object==ob) {
955 if(sce->id.lib) lib++;
964 if(local && lib==0) {
966 ob->id.flag= LIB_LOCAL;
967 new_id(0, (ID *)ob, 0);
969 else if(local && lib) {
970 obn= copy_object(ob);
973 sce= G.main->scene.first;
976 base= sce->base.first;
978 if(base->object==ob) {
991 expand_local_object(ob);
994 /* *************** CALC ****************** */
996 /* there is also a timing calculation in drawobject() */
999 int no_speed_curve= 0;
1001 /* ugly call from render */
1002 void set_mblur_offs(float blur)
1007 void disable_speed_curve(int val)
1009 no_speed_curve= val;
1012 /* ob can be NULL */
1013 float bsystem_time(Object *ob, Object *par, float cfra, float ofs)
1015 /* returns float ( see frame_to_float in ipo.c) */
1018 // if(R.flag & R_SEC_FIELD) {
1019 // if(R.r.mode & R_FIELDSTILL); else cfra+= .5;
1025 cfra*= G.scene->r.framelen;
1027 if(no_speed_curve==0) if(ob && ob->ipo) cfra= calc_ipo_time(ob->ipo, cfra);
1030 if(ob && (ob->ipoflag & OB_OFFS_PARENT)) {
1031 if((ob->partype & PARSLOW)==0) cfra-= ob->sf;
1039 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
1041 float smat[3][3], vec[3];
1047 vec[0]= ob->size[0]+ob->dsize[0];
1048 vec[1]= ob->size[1]+ob->dsize[1];
1049 vec[2]= ob->size[2]+ob->dsize[2];
1050 SizeToMat3(vec, smat);
1053 SizeToMat3(ob->size, smat);
1057 if(ob->transflag & OB_QUAT) {
1059 QuatMul(q1, ob->quat, ob->dquat);
1060 QuatToMat3(q1, rmat);
1063 QuatToMat3(ob->quat, rmat);
1068 vec[0]= ob->rot[0]+ob->drot[0];
1069 vec[1]= ob->rot[1]+ob->drot[1];
1070 vec[2]= ob->rot[2]+ob->drot[2];
1071 EulToMat3(vec, rmat);
1074 EulToMat3(ob->rot, rmat);
1077 Mat3MulMat3(mat, rmat, smat);
1080 void object_to_mat4(Object *ob, float mat[][4])
1084 object_to_mat3(ob, tmat);
1086 Mat4CpyMat3(mat, tmat);
1088 VECCOPY(mat[3], ob->loc);
1090 mat[3][0]+= ob->dloc[0];
1091 mat[3][1]+= ob->dloc[1];
1092 mat[3][2]+= ob->dloc[2];
1096 int enable_cu_speed= 1;
1098 static void ob_parcurve(Object *ob, Object *par, float mat[][4])
1101 float q[4], vec[4], dir[3], *quat, x1, ctime;
1102 float timeoffs= 0.0;
1107 if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1108 makeDispListCurveTypes(par, 0);
1109 if(cu->path==NULL) return;
1111 /* exception, timeoffset is regarded as distance offset */
1112 if(cu->flag & CU_OFFS_PATHDIST) {
1113 SWAP(float, timeoffs, ob->sf);
1116 /* catch exceptions: feature for nla stride editing */
1117 if(ob->ipoflag & OB_DISABLE_PATH) {
1120 /* catch exceptions: curve paths used as a duplicator */
1121 else if(enable_cu_speed) {
1122 ctime= bsystem_time(ob, par, (float)G.scene->r.cfra, 0.0);
1124 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
1125 ctime /= cu->pathlen;
1126 CLAMP(ctime, 0.0, 1.0);
1130 ctime= G.scene->r.cfra - ob->sf;
1131 ctime /= cu->pathlen;
1133 CLAMP(ctime, 0.0, 1.0);
1136 /* time calculus is correct, now apply distance offset */
1137 if(cu->flag & CU_OFFS_PATHDIST) {
1138 ctime += timeoffs/cu->path->totdist;
1141 SWAP(float, timeoffs, ob->sf);
1146 if( where_on_path(par, ctime, vec, dir) ) {
1148 if(cu->flag & CU_FOLLOW) {
1149 quat= vectoquat(dir, ob->trackflag, ob->upflag);
1153 q[0]= (float)cos(0.5*vec[3]);
1154 x1= (float)sin(0.5*vec[3]);
1158 QuatMul(quat, q, quat);
1160 QuatToMat4(quat, mat);
1163 VECCOPY(mat[3], vec);
1168 static void ob_parbone(Object *ob, Object *par, float mat[][4])
1170 bPoseChannel *pchan;
1174 arm=get_armature(par);
1180 /* Make sure the bone is still valid */
1181 pchan= get_pose_channel(par->pose, ob->parsubstr);
1183 printf ("Bone parent: no bone %s\n", ob->parsubstr);
1188 /* get bone transform */
1189 Mat4CpyMat4(mat, pchan->pose_mat);
1191 /* but for backwards compatibility, the child has to move to the tail */
1192 VECCOPY(vec, mat[1]);
1193 VecMulf(vec, pchan->bone->length);
1194 VecAddf(mat[3], mat[3], vec);
1197 static void give_parvert(Object *par, int nr, float *vec)
1199 EditMesh *em = G.editMesh;
1201 /* extern ListBase editNurb; already in bad lev calls */
1208 vec[0]=vec[1]=vec[2]= 0.0;
1210 if(par->type==OB_MESH) {
1211 if(G.obedit && (par->data==G.obedit->data)) {
1212 if(nr >= G.totvert) nr= 0;
1215 eve= em->verts.first;
1218 memcpy(vec, eve->co, 12);
1226 /* maybe this is against the derivedmesh philosphy, but where_is_object is called
1227 by code that is called by build_mesh... (when ob->sf!=0.0) so it can cycle eternally */
1228 DerivedMesh *dm = par->derivedDeform; //mesh_get_derived_deform(par, &needsFree);
1231 if(nr >= dm->getNumVerts(dm)) nr= 0;
1233 dm->getVertCo(dm, nr, vec);
1237 else if ELEM(par->type, OB_CURVE, OB_SURF) {
1241 if(par==G.obedit) nu= editNurb.first;
1245 if((nu->type & 7)==CU_BEZIER) {
1250 VECCOPY(vec, bezt->vec[1]);
1259 a= nu->pntsu*nu->pntsv;
1262 memcpy(vec, bp->vec, 12);
1276 static void ob_parvert3(Object *ob, Object *par, float mat[][4])
1278 float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1280 /* in local ob space */
1283 if ELEM3(par->type, OB_MESH, OB_SURF, OB_CURVE) {
1285 give_parvert(par, ob->par1, v1);
1286 give_parvert(par, ob->par2, v2);
1287 give_parvert(par, ob->par3, v3);
1289 triatoquat(v1, v2, v3, q);
1290 QuatToMat3(q, cmat);
1291 Mat4CpyMat3(mat, cmat);
1293 if(ob->type==OB_CURVE) {
1294 VECCOPY(mat[3], v1);
1297 VecAddf(mat[3], v1, v2);
1298 VecAddf(mat[3], mat[3], v3);
1299 VecMulf(mat[3], 0.3333333f);
1304 static int no_parent_ipo=0;
1305 void set_no_parent_ipo(int val)
1310 static int during_script_flag=0;
1311 void disable_where_script(short on)
1313 during_script_flag= on;
1316 int during_script(void) {
1317 return during_script_flag;
1320 static int during_scriptlink_flag=0;
1321 void disable_where_scriptlink(short on)
1323 during_scriptlink_flag= on;
1326 int during_scriptlink(void) {
1327 return during_scriptlink_flag;
1330 void where_is_object_time(Object *ob, float ctime)
1333 float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
1334 float stime, fac1, fac2, vec[3];
1338 /* new version: correct parent+vertexparent and track+parent */
1339 /* this one only calculates direct attached parent and track */
1340 /* is faster, but should keep track of timeoffs */
1342 if(ob==NULL) return;
1344 /* this is needed to be able to grab objects with ipos, otherwise it always freezes them */
1345 stime= bsystem_time(ob, 0, ctime, 0.0);
1346 if(stime != ob->ctime) {
1350 calc_ipo(ob->ipo, stime);
1351 execute_ipo((ID *)ob, ob->ipo);
1354 do_all_object_actions(ob);
1356 /* do constraint ipos ... */
1357 do_constraint_channels(&ob->constraints, &ob->constraintChannels, ctime);
1360 /* but, the drivers have to be done */
1361 if(ob->ipo) do_ob_ipodrivers(ob, ob->ipo);
1367 if(ob->ipoflag & OB_OFFS_PARENT) ctime-= ob->sf;
1369 /* hurms, code below conflicts with depgraph... (ton) */
1371 if(no_parent_ipo==0 && ctime != par->ctime) {
1373 // only for ipo systems?
1374 pushdata(par, sizeof(Object));
1377 where_is_object_time(par, ctime);
1380 solve_parenting(ob, par, slowmat, 0);
1386 if(ob->partype & PARSLOW) {
1387 // include framerate
1389 fac1= (1.0f/(1.0f+ fabs(ob->sf)));
1390 if(fac1>=1.0) return;
1395 for(a=0; a<16; a++, fp1++, fp2++) {
1396 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1402 object_to_mat4(ob, ob->obmat);
1405 /* Handle tracking */
1407 if( ctime != ob->track->ctime) where_is_object_time(ob->track, ctime);
1408 solve_tracking (ob, ob->track->obmat);
1412 solve_constraints (ob, TARGET_OBJECT, NULL, ctime);
1414 if(ob->scriptlink.totscript && !during_script()) {
1415 if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_REDRAW);
1418 /* set negative scale flag in object */
1419 Crossf(vec, ob->obmat[0], ob->obmat[1]);
1420 if( Inpf(vec, ob->obmat[2]) < 0.0 ) ob->transflag |= OB_NEG_SCALE;
1421 else ob->transflag &= ~OB_NEG_SCALE;
1424 static void solve_parenting (Object *ob, Object *par, float slowmat[][4], int simul)
1432 object_to_mat4(ob, obmat);
1434 if(ob->partype & PARSLOW) Mat4CpyMat4(slowmat, ob->obmat);
1437 switch(ob->partype & PARTYPE) {
1440 if(par->type==OB_CURVE) {
1441 if( ((Curve *)par->data)->flag & CU_PATH ) {
1442 ob_parcurve(ob, par, tmat);
1447 if(ok) Mat4MulSerie(totmat, par->obmat, tmat,
1448 NULL, NULL, NULL, NULL, NULL, NULL);
1449 else Mat4CpyMat4(totmat, par->obmat);
1453 ob_parbone(ob, par, tmat);
1454 Mat4MulSerie(totmat, par->obmat, tmat,
1455 NULL, NULL, NULL, NULL, NULL, NULL);
1461 VECCOPY(totmat[3], par->obmat[3]);
1464 give_parvert(par, ob->par1, vec);
1465 VecMat4MulVecfl(totmat[3], par->obmat, vec);
1469 ob_parvert3(ob, par, tmat);
1471 Mat4MulSerie(totmat, par->obmat, tmat,
1472 NULL, NULL, NULL, NULL, NULL, NULL);
1476 Mat4CpyMat4(totmat, par->obmat);
1481 Mat4MulSerie(tmat, totmat, ob->parentinv,
1482 NULL, NULL, NULL, NULL, NULL, NULL);
1483 Mat4MulSerie(ob->obmat, tmat, obmat,
1484 NULL, NULL, NULL, NULL, NULL, NULL);
1490 // external usable originmat
1491 Mat3CpyMat4(originmat, tmat);
1493 // origin, voor help line
1494 if( (ob->partype & 15)==PARSKEL ) {
1495 VECCOPY(ob->orig, par->obmat[3]);
1498 VECCOPY(ob->orig, totmat[3]);
1503 void solve_tracking (Object *ob, float targetmat[][4])
1510 VecSubf(vec, ob->obmat[3], targetmat[3]);
1511 quat= vectoquat(vec, ob->trackflag, ob->upflag);
1512 QuatToMat3(quat, totmat);
1514 if(ob->parent && (ob->transflag & OB_POWERTRACK)) {
1515 /* 'temporal' : clear parent info */
1516 object_to_mat4(ob, tmat);
1517 tmat[0][3]= ob->obmat[0][3];
1518 tmat[1][3]= ob->obmat[1][3];
1519 tmat[2][3]= ob->obmat[2][3];
1520 tmat[3][0]= ob->obmat[3][0];
1521 tmat[3][1]= ob->obmat[3][1];
1522 tmat[3][2]= ob->obmat[3][2];
1523 tmat[3][3]= ob->obmat[3][3];
1525 else Mat4CpyMat4(tmat, ob->obmat);
1527 Mat4MulMat34(ob->obmat, totmat, tmat);
1531 void where_is_object(Object *ob)
1534 where_is_object_time(ob, (float)G.scene->r.cfra);
1538 void where_is_object_simul(Object *ob)
1539 /* was written for the old game engine (until 2.04) */
1540 /* It seems that this function is only called
1541 for a lamp that is the child of another object */
1546 float slowmat[4][4];
1552 /* no ipo! (because of dloc and realtime-ipos) */
1559 solve_parenting(ob, par, slowmat, 1);
1561 if(ob->partype & PARSLOW) {
1563 fac1= (float)(1.0/(1.0+ fabs(ob->sf)));
1567 for(a=0; a<16; a++, fp1++, fp2++) {
1568 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1574 object_to_mat4(ob, ob->obmat);
1578 solve_tracking(ob, ob->track->obmat);
1580 solve_constraints(ob, TARGET_OBJECT, NULL, G.scene->r.cfra);
1586 extern void Mat4BlendMat4(float out[][4], float dst[][4], float src[][4], float srcweight);
1588 void solve_constraints (Object *ob, short obtype, void *obdata, float ctime)
1591 float tmat[4][4], focusmat[4][4], lastmat[4][4];
1592 int i, clear=1, tot=0;
1594 float aquat[4], quat[4];
1595 float aloc[3], loc[3];
1596 float asize[3], size[3];
1598 float smat[3][3], rmat[3][3], mat[3][3];
1601 for (con = ob->constraints.first; con; con=con->next) {
1602 // inverse kinematics is solved seperate
1603 if (con->type==CONSTRAINT_TYPE_KINEMATIC) continue;
1604 // and this we can skip completely
1605 if (con->flag & CONSTRAINT_DISABLE) continue;
1606 // local constraints are handled in armature.c only
1607 if (con->flag & CONSTRAINT_LOCAL) continue;
1609 /* Clear accumulators if necessary*/
1614 memset(aquat, 0, sizeof(float)*4);
1615 memset(aloc, 0, sizeof(float)*3);
1616 memset(asize, 0, sizeof(float)*3);
1619 enf = con->enforce; // value from ipos (from action channels)
1621 /* Get the targetmat */
1622 get_constraint_target_matrix(con, obtype, obdata, tmat, size, ctime);
1624 Mat4CpyMat4(focusmat, tmat);
1626 /* Extract the components & accumulate */
1627 Mat4ToQuat(focusmat, quat);
1628 VECCOPY(loc, focusmat[3]);
1629 Mat3CpyMat4(mat, focusmat);
1630 Mat3ToSize(mat, size);
1635 for(i=0; i<3; i++) {
1636 aquat[i+1]+=(quat[i+1]) * enf;
1637 aloc[i]+=(loc[i]) * enf;
1638 asize[i]+=(size[i]-1.0f) * enf;
1640 aquat[0]+=(quat[0])*enf;
1641 Mat4CpyMat4(lastmat, focusmat);
1643 /* removed for now, probably becomes option? (ton) */
1645 /* If the next constraint is not the same type (or there isn't one),
1646 * then evaluate the accumulator & request a clear */
1647 if (TRUE) { //(!con->next)||(con->next && con->next->type!=con->type)) {
1649 Mat4CpyMat4(oldmat, ob->obmat);
1651 /* If we have several inputs, do a blend of them */
1655 for (i=0; i<3; i++) {
1656 asize[i]=1.0f + (asize[i]/(a));
1657 aloc[i]=(aloc[i]/a);
1662 QuatToMat3(aquat, rmat);
1663 SizeToMat3(asize, smat);
1664 Mat3MulMat3(mat, rmat, smat);
1665 Mat4CpyMat3(focusmat, mat);
1666 VECCOPY(focusmat[3], aloc);
1668 evaluate_constraint(con, ob, obtype, obdata, focusmat);
1672 /* If we only have one, blend with the current obmat */
1674 float solution[4][4];
1677 float identity[4][4];
1679 if (con->type!=CONSTRAINT_TYPE_KINEMATIC) {
1680 /* If we're not an IK constraint, solve the constraint then blend it to the previous one */
1681 evaluate_constraint(con, ob, obtype, obdata, lastmat);
1683 Mat4CpyMat4 (solution, ob->obmat);
1685 /* Interpolate the enforcement */
1686 Mat4Invert (imat, oldmat);
1687 Mat4MulMat4 (delta, solution, imat);
1691 Mat4BlendMat4(delta, identity, delta, a);
1693 Mat4MulMat4 (ob->obmat, delta, oldmat);
1702 /* for calculation of the inverse parent transform, only used for editor */
1703 void what_does_parent(Object *ob)
1707 Mat4One(workob.obmat);
1708 Mat4One(workob.parentinv);
1709 workob.parent= ob->parent;
1710 workob.track= ob->track;
1712 workob.trackflag= ob->trackflag;
1713 workob.upflag= ob->upflag;
1715 workob.partype= ob->partype;
1716 workob.par1= ob->par1;
1717 workob.par2= ob->par2;
1718 workob.par3= ob->par3;
1720 workob.constraints.first = ob->constraints.first;
1721 workob.constraints.last = ob->constraints.last;
1723 strcpy (workob.parsubstr, ob->parsubstr);
1725 where_is_object(&workob);
1728 BoundBox *unit_boundbox()
1731 float min[3] = {-1,-1,-1}, max[3] = {-1,-1,-1};
1733 bb= MEM_mallocN(sizeof(BoundBox), "bb");
1734 boundbox_set_from_min_max(bb, min, max);
1739 void boundbox_set_from_min_max(BoundBox *bb, float min[3], float max[3])
1741 bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
1742 bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
1744 bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
1745 bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
1747 bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
1748 bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
1751 void minmax_object(Object *ob, float *min, float *max)
1766 if(cu->bb==0) tex_space_curve(cu);
1769 for(a=0; a<8; a++) {
1770 Mat4MulVecfl(ob->obmat, bb.vec[a]);
1771 DO_MINMAX(bb.vec[a], min, max);
1776 bPoseChannel *pchan;
1777 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1778 VECCOPY(vec, pchan->pose_head);
1779 Mat4MulVecfl(ob->obmat, vec);
1780 DO_MINMAX(vec, min, max);
1781 VECCOPY(vec, pchan->pose_tail);
1782 Mat4MulVecfl(ob->obmat, vec);
1783 DO_MINMAX(vec, min, max);
1787 /* no break, get_mesh will give NULL and it passes on to default */
1792 bb = *mesh_get_bb(me);
1794 for(a=0; a<8; a++) {
1795 Mat4MulVecfl(ob->obmat, bb.vec[a]);
1796 DO_MINMAX(bb.vec[a], min, max);
1799 if(min[0] < max[0] ) break;
1801 /* else here no break!!!, mesh can be zero sized */
1804 DO_MINMAX(ob->obmat[3], min, max);
1806 VECCOPY(vec, ob->obmat[3]);
1807 VecAddf(vec, vec, ob->size);
1808 DO_MINMAX(vec, min, max);
1810 VECCOPY(vec, ob->obmat[3]);
1811 VecSubf(vec, vec, ob->size);
1812 DO_MINMAX(vec, min, max);
1817 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
1818 /* requires flags to be set! */
1819 void object_handle_update(Object *ob)
1821 if(ob->recalc & OB_RECALC) {
1823 if(ob->recalc & OB_RECALC_OB) where_is_object(ob);
1825 if(ob->recalc & OB_RECALC_DATA) {
1827 // printf("recalcdata %s\n", ob->id.name+2);
1829 /* includes all keys and modifiers */
1830 if(ob->type==OB_MESH) {
1831 makeDispListMesh(ob);
1833 else if(ob->type==OB_MBALL) {
1834 makeDispListMBall(ob);
1836 else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1837 makeDispListCurveTypes(ob, 0);
1839 else if(ob->type==OB_LATTICE) {
1840 lattice_calc_modifiers(ob);
1842 else if(ob->type==OB_ARMATURE) {
1843 /* this actually only happens for reading old files... */
1844 if(ob->pose==NULL || (ob->pose->flag & POSE_RECALC))
1845 armature_rebuild_pose(ob, ob->data);
1846 do_all_pose_actions(ob);
1851 ob->recalc &= ~OB_RECALC;