6 * ***** BEGIN GPL 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.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL LICENSE BLOCK *****
40 #include "MEM_guardedalloc.h"
42 #include "DNA_action_types.h"
43 #include "DNA_armature_types.h"
44 #include "DNA_camera_types.h"
45 #include "DNA_constraint_types.h"
46 #include "DNA_curve_types.h"
47 #include "DNA_group_types.h"
48 #include "DNA_ipo_types.h"
49 #include "DNA_lamp_types.h"
50 #include "DNA_lattice_types.h"
51 #include "DNA_material_types.h"
52 #include "DNA_mesh_types.h"
53 #include "DNA_meta_types.h"
54 #include "DNA_curve_types.h"
55 #include "DNA_meshdata_types.h"
56 #include "DNA_modifier_types.h"
57 #include "DNA_nla_types.h"
58 #include "DNA_object_types.h"
59 #include "DNA_object_force.h"
60 #include "DNA_object_fluidsim.h"
61 #include "DNA_oops_types.h"
62 #include "DNA_particle_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_bullet.h"
74 #include "BKE_colortools.h"
75 #include "BKE_deform.h"
76 #include "BKE_DerivedMesh.h"
79 #include "BLI_blenlib.h"
80 #include "BLI_arithb.h"
81 #include "BLI_editVert.h"
83 #include "BKE_utildefines.h"
84 #include "BKE_bad_level_calls.h"
87 #include "BKE_global.h"
90 #include "BKE_blender.h"
91 #include "BKE_constraint.h"
92 #include "BKE_curve.h"
93 #include "BKE_displist.h"
94 #include "BKE_group.h"
95 #include "BKE_icons.h"
98 #include "BKE_lattice.h"
99 #include "BKE_library.h"
100 #include "BKE_mesh.h"
101 #include "BKE_mball.h"
102 #include "BKE_modifier.h"
103 #include "BKE_object.h"
104 #include "BKE_particle.h"
105 #include "BKE_pointcache.h"
106 #include "BKE_property.h"
108 #include "BKE_scene.h"
109 #include "BKE_screen.h"
110 #include "BKE_softbody.h"
112 #include "LBM_fluidsim.h"
114 #ifndef DISABLE_PYTHON
115 #include "BPY_extern.h"
118 #include "GPU_material.h"
122 /* Local function protos */
123 static void solve_parenting (Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul);
125 float originmat[3][3]; /* after where_is_object(), can be used in other functions (bad!) */
128 void clear_workob(void)
130 memset(&workob, 0, sizeof(Object));
132 workob.size[0]= workob.size[1]= workob.size[2]= 1.0;
136 void copy_baseflags()
138 Base *base= G.scene->base.first;
141 base->object->flag= base->flag;
146 void copy_objectflags()
148 Base *base= G.scene->base.first;
151 base->flag= base->object->flag;
156 void update_base_layer(Object *ob)
158 Base *base= G.scene->base.first;
161 if (base->object == ob) base->lay= ob->lay;
166 void object_free_particlesystems(Object *ob)
168 while(ob->particlesystem.first){
169 ParticleSystem *psys = ob->particlesystem.first;
171 BLI_remlink(&ob->particlesystem,psys);
177 void object_free_softbody(Object *ob)
185 void object_free_bulletsoftbody(Object *ob)
193 void object_free_modifiers(Object *ob)
195 while (ob->modifiers.first) {
196 ModifierData *md = ob->modifiers.first;
198 BLI_remlink(&ob->modifiers, md);
203 /* particle modifiers were freed, so free the particlesystems as well */
204 object_free_particlesystems(ob);
206 /* same for softbody */
207 object_free_softbody(ob);
210 /* here we will collect all local displist stuff */
211 /* also (ab)used in depsgraph */
212 void object_free_display(Object *ob)
214 if(ob->derivedDeform) {
215 ob->derivedDeform->needsFree = 1;
216 ob->derivedDeform->release(ob->derivedDeform);
217 ob->derivedDeform= NULL;
219 if(ob->derivedFinal) {
220 ob->derivedFinal->needsFree = 1;
221 ob->derivedFinal->release(ob->derivedFinal);
222 ob->derivedFinal= NULL;
225 freedisplist(&ob->disp);
228 /* do not free object itself */
229 void free_object(Object *ob)
233 object_free_display(ob);
235 /* disconnect specific data */
240 if(ob->type==OB_MESH) unlink_mesh(ob->data);
241 else if(ob->type==OB_CURVE) unlink_curve(ob->data);
242 else if(ob->type==OB_MBALL) unlink_mball(ob->data);
247 for(a=0; a<ob->totcol; a++) {
248 if(ob->mat[a]) ob->mat[a]->id.us--;
250 if(ob->mat) MEM_freeN(ob->mat);
252 if(ob->bb) MEM_freeN(ob->bb);
254 if(ob->path) free_path(ob->path);
256 if(ob->ipo) ob->ipo->id.us--;
257 if(ob->action) ob->action->id.us--;
258 if(ob->poselib) ob->poselib->id.us--;
259 if(ob->dup_group) ob->dup_group->id.us--;
260 if(ob->defbase.first)
261 BLI_freelistN(&ob->defbase);
264 free_properties(&ob->prop);
265 object_free_modifiers(ob);
267 free_sensors(&ob->sensors);
268 free_controllers(&ob->controllers);
269 free_actuators(&ob->actuators);
271 free_constraints(&ob->constraints);
272 free_constraint_channels(&ob->constraintChannels);
273 free_nlastrips(&ob->nlastrips);
275 #ifndef DISABLE_PYTHON
276 BPY_free_scriptlink(&ob->scriptlink);
281 ob->pd->tex->id.us--;
284 if(ob->soft) sbFree(ob->soft);
285 if(ob->bsoft) bsbFree(ob->bsoft);
286 if(ob->gpulamp.first) GPU_lamp_free(ob);
289 static void unlink_object__unlinkModifierLinks(void *userData, Object *ob, Object **obpoin)
291 Object *unlinkOb = userData;
293 if (*obpoin==unlinkOb) {
295 ob->recalc |= OB_RECALC;
298 void unlink_object(Object *ob)
315 unlink_controllers(&ob->controllers);
316 unlink_actuators(&ob->actuators);
318 /* check all objects: parents en bevels and fields, also from libraries */
319 obt= G.main->object.first;
323 if(obt->proxy_from==ob) {
324 obt->proxy_from= NULL;
325 obt->recalc |= OB_RECALC_OB;
327 if(obt->proxy_group==ob)
328 obt->proxy_group= NULL;
330 if(obt->parent==ob) {
332 obt->recalc |= OB_RECALC;
337 obt->recalc |= OB_RECALC_OB;
340 modifiers_foreachObjectLink(obt, unlink_object__unlinkModifierLinks, ob);
342 if ELEM(obt->type, OB_CURVE, OB_FONT) {
347 obt->recalc |= OB_RECALC;
349 if(cu->taperobj==ob) {
351 obt->recalc |= OB_RECALC;
353 if(cu->textoncurve==ob) {
354 cu->textoncurve= NULL;
355 obt->recalc |= OB_RECALC;
358 else if(obt->type==OB_ARMATURE && obt->pose) {
360 for(pchan= obt->pose->chanbase.first; pchan; pchan= pchan->next) {
361 for (con = pchan->constraints.first; con; con=con->next) {
362 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
363 ListBase targets = {NULL, NULL};
364 bConstraintTarget *ct;
366 if (cti && cti->get_constraint_targets) {
367 cti->get_constraint_targets(con, &targets);
369 for (ct= targets.first; ct; ct= ct->next) {
372 strcpy(ct->subtarget, "");
373 obt->recalc |= OB_RECALC_DATA;
377 if (cti->flush_constraint_targets)
378 cti->flush_constraint_targets(con, &targets, 0);
381 if(pchan->custom==ob)
386 sca_remove_ob_poin(obt, ob);
388 for (con = obt->constraints.first; con; con=con->next) {
389 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
390 ListBase targets = {NULL, NULL};
391 bConstraintTarget *ct;
393 if (cti && cti->get_constraint_targets) {
394 cti->get_constraint_targets(con, &targets);
396 for (ct= targets.first; ct; ct= ct->next) {
399 strcpy(ct->subtarget, "");
400 obt->recalc |= OB_RECALC_DATA;
404 if (cti->flush_constraint_targets)
405 cti->flush_constraint_targets(con, &targets, 0);
409 /* object is deflector or field */
412 obt->recalc |= OB_RECALC_DATA;
415 for(md=obt->modifiers.first; md; md=md->next)
416 if(md->type == eModifierType_Cloth)
417 obt->recalc |= OB_RECALC_DATA;
421 for(strip= obt->nlastrips.first; strip; strip= strip->next) {
422 if(strip->object==ob)
425 if(strip->modifiers.first) {
426 bActionModifier *amod;
427 for(amod= strip->modifiers.first; amod; amod= amod->next)
433 /* particle systems */
434 if(obt->particlesystem.first) {
435 ParticleSystem *tpsys= obt->particlesystem.first;
436 for(; tpsys; tpsys=tpsys->next) {
437 if(tpsys->keyed_ob==ob) {
438 ParticleSystem *psys= BLI_findlink(&ob->particlesystem,tpsys->keyed_psys-1);
440 if(psys && psys->keyed_ob) {
441 tpsys->keyed_ob= psys->keyed_ob;
442 tpsys->keyed_psys= psys->keyed_psys;
445 tpsys->keyed_ob= NULL;
447 obt->recalc |= OB_RECALC_DATA;
450 if(tpsys->target_ob==ob) {
451 tpsys->target_ob= NULL;
452 obt->recalc |= OB_RECALC_DATA;
455 if(tpsys->part->dup_ob==ob)
456 tpsys->part->dup_ob= NULL;
458 if(tpsys->part->flag&PART_STICKY) {
462 for(p=0,pa=tpsys->particles; p<tpsys->totpart; p++,pa++) {
463 if(pa->stick_ob==ob) {
465 pa->flag &= ~PARS_STICKY;
471 obt->recalc |= OB_RECALC_DATA;
478 mat= G.main->mat.first;
481 for(a=0; a<MAX_MTEX; a++) {
482 if(mat->mtex[a] && ob==mat->mtex[a]->object) {
483 /* actually, test for lib here... to do */
484 mat->mtex[a]->object= NULL;
492 tex= G.main->tex.first;
495 if(tex->env->object == ob) tex->env->object= NULL;
501 if(ob->type==OB_MBALL) {
502 obt= find_basis_mball(ob);
503 if(obt) freedisplist(&obt->disp);
507 wrld= G.main->world.first;
509 if(wrld->id.lib==NULL) {
510 for(a=0; a<MAX_MTEX; a++) {
511 if(wrld->mtex[a] && ob==wrld->mtex[a]->object)
512 wrld->mtex[a]->object= NULL;
520 sce= G.main->scene.first;
522 if(sce->id.lib==NULL) {
523 if(sce->camera==ob) sce->camera= NULL;
528 ipo= G.main->ipo.first;
530 if(ipo->id.lib==NULL) {
532 for(icu= ipo->curve.first; icu; icu= icu->next) {
533 if(icu->driver && icu->driver->ob==ob)
534 icu->driver->ob= NULL;
541 sc= G.main->screen.first;
543 ScrArea *sa= sc->areabase.first;
547 for (sl= sa->spacedata.first; sl; sl= sl->next) {
548 if(sl->spacetype==SPACE_VIEW3D) {
549 View3D *v3d= (View3D*) sl;
551 if(v3d->camera==ob) {
553 if(v3d->persp==V3D_CAMOB) v3d->persp= V3D_PERSP;
555 if(v3d->localvd && v3d->localvd->camera==ob ) {
556 v3d->localvd->camera= NULL;
557 if(v3d->localvd->persp==V3D_CAMOB) v3d->localvd->persp= V3D_PERSP;
560 else if(sl->spacetype==SPACE_IPO) {
561 SpaceIpo *sipo= (SpaceIpo *)sl;
562 if(sipo->from == (ID *)ob) sipo->from= NULL;
564 else if(sl->spacetype==SPACE_OOPS) {
565 SpaceOops *so= (SpaceOops *)sl;
568 oops= so->oops.first;
570 if(oops->id==(ID *)ob) oops->id= NULL;
574 TreeStoreElem *tselem= so->treestore->data;
576 for(a=0; a<so->treestore->usedelem; a++, tselem++) {
577 if(tselem->id==(ID *)ob) tselem->id= NULL;
590 group= G.main->group.first;
592 rem_from_group(group, ob);
593 group= group->id.next;
597 camera= G.main->camera.first;
599 if (camera->dof_ob==ob) {
600 camera->dof_ob = NULL;
602 camera= camera->id.next;
606 int exist_object(Object *obtest)
610 if(obtest==NULL) return 0;
612 ob= G.main->object.first;
614 if(ob==obtest) return 1;
620 void *add_camera(char *name)
624 cam= alloc_libblock(&G.main->camera, ID_CA, name);
629 cam->clipend= 100.0f;
631 cam->ortho_scale= 6.0;
632 cam->flag |= CAM_SHOWTITLESAFE;
633 cam->passepartalpha = 0.2f;
638 Camera *copy_camera(Camera *cam)
642 camn= copy_libblock(cam);
643 id_us_plus((ID *)camn->ipo);
644 #ifndef DISABLE_PYTHON
645 BPY_copy_scriptlink(&camn->scriptlink);
652 void make_local_camera(Camera *cam)
658 /* - only lib users: do nothing
659 * - only local users: set flag
663 if(cam->id.lib==0) return;
666 cam->id.flag= LIB_LOCAL;
667 new_id(0, (ID *)cam, 0);
671 ob= G.main->object.first;
674 if(ob->id.lib) lib= 1;
680 if(local && lib==0) {
682 cam->id.flag= LIB_LOCAL;
683 new_id(0, (ID *)cam, 0);
685 else if(local && lib) {
686 camn= copy_camera(cam);
689 ob= G.main->object.first;
704 /* get the camera's dof value, takes the dof object into account */
705 float dof_camera(Object *ob)
707 Camera *cam = (Camera *)ob->data;
708 if (ob->type != OB_CAMERA)
711 /* too simple, better to return the distance on the view axis only
712 * return VecLenf(ob->obmat[3], cam->dof_ob->obmat[3]); */
713 float mat[4][4], obmat[4][4];
715 Mat4CpyMat4(obmat, ob->obmat);
717 Mat4Invert(ob->imat, obmat);
718 Mat4MulMat4(mat, cam->dof_ob->obmat, ob->imat);
719 return fabs(mat[3][2]);
721 return cam->YF_dofdist;
724 void *add_lamp(char *name)
728 la= alloc_libblock(&G.main->lamp, ID_LA, name);
730 la->r= la->g= la->b= la->k= 1.0;
731 la->haint= la->energy= 1.0;
736 la->mode= LA_SHAD_BUF;
740 la->shadspotsize= 45.0;
744 la->ray_samp= la->ray_sampy= la->ray_sampz= 1;
745 la->area_size=la->area_sizey=la->area_sizez= 1.0;
747 la->buftype= LA_SHADBUF_HALFWAY;
748 la->ray_samp_method = LA_SAMP_HALTON;
749 la->adapt_thresh = 0.001;
751 la->falloff_type = LA_FALLOFF_INVLINEAR;
752 la->curfalloff = curvemapping_add(1, 0.0f, 1.0f, 1.0f, 0.0f);
753 la->sun_effect_type = 0;
754 la->horizon_brightness = 1.0;
756 la->sun_brightness = 1.0;
758 la->backscattered_light = 1.0;
759 la->atm_turbidity = 2.0;
760 la->atm_inscattering_factor = 1.0;
761 la->atm_extinction_factor = 1.0;
762 la->atm_distance_factor = 1.0;
763 la->sun_intensity = 1.0;
764 la->skyblendtype= MA_RAMP_ADD;
765 la->skyblendfac= 1.0f;
766 la->sky_colorspace= BLI_CS_CIE;
767 la->sky_exposure= 1.0f;
769 curvemapping_initialize(la->curfalloff);
773 Lamp *copy_lamp(Lamp *la)
778 lan= copy_libblock(la);
780 for(a=0; a<MAX_MTEX; a++) {
782 lan->mtex[a]= MEM_mallocN(sizeof(MTex), "copylamptex");
783 memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
784 id_us_plus((ID *)lan->mtex[a]->tex);
788 lan->curfalloff = curvemapping_copy(la->curfalloff);
790 id_us_plus((ID *)lan->ipo);
792 if (la->preview) lan->preview = BKE_previewimg_copy(la->preview);
793 #ifndef DISABLE_PYTHON
794 BPY_copy_scriptlink(&la->scriptlink);
799 void make_local_lamp(Lamp *la)
805 /* - only lib users: do nothing
806 * - only local users: set flag
810 if(la->id.lib==0) return;
813 la->id.flag= LIB_LOCAL;
814 new_id(0, (ID *)la, 0);
818 ob= G.main->object.first;
821 if(ob->id.lib) lib= 1;
827 if(local && lib==0) {
829 la->id.flag= LIB_LOCAL;
830 new_id(0, (ID *)la, 0);
832 else if(local && lib) {
836 ob= G.main->object.first;
851 void free_camera(Camera *ca)
853 #ifndef DISABLE_PYTHON
854 BPY_free_scriptlink(&ca->scriptlink);
858 void free_lamp(Lamp *la)
864 #ifndef DISABLE_PYTHON
865 BPY_free_scriptlink(&la->scriptlink);
868 for(a=0; a<MAX_MTEX; a++) {
870 if(mtex && mtex->tex) mtex->tex->id.us--;
871 if(mtex) MEM_freeN(mtex);
875 curvemapping_free(la->curfalloff);
877 BKE_previewimg_free(&la->preview);
878 BKE_icon_delete(&la->id);
888 /* *************************************************** */
890 static void *add_obdata_from_type(int type)
893 case OB_MESH: G.totmesh++; return add_mesh("Mesh");
894 case OB_CURVE: G.totcurve++; return add_curve("Curve", OB_CURVE);
895 case OB_SURF: G.totcurve++; return add_curve("Surf", OB_SURF);
896 case OB_FONT: return add_curve("Text", OB_FONT);
897 case OB_MBALL: return add_mball("Meta");
898 case OB_CAMERA: return add_camera("Camera");
899 case OB_LAMP: G.totlamp++; return add_lamp("Lamp");
900 case OB_LATTICE: return add_lattice("Lattice");
901 case OB_WAVE: return add_wave();
902 case OB_ARMATURE: return add_armature("Armature");
903 case OB_EMPTY: return NULL;
905 printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
910 static char *get_obdata_defname(int type)
913 case OB_MESH: return "Mesh";
914 case OB_CURVE: return "Curve";
915 case OB_SURF: return "Surf";
916 case OB_FONT: return "Font";
917 case OB_MBALL: return "Mball";
918 case OB_CAMERA: return "Camera";
919 case OB_LAMP: return "Lamp";
920 case OB_LATTICE: return "Lattice";
921 case OB_WAVE: return "Wave";
922 case OB_ARMATURE: return "Armature";
923 case OB_EMPTY: return "Empty";
925 printf("get_obdata_defname: Internal error, bad type: %d\n", type);
930 /* more general add: creates minimum required data, but without vertices etc. */
931 Object *add_only_object(int type, char *name)
935 ob= alloc_libblock(&G.main->object, ID_OB, name);
938 /* default object vars */
940 /* ob->transflag= OB_QUAT; */
942 #if 0 /* not used yet */
947 ob->col[0]= ob->col[1]= ob->col[2]= 1.0;
950 ob->loc[0]= ob->loc[1]= ob->loc[2]= 0.0;
951 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
952 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
954 Mat4One(ob->constinv);
955 Mat4One(ob->parentinv);
958 if(U.flag & USER_MAT_ON_OB) ob->colbits= -1;
959 ob->empty_drawtype= OB_ARROWS;
960 ob->empty_drawsize= 1.0;
962 if(type==OB_CAMERA || type==OB_LAMP) {
963 ob->trackflag= OB_NEGZ;
967 ob->trackflag= OB_POSY;
970 ob->ipoflag = OB_OFFS_OB+OB_OFFS_PARENT;
971 ob->ipowin= ID_OB; /* the ipowin shown */
972 ob->dupon= 1; ob->dupoff= 0;
973 ob->dupsta= 1; ob->dupend= 100;
974 ob->dupfacesca = 1.0;
976 /* Game engine defaults*/
977 ob->mass= ob->inertia= 1.0f;
978 ob->formfactor= 0.4f;
981 ob->anisotropicFriction[0] = 1.0f;
982 ob->anisotropicFriction[1] = 1.0f;
983 ob->anisotropicFriction[2] = 1.0f;
984 ob->gameflag= OB_PROP|OB_COLLISION;
987 /* NT fluid sim defaults */
988 ob->fluidsimFlag = 0;
989 ob->fluidsimSettings = NULL;
994 /* general add: to G.scene, with layer from area and default name */
995 /* creates minimum required data, but without vertices etc. */
996 Object *add_object(int type)
1002 strcpy(name, get_obdata_defname(type));
1003 ob = add_only_object(type, name);
1005 ob->data= add_obdata_from_type(type);
1007 ob->lay= G.scene->lay;
1009 base= scene_add_base(G.scene, ob);
1010 scene_select_base(G.scene, base);
1011 ob->recalc |= OB_RECALC;
1016 void base_init_from_view3d(Base *base, View3D *v3d)
1018 Object *ob= base->object;
1021 /* no 3d view, this wont happen often */
1023 VECCOPY(ob->loc, G.scene->cursor);
1025 /* return now because v3d->viewquat isnt available */
1027 } else if (v3d->localview) {
1028 base->lay= ob->lay= v3d->layact + v3d->lay;
1029 VECCOPY(ob->loc, v3d->cursor);
1031 base->lay= ob->lay= v3d->layact;
1032 VECCOPY(ob->loc, G.scene->cursor);
1035 if (U.flag & USER_ADD_VIEWALIGNED) {
1036 v3d->viewquat[0]= -v3d->viewquat[0];
1038 /* Quats arnt used yet */
1039 /*if (ob->transflag & OB_QUAT) {
1040 QUATCOPY(ob->quat, v3d->viewquat);
1042 QuatToEul(v3d->viewquat, ob->rot);
1044 v3d->viewquat[0]= -v3d->viewquat[0];
1048 SoftBody *copy_softbody(SoftBody *sb)
1052 if (sb==NULL) return(NULL);
1054 sbn= MEM_dupallocN(sb);
1055 sbn->totspring= sbn->totpoint= 0;
1060 sbn->totkey= sbn->totpointkey= 0;
1064 sbn->pointcache= BKE_ptcache_copy(sb->pointcache);
1069 BulletSoftBody *copy_bulletsoftbody(BulletSoftBody *bsb)
1071 BulletSoftBody *bsbn;
1075 bsbn = MEM_dupallocN(bsb);
1076 /* no pointer in this structure yet */
1080 ParticleSystem *copy_particlesystem(ParticleSystem *psys)
1082 ParticleSystem *psysn;
1086 psysn= MEM_dupallocN(psys);
1087 psysn->particles= MEM_dupallocN(psys->particles);
1088 psysn->child= MEM_dupallocN(psys->child);
1090 for(a=0, pa=psysn->particles; a<psysn->totpart; a++, pa++) {
1092 pa->hair= MEM_dupallocN(pa->hair);
1094 pa->keys= MEM_dupallocN(pa->keys);
1098 psysn->soft= copy_softbody(psys->soft);
1099 psysn->soft->particles = psysn;
1102 psysn->pathcache= NULL;
1103 psysn->childcache= NULL;
1105 psysn->effectors.first= psysn->effectors.last= 0;
1107 psysn->pathcachebufs.first = psysn->pathcachebufs.last = NULL;
1108 psysn->childcachebufs.first = psysn->childcachebufs.last = NULL;
1109 psysn->reactevents.first = psysn->reactevents.last = NULL;
1110 psysn->renderdata = NULL;
1112 psysn->pointcache= BKE_ptcache_copy(psys->pointcache);
1114 id_us_plus((ID *)psysn->part);
1119 void copy_object_particlesystems(Object *obn, Object *ob)
1121 ParticleSystemModifierData *psmd;
1122 ParticleSystem *psys, *npsys;
1125 obn->particlesystem.first= obn->particlesystem.last= NULL;
1126 for(psys=ob->particlesystem.first; psys; psys=psys->next) {
1127 npsys= copy_particlesystem(psys);
1129 BLI_addtail(&obn->particlesystem, npsys);
1131 /* need to update particle modifiers too */
1132 for(md=obn->modifiers.first; md; md=md->next) {
1133 if(md->type==eModifierType_ParticleSystem) {
1134 psmd= (ParticleSystemModifierData*)md;
1135 if(psmd->psys==psys)
1142 void copy_object_softbody(Object *obn, Object *ob)
1145 obn->soft= copy_softbody(ob->soft);
1148 static void copy_object_pose(Object *obn, Object *ob)
1152 /* note: need to clear obn->pose pointer first, so that copy_pose works (otherwise there's a crash) */
1154 copy_pose(&obn->pose, ob->pose, 1); /* 1 = copy constraints */
1156 for (chan = obn->pose->chanbase.first; chan; chan=chan->next){
1159 chan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
1161 for (con= chan->constraints.first; con; con= con->next) {
1162 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1163 ListBase targets = {NULL, NULL};
1164 bConstraintTarget *ct;
1168 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
1169 if(icu->driver && icu->driver->ob==ob)
1170 icu->driver->ob= obn;
1174 if (cti && cti->get_constraint_targets) {
1175 cti->get_constraint_targets(con, &targets);
1177 for (ct= targets.first; ct; ct= ct->next) {
1182 if (cti->flush_constraint_targets)
1183 cti->flush_constraint_targets(con, &targets, 0);
1189 Object *copy_object(Object *ob)
1195 obn= copy_libblock(ob);
1198 obn->mat= MEM_dupallocN(ob->mat);
1201 if(ob->bb) obn->bb= MEM_dupallocN(ob->bb);
1203 obn->flag &= ~OB_FROMGROUP;
1205 obn->modifiers.first = obn->modifiers.last= NULL;
1207 for (md=ob->modifiers.first; md; md=md->next) {
1208 ModifierData *nmd = modifier_new(md->type);
1209 modifier_copyData(md, nmd);
1210 BLI_addtail(&obn->modifiers, nmd);
1212 #ifndef DISABLE_PYTHON
1213 BPY_copy_scriptlink(&ob->scriptlink);
1215 obn->prop.first = obn->prop.last = NULL;
1216 copy_properties(&obn->prop, &ob->prop);
1218 copy_sensors(&obn->sensors, &ob->sensors);
1219 copy_controllers(&obn->controllers, &ob->controllers);
1220 copy_actuators(&obn->actuators, &ob->actuators);
1223 copy_object_pose(obn, ob);
1224 /* backwards compat... non-armatures can get poses in older files? */
1225 if(ob->type==OB_ARMATURE)
1226 armature_rebuild_pose(obn, obn->data);
1228 copy_defgroups(&obn->defbase, &ob->defbase);
1229 copy_nlastrips(&obn->nlastrips, &ob->nlastrips);
1230 copy_constraints (&obn->constraints, &ob->constraints);
1232 clone_constraint_channels (&obn->constraintChannels, &ob->constraintChannels);
1234 /* increase user numbers */
1235 id_us_plus((ID *)obn->data);
1236 id_us_plus((ID *)obn->ipo);
1237 id_us_plus((ID *)obn->action);
1238 id_us_plus((ID *)obn->dup_group);
1240 for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
1242 obn->disp.first= obn->disp.last= NULL;
1245 obn->pd= MEM_dupallocN(ob->pd);
1247 id_us_plus(&(obn->pd->tex->id));
1249 obn->soft= copy_softbody(ob->soft);
1250 obn->bsoft = copy_bulletsoftbody(ob->bsoft);
1252 copy_object_particlesystems(obn, ob);
1254 obn->derivedDeform = NULL;
1255 obn->derivedFinal = NULL;
1261 obn->gpulamp.first = obn->gpulamp.last = NULL;
1266 void expand_local_object(Object *ob)
1268 bActionStrip *strip;
1269 ParticleSystem *psys;
1272 id_lib_extern((ID *)ob->action);
1273 id_lib_extern((ID *)ob->ipo);
1274 id_lib_extern((ID *)ob->data);
1275 id_lib_extern((ID *)ob->dup_group);
1277 for(a=0; a<ob->totcol; a++) {
1278 id_lib_extern((ID *)ob->mat[a]);
1280 for (strip=ob->nlastrips.first; strip; strip=strip->next) {
1281 id_lib_extern((ID *)strip->act);
1283 for(psys=ob->particlesystem.first; psys; psys=psys->next)
1284 id_lib_extern((ID *)psys->part);
1287 void make_local_object(Object *ob)
1294 /* - only lib users: do nothing
1295 * - only local users: set flag
1296 * - mixed: make copy
1299 if(ob->id.lib==NULL) return;
1301 ob->proxy= ob->proxy_from= NULL;
1305 ob->id.flag= LIB_LOCAL;
1306 new_id(0, (ID *)ob, 0);
1310 sce= G.main->scene.first;
1312 base= sce->base.first;
1314 if(base->object==ob) {
1315 if(sce->id.lib) lib++;
1324 if(local && lib==0) {
1326 ob->id.flag= LIB_LOCAL;
1327 new_id(0, (ID *)ob, 0);
1329 else if(local && lib) {
1330 obn= copy_object(ob);
1333 sce= G.main->scene.first;
1335 if(sce->id.lib==0) {
1336 base= sce->base.first;
1338 if(base->object==ob) {
1351 expand_local_object(ob);
1354 /* *************** PROXY **************** */
1356 /* when you make proxy, ensure the exposed layers are extern */
1357 void armature_set_id_extern(Object *ob)
1359 bArmature *arm= ob->data;
1360 bPoseChannel *pchan;
1361 int lay= arm->layer_protected;
1363 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1364 if(!(pchan->bone->layer & lay))
1365 id_lib_extern((ID *)pchan->custom);
1370 /* proxy rule: lib_object->proxy_from == the one we borrow from, set temporally while object_update */
1371 /* local_object->proxy == pointer to library object, saved in files and read */
1372 /* local_object->proxy_group == pointer to group dupli-object, saved in files and read */
1374 void object_make_proxy(Object *ob, Object *target, Object *gob)
1376 /* paranoia checks */
1377 if(ob->id.lib || target->id.lib==NULL) {
1378 printf("cannot make proxy\n");
1383 ob->proxy_group= gob;
1384 id_lib_extern(&target->id);
1386 ob->recalc= target->recalc= OB_RECALC;
1388 /* copy transform */
1390 VECCOPY(ob->loc, gob->loc);
1391 VECCOPY(ob->rot, gob->rot);
1392 VECCOPY(ob->size, gob->size);
1394 group_tag_recalc(gob->dup_group);
1397 VECCOPY(ob->loc, target->loc);
1398 VECCOPY(ob->rot, target->rot);
1399 VECCOPY(ob->size, target->size);
1402 ob->parent= target->parent; /* libdata */
1403 Mat4CpyMat4(ob->parentinv, target->parentinv);
1404 ob->ipo= target->ipo; /* libdata */
1406 /* skip constraints, constraintchannels, nla? */
1408 /* set object type and link to data */
1409 ob->type= target->type;
1410 ob->data= target->data;
1411 id_us_plus((ID *)ob->data); /* ensures lib data becomes LIB_EXTERN */
1413 /* copy material and index information */
1414 ob->actcol= ob->totcol= 0;
1415 if(ob->mat) MEM_freeN(ob->mat);
1417 if ((target->totcol) && (target->mat) && OB_SUPPORT_MATERIAL(ob)) {
1419 ob->colbits = target->colbits;
1421 ob->actcol= target->actcol;
1422 ob->totcol= target->totcol;
1424 ob->mat = MEM_dupallocN(target->mat);
1425 for(i=0; i<target->totcol; i++) {
1426 /* dont need to run test_object_materials since we know this object is new and not used elsewhere */
1427 id_us_plus((ID *)ob->mat[i]);
1431 /* type conversions */
1432 if(target->type == OB_ARMATURE) {
1433 copy_object_pose(ob, target); /* data copy, object pointers in constraints */
1434 rest_pose(ob->pose); /* clear all transforms in channels */
1435 armature_rebuild_pose(ob, ob->data); /* set all internal links */
1437 armature_set_id_extern(ob);
1442 /* *************** CALC ****************** */
1444 /* there is also a timing calculation in drawobject() */
1446 float bluroffs= 0.0f, fieldoffs= 0.0f;
1447 int no_speed_curve= 0;
1449 /* ugly calls from render */
1450 void set_mblur_offs(float blur)
1455 void set_field_offs(float field)
1460 void disable_speed_curve(int val)
1462 no_speed_curve= val;
1465 /* ob can be NULL */
1466 float bsystem_time(Object *ob, float cfra, float ofs)
1468 /* returns float ( see frame_to_float in ipo.c) */
1470 /* bluroffs and fieldoffs are ugly globals that are set by render */
1471 cfra+= bluroffs+fieldoffs;
1474 cfra*= G.scene->r.framelen;
1477 if (no_speed_curve==0 && ob->ipo)
1478 cfra= calc_ipo_time(ob->ipo, cfra);
1481 if ((ob->ipoflag & OB_OFFS_PARENT) && (ob->partype & PARSLOW)==0)
1482 cfra-= give_timeoffset(ob);
1490 void object_scale_to_mat3(Object *ob, float mat[][3])
1494 vec[0]= ob->size[0]+ob->dsize[0];
1495 vec[1]= ob->size[1]+ob->dsize[1];
1496 vec[2]= ob->size[2]+ob->dsize[2];
1497 SizeToMat3(vec, mat);
1500 SizeToMat3(ob->size, mat);
1504 void object_rot_to_mat3(Object *ob, float mat[][3])
1508 vec[0]= ob->rot[0]+ob->drot[0];
1509 vec[1]= ob->rot[1]+ob->drot[1];
1510 vec[2]= ob->rot[2]+ob->drot[2];
1511 EulToMat3(vec, mat);
1514 EulToMat3(ob->rot, mat);
1518 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
1525 object_scale_to_mat3(ob, smat);
1528 /* Quats arnt used yet */
1529 /*if(ob->transflag & OB_QUAT) {
1531 QuatMul(q1, ob->quat, ob->dquat);
1532 QuatToMat3(q1, rmat);
1535 QuatToMat3(ob->quat, rmat);
1539 object_rot_to_mat3(ob, rmat);
1541 Mat3MulMat3(mat, rmat, smat);
1544 void object_to_mat4(Object *ob, float mat[][4])
1548 object_to_mat3(ob, tmat);
1550 Mat4CpyMat3(mat, tmat);
1552 VECCOPY(mat[3], ob->loc);
1554 mat[3][0]+= ob->dloc[0];
1555 mat[3][1]+= ob->dloc[1];
1556 mat[3][2]+= ob->dloc[2];
1560 int enable_cu_speed= 1;
1562 static void ob_parcurve(Object *ob, Object *par, float mat[][4])
1565 float q[4], vec[4], dir[3], quat[4], x1, ctime;
1566 float timeoffs = 0.0, sf_orig = 0.0;
1571 if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1572 makeDispListCurveTypes(par, 0);
1573 if(cu->path==NULL) return;
1575 /* exception, timeoffset is regarded as distance offset */
1576 if(cu->flag & CU_OFFS_PATHDIST) {
1577 timeoffs = give_timeoffset(ob);
1578 SWAP(float, sf_orig, ob->sf);
1581 /* catch exceptions: feature for nla stride editing */
1582 if(ob->ipoflag & OB_DISABLE_PATH) {
1585 /* catch exceptions: curve paths used as a duplicator */
1586 else if(enable_cu_speed) {
1587 ctime= bsystem_time(ob, (float)G.scene->r.cfra, 0.0);
1589 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
1590 ctime /= cu->pathlen;
1591 CLAMP(ctime, 0.0, 1.0);
1595 ctime= G.scene->r.cfra - give_timeoffset(ob);
1596 ctime /= cu->pathlen;
1598 CLAMP(ctime, 0.0, 1.0);
1601 /* time calculus is correct, now apply distance offset */
1602 if(cu->flag & CU_OFFS_PATHDIST) {
1603 ctime += timeoffs/cu->path->totdist;
1606 SWAP(float, sf_orig, ob->sf);
1611 if( where_on_path(par, ctime, vec, dir) ) {
1613 if(cu->flag & CU_FOLLOW) {
1614 vectoquat(dir, ob->trackflag, ob->upflag, quat);
1618 q[0]= (float)cos(0.5*vec[3]);
1619 x1= (float)sin(0.5*vec[3]);
1623 QuatMul(quat, q, quat);
1625 QuatToMat4(quat, mat);
1628 VECCOPY(mat[3], vec);
1633 static void ob_parbone(Object *ob, Object *par, float mat[][4])
1635 bPoseChannel *pchan;
1639 arm=get_armature(par);
1645 /* Make sure the bone is still valid */
1646 pchan= get_pose_channel(par->pose, ob->parsubstr);
1648 printf ("Object %s with Bone parent: bone %s doesn't exist\n", ob->id.name+2, ob->parsubstr);
1653 /* get bone transform */
1654 Mat4CpyMat4(mat, pchan->pose_mat);
1656 /* but for backwards compatibility, the child has to move to the tail */
1657 VECCOPY(vec, mat[1]);
1658 VecMulf(vec, pchan->bone->length);
1659 VecAddf(mat[3], mat[3], vec);
1662 static void give_parvert(Object *par, int nr, float *vec)
1666 vec[0]=vec[1]=vec[2]= 0.0f;
1668 if(par->type==OB_MESH) {
1669 if(G.obedit && (par->data==G.obedit->data)) {
1670 EditMesh *em = G.editMesh;
1673 for(eve= em->verts.first; eve; eve= eve->next) {
1674 if(eve->keyindex==nr) {
1675 memcpy(vec, eve->co, sizeof(float)*3);
1681 DerivedMesh *dm = par->derivedFinal;
1684 int i, count = 0, numVerts = dm->getNumVerts(dm);
1685 int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
1688 /* get the average of all verts with (original index == nr) */
1689 for(i = 0; i < numVerts; ++i, ++index) {
1691 dm->getVertCo(dm, i, co);
1692 VecAddf(vec, vec, co);
1699 } else if(count > 0) {
1700 VecMulf(vec, 1.0f / count);
1702 /* use first index if its out of range */
1703 dm->getVertCo(dm, 0, vec);
1708 else if ELEM(par->type, OB_CURVE, OB_SURF) {
1717 if(par==G.obedit) nu= editNurb.first;
1720 while(nu && !found) {
1721 if((nu->type & 7)==CU_BEZIER) {
1727 VECCOPY(vec, bezt->vec[1]);
1736 a= nu->pntsu*nu->pntsv;
1740 memcpy(vec, bp->vec, sizeof(float)*3);
1751 else if(par->type==OB_LATTICE) {
1752 Lattice *latt= par->data;
1754 DispList *dl = find_displist(&par->disp, DL_VERTS);
1755 float *co = dl?dl->verts:NULL;
1757 if(par==G.obedit) latt= editLatt;
1759 a= latt->pntsu*latt->pntsv*latt->pntsw;
1765 memcpy(vec, co, 3*sizeof(float));
1767 memcpy(vec, bp->vec, 3*sizeof(float));
1777 static void ob_parvert3(Object *ob, Object *par, float mat[][4])
1779 float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1781 /* in local ob space */
1784 if ELEM4(par->type, OB_MESH, OB_SURF, OB_CURVE, OB_LATTICE) {
1786 give_parvert(par, ob->par1, v1);
1787 give_parvert(par, ob->par2, v2);
1788 give_parvert(par, ob->par3, v3);
1790 triatoquat(v1, v2, v3, q);
1791 QuatToMat3(q, cmat);
1792 Mat4CpyMat3(mat, cmat);
1794 if(ob->type==OB_CURVE) {
1795 VECCOPY(mat[3], v1);
1798 VecAddf(mat[3], v1, v2);
1799 VecAddf(mat[3], mat[3], v3);
1800 VecMulf(mat[3], 0.3333333f);
1805 static int no_parent_ipo=0;
1806 void set_no_parent_ipo(int val)
1811 static int during_script_flag=0;
1812 void disable_where_script(short on)
1814 during_script_flag= on;
1817 int during_script(void) {
1818 return during_script_flag;
1821 static int during_scriptlink_flag=0;
1822 void disable_where_scriptlink(short on)
1824 during_scriptlink_flag= on;
1827 int during_scriptlink(void) {
1828 return during_scriptlink_flag;
1831 void where_is_object_time(Object *ob, float ctime)
1833 float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
1834 float stime, fac1, fac2, vec[3];
1838 /* new version: correct parent+vertexparent and track+parent */
1839 /* this one only calculates direct attached parent and track */
1840 /* is faster, but should keep track of timeoffs */
1842 if(ob==NULL) return;
1844 /* this is needed to be able to grab objects with ipos, otherwise it always freezes them */
1845 stime= bsystem_time(ob, ctime, 0.0);
1846 if(stime != ob->ctime) {
1851 calc_ipo(ob->ipo, stime);
1852 execute_ipo((ID *)ob, ob->ipo);
1855 do_all_object_actions(ob);
1857 /* do constraint ipos ..., note it needs stime (0 = all ipos) */
1858 do_constraint_channels(&ob->constraints, &ob->constraintChannels, stime, 0);
1861 /* but, the drivers have to be done */
1862 if(ob->ipo) do_ob_ipodrivers(ob, ob->ipo, stime);
1863 /* do constraint ipos ..., note it needs stime (1 = only drivers ipos) */
1864 do_constraint_channels(&ob->constraints, &ob->constraintChannels, stime, 1);
1868 Object *par= ob->parent;
1870 if(ob->ipoflag & OB_OFFS_PARENT) ctime-= give_timeoffset(ob);
1872 /* hurms, code below conflicts with depgraph... (ton) */
1873 /* and even worse, it gives bad effects for NLA stride too (try ctime != par->ctime, with MBlur) */
1875 if(no_parent_ipo==0 && stime != par->ctime) {
1877 // only for ipo systems?
1878 pushdata(par, sizeof(Object));
1881 if(par->proxy_from); // was a copied matrix, no where_is! bad...
1882 else where_is_object_time(par, ctime);
1885 solve_parenting(ob, par, ob->obmat, slowmat, 0);
1891 if(ob->partype & PARSLOW) {
1892 // include framerate
1894 fac1= (1.0f/(1.0f+ fabs(give_timeoffset(ob))));
1895 if(fac1>=1.0) return;
1900 for(a=0; a<16; a++, fp1++, fp2++) {
1901 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1907 object_to_mat4(ob, ob->obmat);
1910 /* Handle tracking */
1912 if( ctime != ob->track->ctime) where_is_object_time(ob->track, ctime);
1913 solve_tracking (ob, ob->track->obmat);
1917 /* solve constraints */
1918 if (ob->constraints.first) {
1921 cob= constraints_make_evalob(ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
1923 /* constraints need ctime, not stime. Some call where_is_object_time and bsystem_time */
1924 solve_constraints (&ob->constraints, cob, ctime);
1926 constraints_clear_evalob(cob);
1928 #ifndef DISABLE_PYTHON
1929 if(ob->scriptlink.totscript && !during_script()) {
1930 if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_REDRAW);
1934 /* set negative scale flag in object */
1935 Crossf(vec, ob->obmat[0], ob->obmat[1]);
1936 if( Inpf(vec, ob->obmat[2]) < 0.0 ) ob->transflag |= OB_NEG_SCALE;
1937 else ob->transflag &= ~OB_NEG_SCALE;
1940 static void solve_parenting (Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul)
1948 object_to_mat4(ob, locmat);
1950 if(ob->partype & PARSLOW) Mat4CpyMat4(slowmat, obmat);
1953 switch(ob->partype & PARTYPE) {
1956 if(par->type==OB_CURVE) {
1957 if( ((Curve *)par->data)->flag & CU_PATH ) {
1958 ob_parcurve(ob, par, tmat);
1963 if(ok) Mat4MulSerie(totmat, par->obmat, tmat,
1964 NULL, NULL, NULL, NULL, NULL, NULL);
1965 else Mat4CpyMat4(totmat, par->obmat);
1969 ob_parbone(ob, par, tmat);
1970 Mat4MulSerie(totmat, par->obmat, tmat,
1971 NULL, NULL, NULL, NULL, NULL, NULL);
1977 VECCOPY(totmat[3], par->obmat[3]);
1980 give_parvert(par, ob->par1, vec);
1981 VecMat4MulVecfl(totmat[3], par->obmat, vec);
1985 ob_parvert3(ob, par, tmat);
1987 Mat4MulSerie(totmat, par->obmat, tmat,
1988 NULL, NULL, NULL, NULL, NULL, NULL);
1992 Mat4CpyMat4(totmat, par->obmat);
1997 Mat4MulSerie(tmat, totmat, ob->parentinv,
1998 NULL, NULL, NULL, NULL, NULL, NULL);
1999 Mat4MulSerie(obmat, tmat, locmat,
2000 NULL, NULL, NULL, NULL, NULL, NULL);
2006 // external usable originmat
2007 Mat3CpyMat4(originmat, tmat);
2009 // origin, voor help line
2010 if( (ob->partype & 15)==PARSKEL ) {
2011 VECCOPY(ob->orig, par->obmat[3]);
2014 VECCOPY(ob->orig, totmat[3]);
2019 void solve_tracking (Object *ob, float targetmat[][4])
2026 VecSubf(vec, ob->obmat[3], targetmat[3]);
2027 vectoquat(vec, ob->trackflag, ob->upflag, quat);
2028 QuatToMat3(quat, totmat);
2030 if(ob->parent && (ob->transflag & OB_POWERTRACK)) {
2031 /* 'temporal' : clear parent info */
2032 object_to_mat4(ob, tmat);
2033 tmat[0][3]= ob->obmat[0][3];
2034 tmat[1][3]= ob->obmat[1][3];
2035 tmat[2][3]= ob->obmat[2][3];
2036 tmat[3][0]= ob->obmat[3][0];
2037 tmat[3][1]= ob->obmat[3][1];
2038 tmat[3][2]= ob->obmat[3][2];
2039 tmat[3][3]= ob->obmat[3][3];
2041 else Mat4CpyMat4(tmat, ob->obmat);
2043 Mat4MulMat34(ob->obmat, totmat, tmat);
2047 void where_is_object(Object *ob)
2049 where_is_object_time(ob, (float)G.scene->r.cfra);
2053 void where_is_object_simul(Object *ob)
2054 /* was written for the old game engine (until 2.04) */
2055 /* It seems that this function is only called
2056 for a lamp that is the child of another object */
2061 float slowmat[4][4];
2067 /* no ipo! (because of dloc and realtime-ipos) */
2074 solve_parenting(ob, par, ob->obmat, slowmat, 1);
2076 if(ob->partype & PARSLOW) {
2078 fac1= (float)(1.0/(1.0+ fabs(give_timeoffset(ob))));
2082 for(a=0; a<16; a++, fp1++, fp2++) {
2083 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
2089 object_to_mat4(ob, ob->obmat);
2093 solve_tracking(ob, ob->track->obmat);
2095 /* solve constraints */
2096 if (ob->constraints.first) {
2099 cob= constraints_make_evalob(ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2100 solve_constraints (&ob->constraints, cob, G.scene->r.cfra);
2101 constraints_clear_evalob(cob);
2108 /* for calculation of the inverse parent transform, only used for editor */
2109 void what_does_parent(Object *ob)
2112 Mat4One(workob.obmat);
2113 Mat4One(workob.parentinv);
2114 Mat4One(workob.constinv);
2115 workob.parent= ob->parent;
2116 workob.track= ob->track;
2118 workob.trackflag= ob->trackflag;
2119 workob.upflag= ob->upflag;
2121 workob.partype= ob->partype;
2122 workob.par1= ob->par1;
2123 workob.par2= ob->par2;
2124 workob.par3= ob->par3;
2126 workob.constraints.first = ob->constraints.first;
2127 workob.constraints.last = ob->constraints.last;
2129 strcpy(workob.parsubstr, ob->parsubstr);
2131 where_is_object(&workob);
2134 BoundBox *unit_boundbox()
2137 float min[3] = {-1,-1,-1}, max[3] = {-1,-1,-1};
2139 bb= MEM_callocN(sizeof(BoundBox), "bb");
2140 boundbox_set_from_min_max(bb, min, max);
2145 void boundbox_set_from_min_max(BoundBox *bb, float min[3], float max[3])
2147 bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
2148 bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
2150 bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
2151 bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
2153 bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
2154 bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
2157 BoundBox *object_get_boundbox(Object *ob)
2161 if(ob->type==OB_MESH) {
2162 bb = mesh_get_bb(ob);
2164 else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) {
2165 bb= ( (Curve *)ob->data )->bb;
2167 else if(ob->type==OB_MBALL) {
2173 /* used to temporally disable/enable boundbox */
2174 void object_boundbox_flag(Object *ob, int flag, int set)
2176 BoundBox *bb= object_get_boundbox(ob);
2178 if(set) bb->flag |= flag;
2179 else bb->flag &= ~flag;
2183 void minmax_object(Object *ob, float *min, float *max)
2198 if(cu->bb==NULL) tex_space_curve(cu);
2201 for(a=0; a<8; a++) {
2202 Mat4MulVecfl(ob->obmat, bb.vec[a]);
2203 DO_MINMAX(bb.vec[a], min, max);
2208 bPoseChannel *pchan;
2209 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2210 VECCOPY(vec, pchan->pose_head);
2211 Mat4MulVecfl(ob->obmat, vec);
2212 DO_MINMAX(vec, min, max);
2213 VECCOPY(vec, pchan->pose_tail);
2214 Mat4MulVecfl(ob->obmat, vec);
2215 DO_MINMAX(vec, min, max);
2219 /* no break, get_mesh will give NULL and it passes on to default */
2224 bb = *mesh_get_bb(ob);
2226 for(a=0; a<8; a++) {
2227 Mat4MulVecfl(ob->obmat, bb.vec[a]);
2228 DO_MINMAX(bb.vec[a], min, max);
2231 if(min[0] < max[0] ) break;
2233 /* else here no break!!!, mesh can be zero sized */
2236 DO_MINMAX(ob->obmat[3], min, max);
2238 VECCOPY(vec, ob->obmat[3]);
2239 VecAddf(vec, vec, ob->size);
2240 DO_MINMAX(vec, min, max);
2242 VECCOPY(vec, ob->obmat[3]);
2243 VecSubf(vec, vec, ob->size);
2244 DO_MINMAX(vec, min, max);
2249 /* TODO - use dupli objects bounding boxes */
2250 void minmax_object_duplis(Object *ob, float *min, float *max)
2252 if ((ob->transflag & OB_DUPLI)==0) {
2258 lb= object_duplilist(G.scene, ob);
2259 for(dob= lb->first; dob; dob= dob->next) {
2262 /* should really use bound box of dup object */
2263 DO_MINMAX(dob->mat[3], min, max);
2266 free_object_duplilist(lb); /* does restore */
2271 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
2272 /* local_object->proxy == pointer to library object, saved in files and read */
2274 /* function below is polluted with proxy exceptions, cleanup will follow! */
2276 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
2277 /* requires flags to be set! */
2278 void object_handle_update(Object *ob)
2280 if(ob->recalc & OB_RECALC) {
2282 if(ob->recalc & OB_RECALC_OB) {
2284 // printf("recalcob %s\n", ob->id.name+2);
2286 /* handle proxy copy for target */
2287 if(ob->id.lib && ob->proxy_from) {
2288 // printf("ob proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2289 if(ob->proxy_from->proxy_group) {/* transform proxy into group space */
2290 Object *obg= ob->proxy_from->proxy_group;
2291 Mat4Invert(obg->imat, obg->obmat);
2292 Mat4MulMat4(ob->obmat, ob->proxy_from->obmat, obg->imat);
2295 Mat4CpyMat4(ob->obmat, ob->proxy_from->obmat);
2298 where_is_object(ob);
2299 #ifndef DISABLE_PYTHON
2300 if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_OBJECTUPDATE);
2304 if(ob->recalc & OB_RECALC_DATA) {
2306 // printf("recalcdata %s\n", ob->id.name+2);
2308 /* includes all keys and modifiers */
2309 if(ob->type==OB_MESH) {
2310 makeDerivedMesh(ob, get_viewedit_datamask());
2312 else if(ob->type==OB_MBALL) {
2313 makeDispListMBall(ob);
2315 else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2316 makeDispListCurveTypes(ob, 0);
2318 else if(ob->type==OB_LATTICE) {
2319 lattice_calc_modifiers(ob);
2321 else if(ob->type==OB_CAMERA) {
2322 Camera *cam = (Camera *)ob->data;
2323 calc_ipo(cam->ipo, frame_to_float(G.scene->r.cfra));
2324 execute_ipo(&cam->id, cam->ipo);
2326 else if(ob->type==OB_LAMP) {
2327 Lamp *la = (Lamp *)ob->data;
2328 calc_ipo(la->ipo, frame_to_float(G.scene->r.cfra));
2329 execute_ipo(&la->id, la->ipo);
2331 else if(ob->type==OB_ARMATURE) {
2332 /* this happens for reading old files and to match library armatures with poses */
2333 if(ob->pose==NULL || (ob->pose->flag & POSE_RECALC))
2334 armature_rebuild_pose(ob, ob->data);
2336 if(ob->id.lib && ob->proxy_from) {
2337 copy_pose_result(ob->pose, ob->proxy_from->pose);
2338 // printf("pose proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2341 do_all_pose_actions(ob);
2346 if(ob->particlesystem.first) {
2347 ParticleSystem *tpsys, *psys;
2350 psys= ob->particlesystem.first;
2352 if(psys_check_enabled(ob, psys)) {
2353 particle_system_update(ob, psys);
2356 else if(psys->flag & PSYS_DELETE) {
2358 BLI_remlink(&ob->particlesystem, psys);
2366 if(G.rendering && ob->transflag & OB_DUPLIPARTS) {
2367 /* this is to make sure we get render level duplis in groups:
2368 * the derivedmesh must be created before init_render_mesh,
2369 * since object_duplilist does dupliparticles before that */
2370 dm = mesh_create_derived_render(ob, CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL);
2373 for(psys=ob->particlesystem.first; psys; psys=psys->next)
2374 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
2377 #ifndef DISABLE_PYTHON
2378 if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_OBDATAUPDATE);
2382 /* the no-group proxy case, we call update */
2383 if(ob->proxy && ob->proxy_group==NULL) {
2384 /* set pointer in library proxy target, for copying, but restore it */
2385 ob->proxy->proxy_from= ob;
2386 // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
2387 object_handle_update(ob->proxy);
2390 ob->recalc &= ~OB_RECALC;
2393 /* the case when this is a group proxy, object_update is called in group.c */
2395 ob->proxy->proxy_from= ob;
2396 // printf("set proxy pointer for later group stuff %s\n", ob->id.name);
2400 float give_timeoffset(Object *ob) {
2401 if ((ob->ipoflag & OB_OFFS_PARENTADD) && ob->parent) {
2402 return ob->sf + give_timeoffset(ob->parent);
2408 int give_obdata_texspace(Object *ob, int **texflag, float **loc, float **size, float **rot) {
2413 switch (GS(((ID *)ob->data)->name)) {
2417 if (texflag) *texflag = &me->texflag;
2418 if (loc) *loc = me->loc;
2419 if (size) *size = me->size;
2420 if (rot) *rot = me->rot;
2425 Curve *cu= ob->data;
2426 if (texflag) *texflag = &cu->texflag;
2427 if (loc) *loc = cu->loc;
2428 if (size) *size = cu->size;
2429 if (rot) *rot = cu->rot;
2434 MetaBall *mb= ob->data;
2435 if (texflag) *texflag = &mb->texflag;
2436 if (loc) *loc = mb->loc;
2437 if (size) *size = mb->size;
2438 if (rot) *rot = mb->rot;
2448 * Test a bounding box for ray intersection
2449 * assumes the ray is already local to the boundbox space
2451 int ray_hit_boundbox(struct BoundBox *bb, float ray_start[3], float ray_normal[3])
2453 static int triangle_indexes[12][3] = {{0, 1, 2}, {0, 2, 3},
2454 {3, 2, 6}, {3, 6, 7},
2455 {1, 2, 6}, {1, 6, 5},
2456 {5, 6, 7}, {4, 5, 7},
2457 {0, 3, 7}, {0, 4, 7},
2458 {0, 1, 5}, {0, 4, 5}};
2462 for (i = 0; i < 12 && result == 0; i++)
2466 v1 = triangle_indexes[i][0];
2467 v2 = triangle_indexes[i][1];
2468 v3 = triangle_indexes[i][2];
2469 result = RayIntersectsTriangle(ray_start, ray_normal, bb->vec[v1], bb->vec[v2], bb->vec[v3], &lambda, NULL);