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_anim_types.h"
43 #include "DNA_action_types.h"
44 #include "DNA_armature_types.h"
45 #include "DNA_camera_types.h"
46 #include "DNA_constraint_types.h"
47 #include "DNA_curve_types.h"
48 #include "DNA_group_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_outliner_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 "BLI_blenlib.h"
72 #include "BLI_arithb.h"
73 #include "BLI_editVert.h"
75 #include "BKE_utildefines.h"
78 #include "BKE_global.h"
80 #include "BKE_armature.h"
81 #include "BKE_action.h"
82 #include "BKE_bullet.h"
83 #include "BKE_colortools.h"
84 #include "BKE_deform.h"
85 #include "BKE_DerivedMesh.h"
87 #include "BKE_animsys.h"
89 #include "BKE_blender.h"
90 #include "BKE_constraint.h"
91 #include "BKE_curve.h"
92 #include "BKE_displist.h"
93 #include "BKE_group.h"
94 #include "BKE_icons.h"
96 #include "BKE_lattice.h"
97 #include "BKE_library.h"
99 #include "BKE_mball.h"
100 #include "BKE_modifier.h"
101 #include "BKE_object.h"
102 #include "BKE_particle.h"
103 #include "BKE_pointcache.h"
104 #include "BKE_property.h"
106 #include "BKE_scene.h"
107 #include "BKE_screen.h"
108 #include "BKE_softbody.h"
110 #include "LBM_fluidsim.h"
112 #ifndef DISABLE_PYTHON
113 #include "BPY_extern.h"
116 #include "GPU_material.h"
118 /* Local function protos */
119 static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul);
121 float originmat[3][3]; /* after where_is_object(), can be used in other functions (bad!) */
123 void clear_workob(Object *workob)
125 memset(workob, 0, sizeof(Object));
127 workob->size[0]= workob->size[1]= workob->size[2]= 1.0;
131 void copy_baseflags(struct Scene *scene)
133 Base *base= scene->base.first;
136 base->object->flag= base->flag;
141 void copy_objectflags(struct Scene *scene)
143 Base *base= scene->base.first;
146 base->flag= base->object->flag;
151 void update_base_layer(struct Scene *scene, Object *ob)
153 Base *base= scene->base.first;
156 if (base->object == ob) base->lay= ob->lay;
161 void object_free_particlesystems(Object *ob)
163 while(ob->particlesystem.first){
164 ParticleSystem *psys = ob->particlesystem.first;
166 BLI_remlink(&ob->particlesystem,psys);
172 void object_free_softbody(Object *ob)
180 void object_free_bulletsoftbody(Object *ob)
188 void object_free_modifiers(Object *ob)
190 while (ob->modifiers.first) {
191 ModifierData *md = ob->modifiers.first;
193 BLI_remlink(&ob->modifiers, md);
198 /* particle modifiers were freed, so free the particlesystems as well */
199 object_free_particlesystems(ob);
201 /* same for softbody */
202 object_free_softbody(ob);
205 /* here we will collect all local displist stuff */
206 /* also (ab)used in depsgraph */
207 void object_free_display(Object *ob)
209 if(ob->derivedDeform) {
210 ob->derivedDeform->needsFree = 1;
211 ob->derivedDeform->release(ob->derivedDeform);
212 ob->derivedDeform= NULL;
214 if(ob->derivedFinal) {
215 ob->derivedFinal->needsFree = 1;
216 ob->derivedFinal->release(ob->derivedFinal);
217 ob->derivedFinal= NULL;
220 freedisplist(&ob->disp);
223 /* do not free object itself */
224 void free_object(Object *ob)
228 object_free_display(ob);
230 /* disconnect specific data */
235 if(ob->type==OB_MESH) unlink_mesh(ob->data);
236 else if(ob->type==OB_CURVE) unlink_curve(ob->data);
237 else if(ob->type==OB_MBALL) unlink_mball(ob->data);
242 for(a=0; a<ob->totcol; a++) {
243 if(ob->mat[a]) ob->mat[a]->id.us--;
245 if(ob->mat) MEM_freeN(ob->mat);
247 if(ob->bb) MEM_freeN(ob->bb);
249 if(ob->path) free_path(ob->path);
251 if(ob->adt) BKE_free_animdata((ID *)ob);
252 if(ob->poselib) ob->poselib->id.us--;
253 if(ob->dup_group) ob->dup_group->id.us--;
254 if(ob->defbase.first)
255 BLI_freelistN(&ob->defbase);
258 free_properties(&ob->prop);
259 object_free_modifiers(ob);
261 free_sensors(&ob->sensors);
262 free_controllers(&ob->controllers);
263 free_actuators(&ob->actuators);
265 free_constraints(&ob->constraints);
267 #ifndef DISABLE_PYTHON
268 BPY_free_scriptlink(&ob->scriptlink);
273 ob->pd->tex->id.us--;
276 if(ob->soft) sbFree(ob->soft);
277 if(ob->bsoft) bsbFree(ob->bsoft);
278 if(ob->gpulamp.first) GPU_lamp_free(ob);
281 static void unlink_object__unlinkModifierLinks(void *userData, Object *ob, Object **obpoin)
283 Object *unlinkOb = userData;
285 if (*obpoin==unlinkOb) {
287 ob->recalc |= OB_RECALC;
290 void unlink_object(Scene *scene, Object *ob)
302 //bActionStrip *strip; // XXX animsys
306 unlink_controllers(&ob->controllers);
307 unlink_actuators(&ob->actuators);
309 /* check all objects: parents en bevels and fields, also from libraries */
310 obt= G.main->object.first;
314 if(obt->proxy_from==ob) {
315 obt->proxy_from= NULL;
316 obt->recalc |= OB_RECALC_OB;
318 if(obt->proxy_group==ob)
319 obt->proxy_group= NULL;
321 if(obt->parent==ob) {
323 obt->recalc |= OB_RECALC;
328 obt->recalc |= OB_RECALC_OB;
331 modifiers_foreachObjectLink(obt, unlink_object__unlinkModifierLinks, ob);
333 if ELEM(obt->type, OB_CURVE, OB_FONT) {
338 obt->recalc |= OB_RECALC;
340 if(cu->taperobj==ob) {
342 obt->recalc |= OB_RECALC;
344 if(cu->textoncurve==ob) {
345 cu->textoncurve= NULL;
346 obt->recalc |= OB_RECALC;
349 else if(obt->type==OB_ARMATURE && obt->pose) {
351 for(pchan= obt->pose->chanbase.first; pchan; pchan= pchan->next) {
352 for (con = pchan->constraints.first; con; con=con->next) {
353 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
354 ListBase targets = {NULL, NULL};
355 bConstraintTarget *ct;
357 if (cti && cti->get_constraint_targets) {
358 cti->get_constraint_targets(con, &targets);
360 for (ct= targets.first; ct; ct= ct->next) {
363 strcpy(ct->subtarget, "");
364 obt->recalc |= OB_RECALC_DATA;
368 if (cti->flush_constraint_targets)
369 cti->flush_constraint_targets(con, &targets, 0);
372 if(pchan->custom==ob)
377 sca_remove_ob_poin(obt, ob);
379 for (con = obt->constraints.first; con; con=con->next) {
380 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
381 ListBase targets = {NULL, NULL};
382 bConstraintTarget *ct;
384 if (cti && cti->get_constraint_targets) {
385 cti->get_constraint_targets(con, &targets);
387 for (ct= targets.first; ct; ct= ct->next) {
390 strcpy(ct->subtarget, "");
391 obt->recalc |= OB_RECALC_DATA;
395 if (cti->flush_constraint_targets)
396 cti->flush_constraint_targets(con, &targets, 0);
400 /* object is deflector or field */
403 obt->recalc |= OB_RECALC_DATA;
406 for(md=obt->modifiers.first; md; md=md->next)
407 if(md->type == eModifierType_Cloth)
408 obt->recalc |= OB_RECALC_DATA;
412 #if 0 // XXX old animation system
413 for(strip= obt->nlastrips.first; strip; strip= strip->next) {
414 if(strip->object==ob)
417 if(strip->modifiers.first) {
418 bActionModifier *amod;
419 for(amod= strip->modifiers.first; amod; amod= amod->next)
424 #endif // XXX old animation system
426 /* particle systems */
427 if(obt->particlesystem.first) {
428 ParticleSystem *tpsys= obt->particlesystem.first;
429 for(; tpsys; tpsys=tpsys->next) {
430 if(tpsys->keyed_ob==ob) {
431 ParticleSystem *psys= BLI_findlink(&ob->particlesystem,tpsys->keyed_psys-1);
433 if(psys && psys->keyed_ob) {
434 tpsys->keyed_ob= psys->keyed_ob;
435 tpsys->keyed_psys= psys->keyed_psys;
438 tpsys->keyed_ob= NULL;
440 obt->recalc |= OB_RECALC_DATA;
443 if(tpsys->target_ob==ob) {
444 tpsys->target_ob= NULL;
445 obt->recalc |= OB_RECALC_DATA;
448 if(tpsys->part->dup_ob==ob)
449 tpsys->part->dup_ob= NULL;
451 if(tpsys->part->flag&PART_STICKY) {
455 for(p=0,pa=tpsys->particles; p<tpsys->totpart; p++,pa++) {
456 if(pa->stick_ob==ob) {
458 pa->flag &= ~PARS_STICKY;
464 obt->recalc |= OB_RECALC_DATA;
471 mat= G.main->mat.first;
474 for(a=0; a<MAX_MTEX; a++) {
475 if(mat->mtex[a] && ob==mat->mtex[a]->object) {
476 /* actually, test for lib here... to do */
477 mat->mtex[a]->object= NULL;
485 tex= G.main->tex.first;
488 if(tex->env->object == ob) tex->env->object= NULL;
493 /* mballs (scene==NULL when called from library.c) */
494 if(scene && ob->type==OB_MBALL) {
495 obt= find_basis_mball(scene, ob);
496 if(obt) freedisplist(&obt->disp);
500 wrld= G.main->world.first;
502 if(wrld->id.lib==NULL) {
503 for(a=0; a<MAX_MTEX; a++) {
504 if(wrld->mtex[a] && ob==wrld->mtex[a]->object)
505 wrld->mtex[a]->object= NULL;
513 sce= G.main->scene.first;
515 if(sce->id.lib==NULL) {
516 if(sce->camera==ob) sce->camera= NULL;
517 if(sce->toolsettings->skgen_template==ob) sce->toolsettings->skgen_template = NULL;
522 #if 0 // XXX old animation system
524 ipo= G.main->ipo.first;
526 if(ipo->id.lib==NULL) {
528 for(icu= ipo->curve.first; icu; icu= icu->next) {
529 if(icu->driver && icu->driver->ob==ob)
530 icu->driver->ob= NULL;
535 #endif // XXX old animation system
538 sc= G.main->screen.first;
540 ScrArea *sa= sc->areabase.first;
544 for (sl= sa->spacedata.first; sl; sl= sl->next) {
545 if(sl->spacetype==SPACE_VIEW3D) {
546 View3D *v3d= (View3D*) sl;
548 if(v3d->camera==ob) {
550 // XXX if(v3d->persp==V3D_CAMOB) v3d->persp= V3D_PERSP;
552 if(v3d->localvd && v3d->localvd->camera==ob ) {
553 v3d->localvd->camera= NULL;
554 // XXX if(v3d->localvd->persp==V3D_CAMOB) v3d->localvd->persp= V3D_PERSP;
557 else if(sl->spacetype==SPACE_OUTLINER) {
558 SpaceOops *so= (SpaceOops *)sl;
561 TreeStoreElem *tselem= so->treestore->data;
563 for(a=0; a<so->treestore->usedelem; a++, tselem++) {
564 if(tselem->id==(ID *)ob) tselem->id= NULL;
576 group= G.main->group.first;
578 rem_from_group(group, ob);
579 group= group->id.next;
583 camera= G.main->camera.first;
585 if (camera->dof_ob==ob) {
586 camera->dof_ob = NULL;
588 camera= camera->id.next;
592 int exist_object(Object *obtest)
596 if(obtest==NULL) return 0;
598 ob= G.main->object.first;
600 if(ob==obtest) return 1;
606 void *add_camera(char *name)
610 cam= alloc_libblock(&G.main->camera, ID_CA, name);
615 cam->clipend= 100.0f;
617 cam->ortho_scale= 6.0;
618 cam->flag |= CAM_SHOWTITLESAFE;
619 cam->passepartalpha = 0.2f;
624 Camera *copy_camera(Camera *cam)
628 camn= copy_libblock(cam);
629 camn->adt= BKE_copy_animdata(cam->adt);
631 #ifndef DISABLE_PYTHON
632 BPY_copy_scriptlink(&camn->scriptlink);
639 void make_local_camera(Camera *cam)
645 /* - only lib users: do nothing
646 * - only local users: set flag
650 if(cam->id.lib==0) return;
653 cam->id.flag= LIB_LOCAL;
654 new_id(0, (ID *)cam, 0);
658 ob= G.main->object.first;
661 if(ob->id.lib) lib= 1;
667 if(local && lib==0) {
669 cam->id.flag= LIB_LOCAL;
670 new_id(0, (ID *)cam, 0);
672 else if(local && lib) {
673 camn= copy_camera(cam);
676 ob= G.main->object.first;
691 /* get the camera's dof value, takes the dof object into account */
692 float dof_camera(Object *ob)
694 Camera *cam = (Camera *)ob->data;
695 if (ob->type != OB_CAMERA)
698 /* too simple, better to return the distance on the view axis only
699 * return VecLenf(ob->obmat[3], cam->dof_ob->obmat[3]); */
700 float mat[4][4], obmat[4][4];
702 Mat4CpyMat4(obmat, ob->obmat);
704 Mat4Invert(ob->imat, obmat);
705 Mat4MulMat4(mat, cam->dof_ob->obmat, ob->imat);
706 return (float)fabs(mat[3][2]);
708 return cam->YF_dofdist;
711 void *add_lamp(char *name)
715 la= alloc_libblock(&G.main->lamp, ID_LA, name);
717 la->r= la->g= la->b= la->k= 1.0f;
718 la->haint= la->energy= 1.0f;
721 la->spotblend= 0.15f;
723 la->mode= LA_SHAD_BUF;
727 la->shadspotsize= 45.0f;
731 la->ray_samp= la->ray_sampy= la->ray_sampz= 1;
732 la->area_size=la->area_sizey=la->area_sizez= 1.0f;
734 la->buftype= LA_SHADBUF_HALFWAY;
735 la->ray_samp_method = LA_SAMP_HALTON;
736 la->adapt_thresh = 0.001f;
738 la->falloff_type = LA_FALLOFF_INVLINEAR;
739 la->curfalloff = curvemapping_add(1, 0.0f, 1.0f, 1.0f, 0.0f);
740 la->sun_effect_type = 0;
741 la->horizon_brightness = 1.0;
743 la->sun_brightness = 1.0;
745 la->backscattered_light = 1.0f;
746 la->atm_turbidity = 2.0f;
747 la->atm_inscattering_factor = 1.0f;
748 la->atm_extinction_factor = 1.0f;
749 la->atm_distance_factor = 1.0f;
750 la->sun_intensity = 1.0f;
751 la->skyblendtype= MA_RAMP_ADD;
752 la->skyblendfac= 1.0f;
753 la->sky_colorspace= BLI_CS_CIE;
754 la->sky_exposure= 1.0f;
756 curvemapping_initialize(la->curfalloff);
760 Lamp *copy_lamp(Lamp *la)
765 lan= copy_libblock(la);
767 for(a=0; a<MAX_MTEX; a++) {
769 lan->mtex[a]= MEM_mallocN(sizeof(MTex), "copylamptex");
770 memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
771 id_us_plus((ID *)lan->mtex[a]->tex);
775 lan->curfalloff = curvemapping_copy(la->curfalloff);
777 #if 0 // XXX old animation system
778 id_us_plus((ID *)lan->ipo);
779 #endif // XXX old animation system
781 if (la->preview) lan->preview = BKE_previewimg_copy(la->preview);
782 #ifndef DISABLE_PYTHON
783 BPY_copy_scriptlink(&la->scriptlink);
788 void make_local_lamp(Lamp *la)
794 /* - only lib users: do nothing
795 * - only local users: set flag
799 if(la->id.lib==0) return;
802 la->id.flag= LIB_LOCAL;
803 new_id(0, (ID *)la, 0);
807 ob= G.main->object.first;
810 if(ob->id.lib) lib= 1;
816 if(local && lib==0) {
818 la->id.flag= LIB_LOCAL;
819 new_id(0, (ID *)la, 0);
821 else if(local && lib) {
825 ob= G.main->object.first;
840 void free_camera(Camera *ca)
842 #ifndef DISABLE_PYTHON
843 BPY_free_scriptlink(&ca->scriptlink);
845 BKE_free_animdata((ID *)ca);
848 void free_lamp(Lamp *la)
854 #ifndef DISABLE_PYTHON
855 BPY_free_scriptlink(&la->scriptlink);
858 for(a=0; a<MAX_MTEX; a++) {
860 if(mtex && mtex->tex) mtex->tex->id.us--;
861 if(mtex) MEM_freeN(mtex);
864 BKE_free_animdata((ID *)la);
866 curvemapping_free(la->curfalloff);
868 BKE_previewimg_free(&la->preview);
869 BKE_icon_delete(&la->id);
879 /* *************************************************** */
881 static void *add_obdata_from_type(int type)
884 case OB_MESH: return add_mesh("Mesh");
885 case OB_CURVE: return add_curve("Curve", OB_CURVE);
886 case OB_SURF: return add_curve("Surf", OB_SURF);
887 case OB_FONT: return add_curve("Text", OB_FONT);
888 case OB_MBALL: return add_mball("Meta");
889 case OB_CAMERA: return add_camera("Camera");
890 case OB_LAMP: return add_lamp("Lamp");
891 case OB_LATTICE: return add_lattice("Lattice");
892 case OB_WAVE: return add_wave();
893 case OB_ARMATURE: return add_armature("Armature");
894 case OB_EMPTY: return NULL;
896 printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
901 static char *get_obdata_defname(int type)
904 case OB_MESH: return "Mesh";
905 case OB_CURVE: return "Curve";
906 case OB_SURF: return "Surf";
907 case OB_FONT: return "Font";
908 case OB_MBALL: return "Mball";
909 case OB_CAMERA: return "Camera";
910 case OB_LAMP: return "Lamp";
911 case OB_LATTICE: return "Lattice";
912 case OB_WAVE: return "Wave";
913 case OB_ARMATURE: return "Armature";
914 case OB_EMPTY: return "Empty";
916 printf("get_obdata_defname: Internal error, bad type: %d\n", type);
921 /* more general add: creates minimum required data, but without vertices etc. */
922 Object *add_only_object(int type, char *name)
926 ob= alloc_libblock(&G.main->object, ID_OB, name);
928 /* default object vars */
930 /* ob->transflag= OB_QUAT; */
932 #if 0 /* not used yet */
937 ob->col[0]= ob->col[1]= ob->col[2]= 1.0;
940 ob->loc[0]= ob->loc[1]= ob->loc[2]= 0.0;
941 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
942 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
944 Mat4One(ob->constinv);
945 Mat4One(ob->parentinv);
948 if(U.flag & USER_MAT_ON_OB) ob->colbits= -1;
949 ob->empty_drawtype= OB_ARROWS;
950 ob->empty_drawsize= 1.0;
952 if(type==OB_CAMERA || type==OB_LAMP) {
953 ob->trackflag= OB_NEGZ;
957 ob->trackflag= OB_POSY;
961 #if 0 // XXX old animation system
962 ob->ipoflag = OB_OFFS_OB+OB_OFFS_PARENT;
963 ob->ipowin= ID_OB; /* the ipowin shown */
964 #endif // XXX old animation system
966 ob->dupon= 1; ob->dupoff= 0;
967 ob->dupsta= 1; ob->dupend= 100;
968 ob->dupfacesca = 1.0;
970 /* Game engine defaults*/
971 ob->mass= ob->inertia= 1.0f;
972 ob->formfactor= 0.4f;
975 ob->anisotropicFriction[0] = 1.0f;
976 ob->anisotropicFriction[1] = 1.0f;
977 ob->anisotropicFriction[2] = 1.0f;
978 ob->gameflag= OB_PROP|OB_COLLISION;
981 /* NT fluid sim defaults */
982 ob->fluidsimFlag = 0;
983 ob->fluidsimSettings = NULL;
988 /* general add: to scene, with layer from area and default name */
989 /* creates minimum required data, but without vertices etc. */
990 Object *add_object(struct Scene *scene, int type)
996 strcpy(name, get_obdata_defname(type));
997 ob = add_only_object(type, name);
999 ob->data= add_obdata_from_type(type);
1001 ob->lay= scene->lay;
1003 base= scene_add_base(scene, ob);
1004 scene_select_base(scene, base);
1005 ob->recalc |= OB_RECALC;
1010 SoftBody *copy_softbody(SoftBody *sb)
1014 if (sb==NULL) return(NULL);
1016 sbn= MEM_dupallocN(sb);
1017 sbn->totspring= sbn->totpoint= 0;
1022 sbn->totkey= sbn->totpointkey= 0;
1026 sbn->pointcache= BKE_ptcache_copy(sb->pointcache);
1031 BulletSoftBody *copy_bulletsoftbody(BulletSoftBody *bsb)
1033 BulletSoftBody *bsbn;
1037 bsbn = MEM_dupallocN(bsb);
1038 /* no pointer in this structure yet */
1042 ParticleSystem *copy_particlesystem(ParticleSystem *psys)
1044 ParticleSystem *psysn;
1048 psysn= MEM_dupallocN(psys);
1049 psysn->particles= MEM_dupallocN(psys->particles);
1050 psysn->child= MEM_dupallocN(psys->child);
1052 for(a=0, pa=psysn->particles; a<psysn->totpart; a++, pa++) {
1054 pa->hair= MEM_dupallocN(pa->hair);
1056 pa->keys= MEM_dupallocN(pa->keys);
1060 psysn->soft= copy_softbody(psys->soft);
1061 psysn->soft->particles = psysn;
1064 psysn->pathcache= NULL;
1065 psysn->childcache= NULL;
1067 psysn->effectors.first= psysn->effectors.last= 0;
1069 psysn->pathcachebufs.first = psysn->pathcachebufs.last = NULL;
1070 psysn->childcachebufs.first = psysn->childcachebufs.last = NULL;
1071 psysn->reactevents.first = psysn->reactevents.last = NULL;
1072 psysn->renderdata = NULL;
1074 psysn->pointcache= BKE_ptcache_copy(psys->pointcache);
1076 id_us_plus((ID *)psysn->part);
1081 void copy_object_particlesystems(Object *obn, Object *ob)
1083 ParticleSystemModifierData *psmd;
1084 ParticleSystem *psys, *npsys;
1087 obn->particlesystem.first= obn->particlesystem.last= NULL;
1088 for(psys=ob->particlesystem.first; psys; psys=psys->next) {
1089 npsys= copy_particlesystem(psys);
1091 BLI_addtail(&obn->particlesystem, npsys);
1093 /* need to update particle modifiers too */
1094 for(md=obn->modifiers.first; md; md=md->next) {
1095 if(md->type==eModifierType_ParticleSystem) {
1096 psmd= (ParticleSystemModifierData*)md;
1097 if(psmd->psys==psys)
1104 void copy_object_softbody(Object *obn, Object *ob)
1107 obn->soft= copy_softbody(ob->soft);
1110 static void copy_object_pose(Object *obn, Object *ob)
1114 /* note: need to clear obn->pose pointer first, so that copy_pose works (otherwise there's a crash) */
1116 copy_pose(&obn->pose, ob->pose, 1); /* 1 = copy constraints */
1118 for (chan = obn->pose->chanbase.first; chan; chan=chan->next){
1121 chan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
1123 for (con= chan->constraints.first; con; con= con->next) {
1124 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1125 ListBase targets = {NULL, NULL};
1126 bConstraintTarget *ct;
1128 #if 0 // XXX old animation system
1129 /* note that we can't change lib linked ipo blocks. for making
1130 * proxies this still works correct however because the object
1131 * is changed to object->proxy_from when evaluating the driver. */
1132 if(con->ipo && !con->ipo->id.lib) {
1134 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
1135 if(icu->driver && icu->driver->ob==ob)
1136 icu->driver->ob= obn;
1139 #endif // XXX old animation system
1141 if (cti && cti->get_constraint_targets) {
1142 cti->get_constraint_targets(con, &targets);
1144 for (ct= targets.first; ct; ct= ct->next) {
1149 if (cti->flush_constraint_targets)
1150 cti->flush_constraint_targets(con, &targets, 0);
1156 Object *copy_object(Object *ob)
1162 obn= copy_libblock(ob);
1165 obn->mat= MEM_dupallocN(ob->mat);
1168 if(ob->bb) obn->bb= MEM_dupallocN(ob->bb);
1170 obn->flag &= ~OB_FROMGROUP;
1172 obn->modifiers.first = obn->modifiers.last= NULL;
1174 for (md=ob->modifiers.first; md; md=md->next) {
1175 ModifierData *nmd = modifier_new(md->type);
1176 modifier_copyData(md, nmd);
1177 BLI_addtail(&obn->modifiers, nmd);
1179 #ifndef DISABLE_PYTHON
1180 BPY_copy_scriptlink(&ob->scriptlink);
1182 obn->prop.first = obn->prop.last = NULL;
1183 copy_properties(&obn->prop, &ob->prop);
1185 copy_sensors(&obn->sensors, &ob->sensors);
1186 copy_controllers(&obn->controllers, &ob->controllers);
1187 copy_actuators(&obn->actuators, &ob->actuators);
1190 copy_object_pose(obn, ob);
1191 /* backwards compat... non-armatures can get poses in older files? */
1192 if(ob->type==OB_ARMATURE)
1193 armature_rebuild_pose(obn, obn->data);
1195 copy_defgroups(&obn->defbase, &ob->defbase);
1196 #if 0 // XXX old animation system
1197 copy_nlastrips(&obn->nlastrips, &ob->nlastrips);
1198 #endif // XXX old animation system
1199 copy_constraints(&obn->constraints, &ob->constraints);
1201 /* increase user numbers */
1202 id_us_plus((ID *)obn->data);
1203 #if 0 // XXX old animation system
1204 id_us_plus((ID *)obn->ipo);
1205 id_us_plus((ID *)obn->action);
1206 #endif // XXX old animation system
1207 id_us_plus((ID *)obn->dup_group);
1209 for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
1211 obn->disp.first= obn->disp.last= NULL;
1214 obn->pd= MEM_dupallocN(ob->pd);
1216 id_us_plus(&(obn->pd->tex->id));
1218 obn->soft= copy_softbody(ob->soft);
1219 obn->bsoft = copy_bulletsoftbody(ob->bsoft);
1221 copy_object_particlesystems(obn, ob);
1223 obn->derivedDeform = NULL;
1224 obn->derivedFinal = NULL;
1226 obn->gpulamp.first = obn->gpulamp.last = NULL;
1231 void expand_local_object(Object *ob)
1233 //bActionStrip *strip;
1234 ParticleSystem *psys;
1237 #if 0 // XXX old animation system
1238 id_lib_extern((ID *)ob->action);
1239 id_lib_extern((ID *)ob->ipo);
1240 #endif // XXX old animation system
1241 id_lib_extern((ID *)ob->data);
1242 id_lib_extern((ID *)ob->dup_group);
1244 for(a=0; a<ob->totcol; a++) {
1245 id_lib_extern((ID *)ob->mat[a]);
1247 #if 0 // XXX old animation system
1248 for (strip=ob->nlastrips.first; strip; strip=strip->next) {
1249 id_lib_extern((ID *)strip->act);
1251 #endif // XXX old animation system
1252 for(psys=ob->particlesystem.first; psys; psys=psys->next)
1253 id_lib_extern((ID *)psys->part);
1256 void make_local_object(Object *ob)
1263 /* - only lib users: do nothing
1264 * - only local users: set flag
1265 * - mixed: make copy
1268 if(ob->id.lib==NULL) return;
1270 ob->proxy= ob->proxy_from= NULL;
1274 ob->id.flag= LIB_LOCAL;
1275 new_id(0, (ID *)ob, 0);
1279 sce= G.main->scene.first;
1281 base= sce->base.first;
1283 if(base->object==ob) {
1284 if(sce->id.lib) lib++;
1293 if(local && lib==0) {
1295 ob->id.flag= LIB_LOCAL;
1296 new_id(0, (ID *)ob, 0);
1298 else if(local && lib) {
1299 obn= copy_object(ob);
1302 sce= G.main->scene.first;
1304 if(sce->id.lib==0) {
1305 base= sce->base.first;
1307 if(base->object==ob) {
1320 expand_local_object(ob);
1323 /* *************** PROXY **************** */
1325 /* when you make proxy, ensure the exposed layers are extern */
1326 void armature_set_id_extern(Object *ob)
1328 bArmature *arm= ob->data;
1329 bPoseChannel *pchan;
1330 int lay= arm->layer_protected;
1332 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1333 if(!(pchan->bone->layer & lay))
1334 id_lib_extern((ID *)pchan->custom);
1339 /* proxy rule: lib_object->proxy_from == the one we borrow from, set temporally while object_update */
1340 /* local_object->proxy == pointer to library object, saved in files and read */
1341 /* local_object->proxy_group == pointer to group dupli-object, saved in files and read */
1343 void object_make_proxy(Object *ob, Object *target, Object *gob)
1345 /* paranoia checks */
1346 if(ob->id.lib || target->id.lib==NULL) {
1347 printf("cannot make proxy\n");
1352 ob->proxy_group= gob;
1353 id_lib_extern(&target->id);
1355 ob->recalc= target->recalc= OB_RECALC;
1357 /* copy transform */
1359 VECCOPY(ob->loc, gob->loc);
1360 VECCOPY(ob->rot, gob->rot);
1361 VECCOPY(ob->size, gob->size);
1363 group_tag_recalc(gob->dup_group);
1366 VECCOPY(ob->loc, target->loc);
1367 VECCOPY(ob->rot, target->rot);
1368 VECCOPY(ob->size, target->size);
1371 ob->parent= target->parent; /* libdata */
1372 Mat4CpyMat4(ob->parentinv, target->parentinv);
1373 #if 0 // XXX old animation system
1374 ob->ipo= target->ipo; /* libdata */
1375 #endif // XXX old animation system
1377 /* skip constraints, constraintchannels, nla? */
1379 /* set object type and link to data */
1380 ob->type= target->type;
1381 ob->data= target->data;
1382 id_us_plus((ID *)ob->data); /* ensures lib data becomes LIB_EXTERN */
1384 /* copy material and index information */
1385 ob->actcol= ob->totcol= 0;
1386 if(ob->mat) MEM_freeN(ob->mat);
1388 if ((target->totcol) && (target->mat) && ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) { //XXX OB_SUPPORT_MATERIAL
1390 ob->colbits = target->colbits;
1392 ob->actcol= target->actcol;
1393 ob->totcol= target->totcol;
1395 ob->mat = MEM_dupallocN(target->mat);
1396 for(i=0; i<target->totcol; i++) {
1397 /* dont need to run test_object_materials since we know this object is new and not used elsewhere */
1398 id_us_plus((ID *)ob->mat[i]);
1402 /* type conversions */
1403 if(target->type == OB_ARMATURE) {
1404 copy_object_pose(ob, target); /* data copy, object pointers in constraints */
1405 rest_pose(ob->pose); /* clear all transforms in channels */
1406 armature_rebuild_pose(ob, ob->data); /* set all internal links */
1408 armature_set_id_extern(ob);
1413 /* *************** CALC ****************** */
1415 /* there is also a timing calculation in drawobject() */
1417 float bluroffs= 0.0f, fieldoffs= 0.0f;
1418 int no_speed_curve= 0;
1420 /* ugly calls from render */
1421 void set_mblur_offs(float blur)
1426 void set_field_offs(float field)
1431 void disable_speed_curve(int val)
1433 no_speed_curve= val;
1436 // XXX THIS CRUFT NEEDS SERIOUS RECODING ASAP!
1437 /* ob can be NULL */
1438 float bsystem_time(struct Scene *scene, Object *ob, float cfra, float ofs)
1440 /* returns float ( see frame_to_float in ipo.c) */
1442 /* bluroffs and fieldoffs are ugly globals that are set by render */
1443 cfra+= bluroffs+fieldoffs;
1446 cfra*= scene->r.framelen;
1448 #if 0 // XXX old animation system
1450 if (no_speed_curve==0 && ob->ipo)
1451 cfra= calc_ipo_time(ob->ipo, cfra);
1454 if ((ob->ipoflag & OB_OFFS_PARENT) && (ob->partype & PARSLOW)==0)
1455 cfra-= give_timeoffset(ob);
1457 #endif // XXX old animation system
1464 void object_scale_to_mat3(Object *ob, float mat[][3])
1468 vec[0]= ob->size[0]+ob->dsize[0];
1469 vec[1]= ob->size[1]+ob->dsize[1];
1470 vec[2]= ob->size[2]+ob->dsize[2];
1471 SizeToMat3(vec, mat);
1474 SizeToMat3(ob->size, mat);
1478 void object_rot_to_mat3(Object *ob, float mat[][3])
1482 vec[0]= ob->rot[0]+ob->drot[0];
1483 vec[1]= ob->rot[1]+ob->drot[1];
1484 vec[2]= ob->rot[2]+ob->drot[2];
1485 EulToMat3(vec, mat);
1488 EulToMat3(ob->rot, mat);
1492 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
1499 object_scale_to_mat3(ob, smat);
1502 /* Quats arnt used yet */
1503 /*if(ob->transflag & OB_QUAT) {
1505 QuatMul(q1, ob->quat, ob->dquat);
1506 QuatToMat3(q1, rmat);
1509 QuatToMat3(ob->quat, rmat);
1513 object_rot_to_mat3(ob, rmat);
1515 Mat3MulMat3(mat, rmat, smat);
1518 void object_to_mat4(Object *ob, float mat[][4])
1522 object_to_mat3(ob, tmat);
1524 Mat4CpyMat3(mat, tmat);
1526 VECCOPY(mat[3], ob->loc);
1528 mat[3][0]+= ob->dloc[0];
1529 mat[3][1]+= ob->dloc[1];
1530 mat[3][2]+= ob->dloc[2];
1534 int enable_cu_speed= 1;
1536 static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
1539 float q[4], vec[4], dir[3], quat[4], x1, ctime;
1540 float timeoffs = 0.0, sf_orig = 0.0;
1545 if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1546 makeDispListCurveTypes(scene, par, 0);
1547 if(cu->path==NULL) return;
1549 /* exception, timeoffset is regarded as distance offset */
1550 if(cu->flag & CU_OFFS_PATHDIST) {
1551 timeoffs = give_timeoffset(ob);
1552 SWAP(float, sf_orig, ob->sf);
1555 /* catch exceptions: feature for nla stride editing */
1556 if(ob->ipoflag & OB_DISABLE_PATH) {
1559 /* catch exceptions: curve paths used as a duplicator */
1560 else if(enable_cu_speed) {
1561 ctime= bsystem_time(scene, ob, (float)scene->r.cfra, 0.0);
1563 #if 0 // XXX old animation system
1564 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
1565 ctime /= cu->pathlen;
1566 CLAMP(ctime, 0.0, 1.0);
1568 #endif // XXX old animation system
1571 ctime= scene->r.cfra - give_timeoffset(ob);
1572 ctime /= cu->pathlen;
1574 CLAMP(ctime, 0.0, 1.0);
1577 /* time calculus is correct, now apply distance offset */
1578 if(cu->flag & CU_OFFS_PATHDIST) {
1579 ctime += timeoffs/cu->path->totdist;
1582 SWAP(float, sf_orig, ob->sf);
1587 if( where_on_path(par, ctime, vec, dir) ) {
1589 if(cu->flag & CU_FOLLOW) {
1590 vectoquat(dir, ob->trackflag, ob->upflag, quat);
1594 q[0]= (float)cos(0.5*vec[3]);
1595 x1= (float)sin(0.5*vec[3]);
1599 QuatMul(quat, q, quat);
1601 QuatToMat4(quat, mat);
1604 VECCOPY(mat[3], vec);
1609 static void ob_parbone(Object *ob, Object *par, float mat[][4])
1611 bPoseChannel *pchan;
1614 if (par->type!=OB_ARMATURE) {
1619 /* Make sure the bone is still valid */
1620 pchan= get_pose_channel(par->pose, ob->parsubstr);
1622 printf ("Object %s with Bone parent: bone %s doesn't exist\n", ob->id.name+2, ob->parsubstr);
1627 /* get bone transform */
1628 Mat4CpyMat4(mat, pchan->pose_mat);
1630 /* but for backwards compatibility, the child has to move to the tail */
1631 VECCOPY(vec, mat[1]);
1632 VecMulf(vec, pchan->bone->length);
1633 VecAddf(mat[3], mat[3], vec);
1636 static void give_parvert(Object *par, int nr, float *vec)
1640 vec[0]=vec[1]=vec[2]= 0.0f;
1642 if(par->type==OB_MESH) {
1643 Mesh *me= par->data;
1646 EditMesh *em = me->edit_mesh;
1649 for(eve= em->verts.first; eve; eve= eve->next) {
1650 if(eve->keyindex==nr) {
1651 memcpy(vec, eve->co, sizeof(float)*3);
1657 DerivedMesh *dm = par->derivedFinal;
1660 int i, count = 0, numVerts = dm->getNumVerts(dm);
1661 int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
1664 /* get the average of all verts with (original index == nr) */
1665 for(i = 0; i < numVerts; ++i, ++index) {
1667 dm->getVertCo(dm, i, co);
1668 VecAddf(vec, vec, co);
1675 } else if(count > 0) {
1676 VecMulf(vec, 1.0f / count);
1678 /* use first index if its out of range */
1679 dm->getVertCo(dm, 0, vec);
1684 else if (ELEM(par->type, OB_CURVE, OB_SURF)) {
1693 nu= cu->editnurb->first;
1698 while(nu && !found) {
1699 if((nu->type & 7)==CU_BEZIER) {
1705 VECCOPY(vec, bezt->vec[1]);
1714 a= nu->pntsu*nu->pntsv;
1718 memcpy(vec, bp->vec, sizeof(float)*3);
1729 else if(par->type==OB_LATTICE) {
1730 Lattice *latt= par->data;
1732 DispList *dl = find_displist(&par->disp, DL_VERTS);
1733 float *co = dl?dl->verts:NULL;
1735 if(latt->editlatt) latt= latt->editlatt;
1737 a= latt->pntsu*latt->pntsv*latt->pntsw;
1743 memcpy(vec, co, 3*sizeof(float));
1745 memcpy(vec, bp->vec, 3*sizeof(float));
1755 static void ob_parvert3(Object *ob, Object *par, float mat[][4])
1757 float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1759 /* in local ob space */
1762 if (ELEM4(par->type, OB_MESH, OB_SURF, OB_CURVE, OB_LATTICE)) {
1764 give_parvert(par, ob->par1, v1);
1765 give_parvert(par, ob->par2, v2);
1766 give_parvert(par, ob->par3, v3);
1768 triatoquat(v1, v2, v3, q);
1769 QuatToMat3(q, cmat);
1770 Mat4CpyMat3(mat, cmat);
1772 if(ob->type==OB_CURVE) {
1773 VECCOPY(mat[3], v1);
1776 VecAddf(mat[3], v1, v2);
1777 VecAddf(mat[3], mat[3], v3);
1778 VecMulf(mat[3], 0.3333333f);
1783 // XXX what the hell is this?
1784 static int no_parent_ipo=0;
1785 void set_no_parent_ipo(int val)
1790 static int during_script_flag=0;
1791 void disable_where_script(short on)
1793 during_script_flag= on;
1796 int during_script(void) {
1797 return during_script_flag;
1800 static int during_scriptlink_flag=0;
1801 void disable_where_scriptlink(short on)
1803 during_scriptlink_flag= on;
1806 int during_scriptlink(void) {
1807 return during_scriptlink_flag;
1810 void where_is_object_time(Scene *scene, Object *ob, float ctime)
1812 float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
1813 float stime=ctime, fac1, fac2, vec[3];
1817 /* new version: correct parent+vertexparent and track+parent */
1818 /* this one only calculates direct attached parent and track */
1819 /* is faster, but should keep track of timeoffs */
1821 if(ob==NULL) return;
1823 #if 0 // XXX old animation system
1824 /* this is needed to be able to grab objects with ipos, otherwise it always freezes them */
1825 stime= bsystem_time(scene, ob, ctime, 0.0);
1826 if(stime != ob->ctime) {
1831 calc_ipo(ob->ipo, stime);
1832 execute_ipo((ID *)ob, ob->ipo);
1835 do_all_object_actions(scene, ob);
1837 /* do constraint ipos ..., note it needs stime (0 = all ipos) */
1838 do_constraint_channels(&ob->constraints, &ob->constraintChannels, stime, 0);
1841 /* but, the drivers have to be done */
1842 if(ob->ipo) do_ob_ipodrivers(ob, ob->ipo, stime);
1843 /* do constraint ipos ..., note it needs stime (1 = only drivers ipos) */
1844 do_constraint_channels(&ob->constraints, &ob->constraintChannels, stime, 1);
1846 #endif // XXX old animation system
1848 /* execute drivers only, as animation has already been done */
1849 BKE_animsys_evaluate_animdata(&ob->id, ob->adt, ctime, ADT_RECALC_DRIVERS);
1852 Object *par= ob->parent;
1854 // XXX depreceated - animsys
1855 if(ob->ipoflag & OB_OFFS_PARENT) ctime-= give_timeoffset(ob);
1857 /* hurms, code below conflicts with depgraph... (ton) */
1858 /* and even worse, it gives bad effects for NLA stride too (try ctime != par->ctime, with MBlur) */
1860 if(no_parent_ipo==0 && stime != par->ctime) {
1861 // only for ipo systems?
1862 pushdata(par, sizeof(Object));
1865 if(par->proxy_from); // was a copied matrix, no where_is! bad...
1866 else where_is_object_time(scene, par, ctime);
1869 solve_parenting(scene, ob, par, ob->obmat, slowmat, 0);
1875 if(ob->partype & PARSLOW) {
1876 // include framerate
1877 fac1= ( 1.0f / (1.0f + (float)fabs(give_timeoffset(ob))) );
1878 if(fac1 >= 1.0f) return;
1883 for(a=0; a<16; a++, fp1++, fp2++) {
1884 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1889 object_to_mat4(ob, ob->obmat);
1892 /* Handle tracking */
1894 if( ctime != ob->track->ctime) where_is_object_time(scene, ob->track, ctime);
1895 solve_tracking (ob, ob->track->obmat);
1898 /* solve constraints */
1899 if (ob->constraints.first) {
1902 cob= constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
1904 /* constraints need ctime, not stime. Some call where_is_object_time and bsystem_time */
1905 solve_constraints (&ob->constraints, cob, ctime);
1907 constraints_clear_evalob(cob);
1909 #ifndef DISABLE_PYTHON
1910 if(ob->scriptlink.totscript && !during_script()) {
1911 if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_REDRAW);
1915 /* set negative scale flag in object */
1916 Crossf(vec, ob->obmat[0], ob->obmat[1]);
1917 if( Inpf(vec, ob->obmat[2]) < 0.0 ) ob->transflag |= OB_NEG_SCALE;
1918 else ob->transflag &= ~OB_NEG_SCALE;
1921 static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul)
1929 object_to_mat4(ob, locmat);
1931 if(ob->partype & PARSLOW) Mat4CpyMat4(slowmat, obmat);
1933 switch(ob->partype & PARTYPE) {
1936 if(par->type==OB_CURVE) {
1937 if( ((Curve *)par->data)->flag & CU_PATH ) {
1938 ob_parcurve(scene, ob, par, tmat);
1943 if(ok) Mat4MulSerie(totmat, par->obmat, tmat,
1944 NULL, NULL, NULL, NULL, NULL, NULL);
1945 else Mat4CpyMat4(totmat, par->obmat);
1949 ob_parbone(ob, par, tmat);
1950 Mat4MulSerie(totmat, par->obmat, tmat,
1951 NULL, NULL, NULL, NULL, NULL, NULL);
1957 VECCOPY(totmat[3], par->obmat[3]);
1960 give_parvert(par, ob->par1, vec);
1961 VecMat4MulVecfl(totmat[3], par->obmat, vec);
1965 ob_parvert3(ob, par, tmat);
1967 Mat4MulSerie(totmat, par->obmat, tmat,
1968 NULL, NULL, NULL, NULL, NULL, NULL);
1972 Mat4CpyMat4(totmat, par->obmat);
1977 Mat4MulSerie(tmat, totmat, ob->parentinv,
1978 NULL, NULL, NULL, NULL, NULL, NULL);
1979 Mat4MulSerie(obmat, tmat, locmat,
1980 NULL, NULL, NULL, NULL, NULL, NULL);
1986 // external usable originmat
1987 Mat3CpyMat4(originmat, tmat);
1989 // origin, voor help line
1990 if( (ob->partype & 15)==PARSKEL ) {
1991 VECCOPY(ob->orig, par->obmat[3]);
1994 VECCOPY(ob->orig, totmat[3]);
1999 void solve_tracking (Object *ob, float targetmat[][4])
2006 VecSubf(vec, ob->obmat[3], targetmat[3]);
2007 vectoquat(vec, ob->trackflag, ob->upflag, quat);
2008 QuatToMat3(quat, totmat);
2010 if(ob->parent && (ob->transflag & OB_POWERTRACK)) {
2011 /* 'temporal' : clear parent info */
2012 object_to_mat4(ob, tmat);
2013 tmat[0][3]= ob->obmat[0][3];
2014 tmat[1][3]= ob->obmat[1][3];
2015 tmat[2][3]= ob->obmat[2][3];
2016 tmat[3][0]= ob->obmat[3][0];
2017 tmat[3][1]= ob->obmat[3][1];
2018 tmat[3][2]= ob->obmat[3][2];
2019 tmat[3][3]= ob->obmat[3][3];
2021 else Mat4CpyMat4(tmat, ob->obmat);
2023 Mat4MulMat34(ob->obmat, totmat, tmat);
2027 void where_is_object(struct Scene *scene, Object *ob)
2029 where_is_object_time(scene, ob, (float)scene->r.cfra);
2033 void where_is_object_simul(Scene *scene, Object *ob)
2034 /* was written for the old game engine (until 2.04) */
2035 /* It seems that this function is only called
2036 for a lamp that is the child of another object */
2041 float slowmat[4][4];
2047 /* no ipo! (because of dloc and realtime-ipos) */
2048 // XXX old animation system
2055 solve_parenting(scene, ob, par, ob->obmat, slowmat, 1);
2057 if(ob->partype & PARSLOW) {
2059 fac1= (float)(1.0/(1.0+ fabs(give_timeoffset(ob))));
2063 for(a=0; a<16; a++, fp1++, fp2++) {
2064 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
2070 object_to_mat4(ob, ob->obmat);
2074 solve_tracking(ob, ob->track->obmat);
2076 /* solve constraints */
2077 if (ob->constraints.first) {
2080 cob= constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2081 solve_constraints(&ob->constraints, cob, (float)scene->r.cfra);
2082 constraints_clear_evalob(cob);
2086 // XXX old animation system
2090 /* for calculation of the inverse parent transform, only used for editor */
2091 void what_does_parent(Scene *scene, Object *ob, Object *workob)
2093 clear_workob(workob);
2095 Mat4One(workob->obmat);
2096 Mat4One(workob->parentinv);
2097 Mat4One(workob->constinv);
2098 workob->parent= ob->parent;
2099 workob->track= ob->track;
2101 workob->trackflag= ob->trackflag;
2102 workob->upflag= ob->upflag;
2104 workob->partype= ob->partype;
2105 workob->par1= ob->par1;
2106 workob->par2= ob->par2;
2107 workob->par3= ob->par3;
2109 workob->constraints.first = ob->constraints.first;
2110 workob->constraints.last = ob->constraints.last;
2112 strcpy(workob->parsubstr, ob->parsubstr);
2114 where_is_object(scene, workob);
2117 BoundBox *unit_boundbox()
2120 float min[3] = {-1.0f,-1.0f,-1.0f}, max[3] = {-1.0f,-1.0f,-1.0f};
2122 bb= MEM_callocN(sizeof(BoundBox), "bb");
2123 boundbox_set_from_min_max(bb, min, max);
2128 void boundbox_set_from_min_max(BoundBox *bb, float min[3], float max[3])
2130 bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
2131 bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
2133 bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
2134 bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
2136 bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
2137 bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
2140 BoundBox *object_get_boundbox(Object *ob)
2144 if(ob->type==OB_MESH) {
2145 bb = mesh_get_bb(ob);
2147 else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2148 bb= ( (Curve *)ob->data )->bb;
2150 else if(ob->type==OB_MBALL) {
2156 /* used to temporally disable/enable boundbox */
2157 void object_boundbox_flag(Object *ob, int flag, int set)
2159 BoundBox *bb= object_get_boundbox(ob);
2161 if(set) bb->flag |= flag;
2162 else bb->flag &= ~flag;
2166 void minmax_object(Object *ob, float *min, float *max)
2181 if(cu->bb==NULL) tex_space_curve(cu);
2184 for(a=0; a<8; a++) {
2185 Mat4MulVecfl(ob->obmat, bb.vec[a]);
2186 DO_MINMAX(bb.vec[a], min, max);
2191 bPoseChannel *pchan;
2192 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2193 VECCOPY(vec, pchan->pose_head);
2194 Mat4MulVecfl(ob->obmat, vec);
2195 DO_MINMAX(vec, min, max);
2196 VECCOPY(vec, pchan->pose_tail);
2197 Mat4MulVecfl(ob->obmat, vec);
2198 DO_MINMAX(vec, min, max);
2202 /* no break, get_mesh will give NULL and it passes on to default */
2207 bb = *mesh_get_bb(ob);
2209 for(a=0; a<8; a++) {
2210 Mat4MulVecfl(ob->obmat, bb.vec[a]);
2211 DO_MINMAX(bb.vec[a], min, max);
2214 if(min[0] < max[0] ) break;
2216 /* else here no break!!!, mesh can be zero sized */
2219 DO_MINMAX(ob->obmat[3], min, max);
2221 VECCOPY(vec, ob->obmat[3]);
2222 VecAddf(vec, vec, ob->size);
2223 DO_MINMAX(vec, min, max);
2225 VECCOPY(vec, ob->obmat[3]);
2226 VecSubf(vec, vec, ob->size);
2227 DO_MINMAX(vec, min, max);
2232 /* TODO - use dupli objects bounding boxes */
2233 void minmax_object_duplis(Scene *scene, Object *ob, float *min, float *max)
2235 if ((ob->transflag & OB_DUPLI)==0) {
2241 lb= object_duplilist(scene, ob);
2242 for(dob= lb->first; dob; dob= dob->next) {
2245 /* should really use bound box of dup object */
2246 DO_MINMAX(dob->mat[3], min, max);
2249 free_object_duplilist(lb); /* does restore */
2254 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
2255 /* local_object->proxy == pointer to library object, saved in files and read */
2257 /* function below is polluted with proxy exceptions, cleanup will follow! */
2259 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
2260 /* requires flags to be set! */
2261 void object_handle_update(Scene *scene, Object *ob)
2263 if(ob->recalc & OB_RECALC) {
2265 /* XXX new animsys warning: depsgraph tag OB_RECALC_DATA should not skip drivers,
2266 which is only in where_is_object now */
2267 if(ob->recalc & OB_RECALC) {
2270 printf("recalcob %s\n", ob->id.name+2);
2272 /* handle proxy copy for target */
2273 if(ob->id.lib && ob->proxy_from) {
2274 // printf("ob proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2275 if(ob->proxy_from->proxy_group) {/* transform proxy into group space */
2276 Object *obg= ob->proxy_from->proxy_group;
2277 Mat4Invert(obg->imat, obg->obmat);
2278 Mat4MulMat4(ob->obmat, ob->proxy_from->obmat, obg->imat);
2281 Mat4CpyMat4(ob->obmat, ob->proxy_from->obmat);
2284 where_is_object(scene, ob);
2285 #ifndef DISABLE_PYTHON
2286 if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_OBJECTUPDATE);
2290 if(ob->recalc & OB_RECALC_DATA) {
2293 printf("recalcdata %s\n", ob->id.name+2);
2295 /* includes all keys and modifiers */
2296 if(ob->type==OB_MESH) {
2297 // here was vieweditdatamask? XXX
2298 if(ob==scene->obedit)
2299 makeDerivedMesh(scene, ob, ((Mesh*)ob->data)->edit_mesh, CD_MASK_BAREMESH);
2301 makeDerivedMesh(scene, ob, NULL, CD_MASK_BAREMESH);
2303 else if(ob->type==OB_MBALL) {
2304 makeDispListMBall(scene, ob);
2306 else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2307 makeDispListCurveTypes(scene, ob, 0);
2309 else if(ELEM(ob->type, OB_CAMERA, OB_LAMP)) {
2310 ID *data_id= (ID *)ob->data;
2311 AnimData *adt= BKE_animdata_from_id(data_id);
2312 float ctime= (float)scene->r.cfra; // XXX this is bad...
2314 /* evaluate drivers */
2315 BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
2317 else if(ob->type==OB_LATTICE) {
2318 lattice_calc_modifiers(scene, ob);
2320 else if(ob->type==OB_ARMATURE) {
2321 /* this happens for reading old files and to match library armatures with poses */
2322 // XXX this won't screw up the pose set already...
2323 if(ob->pose==NULL || (ob->pose->flag & POSE_RECALC))
2324 armature_rebuild_pose(ob, ob->data);
2326 if(ob->id.lib && ob->proxy_from) {
2327 copy_pose_result(ob->pose, ob->proxy_from->pose);
2328 // printf("pose proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2331 where_is_pose(scene, ob);
2335 if(ob->particlesystem.first) {
2336 ParticleSystem *tpsys, *psys;
2339 psys= ob->particlesystem.first;
2341 if(psys_check_enabled(ob, psys)) {
2342 particle_system_update(scene, ob, psys);
2345 else if(psys->flag & PSYS_DELETE) {
2347 BLI_remlink(&ob->particlesystem, psys);
2355 if(G.rendering && ob->transflag & OB_DUPLIPARTS) {
2356 /* this is to make sure we get render level duplis in groups:
2357 * the derivedmesh must be created before init_render_mesh,
2358 * since object_duplilist does dupliparticles before that */
2359 dm = mesh_create_derived_render(scene, ob, CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL);
2362 for(psys=ob->particlesystem.first; psys; psys=psys->next)
2363 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
2366 #ifndef DISABLE_PYTHON
2367 if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_OBDATAUPDATE);
2371 /* the no-group proxy case, we call update */
2372 if(ob->proxy && ob->proxy_group==NULL) {
2373 /* set pointer in library proxy target, for copying, but restore it */
2374 ob->proxy->proxy_from= ob;
2375 // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
2376 object_handle_update(scene, ob->proxy);
2379 ob->recalc &= ~OB_RECALC;
2382 /* the case when this is a group proxy, object_update is called in group.c */
2384 ob->proxy->proxy_from= ob;
2385 // printf("set proxy pointer for later group stuff %s\n", ob->id.name);
2389 float give_timeoffset(Object *ob) {
2390 if ((ob->ipoflag & OB_OFFS_PARENTADD) && ob->parent) {
2391 return ob->sf + give_timeoffset(ob->parent);
2397 int give_obdata_texspace(Object *ob, int **texflag, float **loc, float **size, float **rot) {
2402 switch (GS(((ID *)ob->data)->name)) {
2406 if (texflag) *texflag = &me->texflag;
2407 if (loc) *loc = me->loc;
2408 if (size) *size = me->size;
2409 if (rot) *rot = me->rot;
2414 Curve *cu= ob->data;
2415 if (texflag) *texflag = &cu->texflag;
2416 if (loc) *loc = cu->loc;
2417 if (size) *size = cu->size;
2418 if (rot) *rot = cu->rot;
2423 MetaBall *mb= ob->data;
2424 if (texflag) *texflag = &mb->texflag;
2425 if (loc) *loc = mb->loc;
2426 if (size) *size = mb->size;
2427 if (rot) *rot = mb->rot;
2437 * Test a bounding box for ray intersection
2438 * assumes the ray is already local to the boundbox space
2440 int ray_hit_boundbox(struct BoundBox *bb, float ray_start[3], float ray_normal[3])
2442 static int triangle_indexes[12][3] = {{0, 1, 2}, {0, 2, 3},
2443 {3, 2, 6}, {3, 6, 7},
2444 {1, 2, 6}, {1, 6, 5},
2445 {5, 6, 7}, {4, 5, 7},
2446 {0, 3, 7}, {0, 4, 7},
2447 {0, 1, 5}, {0, 4, 5}};
2451 for (i = 0; i < 12 && result == 0; i++)
2455 v1 = triangle_indexes[i][0];
2456 v2 = triangle_indexes[i][1];
2457 v3 = triangle_indexes[i][2];
2458 result = RayIntersectsTriangle(ray_start, ray_normal, bb->vec[v1], bb->vec[v2], bb->vec[v3], &lambda, NULL);