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_collection_types.h"
43 #include "DNA_constraint_types.h"
44 #include "DNA_gpencil_types.h"
45 #include "DNA_gpencil_modifier_types.h"
46 #include "DNA_key_types.h"
47 #include "DNA_lamp_types.h"
48 #include "DNA_lattice_types.h"
49 #include "DNA_material_types.h"
50 #include "DNA_meta_types.h"
51 #include "DNA_mesh_types.h"
52 #include "DNA_meshdata_types.h"
53 #include "DNA_movieclip_types.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_screen_types.h"
56 #include "DNA_sequence_types.h"
57 #include "DNA_shader_fx_types.h"
58 #include "DNA_smoke_types.h"
59 #include "DNA_space_types.h"
60 #include "DNA_view3d_types.h"
61 #include "DNA_world_types.h"
62 #include "DNA_object_types.h"
63 #include "DNA_lightprobe_types.h"
64 #include "DNA_rigidbody_types.h"
66 #include "BLI_blenlib.h"
68 #include "BLI_threads.h"
69 #include "BLI_utildefines.h"
70 #include "BLI_linklist.h"
71 #include "BLI_kdtree.h"
73 #include "BLT_translation.h"
77 #include "BKE_global.h"
78 #include "BKE_idprop.h"
79 #include "BKE_armature.h"
80 #include "BKE_action.h"
81 #include "BKE_deform.h"
82 #include "BKE_DerivedMesh.h"
83 #include "BKE_animsys.h"
85 #include "BKE_collection.h"
86 #include "BKE_constraint.h"
87 #include "BKE_curve.h"
88 #include "BKE_displist.h"
89 #include "BKE_effect.h"
90 #include "BKE_fcurve.h"
91 #include "BKE_gpencil_modifier.h"
92 #include "BKE_icons.h"
95 #include "BKE_layer.h"
96 #include "BKE_lattice.h"
97 #include "BKE_library.h"
98 #include "BKE_library_query.h"
99 #include "BKE_library_remap.h"
100 #include "BKE_linestyle.h"
101 #include "BKE_mesh.h"
102 #include "BKE_editmesh.h"
103 #include "BKE_mball.h"
104 #include "BKE_modifier.h"
105 #include "BKE_multires.h"
106 #include "BKE_node.h"
107 #include "BKE_object.h"
108 #include "BKE_object_facemap.h"
109 #include "BKE_paint.h"
110 #include "BKE_particle.h"
111 #include "BKE_pointcache.h"
112 #include "BKE_lightprobe.h"
113 #include "BKE_rigidbody.h"
114 #include "BKE_scene.h"
115 #include "BKE_sequencer.h"
116 #include "BKE_shader_fx.h"
117 #include "BKE_speaker.h"
118 #include "BKE_softbody.h"
119 #include "BKE_subsurf.h"
120 #include "BKE_subdiv_ccg.h"
121 #include "BKE_material.h"
122 #include "BKE_camera.h"
123 #include "BKE_image.h"
124 #include "BKE_gpencil.h"
126 #include "DEG_depsgraph.h"
127 #include "DEG_depsgraph_query.h"
129 #include "DRW_engine.h"
131 #ifdef WITH_MOD_FLUID
132 #include "LBM_fluidsim.h"
136 #include "BPY_extern.h"
139 #include "CCGSubSurf.h"
140 #include "atomic_ops.h"
142 /* Vertex parent modifies original BMesh which is not safe for threading.
143 * Ideally such a modification should be handled as a separate DAG update
144 * callback for mesh datablock, but for until it is actually supported use
145 * simpler solution with a mutex lock.
148 #define VPARENT_THREADING_HACK
150 #ifdef VPARENT_THREADING_HACK
151 static ThreadMutex vparent_lock = BLI_MUTEX_INITIALIZER;
154 void BKE_object_workob_clear(Object *workob)
156 memset(workob, 0, sizeof(Object));
158 workob->size[0] = workob->size[1] = workob->size[2] = 1.0f;
159 workob->dscale[0] = workob->dscale[1] = workob->dscale[2] = 1.0f;
160 workob->rotmode = ROT_MODE_EUL;
163 void BKE_object_free_particlesystems(Object *ob)
165 ParticleSystem *psys;
167 while ((psys = BLI_pophead(&ob->particlesystem))) {
172 void BKE_object_free_softbody(Object *ob)
177 void BKE_object_free_curve_cache(Object *ob)
179 if (ob->runtime.curve_cache) {
180 BKE_displist_free(&ob->runtime.curve_cache->disp);
181 BKE_curve_bevelList_free(&ob->runtime.curve_cache->bev);
182 if (ob->runtime.curve_cache->path) {
183 free_path(ob->runtime.curve_cache->path);
185 BKE_nurbList_free(&ob->runtime.curve_cache->deformed_nurbs);
186 MEM_freeN(ob->runtime.curve_cache);
187 ob->runtime.curve_cache = NULL;
191 void BKE_object_free_modifiers(Object *ob, const int flag)
194 GpencilModifierData *gp_md;
196 while ((md = BLI_pophead(&ob->modifiers))) {
197 modifier_free_ex(md, flag);
200 while ((gp_md = BLI_pophead(&ob->greasepencil_modifiers))) {
201 BKE_gpencil_modifier_free_ex(gp_md, flag);
203 /* particle modifiers were freed, so free the particlesystems as well */
204 BKE_object_free_particlesystems(ob);
206 /* same for softbody */
207 BKE_object_free_softbody(ob);
209 /* modifiers may have stored data in the DM cache */
210 BKE_object_free_derived_caches(ob);
213 void BKE_object_free_shaderfx(Object *ob, const int flag)
217 while ((fx = BLI_pophead(&ob->shader_fx))) {
218 BKE_shaderfx_free_ex(fx, flag);
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 void BKE_object_modifier_gpencil_hook_reset(Object *ob, HookGpencilModifierData *hmd)
246 if (hmd->object == NULL) {
249 /* reset functionality */
250 bPoseChannel *pchan = BKE_pose_channel_find_name(hmd->object->pose, hmd->subtarget);
252 if (hmd->subtarget[0] && pchan) {
253 float imat[4][4], mat[4][4];
255 /* calculate the world-space matrix for the pose-channel target first, then carry on as usual */
256 mul_m4_m4m4(mat, hmd->object->obmat, pchan->pose_mat);
258 invert_m4_m4(imat, mat);
259 mul_m4_m4m4(hmd->parentinv, imat, ob->obmat);
262 invert_m4_m4(hmd->object->imat, hmd->object->obmat);
263 mul_m4_m4m4(hmd->parentinv, hmd->object->imat, ob->obmat);
267 bool BKE_object_support_modifier_type_check(const Object *ob, int modifier_type)
269 const ModifierTypeInfo *mti;
271 mti = modifierType_getInfo(modifier_type);
273 /* only geometry objects should be able to get modifiers [#25291] */
274 if (!ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
278 if (ob->type == OB_LATTICE && (mti->flags & eModifierTypeFlag_AcceptsLattice) == 0) {
282 if (!((mti->flags & eModifierTypeFlag_AcceptsCVs) ||
283 (ob->type == OB_MESH && (mti->flags & eModifierTypeFlag_AcceptsMesh))))
291 void BKE_object_link_modifiers(Scene *scene, struct Object *ob_dst, const struct Object *ob_src)
294 BKE_object_free_modifiers(ob_dst, 0);
296 if (!ELEM(ob_dst->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
297 /* only objects listed above can have modifiers and linking them to objects
298 * which doesn't have modifiers stack is quite silly */
302 for (md = ob_src->modifiers.first; md; md = md->next) {
303 ModifierData *nmd = NULL;
307 eModifierType_Collision))
312 if (!BKE_object_support_modifier_type_check(ob_dst, md->type))
316 case eModifierType_Softbody:
317 BKE_object_copy_softbody(ob_dst, ob_src, 0);
319 case eModifierType_Skin:
320 /* ensure skin-node customdata exists */
321 BKE_mesh_ensure_skin_customdata(ob_dst->data);
325 nmd = modifier_new(md->type);
326 BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
328 if (md->type == eModifierType_Multires) {
329 /* Has to be done after mod creation, but *before* we actually copy its settings! */
330 multiresModifier_sync_levels_ex(scene, ob_dst, (MultiresModifierData *)md, (MultiresModifierData *)nmd);
333 modifier_copyData(md, nmd);
334 BLI_addtail(&ob_dst->modifiers, nmd);
335 modifier_unique_name(&ob_dst->modifiers, nmd);
338 BKE_object_copy_particlesystems(ob_dst, ob_src, 0);
340 /* TODO: smoke?, cloth? */
343 /* Copy CCG related data. Used to sync copy of mesh with reshaped original
346 static void copy_ccg_data(Mesh *mesh_destination,
350 BLI_assert(mesh_destination->totloop == mesh_source->totloop);
351 CustomData *data_destination = &mesh_destination->ldata;
352 CustomData *data_source = &mesh_source->ldata;
353 const int num_elements = mesh_source->totloop;
354 if (!CustomData_has_layer(data_source, layer_type)) {
357 const int layer_index = CustomData_get_layer_index(
358 data_destination, layer_type);
359 CustomData_free_layer(
360 data_destination, layer_type, num_elements, layer_index);
361 BLI_assert(!CustomData_has_layer(data_destination, layer_type));
362 CustomData_add_layer(
363 data_destination, layer_type, CD_CALLOC, NULL, num_elements);
364 BLI_assert(CustomData_has_layer(data_destination, layer_type));
365 CustomData_copy_layer_type_data(data_source, data_destination,
366 layer_type, 0, 0, num_elements);
369 static void object_update_from_subsurf_ccg(Object *object)
371 /* Currently CCG is only created for Mesh objects. */
372 if (object->type != OB_MESH) {
375 /* Object was never evaluated, so can not have CCG subdivision surface. */
376 Mesh *mesh_eval = object->runtime.mesh_eval;
377 if (mesh_eval == NULL) {
380 SubdivCCG *subdiv_ccg = mesh_eval->runtime.subdiv_ccg;
381 if (subdiv_ccg == NULL) {
384 /* Check whether there is anything to be reshaped. */
385 if (!subdiv_ccg->dirty.coords && !subdiv_ccg->dirty.hidden) {
388 const int tot_level = mesh_eval->runtime.subdiv_ccg_tot_level;
389 Object *object_orig = DEG_get_original_object(object);
390 Mesh *mesh_orig = (Mesh *)object_orig->data;
391 multiresModifier_reshapeFromCCG(tot_level, mesh_orig, subdiv_ccg);
392 /* NOTE: we need to reshape into an original mesh from main database,
395 * - Update copies of that mesh at any moment.
396 * - Save the file without doing extra reshape.
397 * - All the users of the mesh have updated displacement.
399 * However, the tricky part here is that we only know about sculpted
400 * state of a mesh on an object level, and object is being updated after
401 * mesh datablock is updated. This forces us to:
403 * - Update mesh datablock from object evaluation, which is technically
404 * forbidden, but there is no other place for this yet.
405 * - Reshape to the original mesh from main database, and then copy updated
406 * layer to copy of that mesh (since copy of the mesh has decoupled
407 * custom data layers).
409 * All this is defeating all the designs we need to follow to allow safe
410 * threaded evaluation, but this is as good as we can make it within the
411 * current sculpt//evaluated mesh design. This is also how we've survived
412 * with old DerivedMesh based solutions. So, while this is all wrong and
413 * needs reconsideration, doesn't seem to be a big stopper for real
414 * production artists.
416 /* TODO(sergey): Solve this somehow, to be fully stable for threaded
417 * evaluation environment.
419 /* NOTE: runtime.mesh_orig is what was before assigning mesh_eval,
420 * it is orig as in what was in object_eval->data before evaluating
423 * mesh_cow is a copy-on-written version od object_orig->data.
425 Mesh *mesh_cow = object->runtime.mesh_orig;
426 copy_ccg_data(mesh_cow, mesh_orig, CD_MDISPS);
427 copy_ccg_data(mesh_cow, mesh_orig, CD_GRID_PAINT_MASK);
428 /* Everything is now up-to-date. */
429 subdiv_ccg->dirty.coords = false;
430 subdiv_ccg->dirty.hidden = false;
433 /* free data derived from mesh, called when mesh changes or is freed */
434 void BKE_object_free_derived_caches(Object *ob)
436 /* Also serves as signal to remake texspace.
438 * NOTE: This function can be called from threads on different objects
439 * sharing same data datablock. So we need to ensure atomic nature of
440 * data modification here.
442 if (ob->type == OB_MESH) {
446 atomic_fetch_and_or_int32(&me->bb->flag, BOUNDBOX_DIRTY);
449 else if (ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) {
450 Curve *cu = ob->data;
453 atomic_fetch_and_or_int32(&cu->bb->flag, BOUNDBOX_DIRTY);
462 object_update_from_subsurf_ccg(ob);
463 BKE_object_free_derived_mesh_caches(ob);
464 BKE_armature_cached_bbone_deformation_free(ob);
466 if (ob->runtime.mesh_eval != NULL) {
467 Mesh *mesh_eval = ob->runtime.mesh_eval;
468 /* Restore initial pointer. */
469 if (ob->data == mesh_eval) {
470 ob->data = ob->runtime.mesh_orig;
472 /* Evaluated mesh points to edit mesh, but does not own it. */
473 mesh_eval->edit_btmesh = NULL;
474 BKE_mesh_free(mesh_eval);
475 BKE_libblock_free_data(&mesh_eval->id, false);
476 MEM_freeN(mesh_eval);
477 ob->runtime.mesh_eval = NULL;
479 if (ob->runtime.mesh_deform_eval != NULL) {
480 Mesh *mesh_deform_eval = ob->runtime.mesh_deform_eval;
481 BKE_mesh_free(mesh_deform_eval);
482 BKE_libblock_free_data(&mesh_deform_eval->id, false);
483 MEM_freeN(mesh_deform_eval);
484 ob->runtime.mesh_deform_eval = NULL;
487 BKE_object_free_curve_cache(ob);
489 /* clear grease pencil data */
490 DRW_gpencil_freecache(ob);
493 void BKE_object_free_derived_mesh_caches(struct Object *ob)
495 if (ob->derivedFinal) {
496 ob->derivedFinal->needsFree = 1;
497 ob->derivedFinal->release(ob->derivedFinal);
498 ob->derivedFinal = NULL;
500 if (ob->derivedDeform) {
501 ob->derivedDeform->needsFree = 1;
502 ob->derivedDeform->release(ob->derivedDeform);
503 ob->derivedDeform = NULL;
507 void BKE_object_free_caches(Object *object)
510 short update_flag = 0;
512 /* Free particle system caches holding paths. */
513 if (object->particlesystem.first) {
514 ParticleSystem *psys;
515 for (psys = object->particlesystem.first;
519 psys_free_path_cache(psys, psys->edit);
520 update_flag |= ID_RECALC_PSYS_REDO;
524 /* Free memory used by cached derived meshes in the particle system modifiers. */
525 for (md = object->modifiers.first; md != NULL; md = md->next) {
526 if (md->type == eModifierType_ParticleSystem) {
527 ParticleSystemModifierData *psmd = (ParticleSystemModifierData *) md;
528 if (psmd->mesh_final) {
529 BKE_id_free(NULL, psmd->mesh_final);
530 psmd->mesh_final = NULL;
531 if (psmd->mesh_original) {
532 BKE_id_free(NULL, psmd->mesh_original);
533 psmd->mesh_original = NULL;
535 psmd->flag |= eParticleSystemFlag_file_loaded;
536 update_flag |= ID_RECALC_GEOMETRY;
541 /* NOTE: If object is coming from a duplicator, it might be a temporary
542 * object created by dependency graph, which shares pointers with original
543 * object. In this case we can not free anything.
545 if ((object->base_flag & BASE_FROMDUPLI) == 0) {
546 BKE_object_free_derived_caches(object);
547 update_flag |= ID_RECALC_GEOMETRY;
550 /* Tag object for update, so once memory critical operation is over and
551 * scene update routines are back to it's business the object will be
552 * guaranteed to be in a known state.
554 if (update_flag != 0) {
555 DEG_id_tag_update(&object->id, update_flag);
559 /** Free (or release) any data used by this object (does not free the object itself). */
560 void BKE_object_free(Object *ob)
562 BKE_animdata_free((ID *)ob, false);
564 DRW_drawdata_free((ID *)ob);
566 /* BKE_<id>_free shall never touch to ID->us. Never ever. */
567 BKE_object_free_modifiers(ob, LIB_ID_CREATE_NO_USER_REFCOUNT);
568 BKE_object_free_shaderfx(ob, LIB_ID_CREATE_NO_USER_REFCOUNT);
570 MEM_SAFE_FREE(ob->mat);
571 MEM_SAFE_FREE(ob->matbits);
572 MEM_SAFE_FREE(ob->iuser);
573 MEM_SAFE_FREE(ob->bb);
575 BLI_freelistN(&ob->defbase);
576 BLI_freelistN(&ob->fmaps);
578 BKE_pose_free_ex(ob->pose, false);
582 animviz_free_motionpath(ob->mpath);
586 BKE_constraints_free_ex(&ob->constraints, false);
588 BKE_partdeflect_free(ob->pd);
589 BKE_rigidbody_free_object(ob, NULL);
590 BKE_rigidbody_free_constraint(ob);
594 BKE_sculptsession_free(ob);
596 BLI_freelistN(&ob->pc_ids);
598 BLI_freelistN(&ob->lodlevels);
600 /* Free runtime curves data. */
601 if (ob->runtime.curve_cache) {
602 BKE_curve_bevelList_free(&ob->runtime.curve_cache->bev);
603 if (ob->runtime.curve_cache->path)
604 free_path(ob->runtime.curve_cache->path);
605 MEM_freeN(ob->runtime.curve_cache);
606 ob->runtime.curve_cache = NULL;
609 BKE_previewimg_free(&ob->preview);
612 /* actual check for internal data, not context or flags */
613 bool BKE_object_is_in_editmode(const Object *ob)
615 if (ob->data == NULL) {
621 return ((Mesh *)ob->data)->edit_btmesh != NULL;
623 return ((bArmature *)ob->data)->edbo != NULL;
625 return ((Curve *)ob->data)->editfont != NULL;
627 return ((MetaBall *)ob->data)->editelems != NULL;
629 return ((Lattice *)ob->data)->editlatt != NULL;
632 return ((Curve *)ob->data)->editnurb != NULL;
638 bool BKE_object_is_in_editmode_vgroup(const Object *ob)
640 return (OB_TYPE_SUPPORT_VGROUP(ob->type) &&
641 BKE_object_is_in_editmode(ob));
644 bool BKE_object_data_is_in_editmode(const ID *id)
646 const short type = GS(id->name);
647 BLI_assert(OB_DATA_SUPPORT_EDITMODE(type));
650 return ((const Mesh *)id)->edit_btmesh != NULL;
653 (((const Curve *)id)->editnurb != NULL) ||
654 (((const Curve *)id)->editfont != NULL)
657 return ((const MetaBall *)id)->editelems != NULL;
659 return ((const Lattice *)id)->editlatt != NULL;
661 return ((const bArmature *)id)->edbo != NULL;
668 bool BKE_object_is_in_wpaint_select_vert(const Object *ob)
670 if (ob->type == OB_MESH) {
672 return ((ob->mode & OB_MODE_WEIGHT_PAINT) &&
673 (me->edit_btmesh == NULL) &&
674 (ME_EDIT_PAINT_SEL_MODE(me) == SCE_SELECT_VERTEX));
680 bool BKE_object_has_mode_data(const struct Object *ob, eObjectMode object_mode)
682 if (object_mode & OB_MODE_EDIT) {
683 if (BKE_object_is_in_editmode(ob)) {
687 else if (object_mode & OB_MODE_VERTEX_PAINT) {
688 if (ob->sculpt && (ob->sculpt->mode_type == OB_MODE_VERTEX_PAINT)) {
692 else if (object_mode & OB_MODE_WEIGHT_PAINT) {
693 if (ob->sculpt && (ob->sculpt->mode_type == OB_MODE_WEIGHT_PAINT)) {
697 else if (object_mode & OB_MODE_SCULPT) {
698 if (ob->sculpt && (ob->sculpt->mode_type == OB_MODE_SCULPT)) {
702 else if (object_mode & OB_MODE_POSE) {
703 if (ob->pose != NULL) {
710 bool BKE_object_is_mode_compat(const struct Object *ob, eObjectMode object_mode)
712 return ((ob->mode == object_mode) ||
713 (ob->mode & object_mode) != 0);
717 * Return which parts of the object are visible, as evaluated by depsgraph
719 int BKE_object_visibility(const Object *ob, const int dag_eval_mode)
721 if ((ob->base_flag & BASE_VISIBLE) == 0) {
725 /* Test which components the object has. */
726 int visibility = OB_VISIBLE_SELF;
727 if (ob->particlesystem.first) {
728 visibility |= OB_VISIBLE_INSTANCES | OB_VISIBLE_PARTICLES;
730 else if (ob->transflag & OB_DUPLI) {
731 visibility |= OB_VISIBLE_INSTANCES;
734 /* Optional hiding of self if there are particles or instancers. */
735 if (visibility & (OB_VISIBLE_PARTICLES | OB_VISIBLE_INSTANCES)) {
736 switch ((eEvaluationMode)dag_eval_mode) {
737 case DAG_EVAL_VIEWPORT:
738 if (!(ob->duplicator_visibility_flag & OB_DUPLI_FLAG_VIEWPORT)) {
739 visibility &= ~OB_VISIBLE_SELF;
742 case DAG_EVAL_RENDER:
743 if (!(ob->duplicator_visibility_flag & OB_DUPLI_FLAG_RENDER)) {
744 visibility &= ~OB_VISIBLE_SELF;
753 bool BKE_object_exists_check(Main *bmain, const Object *obtest)
757 if (obtest == NULL) return false;
759 ob = bmain->object.first;
761 if (ob == obtest) return true;
767 /* *************************************************** */
769 static const char *get_obdata_defname(int type)
772 case OB_MESH: return DATA_("Mesh");
773 case OB_CURVE: return DATA_("Curve");
774 case OB_SURF: return DATA_("Surf");
775 case OB_FONT: return DATA_("Text");
776 case OB_MBALL: return DATA_("Mball");
777 case OB_CAMERA: return DATA_("Camera");
778 case OB_LAMP: return DATA_("Light");
779 case OB_LATTICE: return DATA_("Lattice");
780 case OB_ARMATURE: return DATA_("Armature");
781 case OB_SPEAKER: return DATA_("Speaker");
782 case OB_EMPTY: return DATA_("Empty");
783 case OB_GPENCIL: return DATA_("GPencil");
785 printf("get_obdata_defname: Internal error, bad type: %d\n", type);
786 return DATA_("Empty");
790 void *BKE_object_obdata_add_from_type(Main *bmain, int type, const char *name)
793 name = get_obdata_defname(type);
797 case OB_MESH: return BKE_mesh_add(bmain, name);
798 case OB_CURVE: return BKE_curve_add(bmain, name, OB_CURVE);
799 case OB_SURF: return BKE_curve_add(bmain, name, OB_SURF);
800 case OB_FONT: return BKE_curve_add(bmain, name, OB_FONT);
801 case OB_MBALL: return BKE_mball_add(bmain, name);
802 case OB_CAMERA: return BKE_camera_add(bmain, name);
803 case OB_LAMP: return BKE_lamp_add(bmain, name);
804 case OB_LATTICE: return BKE_lattice_add(bmain, name);
805 case OB_ARMATURE: return BKE_armature_add(bmain, name);
806 case OB_SPEAKER: return BKE_speaker_add(bmain, name);
807 case OB_LIGHTPROBE:return BKE_lightprobe_add(bmain, name);
808 case OB_GPENCIL: return BKE_gpencil_data_addnew(bmain, name);
809 case OB_EMPTY: return NULL;
811 printf("%s: Internal error, bad type: %d\n", __func__, type);
816 void BKE_object_init(Object *ob)
818 /* BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(ob, id)); */ /* ob->type is already initialized... */
820 ob->col[0] = ob->col[1] = ob->col[2] = 1.0;
823 ob->size[0] = ob->size[1] = ob->size[2] = 1.0;
824 ob->dscale[0] = ob->dscale[1] = ob->dscale[2] = 1.0;
826 /* objects should default to having Euler XYZ rotations,
827 * but rotations default to quaternions
829 ob->rotmode = ROT_MODE_EUL;
831 unit_axis_angle(ob->rotAxis, &ob->rotAngle);
832 unit_axis_angle(ob->drotAxis, &ob->drotAngle);
837 /* rotation locks should be 4D for 4 component rotations by default... */
838 ob->protectflag = OB_LOCK_ROT4D;
840 unit_m4(ob->constinv);
841 unit_m4(ob->parentinv);
844 ob->empty_drawtype = OB_PLAINAXES;
845 ob->empty_drawsize = 1.0;
846 ob->empty_image_depth = OB_EMPTY_IMAGE_DEPTH_DEFAULT;
847 if (ob->type == OB_EMPTY) {
848 copy_v2_fl(ob->ima_ofs, -0.5f);
851 if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
852 ob->trackflag = OB_NEGZ;
853 ob->upflag = OB_POSY;
856 ob->trackflag = OB_POSY;
857 ob->upflag = OB_POSZ;
860 ob->dupon = 1; ob->dupoff = 0;
861 ob->dupsta = 1; ob->dupend = 100;
862 ob->dupfacesca = 1.0;
864 ob->col_group = 0x01;
865 ob->col_mask = 0xffff;
867 ob->duplicator_visibility_flag = OB_DUPLI_FLAG_VIEWPORT | OB_DUPLI_FLAG_RENDER;
869 /* NT fluid sim defaults */
870 ob->fluidsimSettings = NULL;
872 BLI_listbase_clear(&ob->pc_ids);
874 /* Animation Visualization defaults */
875 animviz_settings_init(&ob->avs);
877 ob->display.flag = OB_SHOW_SHADOW;
880 /* more general add: creates minimum required data, but without vertices etc. */
881 Object *BKE_object_add_only_object(Main *bmain, int type, const char *name)
886 name = get_obdata_defname(type);
888 ob = BKE_libblock_alloc(bmain, ID_OB, name, 0);
890 /* We increase object user count when linking to Collections. */
893 /* default object vars */
902 static Object *object_add_common(Main *bmain, ViewLayer *view_layer, int type, const char *name)
906 ob = BKE_object_add_only_object(bmain, type, name);
907 ob->data = BKE_object_obdata_add_from_type(bmain, type, name);
908 BKE_view_layer_base_deselect_all(view_layer);
910 DEG_id_tag_update_ex(bmain, &ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
915 * General add: to scene, with layer from area and default name
917 * Object is added to the active Collection.
918 * If there is no linked collection to the active ViewLayer we create a new one.
920 /* creates minimum required data, but without vertices etc. */
921 Object *BKE_object_add(
922 Main *bmain, Scene *UNUSED(scene), ViewLayer *view_layer,
923 int type, const char *name)
927 LayerCollection *layer_collection;
929 ob = object_add_common(bmain, view_layer, type, name);
931 layer_collection = BKE_layer_collection_get_active(view_layer);
932 BKE_collection_object_add(bmain, layer_collection->collection, ob);
934 base = BKE_view_layer_base_find(view_layer, ob);
935 BKE_view_layer_base_select_and_set_active(view_layer, base);
941 * Add a new object, using another one as a reference
943 * \param ob_src: object to use to determine the collections of the new object.
945 Object *BKE_object_add_from(
946 Main *bmain, Scene *scene, ViewLayer *view_layer,
947 int type, const char *name, Object *ob_src)
952 ob = object_add_common(bmain, view_layer, type, name);
953 BKE_collection_object_add_from(bmain, scene, ob_src, ob);
955 base = BKE_view_layer_base_find(view_layer, ob);
956 BKE_view_layer_base_select_and_set_active(view_layer, base);
962 * Add a new object, but assign the given datablock as the ob->data
963 * for the newly created object.
965 * \param data: The datablock to assign as ob->data for the new object.
966 * This is assumed to be of the correct type.
967 * \param do_id_user: If true, id_us_plus() will be called on data when
968 * assigning it to the object.
970 Object *BKE_object_add_for_data(
971 Main *bmain, ViewLayer *view_layer,
972 int type, const char *name, ID *data, bool do_id_user)
976 LayerCollection *layer_collection;
978 /* same as object_add_common, except we don't create new ob->data */
979 ob = BKE_object_add_only_object(bmain, type, name);
981 if (do_id_user) id_us_plus(data);
983 BKE_view_layer_base_deselect_all(view_layer);
984 DEG_id_tag_update_ex(bmain, &ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
986 layer_collection = BKE_layer_collection_get_active(view_layer);
987 BKE_collection_object_add(bmain, layer_collection->collection, ob);
989 base = BKE_view_layer_base_find(view_layer, ob);
990 BKE_view_layer_base_select_and_set_active(view_layer, base);
996 void BKE_object_copy_softbody(struct Object *ob_dst, const struct Object *ob_src, const int flag)
998 SoftBody *sb = ob_src->soft;
1000 bool tagged_no_main = ob_dst->id.tag & LIB_TAG_NO_MAIN;
1002 ob_dst->softflag = ob_src->softflag;
1004 ob_dst->soft = NULL;
1008 sbn = MEM_dupallocN(sb);
1010 if ((flag & LIB_ID_COPY_CACHES) == 0) {
1011 sbn->totspring = sbn->totpoint = 0;
1013 sbn->bspring = NULL;
1016 sbn->totspring = sb->totspring;
1017 sbn->totpoint = sb->totpoint;
1022 sbn->bpoint = MEM_dupallocN(sbn->bpoint);
1024 for (i = 0; i < sbn->totpoint; i++) {
1025 if (sbn->bpoint[i].springs)
1026 sbn->bpoint[i].springs = MEM_dupallocN(sbn->bpoint[i].springs);
1031 sbn->bspring = MEM_dupallocN(sb->bspring);
1035 sbn->totkey = sbn->totpointkey = 0;
1037 sbn->scratch = NULL;
1039 if (tagged_no_main == 0) {
1040 sbn->shared = MEM_dupallocN(sb->shared);
1041 sbn->shared->pointcache = BKE_ptcache_copy_list(&sbn->shared->ptcaches, &sb->shared->ptcaches, flag);
1044 if (sb->effector_weights)
1045 sbn->effector_weights = MEM_dupallocN(sb->effector_weights);
1050 ParticleSystem *BKE_object_copy_particlesystem(ParticleSystem *psys, const int flag)
1052 ParticleSystem *psysn = MEM_dupallocN(psys);
1054 psys_copy_particles(psysn, psys);
1057 psysn->clmd = (ClothModifierData *)modifier_new(eModifierType_Cloth);
1058 modifier_copyData_ex((ModifierData *)psys->clmd, (ModifierData *)psysn->clmd, flag);
1059 psys->hair_in_mesh = psys->hair_out_mesh = NULL;
1062 BLI_duplicatelist(&psysn->targets, &psys->targets);
1064 psysn->pathcache = NULL;
1065 psysn->childcache = NULL;
1068 psysn->effectors = NULL;
1070 psysn->bvhtree = NULL;
1071 psysn->batch_cache = NULL;
1073 BLI_listbase_clear(&psysn->pathcachebufs);
1074 BLI_listbase_clear(&psysn->childcachebufs);
1076 if (flag & LIB_ID_CREATE_NO_MAIN) {
1077 BLI_assert((psys->flag & PSYS_SHARED_CACHES) == 0);
1078 psysn->flag |= PSYS_SHARED_CACHES;
1079 BLI_assert(psysn->pointcache != NULL);
1082 psysn->pointcache = BKE_ptcache_copy_list(&psysn->ptcaches, &psys->ptcaches, flag);
1085 /* XXX - from reading existing code this seems correct but intended usage of
1086 * pointcache should /w cloth should be added in 'ParticleSystem' - campbell */
1088 psysn->clmd->point_cache = psysn->pointcache;
1091 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
1092 id_us_plus((ID *)psysn->part);
1098 void BKE_object_copy_particlesystems(Object *ob_dst, const Object *ob_src, const int flag)
1100 ParticleSystem *psys, *npsys;
1103 if (ob_dst->type != OB_MESH) {
1104 /* currently only mesh objects can have soft body */
1108 BLI_listbase_clear(&ob_dst->particlesystem);
1109 for (psys = ob_src->particlesystem.first; psys; psys = psys->next) {
1110 npsys = BKE_object_copy_particlesystem(psys, flag);
1112 BLI_addtail(&ob_dst->particlesystem, npsys);
1114 /* need to update particle modifiers too */
1115 for (md = ob_dst->modifiers.first; md; md = md->next) {
1116 if (md->type == eModifierType_ParticleSystem) {
1117 ParticleSystemModifierData *psmd = (ParticleSystemModifierData *)md;
1118 if (psmd->psys == psys)
1121 else if (md->type == eModifierType_DynamicPaint) {
1122 DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
1124 if (pmd->brush->psys == psys) {
1125 pmd->brush->psys = npsys;
1129 else if (md->type == eModifierType_Smoke) {
1130 SmokeModifierData *smd = (SmokeModifierData *) md;
1132 if (smd->type == MOD_SMOKE_TYPE_FLOW) {
1134 if (smd->flow->psys == psys)
1135 smd->flow->psys = npsys;
1143 static void copy_object_pose(Object *obn, const Object *ob, const int flag)
1147 /* note: need to clear obn->pose pointer first, so that BKE_pose_copy_data works (otherwise there's a crash) */
1149 BKE_pose_copy_data_ex(&obn->pose, ob->pose, flag, true); /* true = copy constraints */
1151 for (chan = obn->pose->chanbase.first; chan; chan = chan->next) {
1154 chan->flag &= ~(POSE_LOC | POSE_ROT | POSE_SIZE);
1156 /* XXX Remapping object pointing onto itself should be handled by generic BKE_library_remap stuff, but...
1157 * the flush_constraint_targets callback am not sure about, so will delay that for now. */
1158 for (con = chan->constraints.first; con; con = con->next) {
1159 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
1160 ListBase targets = {NULL, NULL};
1161 bConstraintTarget *ct;
1163 if (cti && cti->get_constraint_targets) {
1164 cti->get_constraint_targets(con, &targets);
1166 for (ct = targets.first; ct; ct = ct->next) {
1171 if (cti->flush_constraint_targets)
1172 cti->flush_constraint_targets(con, &targets, 0);
1178 static void copy_object_lod(Object *obn, const Object *ob, const int UNUSED(flag))
1180 BLI_duplicatelist(&obn->lodlevels, &ob->lodlevels);
1182 obn->currentlod = (LodLevel *)obn->lodlevels.first;
1185 bool BKE_object_pose_context_check(const Object *ob)
1188 (ob->type == OB_ARMATURE) &&
1190 (ob->mode & OB_MODE_POSE))
1199 Object *BKE_object_pose_armature_get(Object *ob)
1204 if (BKE_object_pose_context_check(ob))
1207 ob = modifiers_isDeformedByArmature(ob);
1209 /* Only use selected check when non-active. */
1210 if (BKE_object_pose_context_check(ob))
1216 Object *BKE_object_pose_armature_get_visible(Object *ob, ViewLayer *view_layer, View3D *v3d)
1218 Object *ob_armature = BKE_object_pose_armature_get(ob);
1220 Base *base = BKE_view_layer_base_find(view_layer, ob_armature);
1222 if (BASE_VISIBLE(v3d, base)) {
1231 * Access pose array with special check to get pose object when in weight paint mode.
1233 Object **BKE_object_pose_array_get_ex(ViewLayer *view_layer, View3D *v3d, uint *r_objects_len, bool unique)
1235 Object *ob_active = OBACT(view_layer);
1236 Object *ob_pose = BKE_object_pose_armature_get(ob_active);
1237 Object **objects = NULL;
1238 if (ob_pose == ob_active) {
1239 objects = BKE_view_layer_array_from_objects_in_mode(
1240 view_layer, v3d, r_objects_len, {
1241 .object_mode = OB_MODE_POSE,
1242 .no_dup_data = unique});
1244 else if (ob_pose != NULL) {
1246 objects = MEM_mallocN(sizeof(*objects), __func__);
1247 objects[0] = ob_pose;
1251 objects = MEM_mallocN(0, __func__);
1255 Object **BKE_object_pose_array_get_unique(ViewLayer *view_layer, View3D *v3d, uint *r_objects_len)
1257 return BKE_object_pose_array_get_ex(view_layer, v3d, r_objects_len, true);
1259 Object **BKE_object_pose_array_get(ViewLayer *view_layer, View3D *v3d, uint *r_objects_len)
1261 return BKE_object_pose_array_get_ex(view_layer, v3d, r_objects_len, false);
1264 Base **BKE_object_pose_base_array_get_ex(ViewLayer *view_layer, View3D *v3d, uint *r_bases_len, bool unique)
1266 Base *base_active = BASACT(view_layer);
1267 Object *ob_pose = base_active ? BKE_object_pose_armature_get(base_active->object) : NULL;
1268 Base *base_pose = NULL;
1269 Base **bases = NULL;
1272 if (ob_pose == base_active->object) {
1273 base_pose = base_active;
1276 base_pose = BKE_view_layer_base_find(view_layer, ob_pose);
1280 if (base_active && (base_pose == base_active)) {
1281 bases = BKE_view_layer_array_from_bases_in_mode(
1282 view_layer, v3d, r_bases_len, {
1283 .object_mode = OB_MODE_POSE,
1284 .no_dup_data = unique});
1286 else if (base_pose != NULL) {
1288 bases = MEM_mallocN(sizeof(*bases), __func__);
1289 bases[0] = base_pose;
1293 bases = MEM_mallocN(0, __func__);
1297 Base **BKE_object_pose_base_array_get_unique(ViewLayer *view_layer, View3D *v3d, uint *r_bases_len)
1299 return BKE_object_pose_base_array_get_ex(view_layer, v3d, r_bases_len, true);
1301 Base **BKE_object_pose_base_array_get(ViewLayer *view_layer, View3D *v3d, uint *r_bases_len)
1303 return BKE_object_pose_base_array_get_ex(view_layer, v3d, r_bases_len, false);
1306 void BKE_object_transform_copy(Object *ob_tar, const Object *ob_src)
1308 copy_v3_v3(ob_tar->loc, ob_src->loc);
1309 copy_v3_v3(ob_tar->rot, ob_src->rot);
1310 copy_v3_v3(ob_tar->quat, ob_src->quat);
1311 copy_v3_v3(ob_tar->rotAxis, ob_src->rotAxis);
1312 ob_tar->rotAngle = ob_src->rotAngle;
1313 ob_tar->rotmode = ob_src->rotmode;
1314 copy_v3_v3(ob_tar->size, ob_src->size);
1318 * Only copy internal data of Object ID from source to already allocated/initialized destination.
1319 * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
1321 * WARNING! This function will not handle ID user count!
1323 * \param flag: Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
1325 void BKE_object_copy_data(Main *bmain, Object *ob_dst, const Object *ob_src, const int flag)
1328 GpencilModifierData *gmd;
1331 /* Do not copy runtime data. */
1332 BKE_object_runtime_reset(ob_dst);
1334 /* We never handle usercount here for own data. */
1335 const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
1337 if (ob_src->totcol) {
1338 ob_dst->mat = MEM_dupallocN(ob_src->mat);
1339 ob_dst->matbits = MEM_dupallocN(ob_src->matbits);
1340 ob_dst->totcol = ob_src->totcol;
1342 else if (ob_dst->mat != NULL || ob_dst->matbits != NULL) {
1343 /* This shall not be needed, but better be safe than sorry. */
1344 BLI_assert(!"Object copy: non-NULL material pointers with zero counter, should not happen.");
1346 ob_dst->matbits = NULL;
1349 if (ob_src->iuser) ob_dst->iuser = MEM_dupallocN(ob_src->iuser);
1351 if (ob_src->bb) ob_dst->bb = MEM_dupallocN(ob_src->bb);
1353 BLI_listbase_clear(&ob_dst->modifiers);
1355 for (md = ob_src->modifiers.first; md; md = md->next) {
1356 ModifierData *nmd = modifier_new(md->type);
1357 BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
1358 modifier_copyData_ex(md, nmd, flag_subdata);
1359 BLI_addtail(&ob_dst->modifiers, nmd);
1362 BLI_listbase_clear(&ob_dst->greasepencil_modifiers);
1364 for (gmd = ob_src->greasepencil_modifiers.first; gmd; gmd = gmd->next) {
1365 GpencilModifierData *nmd = BKE_gpencil_modifier_new(gmd->type);
1366 BLI_strncpy(nmd->name, gmd->name, sizeof(nmd->name));
1367 BKE_gpencil_modifier_copyData_ex(gmd, nmd, flag_subdata);
1368 BLI_addtail(&ob_dst->greasepencil_modifiers, nmd);
1371 BLI_listbase_clear(&ob_dst->shader_fx);
1373 for (fx = ob_src->shader_fx.first; fx; fx = fx->next) {
1374 ShaderFxData *nfx = BKE_shaderfx_new(fx->type);
1375 BLI_strncpy(nfx->name, fx->name, sizeof(nfx->name));
1376 BKE_shaderfx_copyData_ex(fx, nfx, flag_subdata);
1377 BLI_addtail(&ob_dst->shader_fx, nfx);
1381 copy_object_pose(ob_dst, ob_src, flag_subdata);
1382 /* backwards compat... non-armatures can get poses in older files? */
1383 if (ob_src->type == OB_ARMATURE) {
1384 const bool do_pose_id_user = (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0;
1385 BKE_pose_rebuild(bmain, ob_dst, ob_dst->data, do_pose_id_user);
1388 defgroup_copy_list(&ob_dst->defbase, &ob_src->defbase);
1389 BKE_object_facemap_copy_list(&ob_dst->fmaps, &ob_src->fmaps);
1390 BKE_constraints_copy_ex(&ob_dst->constraints, &ob_src->constraints, flag_subdata, true);
1392 ob_dst->mode = ob_dst->type != OB_GPENCIL ? OB_MODE_OBJECT : ob_dst->mode;
1393 ob_dst->sculpt = NULL;
1396 ob_dst->pd = MEM_dupallocN(ob_src->pd);
1397 if (ob_dst->pd->rng) {
1398 ob_dst->pd->rng = MEM_dupallocN(ob_src->pd->rng);
1401 BKE_object_copy_softbody(ob_dst, ob_src, flag_subdata);
1402 ob_dst->rigidbody_object = BKE_rigidbody_copy_object(ob_src, flag_subdata);
1403 ob_dst->rigidbody_constraint = BKE_rigidbody_copy_constraint(ob_src, flag_subdata);
1405 BKE_object_copy_particlesystems(ob_dst, ob_src, flag_subdata);
1407 ob_dst->derivedDeform = NULL;
1408 ob_dst->derivedFinal = NULL;
1410 BLI_listbase_clear((ListBase *)&ob_dst->drawdata);
1411 BLI_listbase_clear(&ob_dst->pc_ids);
1413 ob_dst->avs = ob_src->avs;
1414 ob_dst->mpath = animviz_copy_motionpath(ob_src->mpath);
1416 copy_object_lod(ob_dst, ob_src, flag_subdata);
1418 /* Do not copy object's preview (mostly due to the fact renderers create temp copy of objects). */
1419 if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0 && false) { /* XXX TODO temp hack */
1420 BKE_previewimg_id_copy(&ob_dst->id, &ob_src->id);
1423 ob_dst->preview = NULL;
1427 /* copy objects, will re-initialize cached simulation data */
1428 Object *BKE_object_copy(Main *bmain, const Object *ob)
1431 BKE_id_copy_ex(bmain, &ob->id, (ID **)&ob_copy, 0, false);
1433 /* We increase object user count when linking to Collections. */
1434 id_us_min(&ob_copy->id);
1439 void BKE_object_make_local_ex(Main *bmain, Object *ob, const bool lib_local, const bool clear_proxy)
1441 bool is_local = false, is_lib = false;
1443 /* - only lib users: do nothing (unless force_local is set)
1444 * - only local users: set flag
1445 * - mixed: make copy
1446 * In case we make a whole lib's content local, we always want to localize, and we skip remapping (done later).
1449 if (!ID_IS_LINKED(ob)) {
1453 BKE_library_ID_test_usages(bmain, ob, &is_local, &is_lib);
1455 if (lib_local || is_local) {
1457 id_clear_lib_data(bmain, &ob->id);
1458 BKE_id_expand_local(bmain, &ob->id);
1460 if (ob->proxy_from != NULL) {
1461 ob->proxy_from->proxy = NULL;
1462 ob->proxy_from->proxy_group = NULL;
1464 ob->proxy = ob->proxy_from = ob->proxy_group = NULL;
1468 Object *ob_new = BKE_object_copy(bmain, ob);
1471 ob_new->proxy = ob_new->proxy_from = ob_new->proxy_group = NULL;
1473 /* setting newid is mandatory for complex make_lib_local logic... */
1474 ID_NEW_SET(ob, ob_new);
1477 BKE_libblock_remap(bmain, ob, ob_new, ID_REMAP_SKIP_INDIRECT_USAGE);
1483 void BKE_object_make_local(Main *bmain, Object *ob, const bool lib_local)
1485 BKE_object_make_local_ex(bmain, ob, lib_local, true);
1488 /* Returns true if the Object is from an external blend file (libdata) */
1489 bool BKE_object_is_libdata(const Object *ob)
1491 return (ob && ID_IS_LINKED(ob));
1494 /* Returns true if the Object data is from an external blend file (libdata) */
1495 bool BKE_object_obdata_is_libdata(const Object *ob)
1497 /* Linked objects with local obdata are forbidden! */
1498 BLI_assert(!ob || !ob->data || (ID_IS_LINKED(ob) ? ID_IS_LINKED(ob->data) : true));
1499 return (ob && ob->data && ID_IS_LINKED(ob->data));
1502 /* *************** PROXY **************** */
1504 /* when you make proxy, ensure the exposed layers are extern */
1505 static void armature_set_id_extern(Object *ob)
1507 bArmature *arm = ob->data;
1508 bPoseChannel *pchan;
1509 unsigned int lay = arm->layer_protected;
1511 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1512 if (!(pchan->bone->layer & lay))
1513 id_lib_extern((ID *)pchan->custom);
1518 void BKE_object_copy_proxy_drivers(Object *ob, Object *target)
1520 if ((target->adt) && (target->adt->drivers.first)) {
1523 /* add new animdata block */
1525 ob->adt = BKE_animdata_add_id(&ob->id);
1527 /* make a copy of all the drivers (for now), then correct any links that need fixing */
1528 free_fcurves(&ob->adt->drivers);
1529 copy_fcurves(&ob->adt->drivers, &target->adt->drivers);
1531 for (fcu = ob->adt->drivers.first; fcu; fcu = fcu->next) {
1532 ChannelDriver *driver = fcu->driver;
1535 for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
1537 DRIVER_TARGETS_LOOPER_BEGIN(dvar)
1540 if ((Object *)dtar->id == target)
1541 dtar->id = (ID *)ob;
1543 /* only on local objects because this causes indirect links
1544 * 'a -> b -> c', blend to point directly to a.blend
1545 * when a.blend has a proxy thats linked into c.blend */
1546 if (!ID_IS_LINKED(ob))
1547 id_lib_extern((ID *)dtar->id);
1551 DRIVER_TARGETS_LOOPER_END;
1557 /* proxy rule: lib_object->proxy_from == the one we borrow from, set temporally while object_update */
1558 /* local_object->proxy == pointer to library object, saved in files and read */
1559 /* local_object->proxy_group == pointer to collection dupli-object, saved in files and read */
1561 void BKE_object_make_proxy(Main *bmain, Object *ob, Object *target, Object *cob)
1563 /* paranoia checks */
1564 if (ID_IS_LINKED(ob) || !ID_IS_LINKED(target)) {
1565 printf("cannot make proxy\n");
1570 ob->proxy_group = cob;
1571 id_lib_extern(&target->id);
1573 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1574 DEG_id_tag_update(&target->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1577 * - cob means this proxy comes from a collection, just apply the matrix
1578 * so the object wont move from its dupli-transform.
1580 * - no cob means this is being made from a linked object,
1581 * this is closer to making a copy of the object - in-place. */
1583 ob->rotmode = target->rotmode;
1584 mul_m4_m4m4(ob->obmat, cob->obmat, target->obmat);
1585 if (cob->dup_group) { /* should always be true */
1587 mul_v3_mat3_m4v3(tvec, ob->obmat, cob->dup_group->dupli_ofs);
1588 sub_v3_v3(ob->obmat[3], tvec);
1590 BKE_object_apply_mat4(ob, ob->obmat, false, true);
1593 BKE_object_transform_copy(ob, target);
1594 ob->parent = target->parent; /* libdata */
1595 copy_m4_m4(ob->parentinv, target->parentinv);
1598 /* copy animdata stuff - drivers only for now... */
1599 BKE_object_copy_proxy_drivers(ob, target);
1601 /* skip constraints? */
1602 /* FIXME: this is considered by many as a bug */
1604 /* set object type and link to data */
1605 ob->type = target->type;
1606 ob->data = target->data;
1607 id_us_plus((ID *)ob->data); /* ensures lib data becomes LIB_TAG_EXTERN */
1609 /* copy vertex groups */
1610 defgroup_copy_list(&ob->defbase, &target->defbase);
1612 /* copy material and index information */
1613 ob->actcol = ob->totcol = 0;
1614 if (ob->mat) MEM_freeN(ob->mat);
1615 if (ob->matbits) MEM_freeN(ob->matbits);
1618 if ((target->totcol) && (target->mat) && OB_TYPE_SUPPORT_MATERIAL(ob->type)) {
1621 ob->actcol = target->actcol;
1622 ob->totcol = target->totcol;
1624 ob->mat = MEM_dupallocN(target->mat);
1625 ob->matbits = MEM_dupallocN(target->matbits);
1626 for (i = 0; i < target->totcol; i++) {
1627 /* don't need to run test_object_materials since we know this object is new and not used elsewhere */
1628 id_us_plus((ID *)ob->mat[i]);
1632 /* type conversions */
1633 if (target->type == OB_ARMATURE) {
1634 copy_object_pose(ob, target, 0); /* data copy, object pointers in constraints */
1635 BKE_pose_rest(ob->pose); /* clear all transforms in channels */
1636 BKE_pose_rebuild(bmain, ob, ob->data, true); /* set all internal links */
1638 armature_set_id_extern(ob);
1640 else if (target->type == OB_EMPTY) {
1641 ob->empty_drawtype = target->empty_drawtype;
1642 ob->empty_drawsize = target->empty_drawsize;
1645 /* copy IDProperties */
1646 if (ob->id.properties) {
1647 IDP_FreeProperty(ob->id.properties);
1648 MEM_freeN(ob->id.properties);
1649 ob->id.properties = NULL;
1651 if (target->id.properties) {
1652 ob->id.properties = IDP_CopyProperty(target->id.properties);
1655 /* copy drawtype info */
1656 ob->dt = target->dt;
1660 * Use with newly created objects to set their size
1661 * (used to apply scene-scale).
1663 void BKE_object_obdata_size_init(struct Object *ob, const float size)
1665 /* apply radius as a scale to types that support it */
1669 ob->empty_drawsize *= size;
1674 ob->empty_drawsize *= size;
1679 Curve *cu = ob->data;
1685 Camera *cam = ob->data;
1686 cam->drawsize *= size;
1691 Lamp *lamp = ob->data;
1693 lamp->area_size *= size;
1694 lamp->area_sizey *= size;
1695 lamp->area_sizez *= size;
1698 /* Only lattice (not mesh, curve, mball...),
1699 * because its got data when newly added */
1702 struct Lattice *lt = ob->data;
1706 scale_m4_fl(mat, size);
1708 BKE_lattice_transform(lt, (float (*)[4])mat, false);
1714 /* *************** CALC ****************** */
1716 void BKE_object_scale_to_mat3(Object *ob, float mat[3][3])
1719 mul_v3_v3v3(vec, ob->size, ob->dscale);
1720 size_to_mat3(mat, vec);
1723 void BKE_object_rot_to_mat3(Object *ob, float mat[3][3], bool use_drot)
1725 float rmat[3][3], dmat[3][3];
1727 /* 'dmat' is the delta-rotation matrix, which will get (pre)multiplied
1728 * with the rotation matrix to yield the appropriate rotation
1731 /* rotations may either be quats, eulers (with various rotation orders), or axis-angle */
1732 if (ob->rotmode > 0) {
1733 /* euler rotations (will cause gimble lock, but this can be alleviated a bit with rotation orders) */
1734 eulO_to_mat3(rmat, ob->rot, ob->rotmode);
1735 eulO_to_mat3(dmat, ob->drot, ob->rotmode);
1737 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
1738 /* axis-angle - not really that great for 3D-changing orientations */
1739 axis_angle_to_mat3(rmat, ob->rotAxis, ob->rotAngle);
1740 axis_angle_to_mat3(dmat, ob->drotAxis, ob->drotAngle);
1743 /* quats are normalized before use to eliminate scaling issues */
1746 normalize_qt_qt(tquat, ob->quat);
1747 quat_to_mat3(rmat, tquat);
1749 normalize_qt_qt(tquat, ob->dquat);
1750 quat_to_mat3(dmat, tquat);
1753 /* combine these rotations */
1755 mul_m3_m3m3(mat, dmat, rmat);
1757 copy_m3_m3(mat, rmat);
1760 void BKE_object_mat3_to_rot(Object *ob, float mat[3][3], bool use_compat)
1762 BLI_ASSERT_UNIT_M3(mat);
1764 switch (ob->rotmode) {
1768 mat3_normalized_to_quat(ob->quat, mat);
1769 normalize_qt_qt(dquat, ob->dquat);
1770 invert_qt_normalized(dquat);
1771 mul_qt_qtqt(ob->quat, dquat, ob->quat);
1774 case ROT_MODE_AXISANGLE:
1779 /* without drot we could apply 'mat' directly */
1780 mat3_normalized_to_quat(quat, mat);
1781 axis_angle_to_quat(dquat, ob->drotAxis, ob->drotAngle);
1782 invert_qt_normalized(dquat);
1783 mul_qt_qtqt(quat, dquat, quat);
1784 quat_to_axis_angle(ob->rotAxis, &ob->rotAngle, quat);
1787 default: /* euler */
1792 /* without drot we could apply 'mat' directly */
1793 mat3_normalized_to_quat(quat, mat);
1794 eulO_to_quat(dquat, ob->drot, ob->rotmode);
1795 invert_qt_normalized(dquat);
1796 mul_qt_qtqt(quat, dquat, quat);
1797 /* end drot correction */
1799 if (use_compat) quat_to_compatible_eulO(ob->rot, ob->rot, ob->rotmode, quat);
1800 else quat_to_eulO(ob->rot, ob->rotmode, quat);
1806 void BKE_object_tfm_protected_backup(const Object *ob,
1807 ObjectTfmProtectedChannels *obtfm)
1810 #define TFMCPY(_v) (obtfm->_v = ob->_v)
1811 #define TFMCPY3D(_v) copy_v3_v3(obtfm->_v, ob->_v)
1812 #define TFMCPY4D(_v) copy_v4_v4(obtfm->_v, ob->_v)
1833 void BKE_object_tfm_protected_restore(Object *ob,
1834 const ObjectTfmProtectedChannels *obtfm,
1835 const short protectflag)
1839 for (i = 0; i < 3; i++) {
1840 if (protectflag & (OB_LOCK_LOCX << i)) {
1841 ob->loc[i] = obtfm->loc[i];
1842 ob->dloc[i] = obtfm->dloc[i];
1845 if (protectflag & (OB_LOCK_SCALEX << i)) {
1846 ob->size[i] = obtfm->size[i];
1847 ob->dscale[i] = obtfm->dscale[i];
1850 if (protectflag & (OB_LOCK_ROTX << i)) {
1851 ob->rot[i] = obtfm->rot[i];
1852 ob->drot[i] = obtfm->drot[i];
1854 ob->quat[i + 1] = obtfm->quat[i + 1];
1855 ob->dquat[i + 1] = obtfm->dquat[i + 1];
1857 ob->rotAxis[i] = obtfm->rotAxis[i];
1858 ob->drotAxis[i] = obtfm->drotAxis[i];
1862 if ((protectflag & OB_LOCK_ROT4D) && (protectflag & OB_LOCK_ROTW)) {
1863 ob->quat[0] = obtfm->quat[0];
1864 ob->dquat[0] = obtfm->dquat[0];
1866 ob->rotAngle = obtfm->rotAngle;
1867 ob->drotAngle = obtfm->drotAngle;
1871 void BKE_object_to_mat3(Object *ob, float mat[3][3]) /* no parent */
1878 BKE_object_scale_to_mat3(ob, smat);
1881 BKE_object_rot_to_mat3(ob, rmat, true);
1882 mul_m3_m3m3(mat, rmat, smat);
1885 void BKE_object_to_mat4(Object *ob, float mat[4][4])
1889 BKE_object_to_mat3(ob, tmat);
1891 copy_m4_m3(mat, tmat);
1893 add_v3_v3v3(mat[3], ob->loc, ob->dloc);
1896 void BKE_object_matrix_local_get(struct Object *ob, float mat[4][4])
1899 float par_imat[4][4];
1901 BKE_object_get_parent_matrix(ob, ob->parent, par_imat);
1902 invert_m4(par_imat);
1903 mul_m4_m4m4(mat, par_imat, ob->obmat);
1906 copy_m4_m4(mat, ob->obmat);
1911 * \param depsgraph: Used for dupli-frame time.
1912 * \return success if \a mat is set.
1914 static bool ob_parcurve(Object *ob, Object *par,
1915 float dupli_ctime, int dupli_transflag, float mat[4][4])
1917 Curve *cu = par->data;
1918 float vec[4], dir[3], quat[4], radius, ctime;
1920 /* NOTE: Curve cache is supposed to be evaluated here already, however there
1921 * are cases where we can not guarantee that. This includes, for example,
1922 * dependency cycles. We can't correct anything from here, since that would
1923 * cause a threading conflicts.
1925 * TODO(sergey): Somce of the legit looking cases like T56619 need to be
1926 * looked into, and maybe curve cache (and other dependencies) are to be
1927 * evaluated prior to conversion. */
1928 if (par->runtime.curve_cache == NULL) {
1931 if (par->runtime.curve_cache->path == NULL) {
1935 /* catch exceptions: curve paths used as a duplicator */
1936 if ((dupli_transflag & OB_DUPLINOSPEED) == 0) {
1937 /* ctime is now a proper var setting of Curve which gets set by Animato like any other var that's animated,
1938 * but this will only work if it actually is animated...
1940 * we divide the curvetime calculated in the previous step by the length of the path, to get a time
1941 * factor, which then gets clamped to lie within 0.0 - 1.0 range
1944 ctime = cu->ctime / cu->pathlen;
1949 CLAMP(ctime, 0.0f, 1.0f);
1952 ctime = dupli_ctime;
1954 ctime /= cu->pathlen;
1956 CLAMP(ctime, 0.0f, 1.0f);
1962 if (where_on_path(par, ctime, vec, dir, (cu->flag & CU_FOLLOW) ? quat : NULL, &radius, NULL)) {
1963 if (cu->flag & CU_FOLLOW) {
1964 quat_apply_track(quat, ob->trackflag, ob->upflag);
1966 quat_to_mat4(mat, quat);
1968 if (cu->flag & CU_PATH_RADIUS) {
1969 float tmat[4][4], rmat[4][4];
1970 scale_m4_fl(tmat, radius);
1971 mul_m4_m4m4(rmat, tmat, mat);
1972 copy_m4_m4(mat, rmat);
1974 copy_v3_v3(mat[3], vec);
1980 static void ob_parbone(Object *ob, Object *par, float mat[4][4])
1982 bPoseChannel *pchan;
1985 if (par->type != OB_ARMATURE) {
1990 /* Make sure the bone is still valid */
1991 pchan = BKE_pose_channel_find_name(par->pose, ob->parsubstr);
1992 if (!pchan || !pchan->bone) {
1993 printf("Object %s with Bone parent: bone %s doesn't exist\n", ob->id.name + 2, ob->parsubstr);
1998 /* get bone transform */
1999 if (pchan->bone->flag & BONE_RELATIVE_PARENTING) {
2000 /* the new option uses the root - expected behavior, but differs from old... */
2001 /* XXX check on version patching? */
2002 copy_m4_m4(mat, pchan->chan_mat);
2005 copy_m4_m4(mat, pchan->pose_mat);
2007 /* but for backwards compatibility, the child has to move to the tail */
2008 copy_v3_v3(vec, mat[1]);
2009 mul_v3_fl(vec, pchan->bone->length);
2010 add_v3_v3(mat[3], vec);
2014 static void give_parvert(Object *par, int nr, float vec[3])
2018 if (par->type == OB_MESH) {
2019 Mesh *me = par->data;
2020 BMEditMesh *em = me->edit_btmesh;
2021 Mesh *me_eval = (em) ? em->mesh_eval_final : par->runtime.mesh_eval;
2025 const int numVerts = me_eval->totvert;
2027 if (nr < numVerts) {
2028 if (em && me_eval->runtime.is_original) {
2029 if (em->bm->elem_table_dirty & BM_VERT) {
2030 #ifdef VPARENT_THREADING_HACK
2031 BLI_mutex_lock(&vparent_lock);
2032 if (em->bm->elem_table_dirty & BM_VERT) {
2033 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
2035 BLI_mutex_unlock(&vparent_lock);
2037 BLI_assert(!"Not safe for threading");
2038 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
2043 if (CustomData_has_layer(&me_eval->vdata, CD_ORIGINDEX) &&
2044 !(em && me_eval->runtime.is_original))
2046 const int *index = CustomData_get_layer(&me_eval->vdata, CD_ORIGINDEX);
2047 /* Get the average of all verts with (original index == nr). */
2048 for (int i = 0; i < numVerts; i++) {
2049 if (index[i] == nr) {
2050 add_v3_v3(vec, me_eval->mvert[i].co);
2056 if (nr < numVerts) {
2057 add_v3_v3(vec, me_eval->mvert[nr].co);
2064 /* keep as 0, 0, 0 */
2066 else if (count > 0) {
2067 mul_v3_fl(vec, 1.0f / count);
2070 /* use first index if its out of range */
2071 if (me_eval->totvert) {
2072 copy_v3_v3(vec, me_eval->mvert[0].co);
2078 "%s: Evaluated mesh is needed to solve parenting, "
2079 "object position can be wrong now\n", __func__);
2082 else if (ELEM(par->type, OB_CURVE, OB_SURF)) {
2085 /* Unless there's some weird depsgraph failure the cache should exist. */
2086 BLI_assert(par->runtime.curve_cache != NULL);
2088 if (par->runtime.curve_cache->deformed_nurbs.first != NULL) {
2089 nurb = &par->runtime.curve_cache->deformed_nurbs;
2092 Curve *cu = par->data;
2093 nurb = BKE_curve_nurbs_get(cu);
2096 BKE_nurbList_index_get_co(nurb, nr, vec);
2098 else if (par->type == OB_LATTICE) {
2099 Lattice *latt = par->data;
2100 DispList *dl = par->runtime.curve_cache ? BKE_displist_find(&par->runtime.curve_cache->disp, DL_VERTS) : NULL;
2101 float (*co)[3] = dl ? (float (*)[3])dl->verts : NULL;
2104 if (latt->editlatt) latt = latt->editlatt->latt;
2106 tot = latt->pntsu * latt->pntsv * latt->pntsw;
2108 /* ensure dl is correct size */
2109 BLI_assert(dl == NULL || dl->nr == tot);
2113 copy_v3_v3(vec, co[nr]);
2116 copy_v3_v3(vec, latt->def[nr].vec);
2122 static void ob_parvert3(Object *ob, Object *par, float mat[4][4])
2125 /* in local ob space */
2126 if (OB_TYPE_SUPPORT_PARVERT(par->type)) {
2127 float cmat[3][3], v1[3], v2[3], v3[3], q[4];
2129 give_parvert(par, ob->par1, v1);
2130 give_parvert(par, ob->par2, v2);
2131 give_parvert(par, ob->par3, v3);
2133 tri_to_quat(q, v1, v2, v3);
2134 quat_to_mat3(cmat, q);
2135 copy_m4_m3(mat, cmat);
2137 mid_v3_v3v3v3(mat[3], v1, v2, v3);
2144 void BKE_object_get_parent_matrix_for_dupli(Object *ob, Object *par,
2145 float dupli_ctime, int dupli_transflag,
2146 float parentmat[4][4])
2152 switch (ob->partype & PARTYPE) {
2155 if (par->type == OB_CURVE) {
2156 if ((((Curve *)par->data)->flag & CU_PATH) &&
2157 (ob_parcurve(ob, par, dupli_ctime, dupli_transflag, tmat)))
2163 if (ok) mul_m4_m4m4(parentmat, par->obmat, tmat);
2164 else copy_m4_m4(parentmat, par->obmat);
2168 ob_parbone(ob, par, tmat);
2169 mul_m4_m4m4(parentmat, par->obmat, tmat);
2174 give_parvert(par, ob->par1, vec);
2175 mul_v3_m4v3(parentmat[3], par->obmat, vec);
2178 ob_parvert3(ob, par, tmat);
2180 mul_m4_m4m4(parentmat, par->obmat, tmat);
2184 copy_m4_m4(parentmat, par->obmat);
2190 void BKE_object_get_parent_matrix(Object *ob, Object *par, float parentmat[4][4])
2192 BKE_object_get_parent_matrix_for_dupli(ob, par, 0, 0, parentmat);
2196 * \param r_originmat: Optional matrix that stores the space the object is in (without its own matrix applied)
2198 static void solve_parenting(Object *ob, Object *par, float obmat[4][4], float slowmat[4][4],
2199 float r_originmat[3][3], const bool set_origin,
2200 float dupli_ctime, int dupli_transflag)
2206 BKE_object_to_mat4(ob, locmat);
2208 if (ob->partype & PARSLOW) copy_m4_m4(slowmat, obmat);
2210 BKE_object_get_parent_matrix_for_dupli(ob, par, dupli_ctime, dupli_transflag, totmat);
2213 mul_m4_m4m4(tmat, totmat, ob->parentinv);
2214 mul_m4_m4m4(obmat, tmat, locmat);
2217 /* usable originmat */
2218 copy_m3_m4(r_originmat, tmat);
2221 /* origin, for help line */
2223 if ((ob->partype & PARTYPE) == PARSKEL) {
2224 copy_v3_v3(ob->orig, par->obmat[3]);
2227 copy_v3_v3(ob->orig, totmat[3]);
2232 static bool where_is_object_parslow(Object *ob, float obmat[4][4], float slowmat[4][4])
2238 /* include framerate */
2239 fac1 = (1.0f / (1.0f + fabsf(ob->sf)));
2240 if (fac1 >= 1.0f) return false;
2245 for (a = 0; a < 16; a++, fp1++, fp2++) {
2246 fp1[0] = fac1 * fp1[0] + fac2 * fp2[0];
2252 /* note, scene is the active scene while actual_scene is the scene the object resides in */
2253 void BKE_object_where_is_calc_time_ex(
2254 Depsgraph *depsgraph, Scene *scene, Object *ob, float ctime, int dupli_transflag,
2255 RigidBodyWorld *rbw, float r_originmat[3][3])
2257 if (ob == NULL) return;
2259 /* execute drivers only, as animation has already been done */
2260 BKE_animsys_evaluate_animdata(depsgraph, scene, &ob->id, ob->adt, ctime, ADT_RECALC_DRIVERS);
2263 Object *par = ob->parent;
2264 float slowmat[4][4];
2266 /* calculate parent matrix */
2267 solve_parenting(ob, par, ob->obmat, slowmat, r_originmat, true,
2268 ctime, dupli_transflag);
2270 /* "slow parent" is definitely not threadsafe, and may also give bad results jumping around
2271 * An old-fashioned hack which probably doesn't really cut it anymore
2273 if (ob->partype & PARSLOW) {
2274 if (!where_is_object_parslow(ob, ob->obmat, slowmat))
2279 BKE_object_to_mat4(ob, ob->obmat);
2282 /* try to fall back to the scene rigid body world if none given */
2283 rbw = rbw ? rbw : scene->rigidbody_world;
2284 /* read values pushed into RBO from sim/cache... */
2285 BKE_rigidbody_sync_transforms(rbw, ob, ctime);
2287 /* solve constraints */
2288 if (ob->constraints.first && !(ob->transflag & OB_NO_CONSTRAINTS)) {
2290 cob = BKE_constraints_make_evalob(depsgraph, scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2291 BKE_constraints_solve(depsgraph, &ob->constraints, cob, ctime);
2292 BKE_constraints_clear_evalob(cob);
2295 /* set negative scale flag in object */
2296 if (is_negative_m4(ob->obmat)) ob->transflag |= OB_NEG_SCALE;
2297 else ob->transflag &= ~OB_NEG_SCALE;
2300 void BKE_object_where_is_calc_time(Depsgraph *depsgraph, Scene *scene, Object *ob, float ctime)
2302 BKE_object_where_is_calc_time_ex(depsgraph, scene, ob, ctime, 0, NULL, NULL);
2305 void BKE_object_where_is_calc_time_for_dupli(
2306 Depsgraph *depsgraph, Scene *scene, struct Object *ob, float ctime, int dupli_transflag)
2308 BKE_object_where_is_calc_time_ex(depsgraph, scene, ob, ctime, dupli_transflag, NULL, NULL);
2312 /* get object transformation matrix without recalculating dependencies and
2313 * constraints -- assume dependencies are already solved by depsgraph.
2314 * no changes to object and it's parent would be done.
2315 * used for bundles orientation in 3d space relative to parented blender camera */
2316 void BKE_object_where_is_calc_mat4(Object *ob, float obmat[4][4])
2320 float slowmat[4][4];
2322 Object *par = ob->parent;
2324 solve_parenting(ob, par, obmat, slowmat, NULL, false, 0.0f, 0);
2326 if (ob->partype & PARSLOW)
2327 where_is_object_parslow(ob, obmat, slowmat);
2330 BKE_object_to_mat4(ob, obmat);
2334 void BKE_object_where_is_calc_ex(Depsgraph *depsgraph, Scene *scene, RigidBodyWorld *rbw, Object *ob, float r_originmat[3][3])
2336 BKE_object_where_is_calc_time_ex(depsgraph, scene, ob, DEG_get_ctime(depsgraph), 0, rbw, r_originmat);
2338 void BKE_object_where_is_calc(Depsgraph *depsgraph, Scene *scene, Object *ob)
2340 BKE_object_where_is_calc_time_ex(depsgraph, scene, ob, DEG_get_ctime(depsgraph), 0, NULL, NULL);
2344 * For calculation of the inverse parent transform, only used for editor.
2346 * It assumes the object parent is already in the depsgraph.
2347 * Otherwise, after changing ob->parent you need to call:
2348 * - #DEG_relations_tag_update(bmain);
2349 * - #BKE_scene_graph_update_tagged(depsgraph, bmain);
2351 void BKE_object_workob_calc_parent(Depsgraph *depsgraph, Scene *scene, Object *ob, Object *workob)
2353 Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
2354 BKE_object_workob_clear(workob);
2356 unit_m4(workob->obmat);
2357 unit_m4(workob->parentinv);
2358 unit_m4(workob->constinv);
2360 /* Since this is used while calculating parenting, at this moment ob_eval->parent is still NULL. */
2361 workob->parent = DEG_get_evaluated_object(depsgraph, ob->parent);
2363 workob->trackflag = ob_eval->trackflag;
2364 workob->upflag = ob_eval->upflag;
2366 workob->partype = ob_eval->partype;
2367 workob->par1 = ob_eval->par1;
2368 workob->par2 = ob_eval->par2;
2369 workob->par3 = ob_eval->par3;
2371 workob->constraints = ob_eval->constraints;
2373 BLI_strncpy(workob->parsubstr, ob_eval->parsubstr, sizeof(workob->parsubstr));
2375 BKE_object_where_is_calc(depsgraph, scene, workob);
2379 * Applies the global transformation \a mat to the \a ob using a relative parent space if supplied.
2381 * \param mat: the global transformation mat that the object should be set object to.
2382 * \param parent: the parent space in which this object will be set relative to (should probably always be parent_eval).
2383 * \param use_compat: true to ensure that rotations are set using the min difference between the old and new orientation.
2385 void BKE_object_apply_mat4_ex(Object *ob, float mat[4][4], Object *parent, float parentinv[4][4], const bool use_compat)
2387 /* see BKE_pchan_apply_mat4() for the equivalent 'pchan' function */
2391 if (parent != NULL) {
2392 float rmat[4][4], diff_mat[4][4], imat[4][4], parent_mat[4][4];
2394 BKE_object_get_parent_matrix(ob, parent, parent_mat);
2396 mul_m4_m4m4(diff_mat, parent_mat, parentinv);
2397 invert_m4_m4(imat, diff_mat);
2398 mul_m4_m4m4(rmat, imat, mat); /* get the parent relative matrix */
2400 /* same as below, use rmat rather than mat */
2401 mat4_to_loc_rot_size(ob->loc, rot, ob->size, rmat);
2404 mat4_to_loc_rot_size(ob->loc, rot, ob->size, mat);
2407 BKE_object_mat3_to_rot(ob, rot, use_compat);
2409 sub_v3_v3(ob->loc, ob->dloc);
2411 if (ob->dscale[0] != 0.0f) ob->size[0] /= ob->dscale[0];
2412 if (ob->dscale[1] != 0.0f) ob->size[1] /= ob->dscale[1];
2413 if (ob->dscale[2] != 0.0f) ob->size[2] /= ob->dscale[2];
2415 /* BKE_object_mat3_to_rot handles delta rotations */
2418 /* XXX: should be removed after COW operators port to use BKE_object_apply_mat4_ex directly */
2419 void BKE_object_apply_mat4(Object *ob, float mat[4][4], const bool use_compat, const bool use_parent)
2421 BKE_object_apply_mat4_ex(ob, mat, use_parent ? ob->parent : NULL, ob->parentinv, use_compat);
2424 BoundBox *BKE_boundbox_alloc_unit(void)
2427 const float min[3] = {-1.0f, -1.0f, -1.0f}, max[3] = {1.0f, 1.0f, 1.0f};
2429 bb = MEM_callocN(sizeof(BoundBox), "OB-BoundBox");
2430 BKE_boundbox_init_from_minmax(bb, min, max);
2435 void BKE_boundbox_init_from_minmax(BoundBox *bb, const float min[3], const float max[3])
2437 bb->vec[0][0] = bb->vec[1][0] = bb->vec[2][0] = bb->vec[3][0] = min[0];
2438 bb->vec[4][0] = bb->vec[5][0] = bb->vec[6][0] = bb->vec[7][0] = max[0];
2440 bb->vec[0][1] = bb->vec[1][1] = bb->vec[4][1] = bb->vec[5][1] = min[1];
2441 bb->vec[2][1] = bb->vec[3][1] = bb->vec[6][1] = bb->vec[7][1] = max[1];
2443 bb->vec[0][2] = bb->vec[3][2] = bb->vec[4][2] = bb->vec[7][2] = min[2];
2444 bb->vec[1][2] = bb->vec[2][2] = bb->vec[5][2] = bb->vec[6][2] = max[2];
2447 void BKE_boundbox_calc_center_aabb(const BoundBox *bb, float r_cent[3])
2449 r_cent[0] = 0.5f * (bb->vec[0][0] + bb->vec[4][0]);
2450 r_cent[1] = 0.5f * (bb->vec[0][1] + bb->vec[2][1]);
2451 r_cent[2] = 0.5f * (bb->vec[0][2] + bb->vec[1][2]);
2454 void BKE_boundbox_calc_size_aabb(const BoundBox *bb, float r_size[3])
2456 r_size[0] = 0.5f * fabsf(bb->vec[0][0] - bb->vec[4][0]);
2457 r_size[1] = 0.5f * fabsf(bb->vec[0][1] - bb->vec[2][1]);
2458 r_size[2] = 0.5f * fabsf(bb->vec[0][2] - bb->vec[1][2]);
2461 void BKE_boundbox_minmax(const BoundBox *bb, float obmat[4][4], float r_min[3], float r_max[3])
2464 for (i = 0; i < 8; i++) {
2466 mul_v3_m4v3(vec, obmat, bb->vec[i]);
2467 minmax_v3v3_v3(r_min, r_max, vec);
2471 BoundBox *BKE_object_boundbox_get(Object *ob)
2473 BoundBox *bb = NULL;
2477 bb = BKE_mesh_boundbox_get(ob);
2482 bb = BKE_curve_boundbox_get(ob);
2485 bb = BKE_mball_boundbox_get(ob);
2488 bb = BKE_lattice_boundbox_get(ob);
2491 bb = BKE_armature_boundbox_get(ob);
2494 bb = BKE_gpencil_boundbox_get(ob);
2502 /* used to temporally disable/enable boundbox */
2503 void BKE_object_boundbox_flag(Object *ob, int flag, const bool set)
2505 BoundBox *bb = BKE_object_boundbox_get(ob);
2507 if (set) bb->flag |= flag;
2508 else bb->flag &= ~flag;
2512 void BKE_object_boundbox_calc_from_mesh(struct Object *ob, struct Mesh *me_eval)
2514 float min[3], max[3];
2516 INIT_MINMAX(min, max);
2518 if (!BKE_mesh_minmax(me_eval, min, max)) {
2523 if (ob->bb == NULL) {
2524 ob->bb = MEM_callocN(sizeof(BoundBox), "DM-BoundBox");
2527 BKE_boundbox_init_from_minmax(ob->bb, min, max);
2529 ob->bb->flag &= ~BOUNDBOX_DIRTY;
2532 void BKE_object_dimensions_get(Object *ob, float vec[3])
2534 BoundBox *bb = NULL;
2536 bb = BKE_object_boundbox_get(ob);
2540 mat4_to_size(scale, ob->obmat);
2542 vec[0] = fabsf(scale[0]) * (bb->vec[4][0] - bb->vec[0][0]);
2543 vec[1] = fabsf(scale[1]) * (bb->vec[2][1] - bb->vec[0][1]);
2544 vec[2] = fabsf(scale[2]) * (bb->vec[1][2] - bb->vec[0][2]);
2551 void BKE_object_dimensions_set(Object *ob, const float value[3])
2553 BoundBox *bb = NULL;
2555 bb = BKE_object_boundbox_get(ob);
2559 len[0] = bb->vec[4][0] - bb->vec[0][0];
2560 len[1] = bb->vec[2][1] - bb->vec[0][1];
2561 len[2] = bb->vec[1][2] - bb->vec[0][2];
2563 if (len[0] > 0.f) ob->size[0] = value[0] / len[0];
2564 if (len[1] > 0.f) ob->size[1] = value[1] / len[1];
2565 if (len[2] > 0.f) ob->size[2] = value[2] / len[2];
2569 void BKE_object_minmax(Object *ob, float min_r[3], float max_r[3], const bool use_hidden)
2573 bool changed = false;
2580 bb = *BKE_curve_boundbox_get(ob);
2581 BKE_boundbox_minmax(&bb, ob->obmat, min_r, max_r);
2587 Lattice *lt = ob->data;
2588 BPoint *bp = lt->def;
2591 for (w = 0; w < lt->pntsw; w++) {
2592 for (v = 0; v < lt->pntsv; v++) {
2593 for (u = 0; u < lt->pntsu; u++, bp++) {
2594 mul_v3_m4v3(vec, ob->obmat, bp->vec);
2595 minmax_v3v3_v3(min_r, max_r, vec);
2604 changed = BKE_pose_minmax(ob, min_r, max_r, use_hidden, false);
2609 Mesh *me = BKE_mesh_from_object(ob);
2612 bb = *BKE_mesh_boundbox_get(ob);
2613 BKE_boundbox_minmax(&bb, ob->obmat, min_r, max_r);
2620 float ob_min[3], ob_max[3];
2622 changed = BKE_mball_minmax_ex(ob->data, ob_min, ob_max, ob->obmat, 0);
2624 minmax_v3v3_v3(min_r, max_r, ob_min);
2625 minmax_v3v3_v3(min_r, max_r, ob_max);
2631 if (changed == false) {
2634 copy_v3_v3(size, ob->size);
2635 if ((ob->type == OB_EMPTY) || (ob->type == OB_GPENCIL)) {
2636 mul_v3_fl(size, ob->empty_drawsize);
2639 minmax_v3v3_v3(min_r, max_r, ob->obmat[3]);
2641 copy_v3_v3(vec, ob->obmat[3]);
2642 add_v3_v3(vec, size);
2643 minmax_v3v3_v3(min_r, max_r, vec);
2645 copy_v3_v3(vec, ob->obmat[3]);
2646 sub_v3_v3(vec, size);
2647 minmax_v3v3_v3(min_r, max_r, vec);
2651 void BKE_object_empty_draw_type_set(Object *ob, const int value)
2653 ob->empty_drawtype = value;
2655 if (ob->type == OB_EMPTY && ob->empty_drawtype == OB_EMPTY_IMAGE) {
2657 ob->iuser = MEM_callocN(sizeof(ImageUser), "image user");
2659 ob->iuser->flag |= IMA_ANIM_ALWAYS;
2660 ob->iuser->frames = 100;
2661 ob->iuser->sfra = 1;
2666 MEM_freeN(ob->iuser);
2672 bool BKE_object_empty_image_is_visible_in_view3d(const Object *ob, const RegionView3D *rv3d)
2674 char visibility_flag = ob->empty_image_visibility_flag;
2676 if ((visibility_flag & (OB_EMPTY_IMAGE_HIDE_BACK | OB_EMPTY_IMAGE_HIDE_FRONT)) != 0) {
2677 /* TODO: this isn't correct with perspective projection. */
2678 const float dot = dot_v3v3((float *)&ob->obmat[2], (float *)&rv3d->viewinv[2]);
2679 if (visibility_flag & OB_EMPTY_IMAGE_HIDE_BACK) {
2684 if (visibility_flag & OB_EMPTY_IMAGE_HIDE_FRONT) {
2691 if (rv3d->is_persp) {
2692 return (visibility_flag & OB_EMPTY_IMAGE_HIDE_PERSPECTIVE) == 0;
2695 return (visibility_flag & OB_EMPTY_IMAGE_HIDE_ORTHOGRAPHIC) == 0;
2699 bool BKE_object_minmax_dupli(Depsgraph *depsgraph, Scene *scene, Object *ob, float r_min[3], float r_max[3], const bool use_hidden)
2702 if ((ob->transflag & OB_DUPLI) == 0) {
2708 lb = object_duplilist(depsgraph, scene, ob);
2709 for (dob = lb->first; dob; dob = dob->next) {
2710 if ((use_hidden == false) && (dob->no_draw != 0)) {
2714 BoundBox *bb = BKE_object_boundbox_get(dob->ob);
2718 for (i = 0; i < 8; i++) {
2720 mul_v3_m4v3(vec, dob->mat, bb->vec[i]);
2721 minmax_v3v3_v3(r_min, r_max, vec);
2728 free_object_duplilist(lb); /* does restore */
2734 void BKE_object_foreach_display_point(
2735 Object *ob, float obmat[4][4],
2736 void (*func_cb)(const float[3], void *), void *user_data)
2740 if (ob->runtime.mesh_eval) {
2741 const Mesh *me = ob->runtime.mesh_eval;
2742 const MVert *mv = me->mvert;
2743 const int totvert = me->totvert;
2744 for (int i = 0; i < totvert; i++, mv++) {
2745 mul_v3_m4v3(co, obmat, mv->co);
2746 func_cb(co, user_data);
2749 else if (ob->runtime.curve_cache && ob->runtime.curve_cache->disp.first) {
2752 for (dl = ob->runtime.curve_cache->disp.first; dl; dl = dl->next) {
2753 const float *v3 = dl->verts;
2754 int totvert = dl->nr;
2757 for (i = 0; i < totvert; i++, v3 += 3) {
2758 mul_v3_m4v3(co, obmat, v3);
2759 func_cb(co, user_data);
2765 void BKE_scene_foreach_display_point(
2766 Depsgraph *depsgraph,
2767 void (*func_cb)(const float[3], void *), void *user_data)
2769 DEG_OBJECT_ITER_BEGIN(
2771 DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY |
2772 DEG_ITER_OBJECT_FLAG_VISIBLE |
2773 DEG_ITER_OBJECT_FLAG_DUPLI)
2775 if ((ob->base_flag & BASE_SELECTED) != 0) {
2776 BKE_object_foreach_display_point(ob, ob->obmat, func_cb, user_data);
2779 DEG_OBJECT_ITER_END;
2782 /* copied from DNA_object_types.h */
2783 typedef struct ObTfmBack {
2784 float loc[3], dloc[3], orig[3];
2785 float size[3], dscale[3]; /* scale and delta scale */
2786 float rot[3], drot[3]; /* euler rotation */
2787 float quat[4], dquat[4]; /* quaternion rotation */
2788 float rotAxis[3], drotAxis[3]; /* axis angle rotation - axis part */
2789 float rotAngle, drotAngle; /* axis angle rotation - angle part */
2790 float obmat[4][4]; /* final worldspace matrix with constraints & animsys applied */
2791 float parentinv[4][4]; /* inverse result of parent, so that object doesn't 'stick' to parent */
2792 float constinv[4][4]; /* inverse result of constraints. doesn't include effect of parent or object local transform */
2793 float imat[4][4]; /* inverse matrix of 'obmat' for during render, temporally: ipokeys of transform */
2796 void *BKE_object_tfm_backup(Object *ob)
2798 ObTfmBack *obtfm = MEM_mallocN(sizeof(ObTfmBack), "ObTfmBack");
2799 copy_v3_v3(obtfm->loc, ob->loc);
2800 copy_v3_v3(obtfm->dloc, ob->dloc);
2801 copy_v3_v3(obtfm->orig, ob->orig);
2802 copy_v3_v3(obtfm->size, ob->size);
2803 copy_v3_v3(obtfm->dscale, ob->dscale);
2804 copy_v3_v3(obtfm->rot, ob->rot);
2805 copy_v3_v3(obtfm->drot, ob->drot);
2806 copy_qt_qt(obtfm->quat, ob->quat);
2807 copy_qt_qt(obtfm->dquat, ob->dquat);
2808 copy_v3_v3(obtfm->rotAxis, ob->rotAxis);
2809 copy_v3_v3(obtfm->drotAxis, ob->drotAxis);
2810 obtfm->rotAngle = ob->rotAngle;
2811 obtfm->drotAngle = ob->drotAngle;
2812 copy_m4_m4(obtfm->obmat, ob->obmat);
2813 copy_m4_m4(obtfm->parentinv, ob->parentinv);
2814 copy_m4_m4(obtfm->constinv, ob->constinv);
2815 copy_m4_m4(obtfm->imat, ob->imat);
2817 return (void *)obtfm;
2820 void BKE_object_tfm_restore(Object *ob, void *obtfm_pt)
2822 ObTfmBack *obtfm = (ObTfmBack *)obtfm_pt;
2823 copy_v3_v3(ob->loc, obtfm->loc);
2824 copy_v3_v3(ob->dloc, obtfm->dloc);
2825 copy_v3_v3(ob->orig, obtfm->orig);
2826 copy_v3_v3(ob->size, obtfm->size);
2827 copy_v3_v3(ob->dscale, obtfm->dscale);
2828 copy_v3_v3(ob->rot, obtfm->rot);
2829 copy_v3_v3(ob->drot, obtfm->drot);
2830 copy_qt_qt(ob->quat, obtfm->quat);
2831 copy_qt_qt(ob->dquat, obtfm->dquat);
2832 copy_v3_v3(ob->rotAxis, obtfm->rotAxis);
2833 copy_v3_v3(ob->drotAxis, obtfm->drotAxis);
2834 ob->rotAngle = obtfm->rotAngle;
2835 ob->drotAngle = obtfm->drotAngle;
2836 copy_m4_m4(ob->obmat, obtfm->obmat);
2837 copy_m4_m4(ob->parentinv, obtfm->parentinv);
2838 copy_m4_m4(ob->constinv, obtfm->constinv);
2839 copy_m4_m4(ob->imat, obtfm->imat);
2842 bool BKE_object_parent_loop_check(const Object *par, const Object *ob)
2844 /* test if 'ob' is a parent somewhere in par's parents */
2845 if (par == NULL) return false;
2846 if (ob == par) return true;
2847 return BKE_object_parent_loop_check(par->parent, ob);
2850 static void object_handle_update_proxy(Depsgraph *depsgraph,
2853 const bool do_proxy_update)
2855 /* The case when this is a collection proxy, object_update is called in collection.c */
2856 if (object->proxy == NULL) {
2859 /* set pointer in library proxy target, for copying, but restore it */
2860 object->proxy->proxy_from = object;
2861 // printf("set proxy pointer for later collection stuff %s\n", ob->id.name);
2863 /* the no-group proxy case, we call update */
2864 if (object->proxy_group == NULL) {
2865 if (do_proxy_update) {
2866 // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
2867 BKE_object_handle_update(depsgraph, scene, object->proxy);
2872 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
2873 /* local_object->proxy == pointer to library object, saved in files and read */
2875 /* function below is polluted with proxy exceptions, cleanup will follow! */
2877 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
2878 /* requires flags to be set! */
2879 /* Ideally we shouldn't have to pass the rigid body world, but need bigger restructuring to avoid id */
2880 void BKE_object_handle_update_ex(Depsgraph *depsgraph,
2881 Scene *scene, Object *ob,
2882 RigidBodyWorld *rbw,
2883 const bool do_proxy_update)
2885 const ID *object_data = ob->data;
2886 const bool recalc_object = (ob->id.recalc & ID_RECALC_ALL) != 0;
2887 const bool recalc_data =
2888 (object_data != NULL) ? ((object_data->recalc & ID_RECALC_ALL) != 0)
2890 if (!recalc_object && ! recalc_data) {
2891 object_handle_update_proxy(depsgraph, scene, ob, do_proxy_update);
2894 /* Speed optimization for animation lookups. */
2895 if (ob->pose != NULL) {
2896 BKE_pose_channels_hash_make(ob->pose);
2897 if (ob->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
2898 BKE_pose_update_constraint_flags(ob->pose);
2902 if (ob->type == OB_ARMATURE) {
2903 /* this happens for reading old files and to match library armatures
2904 * with poses we do it ahead of BKE_object_where_is_calc to ensure animation
2905 * is evaluated on the rebuilt pose, otherwise we get incorrect poses
2907 if (ob->pose == NULL || (ob->pose->flag & POSE_RECALC)) {
2908 /* No need to pass bmain here, we assume we do not need to rebuild DEG from here... */
2909 BKE_pose_rebuild(NULL, ob, ob->data, true);
2913 /* XXX new animsys warning: depsgraph tag ID_RECALC_GEOMETRY should not skip drivers,
2914 * which is only in BKE_object_where_is_calc now */
2915 /* XXX: should this case be ID_RECALC_TRANSFORM instead? */
2916 if (recalc_object || recalc_data) {
2917 if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) {
2918 printf("recalcob %s\n", ob->id.name + 2);
2920 /* Handle proxy copy for target. */
2921 if (!BKE_object_eval_proxy_copy(depsgraph, ob)) {
2922 BKE_object_where_is_calc_ex(depsgraph, scene, rbw, ob, NULL);
2927 BKE_object_handle_data_update(depsgraph, scene, ob);
2930 ob->id.recalc &= ID_RECALC_ALL;
2932 object_handle_update_proxy(depsgraph, scene, ob, do_proxy_update);
2935 /* WARNING: "scene" here may not be the scene object actually resides in.
2936 * When dealing with background-sets, "scene" is actually the active scene.
2937 * e.g. "scene" <-- set 1 <-- set 2 ("ob" lives here) <-- set 3 <-- ... <-- set n
2938 * rigid bodies depend on their world so use BKE_object_handle_update_ex() to also pass along the current rigid body world
2940 void BKE_object_handle_update(Depsgraph *depsgraph, Scene *scene, Object *ob)
2942 BKE_object_handle_update_ex(depsgraph, scene, ob, NULL, true);
2945 void BKE_object_sculpt_modifiers_changed(Object *ob)
2947 SculptSession *ss = ob->sculpt;
2949 if (ss && ss->building_vp_handle == false) {
2951 /* we free pbvh on changes, except during sculpt since it can't deal with
2952 * changing PVBH node organization, we hope topology does not change in
2953 * the meantime .. weak */
2955 BKE_pbvh_free(ss->pbvh);
2959 BKE_sculptsession_free_deformMats(ob->sculpt);
2961 /* In vertex/weight paint, force maps to be rebuilt. */
2962 BKE_sculptsession_free_vwpaint_data(ob->sculpt);
2968 BKE_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
2970 for (n = 0; n < totnode; n++)
2971 BKE_pbvh_node_mark_update(nodes[n]);
2978 int BKE_object_obdata_texspace_get(Object *ob, short **r_texflag, float **r_loc, float **r_size, float **r_rot)
2981 if (ob->data == NULL)
2984 switch (GS(((ID *)ob->data)->name)) {
2987 BKE_mesh_texspace_get_reference((Mesh *)ob->data, r_texflag, r_loc, r_rot, r_size);
2992 Curve *cu = ob->data;
2993 if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_DIRTY)) {
2994 BKE_curve_texspace_calc(cu);
2996 if (r_texflag) *r_texflag = &cu->texflag;
2997 if (r_loc) *r_loc = cu->loc;
2998 if (r_size) *r_size = cu->size;
2999 if (r_rot) *r_rot = cu->rot;
3004 MetaBall *mb = ob->data;
3005 if (r_texflag) *r_texflag = &mb->texflag;
3006 if (r_loc) *r_loc = mb->loc;
3007 if (r_size) *r_size = mb->size;
3008 if (r_rot) *r_rot = mb->rot;
3017 /** Get evaluated mesh for given (main, original) object and depsgraph. */
3018 Mesh *BKE_object_get_evaluated_mesh(const Depsgraph *depsgraph, Object *ob)
3020 Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
3021 return ob_eval->runtime.mesh_eval;
3024 /* Get object's mesh with all modifiers applied. */
3025 Mesh *BKE_object_get_final_mesh(Object *object)
3027 if (object->runtime.mesh_eval != NULL) {
3028 BLI_assert((object->id.tag & LIB_TAG_COPIED_ON_WRITE) != 0);
3029 BLI_assert(object->runtime.mesh_eval == object->data);
3030 BLI_assert((object->runtime.mesh_eval->id.tag & LIB_TAG_COPIED_ON_WRITE_EVAL_RESULT) != 0);
3031 return object->runtime.mesh_eval;
3033 /* Wasn't evaluated yet. */
3034 return object->data;
3037 /* Get mesh which is not affected by modifiers:
3038 * - For original objects it will be same as object->data, and it is a mesh
3039 * which is in the corresponding bmain.
3040 * - For copied-on-write objects it will give pointer to a copied-on-write
3041 * mesh which corresponds to original object's mesh.
3043 Mesh *BKE_object_get_pre_modified_mesh(Object *object)
3045 if (object->runtime.mesh_orig != NULL) {
3046 BLI_assert(object->id.tag & LIB_TAG_COPIED_ON_WRITE);
3047 BLI_assert(object->id.orig_id != NULL);
3048 BLI_assert(object->runtime.mesh_orig->id.orig_id == ((Object *)object->id.orig_id)->data);
3049 Mesh *result = object->runtime.mesh_orig;
3050 BLI_assert((result->id.tag & LIB_TAG_COPIED_ON_WRITE) != 0);
3051 BLI_assert((result->id.tag & LIB_TAG_COPIED_ON_WRITE_EVAL_RESULT) == 0);
3054 BLI_assert((object->id.tag & LIB_TAG_COPIED_ON_WRITE) == 0);
3055 return object->data;
3058 /* Get a mesh which corresponds to very very original mesh from bmain.
3059 * - For original objects it will be object->data.
3060 * - For evaluated objects it will be same mesh as corresponding original
3061 * object uses as data.
3063 Mesh *BKE_object_get_original_mesh(Object *object)
3065 Mesh *result = NULL;
3066 if (object->id.orig_id == NULL) {
3067 BLI_assert((object->id.tag & LIB_TAG_COPIED_ON_WRITE) == 0);
3068 result = object->data;
3071 BLI_assert((object->id.tag & LIB_TAG_COPIED_ON_WRITE) != 0);
3072 result = ((Object *)object->id.orig_id)->data;
3074 BLI_assert(result != NULL);
3075 BLI_assert((result->id.tag & (LIB_TAG_COPIED_ON_WRITE | LIB_TAG_COPIED_ON_WRITE_EVAL_RESULT)) == 0);
3079 static int pc_cmp(const void *a, const void *b)
3081 const LinkData *ad = a, *bd = b;
3082 if (POINTER_AS_INT(ad->data) > POINTER_AS_INT(bd->data))
3087 int BKE_object_insert_ptcache(Object *ob)
3089 LinkData *link = NULL;
3092 BLI_listbase_sort(&ob->pc_ids, pc_cmp);
3094 for (link = ob->pc_ids.first, i = 0; link; link = link->next, i++) {
3095 int index = POINTER_AS_INT(link->data);
3101 link = MEM_callocN(sizeof(LinkData), "PCLink");
3102 link->data = POINTER_FROM_INT(i);
3103 BLI_addtail(&ob->pc_ids, link);
3108 static int pc_findindex(ListBase *listbase, int index)
3110 LinkData *link = NULL;
3113 if (listbase == NULL) return -1;
3115 link = listbase->first;
3117 if (POINTER_AS_INT(link->data) == index)
3127 void BKE_object_delete_ptcache(Object *ob, int index)
3129 int list_index = pc_findindex(&ob->pc_ids, index);
3130 LinkData *link = BLI_findlink(&ob->pc_ids, list_index);
3131 BLI_freelinkN(&ob->pc_ids, link);
3134 /* shape key utility function */
3136 /************************* Mesh ************************/
3137 static KeyBlock *insert_meshkey(Main *bmain, Object *ob, const char *name, const bool from_mix)
3139 Mesh *me = ob->data;
3145 key = me->key = BKE_key_add(bmain, (ID *)me);
3146 key->type = KEY_RELATIVE;
3150 if (newkey || from_mix == false) {
3151 /* create from mesh */
3152 kb = BKE_keyblock_add_ctime(key, name, false);
3153 BKE_keyblock_convert_from_mesh(me, key, kb);
3156 /* copy from current values */
3158 float *data = BKE_key_evaluate_object(ob, &totelem);
3160 /* create new block with prepared data */
3161 kb = BKE_keyblock_add_ctime(key, name, false);
3163 kb->totelem = totelem;
3168 /************************* Lattice ************************/
3169 static KeyBlock *insert_lattkey(Main *bmain, Object *ob, const char *name, const bool from_mix)
3171 Lattice *lt = ob->data;
3177 key = lt->key = BKE_key_add(bmain, (ID *)lt);
3178 key->type = KEY_RELATIVE;
3182 if (newkey || from_mix == false) {
3183 kb = BKE_keyblock_add_ctime(key, name, false);
3185 KeyBlock *basekb = (KeyBlock *)key->block.first;
3186 kb->data = MEM_dupallocN(basekb->data);
3187 kb->totelem = basekb->totelem;
3190 BKE_keyblock_convert_from_lattice(lt, kb);
3194 /* copy from current values */
3196 float *data = BKE_key_evaluate_object(ob, &totelem);
3198 /* create new block with prepared data */
3199 kb = BKE_keyblock_add_ctime(key, name, false);
3200 kb->totelem = totelem;
3206 /************************* Curve ************************/
3207 static KeyBlock *insert_curvekey(Main *bmain, Object *ob, const char *name, const bool from_mix)
3209 Curve *cu = ob->data;
3212 ListBase *lb = BKE_curve_nurbs_get(cu);
3216 key = cu->key = BKE_key_add(bmain, (ID *)cu);
3217 key->type = KEY_RELATIVE;
3221 if (newkey || from_mix == false) {
3222 /* create from curve */
3223 kb = BKE_keyblock_add_ctime(key, name, false);
3225 KeyBlock *basekb = (KeyBlock *)key->block.first;
3226 kb->data = MEM_dupallocN(basekb->data);
3227 kb->totelem = basekb->totelem;
3230 BKE_keyblock_convert_from_curve(cu, kb, lb);
3234 /* copy from current values */
3236 float *data = BKE_key_evaluate_object(ob, &totelem);
3238 /* create new block with prepared data */
3239 kb = BKE_keyblock_add_ctime(key, name, false);
3240 kb->totelem = totelem;
3247 KeyBlock *BKE_object_shapekey_insert(Main *bmain, Object *ob, const char *name, const bool from_mix)
3251 return insert_meshkey(bmain, ob, name, from_mix);
3254 return insert_curvekey(bmain, ob, name, from_mix);
3256 return insert_lattkey(bmain, ob, name, from_mix);
3263 bool BKE_object_shapekey_free(Main *bmain, Object *ob)
3267 key_p = BKE_key_from_object_p(ob);
3268 if (ELEM(NULL, key_p, *key_p)) {
3275 BKE_libblock_free_us(bmain, key);
3280 bool BKE_object_shapekey_remove(Main *bmain, Object *ob, KeyBlock *kb)
3283 Key *key = BKE_key_from_object(ob);
3290 kb_index = BLI_findindex(&key->block, kb);
3291 BLI_assert(kb_index != -1);
3293 for (rkb = key->block.first;