2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/object.c
37 #include "MEM_guardedalloc.h"
39 #include "DNA_anim_types.h"
40 #include "DNA_armature_types.h"
41 #include "DNA_camera_types.h"
42 #include "DNA_constraint_types.h"
43 #include "DNA_group_types.h"
44 #include "DNA_key_types.h"
45 #include "DNA_lattice_types.h"
46 #include "DNA_material_types.h"
47 #include "DNA_meta_types.h"
48 #include "DNA_meshdata_types.h"
49 #include "DNA_movieclip_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_sequence_types.h"
53 #include "DNA_smoke_types.h"
54 #include "DNA_sound_types.h"
55 #include "DNA_space_types.h"
56 #include "DNA_view3d_types.h"
57 #include "DNA_world_types.h"
59 #include "BLI_blenlib.h"
60 #include "BLI_bpath.h"
61 #include "BLI_editVert.h"
64 #include "BLI_utildefines.h"
67 #include "BKE_global.h"
68 #include "BKE_idprop.h"
69 #include "BKE_armature.h"
70 #include "BKE_action.h"
71 #include "BKE_bullet.h"
72 #include "BKE_colortools.h"
73 #include "BKE_deform.h"
74 #include "BKE_DerivedMesh.h"
75 #include "BKE_animsys.h"
77 #include "BKE_constraint.h"
78 #include "BKE_curve.h"
79 #include "BKE_displist.h"
80 #include "BKE_effect.h"
81 #include "BKE_fcurve.h"
82 #include "BKE_group.h"
83 #include "BKE_icons.h"
86 #include "BKE_lattice.h"
87 #include "BKE_library.h"
89 #include "BKE_tessmesh.h"
90 #include "BKE_mball.h"
91 #include "BKE_modifier.h"
93 #include "BKE_object.h"
94 #include "BKE_paint.h"
95 #include "BKE_particle.h"
96 #include "BKE_pointcache.h"
97 #include "BKE_property.h"
99 #include "BKE_scene.h"
100 #include "BKE_sequencer.h"
101 #include "BKE_speaker.h"
102 #include "BKE_softbody.h"
103 #include "BKE_material.h"
104 #include "BKE_camera.h"
106 #include "LBM_fluidsim.h"
109 #include "BPY_extern.h"
112 #include "GPU_material.h"
114 /* Local function protos */
115 static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul);
117 float originmat[3][3]; /* after where_is_object(), can be used in other functions (bad!) */
119 void clear_workob(Object *workob)
121 memset(workob, 0, sizeof(Object));
123 workob->size[0]= workob->size[1]= workob->size[2]= 1.0f;
124 workob->rotmode= ROT_MODE_EUL;
127 void copy_baseflags(struct Scene *scene)
129 Base *base= scene->base.first;
132 base->object->flag= base->flag;
137 void copy_objectflags(struct Scene *scene)
139 Base *base= scene->base.first;
142 base->flag= base->object->flag;
147 void update_base_layer(struct Scene *scene, Object *ob)
149 Base *base= scene->base.first;
152 if (base->object == ob) base->lay= ob->lay;
157 void object_free_particlesystems(Object *ob)
159 while(ob->particlesystem.first){
160 ParticleSystem *psys = ob->particlesystem.first;
162 BLI_remlink(&ob->particlesystem,psys);
168 void object_free_softbody(Object *ob)
176 void object_free_bulletsoftbody(Object *ob)
184 void object_free_modifiers(Object *ob)
186 while (ob->modifiers.first) {
187 ModifierData *md = ob->modifiers.first;
189 BLI_remlink(&ob->modifiers, md);
194 /* particle modifiers were freed, so free the particlesystems as well */
195 object_free_particlesystems(ob);
197 /* same for softbody */
198 object_free_softbody(ob);
201 void object_link_modifiers(struct Object *ob, struct Object *from)
204 object_free_modifiers(ob);
206 for (md=from->modifiers.first; md; md=md->next) {
207 ModifierData *nmd = NULL;
209 if(ELEM4(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_ParticleInstance, eModifierType_Collision)) continue;
211 nmd = modifier_new(md->type);
212 modifier_copyData(md, nmd);
213 BLI_addtail(&ob->modifiers, nmd);
216 copy_object_particlesystems(ob, from);
217 copy_object_softbody(ob, from);
219 // TODO: smoke?, cloth?
222 /* here we will collect all local displist stuff */
223 /* also (ab)used in depsgraph */
224 void object_free_display(Object *ob)
226 if(ob->derivedDeform) {
227 ob->derivedDeform->needsFree = 1;
228 ob->derivedDeform->release(ob->derivedDeform);
229 ob->derivedDeform= NULL;
231 if(ob->derivedFinal) {
232 ob->derivedFinal->needsFree = 1;
233 ob->derivedFinal->release(ob->derivedFinal);
234 ob->derivedFinal= NULL;
237 freedisplist(&ob->disp);
240 void free_sculptsession_deformMats(SculptSession *ss)
242 if(ss->orig_cos) MEM_freeN(ss->orig_cos);
243 if(ss->deform_cos) MEM_freeN(ss->deform_cos);
244 if(ss->deform_imats) MEM_freeN(ss->deform_imats);
247 ss->deform_cos = NULL;
248 ss->deform_imats = NULL;
251 void free_sculptsession(Object *ob)
253 if(ob && ob->sculpt) {
254 SculptSession *ss = ob->sculpt;
255 DerivedMesh *dm= ob->derivedFinal;
258 BLI_pbvh_free(ss->pbvh);
259 if(dm && dm->getPBVH)
260 dm->getPBVH(NULL, dm); /* signal to clear */
263 MEM_freeN(ss->texcache);
266 MEM_freeN(ss->layer_co);
269 MEM_freeN(ss->orig_cos);
271 MEM_freeN(ss->deform_cos);
273 MEM_freeN(ss->deform_imats);
282 /* do not free object itself */
283 void free_object(Object *ob)
287 object_free_display(ob);
289 /* disconnect specific data */
294 if(ob->type==OB_MESH) unlink_mesh(ob->data);
295 else if(ob->type==OB_CURVE) unlink_curve(ob->data);
296 else if(ob->type==OB_MBALL) unlink_mball(ob->data);
301 for(a=0; a<ob->totcol; a++) {
302 if(ob->mat[a]) ob->mat[a]->id.us--;
304 if(ob->mat) MEM_freeN(ob->mat);
305 if(ob->matbits) MEM_freeN(ob->matbits);
308 if(ob->bb) MEM_freeN(ob->bb);
310 if(ob->adt) BKE_free_animdata((ID *)ob);
311 if(ob->poselib) ob->poselib->id.us--;
312 if(ob->gpd) ((ID *)ob->gpd)->us--;
313 if(ob->defbase.first)
314 BLI_freelistN(&ob->defbase);
318 animviz_free_motionpath(ob->mpath);
319 free_properties(&ob->prop);
320 object_free_modifiers(ob);
322 free_sensors(&ob->sensors);
323 free_controllers(&ob->controllers);
324 free_actuators(&ob->actuators);
326 free_constraints(&ob->constraints);
328 free_partdeflect(ob->pd);
330 if(ob->soft) sbFree(ob->soft);
331 if(ob->bsoft) bsbFree(ob->bsoft);
332 if(ob->gpulamp.first) GPU_lamp_free(ob);
334 free_sculptsession(ob);
336 if(ob->pc_ids.first) BLI_freelistN(&ob->pc_ids);
339 static void unlink_object__unlinkModifierLinks(void *userData, Object *ob, Object **obpoin)
341 Object *unlinkOb = userData;
343 if (*obpoin==unlinkOb) {
345 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME; // XXX: should this just be OB_RECALC_DATA?
349 void unlink_object(Object *ob)
362 //bActionStrip *strip; // XXX animsys
368 unlink_controllers(&ob->controllers);
369 unlink_actuators(&ob->actuators);
371 /* check all objects: parents en bevels and fields, also from libraries */
372 // FIXME: need to check all animation blocks (drivers)
373 obt= bmain->object.first;
377 if(obt->proxy_from==ob) {
378 obt->proxy_from= NULL;
379 obt->recalc |= OB_RECALC_OB;
381 if(obt->proxy_group==ob)
382 obt->proxy_group= NULL;
384 if(obt->parent==ob) {
386 obt->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
389 modifiers_foreachObjectLink(obt, unlink_object__unlinkModifierLinks, ob);
391 if ELEM(obt->type, OB_CURVE, OB_FONT) {
396 obt->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
398 if(cu->taperobj==ob) {
400 obt->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
402 if(cu->textoncurve==ob) {
403 cu->textoncurve= NULL;
404 obt->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
407 else if(obt->type==OB_ARMATURE && obt->pose) {
409 for(pchan= obt->pose->chanbase.first; pchan; pchan= pchan->next) {
410 for (con = pchan->constraints.first; con; con=con->next) {
411 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
412 ListBase targets = {NULL, NULL};
413 bConstraintTarget *ct;
415 if (cti && cti->get_constraint_targets) {
416 cti->get_constraint_targets(con, &targets);
418 for (ct= targets.first; ct; ct= ct->next) {
421 ct->subtarget[0]= '\0';
422 obt->recalc |= OB_RECALC_DATA;
426 if (cti->flush_constraint_targets)
427 cti->flush_constraint_targets(con, &targets, 0);
430 if(pchan->custom==ob)
433 } else if(ELEM(OB_MBALL, ob->type, obt->type)) {
434 if(is_mball_basis_for(obt, ob))
435 obt->recalc|= OB_RECALC_DATA;
438 sca_remove_ob_poin(obt, ob);
440 for (con = obt->constraints.first; con; con=con->next) {
441 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
442 ListBase targets = {NULL, NULL};
443 bConstraintTarget *ct;
445 if (cti && cti->get_constraint_targets) {
446 cti->get_constraint_targets(con, &targets);
448 for (ct= targets.first; ct; ct= ct->next) {
451 ct->subtarget[0]= '\0';
452 obt->recalc |= OB_RECALC_DATA;
456 if (cti->flush_constraint_targets)
457 cti->flush_constraint_targets(con, &targets, 0);
461 /* object is deflector or field */
464 obt->recalc |= OB_RECALC_DATA;
467 for(md=obt->modifiers.first; md; md=md->next)
468 if(md->type == eModifierType_Cloth)
469 obt->recalc |= OB_RECALC_DATA;
473 #if 0 // XXX old animation system
474 for(strip= obt->nlastrips.first; strip; strip= strip->next) {
475 if(strip->object==ob)
478 if(strip->modifiers.first) {
479 bActionModifier *amod;
480 for(amod= strip->modifiers.first; amod; amod= amod->next)
485 #endif // XXX old animation system
487 /* particle systems */
488 if(obt->particlesystem.first) {
489 ParticleSystem *tpsys= obt->particlesystem.first;
490 for(; tpsys; tpsys=tpsys->next) {
491 BoidState *state = NULL;
492 BoidRule *rule = NULL;
494 ParticleTarget *pt = tpsys->targets.first;
495 for(; pt; pt=pt->next) {
498 obt->recalc |= OB_RECALC_DATA;
503 if(tpsys->target_ob==ob) {
504 tpsys->target_ob= NULL;
505 obt->recalc |= OB_RECALC_DATA;
508 if(tpsys->part->dup_ob==ob)
509 tpsys->part->dup_ob= NULL;
511 if(tpsys->part->phystype==PART_PHYS_BOIDS) {
516 for(p=0,pa=tpsys->particles; p<tpsys->totpart; p++,pa++) {
518 if(bpa->ground == ob)
522 if(tpsys->part->boids) {
523 for(state = tpsys->part->boids->states.first; state; state=state->next) {
524 for(rule = state->rules.first; rule; rule=rule->next) {
525 if(rule->type==eBoidRuleType_Avoid) {
526 BoidRuleGoalAvoid *gabr = (BoidRuleGoalAvoid*)rule;
530 else if(rule->type==eBoidRuleType_FollowLeader) {
531 BoidRuleFollowLeader *flbr = (BoidRuleFollowLeader*)rule;
540 obt->recalc |= OB_RECALC_DATA;
547 mat= bmain->mat.first;
550 for(a=0; a<MAX_MTEX; a++) {
551 if(mat->mtex[a] && ob==mat->mtex[a]->object) {
552 /* actually, test for lib here... to do */
553 mat->mtex[a]->object= NULL;
561 for(tex= bmain->tex.first; tex; tex= tex->id.next) {
562 if(tex->env && (ob==tex->env->object)) tex->env->object= NULL;
563 if(tex->pd && (ob==tex->pd->object)) tex->pd->object= NULL;
564 if(tex->vd && (ob==tex->vd->object)) tex->vd->object= NULL;
568 wrld= bmain->world.first;
570 if(wrld->id.lib==NULL) {
571 for(a=0; a<MAX_MTEX; a++) {
572 if(wrld->mtex[a] && ob==wrld->mtex[a]->object)
573 wrld->mtex[a]->object= NULL;
581 sce= bmain->scene.first;
583 if(sce->id.lib==NULL) {
584 if(sce->camera==ob) sce->camera= NULL;
585 if(sce->toolsettings->skgen_template==ob) sce->toolsettings->skgen_template = NULL;
586 if(sce->toolsettings->particle.object==ob) sce->toolsettings->particle.object= NULL;
588 #ifdef DURIAN_CAMERA_SWITCH
592 for (m= sce->markers.first; m; m= m->next) {
600 SEQ_BEGIN(sce->ed, seq)
601 if(seq->scene_camera==ob) {
602 seq->scene_camera= NULL;
611 #if 0 // XXX old animation system
613 ipo= bmain->ipo.first;
615 if(ipo->id.lib==NULL) {
617 for(icu= ipo->curve.first; icu; icu= icu->next) {
618 if(icu->driver && icu->driver->ob==ob)
619 icu->driver->ob= NULL;
624 #endif // XXX old animation system
627 sc= bmain->screen.first;
629 ScrArea *sa= sc->areabase.first;
633 for (sl= sa->spacedata.first; sl; sl= sl->next) {
634 if(sl->spacetype==SPACE_VIEW3D) {
635 View3D *v3d= (View3D*) sl;
638 if(v3d->camera==ob) {
642 if(v3d->localvd && v3d->localvd->camera==ob ) {
643 v3d->localvd->camera= NULL;
648 if (sa->spacetype == SPACE_VIEW3D) {
649 for (ar= sa->regionbase.first; ar; ar= ar->next) {
650 if (ar->regiontype==RGN_TYPE_WINDOW) {
651 rv3d= (RegionView3D *)ar->regiondata;
652 if (found == 1 || found == 3) {
653 if (rv3d->persp == RV3D_CAMOB)
654 rv3d->persp= RV3D_PERSP;
656 if (found == 2 || found == 3) {
657 if (rv3d->localvd && rv3d->localvd->persp == RV3D_CAMOB)
658 rv3d->localvd->persp= RV3D_PERSP;
665 else if(sl->spacetype==SPACE_OUTLINER) {
666 SpaceOops *so= (SpaceOops *)sl;
669 TreeStoreElem *tselem= so->treestore->data;
671 for(a=0; a<so->treestore->usedelem; a++, tselem++) {
672 if(tselem->id==(ID *)ob) tselem->id= NULL;
676 else if(sl->spacetype==SPACE_BUTS) {
677 SpaceButs *sbuts= (SpaceButs *)sl;
679 if(sbuts->pinid==(ID *)ob) {
680 sbuts->flag&= ~SB_PIN_CONTEXT;
692 group= bmain->group.first;
694 rem_from_group(group, ob, NULL, NULL);
695 group= group->id.next;
699 camera= bmain->camera.first;
701 if (camera->dof_ob==ob) {
702 camera->dof_ob = NULL;
704 camera= camera->id.next;
708 int exist_object(Object *obtest)
712 if(obtest==NULL) return 0;
714 ob= G.main->object.first;
716 if(ob==obtest) return 1;
722 /* *************************************************** */
724 static void *add_obdata_from_type(int type)
727 case OB_MESH: return add_mesh("Mesh");
728 case OB_CURVE: return add_curve("Curve", OB_CURVE);
729 case OB_SURF: return add_curve("Surf", OB_SURF);
730 case OB_FONT: return add_curve("Text", OB_FONT);
731 case OB_MBALL: return add_mball("Meta");
732 case OB_CAMERA: return add_camera("Camera");
733 case OB_LAMP: return add_lamp("Lamp");
734 case OB_LATTICE: return add_lattice("Lattice");
735 case OB_ARMATURE: return add_armature("Armature");
736 case OB_SPEAKER: return add_speaker("Speaker");
737 case OB_EMPTY: return NULL;
739 printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
744 static const char *get_obdata_defname(int type)
747 case OB_MESH: return "Mesh";
748 case OB_CURVE: return "Curve";
749 case OB_SURF: return "Surf";
750 case OB_FONT: return "Text";
751 case OB_MBALL: return "Mball";
752 case OB_CAMERA: return "Camera";
753 case OB_LAMP: return "Lamp";
754 case OB_LATTICE: return "Lattice";
755 case OB_ARMATURE: return "Armature";
756 case OB_SPEAKER: return "Speaker";
757 case OB_EMPTY: return "Empty";
759 printf("get_obdata_defname: Internal error, bad type: %d\n", type);
764 /* more general add: creates minimum required data, but without vertices etc. */
765 Object *add_only_object(int type, const char *name)
769 ob= alloc_libblock(&G.main->object, ID_OB, name);
771 /* default object vars */
774 ob->col[0]= ob->col[1]= ob->col[2]= 1.0;
777 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
779 /* objects should default to having Euler XYZ rotations,
780 * but rotations default to quaternions
782 ob->rotmode= ROT_MODE_EUL;
784 unit_axis_angle(ob->rotAxis, &ob->rotAngle);
785 unit_axis_angle(ob->drotAxis, &ob->drotAngle);
790 /* rotation locks should be 4D for 4 component rotations by default... */
791 ob->protectflag = OB_LOCK_ROT4D;
793 unit_m4(ob->constinv);
794 unit_m4(ob->parentinv);
797 ob->empty_drawtype= OB_PLAINAXES;
798 ob->empty_drawsize= 1.0;
800 if(type==OB_CAMERA || type==OB_LAMP || type==OB_SPEAKER) {
801 ob->trackflag= OB_NEGZ;
805 ob->trackflag= OB_POSY;
809 ob->dupon= 1; ob->dupoff= 0;
810 ob->dupsta= 1; ob->dupend= 100;
811 ob->dupfacesca = 1.0;
813 /* Game engine defaults*/
814 ob->mass= ob->inertia= 1.0f;
815 ob->formfactor= 0.4f;
818 ob->anisotropicFriction[0] = 1.0f;
819 ob->anisotropicFriction[1] = 1.0f;
820 ob->anisotropicFriction[2] = 1.0f;
821 ob->gameflag= OB_PROP|OB_COLLISION;
825 /* ob->pad3 == Contact Processing Threshold */
826 ob->m_contactProcessingThreshold = 1.;
827 ob->obstacleRad = 1.;
829 /* NT fluid sim defaults */
830 ob->fluidsimSettings = NULL;
832 ob->pc_ids.first = ob->pc_ids.last = NULL;
834 /* Animation Visualisation defaults */
835 animviz_settings_init(&ob->avs);
840 /* general add: to scene, with layer from area and default name */
841 /* creates minimum required data, but without vertices etc. */
842 Object *add_object(struct Scene *scene, int type)
848 BLI_strncpy(name, get_obdata_defname(type), sizeof(name));
849 ob = add_only_object(type, name);
851 ob->data= add_obdata_from_type(type);
855 base= scene_add_base(scene, ob);
856 scene_select_base(scene, base);
857 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
862 SoftBody *copy_softbody(SoftBody *sb)
866 if (sb==NULL) return(NULL);
868 sbn= MEM_dupallocN(sb);
869 sbn->totspring= sbn->totpoint= 0;
874 sbn->totkey= sbn->totpointkey= 0;
878 sbn->pointcache= BKE_ptcache_copy_list(&sbn->ptcaches, &sb->ptcaches);
880 if(sb->effector_weights)
881 sbn->effector_weights = MEM_dupallocN(sb->effector_weights);
886 BulletSoftBody *copy_bulletsoftbody(BulletSoftBody *bsb)
888 BulletSoftBody *bsbn;
892 bsbn = MEM_dupallocN(bsb);
893 /* no pointer in this structure yet */
897 static ParticleSystem *copy_particlesystem(ParticleSystem *psys)
899 ParticleSystem *psysn;
903 psysn= MEM_dupallocN(psys);
904 psysn->particles= MEM_dupallocN(psys->particles);
905 psysn->child= MEM_dupallocN(psys->child);
907 if(psys->part->type == PART_HAIR) {
908 for(p=0, pa=psysn->particles; p<psysn->totpart; p++, pa++)
909 pa->hair = MEM_dupallocN(pa->hair);
912 if(psysn->particles && (psysn->particles->keys || psysn->particles->boid)) {
913 ParticleKey *key = psysn->particles->keys;
914 BoidParticle *boid = psysn->particles->boid;
917 key = MEM_dupallocN(key);
920 boid = MEM_dupallocN(boid);
922 for(p=0, pa=psysn->particles; p<psysn->totpart; p++, pa++) {
933 psysn->clmd = (ClothModifierData *)modifier_new(eModifierType_Cloth);
934 modifier_copyData((ModifierData*)psys->clmd, (ModifierData*)psysn->clmd);
935 psys->hair_in_dm = psys->hair_out_dm = NULL;
938 BLI_duplicatelist(&psysn->targets, &psys->targets);
940 psysn->pathcache= NULL;
941 psysn->childcache= NULL;
945 psysn->effectors= NULL;
947 psysn->pathcachebufs.first = psysn->pathcachebufs.last = NULL;
948 psysn->childcachebufs.first = psysn->childcachebufs.last = NULL;
949 psysn->renderdata = NULL;
951 psysn->pointcache= BKE_ptcache_copy_list(&psysn->ptcaches, &psys->ptcaches);
953 /* XXX - from reading existing code this seems correct but intended usage of
954 * pointcache should /w cloth should be added in 'ParticleSystem' - campbell */
956 psysn->clmd->point_cache= psysn->pointcache;
959 id_us_plus((ID *)psysn->part);
964 void copy_object_particlesystems(Object *obn, Object *ob)
966 ParticleSystem *psys, *npsys;
969 obn->particlesystem.first= obn->particlesystem.last= NULL;
970 for(psys=ob->particlesystem.first; psys; psys=psys->next) {
971 npsys= copy_particlesystem(psys);
973 BLI_addtail(&obn->particlesystem, npsys);
975 /* need to update particle modifiers too */
976 for(md=obn->modifiers.first; md; md=md->next) {
977 if(md->type==eModifierType_ParticleSystem) {
978 ParticleSystemModifierData *psmd= (ParticleSystemModifierData*)md;
982 else if(md->type==eModifierType_DynamicPaint) {
983 DynamicPaintModifierData *pmd= (DynamicPaintModifierData*)md;
985 if(pmd->brush->psys==psys) {
986 pmd->brush->psys= npsys;
990 else if (md->type==eModifierType_Smoke) {
991 SmokeModifierData *smd = (SmokeModifierData*) md;
993 if(smd->type==MOD_SMOKE_TYPE_FLOW) {
995 if (smd->flow->psys == psys)
996 smd->flow->psys= npsys;
1004 void copy_object_softbody(Object *obn, Object *ob)
1007 obn->soft= copy_softbody(ob->soft);
1010 static void copy_object_pose(Object *obn, Object *ob)
1014 /* note: need to clear obn->pose pointer first, so that copy_pose works (otherwise there's a crash) */
1016 copy_pose(&obn->pose, ob->pose, 1); /* 1 = copy constraints */
1018 for (chan = obn->pose->chanbase.first; chan; chan=chan->next){
1021 chan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
1023 for (con= chan->constraints.first; con; con= con->next) {
1024 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1025 ListBase targets = {NULL, NULL};
1026 bConstraintTarget *ct;
1028 #if 0 // XXX old animation system
1029 /* note that we can't change lib linked ipo blocks. for making
1030 * proxies this still works correct however because the object
1031 * is changed to object->proxy_from when evaluating the driver. */
1032 if(con->ipo && !con->ipo->id.lib) {
1035 con->ipo= copy_ipo(con->ipo);
1037 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
1038 if(icu->driver && icu->driver->ob==ob)
1039 icu->driver->ob= obn;
1042 #endif // XXX old animation system
1044 if (cti && cti->get_constraint_targets) {
1045 cti->get_constraint_targets(con, &targets);
1047 for (ct= targets.first; ct; ct= ct->next) {
1052 if (cti->flush_constraint_targets)
1053 cti->flush_constraint_targets(con, &targets, 0);
1059 static int object_pose_context(Object *ob)
1062 (ob->type == OB_ARMATURE) &&
1064 (ob->mode & OB_MODE_POSE)
1073 //Object *object_pose_armature_get(Object *ob)
1074 Object *object_pose_armature_get(struct Object *ob)
1079 if(object_pose_context(ob))
1082 ob= modifiers_isDeformedByArmature(ob);
1084 if(object_pose_context(ob))
1090 static void copy_object_transform(Object *ob_tar, Object *ob_src)
1092 copy_v3_v3(ob_tar->loc, ob_src->loc);
1093 copy_v3_v3(ob_tar->rot, ob_src->rot);
1094 copy_v3_v3(ob_tar->quat, ob_src->quat);
1095 copy_v3_v3(ob_tar->rotAxis, ob_src->rotAxis);
1096 ob_tar->rotAngle= ob_src->rotAngle;
1097 ob_tar->rotmode= ob_src->rotmode;
1098 copy_v3_v3(ob_tar->size, ob_src->size);
1101 Object *copy_object(Object *ob)
1107 obn= copy_libblock(&ob->id);
1110 obn->mat= MEM_dupallocN(ob->mat);
1111 obn->matbits= MEM_dupallocN(ob->matbits);
1112 obn->totcol= ob->totcol;
1115 if(ob->bb) obn->bb= MEM_dupallocN(ob->bb);
1116 obn->flag &= ~OB_FROMGROUP;
1118 obn->modifiers.first = obn->modifiers.last= NULL;
1120 for (md=ob->modifiers.first; md; md=md->next) {
1121 ModifierData *nmd = modifier_new(md->type);
1122 BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
1123 modifier_copyData(md, nmd);
1124 BLI_addtail(&obn->modifiers, nmd);
1127 obn->prop.first = obn->prop.last = NULL;
1128 copy_properties(&obn->prop, &ob->prop);
1130 copy_sensors(&obn->sensors, &ob->sensors);
1131 copy_controllers(&obn->controllers, &ob->controllers);
1132 copy_actuators(&obn->actuators, &ob->actuators);
1135 copy_object_pose(obn, ob);
1136 /* backwards compat... non-armatures can get poses in older files? */
1137 if(ob->type==OB_ARMATURE)
1138 armature_rebuild_pose(obn, obn->data);
1140 defgroup_copy_list(&obn->defbase, &ob->defbase);
1141 copy_constraints(&obn->constraints, &ob->constraints, TRUE);
1146 /* increase user numbers */
1147 id_us_plus((ID *)obn->data);
1148 id_us_plus((ID *)obn->gpd);
1149 id_lib_extern((ID *)obn->dup_group);
1151 for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
1153 obn->disp.first= obn->disp.last= NULL;
1156 obn->pd= MEM_dupallocN(ob->pd);
1158 id_us_plus(&(obn->pd->tex->id));
1160 obn->pd->rng = MEM_dupallocN(ob->pd->rng);
1162 obn->soft= copy_softbody(ob->soft);
1163 obn->bsoft = copy_bulletsoftbody(ob->bsoft);
1165 copy_object_particlesystems(obn, ob);
1167 obn->derivedDeform = NULL;
1168 obn->derivedFinal = NULL;
1170 obn->gpulamp.first = obn->gpulamp.last = NULL;
1171 obn->pc_ids.first = obn->pc_ids.last = NULL;
1178 static void extern_local_object(Object *ob)
1180 //bActionStrip *strip;
1181 ParticleSystem *psys;
1183 #if 0 // XXX old animation system
1184 id_lib_extern((ID *)ob->action);
1185 id_lib_extern((ID *)ob->ipo);
1186 #endif // XXX old animation system
1187 id_lib_extern((ID *)ob->data);
1188 id_lib_extern((ID *)ob->dup_group);
1189 id_lib_extern((ID *)ob->poselib);
1190 id_lib_extern((ID *)ob->gpd);
1192 extern_local_matarar(ob->mat, ob->totcol);
1194 #if 0 // XXX old animation system
1195 for (strip=ob->nlastrips.first; strip; strip=strip->next) {
1196 id_lib_extern((ID *)strip->act);
1198 #endif // XXX old animation system
1199 for(psys=ob->particlesystem.first; psys; psys=psys->next)
1200 id_lib_extern((ID *)psys->part);
1203 void make_local_object(Object *ob)
1205 Main *bmain= G.main;
1208 int is_local= FALSE, is_lib= FALSE;
1210 /* - only lib users: do nothing
1211 * - only local users: set flag
1212 * - mixed: make copy
1215 if(ob->id.lib==NULL) return;
1217 ob->proxy= ob->proxy_from= NULL;
1220 id_clear_lib_data(bmain, &ob->id);
1221 extern_local_object(ob);
1224 for(sce= bmain->scene.first; sce && ELEM(0, is_lib, is_local); sce= sce->id.next) {
1225 if(object_in_scene(ob, sce)) {
1226 if(sce->id.lib) is_lib= TRUE;
1227 else is_local= TRUE;
1231 if(is_local && is_lib == FALSE) {
1232 id_clear_lib_data(bmain, &ob->id);
1233 extern_local_object(ob);
1235 else if(is_local && is_lib) {
1236 Object *obn= copy_object(ob);
1240 /* Remap paths of new ID using old library as base. */
1241 BKE_id_lib_local_paths(bmain, &obn->id);
1243 sce= bmain->scene.first;
1245 if(sce->id.lib==NULL) {
1246 base= sce->base.first;
1248 if(base->object==ob) {
1263 * Returns true if the Object is a from an external blend file (libdata)
1265 int object_is_libdata(Object *ob)
1268 if (ob->proxy) return 0;
1269 if (ob->id.lib) return 1;
1273 /* Returns true if the Object data is a from an external blend file (libdata) */
1274 int object_data_is_libdata(Object *ob)
1277 if(ob->proxy && (ob->data==NULL || ((ID *)ob->data)->lib==NULL)) return 0;
1278 if(ob->id.lib) return 1;
1279 if(ob->data==NULL) return 0;
1280 if(((ID *)ob->data)->lib) return 1;
1285 /* *************** PROXY **************** */
1287 /* when you make proxy, ensure the exposed layers are extern */
1288 static void armature_set_id_extern(Object *ob)
1290 bArmature *arm= ob->data;
1291 bPoseChannel *pchan;
1292 unsigned int lay= arm->layer_protected;
1294 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1295 if(!(pchan->bone->layer & lay))
1296 id_lib_extern((ID *)pchan->custom);
1301 void object_copy_proxy_drivers(Object *ob, Object *target)
1303 if ((target->adt) && (target->adt->drivers.first)) {
1306 /* add new animdata block */
1308 ob->adt= BKE_id_add_animdata(&ob->id);
1310 /* make a copy of all the drivers (for now), then correct any links that need fixing */
1311 free_fcurves(&ob->adt->drivers);
1312 copy_fcurves(&ob->adt->drivers, &target->adt->drivers);
1314 for (fcu= ob->adt->drivers.first; fcu; fcu= fcu->next) {
1315 ChannelDriver *driver= fcu->driver;
1318 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1320 DRIVER_TARGETS_LOOPER(dvar)
1323 if ((Object *)dtar->id == target)
1326 /* only on local objects because this causes indirect links a -> b -> c,blend to point directly to a.blend
1327 * when a.blend has a proxy thats linked into c.blend */
1328 if(ob->id.lib==NULL)
1329 id_lib_extern((ID *)dtar->id);
1333 DRIVER_TARGETS_LOOPER_END
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_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1358 * - gob means this proxy comes from a group, just apply the matrix
1359 * so the object wont move from its dupli-transform.
1361 * - no gob means this is being made from a linked object,
1362 * this is closer to making a copy of the object - in-place. */
1364 ob->rotmode= target->rotmode;
1365 mul_m4_m4m4(ob->obmat, target->obmat, gob->obmat);
1366 if(gob->dup_group) { /* should always be true */
1368 copy_v3_v3(tvec, gob->dup_group->dupli_ofs);
1369 mul_mat3_m4_v3(ob->obmat, tvec);
1370 sub_v3_v3(ob->obmat[3], tvec);
1372 object_apply_mat4(ob, ob->obmat, FALSE, TRUE);
1375 copy_object_transform(ob, target);
1376 ob->parent= target->parent; /* libdata */
1377 copy_m4_m4(ob->parentinv, target->parentinv);
1380 /* copy animdata stuff - drivers only for now... */
1381 object_copy_proxy_drivers(ob, target);
1383 /* skip constraints? */
1384 // FIXME: this is considered by many as a bug
1386 /* set object type and link to data */
1387 ob->type= target->type;
1388 ob->data= target->data;
1389 id_us_plus((ID *)ob->data); /* ensures lib data becomes LIB_EXTERN */
1391 /* copy material and index information */
1392 ob->actcol= ob->totcol= 0;
1393 if(ob->mat) MEM_freeN(ob->mat);
1394 if(ob->matbits) MEM_freeN(ob->matbits);
1397 if ((target->totcol) && (target->mat) && OB_TYPE_SUPPORT_MATERIAL(ob->type)) {
1399 ob->colbits = target->colbits;
1401 ob->actcol= target->actcol;
1402 ob->totcol= target->totcol;
1404 ob->mat = MEM_dupallocN(target->mat);
1405 ob->matbits = MEM_dupallocN(target->matbits);
1406 for(i=0; i<target->totcol; i++) {
1407 /* dont need to run test_object_materials since we know this object is new and not used elsewhere */
1408 id_us_plus((ID *)ob->mat[i]);
1412 /* type conversions */
1413 if(target->type == OB_ARMATURE) {
1414 copy_object_pose(ob, target); /* data copy, object pointers in constraints */
1415 rest_pose(ob->pose); /* clear all transforms in channels */
1416 armature_rebuild_pose(ob, ob->data); /* set all internal links */
1418 armature_set_id_extern(ob);
1420 else if (target->type == OB_EMPTY) {
1421 ob->empty_drawtype = target->empty_drawtype;
1422 ob->empty_drawsize = target->empty_drawsize;
1425 /* copy IDProperties */
1426 if(ob->id.properties) {
1427 IDP_FreeProperty(ob->id.properties);
1428 MEM_freeN(ob->id.properties);
1429 ob->id.properties= NULL;
1431 if(target->id.properties) {
1432 ob->id.properties= IDP_CopyProperty(target->id.properties);
1435 /* copy drawtype info */
1440 /* *************** CALC ****************** */
1442 void object_scale_to_mat3(Object *ob, float mat[][3])
1445 add_v3_v3v3(vec, ob->size, ob->dsize);
1446 size_to_mat3( mat,vec);
1449 void object_rot_to_mat3(Object *ob, float mat[][3])
1451 float rmat[3][3], dmat[3][3];
1453 /* 'dmat' is the delta-rotation matrix, which will get (pre)multiplied
1454 * with the rotation matrix to yield the appropriate rotation
1457 /* rotations may either be quats, eulers (with various rotation orders), or axis-angle */
1458 if (ob->rotmode > 0) {
1459 /* euler rotations (will cause gimble lock, but this can be alleviated a bit with rotation orders) */
1460 eulO_to_mat3(rmat, ob->rot, ob->rotmode);
1461 eulO_to_mat3(dmat, ob->drot, ob->rotmode);
1463 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
1464 /* axis-angle - not really that great for 3D-changing orientations */
1465 axis_angle_to_mat3(rmat, ob->rotAxis, ob->rotAngle);
1466 axis_angle_to_mat3(dmat, ob->drotAxis, ob->drotAngle);
1469 /* quats are normalised before use to eliminate scaling issues */
1472 normalize_qt_qt(tquat, ob->quat);
1473 quat_to_mat3(rmat, tquat);
1475 normalize_qt_qt(tquat, ob->dquat);
1476 quat_to_mat3(dmat, tquat);
1479 /* combine these rotations */
1480 mul_m3_m3m3(mat, dmat, rmat);
1483 void object_mat3_to_rot(Object *ob, float mat[][3], short use_compat)
1485 switch(ob->rotmode) {
1489 mat3_to_quat(ob->quat, mat);
1490 normalize_qt_qt(dquat, ob->dquat);
1492 mul_qt_qtqt(ob->quat, dquat, ob->quat);
1495 case ROT_MODE_AXISANGLE:
1496 mat3_to_axis_angle(ob->rotAxis, &ob->rotAngle, mat);
1497 sub_v3_v3(ob->rotAxis, ob->drotAxis);
1498 ob->rotAngle -= ob->drotAngle;
1500 default: /* euler */
1506 /* without drot we could apply 'mat' directly */
1507 mat3_to_quat(quat, mat);
1508 eulO_to_quat(dquat, ob->drot, ob->rotmode);
1510 mul_qt_qtqt(quat, dquat, quat);
1511 quat_to_mat3(tmat, quat);
1512 /* end drot correction */
1514 if(use_compat) mat3_to_compatible_eulO(ob->rot, ob->rot, ob->rotmode, tmat);
1515 else mat3_to_eulO(ob->rot, ob->rotmode, tmat);
1520 void object_tfm_protected_backup(const Object *ob,
1521 ObjectTfmProtectedChannels *obtfm)
1524 #define TFMCPY( _v) (obtfm->_v = ob->_v)
1525 #define TFMCPY3D( _v) copy_v3_v3(obtfm->_v, ob->_v)
1526 #define TFMCPY4D( _v) copy_v4_v4(obtfm->_v, ob->_v)
1547 void object_tfm_protected_restore(Object *ob,
1548 const ObjectTfmProtectedChannels *obtfm,
1549 const short protectflag)
1553 for (i= 0; i < 3; i++) {
1554 if (protectflag & (OB_LOCK_LOCX<<i)) {
1555 ob->loc[i]= obtfm->loc[i];
1556 ob->dloc[i]= obtfm->dloc[i];
1559 if (protectflag & (OB_LOCK_SCALEX<<i)) {
1560 ob->size[i]= obtfm->size[i];
1561 ob->dsize[i]= obtfm->dsize[i];
1564 if (protectflag & (OB_LOCK_ROTX<<i)) {
1565 ob->rot[i]= obtfm->rot[i];
1566 ob->drot[i]= obtfm->drot[i];
1568 ob->quat[i + 1]= obtfm->quat[i + 1];
1569 ob->dquat[i + 1]= obtfm->dquat[i + 1];
1571 ob->rotAxis[i]= obtfm->rotAxis[i];
1572 ob->drotAxis[i]= obtfm->drotAxis[i];
1576 if ((protectflag & OB_LOCK_ROT4D) && (protectflag & OB_LOCK_ROTW)) {
1577 ob->quat[0]= obtfm->quat[0];
1578 ob->dquat[0]= obtfm->dquat[0];
1580 ob->rotAngle= obtfm->rotAngle;
1581 ob->drotAngle= obtfm->drotAngle;
1585 /* see pchan_apply_mat4() for the equivalent 'pchan' function */
1586 void object_apply_mat4(Object *ob, float mat[][4], const short use_compat, const short use_parent)
1590 if(use_parent && ob->parent) {
1591 float rmat[4][4], diff_mat[4][4], imat[4][4];
1592 mul_m4_m4m4(diff_mat, ob->parentinv, ob->parent->obmat);
1593 invert_m4_m4(imat, diff_mat);
1594 mul_m4_m4m4(rmat, mat, imat); /* get the parent relative matrix */
1595 object_apply_mat4(ob, rmat, use_compat, FALSE);
1597 /* same as below, use rmat rather than mat */
1598 mat4_to_loc_rot_size(ob->loc, rot, ob->size, rmat);
1599 object_mat3_to_rot(ob, rot, use_compat);
1602 mat4_to_loc_rot_size(ob->loc, rot, ob->size, mat);
1603 object_mat3_to_rot(ob, rot, use_compat);
1606 sub_v3_v3(ob->loc, ob->dloc);
1607 sub_v3_v3(ob->size, ob->dsize);
1608 /* object_mat3_to_rot handles delta rotations */
1611 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
1618 object_scale_to_mat3(ob, smat);
1621 object_rot_to_mat3(ob, rmat);
1622 mul_m3_m3m3(mat, rmat, smat);
1625 void object_to_mat4(Object *ob, float mat[][4])
1629 object_to_mat3(ob, tmat);
1631 copy_m4_m3(mat, tmat);
1633 add_v3_v3v3(mat[3], ob->loc, ob->dloc);
1637 int enable_cu_speed= 1;
1639 static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
1642 float vec[4], dir[3], quat[4], radius, ctime;
1643 float timeoffs = 0.0, sf_orig = 0.0;
1648 if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1649 makeDispListCurveTypes(scene, par, 0);
1650 if(cu->path==NULL) return;
1652 /* catch exceptions: feature for nla stride editing */
1653 if(ob->ipoflag & OB_DISABLE_PATH) {
1656 /* catch exceptions: curve paths used as a duplicator */
1657 else if(enable_cu_speed) {
1658 /* ctime is now a proper var setting of Curve which gets set by Animato like any other var that's animated,
1659 * but this will only work if it actually is animated...
1661 * we divide the curvetime calculated in the previous step by the length of the path, to get a time
1662 * factor, which then gets clamped to lie within 0.0 - 1.0 range
1664 if (IS_EQF(cu->pathlen, 0.0f) == 0)
1665 ctime= cu->ctime / cu->pathlen;
1669 CLAMP(ctime, 0.0f, 1.0f);
1672 ctime= scene->r.cfra;
1673 if (IS_EQF(cu->pathlen, 0.0f) == 0)
1674 ctime /= cu->pathlen;
1676 CLAMP(ctime, 0.0f, 1.0f);
1679 /* time calculus is correct, now apply distance offset */
1680 if(cu->flag & CU_OFFS_PATHDIST) {
1681 ctime += timeoffs/cu->path->totdist;
1684 SWAP(float, sf_orig, ob->sf);
1689 if( where_on_path(par, ctime, vec, dir, cu->flag & CU_FOLLOW ? quat:NULL, &radius, NULL) ) {
1691 if(cu->flag & CU_FOLLOW) {
1694 vec_to_quat( quat,dir, ob->trackflag, ob->upflag);
1698 q[0]= (float)cos(0.5*vec[3]);
1699 x1= (float)sin(0.5*vec[3]);
1703 mul_qt_qtqt(quat, q, quat);
1705 quat_apply_track(quat, ob->trackflag, ob->upflag);
1708 quat_to_mat4(mat, quat);
1711 if(cu->flag & CU_PATH_RADIUS) {
1712 float tmat[4][4], rmat[4][4];
1713 scale_m4_fl(tmat, radius);
1714 mul_m4_m4m4(rmat, mat, tmat);
1715 copy_m4_m4(mat, rmat);
1718 copy_v3_v3(mat[3], vec);
1723 static void ob_parbone(Object *ob, Object *par, float mat[][4])
1725 bPoseChannel *pchan;
1728 if (par->type!=OB_ARMATURE) {
1733 /* Make sure the bone is still valid */
1734 pchan= get_pose_channel(par->pose, ob->parsubstr);
1736 printf ("Object %s with Bone parent: bone %s doesn't exist\n", ob->id.name+2, ob->parsubstr);
1741 /* get bone transform */
1742 copy_m4_m4(mat, pchan->pose_mat);
1744 /* but for backwards compatibility, the child has to move to the tail */
1745 copy_v3_v3(vec, mat[1]);
1746 mul_v3_fl(vec, pchan->bone->length);
1747 add_v3_v3(mat[3], vec);
1750 static void give_parvert(Object *par, int nr, float *vec)
1755 vec[0]=vec[1]=vec[2]= 0.0f;
1757 if(par->type==OB_MESH) {
1758 Mesh *me= par->data;
1761 em = me->edit_btmesh;
1767 BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
1768 int *keyindex = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
1770 if(keyindex && *keyindex==nr) {
1771 memcpy(vec, eve->co, sizeof(float)*3);
1777 dm = (em)? em->derivedFinal: par->derivedFinal;
1780 MVert *mvert= dm->getVertArray(dm);
1781 int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
1782 int i, vindex, numVerts = dm->getNumVerts(dm);
1784 /* get the average of all verts with (original index == nr) */
1786 for(i = 0; i < numVerts; i++) {
1787 vindex= (index)? index[i]: i;
1790 add_v3_v3(vec, mvert[i].co);
1797 } else if(count > 0) {
1798 mul_v3_fl(vec, 1.0f / count);
1800 /* use first index if its out of range */
1801 dm->getVertCo(dm, 0, vec);
1805 else if (ELEM(par->type, OB_CURVE, OB_SURF)) {
1814 nurbs= BKE_curve_nurbs(cu);
1818 while(nu && !found) {
1819 if(nu->type == CU_BEZIER) {
1825 copy_v3_v3(vec, bezt->vec[1]);
1834 a= nu->pntsu*nu->pntsv;
1838 memcpy(vec, bp->vec, sizeof(float)*3);
1849 else if(par->type==OB_LATTICE) {
1850 Lattice *latt= par->data;
1852 DispList *dl = find_displist(&par->disp, DL_VERTS);
1853 float *co = dl?dl->verts:NULL;
1855 if(latt->editlatt) latt= latt->editlatt->latt;
1857 a= latt->pntsu*latt->pntsv*latt->pntsw;
1863 memcpy(vec, co, 3*sizeof(float));
1865 memcpy(vec, bp->vec, 3*sizeof(float));
1875 static void ob_parvert3(Object *ob, Object *par, float mat[][4])
1877 float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1879 /* in local ob space */
1882 if (ELEM4(par->type, OB_MESH, OB_SURF, OB_CURVE, OB_LATTICE)) {
1884 give_parvert(par, ob->par1, v1);
1885 give_parvert(par, ob->par2, v2);
1886 give_parvert(par, ob->par3, v3);
1888 tri_to_quat( q,v1, v2, v3);
1889 quat_to_mat3( cmat,q);
1890 copy_m4_m3(mat, cmat);
1892 if(ob->type==OB_CURVE) {
1893 copy_v3_v3(mat[3], v1);
1896 add_v3_v3v3(mat[3], v1, v2);
1897 add_v3_v3(mat[3], v3);
1898 mul_v3_fl(mat[3], 0.3333333f);
1903 static int where_is_object_parslow(Object *ob, float obmat[4][4], float slowmat[4][4])
1909 // include framerate
1910 fac1= ( 1.0f / (1.0f + fabsf(ob->sf)) );
1911 if(fac1 >= 1.0f) return 0;
1916 for(a=0; a<16; a++, fp1++, fp2++) {
1917 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1923 void where_is_object_time(Scene *scene, Object *ob, float ctime)
1925 float slowmat[4][4] = MAT4_UNITY;
1928 /* new version: correct parent+vertexparent and track+parent */
1929 /* this one only calculates direct attached parent and track */
1930 /* is faster, but should keep track of timeoffs */
1932 if(ob==NULL) return;
1934 /* execute drivers only, as animation has already been done */
1935 BKE_animsys_evaluate_animdata(scene, &ob->id, ob->adt, ctime, ADT_RECALC_DRIVERS);
1938 Object *par= ob->parent;
1940 /* hurms, code below conflicts with depgraph... (ton) */
1941 /* and even worse, it gives bad effects for NLA stride too (try ctime != par->ctime, with MBlur) */
1942 if(stime != par->ctime) {
1943 // only for ipo systems?
1946 if(par->proxy_from); // was a copied matrix, no where_is! bad...
1947 else where_is_object_time(scene, par, ctime);
1949 solve_parenting(scene, ob, par, ob->obmat, slowmat, 0);
1954 solve_parenting(scene, ob, par, ob->obmat, slowmat, 0);
1956 /* "slow parent" is definitely not threadsafe, and may also give bad results jumping around
1957 * An old-fashioned hack which probably doesn't really cut it anymore
1959 if(ob->partype & PARSLOW) {
1960 if(!where_is_object_parslow(ob, ob->obmat, slowmat))
1965 object_to_mat4(ob, ob->obmat);
1968 /* solve constraints */
1969 if (ob->constraints.first && !(ob->transflag & OB_NO_CONSTRAINTS)) {
1972 cob= constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
1974 /* constraints need ctime, not stime. Some call where_is_object_time and bsystem_time */
1975 solve_constraints (&ob->constraints, cob, ctime);
1977 constraints_clear_evalob(cob);
1980 /* set negative scale flag in object */
1981 if(is_negative_m4(ob->obmat)) ob->transflag |= OB_NEG_SCALE;
1982 else ob->transflag &= ~OB_NEG_SCALE;
1985 /* get object transformation matrix without recalculating dependencies and
1986 constraints -- assume dependencies are already solved by depsgraph.
1987 no changes to object and it's parent would be done.
1988 used for bundles orientation in 3d space relative to parented blender camera */
1989 void where_is_object_mat(Scene *scene, Object *ob, float obmat[4][4])
1991 float slowmat[4][4] = MAT4_UNITY;
1994 Object *par= ob->parent;
1996 solve_parenting(scene, ob, par, obmat, slowmat, 1);
1998 if(ob->partype & PARSLOW)
1999 where_is_object_parslow(ob, obmat, slowmat);
2002 object_to_mat4(ob, obmat);
2006 static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul)
2014 object_to_mat4(ob, locmat);
2016 if(ob->partype & PARSLOW) copy_m4_m4(slowmat, obmat);
2018 switch(ob->partype & PARTYPE) {
2021 if(par->type==OB_CURVE) {
2022 if( ((Curve *)par->data)->flag & CU_PATH ) {
2023 ob_parcurve(scene, ob, par, tmat);
2028 if(ok) mul_serie_m4(totmat, par->obmat, tmat,
2029 NULL, NULL, NULL, NULL, NULL, NULL);
2030 else copy_m4_m4(totmat, par->obmat);
2034 ob_parbone(ob, par, tmat);
2035 mul_serie_m4(totmat, par->obmat, tmat,
2036 NULL, NULL, NULL, NULL, NULL, NULL);
2042 copy_v3_v3(totmat[3], par->obmat[3]);
2045 give_parvert(par, ob->par1, vec);
2046 mul_v3_m4v3(totmat[3], par->obmat, vec);
2050 ob_parvert3(ob, par, tmat);
2052 mul_serie_m4(totmat, par->obmat, tmat,
2053 NULL, NULL, NULL, NULL, NULL, NULL);
2057 copy_m4_m4(totmat, par->obmat);
2062 mul_serie_m4(tmat, totmat, ob->parentinv,
2063 NULL, NULL, NULL, NULL, NULL, NULL);
2064 mul_serie_m4(obmat, tmat, locmat,
2065 NULL, NULL, NULL, NULL, NULL, NULL);
2071 // external usable originmat
2072 copy_m3_m4(originmat, tmat);
2074 // origin, voor help line
2075 if( (ob->partype & PARTYPE)==PARSKEL ) {
2076 copy_v3_v3(ob->orig, par->obmat[3]);
2079 copy_v3_v3(ob->orig, totmat[3]);
2085 void where_is_object(struct Scene *scene, Object *ob)
2087 where_is_object_time(scene, ob, (float)scene->r.cfra);
2091 void where_is_object_simul(Scene *scene, Object *ob)
2092 /* was written for the old game engine (until 2.04) */
2093 /* It seems that this function is only called
2094 for a lamp that is the child of another object */
2098 float slowmat[4][4];
2106 solve_parenting(scene, ob, par, ob->obmat, slowmat, 1);
2108 if(ob->partype & PARSLOW) {
2109 fac1= (float)(1.0/(1.0+ fabs(ob->sf)));
2113 for(a=0; a<16; a++, fp1++, fp2++) {
2114 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
2119 object_to_mat4(ob, ob->obmat);
2122 /* solve constraints */
2123 if (ob->constraints.first) {
2126 cob= constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2127 solve_constraints(&ob->constraints, cob, (float)scene->r.cfra);
2128 constraints_clear_evalob(cob);
2132 /* for calculation of the inverse parent transform, only used for editor */
2133 void what_does_parent(Scene *scene, Object *ob, Object *workob)
2135 clear_workob(workob);
2137 unit_m4(workob->obmat);
2138 unit_m4(workob->parentinv);
2139 unit_m4(workob->constinv);
2140 workob->parent= ob->parent;
2142 workob->trackflag= ob->trackflag;
2143 workob->upflag= ob->upflag;
2145 workob->partype= ob->partype;
2146 workob->par1= ob->par1;
2147 workob->par2= ob->par2;
2148 workob->par3= ob->par3;
2150 workob->constraints.first = ob->constraints.first;
2151 workob->constraints.last = ob->constraints.last;
2153 BLI_strncpy(workob->parsubstr, ob->parsubstr, sizeof(workob->parsubstr));
2155 where_is_object(scene, workob);
2158 BoundBox *unit_boundbox(void)
2161 float min[3] = {-1.0f,-1.0f,-1.0f}, max[3] = {-1.0f,-1.0f,-1.0f};
2163 bb= MEM_callocN(sizeof(BoundBox), "OB-BoundBox");
2164 boundbox_set_from_min_max(bb, min, max);
2169 void boundbox_set_from_min_max(BoundBox *bb, float min[3], float max[3])
2171 bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
2172 bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
2174 bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
2175 bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
2177 bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
2178 bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
2181 BoundBox *object_get_boundbox(Object *ob)
2185 if(ob->type==OB_MESH) {
2186 bb = mesh_get_bb(ob);
2188 else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2189 bb= ob->bb ? ob->bb : ( (Curve *)ob->data )->bb;
2191 else if(ob->type==OB_MBALL) {
2197 /* used to temporally disable/enable boundbox */
2198 void object_boundbox_flag(Object *ob, int flag, int set)
2200 BoundBox *bb= object_get_boundbox(ob);
2202 if(set) bb->flag |= flag;
2203 else bb->flag &= ~flag;
2207 void object_get_dimensions(Object *ob, float *value)
2209 BoundBox *bb = NULL;
2211 bb= object_get_boundbox(ob);
2215 mat4_to_size( scale,ob->obmat);
2217 value[0] = fabsf(scale[0]) * (bb->vec[4][0] - bb->vec[0][0]);
2218 value[1] = fabsf(scale[1]) * (bb->vec[2][1] - bb->vec[0][1]);
2219 value[2] = fabsf(scale[2]) * (bb->vec[1][2] - bb->vec[0][2]);
2221 value[0] = value[1] = value[2] = 0.f;
2225 void object_set_dimensions(Object *ob, const float *value)
2227 BoundBox *bb = NULL;
2229 bb= object_get_boundbox(ob);
2231 float scale[3], len[3];
2233 mat4_to_size( scale,ob->obmat);
2235 len[0] = bb->vec[4][0] - bb->vec[0][0];
2236 len[1] = bb->vec[2][1] - bb->vec[0][1];
2237 len[2] = bb->vec[1][2] - bb->vec[0][2];
2239 if (len[0] > 0.f) ob->size[0] = value[0] / len[0];
2240 if (len[1] > 0.f) ob->size[1] = value[1] / len[1];
2241 if (len[2] > 0.f) ob->size[2] = value[2] / len[2];
2245 void minmax_object(Object *ob, float min[3], float max[3])
2250 short change= FALSE;
2257 Curve *cu= ob->data;
2259 if(cu->bb==NULL) tex_space_curve(cu);
2262 for(a=0; a<8; a++) {
2263 mul_m4_v3(ob->obmat, bb.vec[a]);
2264 DO_MINMAX(bb.vec[a], min, max);
2271 Lattice *lt= ob->data;
2272 BPoint *bp= lt->def;
2275 for(w=0; w<lt->pntsw; w++) {
2276 for(v=0; v<lt->pntsv; v++) {
2277 for(u=0; u<lt->pntsu; u++, bp++) {
2278 mul_v3_m4v3(vec, ob->obmat, bp->vec);
2279 DO_MINMAX(vec, min, max);
2288 bPoseChannel *pchan;
2289 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2290 mul_v3_m4v3(vec, ob->obmat, pchan->pose_head);
2291 DO_MINMAX(vec, min, max);
2292 mul_v3_m4v3(vec, ob->obmat, pchan->pose_tail);
2293 DO_MINMAX(vec, min, max);
2300 Mesh *me= get_mesh(ob);
2303 bb = *mesh_get_bb(ob);
2305 for(a=0; a<8; a++) {
2306 mul_m4_v3(ob->obmat, bb.vec[a]);
2307 DO_MINMAX(bb.vec[a], min, max);
2315 if(change == FALSE) {
2316 DO_MINMAX(ob->obmat[3], min, max);
2318 copy_v3_v3(vec, ob->obmat[3]);
2319 add_v3_v3(vec, ob->size);
2320 DO_MINMAX(vec, min, max);
2322 copy_v3_v3(vec, ob->obmat[3]);
2323 sub_v3_v3(vec, ob->size);
2324 DO_MINMAX(vec, min, max);
2328 int minmax_object_duplis(Scene *scene, Object *ob, float *min, float *max)
2331 if ((ob->transflag & OB_DUPLI)==0) {
2337 lb= object_duplilist(scene, ob);
2338 for(dob= lb->first; dob; dob= dob->next) {
2339 if(dob->no_draw == 0) {
2340 BoundBox *bb= object_get_boundbox(dob->ob);
2344 for(i=0; i<8; i++) {
2346 mul_v3_m4v3(vec, dob->mat, bb->vec[i]);
2347 DO_MINMAX(vec, min, max);
2354 free_object_duplilist(lb); /* does restore */
2360 void BKE_object_foreach_display_point(
2361 Object *ob, float obmat[4][4],
2362 void (*func_cb)(const float[3], void *), void *user_data)
2366 if (ob->derivedFinal) {
2367 DerivedMesh *dm= ob->derivedFinal;
2368 MVert *mv= dm->getVertArray(dm);
2369 int totvert= dm->getNumVerts(dm);
2372 for (i= 0; i < totvert; i++, mv++) {
2373 mul_v3_m4v3(co, obmat, mv->co);
2374 func_cb(co, user_data);
2377 else if (ob->disp.first) {
2380 for (dl=ob->disp.first; dl; dl=dl->next) {
2381 float *v3= dl->verts;
2382 int totvert= dl->nr;
2385 for (i= 0; i < totvert; i++, v3+=3) {
2386 mul_v3_m4v3(co, obmat, v3);
2387 func_cb(co, user_data);
2393 void BKE_scene_foreach_display_point(
2394 Scene *scene, View3D *v3d, const short flag,
2395 void (*func_cb)(const float[3], void *), void *user_data)
2400 for(base= FIRSTBASE; base; base = base->next) {
2401 if(BASE_VISIBLE(v3d, base) && (base->flag & flag) == flag) {
2404 if ((ob->transflag & OB_DUPLI)==0) {
2405 BKE_object_foreach_display_point(ob, ob->obmat, func_cb, user_data);
2411 lb= object_duplilist(scene, ob);
2412 for(dob= lb->first; dob; dob= dob->next) {
2413 if(dob->no_draw == 0) {
2414 BKE_object_foreach_display_point(dob->ob, dob->mat, func_cb, user_data);
2417 free_object_duplilist(lb); /* does restore */
2423 /* copied from DNA_object_types.h */
2424 typedef struct ObTfmBack {
2425 float loc[3], dloc[3], orig[3];
2426 float size[3], dsize[3]; /* scale and delta scale */
2427 float rot[3], drot[3]; /* euler rotation */
2428 float quat[4], dquat[4]; /* quaternion rotation */
2429 float rotAxis[3], drotAxis[3]; /* axis angle rotation - axis part */
2430 float rotAngle, drotAngle; /* axis angle rotation - angle part */
2431 float obmat[4][4]; /* final worldspace matrix with constraints & animsys applied */
2432 float parentinv[4][4]; /* inverse result of parent, so that object doesn't 'stick' to parent */
2433 float constinv[4][4]; /* inverse result of constraints. doesn't include effect of parent or object local transform */
2434 float imat[4][4]; /* inverse matrix of 'obmat' for during render, old game engine, temporally: ipokeys of transform */
2437 void *object_tfm_backup(Object *ob)
2439 ObTfmBack *obtfm= MEM_mallocN(sizeof(ObTfmBack), "ObTfmBack");
2440 copy_v3_v3(obtfm->loc, ob->loc);
2441 copy_v3_v3(obtfm->dloc, ob->dloc);
2442 copy_v3_v3(obtfm->orig, ob->orig);
2443 copy_v3_v3(obtfm->size, ob->size);
2444 copy_v3_v3(obtfm->dsize, ob->dsize);
2445 copy_v3_v3(obtfm->rot, ob->rot);
2446 copy_v3_v3(obtfm->drot, ob->drot);
2447 copy_qt_qt(obtfm->quat, ob->quat);
2448 copy_qt_qt(obtfm->dquat, ob->dquat);
2449 copy_v3_v3(obtfm->rotAxis, ob->rotAxis);
2450 copy_v3_v3(obtfm->drotAxis, ob->drotAxis);
2451 obtfm->rotAngle= ob->rotAngle;
2452 obtfm->drotAngle= ob->drotAngle;
2453 copy_m4_m4(obtfm->obmat, ob->obmat);
2454 copy_m4_m4(obtfm->parentinv, ob->parentinv);
2455 copy_m4_m4(obtfm->constinv, ob->constinv);
2456 copy_m4_m4(obtfm->imat, ob->imat);
2458 return (void *)obtfm;
2461 void object_tfm_restore(Object *ob, void *obtfm_pt)
2463 ObTfmBack *obtfm= (ObTfmBack *)obtfm_pt;
2464 copy_v3_v3(ob->loc, obtfm->loc);
2465 copy_v3_v3(ob->dloc, obtfm->dloc);
2466 copy_v3_v3(ob->orig, obtfm->orig);
2467 copy_v3_v3(ob->size, obtfm->size);
2468 copy_v3_v3(ob->dsize, obtfm->dsize);
2469 copy_v3_v3(ob->rot, obtfm->rot);
2470 copy_v3_v3(ob->drot, obtfm->drot);
2471 copy_qt_qt(ob->quat, obtfm->quat);
2472 copy_qt_qt(ob->dquat, obtfm->dquat);
2473 copy_v3_v3(ob->rotAxis, obtfm->rotAxis);
2474 copy_v3_v3(ob->drotAxis, obtfm->drotAxis);
2475 ob->rotAngle= obtfm->rotAngle;
2476 ob->drotAngle= obtfm->drotAngle;
2477 copy_m4_m4(ob->obmat, obtfm->obmat);
2478 copy_m4_m4(ob->parentinv, obtfm->parentinv);
2479 copy_m4_m4(ob->constinv, obtfm->constinv);
2480 copy_m4_m4(ob->imat, obtfm->imat);
2483 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
2484 /* local_object->proxy == pointer to library object, saved in files and read */
2486 /* function below is polluted with proxy exceptions, cleanup will follow! */
2488 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
2489 /* requires flags to be set! */
2490 void object_handle_update(Scene *scene, Object *ob)
2492 if(ob->recalc & OB_RECALC_ALL) {
2493 /* speed optimization for animation lookups */
2495 make_pose_channels_hash(ob->pose);
2497 if(ob->recalc & OB_RECALC_DATA) {
2498 if(ob->type==OB_ARMATURE) {
2499 /* this happens for reading old files and to match library armatures
2500 with poses we do it ahead of where_is_object to ensure animation
2501 is evaluated on the rebuilt pose, otherwise we get incorrect poses
2503 if(ob->pose==NULL || (ob->pose->flag & POSE_RECALC))
2504 armature_rebuild_pose(ob, ob->data);
2508 /* XXX new animsys warning: depsgraph tag OB_RECALC_DATA should not skip drivers,
2509 which is only in where_is_object now */
2510 // XXX: should this case be OB_RECALC_OB instead?
2511 if(ob->recalc & OB_RECALC_ALL) {
2514 printf("recalcob %s\n", ob->id.name+2);
2516 /* handle proxy copy for target */
2517 if(ob->id.lib && ob->proxy_from) {
2518 // printf("ob proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2519 if(ob->proxy_from->proxy_group) {/* transform proxy into group space */
2520 Object *obg= ob->proxy_from->proxy_group;
2521 invert_m4_m4(obg->imat, obg->obmat);
2522 mul_m4_m4m4(ob->obmat, ob->proxy_from->obmat, obg->imat);
2523 if(obg->dup_group) { /* should always be true */
2524 add_v3_v3(ob->obmat[3], obg->dup_group->dupli_ofs);
2528 copy_m4_m4(ob->obmat, ob->proxy_from->obmat);
2531 where_is_object(scene, ob);
2534 if(ob->recalc & OB_RECALC_DATA) {
2535 ID *data_id= (ID *)ob->data;
2536 AnimData *adt= BKE_animdata_from_id(data_id);
2537 float ctime= (float)scene->r.cfra; // XXX this is bad...
2542 printf("recalcdata %s\n", ob->id.name+2);
2545 /* evaluate drivers */
2546 // XXX: for mesh types, should we push this to derivedmesh instead?
2547 BKE_animsys_evaluate_animdata(scene, data_id, adt, ctime, ADT_RECALC_DRIVERS);
2550 /* includes all keys and modifiers */
2554 #if 0 // XXX, comment for 2.56a release, background wont set 'scene->customdata_mask'
2555 BMEditMesh *em = (ob == scene->obedit)? ((Mesh*)ob->data)->edit_btmesh : NULL;
2556 BLI_assert((scene->customdata_mask & CD_MASK_BAREMESH) == CD_MASK_BAREMESH);
2558 makeDerivedMesh(scene, ob, em, scene->customdata_mask, 0); /* was CD_MASK_BAREMESH */
2560 makeDerivedMesh(scene, ob, NULL, scene->customdata_mask, 0);
2562 #else /* ensure CD_MASK_BAREMESH for now */
2563 BMEditMesh *em = (ob == scene->obedit)? ((Mesh*)ob->data)->edit_btmesh : NULL;
2565 makeDerivedMesh(scene, ob, em, scene->customdata_mask | CD_MASK_BAREMESH, 0); /* was CD_MASK_BAREMESH */
2567 makeDerivedMesh(scene, ob, NULL, scene->customdata_mask | CD_MASK_BAREMESH, 0);
2574 if(ob->id.lib && ob->proxy_from) {
2575 // printf("pose proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2576 copy_pose_result(ob->pose, ob->proxy_from->pose);
2579 where_is_pose(scene, ob);
2584 makeDispListMBall(scene, ob);
2590 makeDispListCurveTypes(scene, ob, 0);
2594 lattice_calc_modifiers(scene, ob);
2599 if(ob->particlesystem.first) {
2600 ParticleSystem *tpsys, *psys;
2602 ob->transflag &= ~OB_DUPLIPARTS;
2604 psys= ob->particlesystem.first;
2606 if(psys_check_enabled(ob, psys)) {
2607 /* check use of dupli objects here */
2608 if(psys->part && (psys->part->draw_as == PART_DRAW_REND || G.rendering) &&
2609 ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob)
2610 || (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
2611 ob->transflag |= OB_DUPLIPARTS;
2613 particle_system_update(scene, ob, psys);
2616 else if(psys->flag & PSYS_DELETE) {
2618 BLI_remlink(&ob->particlesystem, psys);
2626 if(G.rendering && ob->transflag & OB_DUPLIPARTS) {
2627 /* this is to make sure we get render level duplis in groups:
2628 * the derivedmesh must be created before init_render_mesh,
2629 * since object_duplilist does dupliparticles before that */
2630 dm = mesh_create_derived_render(scene, ob, CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL);
2633 for(psys=ob->particlesystem.first; psys; psys=psys->next)
2634 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
2638 /* check if quick cache is needed */
2639 BKE_ptcache_ids_from_object(&pidlist, ob, scene, MAX_DUPLI_RECUR);
2641 for(pid=pidlist.first; pid; pid=pid->next) {
2642 if((pid->cache->flag & PTCACHE_BAKED)
2643 || (pid->cache->flag & PTCACHE_QUICK_CACHE)==0)
2646 if(pid->cache->flag & PTCACHE_OUTDATED || (pid->cache->flag & PTCACHE_SIMULATION_VALID)==0) {
2647 scene->physics_settings.quick_cache_step =
2648 scene->physics_settings.quick_cache_step ?
2649 MIN2(scene->physics_settings.quick_cache_step, pid->cache->step) :
2654 BLI_freelistN(&pidlist);
2657 /* the no-group proxy case, we call update */
2658 if(ob->proxy && ob->proxy_group==NULL) {
2659 /* set pointer in library proxy target, for copying, but restore it */
2660 ob->proxy->proxy_from= ob;
2661 // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
2662 object_handle_update(scene, ob->proxy);
2665 ob->recalc &= ~OB_RECALC_ALL;
2668 /* the case when this is a group proxy, object_update is called in group.c */
2670 ob->proxy->proxy_from= ob;
2671 // printf("set proxy pointer for later group stuff %s\n", ob->id.name);
2675 void object_sculpt_modifiers_changed(Object *ob)
2677 SculptSession *ss= ob->sculpt;
2680 /* we free pbvh on changes, except during sculpt since it can't deal with
2681 changing PVBH node organization, we hope topology does not change in
2682 the meantime .. weak */
2684 BLI_pbvh_free(ss->pbvh);
2688 free_sculptsession_deformMats(ob->sculpt);
2693 BLI_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
2695 for(n = 0; n < totnode; n++)
2696 BLI_pbvh_node_mark_update(nodes[n]);
2702 int give_obdata_texspace(Object *ob, short **texflag, float **loc, float **size, float **rot)
2708 switch (GS(((ID *)ob->data)->name)) {
2712 if (texflag) *texflag = &me->texflag;
2713 if (loc) *loc = me->loc;
2714 if (size) *size = me->size;
2715 if (rot) *rot = me->rot;
2720 Curve *cu= ob->data;
2721 if (texflag) *texflag = &cu->texflag;
2722 if (loc) *loc = cu->loc;
2723 if (size) *size = cu->size;
2724 if (rot) *rot = cu->rot;
2729 MetaBall *mb= ob->data;
2730 if (texflag) *texflag = &mb->texflag;
2731 if (loc) *loc = mb->loc;
2732 if (size) *size = mb->size;
2733 if (rot) *rot = mb->rot;
2743 * Test a bounding box for ray intersection
2744 * assumes the ray is already local to the boundbox space
2746 int ray_hit_boundbox(struct BoundBox *bb, float ray_start[3], float ray_normal[3])
2748 static int triangle_indexes[12][3] = {{0, 1, 2}, {0, 2, 3},
2749 {3, 2, 6}, {3, 6, 7},
2750 {1, 2, 6}, {1, 6, 5},
2751 {5, 6, 7}, {4, 5, 7},
2752 {0, 3, 7}, {0, 4, 7},
2753 {0, 1, 5}, {0, 4, 5}};
2757 for (i = 0; i < 12 && result == 0; i++)
2761 v1 = triangle_indexes[i][0];
2762 v2 = triangle_indexes[i][1];
2763 v3 = triangle_indexes[i][2];
2764 result = isect_ray_tri_v3(ray_start, ray_normal, bb->vec[v1], bb->vec[v2], bb->vec[v3], &lambda, NULL);
2770 static int pc_cmp(void *a, void *b)
2772 LinkData *ad = a, *bd = b;
2773 if(GET_INT_FROM_POINTER(ad->data) > GET_INT_FROM_POINTER(bd->data))
2778 int object_insert_ptcache(Object *ob)
2780 LinkData *link = NULL;
2783 BLI_sortlist(&ob->pc_ids, pc_cmp);
2785 for(link=ob->pc_ids.first, i = 0; link; link=link->next, i++)
2787 int index = GET_INT_FROM_POINTER(link->data);
2793 link = MEM_callocN(sizeof(LinkData), "PCLink");
2794 link->data = SET_INT_IN_POINTER(i);
2795 BLI_addtail(&ob->pc_ids, link);
2801 static int pc_findindex(ListBase *listbase, int index)
2803 LinkData *link= NULL;
2806 if (listbase == NULL) return -1;
2808 link= listbase->first;
2810 if ((int)link->data == index)
2820 void object_delete_ptcache(Object *ob, int index)
2822 int list_index = pc_findindex(&ob->pc_ids, index);
2823 LinkData *link = BLI_findlink(&ob->pc_ids, list_index);
2824 BLI_freelinkN(&ob->pc_ids, link);
2828 /* shape key utility function */
2830 /************************* Mesh ************************/
2831 static KeyBlock *insert_meshkey(Scene *scene, Object *ob, const char *name, int from_mix)
2839 key= me->key= add_key((ID *)me);
2840 key->type= KEY_RELATIVE;
2844 if(newkey || from_mix==FALSE) {
2845 /* create from mesh */
2846 kb= add_keyblock(key, name);
2847 mesh_to_key(me, kb);
2850 /* copy from current values */
2851 float *data= do_ob_key(scene, ob);
2853 /* create new block with prepared data */
2854 kb= add_keyblock(key, name);
2856 kb->totelem= me->totvert;
2861 /************************* Lattice ************************/
2862 static KeyBlock *insert_lattkey(Scene *scene, Object *ob, const char *name, int from_mix)
2864 Lattice *lt= ob->data;
2870 key= lt->key= add_key( (ID *)lt);
2871 key->type= KEY_RELATIVE;
2875 if(newkey || from_mix==FALSE) {
2876 kb= add_keyblock(key, name);
2878 KeyBlock *basekb= (KeyBlock *)key->block.first;
2879 kb->data= MEM_dupallocN(basekb->data);
2880 kb->totelem= basekb->totelem;
2883 latt_to_key(lt, kb);
2887 /* copy from current values */
2888 float *data= do_ob_key(scene, ob);
2890 /* create new block with prepared data */
2891 kb= add_keyblock(key, name);
2892 kb->totelem= lt->pntsu*lt->pntsv*lt->pntsw;
2898 /************************* Curve ************************/
2899 static KeyBlock *insert_curvekey(Scene *scene, Object *ob, const char *name, int from_mix)
2901 Curve *cu= ob->data;
2904 ListBase *lb= BKE_curve_nurbs(cu);
2908 key= cu->key= add_key( (ID *)cu);
2909 key->type = KEY_RELATIVE;
2913 if(newkey || from_mix==FALSE) {
2914 /* create from curve */
2915 kb= add_keyblock(key, name);
2917 KeyBlock *basekb= (KeyBlock *)key->block.first;
2918 kb->data= MEM_dupallocN(basekb->data);
2919 kb->totelem= basekb->totelem;
2922 curve_to_key(cu, kb, lb);
2926 /* copy from current values */
2927 float *data= do_ob_key(scene, ob);
2929 /* create new block with prepared data */
2930 kb= add_keyblock(key, name);
2931 kb->totelem= count_curveverts(lb);
2938 KeyBlock *object_insert_shape_key(Scene *scene, Object *ob, const char *name, int from_mix)
2940 if(ob->type==OB_MESH) return insert_meshkey(scene, ob, name, from_mix);
2941 else if ELEM(ob->type, OB_CURVE, OB_SURF)return insert_curvekey(scene, ob, name, from_mix);
2942 else if(ob->type==OB_LATTICE) return insert_lattkey(scene, ob, name, from_mix);
2946 /* most important if this is modified it should _always_ return True, in certain
2947 * cases false positives are hard to avoid (shape keys for eg)
2949 int object_is_modified(Scene *scene, Object *ob)
2953 if(ob_get_key(ob)) {
2954 flag |= eModifierMode_Render;
2959 for(md=modifiers_getVirtualModifierList(ob); md && (flag != (eModifierMode_Render | eModifierMode_Realtime)); md=md->next) {
2960 if((flag & eModifierMode_Render) == 0 && modifier_isEnabled(scene, md, eModifierMode_Render)) flag |= eModifierMode_Render;
2961 if((flag & eModifierMode_Realtime) == 0 && modifier_isEnabled(scene, md, eModifierMode_Realtime)) flag |= eModifierMode_Realtime;
2968 static void copy_object__forwardModifierLinks(void *UNUSED(userData), Object *UNUSED(ob), ID **idpoin)
2970 /* this is copied from ID_NEW; it might be better to have a macro */
2971 if(*idpoin && (*idpoin)->newid) *idpoin = (*idpoin)->newid;
2974 void object_relink(Object *ob)
2979 relink_constraints(&ob->constraints);
2982 for (chan = ob->pose->chanbase.first; chan; chan=chan->next){
2983 relink_constraints(&chan->constraints);
2986 modifiers_foreachIDLink(ob, copy_object__forwardModifierLinks, NULL);
2989 BKE_relink_animdata(ob->adt);
2994 ID_NEW(ob->proxy_group);
2997 MovieClip *object_get_movieclip(Scene *scene, Object *ob, int use_default)
2999 MovieClip *clip= use_default ? scene->clip : NULL;
3000 bConstraint *con= ob->constraints.first, *scon= NULL;
3003 if(con->type==CONSTRAINT_TYPE_CAMERASOLVER){
3004 if(scon==NULL || (scon->flag&CONSTRAINT_OFF))
3012 bCameraSolverConstraint *solver= scon->data;
3013 if((solver->flag&CAMERASOLVER_ACTIVECLIP)==0)