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_gpencil_types.h"
44 #include "DNA_group_types.h"
45 #include "DNA_key_types.h"
46 #include "DNA_lamp_types.h"
47 #include "DNA_lattice_types.h"
48 #include "DNA_material_types.h"
49 #include "DNA_meta_types.h"
50 #include "DNA_mesh_types.h"
51 #include "DNA_meshdata_types.h"
52 #include "DNA_movieclip_types.h"
53 #include "DNA_scene_types.h"
54 #include "DNA_screen_types.h"
55 #include "DNA_sequence_types.h"
56 #include "DNA_smoke_types.h"
57 #include "DNA_space_types.h"
58 #include "DNA_view3d_types.h"
59 #include "DNA_world_types.h"
60 #include "DNA_object_types.h"
61 #include "DNA_property_types.h"
62 #include "DNA_rigidbody_types.h"
64 #include "BLI_blenlib.h"
66 #include "BLI_threads.h"
67 #include "BLI_utildefines.h"
68 #include "BLI_linklist.h"
69 #include "BLI_kdtree.h"
71 #include "BLT_translation.h"
75 #include "BKE_global.h"
76 #include "BKE_idprop.h"
77 #include "BKE_armature.h"
78 #include "BKE_action.h"
79 #include "BKE_bullet.h"
80 #include "BKE_deform.h"
81 #include "BKE_depsgraph.h"
82 #include "BKE_DerivedMesh.h"
83 #include "BKE_animsys.h"
85 #include "BKE_constraint.h"
86 #include "BKE_curve.h"
87 #include "BKE_displist.h"
88 #include "BKE_effect.h"
89 #include "BKE_fcurve.h"
90 #include "BKE_group.h"
91 #include "BKE_icons.h"
94 #include "BKE_lattice.h"
95 #include "BKE_library.h"
96 #include "BKE_library_query.h"
97 #include "BKE_library_remap.h"
98 #include "BKE_linestyle.h"
100 #include "BKE_editmesh.h"
101 #include "BKE_mball.h"
102 #include "BKE_modifier.h"
103 #include "BKE_multires.h"
104 #include "BKE_node.h"
105 #include "BKE_object.h"
106 #include "BKE_paint.h"
107 #include "BKE_particle.h"
108 #include "BKE_pointcache.h"
109 #include "BKE_property.h"
110 #include "BKE_rigidbody.h"
112 #include "BKE_scene.h"
113 #include "BKE_sequencer.h"
114 #include "BKE_speaker.h"
115 #include "BKE_softbody.h"
116 #include "BKE_subsurf.h"
117 #include "BKE_material.h"
118 #include "BKE_camera.h"
119 #include "BKE_image.h"
121 #ifdef WITH_MOD_FLUID
122 #include "LBM_fluidsim.h"
126 #include "BPY_extern.h"
129 #include "CCGSubSurf.h"
130 #include "atomic_ops.h"
132 #include "GPU_material.h"
134 /* Vertex parent modifies original BMesh which is not safe for threading.
135 * Ideally such a modification should be handled as a separate DAG update
136 * callback for mesh datablock, but for until it is actually supported use
137 * simpler solution with a mutex lock.
140 #define VPARENT_THREADING_HACK
142 #ifdef VPARENT_THREADING_HACK
143 static ThreadMutex vparent_lock = BLI_MUTEX_INITIALIZER;
146 void BKE_object_workob_clear(Object *workob)
148 memset(workob, 0, sizeof(Object));
150 workob->size[0] = workob->size[1] = workob->size[2] = 1.0f;
151 workob->dscale[0] = workob->dscale[1] = workob->dscale[2] = 1.0f;
152 workob->rotmode = ROT_MODE_EUL;
155 void BKE_object_update_base_layer(struct Scene *scene, Object *ob)
157 Base *base = scene->base.first;
160 if (base->object == ob) base->lay = ob->lay;
165 void BKE_object_free_particlesystems(Object *ob)
167 ParticleSystem *psys;
169 while ((psys = BLI_pophead(&ob->particlesystem))) {
174 void BKE_object_free_softbody(Object *ob)
182 void BKE_object_free_bulletsoftbody(Object *ob)
190 void BKE_object_free_curve_cache(Object *ob)
192 if (ob->curve_cache) {
193 BKE_displist_free(&ob->curve_cache->disp);
194 BKE_curve_bevelList_free(&ob->curve_cache->bev);
195 if (ob->curve_cache->path) {
196 free_path(ob->curve_cache->path);
198 BKE_nurbList_free(&ob->curve_cache->deformed_nurbs);
199 MEM_freeN(ob->curve_cache);
200 ob->curve_cache = NULL;
204 void BKE_object_free_modifiers(Object *ob, const int flag)
208 while ((md = BLI_pophead(&ob->modifiers))) {
209 modifier_free_ex(md, flag);
212 /* particle modifiers were freed, so free the particlesystems as well */
213 BKE_object_free_particlesystems(ob);
215 /* same for softbody */
216 BKE_object_free_softbody(ob);
218 /* modifiers may have stored data in the DM cache */
219 BKE_object_free_derived_caches(ob);
222 void BKE_object_modifier_hook_reset(Object *ob, HookModifierData *hmd)
224 /* reset functionality */
226 bPoseChannel *pchan = BKE_pose_channel_find_name(hmd->object->pose, hmd->subtarget);
228 if (hmd->subtarget[0] && pchan) {
229 float imat[4][4], mat[4][4];
231 /* calculate the world-space matrix for the pose-channel target first, then carry on as usual */
232 mul_m4_m4m4(mat, hmd->object->obmat, pchan->pose_mat);
234 invert_m4_m4(imat, mat);
235 mul_m4_m4m4(hmd->parentinv, imat, ob->obmat);
238 invert_m4_m4(hmd->object->imat, hmd->object->obmat);
239 mul_m4_m4m4(hmd->parentinv, hmd->object->imat, ob->obmat);
244 bool BKE_object_support_modifier_type_check(const Object *ob, int modifier_type)
246 const ModifierTypeInfo *mti;
248 mti = modifierType_getInfo(modifier_type);
250 /* only geometry objects should be able to get modifiers [#25291] */
251 if (!ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
255 if (ob->type == OB_LATTICE && (mti->flags & eModifierTypeFlag_AcceptsLattice) == 0) {
259 if (!((mti->flags & eModifierTypeFlag_AcceptsCVs) ||
260 (ob->type == OB_MESH && (mti->flags & eModifierTypeFlag_AcceptsMesh))))
268 void BKE_object_link_modifiers(struct Object *ob_dst, const struct Object *ob_src)
271 BKE_object_free_modifiers(ob_dst, 0);
273 if (!ELEM(ob_dst->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
274 /* only objects listed above can have modifiers and linking them to objects
275 * which doesn't have modifiers stack is quite silly */
279 for (md = ob_src->modifiers.first; md; md = md->next) {
280 ModifierData *nmd = NULL;
284 eModifierType_Collision))
289 if (!BKE_object_support_modifier_type_check(ob_dst, md->type))
293 case eModifierType_Softbody:
294 BKE_object_copy_softbody(ob_dst, ob_src);
296 case eModifierType_Skin:
297 /* ensure skin-node customdata exists */
298 BKE_mesh_ensure_skin_customdata(ob_dst->data);
302 nmd = modifier_new(md->type);
303 BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
305 if (md->type == eModifierType_Multires) {
306 /* Has to be done after mod creation, but *before* we actually copy its settings! */
307 multiresModifier_sync_levels_ex(ob_dst, (MultiresModifierData *)md, (MultiresModifierData *)nmd);
310 modifier_copyData(md, nmd);
311 BLI_addtail(&ob_dst->modifiers, nmd);
312 modifier_unique_name(&ob_dst->modifiers, nmd);
315 BKE_object_copy_particlesystems(ob_dst, ob_src, 0);
317 /* TODO: smoke?, cloth? */
320 /* free data derived from mesh, called when mesh changes or is freed */
321 void BKE_object_free_derived_caches(Object *ob)
323 /* Also serves as signal to remake texspace.
325 * NOTE: This function can be called from threads on different objects
326 * sharing same data datablock. So we need to ensure atomic nature of
327 * data modification here.
329 if (ob->type == OB_MESH) {
333 atomic_fetch_and_or_int32(&me->bb->flag, BOUNDBOX_DIRTY);
336 else if (ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) {
337 Curve *cu = ob->data;
340 atomic_fetch_and_or_int32(&cu->bb->flag, BOUNDBOX_DIRTY);
349 if (ob->derivedFinal) {
350 ob->derivedFinal->needsFree = 1;
351 ob->derivedFinal->release(ob->derivedFinal);
352 ob->derivedFinal = NULL;
354 if (ob->derivedDeform) {
355 ob->derivedDeform->needsFree = 1;
356 ob->derivedDeform->release(ob->derivedDeform);
357 ob->derivedDeform = NULL;
360 BKE_object_free_curve_cache(ob);
363 void BKE_object_free_caches(Object *object)
366 short update_flag = 0;
368 /* Free particle system caches holding paths. */
369 if (object->particlesystem.first) {
370 ParticleSystem *psys;
371 for (psys = object->particlesystem.first;
375 psys_free_path_cache(psys, psys->edit);
376 update_flag |= PSYS_RECALC_REDO;
380 /* Free memory used by cached derived meshes in the particle system modifiers. */
381 for (md = object->modifiers.first; md != NULL; md = md->next) {
382 if (md->type == eModifierType_ParticleSystem) {
383 ParticleSystemModifierData *psmd = (ParticleSystemModifierData *) md;
384 if (psmd->dm_final != NULL) {
385 psmd->dm_final->needsFree = 1;
386 psmd->dm_final->release(psmd->dm_final);
387 psmd->dm_final = NULL;
388 if (psmd->dm_deformed != NULL) {
389 psmd->dm_deformed->needsFree = 1;
390 psmd->dm_deformed->release(psmd->dm_deformed);
391 psmd->dm_deformed = NULL;
393 psmd->flag |= eParticleSystemFlag_file_loaded;
394 update_flag |= OB_RECALC_DATA;
399 /* Tag object for update, so once memory critical operation is over and
400 * scene update routines are back to it's business the object will be
401 * guaranteed to be in a known state.
403 if (update_flag != 0) {
404 DAG_id_tag_update(&object->id, update_flag);
408 /** Free (or release) any data used by this object (does not free the object itself). */
409 void BKE_object_free(Object *ob)
411 BKE_animdata_free((ID *)ob, false);
413 /* BKE_<id>_free shall never touch to ID->us. Never ever. */
414 BKE_object_free_modifiers(ob, LIB_ID_CREATE_NO_USER_REFCOUNT);
416 MEM_SAFE_FREE(ob->mat);
417 MEM_SAFE_FREE(ob->matbits);
418 MEM_SAFE_FREE(ob->iuser);
419 MEM_SAFE_FREE(ob->bb);
421 BLI_freelistN(&ob->defbase);
423 BKE_pose_free_ex(ob->pose, false);
427 animviz_free_motionpath(ob->mpath);
430 BKE_bproperty_free_list(&ob->prop);
432 free_sensors(&ob->sensors);
433 free_controllers(&ob->controllers);
434 free_actuators(&ob->actuators);
436 BKE_constraints_free_ex(&ob->constraints, false);
438 free_partdeflect(ob->pd);
439 BKE_rigidbody_free_object(ob);
440 BKE_rigidbody_free_constraint(ob);
452 BKE_sculptsession_free(ob);
454 BLI_freelistN(&ob->pc_ids);
456 BLI_freelistN(&ob->lodlevels);
458 /* Free runtime curves data. */
459 if (ob->curve_cache) {
460 BKE_curve_bevelList_free(&ob->curve_cache->bev);
461 if (ob->curve_cache->path)
462 free_path(ob->curve_cache->path);
463 MEM_freeN(ob->curve_cache);
464 ob->curve_cache = NULL;
467 BKE_previewimg_free(&ob->preview);
470 /* actual check for internal data, not context or flags */
471 bool BKE_object_is_in_editmode(const Object *ob)
473 if (ob->data == NULL)
476 if (ob->type == OB_MESH) {
481 else if (ob->type == OB_ARMATURE) {
482 bArmature *arm = ob->data;
487 else if (ob->type == OB_FONT) {
488 Curve *cu = ob->data;
493 else if (ob->type == OB_MBALL) {
494 MetaBall *mb = ob->data;
499 else if (ob->type == OB_LATTICE) {
500 Lattice *lt = ob->data;
505 else if (ob->type == OB_SURF || ob->type == OB_CURVE) {
506 Curve *cu = ob->data;
514 bool BKE_object_is_in_editmode_vgroup(const Object *ob)
516 return (OB_TYPE_SUPPORT_VGROUP(ob->type) &&
517 BKE_object_is_in_editmode(ob));
520 bool BKE_object_is_in_wpaint_select_vert(const Object *ob)
522 if (ob->type == OB_MESH) {
524 return ((ob->mode & OB_MODE_WEIGHT_PAINT) &&
525 (me->edit_btmesh == NULL) &&
526 (ME_EDIT_PAINT_SEL_MODE(me) == SCE_SELECT_VERTEX));
532 bool BKE_object_exists_check(Main *bmain, const Object *obtest)
536 if (obtest == NULL) return false;
538 ob = bmain->object.first;
540 if (ob == obtest) return true;
546 /* *************************************************** */
548 static const char *get_obdata_defname(int type)
551 case OB_MESH: return DATA_("Mesh");
552 case OB_CURVE: return DATA_("Curve");
553 case OB_SURF: return DATA_("Surf");
554 case OB_FONT: return DATA_("Text");
555 case OB_MBALL: return DATA_("Mball");
556 case OB_CAMERA: return DATA_("Camera");
557 case OB_LAMP: return DATA_("Lamp");
558 case OB_LATTICE: return DATA_("Lattice");
559 case OB_ARMATURE: return DATA_("Armature");
560 case OB_SPEAKER: return DATA_("Speaker");
561 case OB_EMPTY: return DATA_("Empty");
563 printf("get_obdata_defname: Internal error, bad type: %d\n", type);
564 return DATA_("Empty");
568 void *BKE_object_obdata_add_from_type(Main *bmain, int type, const char *name)
571 name = get_obdata_defname(type);
575 case OB_MESH: return BKE_mesh_add(bmain, name);
576 case OB_CURVE: return BKE_curve_add(bmain, name, OB_CURVE);
577 case OB_SURF: return BKE_curve_add(bmain, name, OB_SURF);
578 case OB_FONT: return BKE_curve_add(bmain, name, OB_FONT);
579 case OB_MBALL: return BKE_mball_add(bmain, name);
580 case OB_CAMERA: return BKE_camera_add(bmain, name);
581 case OB_LAMP: return BKE_lamp_add(bmain, name);
582 case OB_LATTICE: return BKE_lattice_add(bmain, name);
583 case OB_ARMATURE: return BKE_armature_add(bmain, name);
584 case OB_SPEAKER: return BKE_speaker_add(bmain, name);
585 case OB_EMPTY: return NULL;
587 printf("%s: Internal error, bad type: %d\n", __func__, type);
592 void BKE_object_init(Object *ob)
594 /* BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(ob, id)); */ /* ob->type is already initialized... */
596 ob->col[0] = ob->col[1] = ob->col[2] = 1.0;
599 ob->size[0] = ob->size[1] = ob->size[2] = 1.0;
600 ob->dscale[0] = ob->dscale[1] = ob->dscale[2] = 1.0;
602 /* objects should default to having Euler XYZ rotations,
603 * but rotations default to quaternions
605 ob->rotmode = ROT_MODE_EUL;
607 unit_axis_angle(ob->rotAxis, &ob->rotAngle);
608 unit_axis_angle(ob->drotAxis, &ob->drotAngle);
613 /* rotation locks should be 4D for 4 component rotations by default... */
614 ob->protectflag = OB_LOCK_ROT4D;
616 unit_m4(ob->constinv);
617 unit_m4(ob->parentinv);
620 ob->empty_drawtype = OB_PLAINAXES;
621 ob->empty_drawsize = 1.0;
622 if (ob->type == OB_EMPTY) {
623 copy_v2_fl(ob->ima_ofs, -0.5f);
626 if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
627 ob->trackflag = OB_NEGZ;
628 ob->upflag = OB_POSY;
631 ob->trackflag = OB_POSY;
632 ob->upflag = OB_POSZ;
635 ob->dupon = 1; ob->dupoff = 0;
636 ob->dupsta = 1; ob->dupend = 100;
637 ob->dupfacesca = 1.0;
639 /* Game engine defaults*/
640 ob->mass = ob->inertia = 1.0f;
641 ob->formfactor = 0.4f;
644 ob->anisotropicFriction[0] = 1.0f;
645 ob->anisotropicFriction[1] = 1.0f;
646 ob->anisotropicFriction[2] = 1.0f;
647 ob->gameflag = OB_PROP | OB_COLLISION;
651 ob->obstacleRad = 1.0f;
652 ob->step_height = 0.15f;
653 ob->jump_speed = 10.0f;
654 ob->fall_speed = 55.0f;
656 ob->col_group = 0x01;
657 ob->col_mask = 0xffff;
660 /* NT fluid sim defaults */
661 ob->fluidsimSettings = NULL;
663 BLI_listbase_clear(&ob->pc_ids);
665 /* Animation Visualization defaults */
666 animviz_settings_init(&ob->avs);
669 /* more general add: creates minimum required data, but without vertices etc. */
670 Object *BKE_object_add_only_object(Main *bmain, int type, const char *name)
675 name = get_obdata_defname(type);
677 ob = BKE_libblock_alloc(bmain, ID_OB, name, 0);
679 /* default object vars */
687 /* general add: to scene, with layer from area and default name */
688 /* creates minimum required data, but without vertices etc. */
689 Object *BKE_object_add(
690 Main *bmain, Scene *scene,
691 int type, const char *name)
696 ob = BKE_object_add_only_object(bmain, type, name);
698 ob->data = BKE_object_obdata_add_from_type(bmain, type, name);
700 ob->lay = scene->lay;
702 base = BKE_scene_base_add(scene, ob);
703 BKE_scene_base_deselect_all(scene);
704 BKE_scene_base_select(scene, base);
705 DAG_id_tag_update_ex(bmain, &ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
711 #ifdef WITH_GAMEENGINE
713 void BKE_object_lod_add(Object *ob)
715 LodLevel *lod = MEM_callocN(sizeof(LodLevel), "LoD Level");
716 LodLevel *last = ob->lodlevels.last;
718 /* If the lod list is empty, initialize it with the base lod level */
720 LodLevel *base = MEM_callocN(sizeof(LodLevel), "Base LoD Level");
721 BLI_addtail(&ob->lodlevels, base);
722 base->flags = OB_LOD_USE_MESH | OB_LOD_USE_MAT;
724 base->obhysteresis = 10;
725 last = ob->currentlod = base;
728 lod->distance = last->distance + 25.0f;
729 lod->obhysteresis = 10;
730 lod->flags = OB_LOD_USE_MESH | OB_LOD_USE_MAT;
732 BLI_addtail(&ob->lodlevels, lod);
735 static int lod_cmp(const void *a, const void *b)
737 const LodLevel *loda = a;
738 const LodLevel *lodb = b;
740 if (loda->distance < lodb->distance) return -1;
741 return loda->distance > lodb->distance;
744 void BKE_object_lod_sort(Object *ob)
746 BLI_listbase_sort(&ob->lodlevels, lod_cmp);
749 bool BKE_object_lod_remove(Object *ob, int level)
753 if (level < 1 || level > BLI_listbase_count(&ob->lodlevels) - 1)
756 rem = BLI_findlink(&ob->lodlevels, level);
758 if (rem == ob->currentlod) {
759 ob->currentlod = rem->prev;
762 BLI_remlink(&ob->lodlevels, rem);
765 /* If there are no user defined lods, remove the base lod as well */
766 if (BLI_listbase_is_single(&ob->lodlevels)) {
767 LodLevel *base = ob->lodlevels.first;
768 BLI_remlink(&ob->lodlevels, base);
770 ob->currentlod = NULL;
776 static LodLevel *lod_level_select(Object *ob, const float camera_position[3])
778 LodLevel *current = ob->currentlod;
781 if (!current) return NULL;
783 dist_sq = len_squared_v3v3(ob->obmat[3], camera_position);
785 if (dist_sq < SQUARE(current->distance)) {
786 /* check for higher LoD */
787 while (current->prev && dist_sq < SQUARE(current->distance)) {
788 current = current->prev;
792 /* check for lower LoD */
793 while (current->next && dist_sq > SQUARE(current->next->distance)) {
794 current = current->next;
801 bool BKE_object_lod_is_usable(Object *ob, Scene *scene)
803 bool active = (scene) ? ob == OBACT : false;
804 return (ob->mode == OB_MODE_OBJECT || !active);
807 void BKE_object_lod_update(Object *ob, const float camera_position[3])
809 LodLevel *cur_level = ob->currentlod;
810 LodLevel *new_level = lod_level_select(ob, camera_position);
812 if (new_level != cur_level) {
813 ob->currentlod = new_level;
817 static Object *lod_ob_get(Object *ob, Scene *scene, int flag)
819 LodLevel *current = ob->currentlod;
821 if (!current || !BKE_object_lod_is_usable(ob, scene))
824 while (current->prev && (!(current->flags & flag) || !current->source || current->source->type != OB_MESH)) {
825 current = current->prev;
828 return current->source;
831 struct Object *BKE_object_lod_meshob_get(Object *ob, Scene *scene)
833 return lod_ob_get(ob, scene, OB_LOD_USE_MESH);
836 struct Object *BKE_object_lod_matob_get(Object *ob, Scene *scene)
838 return lod_ob_get(ob, scene, OB_LOD_USE_MAT);
841 #endif /* WITH_GAMEENGINE */
844 SoftBody *copy_softbody(const SoftBody *sb, const int flag)
848 if (sb == NULL) return(NULL);
850 sbn = MEM_dupallocN(sb);
852 if ((flag & LIB_ID_COPY_CACHES) == 0) {
853 sbn->totspring = sbn->totpoint = 0;
858 sbn->totspring = sb->totspring;
859 sbn->totpoint = sb->totpoint;
864 sbn->bpoint = MEM_dupallocN(sbn->bpoint);
866 for (i = 0; i < sbn->totpoint; i++) {
867 if (sbn->bpoint[i].springs)
868 sbn->bpoint[i].springs = MEM_dupallocN(sbn->bpoint[i].springs);
873 sbn->bspring = MEM_dupallocN(sb->bspring);
877 sbn->totkey = sbn->totpointkey = 0;
881 sbn->pointcache = BKE_ptcache_copy_list(&sbn->ptcaches, &sb->ptcaches, flag);
883 if (sb->effector_weights)
884 sbn->effector_weights = MEM_dupallocN(sb->effector_weights);
889 BulletSoftBody *copy_bulletsoftbody(const BulletSoftBody *bsb, const int UNUSED(flag))
891 BulletSoftBody *bsbn;
895 bsbn = MEM_dupallocN(bsb);
896 /* no pointer in this structure yet */
900 ParticleSystem *BKE_object_copy_particlesystem(ParticleSystem *psys, const int flag)
902 ParticleSystem *psysn;
906 psysn = MEM_dupallocN(psys);
907 psysn->particles = MEM_dupallocN(psys->particles);
908 psysn->child = MEM_dupallocN(psys->child);
910 if (psys->part->type == PART_HAIR) {
911 for (p = 0, pa = psysn->particles; p < psysn->totpart; p++, pa++)
912 pa->hair = MEM_dupallocN(pa->hair);
915 if (psysn->particles && (psysn->particles->keys || psysn->particles->boid)) {
916 ParticleKey *key = psysn->particles->keys;
917 BoidParticle *boid = psysn->particles->boid;
920 key = MEM_dupallocN(key);
923 boid = MEM_dupallocN(boid);
925 for (p = 0, pa = psysn->particles; p < psysn->totpart; p++, pa++) {
936 psysn->clmd = (ClothModifierData *)modifier_new(eModifierType_Cloth);
937 modifier_copyData_ex((ModifierData *)psys->clmd, (ModifierData *)psysn->clmd, flag);
938 psys->hair_in_dm = psys->hair_out_dm = NULL;
941 BLI_duplicatelist(&psysn->targets, &psys->targets);
943 psysn->pathcache = NULL;
944 psysn->childcache = NULL;
947 psysn->effectors = NULL;
949 psysn->bvhtree = NULL;
951 BLI_listbase_clear(&psysn->pathcachebufs);
952 BLI_listbase_clear(&psysn->childcachebufs);
953 psysn->renderdata = NULL;
955 /* XXX Never copy caches here? */
956 psysn->pointcache = BKE_ptcache_copy_list(&psysn->ptcaches, &psys->ptcaches, flag & ~LIB_ID_COPY_CACHES);
958 /* XXX - from reading existing code this seems correct but intended usage of
959 * pointcache should /w cloth should be added in 'ParticleSystem' - campbell */
961 psysn->clmd->point_cache = psysn->pointcache;
964 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
965 id_us_plus((ID *)psysn->part);
971 void BKE_object_copy_particlesystems(Object *ob_dst, const Object *ob_src, const int flag)
973 ParticleSystem *psys, *npsys;
976 if (ob_dst->type != OB_MESH) {
977 /* currently only mesh objects can have soft body */
981 BLI_listbase_clear(&ob_dst->particlesystem);
982 for (psys = ob_src->particlesystem.first; psys; psys = psys->next) {
983 npsys = BKE_object_copy_particlesystem(psys, flag);
985 BLI_addtail(&ob_dst->particlesystem, npsys);
987 /* need to update particle modifiers too */
988 for (md = ob_dst->modifiers.first; md; md = md->next) {
989 if (md->type == eModifierType_ParticleSystem) {
990 ParticleSystemModifierData *psmd = (ParticleSystemModifierData *)md;
991 if (psmd->psys == psys)
994 else if (md->type == eModifierType_DynamicPaint) {
995 DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
997 if (pmd->brush->psys == psys) {
998 pmd->brush->psys = npsys;
1002 else if (md->type == eModifierType_Smoke) {
1003 SmokeModifierData *smd = (SmokeModifierData *) md;
1005 if (smd->type == MOD_SMOKE_TYPE_FLOW) {
1007 if (smd->flow->psys == psys)
1008 smd->flow->psys = npsys;
1016 void BKE_object_copy_softbody(Object *ob_dst, const Object *ob_src)
1019 ob_dst->softflag = ob_src->softflag;
1020 ob_dst->soft = copy_softbody(ob_src->soft, 0);
1024 static void copy_object_pose(Object *obn, const Object *ob, const int flag)
1028 /* note: need to clear obn->pose pointer first, so that BKE_pose_copy_data works (otherwise there's a crash) */
1030 BKE_pose_copy_data_ex(&obn->pose, ob->pose, flag, true); /* true = copy constraints */
1032 for (chan = obn->pose->chanbase.first; chan; chan = chan->next) {
1035 chan->flag &= ~(POSE_LOC | POSE_ROT | POSE_SIZE);
1037 /* XXX Remapping object pointing onto itself should be handled by generic BKE_library_remap stuff, but...
1038 * the flush_constraint_targets callback am not sure about, so will delay that for now. */
1039 for (con = chan->constraints.first; con; con = con->next) {
1040 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
1041 ListBase targets = {NULL, NULL};
1042 bConstraintTarget *ct;
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 void copy_object_lod(Object *obn, const Object *ob, const int UNUSED(flag))
1061 BLI_duplicatelist(&obn->lodlevels, &ob->lodlevels);
1063 obn->currentlod = (LodLevel *)obn->lodlevels.first;
1066 bool BKE_object_pose_context_check(const Object *ob)
1069 (ob->type == OB_ARMATURE) &&
1071 (ob->mode & OB_MODE_POSE))
1080 Object *BKE_object_pose_armature_get(Object *ob)
1085 if (BKE_object_pose_context_check(ob))
1088 ob = modifiers_isDeformedByArmature(ob);
1090 if (BKE_object_pose_context_check(ob))
1096 void BKE_object_transform_copy(Object *ob_tar, const Object *ob_src)
1098 copy_v3_v3(ob_tar->loc, ob_src->loc);
1099 copy_v3_v3(ob_tar->rot, ob_src->rot);
1100 copy_v3_v3(ob_tar->quat, ob_src->quat);
1101 copy_v3_v3(ob_tar->rotAxis, ob_src->rotAxis);
1102 ob_tar->rotAngle = ob_src->rotAngle;
1103 ob_tar->rotmode = ob_src->rotmode;
1104 copy_v3_v3(ob_tar->size, ob_src->size);
1108 * Only copy internal data of Object ID from source to already allocated/initialized destination.
1109 * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
1111 * WARNING! This function will not handle ID user count!
1113 * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
1115 void BKE_object_copy_data(Main *UNUSED(bmain), Object *ob_dst, const Object *ob_src, const int flag)
1119 /* We never handle usercount here for own data. */
1120 const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
1122 if (ob_src->totcol) {
1123 ob_dst->mat = MEM_dupallocN(ob_src->mat);
1124 ob_dst->matbits = MEM_dupallocN(ob_src->matbits);
1125 ob_dst->totcol = ob_src->totcol;
1127 else if (ob_dst->mat != NULL || ob_dst->matbits != NULL) {
1128 /* This shall not be needed, but better be safe than sorry. */
1129 BLI_assert(!"Object copy: non-NULL material pointers with zero counter, should not happen.");
1131 ob_dst->matbits = NULL;
1134 if (ob_src->iuser) ob_dst->iuser = MEM_dupallocN(ob_src->iuser);
1136 if (ob_src->bb) ob_dst->bb = MEM_dupallocN(ob_src->bb);
1137 ob_dst->flag &= ~OB_FROMGROUP;
1139 BLI_listbase_clear(&ob_dst->modifiers);
1141 for (md = ob_src->modifiers.first; md; md = md->next) {
1142 ModifierData *nmd = modifier_new(md->type);
1143 BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
1144 modifier_copyData_ex(md, nmd, flag_subdata);
1145 BLI_addtail(&ob_dst->modifiers, nmd);
1148 BLI_listbase_clear(&ob_dst->prop);
1149 BKE_bproperty_copy_list(&ob_dst->prop, &ob_src->prop);
1151 BKE_sca_logic_copy(ob_dst, ob_src, flag_subdata);
1154 copy_object_pose(ob_dst, ob_src, flag_subdata);
1155 /* backwards compat... non-armatures can get poses in older files? */
1156 if (ob_src->type == OB_ARMATURE)
1157 BKE_pose_rebuild(ob_dst, ob_dst->data);
1159 defgroup_copy_list(&ob_dst->defbase, &ob_src->defbase);
1160 BKE_constraints_copy_ex(&ob_dst->constraints, &ob_src->constraints, flag_subdata, true);
1162 ob_dst->mode = OB_MODE_OBJECT;
1163 ob_dst->sculpt = NULL;
1166 ob_dst->pd = MEM_dupallocN(ob_src->pd);
1167 if (ob_dst->pd->rng) {
1168 ob_dst->pd->rng = MEM_dupallocN(ob_src->pd->rng);
1171 ob_dst->soft = copy_softbody(ob_src->soft, flag_subdata);
1172 ob_dst->bsoft = copy_bulletsoftbody(ob_src->bsoft, flag_subdata);
1173 ob_dst->rigidbody_object = BKE_rigidbody_copy_object(ob_src, flag_subdata);
1174 ob_dst->rigidbody_constraint = BKE_rigidbody_copy_constraint(ob_src, flag_subdata);
1176 BKE_object_copy_particlesystems(ob_dst, ob_src, flag_subdata);
1178 ob_dst->derivedDeform = NULL;
1179 ob_dst->derivedFinal = NULL;
1181 BLI_listbase_clear(&ob_dst->gpulamp);
1182 BLI_listbase_clear(&ob_dst->pc_ids);
1184 ob_dst->mpath = NULL;
1186 copy_object_lod(ob_dst, ob_src, flag_subdata);
1188 /* Do not copy runtime curve data. */
1189 ob_dst->curve_cache = NULL;
1191 /* Do not copy object's preview (mostly due to the fact renderers create temp copy of objects). */
1192 if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0 && false) { /* XXX TODO temp hack */
1193 BKE_previewimg_id_copy(&ob_dst->id, &ob_src->id);
1196 ob_dst->preview = NULL;
1200 /* copy objects, will re-initialize cached simulation data */
1201 Object *BKE_object_copy(Main *bmain, const Object *ob)
1204 BKE_id_copy_ex(bmain, &ob->id, (ID **)&ob_copy, 0, false);
1208 void BKE_object_make_local_ex(Main *bmain, Object *ob, const bool lib_local, const bool clear_proxy)
1210 bool is_local = false, is_lib = false;
1212 /* - only lib users: do nothing (unless force_local is set)
1213 * - only local users: set flag
1214 * - mixed: make copy
1215 * In case we make a whole lib's content local, we always want to localize, and we skip remapping (done later).
1218 if (!ID_IS_LINKED(ob)) {
1222 BKE_library_ID_test_usages(bmain, ob, &is_local, &is_lib);
1224 if (lib_local || is_local) {
1226 id_clear_lib_data(bmain, &ob->id);
1227 BKE_id_expand_local(bmain, &ob->id);
1229 if (ob->proxy_from != NULL) {
1230 ob->proxy_from->proxy = NULL;
1231 ob->proxy_from->proxy_group = NULL;
1233 ob->proxy = ob->proxy_from = ob->proxy_group = NULL;
1237 Object *ob_new = BKE_object_copy(bmain, ob);
1240 ob_new->proxy = ob_new->proxy_from = ob_new->proxy_group = NULL;
1242 /* setting newid is mandatory for complex make_lib_local logic... */
1243 ID_NEW_SET(ob, ob_new);
1246 BKE_libblock_remap(bmain, ob, ob_new, ID_REMAP_SKIP_INDIRECT_USAGE);
1252 void BKE_object_make_local(Main *bmain, Object *ob, const bool lib_local)
1254 BKE_object_make_local_ex(bmain, ob, lib_local, true);
1257 /* Returns true if the Object is from an external blend file (libdata) */
1258 bool BKE_object_is_libdata(const Object *ob)
1260 return (ob && ID_IS_LINKED(ob));
1263 /* Returns true if the Object data is from an external blend file (libdata) */
1264 bool BKE_object_obdata_is_libdata(const Object *ob)
1266 /* Linked objects with local obdata are forbidden! */
1267 BLI_assert(!ob || !ob->data || (ID_IS_LINKED(ob) ? ID_IS_LINKED(ob->data) : true));
1268 return (ob && ob->data && ID_IS_LINKED(ob->data));
1271 /* *************** PROXY **************** */
1273 /* when you make proxy, ensure the exposed layers are extern */
1274 static void armature_set_id_extern(Object *ob)
1276 bArmature *arm = ob->data;
1277 bPoseChannel *pchan;
1278 unsigned int lay = arm->layer_protected;
1280 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1281 if (!(pchan->bone->layer & lay))
1282 id_lib_extern((ID *)pchan->custom);
1287 void BKE_object_copy_proxy_drivers(Object *ob, Object *target)
1289 if ((target->adt) && (target->adt->drivers.first)) {
1292 /* add new animdata block */
1294 ob->adt = BKE_animdata_add_id(&ob->id);
1296 /* make a copy of all the drivers (for now), then correct any links that need fixing */
1297 free_fcurves(&ob->adt->drivers);
1298 copy_fcurves(&ob->adt->drivers, &target->adt->drivers);
1300 for (fcu = ob->adt->drivers.first; fcu; fcu = fcu->next) {
1301 ChannelDriver *driver = fcu->driver;
1304 for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
1306 DRIVER_TARGETS_LOOPER(dvar)
1309 if ((Object *)dtar->id == target)
1310 dtar->id = (ID *)ob;
1312 /* only on local objects because this causes indirect links
1313 * 'a -> b -> c', blend to point directly to a.blend
1314 * when a.blend has a proxy thats linked into c.blend */
1315 if (!ID_IS_LINKED(ob))
1316 id_lib_extern((ID *)dtar->id);
1320 DRIVER_TARGETS_LOOPER_END
1326 /* proxy rule: lib_object->proxy_from == the one we borrow from, set temporally while object_update */
1327 /* local_object->proxy == pointer to library object, saved in files and read */
1328 /* local_object->proxy_group == pointer to group dupli-object, saved in files and read */
1330 void BKE_object_make_proxy(Object *ob, Object *target, Object *gob)
1332 /* paranoia checks */
1333 if (ID_IS_LINKED(ob) || !ID_IS_LINKED(target)) {
1334 printf("cannot make proxy\n");
1339 ob->proxy_group = gob;
1340 id_lib_extern(&target->id);
1342 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1343 DAG_id_tag_update(&target->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1346 * - gob means this proxy comes from a group, just apply the matrix
1347 * so the object wont move from its dupli-transform.
1349 * - no gob means this is being made from a linked object,
1350 * this is closer to making a copy of the object - in-place. */
1352 ob->rotmode = target->rotmode;
1353 mul_m4_m4m4(ob->obmat, gob->obmat, target->obmat);
1354 if (gob->dup_group) { /* should always be true */
1356 mul_v3_mat3_m4v3(tvec, ob->obmat, gob->dup_group->dupli_ofs);
1357 sub_v3_v3(ob->obmat[3], tvec);
1359 BKE_object_apply_mat4(ob, ob->obmat, false, true);
1362 BKE_object_transform_copy(ob, target);
1363 ob->parent = target->parent; /* libdata */
1364 copy_m4_m4(ob->parentinv, target->parentinv);
1367 /* copy animdata stuff - drivers only for now... */
1368 BKE_object_copy_proxy_drivers(ob, target);
1370 /* skip constraints? */
1371 /* FIXME: this is considered by many as a bug */
1373 /* set object type and link to data */
1374 ob->type = target->type;
1375 ob->data = target->data;
1376 id_us_plus((ID *)ob->data); /* ensures lib data becomes LIB_TAG_EXTERN */
1378 /* copy vertex groups */
1379 defgroup_copy_list(&ob->defbase, &target->defbase);
1381 /* copy material and index information */
1382 ob->actcol = ob->totcol = 0;
1383 if (ob->mat) MEM_freeN(ob->mat);
1384 if (ob->matbits) MEM_freeN(ob->matbits);
1387 if ((target->totcol) && (target->mat) && OB_TYPE_SUPPORT_MATERIAL(ob->type)) {
1390 ob->actcol = target->actcol;
1391 ob->totcol = target->totcol;
1393 ob->mat = MEM_dupallocN(target->mat);
1394 ob->matbits = MEM_dupallocN(target->matbits);
1395 for (i = 0; i < target->totcol; i++) {
1396 /* don't need to run test_object_materials since we know this object is new and not used elsewhere */
1397 id_us_plus((ID *)ob->mat[i]);
1401 /* type conversions */
1402 if (target->type == OB_ARMATURE) {
1403 copy_object_pose(ob, target, 0); /* data copy, object pointers in constraints */
1404 BKE_pose_rest(ob->pose); /* clear all transforms in channels */
1405 BKE_pose_rebuild(ob, ob->data); /* set all internal links */
1407 armature_set_id_extern(ob);
1409 else if (target->type == OB_EMPTY) {
1410 ob->empty_drawtype = target->empty_drawtype;
1411 ob->empty_drawsize = target->empty_drawsize;
1414 /* copy IDProperties */
1415 if (ob->id.properties) {
1416 IDP_FreeProperty(ob->id.properties);
1417 MEM_freeN(ob->id.properties);
1418 ob->id.properties = NULL;
1420 if (target->id.properties) {
1421 ob->id.properties = IDP_CopyProperty(target->id.properties);
1424 /* copy drawtype info */
1425 ob->dt = target->dt;
1429 * Use with newly created objects to set their size
1430 * (used to apply scene-scale).
1432 void BKE_object_obdata_size_init(struct Object *ob, const float size)
1434 /* apply radius as a scale to types that support it */
1438 ob->empty_drawsize *= size;
1443 Curve *cu = ob->data;
1449 Camera *cam = ob->data;
1450 cam->drawsize *= size;
1455 Lamp *lamp = ob->data;
1457 lamp->area_size *= size;
1458 lamp->area_sizey *= size;
1459 lamp->area_sizez *= size;
1462 /* Only lattice (not mesh, curve, mball...),
1463 * because its got data when newly added */
1466 struct Lattice *lt = ob->data;
1470 scale_m4_fl(mat, size);
1472 BKE_lattice_transform(lt, (float (*)[4])mat, false);
1478 /* *************** CALC ****************** */
1480 void BKE_object_scale_to_mat3(Object *ob, float mat[3][3])
1483 mul_v3_v3v3(vec, ob->size, ob->dscale);
1484 size_to_mat3(mat, vec);
1487 void BKE_object_rot_to_mat3(Object *ob, float mat[3][3], bool use_drot)
1489 float rmat[3][3], dmat[3][3];
1491 /* 'dmat' is the delta-rotation matrix, which will get (pre)multiplied
1492 * with the rotation matrix to yield the appropriate rotation
1495 /* rotations may either be quats, eulers (with various rotation orders), or axis-angle */
1496 if (ob->rotmode > 0) {
1497 /* euler rotations (will cause gimble lock, but this can be alleviated a bit with rotation orders) */
1498 eulO_to_mat3(rmat, ob->rot, ob->rotmode);
1499 eulO_to_mat3(dmat, ob->drot, ob->rotmode);
1501 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
1502 /* axis-angle - not really that great for 3D-changing orientations */
1503 axis_angle_to_mat3(rmat, ob->rotAxis, ob->rotAngle);
1504 axis_angle_to_mat3(dmat, ob->drotAxis, ob->drotAngle);
1507 /* quats are normalized before use to eliminate scaling issues */
1510 normalize_qt_qt(tquat, ob->quat);
1511 quat_to_mat3(rmat, tquat);
1513 normalize_qt_qt(tquat, ob->dquat);
1514 quat_to_mat3(dmat, tquat);
1517 /* combine these rotations */
1519 mul_m3_m3m3(mat, dmat, rmat);
1521 copy_m3_m3(mat, rmat);
1524 void BKE_object_mat3_to_rot(Object *ob, float mat[3][3], bool use_compat)
1526 BLI_ASSERT_UNIT_M3(mat);
1528 switch (ob->rotmode) {
1532 mat3_normalized_to_quat(ob->quat, mat);
1533 normalize_qt_qt(dquat, ob->dquat);
1534 invert_qt_normalized(dquat);
1535 mul_qt_qtqt(ob->quat, dquat, ob->quat);
1538 case ROT_MODE_AXISANGLE:
1543 /* without drot we could apply 'mat' directly */
1544 mat3_normalized_to_quat(quat, mat);
1545 axis_angle_to_quat(dquat, ob->drotAxis, ob->drotAngle);
1546 invert_qt_normalized(dquat);
1547 mul_qt_qtqt(quat, dquat, quat);
1548 quat_to_axis_angle(ob->rotAxis, &ob->rotAngle, quat);
1551 default: /* euler */
1556 /* without drot we could apply 'mat' directly */
1557 mat3_normalized_to_quat(quat, mat);
1558 eulO_to_quat(dquat, ob->drot, ob->rotmode);
1559 invert_qt_normalized(dquat);
1560 mul_qt_qtqt(quat, dquat, quat);
1561 /* end drot correction */
1563 if (use_compat) quat_to_compatible_eulO(ob->rot, ob->rot, ob->rotmode, quat);
1564 else quat_to_eulO(ob->rot, ob->rotmode, quat);
1570 void BKE_object_tfm_protected_backup(const Object *ob,
1571 ObjectTfmProtectedChannels *obtfm)
1574 #define TFMCPY(_v) (obtfm->_v = ob->_v)
1575 #define TFMCPY3D(_v) copy_v3_v3(obtfm->_v, ob->_v)
1576 #define TFMCPY4D(_v) copy_v4_v4(obtfm->_v, ob->_v)
1597 void BKE_object_tfm_protected_restore(Object *ob,
1598 const ObjectTfmProtectedChannels *obtfm,
1599 const short protectflag)
1603 for (i = 0; i < 3; i++) {
1604 if (protectflag & (OB_LOCK_LOCX << i)) {
1605 ob->loc[i] = obtfm->loc[i];
1606 ob->dloc[i] = obtfm->dloc[i];
1609 if (protectflag & (OB_LOCK_SCALEX << i)) {
1610 ob->size[i] = obtfm->size[i];
1611 ob->dscale[i] = obtfm->dscale[i];
1614 if (protectflag & (OB_LOCK_ROTX << i)) {
1615 ob->rot[i] = obtfm->rot[i];
1616 ob->drot[i] = obtfm->drot[i];
1618 ob->quat[i + 1] = obtfm->quat[i + 1];
1619 ob->dquat[i + 1] = obtfm->dquat[i + 1];
1621 ob->rotAxis[i] = obtfm->rotAxis[i];
1622 ob->drotAxis[i] = obtfm->drotAxis[i];
1626 if ((protectflag & OB_LOCK_ROT4D) && (protectflag & OB_LOCK_ROTW)) {
1627 ob->quat[0] = obtfm->quat[0];
1628 ob->dquat[0] = obtfm->dquat[0];
1630 ob->rotAngle = obtfm->rotAngle;
1631 ob->drotAngle = obtfm->drotAngle;
1635 void BKE_object_to_mat3(Object *ob, float mat[3][3]) /* no parent */
1642 BKE_object_scale_to_mat3(ob, smat);
1645 BKE_object_rot_to_mat3(ob, rmat, true);
1646 mul_m3_m3m3(mat, rmat, smat);
1649 void BKE_object_to_mat4(Object *ob, float mat[4][4])
1653 BKE_object_to_mat3(ob, tmat);
1655 copy_m4_m3(mat, tmat);
1657 add_v3_v3v3(mat[3], ob->loc, ob->dloc);
1660 void BKE_object_matrix_local_get(struct Object *ob, float mat[4][4])
1663 float par_imat[4][4];
1665 BKE_object_get_parent_matrix(NULL, ob, ob->parent, par_imat);
1666 invert_m4(par_imat);
1667 mul_m4_m4m4(mat, par_imat, ob->obmat);
1670 copy_m4_m4(mat, ob->obmat);
1675 int enable_cu_speed = 1;
1678 * \param scene: Used when curve cache needs to be calculated, or for dupli-frame time.
1679 * \return success if \a mat is set.
1681 static bool ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[4][4])
1683 Curve *cu = par->data;
1684 float vec[4], dir[3], quat[4], radius, ctime;
1686 /* only happens on reload file, but violates depsgraph still... fix! */
1687 if (par->curve_cache == NULL) {
1688 if (scene == NULL) {
1691 BKE_displist_make_curveTypes(scene, par, 0);
1694 if (par->curve_cache->path == NULL) {
1698 /* catch exceptions: curve paths used as a duplicator */
1699 if (enable_cu_speed) {
1700 /* ctime is now a proper var setting of Curve which gets set by Animato like any other var that's animated,
1701 * but this will only work if it actually is animated...
1703 * we divide the curvetime calculated in the previous step by the length of the path, to get a time
1704 * factor, which then gets clamped to lie within 0.0 - 1.0 range
1707 ctime = cu->ctime / cu->pathlen;
1713 CLAMP(ctime, 0.0f, 1.0f);
1716 /* For dupli-frames only */
1717 if (scene == NULL) {
1721 ctime = BKE_scene_frame_get(scene);
1723 ctime /= cu->pathlen;
1726 CLAMP(ctime, 0.0f, 1.0f);
1732 if (where_on_path(par, ctime, vec, dir, (cu->flag & CU_FOLLOW) ? quat : NULL, &radius, NULL)) {
1734 if (cu->flag & CU_FOLLOW) {
1737 vec_to_quat(quat, dir, ob->trackflag, ob->upflag);
1741 q[0] = cosf(0.5 * vec[3]);
1742 si = sinf(0.5 * vec[3]);
1743 q[1] = -si * dir[0];
1744 q[2] = -si * dir[1];
1745 q[3] = -si * dir[2];
1746 mul_qt_qtqt(quat, q, quat);
1748 quat_apply_track(quat, ob->trackflag, ob->upflag);
1751 quat_to_mat4(mat, quat);
1754 if (cu->flag & CU_PATH_RADIUS) {
1755 float tmat[4][4], rmat[4][4];
1756 scale_m4_fl(tmat, radius);
1757 mul_m4_m4m4(rmat, tmat, mat);
1758 copy_m4_m4(mat, rmat);
1761 copy_v3_v3(mat[3], vec);
1768 static void ob_parbone(Object *ob, Object *par, float mat[4][4])
1770 bPoseChannel *pchan;
1773 if (par->type != OB_ARMATURE) {
1778 /* Make sure the bone is still valid */
1779 pchan = BKE_pose_channel_find_name(par->pose, ob->parsubstr);
1780 if (!pchan || !pchan->bone) {
1781 printf("Object %s with Bone parent: bone %s doesn't exist\n", ob->id.name + 2, ob->parsubstr);
1786 /* get bone transform */
1787 if (pchan->bone->flag & BONE_RELATIVE_PARENTING) {
1788 /* the new option uses the root - expected bahaviour, but differs from old... */
1789 /* XXX check on version patching? */
1790 copy_m4_m4(mat, pchan->chan_mat);
1793 copy_m4_m4(mat, pchan->pose_mat);
1795 /* but for backwards compatibility, the child has to move to the tail */
1796 copy_v3_v3(vec, mat[1]);
1797 mul_v3_fl(vec, pchan->bone->length);
1798 add_v3_v3(mat[3], vec);
1802 static void give_parvert(Object *par, int nr, float vec[3])
1806 if (par->type == OB_MESH) {
1807 Mesh *me = par->data;
1808 BMEditMesh *em = me->edit_btmesh;
1811 dm = (em) ? em->derivedFinal : par->derivedFinal;
1815 int numVerts = dm->getNumVerts(dm);
1817 if (nr < numVerts) {
1818 bool use_special_ss_case = false;
1820 if (dm->type == DM_TYPE_CCGDM) {
1822 VirtualModifierData virtualModifierData;
1823 use_special_ss_case = true;
1824 for (md = modifiers_getVirtualModifierList(par, &virtualModifierData);
1828 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1829 /* TODO(sergey): Check for disabled modifiers. */
1830 if (mti->type != eModifierTypeType_OnlyDeform && md->next != NULL) {
1831 use_special_ss_case = false;
1837 if (!use_special_ss_case) {
1838 /* avoid dm->getVertDataArray() since it allocates arrays in the dm (not thread safe) */
1839 if (em && dm->type == DM_TYPE_EDITBMESH) {
1840 if (em->bm->elem_table_dirty & BM_VERT) {
1841 #ifdef VPARENT_THREADING_HACK
1842 BLI_mutex_lock(&vparent_lock);
1843 if (em->bm->elem_table_dirty & BM_VERT) {
1844 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
1846 BLI_mutex_unlock(&vparent_lock);
1848 BLI_assert(!"Not safe for threading");
1849 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
1855 if (use_special_ss_case) {
1856 /* Special case if the last modifier is SS and no constructive modifier are in front of it. */
1857 CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
1858 CCGVert *ccg_vert = ccgSubSurf_getVert(ccgdm->ss, SET_INT_IN_POINTER(nr));
1859 /* In case we deleted some verts, nr may refer to inexistent one now, see T42557. */
1861 float *co = ccgSubSurf_getVertData(ccgdm->ss, ccg_vert);
1866 else if (CustomData_has_layer(&dm->vertData, CD_ORIGINDEX) &&
1867 !(em && dm->type == DM_TYPE_EDITBMESH))
1871 /* Get the average of all verts with (original index == nr). */
1872 for (i = 0; i < numVerts; i++) {
1873 const int *index = dm->getVertData(dm, i, CD_ORIGINDEX);
1876 dm->getVertCo(dm, i, co);
1883 if (nr < numVerts) {
1885 dm->getVertCo(dm, nr, co);
1893 /* keep as 0, 0, 0 */
1895 else if (count > 0) {
1896 mul_v3_fl(vec, 1.0f / count);
1899 /* use first index if its out of range */
1900 dm->getVertCo(dm, 0, vec);
1905 "%s: DerivedMesh is needed to solve parenting, "
1906 "object position can be wrong now\n", __func__);
1909 else if (ELEM(par->type, OB_CURVE, OB_SURF)) {
1912 /* Unless there's some weird depsgraph failure the cache should exist. */
1913 BLI_assert(par->curve_cache != NULL);
1915 if (par->curve_cache->deformed_nurbs.first != NULL) {
1916 nurb = &par->curve_cache->deformed_nurbs;
1919 Curve *cu = par->data;
1920 nurb = BKE_curve_nurbs_get(cu);
1923 BKE_nurbList_index_get_co(nurb, nr, vec);
1925 else if (par->type == OB_LATTICE) {
1926 Lattice *latt = par->data;
1927 DispList *dl = par->curve_cache ? BKE_displist_find(&par->curve_cache->disp, DL_VERTS) : NULL;
1928 float (*co)[3] = dl ? (float (*)[3])dl->verts : NULL;
1931 if (latt->editlatt) latt = latt->editlatt->latt;
1933 tot = latt->pntsu * latt->pntsv * latt->pntsw;
1935 /* ensure dl is correct size */
1936 BLI_assert(dl == NULL || dl->nr == tot);
1940 copy_v3_v3(vec, co[nr]);
1943 copy_v3_v3(vec, latt->def[nr].vec);
1949 static void ob_parvert3(Object *ob, Object *par, float mat[4][4])
1952 /* in local ob space */
1953 if (OB_TYPE_SUPPORT_PARVERT(par->type)) {
1954 float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1956 give_parvert(par, ob->par1, v1);
1957 give_parvert(par, ob->par2, v2);
1958 give_parvert(par, ob->par3, v3);
1960 tri_to_quat(q, v1, v2, v3);
1961 quat_to_mat3(cmat, q);
1962 copy_m4_m3(mat, cmat);
1964 mid_v3_v3v3v3(mat[3], v1, v2, v3);
1972 void BKE_object_get_parent_matrix(Scene *scene, Object *ob, Object *par, float parentmat[4][4])
1978 switch (ob->partype & PARTYPE) {
1981 if (par->type == OB_CURVE) {
1982 if ((((Curve *)par->data)->flag & CU_PATH) &&
1983 (ob_parcurve(scene, ob, par, tmat)))
1989 if (ok) mul_m4_m4m4(parentmat, par->obmat, tmat);
1990 else copy_m4_m4(parentmat, par->obmat);
1994 ob_parbone(ob, par, tmat);
1995 mul_m4_m4m4(parentmat, par->obmat, tmat);
2000 give_parvert(par, ob->par1, vec);
2001 mul_v3_m4v3(parentmat[3], par->obmat, vec);
2004 ob_parvert3(ob, par, tmat);
2006 mul_m4_m4m4(parentmat, par->obmat, tmat);
2010 copy_m4_m4(parentmat, par->obmat);
2017 * \param r_originmat Optional matrix that stores the space the object is in (without its own matrix applied)
2019 static void solve_parenting(Scene *scene, Object *ob, Object *par, float obmat[4][4], float slowmat[4][4],
2020 float r_originmat[3][3], const bool set_origin)
2026 BKE_object_to_mat4(ob, locmat);
2028 if (ob->partype & PARSLOW) copy_m4_m4(slowmat, obmat);
2030 BKE_object_get_parent_matrix(scene, ob, par, totmat);
2033 mul_m4_m4m4(tmat, totmat, ob->parentinv);
2034 mul_m4_m4m4(obmat, tmat, locmat);
2037 /* usable originmat */
2038 copy_m3_m4(r_originmat, tmat);
2041 /* origin, for help line */
2043 if ((ob->partype & PARTYPE) == PARSKEL) {
2044 copy_v3_v3(ob->orig, par->obmat[3]);
2047 copy_v3_v3(ob->orig, totmat[3]);
2052 static bool where_is_object_parslow(Object *ob, float obmat[4][4], float slowmat[4][4])
2058 /* include framerate */
2059 fac1 = (1.0f / (1.0f + fabsf(ob->sf)));
2060 if (fac1 >= 1.0f) return false;
2065 for (a = 0; a < 16; a++, fp1++, fp2++) {
2066 fp1[0] = fac1 * fp1[0] + fac2 * fp2[0];
2072 /* note, scene is the active scene while actual_scene is the scene the object resides in */
2073 void BKE_object_where_is_calc_time_ex(Scene *scene, Object *ob, float ctime,
2074 RigidBodyWorld *rbw, float r_originmat[3][3])
2076 if (ob == NULL) return;
2078 /* execute drivers only, as animation has already been done */
2079 BKE_animsys_evaluate_animdata(scene, &ob->id, ob->adt, ctime, ADT_RECALC_DRIVERS);
2082 Object *par = ob->parent;
2083 float slowmat[4][4];
2085 /* calculate parent matrix */
2086 solve_parenting(scene, ob, par, ob->obmat, slowmat, r_originmat, true);
2088 /* "slow parent" is definitely not threadsafe, and may also give bad results jumping around
2089 * An old-fashioned hack which probably doesn't really cut it anymore
2091 if (ob->partype & PARSLOW) {
2092 if (!where_is_object_parslow(ob, ob->obmat, slowmat))
2097 BKE_object_to_mat4(ob, ob->obmat);
2100 /* try to fall back to the scene rigid body world if none given */
2101 rbw = rbw ? rbw : scene->rigidbody_world;
2102 /* read values pushed into RBO from sim/cache... */
2103 BKE_rigidbody_sync_transforms(rbw, ob, ctime);
2105 /* solve constraints */
2106 if (ob->constraints.first && !(ob->transflag & OB_NO_CONSTRAINTS)) {
2108 cob = BKE_constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2109 BKE_constraints_solve(&ob->constraints, cob, ctime);
2110 BKE_constraints_clear_evalob(cob);
2113 /* set negative scale flag in object */
2114 if (is_negative_m4(ob->obmat)) ob->transflag |= OB_NEG_SCALE;
2115 else ob->transflag &= ~OB_NEG_SCALE;
2118 void BKE_object_where_is_calc_time(Scene *scene, Object *ob, float ctime)
2120 BKE_object_where_is_calc_time_ex(scene, ob, ctime, NULL, NULL);
2123 /* get object transformation matrix without recalculating dependencies and
2124 * constraints -- assume dependencies are already solved by depsgraph.
2125 * no changes to object and it's parent would be done.
2126 * used for bundles orientation in 3d space relative to parented blender camera */
2127 void BKE_object_where_is_calc_mat4(Scene *scene, Object *ob, float obmat[4][4])
2131 float slowmat[4][4];
2133 Object *par = ob->parent;
2135 solve_parenting(scene, ob, par, obmat, slowmat, NULL, false);
2137 if (ob->partype & PARSLOW)
2138 where_is_object_parslow(ob, obmat, slowmat);
2141 BKE_object_to_mat4(ob, obmat);
2145 void BKE_object_where_is_calc_ex(Scene *scene, RigidBodyWorld *rbw, Object *ob, float r_originmat[3][3])
2147 BKE_object_where_is_calc_time_ex(scene, ob, BKE_scene_frame_get(scene), rbw, r_originmat);
2149 void BKE_object_where_is_calc(Scene *scene, Object *ob)
2151 BKE_object_where_is_calc_time_ex(scene, ob, BKE_scene_frame_get(scene), NULL, NULL);
2154 /* for calculation of the inverse parent transform, only used for editor */
2155 void BKE_object_workob_calc_parent(Scene *scene, Object *ob, Object *workob)
2157 BKE_object_workob_clear(workob);
2159 unit_m4(workob->obmat);
2160 unit_m4(workob->parentinv);
2161 unit_m4(workob->constinv);
2162 workob->parent = ob->parent;
2164 workob->trackflag = ob->trackflag;
2165 workob->upflag = ob->upflag;
2167 workob->partype = ob->partype;
2168 workob->par1 = ob->par1;
2169 workob->par2 = ob->par2;
2170 workob->par3 = ob->par3;
2172 workob->constraints.first = ob->constraints.first;
2173 workob->constraints.last = ob->constraints.last;
2175 BLI_strncpy(workob->parsubstr, ob->parsubstr, sizeof(workob->parsubstr));
2177 BKE_object_where_is_calc(scene, workob);
2180 /* see BKE_pchan_apply_mat4() for the equivalent 'pchan' function */
2181 void BKE_object_apply_mat4(Object *ob, float mat[4][4], const bool use_compat, const bool use_parent)
2185 if (use_parent && ob->parent) {
2186 float rmat[4][4], diff_mat[4][4], imat[4][4], parent_mat[4][4];
2188 BKE_object_get_parent_matrix(NULL, ob, ob->parent, parent_mat);
2190 mul_m4_m4m4(diff_mat, parent_mat, ob->parentinv);
2191 invert_m4_m4(imat, diff_mat);
2192 mul_m4_m4m4(rmat, imat, mat); /* get the parent relative matrix */
2194 /* same as below, use rmat rather than mat */
2195 mat4_to_loc_rot_size(ob->loc, rot, ob->size, rmat);
2198 mat4_to_loc_rot_size(ob->loc, rot, ob->size, mat);
2201 BKE_object_mat3_to_rot(ob, rot, use_compat);
2203 sub_v3_v3(ob->loc, ob->dloc);
2205 if (ob->dscale[0] != 0.0f) ob->size[0] /= ob->dscale[0];
2206 if (ob->dscale[1] != 0.0f) ob->size[1] /= ob->dscale[1];
2207 if (ob->dscale[2] != 0.0f) ob->size[2] /= ob->dscale[2];
2209 /* BKE_object_mat3_to_rot handles delta rotations */
2212 BoundBox *BKE_boundbox_alloc_unit(void)
2215 const float min[3] = {-1.0f, -1.0f, -1.0f}, max[3] = {-1.0f, -1.0f, -1.0f};
2217 bb = MEM_callocN(sizeof(BoundBox), "OB-BoundBox");
2218 BKE_boundbox_init_from_minmax(bb, min, max);
2223 void BKE_boundbox_init_from_minmax(BoundBox *bb, const float min[3], const float max[3])
2225 bb->vec[0][0] = bb->vec[1][0] = bb->vec[2][0] = bb->vec[3][0] = min[0];
2226 bb->vec[4][0] = bb->vec[5][0] = bb->vec[6][0] = bb->vec[7][0] = max[0];
2228 bb->vec[0][1] = bb->vec[1][1] = bb->vec[4][1] = bb->vec[5][1] = min[1];
2229 bb->vec[2][1] = bb->vec[3][1] = bb->vec[6][1] = bb->vec[7][1] = max[1];
2231 bb->vec[0][2] = bb->vec[3][2] = bb->vec[4][2] = bb->vec[7][2] = min[2];
2232 bb->vec[1][2] = bb->vec[2][2] = bb->vec[5][2] = bb->vec[6][2] = max[2];
2235 void BKE_boundbox_calc_center_aabb(const BoundBox *bb, float r_cent[3])
2237 r_cent[0] = 0.5f * (bb->vec[0][0] + bb->vec[4][0]);
2238 r_cent[1] = 0.5f * (bb->vec[0][1] + bb->vec[2][1]);
2239 r_cent[2] = 0.5f * (bb->vec[0][2] + bb->vec[1][2]);
2242 void BKE_boundbox_calc_size_aabb(const BoundBox *bb, float r_size[3])
2244 r_size[0] = 0.5f * fabsf(bb->vec[0][0] - bb->vec[4][0]);
2245 r_size[1] = 0.5f * fabsf(bb->vec[0][1] - bb->vec[2][1]);
2246 r_size[2] = 0.5f * fabsf(bb->vec[0][2] - bb->vec[1][2]);
2249 void BKE_boundbox_minmax(const BoundBox *bb, float obmat[4][4], float r_min[3], float r_max[3])
2252 for (i = 0; i < 8; i++) {
2254 mul_v3_m4v3(vec, obmat, bb->vec[i]);
2255 minmax_v3v3_v3(r_min, r_max, vec);
2259 BoundBox *BKE_object_boundbox_get(Object *ob)
2261 BoundBox *bb = NULL;
2263 if (ob->type == OB_MESH) {
2264 bb = BKE_mesh_boundbox_get(ob);
2266 else if (ELEM(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2267 bb = BKE_curve_boundbox_get(ob);
2269 else if (ob->type == OB_MBALL) {
2272 else if (ob->type == OB_LATTICE) {
2273 bb = BKE_lattice_boundbox_get(ob);
2275 else if (ob->type == OB_ARMATURE) {
2276 bb = BKE_armature_boundbox_get(ob);
2281 /* used to temporally disable/enable boundbox */
2282 void BKE_object_boundbox_flag(Object *ob, int flag, const bool set)
2284 BoundBox *bb = BKE_object_boundbox_get(ob);
2286 if (set) bb->flag |= flag;
2287 else bb->flag &= ~flag;
2291 void BKE_object_dimensions_get(Object *ob, float vec[3])
2293 BoundBox *bb = NULL;
2295 bb = BKE_object_boundbox_get(ob);
2299 mat4_to_size(scale, ob->obmat);
2301 vec[0] = fabsf(scale[0]) * (bb->vec[4][0] - bb->vec[0][0]);
2302 vec[1] = fabsf(scale[1]) * (bb->vec[2][1] - bb->vec[0][1]);
2303 vec[2] = fabsf(scale[2]) * (bb->vec[1][2] - bb->vec[0][2]);
2310 void BKE_object_dimensions_set(Object *ob, const float value[3])
2312 BoundBox *bb = NULL;
2314 bb = BKE_object_boundbox_get(ob);
2316 float scale[3], len[3];
2318 mat4_to_size(scale, ob->obmat);
2320 len[0] = bb->vec[4][0] - bb->vec[0][0];
2321 len[1] = bb->vec[2][1] - bb->vec[0][1];
2322 len[2] = bb->vec[1][2] - bb->vec[0][2];
2324 if (len[0] > 0.f) ob->size[0] = value[0] / len[0];
2325 if (len[1] > 0.f) ob->size[1] = value[1] / len[1];
2326 if (len[2] > 0.f) ob->size[2] = value[2] / len[2];
2330 void BKE_object_minmax(Object *ob, float min_r[3], float max_r[3], const bool use_hidden)
2334 bool changed = false;
2341 bb = *BKE_curve_boundbox_get(ob);
2342 BKE_boundbox_minmax(&bb, ob->obmat, min_r, max_r);
2348 Lattice *lt = ob->data;
2349 BPoint *bp = lt->def;
2352 for (w = 0; w < lt->pntsw; w++) {
2353 for (v = 0; v < lt->pntsv; v++) {
2354 for (u = 0; u < lt->pntsu; u++, bp++) {
2355 mul_v3_m4v3(vec, ob->obmat, bp->vec);
2356 minmax_v3v3_v3(min_r, max_r, vec);
2365 changed = BKE_pose_minmax(ob, min_r, max_r, use_hidden, false);
2370 Mesh *me = BKE_mesh_from_object(ob);
2373 bb = *BKE_mesh_boundbox_get(ob);
2374 BKE_boundbox_minmax(&bb, ob->obmat, min_r, max_r);
2381 float ob_min[3], ob_max[3];
2383 changed = BKE_mball_minmax_ex(ob->data, ob_min, ob_max, ob->obmat, 0);
2385 minmax_v3v3_v3(min_r, max_r, ob_min);
2386 minmax_v3v3_v3(min_r, max_r, ob_max);
2392 if (changed == false) {
2395 copy_v3_v3(size, ob->size);
2396 if (ob->type == OB_EMPTY) {
2397 mul_v3_fl(size, ob->empty_drawsize);
2400 minmax_v3v3_v3(min_r, max_r, ob->obmat[3]);
2402 copy_v3_v3(vec, ob->obmat[3]);
2403 add_v3_v3(vec, size);
2404 minmax_v3v3_v3(min_r, max_r, vec);
2406 copy_v3_v3(vec, ob->obmat[3]);
2407 sub_v3_v3(vec, size);
2408 minmax_v3v3_v3(min_r, max_r, vec);
2412 void BKE_object_empty_draw_type_set(Object *ob, const int value)
2414 ob->empty_drawtype = value;
2416 if (ob->type == OB_EMPTY && ob->empty_drawtype == OB_EMPTY_IMAGE) {
2418 ob->iuser = MEM_callocN(sizeof(ImageUser), "image user");
2420 ob->iuser->frames = 100;
2421 ob->iuser->sfra = 1;
2422 ob->iuser->fie_ima = 2;
2427 MEM_freeN(ob->iuser);
2433 bool BKE_object_minmax_dupli(
2434 Main *bmain, Scene *scene,
2435 Object *ob, float r_min[3], float r_max[3], const bool use_hidden)
2438 if ((ob->transflag & OB_DUPLI) == 0) {
2444 lb = object_duplilist(bmain, bmain->eval_ctx, scene, ob);
2445 for (dob = lb->first; dob; dob = dob->next) {
2446 if ((use_hidden == false) && (dob->no_draw != 0)) {
2450 BoundBox *bb = BKE_object_boundbox_get(dob->ob);
2454 for (i = 0; i < 8; i++) {
2456 mul_v3_m4v3(vec, dob->mat, bb->vec[i]);
2457 minmax_v3v3_v3(r_min, r_max, vec);
2464 free_object_duplilist(lb); /* does restore */
2470 void BKE_object_foreach_display_point(
2471 Object *ob, float obmat[4][4],
2472 void (*func_cb)(const float[3], void *), void *user_data)
2476 if (ob->derivedFinal) {
2477 DerivedMesh *dm = ob->derivedFinal;
2478 MVert *mv = dm->getVertArray(dm);
2479 int totvert = dm->getNumVerts(dm);
2482 for (i = 0; i < totvert; i++, mv++) {
2483 mul_v3_m4v3(co, obmat, mv->co);
2484 func_cb(co, user_data);
2487 else if (ob->curve_cache && ob->curve_cache->disp.first) {
2490 for (dl = ob->curve_cache->disp.first; dl; dl = dl->next) {
2491 const float *v3 = dl->verts;
2492 int totvert = dl->nr;
2495 for (i = 0; i < totvert; i++, v3 += 3) {
2496 mul_v3_m4v3(co, obmat, v3);
2497 func_cb(co, user_data);
2503 void BKE_scene_foreach_display_point(
2504 Main *bmain, Scene *scene, View3D *v3d, const short flag,
2505 void (*func_cb)(const float[3], void *), void *user_data)
2510 for (base = FIRSTBASE; base; base = base->next) {
2511 if (BASE_VISIBLE_BGMODE(v3d, scene, base) && (base->flag & flag) == flag) {
2514 if ((ob->transflag & OB_DUPLI) == 0) {
2515 BKE_object_foreach_display_point(ob, ob->obmat, func_cb, user_data);
2521 lb = object_duplilist(bmain, bmain->eval_ctx, scene, ob);
2522 for (dob = lb->first; dob; dob = dob->next) {
2523 if (dob->no_draw == 0) {
2524 BKE_object_foreach_display_point(dob->ob, dob->mat, func_cb, user_data);
2527 free_object_duplilist(lb); /* does restore */
2533 /* copied from DNA_object_types.h */
2534 typedef struct ObTfmBack {
2535 float loc[3], dloc[3], orig[3];
2536 float size[3], dscale[3]; /* scale and delta scale */
2537 float rot[3], drot[3]; /* euler rotation */
2538 float quat[4], dquat[4]; /* quaternion rotation */
2539 float rotAxis[3], drotAxis[3]; /* axis angle rotation - axis part */
2540 float rotAngle, drotAngle; /* axis angle rotation - angle part */
2541 float obmat[4][4]; /* final worldspace matrix with constraints & animsys applied */
2542 float parentinv[4][4]; /* inverse result of parent, so that object doesn't 'stick' to parent */
2543 float constinv[4][4]; /* inverse result of constraints. doesn't include effect of parent or object local transform */
2544 float imat[4][4]; /* inverse matrix of 'obmat' for during render, old game engine, temporally: ipokeys of transform */
2547 void *BKE_object_tfm_backup(Object *ob)
2549 ObTfmBack *obtfm = MEM_mallocN(sizeof(ObTfmBack), "ObTfmBack");
2550 copy_v3_v3(obtfm->loc, ob->loc);
2551 copy_v3_v3(obtfm->dloc, ob->dloc);
2552 copy_v3_v3(obtfm->orig, ob->orig);
2553 copy_v3_v3(obtfm->size, ob->size);
2554 copy_v3_v3(obtfm->dscale, ob->dscale);
2555 copy_v3_v3(obtfm->rot, ob->rot);
2556 copy_v3_v3(obtfm->drot, ob->drot);
2557 copy_qt_qt(obtfm->quat, ob->quat);
2558 copy_qt_qt(obtfm->dquat, ob->dquat);
2559 copy_v3_v3(obtfm->rotAxis, ob->rotAxis);
2560 copy_v3_v3(obtfm->drotAxis, ob->drotAxis);
2561 obtfm->rotAngle = ob->rotAngle;
2562 obtfm->drotAngle = ob->drotAngle;
2563 copy_m4_m4(obtfm->obmat, ob->obmat);
2564 copy_m4_m4(obtfm->parentinv, ob->parentinv);
2565 copy_m4_m4(obtfm->constinv, ob->constinv);
2566 copy_m4_m4(obtfm->imat, ob->imat);
2568 return (void *)obtfm;
2571 void BKE_object_tfm_restore(Object *ob, void *obtfm_pt)
2573 ObTfmBack *obtfm = (ObTfmBack *)obtfm_pt;
2574 copy_v3_v3(ob->loc, obtfm->loc);
2575 copy_v3_v3(ob->dloc, obtfm->dloc);
2576 copy_v3_v3(ob->orig, obtfm->orig);
2577 copy_v3_v3(ob->size, obtfm->size);
2578 copy_v3_v3(ob->dscale, obtfm->dscale);
2579 copy_v3_v3(ob->rot, obtfm->rot);
2580 copy_v3_v3(ob->drot, obtfm->drot);
2581 copy_qt_qt(ob->quat, obtfm->quat);
2582 copy_qt_qt(ob->dquat, obtfm->dquat);
2583 copy_v3_v3(ob->rotAxis, obtfm->rotAxis);
2584 copy_v3_v3(ob->drotAxis, obtfm->drotAxis);
2585 ob->rotAngle = obtfm->rotAngle;
2586 ob->drotAngle = obtfm->drotAngle;
2587 copy_m4_m4(ob->obmat, obtfm->obmat);
2588 copy_m4_m4(ob->parentinv, obtfm->parentinv);
2589 copy_m4_m4(ob->constinv, obtfm->constinv);
2590 copy_m4_m4(ob->imat, obtfm->imat);
2593 bool BKE_object_parent_loop_check(const Object *par, const Object *ob)
2595 /* test if 'ob' is a parent somewhere in par's parents */
2596 if (par == NULL) return false;
2597 if (ob == par) return true;
2598 return BKE_object_parent_loop_check(par->parent, ob);
2601 static void object_handle_update_proxy(Main *bmain,
2602 EvaluationContext *eval_ctx,
2605 const bool do_proxy_update)
2607 /* The case when this is a group proxy, object_update is called in group.c */
2608 if (object->proxy == NULL) {
2611 /* set pointer in library proxy target, for copying, but restore it */
2612 object->proxy->proxy_from = object;
2613 // printf("set proxy pointer for later group stuff %s\n", ob->id.name);
2615 /* the no-group proxy case, we call update */
2616 if (object->proxy_group == NULL) {
2617 if (do_proxy_update) {
2618 // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
2619 BKE_object_handle_update(bmain, eval_ctx, scene, object->proxy);
2624 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
2625 /* local_object->proxy == pointer to library object, saved in files and read */
2627 /* function below is polluted with proxy exceptions, cleanup will follow! */
2629 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
2630 /* requires flags to be set! */
2631 /* Ideally we shouldn't have to pass the rigid body world, but need bigger restructuring to avoid id */
2632 void BKE_object_handle_update_ex(Main *bmain,
2633 EvaluationContext *eval_ctx,
2634 Scene *scene, Object *ob,
2635 RigidBodyWorld *rbw,
2636 const bool do_proxy_update)
2638 if ((ob->recalc & OB_RECALC_ALL) == 0) {
2639 object_handle_update_proxy(bmain, eval_ctx, scene, ob, do_proxy_update);
2642 /* Speed optimization for animation lookups. */
2643 if (ob->pose != NULL) {
2644 BKE_pose_channels_hash_make(ob->pose);
2645 if (ob->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
2646 BKE_pose_update_constraint_flags(ob->pose);
2649 if (ob->recalc & OB_RECALC_DATA) {
2650 if (ob->type == OB_ARMATURE) {
2651 /* this happens for reading old files and to match library armatures
2652 * with poses we do it ahead of BKE_object_where_is_calc to ensure animation
2653 * is evaluated on the rebuilt pose, otherwise we get incorrect poses
2655 if (ob->pose == NULL || (ob->pose->flag & POSE_RECALC))
2656 BKE_pose_rebuild(ob, ob->data);
2659 /* XXX new animsys warning: depsgraph tag OB_RECALC_DATA should not skip drivers,
2660 * which is only in BKE_object_where_is_calc now */
2661 /* XXX: should this case be OB_RECALC_OB instead? */
2662 if (ob->recalc & OB_RECALC_ALL) {
2663 if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) {
2664 printf("recalcob %s\n", ob->id.name + 2);
2666 /* Handle proxy copy for target. */
2667 if (!BKE_object_eval_proxy_copy(eval_ctx, ob)) {
2668 BKE_object_where_is_calc_ex(scene, rbw, ob, NULL);
2672 if (ob->recalc & OB_RECALC_DATA) {
2673 BKE_object_handle_data_update(bmain, eval_ctx, scene, ob);
2676 ob->recalc &= ~OB_RECALC_ALL;
2678 object_handle_update_proxy(bmain, eval_ctx, scene, ob, do_proxy_update);
2681 /* WARNING: "scene" here may not be the scene object actually resides in.
2682 * When dealing with background-sets, "scene" is actually the active scene.
2683 * e.g. "scene" <-- set 1 <-- set 2 ("ob" lives here) <-- set 3 <-- ... <-- set n
2684 * rigid bodies depend on their world so use BKE_object_handle_update_ex() to also pass along the corrent rigid body world
2686 void BKE_object_handle_update(Main *bmain, EvaluationContext *eval_ctx, Scene *scene, Object *ob)
2688 BKE_object_handle_update_ex(bmain, eval_ctx, scene, ob, NULL, true);
2691 void BKE_object_sculpt_modifiers_changed(Object *ob)
2693 SculptSession *ss = ob->sculpt;
2695 if (ss && ss->building_vp_handle == false) {
2697 /* we free pbvh on changes, except during sculpt since it can't deal with
2698 * changing PVBH node organization, we hope topology does not change in
2699 * the meantime .. weak */
2701 BKE_pbvh_free(ss->pbvh);
2705 BKE_sculptsession_free_deformMats(ob->sculpt);
2707 /* In vertex/weight paint, force maps to be rebuilt. */
2708 BKE_sculptsession_free_vwpaint_data(ob->sculpt);
2714 BKE_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
2716 for (n = 0; n < totnode; n++)
2717 BKE_pbvh_node_mark_update(nodes[n]);
2724 int BKE_object_obdata_texspace_get(Object *ob, short **r_texflag, float **r_loc, float **r_size, float **r_rot)
2727 if (ob->data == NULL)
2730 switch (GS(((ID *)ob->data)->name)) {
2733 Mesh *me = ob->data;
2734 if (me->bb == NULL || (me->bb->flag & BOUNDBOX_DIRTY)) {
2735 BKE_mesh_texspace_calc(me);
2737 if (r_texflag) *r_texflag = &me->texflag;
2738 if (r_loc) *r_loc = me->loc;
2739 if (r_size) *r_size = me->size;
2740 if (r_rot) *r_rot = me->rot;
2745 Curve *cu = ob->data;
2746 if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_DIRTY)) {
2747 BKE_curve_texspace_calc(cu);
2749 if (r_texflag) *r_texflag = &cu->texflag;
2750 if (r_loc) *r_loc = cu->loc;
2751 if (r_size) *r_size = cu->size;
2752 if (r_rot) *r_rot = cu->rot;
2757 MetaBall *mb = ob->data;
2758 if (r_texflag) *r_texflag = &mb->texflag;
2759 if (r_loc) *r_loc = mb->loc;
2760 if (r_size) *r_size = mb->size;
2761 if (r_rot) *r_rot = mb->rot;
2770 static int pc_cmp(const void *a, const void *b)
2772 const LinkData *ad = a, *bd = b;
2773 if (GET_INT_FROM_POINTER(ad->data) > GET_INT_FROM_POINTER(bd->data))
2778 int BKE_object_insert_ptcache(Object *ob)
2780 LinkData *link = NULL;
2783 BLI_listbase_sort(&ob->pc_ids, pc_cmp);
2785 for (link = ob->pc_ids.first, i = 0; link; link = link->next, i++) {
2786 int index = GET_INT_FROM_POINTER(link->data);
2792 link = MEM_callocN(sizeof(LinkData), "PCLink");
2793 link->data = SET_INT_IN_POINTER(i);
2794 BLI_addtail(&ob->pc_ids, link);
2799 static int pc_findindex(ListBase *listbase, int index)
2801 LinkData *link = NULL;
2804 if (listbase == NULL) return -1;
2806 link = listbase->first;
2808 if (GET_INT_FROM_POINTER(link->data) == index)
2818 void BKE_object_delete_ptcache(Object *ob, int index)
2820 int list_index = pc_findindex(&ob->pc_ids, index);
2821 LinkData *link = BLI_findlink(&ob->pc_ids, list_index);
2822 BLI_freelinkN(&ob->pc_ids, link);
2825 /* shape key utility function */
2827 /************************* Mesh ************************/
2828 static KeyBlock *insert_meshkey(Main *bmain, Object *ob, const char *name, const bool from_mix)
2830 Mesh *me = ob->data;
2836 key = me->key = BKE_key_add(bmain, (ID *)me);
2837 key->type = KEY_RELATIVE;
2841 if (newkey || from_mix == false) {
2842 /* create from mesh */
2843 kb = BKE_keyblock_add_ctime(key, name, false);
2844 BKE_keyblock_convert_from_mesh(me, kb);
2847 /* copy from current values */
2849 float *data = BKE_key_evaluate_object(ob, &totelem);
2851 /* create new block with prepared data */
2852 kb = BKE_keyblock_add_ctime(key, name, false);
2854 kb->totelem = totelem;
2859 /************************* Lattice ************************/
2860 static KeyBlock *insert_lattkey(Main *bmain, Object *ob, const char *name, const bool from_mix)
2862 Lattice *lt = ob->data;
2868 key = lt->key = BKE_key_add(bmain, (ID *)lt);
2869 key->type = KEY_RELATIVE;
2873 if (newkey || from_mix == false) {
2874 kb = BKE_keyblock_add_ctime(key, name, false);
2876 KeyBlock *basekb = (KeyBlock *)key->block.first;
2877 kb->data = MEM_dupallocN(basekb->data);
2878 kb->totelem = basekb->totelem;
2881 BKE_keyblock_convert_from_lattice(lt, kb);
2885 /* copy from current values */
2887 float *data = BKE_key_evaluate_object(ob, &totelem);
2889 /* create new block with prepared data */
2890 kb = BKE_keyblock_add_ctime(key, name, false);
2891 kb->totelem = totelem;
2897 /************************* Curve ************************/
2898 static KeyBlock *insert_curvekey(Main *bmain, Object *ob, const char *name, const bool from_mix)
2900 Curve *cu = ob->data;
2903 ListBase *lb = BKE_curve_nurbs_get(cu);
2907 key = cu->key = BKE_key_add(bmain, (ID *)cu);
2908 key->type = KEY_RELATIVE;
2912 if (newkey || from_mix == false) {
2913 /* create from curve */
2914 kb = BKE_keyblock_add_ctime(key, name, false);
2916 KeyBlock *basekb = (KeyBlock *)key->block.first;
2917 kb->data = MEM_dupallocN(basekb->data);
2918 kb->totelem = basekb->totelem;
2921 BKE_keyblock_convert_from_curve(cu, kb, lb);
2925 /* copy from current values */
2927 float *data = BKE_key_evaluate_object(ob, &totelem);
2929 /* create new block with prepared data */
2930 kb = BKE_keyblock_add_ctime(key, name, false);
2931 kb->totelem = totelem;
2938 KeyBlock *BKE_object_shapekey_insert(Main *bmain, Object *ob, const char *name, const bool from_mix)
2942 return insert_meshkey(bmain, ob, name, from_mix);
2945 return insert_curvekey(bmain, ob, name, from_mix);
2947 return insert_lattkey(bmain, ob, name, from_mix);
2954 bool BKE_object_shapekey_free(Main *bmain, Object *ob)
2958 key_p = BKE_key_from_object_p(ob);
2959 if (ELEM(NULL, key_p, *key_p)) {
2966 BKE_libblock_free_us(bmain, key);
2971 bool BKE_object_shapekey_remove(Main *bmain, Object *ob, KeyBlock *kb)
2974 Key *key = BKE_key_from_object(ob);
2981 kb_index = BLI_findindex(&key->block, kb);
2982 BLI_assert(kb_index != -1);
2984 for (rkb = key->block.first; rkb; rkb = rkb->next) {
2985 if (rkb->relative == kb_index) {
2986 /* remap to the 'Basis' */
2989 else if (rkb->relative >= kb_index) {
2990 /* Fix positional shift of the keys when kb is deleted from the list */
2995 BLI_remlink(&key->block, kb);
2997 if (key->refkey == kb) {
2998 key->refkey = key->block.first;
3001 /* apply new basis key on original data */
3004 BKE_keyblock_convert_to_mesh(key->refkey, ob->data);
3008 BKE_keyblock_convert_to_curve(key->refkey, ob->data, BKE_curve_nurbs_get(ob->data));
3011 BKE_keyblock_convert_to_lattice(key->refkey, ob->data);
3018 MEM_freeN(kb->data);
3022 if (ob->shapenr > 1) {
3026 if (key->totkey == 0) {
3027 BKE_object_shapekey_free(bmain, ob);
3033 bool BKE_object_flag_test_recursive(const Object *ob, short flag)
3035 if (ob->flag & flag) {
3038 else if (ob->parent) {
3039 return BKE_object_flag_test_recursive(ob->parent, flag);
3046 bool BKE_object_is_child_recursive(const Object *ob_parent, const Object *ob_child)
3048 for (ob_child = ob_child->parent; ob_child; ob_child = ob_child->parent) {
3049 if (ob_child == ob_parent) {
3056 /* most important if this is modified it should _always_ return True, in certain
3057 * cases false positives are hard to avoid (shape keys for example) */
3058 int BKE_object_is_modified(Scene *scene, Object *ob)
3062 if (BKE_key_from_object(ob)) {
3063 flag |= eModifierMode_Render | eModifierMode_Realtime;
3067 VirtualModifierData virtualModifierData;
3069 for (md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
3070 md && (flag != (eModifierMode_Render | eModifierMode_Realtime));
3073 if ((flag & eModifierMode_Render) == 0 && modifier_isEnabled(scene, md, eModifierMode_Render))
3074 flag |= eModifierMode_Render;
3076 if ((flag & eModifierMode_Realtime) == 0 && modifier_isEnabled(scene, md, eModifierMode_Realtime))
3077 flag |= eModifierMode_Realtime;
3084 /* Check of objects moves in time. */
3085 /* NOTE: This function is currently optimized for usage in combination
3086 * with mti->canDeform, so modifiers can quickly check if their target
3087 * objects moves (causing deformation motion blur) or not.
3089 * This makes it possible to give some degree of false-positives here,
3090 * but it's currently an acceptable tradeoff between complexity and check
3091 * speed. In combination with checks of modifier stack and real life usage
3092 * percentage of false-positives shouldn't be that hight.
3094 static bool object_moves_in_time(Object *object)
3096 AnimData *adt = object->adt;
3098 /* If object has any sort of animation data assume it is moving. */
3099 if (adt->action != NULL ||
3100 !BLI_listbase_is_empty(&adt->nla_tracks) ||
3101 !BLI_listbase_is_empty(&adt->drivers) ||
3102 !BLI_listbase_is_empty(&adt->overrides))
3107 if (!BLI_listbase_is_empty(&object->constraints)) {
3110 if (object->parent != NULL) {
3111 /* TODO(sergey): Do recursive check here? */
3117 static bool object_deforms_in_time(Object *object)
3119 if (BKE_key_from_object(object) != NULL) {
3122 if (!BLI_listbase_is_empty(&object->modifiers)) {
3125 return object_moves_in_time(object);
3128 static bool constructive_modifier_is_deform_modified(ModifierData *md)
3130 /* TODO(sergey): Consider generalizing this a bit so all modifier logic
3131 * is concentrated in MOD_{modifier}.c file,
3133 if (md->type == eModifierType_Array) {
3134 ArrayModifierData *amd = (ArrayModifierData *)md;
3135 /* TODO(sergey): Check if curve is deformed. */
3136 return (amd->start_cap != NULL && object_moves_in_time(amd->start_cap)) ||
3137 (amd->end_cap != NULL && object_moves_in_time(amd->end_cap)) ||
3138 (amd->curve_ob != NULL && object_moves_in_time(amd->curve_ob)) ||
3139 (amd->offset_ob != NULL && object_moves_in_time(amd->offset_ob));
3141 else if (md->type == eModifierType_Mirror) {
3142 MirrorModifierData *mmd = (MirrorModifierData *)md;
3143 return mmd->mirror_ob != NULL && object_moves_in_time(mmd->mirror_ob);
3145 else if (md->type == eModifierType_Screw) {
3146 ScrewModifierData *smd = (ScrewModifierData *)md;
3147 return smd->ob_axis != NULL && object_moves_in_time(smd->ob_axis);
3149 else if (md->type == eModifierType_MeshSequenceCache) {
3150 /* NOTE: Not ideal because it's unknown whether topology changes or not.
3151 * This will be detected later, so by assuming it's only deformation
3152 * going on here we allow to bake deform-only mesh to Alembic and have
3153 * proper motion blur after that.
3160 static bool modifiers_has_animation_check(Object *ob)
3162 /* TODO(sergey): This is a bit code duplication with depsgraph, but
3163 * would be nicer to solve this as a part of new dependency graph
3164 * work, so we avoid conflicts and so.
3166 if (ob->adt != NULL) {
3167 AnimData *adt = ob->adt;
3169 if (adt->action != NULL) {
3170 for (fcu = adt->action->curves.first; fcu; fcu = fcu->next) {
3171 if (fcu->rna_path && strstr(fcu->rna_path, "modifiers[")) {
3176 for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
3177 if (fcu->rna_path && strstr(fcu->rna_path, "modifiers[")) {
3185 /* test if object is affected by deforming modifiers (for motion blur). again
3186 * most important is to avoid false positives, this is to skip computations
3187 * and we can still if there was actual deformation afterwards */
3188 int BKE_object_is_deform_modified(Scene *scene, Object *ob)
3191 VirtualModifierData virtualModifierData;
3193 const bool is_modifier_animated = modifiers_has_animation_check(ob);
3195 if (BKE_key_from_object(ob)) {
3196 flag |= eModifierMode_Realtime | eModifierMode_Render;
3199 if (ob->type == OB_CURVE) {
3200 Curve *cu = (Curve *)ob->data;
3201 if (cu->taperobj != NULL && object_deforms_in_time(cu->taperobj)) {
3202 flag |= eModifierMode_Realtime | eModifierMode_Render;
3207 for (md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
3208 md && (flag != (eModifierMode_Render | eModifierMode_Realtime));
3211 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
3212 bool can_deform = mti->type == eModifierTypeType_OnlyDeform ||
3213 is_modifier_animated;
3216 can_deform = constructive_modifier_is_deform_modified(md);
3220 if (!(flag & eModifierMode_Render) && modifier_isEnabled(scene, md, eModifierMode_Render))
3221 flag |= eModifierMode_Render;
3223 if (!(flag & eModifierMode_Realtime) && modifier_isEnabled(scene, md, eModifierMode_Realtime))
3224 flag |= eModifierMode_Realtime;
3231 /* See if an object is using an animated modifier */
3232 bool BKE_object_is_animated(Scene *scene, Object *ob)
3235 VirtualModifierData virtualModifierData;
3237 for (md = modifiers_getVirtualModifierList(ob, &virtualModifierData); md; md = md->next)
3238 if (modifier_dependsOnTime(md) &&
3239 (modifier_isEnabled(scene, md, eModifierMode_Realtime) ||
3240 modifier_isEnabled(scene, md, eModifierMode_Render)))
3247 MovieClip *BKE_object_movieclip_get(Scene *scene, Object *ob, bool use_default)
3249 MovieClip *clip = use_default ? scene->clip : NULL;
3250 bConstraint *con = ob->constraints.first, *scon = NULL;
3253 if (con->type == CONSTRAINT_TYPE_CAMERASOLVER) {
3254 if (scon == NULL || (scon->flag & CONSTRAINT_OFF))
3262 bCameraSolverConstraint *solver = scon->data;
3263 if ((solver->flag & CAMERASOLVER_ACTIVECLIP) == 0)
3264 clip = solver->clip;
3274 * Find an associated Armature object
3276 static Object *obrel_armature_find(Object *ob)
3278 Object *ob_arm = NULL;
3280 if (ob->parent && ob->partype == PARSKEL && ob->parent->type == OB_ARMATURE) {
3281 ob_arm = ob->parent;