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;
1128 if (ob_src->iuser) ob_dst->iuser = MEM_dupallocN(ob_src->iuser);
1130 if (ob_src->bb) ob_dst->bb = MEM_dupallocN(ob_src->bb);
1131 ob_dst->flag &= ~OB_FROMGROUP;
1133 BLI_listbase_clear(&ob_dst->modifiers);
1135 for (md = ob_src->modifiers.first; md; md = md->next) {
1136 ModifierData *nmd = modifier_new(md->type);
1137 BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
1138 modifier_copyData_ex(md, nmd, flag_subdata);
1139 BLI_addtail(&ob_dst->modifiers, nmd);
1142 BLI_listbase_clear(&ob_dst->prop);
1143 BKE_bproperty_copy_list(&ob_dst->prop, &ob_src->prop);
1145 BKE_sca_logic_copy(ob_dst, ob_src, flag_subdata);
1148 copy_object_pose(ob_dst, ob_src, flag_subdata);
1149 /* backwards compat... non-armatures can get poses in older files? */
1150 if (ob_src->type == OB_ARMATURE)
1151 BKE_pose_rebuild(ob_dst, ob_dst->data);
1153 defgroup_copy_list(&ob_dst->defbase, &ob_src->defbase);
1154 BKE_constraints_copy_ex(&ob_dst->constraints, &ob_src->constraints, flag_subdata, true);
1156 ob_dst->mode = OB_MODE_OBJECT;
1157 ob_dst->sculpt = NULL;
1160 ob_dst->pd = MEM_dupallocN(ob_src->pd);
1161 if (ob_dst->pd->rng) {
1162 ob_dst->pd->rng = MEM_dupallocN(ob_src->pd->rng);
1165 ob_dst->soft = copy_softbody(ob_src->soft, flag_subdata);
1166 ob_dst->bsoft = copy_bulletsoftbody(ob_src->bsoft, flag_subdata);
1167 ob_dst->rigidbody_object = BKE_rigidbody_copy_object(ob_src, flag_subdata);
1168 ob_dst->rigidbody_constraint = BKE_rigidbody_copy_constraint(ob_src, flag_subdata);
1170 BKE_object_copy_particlesystems(ob_dst, ob_src, flag_subdata);
1172 ob_dst->derivedDeform = NULL;
1173 ob_dst->derivedFinal = NULL;
1175 BLI_listbase_clear(&ob_dst->gpulamp);
1176 BLI_listbase_clear(&ob_dst->pc_ids);
1178 ob_dst->mpath = NULL;
1180 copy_object_lod(ob_dst, ob_src, flag_subdata);
1182 /* Do not copy runtime curve data. */
1183 ob_dst->curve_cache = NULL;
1185 /* Do not copy object's preview (mostly due to the fact renderers create temp copy of objects). */
1186 if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0 && false) { /* XXX TODO temp hack */
1187 BKE_previewimg_id_copy(&ob_dst->id, &ob_src->id);
1190 ob_dst->preview = NULL;
1194 /* copy objects, will re-initialize cached simulation data */
1195 Object *BKE_object_copy(Main *bmain, const Object *ob)
1198 BKE_id_copy_ex(bmain, &ob->id, (ID **)&ob_copy, 0, false);
1202 void BKE_object_make_local_ex(Main *bmain, Object *ob, const bool lib_local, const bool clear_proxy)
1204 bool is_local = false, is_lib = false;
1206 /* - only lib users: do nothing (unless force_local is set)
1207 * - only local users: set flag
1208 * - mixed: make copy
1209 * In case we make a whole lib's content local, we always want to localize, and we skip remapping (done later).
1212 if (!ID_IS_LINKED(ob)) {
1216 BKE_library_ID_test_usages(bmain, ob, &is_local, &is_lib);
1218 if (lib_local || is_local) {
1220 id_clear_lib_data(bmain, &ob->id);
1221 BKE_id_expand_local(bmain, &ob->id);
1223 if (ob->proxy_from != NULL) {
1224 ob->proxy_from->proxy = NULL;
1225 ob->proxy_from->proxy_group = NULL;
1227 ob->proxy = ob->proxy_from = ob->proxy_group = NULL;
1231 Object *ob_new = BKE_object_copy(bmain, ob);
1234 ob_new->proxy = ob_new->proxy_from = ob_new->proxy_group = NULL;
1236 /* setting newid is mandatory for complex make_lib_local logic... */
1237 ID_NEW_SET(ob, ob_new);
1240 BKE_libblock_remap(bmain, ob, ob_new, ID_REMAP_SKIP_INDIRECT_USAGE);
1246 void BKE_object_make_local(Main *bmain, Object *ob, const bool lib_local)
1248 BKE_object_make_local_ex(bmain, ob, lib_local, true);
1251 /* Returns true if the Object is from an external blend file (libdata) */
1252 bool BKE_object_is_libdata(const Object *ob)
1254 return (ob && ID_IS_LINKED(ob));
1257 /* Returns true if the Object data is from an external blend file (libdata) */
1258 bool BKE_object_obdata_is_libdata(const Object *ob)
1260 /* Linked objects with local obdata are forbidden! */
1261 BLI_assert(!ob || !ob->data || (ID_IS_LINKED(ob) ? ID_IS_LINKED(ob->data) : true));
1262 return (ob && ob->data && ID_IS_LINKED(ob->data));
1265 /* *************** PROXY **************** */
1267 /* when you make proxy, ensure the exposed layers are extern */
1268 static void armature_set_id_extern(Object *ob)
1270 bArmature *arm = ob->data;
1271 bPoseChannel *pchan;
1272 unsigned int lay = arm->layer_protected;
1274 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1275 if (!(pchan->bone->layer & lay))
1276 id_lib_extern((ID *)pchan->custom);
1281 void BKE_object_copy_proxy_drivers(Object *ob, Object *target)
1283 if ((target->adt) && (target->adt->drivers.first)) {
1286 /* add new animdata block */
1288 ob->adt = BKE_animdata_add_id(&ob->id);
1290 /* make a copy of all the drivers (for now), then correct any links that need fixing */
1291 free_fcurves(&ob->adt->drivers);
1292 copy_fcurves(&ob->adt->drivers, &target->adt->drivers);
1294 for (fcu = ob->adt->drivers.first; fcu; fcu = fcu->next) {
1295 ChannelDriver *driver = fcu->driver;
1298 for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
1300 DRIVER_TARGETS_LOOPER(dvar)
1303 if ((Object *)dtar->id == target)
1304 dtar->id = (ID *)ob;
1306 /* only on local objects because this causes indirect links
1307 * 'a -> b -> c', blend to point directly to a.blend
1308 * when a.blend has a proxy thats linked into c.blend */
1309 if (!ID_IS_LINKED(ob))
1310 id_lib_extern((ID *)dtar->id);
1314 DRIVER_TARGETS_LOOPER_END
1320 /* proxy rule: lib_object->proxy_from == the one we borrow from, set temporally while object_update */
1321 /* local_object->proxy == pointer to library object, saved in files and read */
1322 /* local_object->proxy_group == pointer to group dupli-object, saved in files and read */
1324 void BKE_object_make_proxy(Object *ob, Object *target, Object *gob)
1326 /* paranoia checks */
1327 if (ID_IS_LINKED(ob) || !ID_IS_LINKED(target)) {
1328 printf("cannot make proxy\n");
1333 ob->proxy_group = gob;
1334 id_lib_extern(&target->id);
1336 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1337 DAG_id_tag_update(&target->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1340 * - gob means this proxy comes from a group, just apply the matrix
1341 * so the object wont move from its dupli-transform.
1343 * - no gob means this is being made from a linked object,
1344 * this is closer to making a copy of the object - in-place. */
1346 ob->rotmode = target->rotmode;
1347 mul_m4_m4m4(ob->obmat, gob->obmat, target->obmat);
1348 if (gob->dup_group) { /* should always be true */
1350 mul_v3_mat3_m4v3(tvec, ob->obmat, gob->dup_group->dupli_ofs);
1351 sub_v3_v3(ob->obmat[3], tvec);
1353 BKE_object_apply_mat4(ob, ob->obmat, false, true);
1356 BKE_object_transform_copy(ob, target);
1357 ob->parent = target->parent; /* libdata */
1358 copy_m4_m4(ob->parentinv, target->parentinv);
1361 /* copy animdata stuff - drivers only for now... */
1362 BKE_object_copy_proxy_drivers(ob, target);
1364 /* skip constraints? */
1365 /* FIXME: this is considered by many as a bug */
1367 /* set object type and link to data */
1368 ob->type = target->type;
1369 ob->data = target->data;
1370 id_us_plus((ID *)ob->data); /* ensures lib data becomes LIB_TAG_EXTERN */
1372 /* copy vertex groups */
1373 defgroup_copy_list(&ob->defbase, &target->defbase);
1375 /* copy material and index information */
1376 ob->actcol = ob->totcol = 0;
1377 if (ob->mat) MEM_freeN(ob->mat);
1378 if (ob->matbits) MEM_freeN(ob->matbits);
1381 if ((target->totcol) && (target->mat) && OB_TYPE_SUPPORT_MATERIAL(ob->type)) {
1384 ob->actcol = target->actcol;
1385 ob->totcol = target->totcol;
1387 ob->mat = MEM_dupallocN(target->mat);
1388 ob->matbits = MEM_dupallocN(target->matbits);
1389 for (i = 0; i < target->totcol; i++) {
1390 /* don't need to run test_object_materials since we know this object is new and not used elsewhere */
1391 id_us_plus((ID *)ob->mat[i]);
1395 /* type conversions */
1396 if (target->type == OB_ARMATURE) {
1397 copy_object_pose(ob, target, 0); /* data copy, object pointers in constraints */
1398 BKE_pose_rest(ob->pose); /* clear all transforms in channels */
1399 BKE_pose_rebuild(ob, ob->data); /* set all internal links */
1401 armature_set_id_extern(ob);
1403 else if (target->type == OB_EMPTY) {
1404 ob->empty_drawtype = target->empty_drawtype;
1405 ob->empty_drawsize = target->empty_drawsize;
1408 /* copy IDProperties */
1409 if (ob->id.properties) {
1410 IDP_FreeProperty(ob->id.properties);
1411 MEM_freeN(ob->id.properties);
1412 ob->id.properties = NULL;
1414 if (target->id.properties) {
1415 ob->id.properties = IDP_CopyProperty(target->id.properties);
1418 /* copy drawtype info */
1419 ob->dt = target->dt;
1423 * Use with newly created objects to set their size
1424 * (used to apply scene-scale).
1426 void BKE_object_obdata_size_init(struct Object *ob, const float size)
1428 /* apply radius as a scale to types that support it */
1432 ob->empty_drawsize *= size;
1437 Curve *cu = ob->data;
1443 Camera *cam = ob->data;
1444 cam->drawsize *= size;
1449 Lamp *lamp = ob->data;
1451 lamp->area_size *= size;
1452 lamp->area_sizey *= size;
1453 lamp->area_sizez *= size;
1456 /* Only lattice (not mesh, curve, mball...),
1457 * because its got data when newly added */
1460 struct Lattice *lt = ob->data;
1464 scale_m4_fl(mat, size);
1466 BKE_lattice_transform(lt, (float (*)[4])mat, false);
1472 /* *************** CALC ****************** */
1474 void BKE_object_scale_to_mat3(Object *ob, float mat[3][3])
1477 mul_v3_v3v3(vec, ob->size, ob->dscale);
1478 size_to_mat3(mat, vec);
1481 void BKE_object_rot_to_mat3(Object *ob, float mat[3][3], bool use_drot)
1483 float rmat[3][3], dmat[3][3];
1485 /* 'dmat' is the delta-rotation matrix, which will get (pre)multiplied
1486 * with the rotation matrix to yield the appropriate rotation
1489 /* rotations may either be quats, eulers (with various rotation orders), or axis-angle */
1490 if (ob->rotmode > 0) {
1491 /* euler rotations (will cause gimble lock, but this can be alleviated a bit with rotation orders) */
1492 eulO_to_mat3(rmat, ob->rot, ob->rotmode);
1493 eulO_to_mat3(dmat, ob->drot, ob->rotmode);
1495 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
1496 /* axis-angle - not really that great for 3D-changing orientations */
1497 axis_angle_to_mat3(rmat, ob->rotAxis, ob->rotAngle);
1498 axis_angle_to_mat3(dmat, ob->drotAxis, ob->drotAngle);
1501 /* quats are normalized before use to eliminate scaling issues */
1504 normalize_qt_qt(tquat, ob->quat);
1505 quat_to_mat3(rmat, tquat);
1507 normalize_qt_qt(tquat, ob->dquat);
1508 quat_to_mat3(dmat, tquat);
1511 /* combine these rotations */
1513 mul_m3_m3m3(mat, dmat, rmat);
1515 copy_m3_m3(mat, rmat);
1518 void BKE_object_mat3_to_rot(Object *ob, float mat[3][3], bool use_compat)
1520 BLI_ASSERT_UNIT_M3(mat);
1522 switch (ob->rotmode) {
1526 mat3_normalized_to_quat(ob->quat, mat);
1527 normalize_qt_qt(dquat, ob->dquat);
1528 invert_qt_normalized(dquat);
1529 mul_qt_qtqt(ob->quat, dquat, ob->quat);
1532 case ROT_MODE_AXISANGLE:
1537 /* without drot we could apply 'mat' directly */
1538 mat3_normalized_to_quat(quat, mat);
1539 axis_angle_to_quat(dquat, ob->drotAxis, ob->drotAngle);
1540 invert_qt_normalized(dquat);
1541 mul_qt_qtqt(quat, dquat, quat);
1542 quat_to_axis_angle(ob->rotAxis, &ob->rotAngle, quat);
1545 default: /* euler */
1550 /* without drot we could apply 'mat' directly */
1551 mat3_normalized_to_quat(quat, mat);
1552 eulO_to_quat(dquat, ob->drot, ob->rotmode);
1553 invert_qt_normalized(dquat);
1554 mul_qt_qtqt(quat, dquat, quat);
1555 /* end drot correction */
1557 if (use_compat) quat_to_compatible_eulO(ob->rot, ob->rot, ob->rotmode, quat);
1558 else quat_to_eulO(ob->rot, ob->rotmode, quat);
1564 void BKE_object_tfm_protected_backup(const Object *ob,
1565 ObjectTfmProtectedChannels *obtfm)
1568 #define TFMCPY(_v) (obtfm->_v = ob->_v)
1569 #define TFMCPY3D(_v) copy_v3_v3(obtfm->_v, ob->_v)
1570 #define TFMCPY4D(_v) copy_v4_v4(obtfm->_v, ob->_v)
1591 void BKE_object_tfm_protected_restore(Object *ob,
1592 const ObjectTfmProtectedChannels *obtfm,
1593 const short protectflag)
1597 for (i = 0; i < 3; i++) {
1598 if (protectflag & (OB_LOCK_LOCX << i)) {
1599 ob->loc[i] = obtfm->loc[i];
1600 ob->dloc[i] = obtfm->dloc[i];
1603 if (protectflag & (OB_LOCK_SCALEX << i)) {
1604 ob->size[i] = obtfm->size[i];
1605 ob->dscale[i] = obtfm->dscale[i];
1608 if (protectflag & (OB_LOCK_ROTX << i)) {
1609 ob->rot[i] = obtfm->rot[i];
1610 ob->drot[i] = obtfm->drot[i];
1612 ob->quat[i + 1] = obtfm->quat[i + 1];
1613 ob->dquat[i + 1] = obtfm->dquat[i + 1];
1615 ob->rotAxis[i] = obtfm->rotAxis[i];
1616 ob->drotAxis[i] = obtfm->drotAxis[i];
1620 if ((protectflag & OB_LOCK_ROT4D) && (protectflag & OB_LOCK_ROTW)) {
1621 ob->quat[0] = obtfm->quat[0];
1622 ob->dquat[0] = obtfm->dquat[0];
1624 ob->rotAngle = obtfm->rotAngle;
1625 ob->drotAngle = obtfm->drotAngle;
1629 void BKE_object_to_mat3(Object *ob, float mat[3][3]) /* no parent */
1636 BKE_object_scale_to_mat3(ob, smat);
1639 BKE_object_rot_to_mat3(ob, rmat, true);
1640 mul_m3_m3m3(mat, rmat, smat);
1643 void BKE_object_to_mat4(Object *ob, float mat[4][4])
1647 BKE_object_to_mat3(ob, tmat);
1649 copy_m4_m3(mat, tmat);
1651 add_v3_v3v3(mat[3], ob->loc, ob->dloc);
1654 void BKE_object_matrix_local_get(struct Object *ob, float mat[4][4])
1657 float par_imat[4][4];
1659 BKE_object_get_parent_matrix(NULL, ob, ob->parent, par_imat);
1660 invert_m4(par_imat);
1661 mul_m4_m4m4(mat, par_imat, ob->obmat);
1664 copy_m4_m4(mat, ob->obmat);
1669 int enable_cu_speed = 1;
1672 * \param scene: Used when curve cache needs to be calculated, or for dupli-frame time.
1673 * \return success if \a mat is set.
1675 static bool ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[4][4])
1677 Curve *cu = par->data;
1678 float vec[4], dir[3], quat[4], radius, ctime;
1680 /* only happens on reload file, but violates depsgraph still... fix! */
1681 if (par->curve_cache == NULL) {
1682 if (scene == NULL) {
1685 BKE_displist_make_curveTypes(scene, par, 0);
1688 if (par->curve_cache->path == NULL) {
1692 /* catch exceptions: curve paths used as a duplicator */
1693 if (enable_cu_speed) {
1694 /* ctime is now a proper var setting of Curve which gets set by Animato like any other var that's animated,
1695 * but this will only work if it actually is animated...
1697 * we divide the curvetime calculated in the previous step by the length of the path, to get a time
1698 * factor, which then gets clamped to lie within 0.0 - 1.0 range
1701 ctime = cu->ctime / cu->pathlen;
1707 CLAMP(ctime, 0.0f, 1.0f);
1710 /* For dupli-frames only */
1711 if (scene == NULL) {
1715 ctime = BKE_scene_frame_get(scene);
1717 ctime /= cu->pathlen;
1720 CLAMP(ctime, 0.0f, 1.0f);
1726 if (where_on_path(par, ctime, vec, dir, (cu->flag & CU_FOLLOW) ? quat : NULL, &radius, NULL)) {
1728 if (cu->flag & CU_FOLLOW) {
1731 vec_to_quat(quat, dir, ob->trackflag, ob->upflag);
1735 q[0] = cosf(0.5 * vec[3]);
1736 si = sinf(0.5 * vec[3]);
1737 q[1] = -si * dir[0];
1738 q[2] = -si * dir[1];
1739 q[3] = -si * dir[2];
1740 mul_qt_qtqt(quat, q, quat);
1742 quat_apply_track(quat, ob->trackflag, ob->upflag);
1745 quat_to_mat4(mat, quat);
1748 if (cu->flag & CU_PATH_RADIUS) {
1749 float tmat[4][4], rmat[4][4];
1750 scale_m4_fl(tmat, radius);
1751 mul_m4_m4m4(rmat, tmat, mat);
1752 copy_m4_m4(mat, rmat);
1755 copy_v3_v3(mat[3], vec);
1762 static void ob_parbone(Object *ob, Object *par, float mat[4][4])
1764 bPoseChannel *pchan;
1767 if (par->type != OB_ARMATURE) {
1772 /* Make sure the bone is still valid */
1773 pchan = BKE_pose_channel_find_name(par->pose, ob->parsubstr);
1774 if (!pchan || !pchan->bone) {
1775 printf("Object %s with Bone parent: bone %s doesn't exist\n", ob->id.name + 2, ob->parsubstr);
1780 /* get bone transform */
1781 if (pchan->bone->flag & BONE_RELATIVE_PARENTING) {
1782 /* the new option uses the root - expected bahaviour, but differs from old... */
1783 /* XXX check on version patching? */
1784 copy_m4_m4(mat, pchan->chan_mat);
1787 copy_m4_m4(mat, pchan->pose_mat);
1789 /* but for backwards compatibility, the child has to move to the tail */
1790 copy_v3_v3(vec, mat[1]);
1791 mul_v3_fl(vec, pchan->bone->length);
1792 add_v3_v3(mat[3], vec);
1796 static void give_parvert(Object *par, int nr, float vec[3])
1800 if (par->type == OB_MESH) {
1801 Mesh *me = par->data;
1802 BMEditMesh *em = me->edit_btmesh;
1805 dm = (em) ? em->derivedFinal : par->derivedFinal;
1809 int numVerts = dm->getNumVerts(dm);
1811 if (nr < numVerts) {
1812 bool use_special_ss_case = false;
1814 if (dm->type == DM_TYPE_CCGDM) {
1816 VirtualModifierData virtualModifierData;
1817 use_special_ss_case = true;
1818 for (md = modifiers_getVirtualModifierList(par, &virtualModifierData);
1822 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1823 /* TODO(sergey): Check for disabled modifiers. */
1824 if (mti->type != eModifierTypeType_OnlyDeform && md->next != NULL) {
1825 use_special_ss_case = false;
1831 if (!use_special_ss_case) {
1832 /* avoid dm->getVertDataArray() since it allocates arrays in the dm (not thread safe) */
1833 if (em && dm->type == DM_TYPE_EDITBMESH) {
1834 if (em->bm->elem_table_dirty & BM_VERT) {
1835 #ifdef VPARENT_THREADING_HACK
1836 BLI_mutex_lock(&vparent_lock);
1837 if (em->bm->elem_table_dirty & BM_VERT) {
1838 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
1840 BLI_mutex_unlock(&vparent_lock);
1842 BLI_assert(!"Not safe for threading");
1843 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
1849 if (use_special_ss_case) {
1850 /* Special case if the last modifier is SS and no constructive modifier are in front of it. */
1851 CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
1852 CCGVert *ccg_vert = ccgSubSurf_getVert(ccgdm->ss, SET_INT_IN_POINTER(nr));
1853 /* In case we deleted some verts, nr may refer to inexistent one now, see T42557. */
1855 float *co = ccgSubSurf_getVertData(ccgdm->ss, ccg_vert);
1860 else if (CustomData_has_layer(&dm->vertData, CD_ORIGINDEX) &&
1861 !(em && dm->type == DM_TYPE_EDITBMESH))
1865 /* Get the average of all verts with (original index == nr). */
1866 for (i = 0; i < numVerts; i++) {
1867 const int *index = dm->getVertData(dm, i, CD_ORIGINDEX);
1870 dm->getVertCo(dm, i, co);
1877 if (nr < numVerts) {
1879 dm->getVertCo(dm, nr, co);
1887 /* keep as 0, 0, 0 */
1889 else if (count > 0) {
1890 mul_v3_fl(vec, 1.0f / count);
1893 /* use first index if its out of range */
1894 dm->getVertCo(dm, 0, vec);
1899 "%s: DerivedMesh is needed to solve parenting, "
1900 "object position can be wrong now\n", __func__);
1903 else if (ELEM(par->type, OB_CURVE, OB_SURF)) {
1906 /* Unless there's some weird depsgraph failure the cache should exist. */
1907 BLI_assert(par->curve_cache != NULL);
1909 if (par->curve_cache->deformed_nurbs.first != NULL) {
1910 nurb = &par->curve_cache->deformed_nurbs;
1913 Curve *cu = par->data;
1914 nurb = BKE_curve_nurbs_get(cu);
1917 BKE_nurbList_index_get_co(nurb, nr, vec);
1919 else if (par->type == OB_LATTICE) {
1920 Lattice *latt = par->data;
1921 DispList *dl = par->curve_cache ? BKE_displist_find(&par->curve_cache->disp, DL_VERTS) : NULL;
1922 float (*co)[3] = dl ? (float (*)[3])dl->verts : NULL;
1925 if (latt->editlatt) latt = latt->editlatt->latt;
1927 tot = latt->pntsu * latt->pntsv * latt->pntsw;
1929 /* ensure dl is correct size */
1930 BLI_assert(dl == NULL || dl->nr == tot);
1934 copy_v3_v3(vec, co[nr]);
1937 copy_v3_v3(vec, latt->def[nr].vec);
1943 static void ob_parvert3(Object *ob, Object *par, float mat[4][4])
1946 /* in local ob space */
1947 if (OB_TYPE_SUPPORT_PARVERT(par->type)) {
1948 float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1950 give_parvert(par, ob->par1, v1);
1951 give_parvert(par, ob->par2, v2);
1952 give_parvert(par, ob->par3, v3);
1954 tri_to_quat(q, v1, v2, v3);
1955 quat_to_mat3(cmat, q);
1956 copy_m4_m3(mat, cmat);
1958 mid_v3_v3v3v3(mat[3], v1, v2, v3);
1966 void BKE_object_get_parent_matrix(Scene *scene, Object *ob, Object *par, float parentmat[4][4])
1972 switch (ob->partype & PARTYPE) {
1975 if (par->type == OB_CURVE) {
1976 if ((((Curve *)par->data)->flag & CU_PATH) &&
1977 (ob_parcurve(scene, ob, par, tmat)))
1983 if (ok) mul_m4_m4m4(parentmat, par->obmat, tmat);
1984 else copy_m4_m4(parentmat, par->obmat);
1988 ob_parbone(ob, par, tmat);
1989 mul_m4_m4m4(parentmat, par->obmat, tmat);
1994 give_parvert(par, ob->par1, vec);
1995 mul_v3_m4v3(parentmat[3], par->obmat, vec);
1998 ob_parvert3(ob, par, tmat);
2000 mul_m4_m4m4(parentmat, par->obmat, tmat);
2004 copy_m4_m4(parentmat, par->obmat);
2011 * \param r_originmat Optional matrix that stores the space the object is in (without its own matrix applied)
2013 static void solve_parenting(Scene *scene, Object *ob, Object *par, float obmat[4][4], float slowmat[4][4],
2014 float r_originmat[3][3], const bool set_origin)
2020 BKE_object_to_mat4(ob, locmat);
2022 if (ob->partype & PARSLOW) copy_m4_m4(slowmat, obmat);
2024 BKE_object_get_parent_matrix(scene, ob, par, totmat);
2027 mul_m4_m4m4(tmat, totmat, ob->parentinv);
2028 mul_m4_m4m4(obmat, tmat, locmat);
2031 /* usable originmat */
2032 copy_m3_m4(r_originmat, tmat);
2035 /* origin, for help line */
2037 if ((ob->partype & PARTYPE) == PARSKEL) {
2038 copy_v3_v3(ob->orig, par->obmat[3]);
2041 copy_v3_v3(ob->orig, totmat[3]);
2046 static bool where_is_object_parslow(Object *ob, float obmat[4][4], float slowmat[4][4])
2052 /* include framerate */
2053 fac1 = (1.0f / (1.0f + fabsf(ob->sf)));
2054 if (fac1 >= 1.0f) return false;
2059 for (a = 0; a < 16; a++, fp1++, fp2++) {
2060 fp1[0] = fac1 * fp1[0] + fac2 * fp2[0];
2066 /* note, scene is the active scene while actual_scene is the scene the object resides in */
2067 void BKE_object_where_is_calc_time_ex(Scene *scene, Object *ob, float ctime,
2068 RigidBodyWorld *rbw, float r_originmat[3][3])
2070 if (ob == NULL) return;
2072 /* execute drivers only, as animation has already been done */
2073 BKE_animsys_evaluate_animdata(scene, &ob->id, ob->adt, ctime, ADT_RECALC_DRIVERS);
2076 Object *par = ob->parent;
2077 float slowmat[4][4];
2079 /* calculate parent matrix */
2080 solve_parenting(scene, ob, par, ob->obmat, slowmat, r_originmat, true);
2082 /* "slow parent" is definitely not threadsafe, and may also give bad results jumping around
2083 * An old-fashioned hack which probably doesn't really cut it anymore
2085 if (ob->partype & PARSLOW) {
2086 if (!where_is_object_parslow(ob, ob->obmat, slowmat))
2091 BKE_object_to_mat4(ob, ob->obmat);
2094 /* try to fall back to the scene rigid body world if none given */
2095 rbw = rbw ? rbw : scene->rigidbody_world;
2096 /* read values pushed into RBO from sim/cache... */
2097 BKE_rigidbody_sync_transforms(rbw, ob, ctime);
2099 /* solve constraints */
2100 if (ob->constraints.first && !(ob->transflag & OB_NO_CONSTRAINTS)) {
2102 cob = BKE_constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2103 BKE_constraints_solve(&ob->constraints, cob, ctime);
2104 BKE_constraints_clear_evalob(cob);
2107 /* set negative scale flag in object */
2108 if (is_negative_m4(ob->obmat)) ob->transflag |= OB_NEG_SCALE;
2109 else ob->transflag &= ~OB_NEG_SCALE;
2112 void BKE_object_where_is_calc_time(Scene *scene, Object *ob, float ctime)
2114 BKE_object_where_is_calc_time_ex(scene, ob, ctime, NULL, NULL);
2117 /* get object transformation matrix without recalculating dependencies and
2118 * constraints -- assume dependencies are already solved by depsgraph.
2119 * no changes to object and it's parent would be done.
2120 * used for bundles orientation in 3d space relative to parented blender camera */
2121 void BKE_object_where_is_calc_mat4(Scene *scene, Object *ob, float obmat[4][4])
2125 float slowmat[4][4];
2127 Object *par = ob->parent;
2129 solve_parenting(scene, ob, par, obmat, slowmat, NULL, false);
2131 if (ob->partype & PARSLOW)
2132 where_is_object_parslow(ob, obmat, slowmat);
2135 BKE_object_to_mat4(ob, obmat);
2139 void BKE_object_where_is_calc_ex(Scene *scene, RigidBodyWorld *rbw, Object *ob, float r_originmat[3][3])
2141 BKE_object_where_is_calc_time_ex(scene, ob, BKE_scene_frame_get(scene), rbw, r_originmat);
2143 void BKE_object_where_is_calc(Scene *scene, Object *ob)
2145 BKE_object_where_is_calc_time_ex(scene, ob, BKE_scene_frame_get(scene), NULL, NULL);
2148 /* for calculation of the inverse parent transform, only used for editor */
2149 void BKE_object_workob_calc_parent(Scene *scene, Object *ob, Object *workob)
2151 BKE_object_workob_clear(workob);
2153 unit_m4(workob->obmat);
2154 unit_m4(workob->parentinv);
2155 unit_m4(workob->constinv);
2156 workob->parent = ob->parent;
2158 workob->trackflag = ob->trackflag;
2159 workob->upflag = ob->upflag;
2161 workob->partype = ob->partype;
2162 workob->par1 = ob->par1;
2163 workob->par2 = ob->par2;
2164 workob->par3 = ob->par3;
2166 workob->constraints.first = ob->constraints.first;
2167 workob->constraints.last = ob->constraints.last;
2169 BLI_strncpy(workob->parsubstr, ob->parsubstr, sizeof(workob->parsubstr));
2171 BKE_object_where_is_calc(scene, workob);
2174 /* see BKE_pchan_apply_mat4() for the equivalent 'pchan' function */
2175 void BKE_object_apply_mat4(Object *ob, float mat[4][4], const bool use_compat, const bool use_parent)
2179 if (use_parent && ob->parent) {
2180 float rmat[4][4], diff_mat[4][4], imat[4][4], parent_mat[4][4];
2182 BKE_object_get_parent_matrix(NULL, ob, ob->parent, parent_mat);
2184 mul_m4_m4m4(diff_mat, parent_mat, ob->parentinv);
2185 invert_m4_m4(imat, diff_mat);
2186 mul_m4_m4m4(rmat, imat, mat); /* get the parent relative matrix */
2188 /* same as below, use rmat rather than mat */
2189 mat4_to_loc_rot_size(ob->loc, rot, ob->size, rmat);
2192 mat4_to_loc_rot_size(ob->loc, rot, ob->size, mat);
2195 BKE_object_mat3_to_rot(ob, rot, use_compat);
2197 sub_v3_v3(ob->loc, ob->dloc);
2199 if (ob->dscale[0] != 0.0f) ob->size[0] /= ob->dscale[0];
2200 if (ob->dscale[1] != 0.0f) ob->size[1] /= ob->dscale[1];
2201 if (ob->dscale[2] != 0.0f) ob->size[2] /= ob->dscale[2];
2203 /* BKE_object_mat3_to_rot handles delta rotations */
2206 BoundBox *BKE_boundbox_alloc_unit(void)
2209 const float min[3] = {-1.0f, -1.0f, -1.0f}, max[3] = {-1.0f, -1.0f, -1.0f};
2211 bb = MEM_callocN(sizeof(BoundBox), "OB-BoundBox");
2212 BKE_boundbox_init_from_minmax(bb, min, max);
2217 void BKE_boundbox_init_from_minmax(BoundBox *bb, const float min[3], const float max[3])
2219 bb->vec[0][0] = bb->vec[1][0] = bb->vec[2][0] = bb->vec[3][0] = min[0];
2220 bb->vec[4][0] = bb->vec[5][0] = bb->vec[6][0] = bb->vec[7][0] = max[0];
2222 bb->vec[0][1] = bb->vec[1][1] = bb->vec[4][1] = bb->vec[5][1] = min[1];
2223 bb->vec[2][1] = bb->vec[3][1] = bb->vec[6][1] = bb->vec[7][1] = max[1];
2225 bb->vec[0][2] = bb->vec[3][2] = bb->vec[4][2] = bb->vec[7][2] = min[2];
2226 bb->vec[1][2] = bb->vec[2][2] = bb->vec[5][2] = bb->vec[6][2] = max[2];
2229 void BKE_boundbox_calc_center_aabb(const BoundBox *bb, float r_cent[3])
2231 r_cent[0] = 0.5f * (bb->vec[0][0] + bb->vec[4][0]);
2232 r_cent[1] = 0.5f * (bb->vec[0][1] + bb->vec[2][1]);
2233 r_cent[2] = 0.5f * (bb->vec[0][2] + bb->vec[1][2]);
2236 void BKE_boundbox_calc_size_aabb(const BoundBox *bb, float r_size[3])
2238 r_size[0] = 0.5f * fabsf(bb->vec[0][0] - bb->vec[4][0]);
2239 r_size[1] = 0.5f * fabsf(bb->vec[0][1] - bb->vec[2][1]);
2240 r_size[2] = 0.5f * fabsf(bb->vec[0][2] - bb->vec[1][2]);
2243 void BKE_boundbox_minmax(const BoundBox *bb, float obmat[4][4], float r_min[3], float r_max[3])
2246 for (i = 0; i < 8; i++) {
2248 mul_v3_m4v3(vec, obmat, bb->vec[i]);
2249 minmax_v3v3_v3(r_min, r_max, vec);
2253 BoundBox *BKE_object_boundbox_get(Object *ob)
2255 BoundBox *bb = NULL;
2257 if (ob->type == OB_MESH) {
2258 bb = BKE_mesh_boundbox_get(ob);
2260 else if (ELEM(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2261 bb = BKE_curve_boundbox_get(ob);
2263 else if (ob->type == OB_MBALL) {
2266 else if (ob->type == OB_LATTICE) {
2267 bb = BKE_lattice_boundbox_get(ob);
2269 else if (ob->type == OB_ARMATURE) {
2270 bb = BKE_armature_boundbox_get(ob);
2275 /* used to temporally disable/enable boundbox */
2276 void BKE_object_boundbox_flag(Object *ob, int flag, const bool set)
2278 BoundBox *bb = BKE_object_boundbox_get(ob);
2280 if (set) bb->flag |= flag;
2281 else bb->flag &= ~flag;
2285 void BKE_object_dimensions_get(Object *ob, float vec[3])
2287 BoundBox *bb = NULL;
2289 bb = BKE_object_boundbox_get(ob);
2293 mat4_to_size(scale, ob->obmat);
2295 vec[0] = fabsf(scale[0]) * (bb->vec[4][0] - bb->vec[0][0]);
2296 vec[1] = fabsf(scale[1]) * (bb->vec[2][1] - bb->vec[0][1]);
2297 vec[2] = fabsf(scale[2]) * (bb->vec[1][2] - bb->vec[0][2]);
2304 void BKE_object_dimensions_set(Object *ob, const float value[3])
2306 BoundBox *bb = NULL;
2308 bb = BKE_object_boundbox_get(ob);
2310 float scale[3], len[3];
2312 mat4_to_size(scale, ob->obmat);
2314 len[0] = bb->vec[4][0] - bb->vec[0][0];
2315 len[1] = bb->vec[2][1] - bb->vec[0][1];
2316 len[2] = bb->vec[1][2] - bb->vec[0][2];
2318 if (len[0] > 0.f) ob->size[0] = value[0] / len[0];
2319 if (len[1] > 0.f) ob->size[1] = value[1] / len[1];
2320 if (len[2] > 0.f) ob->size[2] = value[2] / len[2];
2324 void BKE_object_minmax(Object *ob, float min_r[3], float max_r[3], const bool use_hidden)
2328 bool changed = false;
2335 bb = *BKE_curve_boundbox_get(ob);
2336 BKE_boundbox_minmax(&bb, ob->obmat, min_r, max_r);
2342 Lattice *lt = ob->data;
2343 BPoint *bp = lt->def;
2346 for (w = 0; w < lt->pntsw; w++) {
2347 for (v = 0; v < lt->pntsv; v++) {
2348 for (u = 0; u < lt->pntsu; u++, bp++) {
2349 mul_v3_m4v3(vec, ob->obmat, bp->vec);
2350 minmax_v3v3_v3(min_r, max_r, vec);
2359 changed = BKE_pose_minmax(ob, min_r, max_r, use_hidden, false);
2364 Mesh *me = BKE_mesh_from_object(ob);
2367 bb = *BKE_mesh_boundbox_get(ob);
2368 BKE_boundbox_minmax(&bb, ob->obmat, min_r, max_r);
2375 float ob_min[3], ob_max[3];
2377 changed = BKE_mball_minmax_ex(ob->data, ob_min, ob_max, ob->obmat, 0);
2379 minmax_v3v3_v3(min_r, max_r, ob_min);
2380 minmax_v3v3_v3(min_r, max_r, ob_max);
2386 if (changed == false) {
2389 copy_v3_v3(size, ob->size);
2390 if (ob->type == OB_EMPTY) {
2391 mul_v3_fl(size, ob->empty_drawsize);
2394 minmax_v3v3_v3(min_r, max_r, ob->obmat[3]);
2396 copy_v3_v3(vec, ob->obmat[3]);
2397 add_v3_v3(vec, size);
2398 minmax_v3v3_v3(min_r, max_r, vec);
2400 copy_v3_v3(vec, ob->obmat[3]);
2401 sub_v3_v3(vec, size);
2402 minmax_v3v3_v3(min_r, max_r, vec);
2406 void BKE_object_empty_draw_type_set(Object *ob, const int value)
2408 ob->empty_drawtype = value;
2410 if (ob->type == OB_EMPTY && ob->empty_drawtype == OB_EMPTY_IMAGE) {
2412 ob->iuser = MEM_callocN(sizeof(ImageUser), "image user");
2414 ob->iuser->frames = 100;
2415 ob->iuser->sfra = 1;
2416 ob->iuser->fie_ima = 2;
2421 MEM_freeN(ob->iuser);
2427 bool BKE_object_minmax_dupli(
2428 Main *bmain, Scene *scene,
2429 Object *ob, float r_min[3], float r_max[3], const bool use_hidden)
2432 if ((ob->transflag & OB_DUPLI) == 0) {
2438 lb = object_duplilist(bmain, bmain->eval_ctx, scene, ob);
2439 for (dob = lb->first; dob; dob = dob->next) {
2440 if ((use_hidden == false) && (dob->no_draw != 0)) {
2444 BoundBox *bb = BKE_object_boundbox_get(dob->ob);
2448 for (i = 0; i < 8; i++) {
2450 mul_v3_m4v3(vec, dob->mat, bb->vec[i]);
2451 minmax_v3v3_v3(r_min, r_max, vec);
2458 free_object_duplilist(lb); /* does restore */
2464 void BKE_object_foreach_display_point(
2465 Object *ob, float obmat[4][4],
2466 void (*func_cb)(const float[3], void *), void *user_data)
2470 if (ob->derivedFinal) {
2471 DerivedMesh *dm = ob->derivedFinal;
2472 MVert *mv = dm->getVertArray(dm);
2473 int totvert = dm->getNumVerts(dm);
2476 for (i = 0; i < totvert; i++, mv++) {
2477 mul_v3_m4v3(co, obmat, mv->co);
2478 func_cb(co, user_data);
2481 else if (ob->curve_cache && ob->curve_cache->disp.first) {
2484 for (dl = ob->curve_cache->disp.first; dl; dl = dl->next) {
2485 const float *v3 = dl->verts;
2486 int totvert = dl->nr;
2489 for (i = 0; i < totvert; i++, v3 += 3) {
2490 mul_v3_m4v3(co, obmat, v3);
2491 func_cb(co, user_data);
2497 void BKE_scene_foreach_display_point(
2498 Main *bmain, Scene *scene, View3D *v3d, const short flag,
2499 void (*func_cb)(const float[3], void *), void *user_data)
2504 for (base = FIRSTBASE; base; base = base->next) {
2505 if (BASE_VISIBLE_BGMODE(v3d, scene, base) && (base->flag & flag) == flag) {
2508 if ((ob->transflag & OB_DUPLI) == 0) {
2509 BKE_object_foreach_display_point(ob, ob->obmat, func_cb, user_data);
2515 lb = object_duplilist(bmain, bmain->eval_ctx, scene, ob);
2516 for (dob = lb->first; dob; dob = dob->next) {
2517 if (dob->no_draw == 0) {
2518 BKE_object_foreach_display_point(dob->ob, dob->mat, func_cb, user_data);
2521 free_object_duplilist(lb); /* does restore */
2527 /* copied from DNA_object_types.h */
2528 typedef struct ObTfmBack {
2529 float loc[3], dloc[3], orig[3];
2530 float size[3], dscale[3]; /* scale and delta scale */
2531 float rot[3], drot[3]; /* euler rotation */
2532 float quat[4], dquat[4]; /* quaternion rotation */
2533 float rotAxis[3], drotAxis[3]; /* axis angle rotation - axis part */
2534 float rotAngle, drotAngle; /* axis angle rotation - angle part */
2535 float obmat[4][4]; /* final worldspace matrix with constraints & animsys applied */
2536 float parentinv[4][4]; /* inverse result of parent, so that object doesn't 'stick' to parent */
2537 float constinv[4][4]; /* inverse result of constraints. doesn't include effect of parent or object local transform */
2538 float imat[4][4]; /* inverse matrix of 'obmat' for during render, old game engine, temporally: ipokeys of transform */
2541 void *BKE_object_tfm_backup(Object *ob)
2543 ObTfmBack *obtfm = MEM_mallocN(sizeof(ObTfmBack), "ObTfmBack");
2544 copy_v3_v3(obtfm->loc, ob->loc);
2545 copy_v3_v3(obtfm->dloc, ob->dloc);
2546 copy_v3_v3(obtfm->orig, ob->orig);
2547 copy_v3_v3(obtfm->size, ob->size);
2548 copy_v3_v3(obtfm->dscale, ob->dscale);
2549 copy_v3_v3(obtfm->rot, ob->rot);
2550 copy_v3_v3(obtfm->drot, ob->drot);
2551 copy_qt_qt(obtfm->quat, ob->quat);
2552 copy_qt_qt(obtfm->dquat, ob->dquat);
2553 copy_v3_v3(obtfm->rotAxis, ob->rotAxis);
2554 copy_v3_v3(obtfm->drotAxis, ob->drotAxis);
2555 obtfm->rotAngle = ob->rotAngle;
2556 obtfm->drotAngle = ob->drotAngle;
2557 copy_m4_m4(obtfm->obmat, ob->obmat);
2558 copy_m4_m4(obtfm->parentinv, ob->parentinv);
2559 copy_m4_m4(obtfm->constinv, ob->constinv);
2560 copy_m4_m4(obtfm->imat, ob->imat);
2562 return (void *)obtfm;
2565 void BKE_object_tfm_restore(Object *ob, void *obtfm_pt)
2567 ObTfmBack *obtfm = (ObTfmBack *)obtfm_pt;
2568 copy_v3_v3(ob->loc, obtfm->loc);
2569 copy_v3_v3(ob->dloc, obtfm->dloc);
2570 copy_v3_v3(ob->orig, obtfm->orig);
2571 copy_v3_v3(ob->size, obtfm->size);
2572 copy_v3_v3(ob->dscale, obtfm->dscale);
2573 copy_v3_v3(ob->rot, obtfm->rot);
2574 copy_v3_v3(ob->drot, obtfm->drot);
2575 copy_qt_qt(ob->quat, obtfm->quat);
2576 copy_qt_qt(ob->dquat, obtfm->dquat);
2577 copy_v3_v3(ob->rotAxis, obtfm->rotAxis);
2578 copy_v3_v3(ob->drotAxis, obtfm->drotAxis);
2579 ob->rotAngle = obtfm->rotAngle;
2580 ob->drotAngle = obtfm->drotAngle;
2581 copy_m4_m4(ob->obmat, obtfm->obmat);
2582 copy_m4_m4(ob->parentinv, obtfm->parentinv);
2583 copy_m4_m4(ob->constinv, obtfm->constinv);
2584 copy_m4_m4(ob->imat, obtfm->imat);
2587 bool BKE_object_parent_loop_check(const Object *par, const Object *ob)
2589 /* test if 'ob' is a parent somewhere in par's parents */
2590 if (par == NULL) return false;
2591 if (ob == par) return true;
2592 return BKE_object_parent_loop_check(par->parent, ob);
2595 static void object_handle_update_proxy(Main *bmain,
2596 EvaluationContext *eval_ctx,
2599 const bool do_proxy_update)
2601 /* The case when this is a group proxy, object_update is called in group.c */
2602 if (object->proxy == NULL) {
2605 /* set pointer in library proxy target, for copying, but restore it */
2606 object->proxy->proxy_from = object;
2607 // printf("set proxy pointer for later group stuff %s\n", ob->id.name);
2609 /* the no-group proxy case, we call update */
2610 if (object->proxy_group == NULL) {
2611 if (do_proxy_update) {
2612 // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
2613 BKE_object_handle_update(bmain, eval_ctx, scene, object->proxy);
2618 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
2619 /* local_object->proxy == pointer to library object, saved in files and read */
2621 /* function below is polluted with proxy exceptions, cleanup will follow! */
2623 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
2624 /* requires flags to be set! */
2625 /* Ideally we shouldn't have to pass the rigid body world, but need bigger restructuring to avoid id */
2626 void BKE_object_handle_update_ex(Main *bmain,
2627 EvaluationContext *eval_ctx,
2628 Scene *scene, Object *ob,
2629 RigidBodyWorld *rbw,
2630 const bool do_proxy_update)
2632 if ((ob->recalc & OB_RECALC_ALL) == 0) {
2633 object_handle_update_proxy(bmain, eval_ctx, scene, ob, do_proxy_update);
2636 /* Speed optimization for animation lookups. */
2637 if (ob->pose != NULL) {
2638 BKE_pose_channels_hash_make(ob->pose);
2639 if (ob->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
2640 BKE_pose_update_constraint_flags(ob->pose);
2643 if (ob->recalc & OB_RECALC_DATA) {
2644 if (ob->type == OB_ARMATURE) {
2645 /* this happens for reading old files and to match library armatures
2646 * with poses we do it ahead of BKE_object_where_is_calc to ensure animation
2647 * is evaluated on the rebuilt pose, otherwise we get incorrect poses
2649 if (ob->pose == NULL || (ob->pose->flag & POSE_RECALC))
2650 BKE_pose_rebuild(ob, ob->data);
2653 /* XXX new animsys warning: depsgraph tag OB_RECALC_DATA should not skip drivers,
2654 * which is only in BKE_object_where_is_calc now */
2655 /* XXX: should this case be OB_RECALC_OB instead? */
2656 if (ob->recalc & OB_RECALC_ALL) {
2657 if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) {
2658 printf("recalcob %s\n", ob->id.name + 2);
2660 /* Handle proxy copy for target. */
2661 if (!BKE_object_eval_proxy_copy(eval_ctx, ob)) {
2662 BKE_object_where_is_calc_ex(scene, rbw, ob, NULL);
2666 if (ob->recalc & OB_RECALC_DATA) {
2667 BKE_object_handle_data_update(bmain, eval_ctx, scene, ob);
2670 ob->recalc &= ~OB_RECALC_ALL;
2672 object_handle_update_proxy(bmain, eval_ctx, scene, ob, do_proxy_update);
2675 /* WARNING: "scene" here may not be the scene object actually resides in.
2676 * When dealing with background-sets, "scene" is actually the active scene.
2677 * e.g. "scene" <-- set 1 <-- set 2 ("ob" lives here) <-- set 3 <-- ... <-- set n
2678 * rigid bodies depend on their world so use BKE_object_handle_update_ex() to also pass along the corrent rigid body world
2680 void BKE_object_handle_update(Main *bmain, EvaluationContext *eval_ctx, Scene *scene, Object *ob)
2682 BKE_object_handle_update_ex(bmain, eval_ctx, scene, ob, NULL, true);
2685 void BKE_object_sculpt_modifiers_changed(Object *ob)
2687 SculptSession *ss = ob->sculpt;
2689 if (ss && ss->building_vp_handle == false) {
2691 /* we free pbvh on changes, except during sculpt since it can't deal with
2692 * changing PVBH node organization, we hope topology does not change in
2693 * the meantime .. weak */
2695 BKE_pbvh_free(ss->pbvh);
2699 BKE_sculptsession_free_deformMats(ob->sculpt);
2701 /* In vertex/weight paint, force maps to be rebuilt. */
2702 BKE_sculptsession_free_vwpaint_data(ob->sculpt);
2708 BKE_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
2710 for (n = 0; n < totnode; n++)
2711 BKE_pbvh_node_mark_update(nodes[n]);
2718 int BKE_object_obdata_texspace_get(Object *ob, short **r_texflag, float **r_loc, float **r_size, float **r_rot)
2721 if (ob->data == NULL)
2724 switch (GS(((ID *)ob->data)->name)) {
2727 Mesh *me = ob->data;
2728 if (me->bb == NULL || (me->bb->flag & BOUNDBOX_DIRTY)) {
2729 BKE_mesh_texspace_calc(me);
2731 if (r_texflag) *r_texflag = &me->texflag;
2732 if (r_loc) *r_loc = me->loc;
2733 if (r_size) *r_size = me->size;
2734 if (r_rot) *r_rot = me->rot;
2739 Curve *cu = ob->data;
2740 if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_DIRTY)) {
2741 BKE_curve_texspace_calc(cu);
2743 if (r_texflag) *r_texflag = &cu->texflag;
2744 if (r_loc) *r_loc = cu->loc;
2745 if (r_size) *r_size = cu->size;
2746 if (r_rot) *r_rot = cu->rot;
2751 MetaBall *mb = ob->data;
2752 if (r_texflag) *r_texflag = &mb->texflag;
2753 if (r_loc) *r_loc = mb->loc;
2754 if (r_size) *r_size = mb->size;
2755 if (r_rot) *r_rot = mb->rot;
2764 static int pc_cmp(const void *a, const void *b)
2766 const LinkData *ad = a, *bd = b;
2767 if (GET_INT_FROM_POINTER(ad->data) > GET_INT_FROM_POINTER(bd->data))
2772 int BKE_object_insert_ptcache(Object *ob)
2774 LinkData *link = NULL;
2777 BLI_listbase_sort(&ob->pc_ids, pc_cmp);
2779 for (link = ob->pc_ids.first, i = 0; link; link = link->next, i++) {
2780 int index = GET_INT_FROM_POINTER(link->data);
2786 link = MEM_callocN(sizeof(LinkData), "PCLink");
2787 link->data = SET_INT_IN_POINTER(i);
2788 BLI_addtail(&ob->pc_ids, link);
2793 static int pc_findindex(ListBase *listbase, int index)
2795 LinkData *link = NULL;
2798 if (listbase == NULL) return -1;
2800 link = listbase->first;
2802 if (GET_INT_FROM_POINTER(link->data) == index)
2812 void BKE_object_delete_ptcache(Object *ob, int index)
2814 int list_index = pc_findindex(&ob->pc_ids, index);
2815 LinkData *link = BLI_findlink(&ob->pc_ids, list_index);
2816 BLI_freelinkN(&ob->pc_ids, link);
2819 /* shape key utility function */
2821 /************************* Mesh ************************/
2822 static KeyBlock *insert_meshkey(Main *bmain, Object *ob, const char *name, const bool from_mix)
2824 Mesh *me = ob->data;
2830 key = me->key = BKE_key_add(bmain, (ID *)me);
2831 key->type = KEY_RELATIVE;
2835 if (newkey || from_mix == false) {
2836 /* create from mesh */
2837 kb = BKE_keyblock_add_ctime(key, name, false);
2838 BKE_keyblock_convert_from_mesh(me, kb);
2841 /* copy from current values */
2843 float *data = BKE_key_evaluate_object(ob, &totelem);
2845 /* create new block with prepared data */
2846 kb = BKE_keyblock_add_ctime(key, name, false);
2848 kb->totelem = totelem;
2853 /************************* Lattice ************************/
2854 static KeyBlock *insert_lattkey(Main *bmain, Object *ob, const char *name, const bool from_mix)
2856 Lattice *lt = ob->data;
2862 key = lt->key = BKE_key_add(bmain, (ID *)lt);
2863 key->type = KEY_RELATIVE;
2867 if (newkey || from_mix == false) {
2868 kb = BKE_keyblock_add_ctime(key, name, false);
2870 KeyBlock *basekb = (KeyBlock *)key->block.first;
2871 kb->data = MEM_dupallocN(basekb->data);
2872 kb->totelem = basekb->totelem;
2875 BKE_keyblock_convert_from_lattice(lt, kb);
2879 /* copy from current values */
2881 float *data = BKE_key_evaluate_object(ob, &totelem);
2883 /* create new block with prepared data */
2884 kb = BKE_keyblock_add_ctime(key, name, false);
2885 kb->totelem = totelem;
2891 /************************* Curve ************************/
2892 static KeyBlock *insert_curvekey(Main *bmain, Object *ob, const char *name, const bool from_mix)
2894 Curve *cu = ob->data;
2897 ListBase *lb = BKE_curve_nurbs_get(cu);
2901 key = cu->key = BKE_key_add(bmain, (ID *)cu);
2902 key->type = KEY_RELATIVE;
2906 if (newkey || from_mix == false) {
2907 /* create from curve */
2908 kb = BKE_keyblock_add_ctime(key, name, false);
2910 KeyBlock *basekb = (KeyBlock *)key->block.first;
2911 kb->data = MEM_dupallocN(basekb->data);
2912 kb->totelem = basekb->totelem;
2915 BKE_keyblock_convert_from_curve(cu, kb, lb);
2919 /* copy from current values */
2921 float *data = BKE_key_evaluate_object(ob, &totelem);
2923 /* create new block with prepared data */
2924 kb = BKE_keyblock_add_ctime(key, name, false);
2925 kb->totelem = totelem;
2932 KeyBlock *BKE_object_shapekey_insert(Main *bmain, Object *ob, const char *name, const bool from_mix)
2936 return insert_meshkey(bmain, ob, name, from_mix);
2939 return insert_curvekey(bmain, ob, name, from_mix);
2941 return insert_lattkey(bmain, ob, name, from_mix);
2948 bool BKE_object_shapekey_free(Main *bmain, Object *ob)
2952 key_p = BKE_key_from_object_p(ob);
2953 if (ELEM(NULL, key_p, *key_p)) {
2960 BKE_libblock_free_us(bmain, key);
2965 bool BKE_object_shapekey_remove(Main *bmain, Object *ob, KeyBlock *kb)
2968 Key *key = BKE_key_from_object(ob);
2975 kb_index = BLI_findindex(&key->block, kb);
2976 BLI_assert(kb_index != -1);
2978 for (rkb = key->block.first; rkb; rkb = rkb->next) {
2979 if (rkb->relative == kb_index) {
2980 /* remap to the 'Basis' */
2983 else if (rkb->relative >= kb_index) {
2984 /* Fix positional shift of the keys when kb is deleted from the list */
2989 BLI_remlink(&key->block, kb);
2991 if (key->refkey == kb) {
2992 key->refkey = key->block.first;
2995 /* apply new basis key on original data */
2998 BKE_keyblock_convert_to_mesh(key->refkey, ob->data);
3002 BKE_keyblock_convert_to_curve(key->refkey, ob->data, BKE_curve_nurbs_get(ob->data));
3005 BKE_keyblock_convert_to_lattice(key->refkey, ob->data);
3012 MEM_freeN(kb->data);
3016 if (ob->shapenr > 1) {
3020 if (key->totkey == 0) {
3021 BKE_object_shapekey_free(bmain, ob);
3027 bool BKE_object_flag_test_recursive(const Object *ob, short flag)
3029 if (ob->flag & flag) {
3032 else if (ob->parent) {
3033 return BKE_object_flag_test_recursive(ob->parent, flag);
3040 bool BKE_object_is_child_recursive(const Object *ob_parent, const Object *ob_child)
3042 for (ob_child = ob_child->parent; ob_child; ob_child = ob_child->parent) {
3043 if (ob_child == ob_parent) {
3050 /* most important if this is modified it should _always_ return True, in certain
3051 * cases false positives are hard to avoid (shape keys for example) */
3052 int BKE_object_is_modified(Scene *scene, Object *ob)
3056 if (BKE_key_from_object(ob)) {
3057 flag |= eModifierMode_Render | eModifierMode_Realtime;
3061 VirtualModifierData virtualModifierData;
3063 for (md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
3064 md && (flag != (eModifierMode_Render | eModifierMode_Realtime));
3067 if ((flag & eModifierMode_Render) == 0 && modifier_isEnabled(scene, md, eModifierMode_Render))
3068 flag |= eModifierMode_Render;
3070 if ((flag & eModifierMode_Realtime) == 0 && modifier_isEnabled(scene, md, eModifierMode_Realtime))
3071 flag |= eModifierMode_Realtime;
3078 /* Check of objects moves in time. */
3079 /* NOTE: This function is currently optimized for usage in combination
3080 * with mti->canDeform, so modifiers can quickly check if their target
3081 * objects moves (causing deformation motion blur) or not.
3083 * This makes it possible to give some degree of false-positives here,
3084 * but it's currently an acceptable tradeoff between complexity and check
3085 * speed. In combination with checks of modifier stack and real life usage
3086 * percentage of false-positives shouldn't be that hight.
3088 static bool object_moves_in_time(Object *object)
3090 AnimData *adt = object->adt;
3092 /* If object has any sort of animation data assume it is moving. */
3093 if (adt->action != NULL ||
3094 !BLI_listbase_is_empty(&adt->nla_tracks) ||
3095 !BLI_listbase_is_empty(&adt->drivers) ||
3096 !BLI_listbase_is_empty(&adt->overrides))
3101 if (!BLI_listbase_is_empty(&object->constraints)) {
3104 if (object->parent != NULL) {
3105 /* TODO(sergey): Do recursive check here? */
3111 static bool object_deforms_in_time(Object *object)
3113 if (BKE_key_from_object(object) != NULL) {
3116 if (!BLI_listbase_is_empty(&object->modifiers)) {
3119 return object_moves_in_time(object);
3122 static bool constructive_modifier_is_deform_modified(ModifierData *md)
3124 /* TODO(sergey): Consider generalizing this a bit so all modifier logic
3125 * is concentrated in MOD_{modifier}.c file,
3127 if (md->type == eModifierType_Array) {
3128 ArrayModifierData *amd = (ArrayModifierData *)md;
3129 /* TODO(sergey): Check if curve is deformed. */
3130 return (amd->start_cap != NULL && object_moves_in_time(amd->start_cap)) ||
3131 (amd->end_cap != NULL && object_moves_in_time(amd->end_cap)) ||
3132 (amd->curve_ob != NULL && object_moves_in_time(amd->curve_ob)) ||
3133 (amd->offset_ob != NULL && object_moves_in_time(amd->offset_ob));
3135 else if (md->type == eModifierType_Mirror) {
3136 MirrorModifierData *mmd = (MirrorModifierData *)md;
3137 return mmd->mirror_ob != NULL && object_moves_in_time(mmd->mirror_ob);
3139 else if (md->type == eModifierType_Screw) {
3140 ScrewModifierData *smd = (ScrewModifierData *)md;
3141 return smd->ob_axis != NULL && object_moves_in_time(smd->ob_axis);
3143 else if (md->type == eModifierType_MeshSequenceCache) {
3144 /* NOTE: Not ideal because it's unknown whether topology changes or not.
3145 * This will be detected later, so by assuming it's only deformation
3146 * going on here we allow to bake deform-only mesh to Alembic and have
3147 * proper motion blur after that.
3154 static bool modifiers_has_animation_check(Object *ob)
3156 /* TODO(sergey): This is a bit code duplication with depsgraph, but
3157 * would be nicer to solve this as a part of new dependency graph
3158 * work, so we avoid conflicts and so.
3160 if (ob->adt != NULL) {
3161 AnimData *adt = ob->adt;
3163 if (adt->action != NULL) {
3164 for (fcu = adt->action->curves.first; fcu; fcu = fcu->next) {
3165 if (fcu->rna_path && strstr(fcu->rna_path, "modifiers[")) {
3170 for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
3171 if (fcu->rna_path && strstr(fcu->rna_path, "modifiers[")) {
3179 /* test if object is affected by deforming modifiers (for motion blur). again
3180 * most important is to avoid false positives, this is to skip computations
3181 * and we can still if there was actual deformation afterwards */
3182 int BKE_object_is_deform_modified(Scene *scene, Object *ob)
3185 VirtualModifierData virtualModifierData;
3187 const bool is_modifier_animated = modifiers_has_animation_check(ob);
3189 if (BKE_key_from_object(ob)) {
3190 flag |= eModifierMode_Realtime | eModifierMode_Render;
3193 if (ob->type == OB_CURVE) {
3194 Curve *cu = (Curve *)ob->data;
3195 if (cu->taperobj != NULL && object_deforms_in_time(cu->taperobj)) {
3196 flag |= eModifierMode_Realtime | eModifierMode_Render;
3201 for (md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
3202 md && (flag != (eModifierMode_Render | eModifierMode_Realtime));
3205 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
3206 bool can_deform = mti->type == eModifierTypeType_OnlyDeform ||
3207 is_modifier_animated;
3210 can_deform = constructive_modifier_is_deform_modified(md);
3214 if (!(flag & eModifierMode_Render) && modifier_isEnabled(scene, md, eModifierMode_Render))
3215 flag |= eModifierMode_Render;
3217 if (!(flag & eModifierMode_Realtime) && modifier_isEnabled(scene, md, eModifierMode_Realtime))
3218 flag |= eModifierMode_Realtime;
3225 /* See if an object is using an animated modifier */
3226 bool BKE_object_is_animated(Scene *scene, Object *ob)
3229 VirtualModifierData virtualModifierData;
3231 for (md = modifiers_getVirtualModifierList(ob, &virtualModifierData); md; md = md->next)
3232 if (modifier_dependsOnTime(md) &&
3233 (modifier_isEnabled(scene, md, eModifierMode_Realtime) ||
3234 modifier_isEnabled(scene, md, eModifierMode_Render)))
3241 MovieClip *BKE_object_movieclip_get(Scene *scene, Object *ob, bool use_default)
3243 MovieClip *clip = use_default ? scene->clip : NULL;
3244 bConstraint *con = ob->constraints.first, *scon = NULL;
3247 if (con->type == CONSTRAINT_TYPE_CAMERASOLVER) {
3248 if (scon == NULL || (scon->flag & CONSTRAINT_OFF))
3256 bCameraSolverConstraint *solver = scon->data;
3257 if ((solver->flag & CAMERASOLVER_ACTIVECLIP) == 0)
3258 clip = solver->clip;
3268 * Find an associated Armature object
3270 static Object *obrel_armature_find(Object *ob)
3272 Object *ob_arm = NULL;
3274 if (ob->parent && ob->partype == PARSKEL && ob->parent->type == OB_ARMATURE) {
3275 ob_arm = ob->parent;
3279 for (mod = (ModifierData *)ob->modifiers.first; mod; mod = mod->next) {