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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 *****
32 /** \file blender/blenkernel/intern/object.c
41 #include "MEM_guardedalloc.h"
43 #include "DNA_anim_types.h"
44 #include "DNA_armature_types.h"
45 #include "DNA_camera_types.h"
46 #include "DNA_constraint_types.h"
47 #include "DNA_group_types.h"
48 #include "DNA_key_types.h"
49 #include "DNA_lamp_types.h"
50 #include "DNA_lattice_types.h"
51 #include "DNA_material_types.h"
52 #include "DNA_meta_types.h"
53 #include "DNA_meshdata_types.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_screen_types.h"
56 #include "DNA_sequence_types.h"
57 #include "DNA_space_types.h"
58 #include "DNA_view3d_types.h"
59 #include "DNA_world_types.h"
61 #include "BLI_blenlib.h"
62 #include "BLI_editVert.h"
65 #include "BLI_utildefines.h"
68 #include "BKE_global.h"
69 #include "BKE_idprop.h"
70 #include "BKE_armature.h"
71 #include "BKE_action.h"
72 #include "BKE_bullet.h"
73 #include "BKE_colortools.h"
74 #include "BKE_deform.h"
75 #include "BKE_DerivedMesh.h"
76 #include "BKE_animsys.h"
78 #include "BKE_constraint.h"
79 #include "BKE_curve.h"
80 #include "BKE_displist.h"
81 #include "BKE_effect.h"
82 #include "BKE_fcurve.h"
83 #include "BKE_group.h"
84 #include "BKE_icons.h"
86 #include "BKE_lattice.h"
87 #include "BKE_library.h"
89 #include "BKE_mball.h"
90 #include "BKE_modifier.h"
91 #include "BKE_object.h"
92 #include "BKE_paint.h"
93 #include "BKE_particle.h"
94 #include "BKE_pointcache.h"
95 #include "BKE_property.h"
97 #include "BKE_scene.h"
98 #include "BKE_sequencer.h"
99 #include "BKE_softbody.h"
101 #include "LBM_fluidsim.h"
104 #include "BPY_extern.h"
107 #include "GPU_material.h"
109 /* Local function protos */
110 static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul);
112 float originmat[3][3]; /* after where_is_object(), can be used in other functions (bad!) */
114 void clear_workob(Object *workob)
116 memset(workob, 0, sizeof(Object));
118 workob->size[0]= workob->size[1]= workob->size[2]= 1.0f;
119 workob->rotmode= ROT_MODE_EUL;
122 void copy_baseflags(struct Scene *scene)
124 Base *base= scene->base.first;
127 base->object->flag= base->flag;
132 void copy_objectflags(struct Scene *scene)
134 Base *base= scene->base.first;
137 base->flag= base->object->flag;
142 void update_base_layer(struct Scene *scene, Object *ob)
144 Base *base= scene->base.first;
147 if (base->object == ob) base->lay= ob->lay;
152 void object_free_particlesystems(Object *ob)
154 while(ob->particlesystem.first){
155 ParticleSystem *psys = ob->particlesystem.first;
157 BLI_remlink(&ob->particlesystem,psys);
163 void object_free_softbody(Object *ob)
171 void object_free_bulletsoftbody(Object *ob)
179 void object_free_modifiers(Object *ob)
181 while (ob->modifiers.first) {
182 ModifierData *md = ob->modifiers.first;
184 BLI_remlink(&ob->modifiers, md);
189 /* particle modifiers were freed, so free the particlesystems as well */
190 object_free_particlesystems(ob);
192 /* same for softbody */
193 object_free_softbody(ob);
196 void object_link_modifiers(struct Object *ob, struct Object *from)
199 object_free_modifiers(ob);
201 for (md=from->modifiers.first; md; md=md->next) {
202 ModifierData *nmd = NULL;
204 if(ELEM4(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_ParticleInstance, eModifierType_Collision)) continue;
206 nmd = modifier_new(md->type);
207 modifier_copyData(md, nmd);
208 BLI_addtail(&ob->modifiers, nmd);
211 copy_object_particlesystems(ob, from);
212 copy_object_softbody(ob, from);
214 // TODO: smoke?, cloth?
217 /* here we will collect all local displist stuff */
218 /* also (ab)used in depsgraph */
219 void object_free_display(Object *ob)
221 if(ob->derivedDeform) {
222 ob->derivedDeform->needsFree = 1;
223 ob->derivedDeform->release(ob->derivedDeform);
224 ob->derivedDeform= NULL;
226 if(ob->derivedFinal) {
227 ob->derivedFinal->needsFree = 1;
228 ob->derivedFinal->release(ob->derivedFinal);
229 ob->derivedFinal= NULL;
232 freedisplist(&ob->disp);
235 void free_sculptsession(Object *ob)
237 if(ob && ob->sculpt) {
238 SculptSession *ss = ob->sculpt;
239 DerivedMesh *dm= ob->derivedFinal;
242 BLI_pbvh_free(ss->pbvh);
243 if(dm && dm->getPBVH)
244 dm->getPBVH(NULL, dm); /* signal to clear */
247 MEM_freeN(ss->texcache);
250 MEM_freeN(ss->layer_co);
253 MEM_freeN(ss->orig_cos);
255 MEM_freeN(ss->deform_cos);
257 MEM_freeN(ss->deform_imats);
265 /* do not free object itself */
266 void free_object(Object *ob)
270 object_free_display(ob);
272 /* disconnect specific data */
277 if(ob->type==OB_MESH) unlink_mesh(ob->data);
278 else if(ob->type==OB_CURVE) unlink_curve(ob->data);
279 else if(ob->type==OB_MBALL) unlink_mball(ob->data);
284 for(a=0; a<ob->totcol; a++) {
285 if(ob->mat[a]) ob->mat[a]->id.us--;
287 if(ob->mat) MEM_freeN(ob->mat);
288 if(ob->matbits) MEM_freeN(ob->matbits);
291 if(ob->bb) MEM_freeN(ob->bb);
293 if(ob->path) free_path(ob->path);
295 if(ob->adt) BKE_free_animdata((ID *)ob);
296 if(ob->poselib) ob->poselib->id.us--;
297 if(ob->gpd) ((ID *)ob->gpd)->us--;
298 if(ob->defbase.first)
299 BLI_freelistN(&ob->defbase);
303 animviz_free_motionpath(ob->mpath);
304 free_properties(&ob->prop);
305 object_free_modifiers(ob);
307 free_sensors(&ob->sensors);
308 free_controllers(&ob->controllers);
309 free_actuators(&ob->actuators);
311 free_constraints(&ob->constraints);
313 free_partdeflect(ob->pd);
315 if(ob->soft) sbFree(ob->soft);
316 if(ob->bsoft) bsbFree(ob->bsoft);
317 if(ob->gpulamp.first) GPU_lamp_free(ob);
319 free_sculptsession(ob);
321 if(ob->pc_ids.first) BLI_freelistN(&ob->pc_ids);
324 static void unlink_object__unlinkModifierLinks(void *userData, Object *ob, Object **obpoin)
326 Object *unlinkOb = userData;
328 if (*obpoin==unlinkOb) {
330 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME; // XXX: should this just be OB_RECALC_DATA?
334 void unlink_object(Object *ob)
347 //bActionStrip *strip; // XXX animsys
353 unlink_controllers(&ob->controllers);
354 unlink_actuators(&ob->actuators);
356 /* check all objects: parents en bevels and fields, also from libraries */
357 // FIXME: need to check all animation blocks (drivers)
358 obt= bmain->object.first;
362 if(obt->proxy_from==ob) {
363 obt->proxy_from= NULL;
364 obt->recalc |= OB_RECALC_OB;
366 if(obt->proxy_group==ob)
367 obt->proxy_group= NULL;
369 if(obt->parent==ob) {
371 obt->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
374 modifiers_foreachObjectLink(obt, unlink_object__unlinkModifierLinks, ob);
376 if ELEM(obt->type, OB_CURVE, OB_FONT) {
381 obt->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
383 if(cu->taperobj==ob) {
385 obt->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
387 if(cu->textoncurve==ob) {
388 cu->textoncurve= NULL;
389 obt->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
392 else if(obt->type==OB_ARMATURE && obt->pose) {
394 for(pchan= obt->pose->chanbase.first; pchan; pchan= pchan->next) {
395 for (con = pchan->constraints.first; con; con=con->next) {
396 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
397 ListBase targets = {NULL, NULL};
398 bConstraintTarget *ct;
400 if (cti && cti->get_constraint_targets) {
401 cti->get_constraint_targets(con, &targets);
403 for (ct= targets.first; ct; ct= ct->next) {
406 strcpy(ct->subtarget, "");
407 obt->recalc |= OB_RECALC_DATA;
411 if (cti->flush_constraint_targets)
412 cti->flush_constraint_targets(con, &targets, 0);
415 if(pchan->custom==ob)
418 } else if(ELEM(OB_MBALL, ob->type, obt->type)) {
419 if(is_mball_basis_for(obt, ob))
420 obt->recalc|= OB_RECALC_DATA;
423 sca_remove_ob_poin(obt, ob);
425 for (con = obt->constraints.first; con; con=con->next) {
426 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
427 ListBase targets = {NULL, NULL};
428 bConstraintTarget *ct;
430 if (cti && cti->get_constraint_targets) {
431 cti->get_constraint_targets(con, &targets);
433 for (ct= targets.first; ct; ct= ct->next) {
436 strcpy(ct->subtarget, "");
437 obt->recalc |= OB_RECALC_DATA;
441 if (cti->flush_constraint_targets)
442 cti->flush_constraint_targets(con, &targets, 0);
446 /* object is deflector or field */
449 obt->recalc |= OB_RECALC_DATA;
452 for(md=obt->modifiers.first; md; md=md->next)
453 if(md->type == eModifierType_Cloth)
454 obt->recalc |= OB_RECALC_DATA;
458 #if 0 // XXX old animation system
459 for(strip= obt->nlastrips.first; strip; strip= strip->next) {
460 if(strip->object==ob)
463 if(strip->modifiers.first) {
464 bActionModifier *amod;
465 for(amod= strip->modifiers.first; amod; amod= amod->next)
470 #endif // XXX old animation system
472 /* particle systems */
473 if(obt->particlesystem.first) {
474 ParticleSystem *tpsys= obt->particlesystem.first;
475 for(; tpsys; tpsys=tpsys->next) {
476 BoidState *state = NULL;
477 BoidRule *rule = NULL;
479 ParticleTarget *pt = tpsys->targets.first;
480 for(; pt; pt=pt->next) {
483 obt->recalc |= OB_RECALC_DATA;
488 if(tpsys->target_ob==ob) {
489 tpsys->target_ob= NULL;
490 obt->recalc |= OB_RECALC_DATA;
493 if(tpsys->part->dup_ob==ob)
494 tpsys->part->dup_ob= NULL;
496 if(tpsys->part->phystype==PART_PHYS_BOIDS) {
501 for(p=0,pa=tpsys->particles; p<tpsys->totpart; p++,pa++) {
503 if(bpa->ground == ob)
507 if(tpsys->part->boids) {
508 for(state = tpsys->part->boids->states.first; state; state=state->next) {
509 for(rule = state->rules.first; rule; rule=rule->next) {
510 if(rule->type==eBoidRuleType_Avoid) {
511 BoidRuleGoalAvoid *gabr = (BoidRuleGoalAvoid*)rule;
515 else if(rule->type==eBoidRuleType_FollowLeader) {
516 BoidRuleFollowLeader *flbr = (BoidRuleFollowLeader*)rule;
525 obt->recalc |= OB_RECALC_DATA;
532 mat= bmain->mat.first;
535 for(a=0; a<MAX_MTEX; a++) {
536 if(mat->mtex[a] && ob==mat->mtex[a]->object) {
537 /* actually, test for lib here... to do */
538 mat->mtex[a]->object= NULL;
546 for(tex= bmain->tex.first; tex; tex= tex->id.next) {
547 if(tex->env && (ob==tex->env->object)) tex->env->object= NULL;
548 if(tex->pd && (ob==tex->pd->object)) tex->pd->object= NULL;
549 if(tex->vd && (ob==tex->vd->object)) tex->vd->object= NULL;
553 wrld= bmain->world.first;
555 if(wrld->id.lib==NULL) {
556 for(a=0; a<MAX_MTEX; a++) {
557 if(wrld->mtex[a] && ob==wrld->mtex[a]->object)
558 wrld->mtex[a]->object= NULL;
566 sce= bmain->scene.first;
568 if(sce->id.lib==NULL) {
569 if(sce->camera==ob) sce->camera= NULL;
570 if(sce->toolsettings->skgen_template==ob) sce->toolsettings->skgen_template = NULL;
571 if(sce->toolsettings->particle.object==ob) sce->toolsettings->particle.object= NULL;
573 #ifdef DURIAN_CAMERA_SWITCH
577 for (m= sce->markers.first; m; m= m->next) {
585 SEQ_BEGIN(sce->ed, seq)
586 if(seq->scene_camera==ob) {
587 seq->scene_camera= NULL;
596 #if 0 // XXX old animation system
598 ipo= bmain->ipo.first;
600 if(ipo->id.lib==NULL) {
602 for(icu= ipo->curve.first; icu; icu= icu->next) {
603 if(icu->driver && icu->driver->ob==ob)
604 icu->driver->ob= NULL;
609 #endif // XXX old animation system
612 sc= bmain->screen.first;
614 ScrArea *sa= sc->areabase.first;
618 for (sl= sa->spacedata.first; sl; sl= sl->next) {
619 if(sl->spacetype==SPACE_VIEW3D) {
620 View3D *v3d= (View3D*) sl;
623 if(v3d->camera==ob) {
627 if(v3d->localvd && v3d->localvd->camera==ob ) {
628 v3d->localvd->camera= NULL;
633 if (sa->spacetype == SPACE_VIEW3D) {
634 for (ar= sa->regionbase.first; ar; ar= ar->next) {
635 if (ar->regiontype==RGN_TYPE_WINDOW) {
636 rv3d= (RegionView3D *)ar->regiondata;
637 if (found == 1 || found == 3) {
638 if (rv3d->persp == RV3D_CAMOB)
639 rv3d->persp= RV3D_PERSP;
641 if (found == 2 || found == 3) {
642 if (rv3d->localvd && rv3d->localvd->persp == RV3D_CAMOB)
643 rv3d->localvd->persp= RV3D_PERSP;
650 else if(sl->spacetype==SPACE_OUTLINER) {
651 SpaceOops *so= (SpaceOops *)sl;
654 TreeStoreElem *tselem= so->treestore->data;
656 for(a=0; a<so->treestore->usedelem; a++, tselem++) {
657 if(tselem->id==(ID *)ob) tselem->id= NULL;
661 else if(sl->spacetype==SPACE_BUTS) {
662 SpaceButs *sbuts= (SpaceButs *)sl;
664 if(sbuts->pinid==(ID *)ob) {
665 sbuts->flag&= ~SB_PIN_CONTEXT;
677 group= bmain->group.first;
679 rem_from_group(group, ob, NULL, NULL);
680 group= group->id.next;
684 camera= bmain->camera.first;
686 if (camera->dof_ob==ob) {
687 camera->dof_ob = NULL;
689 camera= camera->id.next;
693 int exist_object(Object *obtest)
697 if(obtest==NULL) return 0;
699 ob= G.main->object.first;
701 if(ob==obtest) return 1;
707 void *add_camera(const char *name)
711 cam= alloc_libblock(&G.main->camera, ID_CA, name);
715 cam->clipend= 100.0f;
717 cam->ortho_scale= 6.0;
718 cam->flag |= CAM_SHOWPASSEPARTOUT;
719 cam->passepartalpha = 0.5f;
724 Camera *copy_camera(Camera *cam)
728 camn= copy_libblock(cam);
735 void make_local_camera(Camera *cam)
742 /* - only lib users: do nothing
743 * - only local users: set flag
747 if(cam->id.lib==NULL) return;
750 cam->id.flag= LIB_LOCAL;
751 new_id(NULL, (ID *)cam, NULL);
755 ob= bmain->object.first;
758 if(ob->id.lib) lib= 1;
764 if(local && lib==0) {
766 cam->id.flag= LIB_LOCAL;
767 new_id(NULL, (ID *)cam, NULL);
769 else if(local && lib) {
770 camn= copy_camera(cam);
773 ob= bmain->object.first;
777 if(ob->id.lib==NULL) {
788 /* get the camera's dof value, takes the dof object into account */
789 float dof_camera(Object *ob)
791 Camera *cam = (Camera *)ob->data;
792 if (ob->type != OB_CAMERA)
795 /* too simple, better to return the distance on the view axis only
796 * return len_v3v3(ob->obmat[3], cam->dof_ob->obmat[3]); */
797 float mat[4][4], imat[4][4], obmat[4][4];
799 copy_m4_m4(obmat, ob->obmat);
801 invert_m4_m4(imat, obmat);
802 mul_m4_m4m4(mat, cam->dof_ob->obmat, imat);
803 return (float)fabs(mat[3][2]);
805 return cam->YF_dofdist;
808 void *add_lamp(const char *name)
812 la= alloc_libblock(&G.main->lamp, ID_LA, name);
814 la->r= la->g= la->b= la->k= 1.0f;
815 la->haint= la->energy= 1.0f;
818 la->spotblend= 0.15f;
820 la->mode= LA_SHAD_BUF;
824 la->shadspotsize= 45.0f;
828 la->compressthresh= 0.05f;
829 la->ray_samp= la->ray_sampy= la->ray_sampz= 1;
830 la->area_size=la->area_sizey=la->area_sizez= 1.0f;
832 la->buftype= LA_SHADBUF_HALFWAY;
833 la->ray_samp_method = LA_SAMP_HALTON;
834 la->adapt_thresh = 0.001f;
836 la->falloff_type = LA_FALLOFF_INVSQUARE;
837 la->curfalloff = curvemapping_add(1, 0.0f, 1.0f, 1.0f, 0.0f);
838 la->sun_effect_type = 0;
839 la->horizon_brightness = 1.0;
841 la->sun_brightness = 1.0;
843 la->backscattered_light = 1.0f;
844 la->atm_turbidity = 2.0f;
845 la->atm_inscattering_factor = 1.0f;
846 la->atm_extinction_factor = 1.0f;
847 la->atm_distance_factor = 1.0f;
848 la->sun_intensity = 1.0f;
849 la->skyblendtype= MA_RAMP_ADD;
850 la->skyblendfac= 1.0f;
851 la->sky_colorspace= BLI_XYZ_CIE;
852 la->sky_exposure= 1.0f;
854 curvemapping_initialize(la->curfalloff);
858 Lamp *copy_lamp(Lamp *la)
863 lan= copy_libblock(la);
865 for(a=0; a<MAX_MTEX; a++) {
867 lan->mtex[a]= MEM_mallocN(sizeof(MTex), "copylamptex");
868 memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
869 id_us_plus((ID *)lan->mtex[a]->tex);
873 lan->curfalloff = curvemapping_copy(la->curfalloff);
875 #if 0 // XXX old animation system
876 id_us_plus((ID *)lan->ipo);
877 #endif // XXX old animation system
879 if (la->preview) lan->preview = BKE_previewimg_copy(la->preview);
884 void make_local_lamp(Lamp *la)
891 /* - only lib users: do nothing
892 * - only local users: set flag
896 if(la->id.lib==NULL) return;
899 la->id.flag= LIB_LOCAL;
900 new_id(NULL, (ID *)la, NULL);
904 ob= bmain->object.first;
907 if(ob->id.lib) lib= 1;
913 if(local && lib==0) {
915 la->id.flag= LIB_LOCAL;
916 new_id(NULL, (ID *)la, NULL);
918 else if(local && lib) {
922 ob= bmain->object.first;
926 if(ob->id.lib==NULL) {
937 void free_camera(Camera *ca)
939 BKE_free_animdata((ID *)ca);
942 void free_lamp(Lamp *la)
947 for(a=0; a<MAX_MTEX; a++) {
949 if(mtex && mtex->tex) mtex->tex->id.us--;
950 if(mtex) MEM_freeN(mtex);
953 BKE_free_animdata((ID *)la);
955 curvemapping_free(la->curfalloff);
957 BKE_previewimg_free(&la->preview);
958 BKE_icon_delete(&la->id);
962 /* *************************************************** */
964 static void *add_obdata_from_type(int type)
967 case OB_MESH: return add_mesh("Mesh");
968 case OB_CURVE: return add_curve("Curve", OB_CURVE);
969 case OB_SURF: return add_curve("Surf", OB_SURF);
970 case OB_FONT: return add_curve("Text", OB_FONT);
971 case OB_MBALL: return add_mball("Meta");
972 case OB_CAMERA: return add_camera("Camera");
973 case OB_LAMP: return add_lamp("Lamp");
974 case OB_LATTICE: return add_lattice("Lattice");
975 case OB_ARMATURE: return add_armature("Armature");
976 case OB_EMPTY: return NULL;
978 printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
983 static const char *get_obdata_defname(int type)
986 case OB_MESH: return "Mesh";
987 case OB_CURVE: return "Curve";
988 case OB_SURF: return "Surf";
989 case OB_FONT: return "Text";
990 case OB_MBALL: return "Mball";
991 case OB_CAMERA: return "Camera";
992 case OB_LAMP: return "Lamp";
993 case OB_LATTICE: return "Lattice";
994 case OB_ARMATURE: return "Armature";
995 case OB_EMPTY: return "Empty";
997 printf("get_obdata_defname: Internal error, bad type: %d\n", type);
1002 /* more general add: creates minimum required data, but without vertices etc. */
1003 Object *add_only_object(int type, const char *name)
1007 ob= alloc_libblock(&G.main->object, ID_OB, name);
1009 /* default object vars */
1012 ob->col[0]= ob->col[1]= ob->col[2]= 1.0;
1015 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
1017 /* objects should default to having Euler XYZ rotations,
1018 * but rotations default to quaternions
1020 ob->rotmode= ROT_MODE_EUL;
1022 unit_axis_angle(ob->rotAxis, &ob->rotAngle);
1023 unit_axis_angle(ob->drotAxis, &ob->drotAngle);
1028 /* rotation locks should be 4D for 4 component rotations by default... */
1029 ob->protectflag = OB_LOCK_ROT4D;
1031 unit_m4(ob->constinv);
1032 unit_m4(ob->parentinv);
1035 ob->empty_drawtype= OB_PLAINAXES;
1036 ob->empty_drawsize= 1.0;
1038 if(type==OB_CAMERA || type==OB_LAMP) {
1039 ob->trackflag= OB_NEGZ;
1040 ob->upflag= OB_POSY;
1043 ob->trackflag= OB_POSY;
1044 ob->upflag= OB_POSZ;
1047 ob->dupon= 1; ob->dupoff= 0;
1048 ob->dupsta= 1; ob->dupend= 100;
1049 ob->dupfacesca = 1.0;
1051 /* Game engine defaults*/
1052 ob->mass= ob->inertia= 1.0f;
1053 ob->formfactor= 0.4f;
1056 ob->anisotropicFriction[0] = 1.0f;
1057 ob->anisotropicFriction[1] = 1.0f;
1058 ob->anisotropicFriction[2] = 1.0f;
1059 ob->gameflag= OB_PROP|OB_COLLISION;
1063 /* ob->pad3 == Contact Processing Threshold */
1064 ob->m_contactProcessingThreshold = 1.;
1066 /* NT fluid sim defaults */
1067 ob->fluidsimFlag = 0;
1068 ob->fluidsimSettings = NULL;
1070 ob->pc_ids.first = ob->pc_ids.last = NULL;
1072 /* Animation Visualisation defaults */
1073 animviz_settings_init(&ob->avs);
1078 /* general add: to scene, with layer from area and default name */
1079 /* creates minimum required data, but without vertices etc. */
1080 Object *add_object(struct Scene *scene, int type)
1086 BLI_strncpy(name, get_obdata_defname(type), sizeof(name));
1087 ob = add_only_object(type, name);
1089 ob->data= add_obdata_from_type(type);
1091 ob->lay= scene->lay;
1093 base= scene_add_base(scene, ob);
1094 scene_select_base(scene, base);
1095 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1100 SoftBody *copy_softbody(SoftBody *sb)
1104 if (sb==NULL) return(NULL);
1106 sbn= MEM_dupallocN(sb);
1107 sbn->totspring= sbn->totpoint= 0;
1112 sbn->totkey= sbn->totpointkey= 0;
1116 sbn->pointcache= BKE_ptcache_copy_list(&sbn->ptcaches, &sb->ptcaches);
1118 if(sb->effector_weights)
1119 sbn->effector_weights = MEM_dupallocN(sb->effector_weights);
1124 BulletSoftBody *copy_bulletsoftbody(BulletSoftBody *bsb)
1126 BulletSoftBody *bsbn;
1130 bsbn = MEM_dupallocN(bsb);
1131 /* no pointer in this structure yet */
1135 static ParticleSystem *copy_particlesystem(ParticleSystem *psys)
1137 ParticleSystem *psysn;
1141 psysn= MEM_dupallocN(psys);
1142 psysn->particles= MEM_dupallocN(psys->particles);
1143 psysn->child= MEM_dupallocN(psys->child);
1145 if(psys->part->type == PART_HAIR) {
1146 for(p=0, pa=psysn->particles; p<psysn->totpart; p++, pa++)
1147 pa->hair = MEM_dupallocN(pa->hair);
1150 if(psysn->particles && (psysn->particles->keys || psysn->particles->boid)) {
1151 ParticleKey *key = psysn->particles->keys;
1152 BoidParticle *boid = psysn->particles->boid;
1155 key = MEM_dupallocN(key);
1158 boid = MEM_dupallocN(boid);
1160 for(p=0, pa=psysn->particles; p<psysn->totpart; p++, pa++) {
1171 psysn->clmd = (ClothModifierData *)modifier_new(eModifierType_Cloth);
1172 modifier_copyData((ModifierData*)psys->clmd, (ModifierData*)psysn->clmd);
1173 psys->hair_in_dm = psys->hair_out_dm = NULL;
1176 BLI_duplicatelist(&psysn->targets, &psys->targets);
1178 psysn->pathcache= NULL;
1179 psysn->childcache= NULL;
1183 psysn->effectors= NULL;
1185 psysn->pathcachebufs.first = psysn->pathcachebufs.last = NULL;
1186 psysn->childcachebufs.first = psysn->childcachebufs.last = NULL;
1187 psysn->renderdata = NULL;
1189 psysn->pointcache= BKE_ptcache_copy_list(&psysn->ptcaches, &psys->ptcaches);
1191 /* XXX - from reading existing code this seems correct but intended usage of
1192 * pointcache should /w cloth should be added in 'ParticleSystem' - campbell */
1194 psysn->clmd->point_cache= psysn->pointcache;
1197 id_us_plus((ID *)psysn->part);
1202 void copy_object_particlesystems(Object *obn, Object *ob)
1204 ParticleSystemModifierData *psmd;
1205 ParticleSystem *psys, *npsys;
1208 obn->particlesystem.first= obn->particlesystem.last= NULL;
1209 for(psys=ob->particlesystem.first; psys; psys=psys->next) {
1210 npsys= copy_particlesystem(psys);
1212 BLI_addtail(&obn->particlesystem, npsys);
1214 /* need to update particle modifiers too */
1215 for(md=obn->modifiers.first; md; md=md->next) {
1216 if(md->type==eModifierType_ParticleSystem) {
1217 psmd= (ParticleSystemModifierData*)md;
1218 if(psmd->psys==psys)
1225 void copy_object_softbody(Object *obn, Object *ob)
1228 obn->soft= copy_softbody(ob->soft);
1231 static void copy_object_pose(Object *obn, Object *ob)
1235 /* note: need to clear obn->pose pointer first, so that copy_pose works (otherwise there's a crash) */
1237 copy_pose(&obn->pose, ob->pose, 1); /* 1 = copy constraints */
1239 for (chan = obn->pose->chanbase.first; chan; chan=chan->next){
1242 chan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
1244 for (con= chan->constraints.first; con; con= con->next) {
1245 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1246 ListBase targets = {NULL, NULL};
1247 bConstraintTarget *ct;
1249 #if 0 // XXX old animation system
1250 /* note that we can't change lib linked ipo blocks. for making
1251 * proxies this still works correct however because the object
1252 * is changed to object->proxy_from when evaluating the driver. */
1253 if(con->ipo && !con->ipo->id.lib) {
1256 con->ipo= copy_ipo(con->ipo);
1258 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
1259 if(icu->driver && icu->driver->ob==ob)
1260 icu->driver->ob= obn;
1263 #endif // XXX old animation system
1265 if (cti && cti->get_constraint_targets) {
1266 cti->get_constraint_targets(con, &targets);
1268 for (ct= targets.first; ct; ct= ct->next) {
1273 if (cti->flush_constraint_targets)
1274 cti->flush_constraint_targets(con, &targets, 0);
1280 static void copy_object_transform(Object *ob_tar, Object *ob_src)
1282 copy_v3_v3(ob_tar->loc, ob_src->loc);
1283 copy_v3_v3(ob_tar->rot, ob_src->rot);
1284 copy_v3_v3(ob_tar->quat, ob_src->quat);
1285 copy_v3_v3(ob_tar->rotAxis, ob_src->rotAxis);
1286 ob_tar->rotAngle= ob_src->rotAngle;
1287 ob_tar->rotmode= ob_src->rotmode;
1288 copy_v3_v3(ob_tar->size, ob_src->size);
1291 Object *copy_object(Object *ob)
1297 obn= copy_libblock(ob);
1300 obn->mat= MEM_dupallocN(ob->mat);
1301 obn->matbits= MEM_dupallocN(ob->matbits);
1302 obn->totcol= ob->totcol;
1305 if(ob->bb) obn->bb= MEM_dupallocN(ob->bb);
1307 obn->flag &= ~OB_FROMGROUP;
1309 obn->modifiers.first = obn->modifiers.last= NULL;
1311 for (md=ob->modifiers.first; md; md=md->next) {
1312 ModifierData *nmd = modifier_new(md->type);
1313 BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
1314 modifier_copyData(md, nmd);
1315 BLI_addtail(&obn->modifiers, nmd);
1318 obn->prop.first = obn->prop.last = NULL;
1319 copy_properties(&obn->prop, &ob->prop);
1321 copy_sensors(&obn->sensors, &ob->sensors);
1322 copy_controllers(&obn->controllers, &ob->controllers);
1323 copy_actuators(&obn->actuators, &ob->actuators);
1326 copy_object_pose(obn, ob);
1327 /* backwards compat... non-armatures can get poses in older files? */
1328 if(ob->type==OB_ARMATURE)
1329 armature_rebuild_pose(obn, obn->data);
1331 defgroup_copy_list(&obn->defbase, &ob->defbase);
1332 copy_constraints(&obn->constraints, &ob->constraints, TRUE);
1337 /* increase user numbers */
1338 id_us_plus((ID *)obn->data);
1339 id_us_plus((ID *)obn->gpd);
1340 id_lib_extern((ID *)obn->dup_group);
1342 for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
1344 obn->disp.first= obn->disp.last= NULL;
1347 obn->pd= MEM_dupallocN(ob->pd);
1349 id_us_plus(&(obn->pd->tex->id));
1351 obn->pd->rng = MEM_dupallocN(ob->pd->rng);
1353 obn->soft= copy_softbody(ob->soft);
1354 obn->bsoft = copy_bulletsoftbody(ob->bsoft);
1356 copy_object_particlesystems(obn, ob);
1358 obn->derivedDeform = NULL;
1359 obn->derivedFinal = NULL;
1361 obn->gpulamp.first = obn->gpulamp.last = NULL;
1362 obn->pc_ids.first = obn->pc_ids.last = NULL;
1369 void expand_local_object(Object *ob)
1371 //bActionStrip *strip;
1372 ParticleSystem *psys;
1375 #if 0 // XXX old animation system
1376 id_lib_extern((ID *)ob->action);
1377 id_lib_extern((ID *)ob->ipo);
1378 #endif // XXX old animation system
1379 id_lib_extern((ID *)ob->data);
1380 id_lib_extern((ID *)ob->dup_group);
1382 for(a=0; a<ob->totcol; a++) {
1383 id_lib_extern((ID *)ob->mat[a]);
1385 #if 0 // XXX old animation system
1386 for (strip=ob->nlastrips.first; strip; strip=strip->next) {
1387 id_lib_extern((ID *)strip->act);
1389 #endif // XXX old animation system
1390 for(psys=ob->particlesystem.first; psys; psys=psys->next)
1391 id_lib_extern((ID *)psys->part);
1394 void make_local_object(Object *ob)
1396 Main *bmain= G.main;
1402 /* - only lib users: do nothing
1403 * - only local users: set flag
1404 * - mixed: make copy
1407 if(ob->id.lib==NULL) return;
1409 ob->proxy= ob->proxy_from= NULL;
1413 ob->id.flag= LIB_LOCAL;
1414 new_id(NULL, (ID *)ob, NULL);
1418 sce= bmain->scene.first;
1420 base= sce->base.first;
1422 if(base->object==ob) {
1423 if(sce->id.lib) lib++;
1432 if(local && lib==0) {
1434 ob->id.flag= LIB_LOCAL;
1435 new_id(NULL, (ID *)ob, NULL);
1437 else if(local && lib) {
1438 obn= copy_object(ob);
1441 sce= bmain->scene.first;
1443 if(sce->id.lib==NULL) {
1444 base= sce->base.first;
1446 if(base->object==ob) {
1459 expand_local_object(ob);
1463 * Returns true if the Object is a from an external blend file (libdata)
1465 int object_is_libdata(Object *ob)
1468 if (ob->proxy) return 0;
1469 if (ob->id.lib) return 1;
1473 /* Returns true if the Object data is a from an external blend file (libdata) */
1474 int object_data_is_libdata(Object *ob)
1477 if(ob->proxy && (ob->data==NULL || ((ID *)ob->data)->lib==NULL)) return 0;
1478 if(ob->id.lib) return 1;
1479 if(ob->data==NULL) return 0;
1480 if(((ID *)ob->data)->lib) return 1;
1485 /* *************** PROXY **************** */
1487 /* when you make proxy, ensure the exposed layers are extern */
1488 static void armature_set_id_extern(Object *ob)
1490 bArmature *arm= ob->data;
1491 bPoseChannel *pchan;
1492 unsigned int lay= arm->layer_protected;
1494 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1495 if(!(pchan->bone->layer & lay))
1496 id_lib_extern((ID *)pchan->custom);
1501 void object_copy_proxy_drivers(Object *ob, Object *target)
1503 if ((target->adt) && (target->adt->drivers.first)) {
1506 /* add new animdata block */
1508 ob->adt= BKE_id_add_animdata(&ob->id);
1510 /* make a copy of all the drivers (for now), then correct any links that need fixing */
1511 free_fcurves(&ob->adt->drivers);
1512 copy_fcurves(&ob->adt->drivers, &target->adt->drivers);
1514 for (fcu= ob->adt->drivers.first; fcu; fcu= fcu->next) {
1515 ChannelDriver *driver= fcu->driver;
1518 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1520 DRIVER_TARGETS_LOOPER(dvar)
1523 if ((Object *)dtar->id == target)
1526 /* only on local objects because this causes indirect links a -> b -> c,blend to point directly to a.blend
1527 * when a.blend has a proxy thats linked into c.blend */
1528 if(ob->id.lib==NULL)
1529 id_lib_extern((ID *)dtar->id);
1533 DRIVER_TARGETS_LOOPER_END
1539 /* proxy rule: lib_object->proxy_from == the one we borrow from, set temporally while object_update */
1540 /* local_object->proxy == pointer to library object, saved in files and read */
1541 /* local_object->proxy_group == pointer to group dupli-object, saved in files and read */
1543 void object_make_proxy(Object *ob, Object *target, Object *gob)
1545 /* paranoia checks */
1546 if(ob->id.lib || target->id.lib==NULL) {
1547 printf("cannot make proxy\n");
1552 ob->proxy_group= gob;
1553 id_lib_extern(&target->id);
1555 ob->recalc= target->recalc= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1558 * - gob means this proxy comes from a group, just apply the matrix
1559 * so the object wont move from its dupli-transform.
1561 * - no gob means this is being made from a linked object,
1562 * this is closer to making a copy of the object - in-place. */
1564 ob->rotmode= target->rotmode;
1565 mul_m4_m4m4(ob->obmat, target->obmat, gob->obmat);
1566 if(gob->dup_group) { /* should always be true */
1567 sub_v3_v3(ob->obmat[3], gob->dup_group->dupli_ofs);
1569 object_apply_mat4(ob, ob->obmat, FALSE, TRUE);
1572 copy_object_transform(ob, target);
1573 ob->parent= target->parent; /* libdata */
1574 copy_m4_m4(ob->parentinv, target->parentinv);
1577 /* copy animdata stuff - drivers only for now... */
1578 object_copy_proxy_drivers(ob, target);
1580 /* skip constraints? */
1581 // FIXME: this is considered by many as a bug
1583 /* set object type and link to data */
1584 ob->type= target->type;
1585 ob->data= target->data;
1586 id_us_plus((ID *)ob->data); /* ensures lib data becomes LIB_EXTERN */
1588 /* copy material and index information */
1589 ob->actcol= ob->totcol= 0;
1590 if(ob->mat) MEM_freeN(ob->mat);
1591 if(ob->matbits) MEM_freeN(ob->matbits);
1594 if ((target->totcol) && (target->mat) && ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) { //XXX OB_SUPPORT_MATERIAL
1596 ob->colbits = target->colbits;
1598 ob->actcol= target->actcol;
1599 ob->totcol= target->totcol;
1601 ob->mat = MEM_dupallocN(target->mat);
1602 ob->matbits = MEM_dupallocN(target->matbits);
1603 for(i=0; i<target->totcol; i++) {
1604 /* dont need to run test_object_materials since we know this object is new and not used elsewhere */
1605 id_us_plus((ID *)ob->mat[i]);
1609 /* type conversions */
1610 if(target->type == OB_ARMATURE) {
1611 copy_object_pose(ob, target); /* data copy, object pointers in constraints */
1612 rest_pose(ob->pose); /* clear all transforms in channels */
1613 armature_rebuild_pose(ob, ob->data); /* set all internal links */
1615 armature_set_id_extern(ob);
1618 /* copy IDProperties */
1619 if(ob->id.properties) {
1620 IDP_FreeProperty(ob->id.properties);
1621 MEM_freeN(ob->id.properties);
1622 ob->id.properties= NULL;
1624 if(target->id.properties) {
1625 ob->id.properties= IDP_CopyProperty(target->id.properties);
1628 /* copy drawtype info */
1633 /* *************** CALC ****************** */
1635 /* there is also a timing calculation in drawobject() */
1637 static int no_speed_curve= 0;
1639 void disable_speed_curve(int val)
1641 no_speed_curve= val;
1644 // XXX THIS CRUFT NEEDS SERIOUS RECODING ASAP!
1645 /* ob can be NULL */
1646 float bsystem_time(struct Scene *scene, Object *UNUSED(ob), float cfra, float ofs)
1648 /* returns float ( see BKE_curframe in scene.c) */
1649 cfra += scene->r.subframe;
1653 cfra*= scene->r.framelen;
1655 #if 0 // XXX old animation system
1658 if ((ob->ipoflag & OB_OFFS_PARENT) && (ob->partype & PARSLOW)==0)
1659 cfra-= give_timeoffset(ob);
1661 #endif // XXX old animation system
1668 void object_scale_to_mat3(Object *ob, float mat[][3])
1671 add_v3_v3v3(vec, ob->size, ob->dsize);
1672 size_to_mat3( mat,vec);
1676 void object_rot_to_mat3(Object *ob, float mat[][3])
1678 float rmat[3][3], dmat[3][3];
1680 /* initialise the delta-rotation matrix, which will get (pre)multiplied
1681 * with the rotation matrix to yield the appropriate rotation
1685 /* rotations may either be quats, eulers (with various rotation orders), or axis-angle */
1686 if (ob->rotmode > 0) {
1687 /* euler rotations (will cause gimble lock, but this can be alleviated a bit with rotation orders) */
1688 eulO_to_mat3( rmat,ob->rot, ob->rotmode);
1689 eulO_to_mat3( dmat,ob->drot, ob->rotmode);
1691 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
1692 /* axis-angle - not really that great for 3D-changing orientations */
1693 axis_angle_to_mat3( rmat,ob->rotAxis, ob->rotAngle);
1694 axis_angle_to_mat3( dmat,ob->drotAxis, ob->drotAngle);
1697 /* quats are normalised before use to eliminate scaling issues */
1700 normalize_qt_qt(tquat, ob->quat);
1701 quat_to_mat3(rmat, tquat);
1703 normalize_qt_qt(tquat, ob->dquat);
1704 quat_to_mat3(dmat, tquat);
1707 /* combine these rotations */
1708 mul_m3_m3m3(mat, dmat, rmat);
1711 void object_mat3_to_rot(Object *ob, float mat[][3], short use_compat)
1713 switch(ob->rotmode) {
1717 mat3_to_quat(ob->quat, mat);
1718 normalize_qt_qt(dquat, ob->dquat);
1720 mul_qt_qtqt(ob->quat, dquat, ob->quat);
1723 case ROT_MODE_AXISANGLE:
1724 mat3_to_axis_angle(ob->rotAxis, &ob->rotAngle, mat);
1725 sub_v3_v3(ob->rotAxis, ob->drotAxis);
1726 ob->rotAngle -= ob->drotAngle;
1728 default: /* euler */
1729 if(use_compat) mat3_to_compatible_eulO(ob->rot, ob->rot, ob->rotmode, mat);
1730 else mat3_to_eulO(ob->rot, ob->rotmode, mat);
1731 sub_v3_v3(ob->rot, ob->drot);
1735 /* see pchan_apply_mat4() for the equivalent 'pchan' function */
1736 void object_apply_mat4(Object *ob, float mat[][4], const short use_compat, const short use_parent)
1740 if(use_parent && ob->parent) {
1741 float rmat[4][4], diff_mat[4][4], imat[4][4];
1742 mul_m4_m4m4(diff_mat, ob->parentinv, ob->parent->obmat);
1743 invert_m4_m4(imat, diff_mat);
1744 mul_m4_m4m4(rmat, mat, imat); /* get the parent relative matrix */
1745 object_apply_mat4(ob, rmat, use_compat, FALSE);
1747 /* same as below, use rmat rather then mat */
1748 mat4_to_loc_rot_size(ob->loc, rot, ob->size, rmat);
1749 object_mat3_to_rot(ob, rot, use_compat);
1752 mat4_to_loc_rot_size(ob->loc, rot, ob->size, mat);
1753 object_mat3_to_rot(ob, rot, use_compat);
1756 sub_v3_v3(ob->loc, ob->dloc);
1757 sub_v3_v3(ob->size, ob->dsize);
1758 /* object_mat3_to_rot handles delta rotations */
1761 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
1768 object_scale_to_mat3(ob, smat);
1771 object_rot_to_mat3(ob, rmat);
1772 mul_m3_m3m3(mat, rmat, smat);
1775 void object_to_mat4(Object *ob, float mat[][4])
1779 object_to_mat3(ob, tmat);
1781 copy_m4_m3(mat, tmat);
1783 add_v3_v3v3(mat[3], ob->loc, ob->dloc);
1787 int enable_cu_speed= 1;
1789 static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
1792 float vec[4], dir[3], quat[4], radius, ctime;
1793 float timeoffs = 0.0, sf_orig = 0.0;
1798 if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1799 makeDispListCurveTypes(scene, par, 0);
1800 if(cu->path==NULL) return;
1802 /* exception, timeoffset is regarded as distance offset */
1803 if(cu->flag & CU_OFFS_PATHDIST) {
1804 timeoffs = give_timeoffset(ob);
1805 SWAP(float, sf_orig, ob->sf);
1808 /* catch exceptions: feature for nla stride editing */
1809 if(ob->ipoflag & OB_DISABLE_PATH) {
1812 /* catch exceptions: curve paths used as a duplicator */
1813 else if(enable_cu_speed) {
1814 /* ctime is now a proper var setting of Curve which gets set by Animato like any other var that's animated,
1815 * but this will only work if it actually is animated...
1817 * we divide the curvetime calculated in the previous step by the length of the path, to get a time
1818 * factor, which then gets clamped to lie within 0.0 - 1.0 range
1820 if (IS_EQF(cu->pathlen, 0.0f) == 0)
1821 ctime= cu->ctime / cu->pathlen;
1825 CLAMP(ctime, 0.0f, 1.0f);
1828 ctime= scene->r.cfra - give_timeoffset(ob);
1829 if (IS_EQF(cu->pathlen, 0.0f) == 0)
1830 ctime /= cu->pathlen;
1832 CLAMP(ctime, 0.0f, 1.0f);
1835 /* time calculus is correct, now apply distance offset */
1836 if(cu->flag & CU_OFFS_PATHDIST) {
1837 ctime += timeoffs/cu->path->totdist;
1840 SWAP(float, sf_orig, ob->sf);
1845 if( where_on_path(par, ctime, vec, dir, cu->flag & CU_FOLLOW ? quat:NULL, &radius, NULL) ) {
1847 if(cu->flag & CU_FOLLOW) {
1850 vec_to_quat( quat,dir, ob->trackflag, ob->upflag);
1854 q[0]= (float)cos(0.5*vec[3]);
1855 x1= (float)sin(0.5*vec[3]);
1859 mul_qt_qtqt(quat, q, quat);
1861 quat_apply_track(quat, ob->trackflag, ob->upflag);
1864 quat_to_mat4(mat, quat);
1867 if(cu->flag & CU_PATH_RADIUS) {
1868 float tmat[4][4], rmat[4][4];
1869 scale_m4_fl(tmat, radius);
1870 mul_m4_m4m4(rmat, mat, tmat);
1871 copy_m4_m4(mat, rmat);
1874 copy_v3_v3(mat[3], vec);
1879 static void ob_parbone(Object *ob, Object *par, float mat[][4])
1881 bPoseChannel *pchan;
1884 if (par->type!=OB_ARMATURE) {
1889 /* Make sure the bone is still valid */
1890 pchan= get_pose_channel(par->pose, ob->parsubstr);
1892 printf ("Object %s with Bone parent: bone %s doesn't exist\n", ob->id.name+2, ob->parsubstr);
1897 /* get bone transform */
1898 copy_m4_m4(mat, pchan->pose_mat);
1900 /* but for backwards compatibility, the child has to move to the tail */
1901 copy_v3_v3(vec, mat[1]);
1902 mul_v3_fl(vec, pchan->bone->length);
1903 add_v3_v3(mat[3], vec);
1906 static void give_parvert(Object *par, int nr, float *vec)
1911 vec[0]=vec[1]=vec[2]= 0.0f;
1913 if(par->type==OB_MESH) {
1914 Mesh *me= par->data;
1917 em = BKE_mesh_get_editmesh(me);
1918 dm = (em)? em->derivedFinal: par->derivedFinal;
1921 MVert *mvert= dm->getVertArray(dm);
1922 int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
1923 int i, count = 0, vindex, numVerts = dm->getNumVerts(dm);
1925 /* get the average of all verts with (original index == nr) */
1926 for(i = 0; i < numVerts; i++) {
1927 vindex= (index)? index[i]: i;
1930 add_v3_v3(vec, mvert[i].co);
1937 } else if(count > 0) {
1938 mul_v3_fl(vec, 1.0f / count);
1940 /* use first index if its out of range */
1941 dm->getVertCo(dm, 0, vec);
1946 BKE_mesh_end_editmesh(me, em);
1948 else if (ELEM(par->type, OB_CURVE, OB_SURF)) {
1957 nurbs= BKE_curve_nurbs(cu);
1961 while(nu && !found) {
1962 if(nu->type == CU_BEZIER) {
1968 VECCOPY(vec, bezt->vec[1]);
1977 a= nu->pntsu*nu->pntsv;
1981 memcpy(vec, bp->vec, sizeof(float)*3);
1992 else if(par->type==OB_LATTICE) {
1993 Lattice *latt= par->data;
1995 DispList *dl = find_displist(&par->disp, DL_VERTS);
1996 float *co = dl?dl->verts:NULL;
1998 if(latt->editlatt) latt= latt->editlatt->latt;
2000 a= latt->pntsu*latt->pntsv*latt->pntsw;
2006 memcpy(vec, co, 3*sizeof(float));
2008 memcpy(vec, bp->vec, 3*sizeof(float));
2018 static void ob_parvert3(Object *ob, Object *par, float mat[][4])
2020 float cmat[3][3], v1[3], v2[3], v3[3], q[4];
2022 /* in local ob space */
2025 if (ELEM4(par->type, OB_MESH, OB_SURF, OB_CURVE, OB_LATTICE)) {
2027 give_parvert(par, ob->par1, v1);
2028 give_parvert(par, ob->par2, v2);
2029 give_parvert(par, ob->par3, v3);
2031 tri_to_quat( q,v1, v2, v3);
2032 quat_to_mat3( cmat,q);
2033 copy_m4_m3(mat, cmat);
2035 if(ob->type==OB_CURVE) {
2036 VECCOPY(mat[3], v1);
2039 add_v3_v3v3(mat[3], v1, v2);
2040 add_v3_v3(mat[3], v3);
2041 mul_v3_fl(mat[3], 0.3333333f);
2046 // XXX what the hell is this?
2047 static int no_parent_ipo=0;
2048 void set_no_parent_ipo(int val)
2053 void where_is_object_time(Scene *scene, Object *ob, float ctime)
2055 float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
2056 float stime=ctime, fac1, fac2;
2059 /* new version: correct parent+vertexparent and track+parent */
2060 /* this one only calculates direct attached parent and track */
2061 /* is faster, but should keep track of timeoffs */
2063 if(ob==NULL) return;
2065 /* execute drivers only, as animation has already been done */
2066 BKE_animsys_evaluate_animdata(&ob->id, ob->adt, ctime, ADT_RECALC_DRIVERS);
2069 Object *par= ob->parent;
2071 // XXX depreceated - animsys
2072 if(ob->ipoflag & OB_OFFS_PARENT) ctime-= give_timeoffset(ob);
2074 /* hurms, code below conflicts with depgraph... (ton) */
2075 /* and even worse, it gives bad effects for NLA stride too (try ctime != par->ctime, with MBlur) */
2076 if(no_parent_ipo==0 && stime != par->ctime) {
2077 // only for ipo systems?
2080 if(par->proxy_from); // was a copied matrix, no where_is! bad...
2081 else where_is_object_time(scene, par, ctime);
2083 solve_parenting(scene, ob, par, ob->obmat, slowmat, 0);
2088 solve_parenting(scene, ob, par, ob->obmat, slowmat, 0);
2090 if(ob->partype & PARSLOW) {
2091 // include framerate
2092 fac1= ( 1.0f / (1.0f + (float)fabs(give_timeoffset(ob))) );
2093 if(fac1 >= 1.0f) return;
2098 for(a=0; a<16; a++, fp1++, fp2++) {
2099 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
2104 object_to_mat4(ob, ob->obmat);
2107 /* solve constraints */
2108 if (ob->constraints.first && !(ob->transflag & OB_NO_CONSTRAINTS)) {
2111 cob= constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2113 /* constraints need ctime, not stime. Some call where_is_object_time and bsystem_time */
2114 solve_constraints (&ob->constraints, cob, ctime);
2116 constraints_clear_evalob(cob);
2119 /* set negative scale flag in object */
2120 if(is_negative_m4(ob->obmat)) ob->transflag |= OB_NEG_SCALE;
2121 else ob->transflag &= ~OB_NEG_SCALE;
2124 static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul)
2132 object_to_mat4(ob, locmat);
2134 if(ob->partype & PARSLOW) copy_m4_m4(slowmat, obmat);
2136 switch(ob->partype & PARTYPE) {
2139 if(par->type==OB_CURVE) {
2140 if( ((Curve *)par->data)->flag & CU_PATH ) {
2141 ob_parcurve(scene, ob, par, tmat);
2146 if(ok) mul_serie_m4(totmat, par->obmat, tmat,
2147 NULL, NULL, NULL, NULL, NULL, NULL);
2148 else copy_m4_m4(totmat, par->obmat);
2152 ob_parbone(ob, par, tmat);
2153 mul_serie_m4(totmat, par->obmat, tmat,
2154 NULL, NULL, NULL, NULL, NULL, NULL);
2160 VECCOPY(totmat[3], par->obmat[3]);
2163 give_parvert(par, ob->par1, vec);
2164 mul_v3_m4v3(totmat[3], par->obmat, vec);
2168 ob_parvert3(ob, par, tmat);
2170 mul_serie_m4(totmat, par->obmat, tmat,
2171 NULL, NULL, NULL, NULL, NULL, NULL);
2175 copy_m4_m4(totmat, par->obmat);
2180 mul_serie_m4(tmat, totmat, ob->parentinv,
2181 NULL, NULL, NULL, NULL, NULL, NULL);
2182 mul_serie_m4(obmat, tmat, locmat,
2183 NULL, NULL, NULL, NULL, NULL, NULL);
2189 // external usable originmat
2190 copy_m3_m4(originmat, tmat);
2192 // origin, voor help line
2193 if( (ob->partype & PARTYPE)==PARSKEL ) {
2194 VECCOPY(ob->orig, par->obmat[3]);
2197 VECCOPY(ob->orig, totmat[3]);
2203 void where_is_object(struct Scene *scene, Object *ob)
2205 where_is_object_time(scene, ob, (float)scene->r.cfra);
2209 void where_is_object_simul(Scene *scene, Object *ob)
2210 /* was written for the old game engine (until 2.04) */
2211 /* It seems that this function is only called
2212 for a lamp that is the child of another object */
2217 float slowmat[4][4];
2225 solve_parenting(scene, ob, par, ob->obmat, slowmat, 1);
2227 if(ob->partype & PARSLOW) {
2229 fac1= (float)(1.0/(1.0+ fabs(give_timeoffset(ob))));
2233 for(a=0; a<16; a++, fp1++, fp2++) {
2234 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
2240 object_to_mat4(ob, ob->obmat);
2243 /* solve constraints */
2244 if (ob->constraints.first) {
2247 cob= constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2248 solve_constraints(&ob->constraints, cob, (float)scene->r.cfra);
2249 constraints_clear_evalob(cob);
2253 /* for calculation of the inverse parent transform, only used for editor */
2254 void what_does_parent(Scene *scene, Object *ob, Object *workob)
2256 clear_workob(workob);
2258 unit_m4(workob->obmat);
2259 unit_m4(workob->parentinv);
2260 unit_m4(workob->constinv);
2261 workob->parent= ob->parent;
2263 workob->trackflag= ob->trackflag;
2264 workob->upflag= ob->upflag;
2266 workob->partype= ob->partype;
2267 workob->par1= ob->par1;
2268 workob->par2= ob->par2;
2269 workob->par3= ob->par3;
2271 workob->constraints.first = ob->constraints.first;
2272 workob->constraints.last = ob->constraints.last;
2274 strcpy(workob->parsubstr, ob->parsubstr);
2276 where_is_object(scene, workob);
2279 BoundBox *unit_boundbox(void)
2282 float min[3] = {-1.0f,-1.0f,-1.0f}, max[3] = {-1.0f,-1.0f,-1.0f};
2284 bb= MEM_callocN(sizeof(BoundBox), "bb");
2285 boundbox_set_from_min_max(bb, min, max);
2290 void boundbox_set_from_min_max(BoundBox *bb, float min[3], float max[3])
2292 bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
2293 bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
2295 bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
2296 bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
2298 bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
2299 bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
2302 BoundBox *object_get_boundbox(Object *ob)
2306 if(ob->type==OB_MESH) {
2307 bb = mesh_get_bb(ob);
2309 else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2310 bb= ob->bb ? ob->bb : ( (Curve *)ob->data )->bb;
2312 else if(ob->type==OB_MBALL) {
2318 /* used to temporally disable/enable boundbox */
2319 void object_boundbox_flag(Object *ob, int flag, int set)
2321 BoundBox *bb= object_get_boundbox(ob);
2323 if(set) bb->flag |= flag;
2324 else bb->flag &= ~flag;
2328 void object_get_dimensions(Object *ob, float *value)
2330 BoundBox *bb = NULL;
2332 bb= object_get_boundbox(ob);
2336 mat4_to_size( scale,ob->obmat);
2338 value[0] = fabsf(scale[0]) * (bb->vec[4][0] - bb->vec[0][0]);
2339 value[1] = fabsf(scale[1]) * (bb->vec[2][1] - bb->vec[0][1]);
2340 value[2] = fabsf(scale[2]) * (bb->vec[1][2] - bb->vec[0][2]);
2342 value[0] = value[1] = value[2] = 0.f;
2346 void object_set_dimensions(Object *ob, const float *value)
2348 BoundBox *bb = NULL;
2350 bb= object_get_boundbox(ob);
2352 float scale[3], len[3];
2354 mat4_to_size( scale,ob->obmat);
2356 len[0] = bb->vec[4][0] - bb->vec[0][0];
2357 len[1] = bb->vec[2][1] - bb->vec[0][1];
2358 len[2] = bb->vec[1][2] - bb->vec[0][2];
2360 if (len[0] > 0.f) ob->size[0] = value[0] / len[0];
2361 if (len[1] > 0.f) ob->size[1] = value[1] / len[1];
2362 if (len[2] > 0.f) ob->size[2] = value[2] / len[2];
2366 void minmax_object(Object *ob, float *min, float *max)
2381 if(cu->bb==NULL) tex_space_curve(cu);
2384 for(a=0; a<8; a++) {
2385 mul_m4_v3(ob->obmat, bb.vec[a]);
2386 DO_MINMAX(bb.vec[a], min, max);
2391 bPoseChannel *pchan;
2392 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2393 mul_v3_m4v3(vec, ob->obmat, pchan->pose_head);
2394 DO_MINMAX(vec, min, max);
2395 mul_v3_m4v3(vec, ob->obmat, pchan->pose_tail);
2396 DO_MINMAX(vec, min, max);
2400 /* no break, get_mesh will give NULL and it passes on to default */
2405 bb = *mesh_get_bb(ob);
2407 for(a=0; a<8; a++) {
2408 mul_m4_v3(ob->obmat, bb.vec[a]);
2409 DO_MINMAX(bb.vec[a], min, max);
2412 if(min[0] < max[0] ) break;
2414 /* else here no break!!!, mesh can be zero sized */
2417 DO_MINMAX(ob->obmat[3], min, max);
2419 copy_v3_v3(vec, ob->obmat[3]);
2420 add_v3_v3(vec, ob->size);
2421 DO_MINMAX(vec, min, max);
2423 copy_v3_v3(vec, ob->obmat[3]);
2424 sub_v3_v3(vec, ob->size);
2425 DO_MINMAX(vec, min, max);
2430 int minmax_object_duplis(Scene *scene, Object *ob, float *min, float *max)
2433 if ((ob->transflag & OB_DUPLI)==0) {
2439 lb= object_duplilist(scene, ob);
2440 for(dob= lb->first; dob; dob= dob->next) {
2441 if(dob->no_draw == 0) {
2442 BoundBox *bb= object_get_boundbox(dob->ob);
2446 for(i=0; i<8; i++) {
2448 mul_v3_m4v3(vec, dob->mat, bb->vec[i]);
2449 DO_MINMAX(vec, min, max);
2456 free_object_duplilist(lb); /* does restore */
2462 /* copied from DNA_object_types.h */
2463 typedef struct ObTfmBack {
2464 float loc[3], dloc[3], orig[3];
2465 float size[3], dsize[3]; /* scale and delta scale */
2466 float rot[3], drot[3]; /* euler rotation */
2467 float quat[4], dquat[4]; /* quaternion rotation */
2468 float rotAxis[3], drotAxis[3]; /* axis angle rotation - axis part */
2469 float rotAngle, drotAngle; /* axis angle rotation - angle part */
2470 float obmat[4][4]; /* final worldspace matrix with constraints & animsys applied */
2471 float parentinv[4][4]; /* inverse result of parent, so that object doesn't 'stick' to parent */
2472 float constinv[4][4]; /* inverse result of constraints. doesn't include effect of parent or object local transform */
2473 float imat[4][4]; /* inverse matrix of 'obmat' for during render, old game engine, temporally: ipokeys of transform */
2476 void *object_tfm_backup(Object *ob)
2478 ObTfmBack *obtfm= MEM_mallocN(sizeof(ObTfmBack), "ObTfmBack");
2479 copy_v3_v3(obtfm->loc, ob->loc);
2480 copy_v3_v3(obtfm->dloc, ob->dloc);
2481 copy_v3_v3(obtfm->orig, ob->orig);
2482 copy_v3_v3(obtfm->size, ob->size);
2483 copy_v3_v3(obtfm->dsize, ob->dsize);
2484 copy_v3_v3(obtfm->rot, ob->rot);
2485 copy_v3_v3(obtfm->drot, ob->drot);
2486 copy_qt_qt(obtfm->quat, ob->quat);
2487 copy_qt_qt(obtfm->dquat, ob->dquat);
2488 copy_v3_v3(obtfm->rotAxis, ob->rotAxis);
2489 copy_v3_v3(obtfm->drotAxis, ob->drotAxis);
2490 obtfm->rotAngle= ob->rotAngle;
2491 obtfm->drotAngle= ob->drotAngle;
2492 copy_m4_m4(obtfm->obmat, ob->obmat);
2493 copy_m4_m4(obtfm->parentinv, ob->parentinv);
2494 copy_m4_m4(obtfm->constinv, ob->constinv);
2495 copy_m4_m4(obtfm->imat, ob->imat);
2497 return (void *)obtfm;
2500 void object_tfm_restore(Object *ob, void *obtfm_pt)
2502 ObTfmBack *obtfm= (ObTfmBack *)obtfm_pt;
2503 copy_v3_v3(ob->loc, obtfm->loc);
2504 copy_v3_v3(ob->dloc, obtfm->dloc);
2505 copy_v3_v3(ob->orig, obtfm->orig);
2506 copy_v3_v3(ob->size, obtfm->size);
2507 copy_v3_v3(ob->dsize, obtfm->dsize);
2508 copy_v3_v3(ob->rot, obtfm->rot);
2509 copy_v3_v3(ob->drot, obtfm->drot);
2510 copy_qt_qt(ob->quat, obtfm->quat);
2511 copy_qt_qt(ob->dquat, obtfm->dquat);
2512 copy_v3_v3(ob->rotAxis, obtfm->rotAxis);
2513 copy_v3_v3(ob->drotAxis, obtfm->drotAxis);
2514 ob->rotAngle= obtfm->rotAngle;
2515 ob->drotAngle= obtfm->drotAngle;
2516 copy_m4_m4(ob->obmat, obtfm->obmat);
2517 copy_m4_m4(ob->parentinv, obtfm->parentinv);
2518 copy_m4_m4(ob->constinv, obtfm->constinv);
2519 copy_m4_m4(ob->imat, obtfm->imat);
2522 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
2523 /* local_object->proxy == pointer to library object, saved in files and read */
2525 /* function below is polluted with proxy exceptions, cleanup will follow! */
2527 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
2528 /* requires flags to be set! */
2529 void object_handle_update(Scene *scene, Object *ob)
2531 if(ob->recalc & OB_RECALC_ALL) {
2532 /* speed optimization for animation lookups */
2534 make_pose_channels_hash(ob->pose);
2536 if(ob->recalc & OB_RECALC_DATA) {
2537 if(ob->type==OB_ARMATURE) {
2538 /* this happens for reading old files and to match library armatures
2539 with poses we do it ahead of where_is_object to ensure animation
2540 is evaluated on the rebuilt pose, otherwise we get incorrect poses
2542 if(ob->pose==NULL || (ob->pose->flag & POSE_RECALC))
2543 armature_rebuild_pose(ob, ob->data);
2547 /* XXX new animsys warning: depsgraph tag OB_RECALC_DATA should not skip drivers,
2548 which is only in where_is_object now */
2549 // XXX: should this case be OB_RECALC_OB instead?
2550 if(ob->recalc & OB_RECALC_ALL) {
2553 printf("recalcob %s\n", ob->id.name+2);
2555 /* handle proxy copy for target */
2556 if(ob->id.lib && ob->proxy_from) {
2557 // printf("ob proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2558 if(ob->proxy_from->proxy_group) {/* transform proxy into group space */
2559 Object *obg= ob->proxy_from->proxy_group;
2560 invert_m4_m4(obg->imat, obg->obmat);
2561 mul_m4_m4m4(ob->obmat, ob->proxy_from->obmat, obg->imat);
2562 if(obg->dup_group) { /* should always be true */
2563 add_v3_v3(ob->obmat[3], obg->dup_group->dupli_ofs);
2567 copy_m4_m4(ob->obmat, ob->proxy_from->obmat);
2570 where_is_object(scene, ob);
2573 if(ob->recalc & OB_RECALC_DATA) {
2574 ID *data_id= (ID *)ob->data;
2575 AnimData *adt= BKE_animdata_from_id(data_id);
2576 float ctime= (float)scene->r.cfra; // XXX this is bad...
2581 printf("recalcdata %s\n", ob->id.name+2);
2584 /* evaluate drivers */
2585 // XXX: for mesh types, should we push this to derivedmesh instead?
2586 BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
2589 /* includes all keys and modifiers */
2593 #if 0 // XXX, comment for 2.56a release, background wont set 'scene->customdata_mask'
2594 EditMesh *em = (ob == scene->obedit)? BKE_mesh_get_editmesh(ob->data): NULL;
2595 BLI_assert((scene->customdata_mask & CD_MASK_BAREMESH) == CD_MASK_BAREMESH);
2597 makeDerivedMesh(scene, ob, em, scene->customdata_mask); /* was CD_MASK_BAREMESH */
2598 BKE_mesh_end_editmesh(ob->data, em);
2600 makeDerivedMesh(scene, ob, NULL, scene->customdata_mask);
2602 #else /* ensure CD_MASK_BAREMESH for now */
2603 EditMesh *em = (ob == scene->obedit)? BKE_mesh_get_editmesh(ob->data): NULL;
2605 makeDerivedMesh(scene, ob, em, scene->customdata_mask | CD_MASK_BAREMESH); /* was CD_MASK_BAREMESH */
2606 BKE_mesh_end_editmesh(ob->data, em);
2608 makeDerivedMesh(scene, ob, NULL, scene->customdata_mask | CD_MASK_BAREMESH);
2615 if(ob->id.lib && ob->proxy_from) {
2616 // printf("pose proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2617 copy_pose_result(ob->pose, ob->proxy_from->pose);
2620 where_is_pose(scene, ob);
2625 makeDispListMBall(scene, ob);
2631 makeDispListCurveTypes(scene, ob, 0);
2635 lattice_calc_modifiers(scene, ob);
2640 if(ob->particlesystem.first) {
2641 ParticleSystem *tpsys, *psys;
2643 ob->transflag &= ~OB_DUPLIPARTS;
2645 psys= ob->particlesystem.first;
2647 if(psys_check_enabled(ob, psys)) {
2648 /* check use of dupli objects here */
2649 if(psys->part && (psys->part->draw_as == PART_DRAW_REND || G.rendering) &&
2650 ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob)
2651 || (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
2652 ob->transflag |= OB_DUPLIPARTS;
2654 particle_system_update(scene, ob, psys);
2657 else if(psys->flag & PSYS_DELETE) {
2659 BLI_remlink(&ob->particlesystem, psys);
2667 if(G.rendering && ob->transflag & OB_DUPLIPARTS) {
2668 /* this is to make sure we get render level duplis in groups:
2669 * the derivedmesh must be created before init_render_mesh,
2670 * since object_duplilist does dupliparticles before that */
2671 dm = mesh_create_derived_render(scene, ob, CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL);
2674 for(psys=ob->particlesystem.first; psys; psys=psys->next)
2675 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
2679 /* check if quick cache is needed */
2680 BKE_ptcache_ids_from_object(&pidlist, ob, scene, MAX_DUPLI_RECUR);
2682 for(pid=pidlist.first; pid; pid=pid->next) {
2683 if((pid->cache->flag & PTCACHE_BAKED)
2684 || (pid->cache->flag & PTCACHE_QUICK_CACHE)==0)
2687 if(pid->cache->flag & PTCACHE_OUTDATED || (pid->cache->flag & PTCACHE_SIMULATION_VALID)==0) {
2688 scene->physics_settings.quick_cache_step =
2689 scene->physics_settings.quick_cache_step ?
2690 MIN2(scene->physics_settings.quick_cache_step, pid->cache->step) :
2695 BLI_freelistN(&pidlist);
2698 /* the no-group proxy case, we call update */
2699 if(ob->proxy && ob->proxy_group==NULL) {
2700 /* set pointer in library proxy target, for copying, but restore it */
2701 ob->proxy->proxy_from= ob;
2702 // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
2703 object_handle_update(scene, ob->proxy);
2706 ob->recalc &= ~OB_RECALC_ALL;
2709 /* the case when this is a group proxy, object_update is called in group.c */
2711 ob->proxy->proxy_from= ob;
2712 // printf("set proxy pointer for later group stuff %s\n", ob->id.name);
2716 float give_timeoffset(Object *ob) {
2717 if ((ob->ipoflag & OB_OFFS_PARENTADD) && ob->parent) {
2718 return ob->sf + give_timeoffset(ob->parent);
2724 int give_obdata_texspace(Object *ob, short **texflag, float **loc, float **size, float **rot) {
2729 switch (GS(((ID *)ob->data)->name)) {
2733 if (texflag) *texflag = &me->texflag;
2734 if (loc) *loc = me->loc;
2735 if (size) *size = me->size;
2736 if (rot) *rot = me->rot;
2741 Curve *cu= ob->data;
2742 if (texflag) *texflag = &cu->texflag;
2743 if (loc) *loc = cu->loc;
2744 if (size) *size = cu->size;
2745 if (rot) *rot = cu->rot;
2750 MetaBall *mb= ob->data;
2751 if (texflag) *texflag = &mb->texflag;
2752 if (loc) *loc = mb->loc;
2753 if (size) *size = mb->size;
2754 if (rot) *rot = mb->rot;
2764 * Test a bounding box for ray intersection
2765 * assumes the ray is already local to the boundbox space
2767 int ray_hit_boundbox(struct BoundBox *bb, float ray_start[3], float ray_normal[3])
2769 static int triangle_indexes[12][3] = {{0, 1, 2}, {0, 2, 3},
2770 {3, 2, 6}, {3, 6, 7},
2771 {1, 2, 6}, {1, 6, 5},
2772 {5, 6, 7}, {4, 5, 7},
2773 {0, 3, 7}, {0, 4, 7},
2774 {0, 1, 5}, {0, 4, 5}};
2778 for (i = 0; i < 12 && result == 0; i++)
2782 v1 = triangle_indexes[i][0];
2783 v2 = triangle_indexes[i][1];
2784 v3 = triangle_indexes[i][2];
2785 result = isect_ray_tri_v3(ray_start, ray_normal, bb->vec[v1], bb->vec[v2], bb->vec[v3], &lambda, NULL);
2791 static int pc_cmp(void *a, void *b)
2793 LinkData *ad = a, *bd = b;
2794 if(GET_INT_FROM_POINTER(ad->data) > GET_INT_FROM_POINTER(bd->data))
2799 int object_insert_ptcache(Object *ob)
2801 LinkData *link = NULL;
2804 BLI_sortlist(&ob->pc_ids, pc_cmp);
2806 for(link=ob->pc_ids.first, i = 0; link; link=link->next, i++)
2808 int index = GET_INT_FROM_POINTER(link->data);
2814 link = MEM_callocN(sizeof(LinkData), "PCLink");
2815 link->data = SET_INT_IN_POINTER(i);
2816 BLI_addtail(&ob->pc_ids, link);
2821 void object_camera_mode(RenderData *rd, Object *camera)
2823 rd->mode &= ~(R_ORTHO|R_PANORAMA);
2824 if(camera && camera->type==OB_CAMERA) {
2825 Camera *cam= camera->data;
2826 if(cam->type == CAM_ORTHO) rd->mode |= R_ORTHO;
2827 if(cam->flag & CAM_PANORAMA) rd->mode |= R_PANORAMA;
2831 /* 'lens' may be set for envmap only */
2832 void object_camera_matrix(
2833 RenderData *rd, Object *camera, int winx, int winy, short field_second,
2834 float winmat[][4], rctf *viewplane, float *clipsta, float *clipend, float *lens, float *ycor,
2835 float *viewdx, float *viewdy
2839 float shiftx=0.0, shifty=0.0, winside, viewfac;
2840 short is_ortho= FALSE;
2843 (*ycor)= rd->yasp / rd->xasp;
2844 if(rd->mode & R_FIELDS)
2847 if(camera->type==OB_CAMERA) {
2850 if(cam->type == CAM_ORTHO) {
2854 /* solve this too... all time depending stuff is in convertblender.c?
2855 * Need to update the camera early because it's used for projection matrices
2856 * and other stuff BEFORE the animation update loop is done
2858 #if 0 // XXX old animation system
2860 calc_ipo(cam->ipo, frame_to_float(re->scene, re->r.cfra));
2861 execute_ipo(&cam->id, cam->ipo);
2863 #endif // XXX old animation system
2867 (*clipsta)= cam->clipsta;
2868 (*clipend)= cam->clipend;
2870 else if(camera->type==OB_LAMP) {
2871 Lamp *la= camera->data;
2872 float fac= cosf((float)M_PI*la->spotsize/360.0f);
2873 float phi= acos(fac);
2875 (*lens)= 16.0f*fac/sinf(phi);
2878 (*clipsta)= la->clipsta;
2879 (*clipend)= la->clipend;
2881 else { /* envmap exception... */;
2885 if((*clipsta)==0.0f || (*clipend)==0.0f) {
2887 (*clipend)= 1000.0f;
2891 /* ortho only with camera available */
2892 if(cam && is_ortho) {
2893 if(rd->xasp*winx >= rd->yasp*winy) {
2897 viewfac= (*ycor) * winy;
2899 /* ortho_scale == 1.0 means exact 1 to 1 mapping */
2900 pixsize= cam->ortho_scale/viewfac;
2903 if(rd->xasp*winx >= rd->yasp*winy) viewfac= ((*lens) * winx)/32.0f;
2904 else viewfac= (*ycor) * ((*lens) * winy)/32.0f;
2905 pixsize= (*clipsta) / viewfac;
2908 /* viewplane fully centered, zbuffer fills in jittered between -.5 and +.5 */
2909 winside= MAX2(winx, winy);
2910 viewplane->xmin= -0.5f*(float)winx + shiftx*winside;
2911 viewplane->ymin= -0.5f*(*ycor)*(float)winy + shifty*winside;
2912 viewplane->xmax= 0.5f*(float)winx + shiftx*winside;
2913 viewplane->ymax= 0.5f*(*ycor)*(float)winy + shifty*winside;
2916 if(rd->mode & R_ODDFIELD) {
2917 viewplane->ymin-= 0.5f * (*ycor);
2918 viewplane->ymax-= 0.5f * (*ycor);
2921 viewplane->ymin+= 0.5f * (*ycor);
2922 viewplane->ymax+= 0.5f * (*ycor);
2925 /* the window matrix is used for clipping, and not changed during OSA steps */
2926 /* using an offset of +0.5 here would give clip errors on edges */
2927 viewplane->xmin *= pixsize;
2928 viewplane->xmax *= pixsize;
2929 viewplane->ymin *= pixsize;
2930 viewplane->ymax *= pixsize;
2933 (*viewdy)= (*ycor) * pixsize;
2936 orthographic_m4(winmat, viewplane->xmin, viewplane->xmax, viewplane->ymin, viewplane->ymax, *clipsta, *clipend);
2938 perspective_m4(winmat, viewplane->xmin, viewplane->xmax, viewplane->ymin, viewplane->ymax, *clipsta, *clipend);
2943 static int pc_findindex(ListBase *listbase, int index)
2945 LinkData *link= NULL;
2948 if (listbase == NULL) return -1;
2950 link= listbase->first;
2952 if ((int)link->data == index)
2962 void object_delete_ptcache(Object *ob, int index)
2964 int list_index = pc_findindex(&ob->pc_ids, index);
2965 LinkData *link = BLI_findlink(&ob->pc_ids, list_index);
2966 BLI_freelinkN(&ob->pc_ids, link);
2970 /* shape key utility function */
2972 /************************* Mesh ************************/
2973 static KeyBlock *insert_meshkey(Scene *scene, Object *ob, const char *name, int from_mix)
2981 key= me->key= add_key((ID *)me);
2982 key->type= KEY_RELATIVE;
2986 if(newkey || from_mix==FALSE) {
2987 /* create from mesh */
2988 kb= add_keyblock(key, name);
2989 mesh_to_key(me, kb);
2992 /* copy from current values */
2993 float *data= do_ob_key(scene, ob);
2995 /* create new block with prepared data */
2996 kb= add_keyblock(key, name);
2998 kb->totelem= me->totvert;
3003 /************************* Lattice ************************/
3004 static KeyBlock *insert_lattkey(Scene *scene, Object *ob, const char *name, int from_mix)
3006 Lattice *lt= ob->data;
3012 key= lt->key= add_key( (ID *)lt);
3013 key->type= KEY_RELATIVE;
3017 if(newkey || from_mix==FALSE) {
3018 kb= add_keyblock(key, name);
3020 /* create from lattice */
3021 latt_to_key(lt, kb);
3024 /* copy from current values */
3025 float *data= do_ob_key(scene, ob);
3027 /* create new block with prepared data */
3028 kb= add_keyblock(key, name);
3029 kb->totelem= lt->pntsu*lt->pntsv*lt->pntsw;
3035 /************************* Curve ************************/
3036 static KeyBlock *insert_curvekey(Scene *scene, Object *ob, const char *name, int from_mix)
3038 Curve *cu= ob->data;
3041 ListBase *lb= BKE_curve_nurbs(cu);
3045 key= cu->key= add_key( (ID *)cu);
3046 key->type = KEY_RELATIVE;
3050 if(newkey || from_mix==FALSE) {
3051 /* create from curve */
3052 kb= add_keyblock(key, name);
3054 KeyBlock *basekb= (KeyBlock *)key->block.first;
3055 kb->data= MEM_dupallocN(basekb->data);
3056 kb->totelem= basekb->totelem;
3057 } else curve_to_key(cu, kb, lb);
3060 /* copy from current values */
3061 float *data= do_ob_key(scene, ob);
3063 /* create new block with prepared data */
3064 kb= add_keyblock(key, name);
3065 kb->totelem= count_curveverts(lb);
3072 KeyBlock *object_insert_shape_key(Scene *scene, Object *ob, const char&