2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17 * All rights reserved.
31 #include "MEM_guardedalloc.h"
33 #include "DNA_anim_types.h"
34 #include "DNA_armature_types.h"
35 #include "DNA_camera_types.h"
36 #include "DNA_collection_types.h"
37 #include "DNA_constraint_types.h"
38 #include "DNA_gpencil_types.h"
39 #include "DNA_gpencil_modifier_types.h"
40 #include "DNA_key_types.h"
41 #include "DNA_light_types.h"
42 #include "DNA_lattice_types.h"
43 #include "DNA_material_types.h"
44 #include "DNA_meta_types.h"
45 #include "DNA_mesh_types.h"
46 #include "DNA_meshdata_types.h"
47 #include "DNA_movieclip_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_sequence_types.h"
51 #include "DNA_shader_fx_types.h"
52 #include "DNA_smoke_types.h"
53 #include "DNA_space_types.h"
54 #include "DNA_view3d_types.h"
55 #include "DNA_world_types.h"
56 #include "DNA_object_types.h"
57 #include "DNA_lightprobe_types.h"
58 #include "DNA_rigidbody_types.h"
60 #include "BLI_blenlib.h"
62 #include "BLI_threads.h"
63 #include "BLI_utildefines.h"
64 #include "BLI_linklist.h"
65 #include "BLI_kdtree.h"
67 #include "BLT_translation.h"
71 #include "BKE_global.h"
72 #include "BKE_idprop.h"
73 #include "BKE_armature.h"
74 #include "BKE_action.h"
75 #include "BKE_deform.h"
76 #include "BKE_DerivedMesh.h"
77 #include "BKE_animsys.h"
79 #include "BKE_collection.h"
80 #include "BKE_constraint.h"
81 #include "BKE_curve.h"
82 #include "BKE_displist.h"
83 #include "BKE_effect.h"
84 #include "BKE_fcurve.h"
85 #include "BKE_gpencil_modifier.h"
86 #include "BKE_icons.h"
88 #include "BKE_light.h"
89 #include "BKE_layer.h"
90 #include "BKE_lattice.h"
91 #include "BKE_library.h"
92 #include "BKE_library_query.h"
93 #include "BKE_library_remap.h"
94 #include "BKE_linestyle.h"
96 #include "BKE_editmesh.h"
97 #include "BKE_mball.h"
98 #include "BKE_modifier.h"
99 #include "BKE_multires.h"
100 #include "BKE_node.h"
101 #include "BKE_object.h"
102 #include "BKE_object_facemap.h"
103 #include "BKE_paint.h"
104 #include "BKE_particle.h"
105 #include "BKE_pointcache.h"
106 #include "BKE_lightprobe.h"
107 #include "BKE_rigidbody.h"
108 #include "BKE_scene.h"
109 #include "BKE_sequencer.h"
110 #include "BKE_shader_fx.h"
111 #include "BKE_speaker.h"
112 #include "BKE_softbody.h"
113 #include "BKE_subsurf.h"
114 #include "BKE_subdiv_ccg.h"
115 #include "BKE_material.h"
116 #include "BKE_camera.h"
117 #include "BKE_image.h"
118 #include "BKE_gpencil.h"
120 #include "DEG_depsgraph.h"
121 #include "DEG_depsgraph_query.h"
123 #include "DRW_engine.h"
125 #ifdef WITH_MOD_FLUID
126 #include "LBM_fluidsim.h"
130 #include "BPY_extern.h"
133 #include "CCGSubSurf.h"
134 #include "atomic_ops.h"
136 static CLG_LogRef LOG = {"bke.object"};
138 /* Vertex parent modifies original BMesh which is not safe for threading.
139 * Ideally such a modification should be handled as a separate DAG update
140 * callback for mesh datablock, but for until it is actually supported use
141 * simpler solution with a mutex lock.
144 #define VPARENT_THREADING_HACK
146 #ifdef VPARENT_THREADING_HACK
147 static ThreadMutex vparent_lock = BLI_MUTEX_INITIALIZER;
150 void BKE_object_workob_clear(Object *workob)
152 memset(workob, 0, sizeof(Object));
154 workob->scale[0] = workob->scale[1] = workob->scale[2] = 1.0f;
155 workob->dscale[0] = workob->dscale[1] = workob->dscale[2] = 1.0f;
156 workob->rotmode = ROT_MODE_EUL;
159 void BKE_object_free_particlesystems(Object *ob)
161 ParticleSystem *psys;
163 while ((psys = BLI_pophead(&ob->particlesystem))) {
168 void BKE_object_free_softbody(Object *ob)
173 void BKE_object_free_curve_cache(Object *ob)
175 if (ob->runtime.curve_cache) {
176 BKE_displist_free(&ob->runtime.curve_cache->disp);
177 BKE_curve_bevelList_free(&ob->runtime.curve_cache->bev);
178 if (ob->runtime.curve_cache->path) {
179 free_path(ob->runtime.curve_cache->path);
181 BKE_nurbList_free(&ob->runtime.curve_cache->deformed_nurbs);
182 MEM_freeN(ob->runtime.curve_cache);
183 ob->runtime.curve_cache = NULL;
187 void BKE_object_free_modifiers(Object *ob, const int flag)
190 GpencilModifierData *gp_md;
192 while ((md = BLI_pophead(&ob->modifiers))) {
193 modifier_free_ex(md, flag);
196 while ((gp_md = BLI_pophead(&ob->greasepencil_modifiers))) {
197 BKE_gpencil_modifier_free_ex(gp_md, flag);
199 /* particle modifiers were freed, so free the particlesystems as well */
200 BKE_object_free_particlesystems(ob);
202 /* same for softbody */
203 BKE_object_free_softbody(ob);
205 /* modifiers may have stored data in the DM cache */
206 BKE_object_free_derived_caches(ob);
209 void BKE_object_free_shaderfx(Object *ob, const int flag)
213 while ((fx = BLI_pophead(&ob->shader_fx))) {
214 BKE_shaderfx_free_ex(fx, flag);
218 void BKE_object_modifier_hook_reset(Object *ob, HookModifierData *hmd)
220 /* reset functionality */
222 bPoseChannel *pchan = BKE_pose_channel_find_name(hmd->object->pose, hmd->subtarget);
224 if (hmd->subtarget[0] && pchan) {
225 float imat[4][4], mat[4][4];
227 /* calculate the world-space matrix for the pose-channel target first, then carry on as usual */
228 mul_m4_m4m4(mat, hmd->object->obmat, pchan->pose_mat);
230 invert_m4_m4(imat, mat);
231 mul_m4_m4m4(hmd->parentinv, imat, ob->obmat);
234 invert_m4_m4(hmd->object->imat, hmd->object->obmat);
235 mul_m4_m4m4(hmd->parentinv, hmd->object->imat, ob->obmat);
240 void BKE_object_modifier_gpencil_hook_reset(Object *ob, HookGpencilModifierData *hmd)
242 if (hmd->object == NULL) {
245 /* reset functionality */
246 bPoseChannel *pchan = BKE_pose_channel_find_name(hmd->object->pose, hmd->subtarget);
248 if (hmd->subtarget[0] && pchan) {
249 float imat[4][4], mat[4][4];
251 /* calculate the world-space matrix for the pose-channel target first, then carry on as usual */
252 mul_m4_m4m4(mat, hmd->object->obmat, pchan->pose_mat);
254 invert_m4_m4(imat, mat);
255 mul_m4_m4m4(hmd->parentinv, imat, ob->obmat);
258 invert_m4_m4(hmd->object->imat, hmd->object->obmat);
259 mul_m4_m4m4(hmd->parentinv, hmd->object->imat, ob->obmat);
263 bool BKE_object_support_modifier_type_check(const Object *ob, int modifier_type)
265 const ModifierTypeInfo *mti;
267 mti = modifierType_getInfo(modifier_type);
269 /* only geometry objects should be able to get modifiers [#25291] */
270 if (!ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
274 if (ob->type == OB_LATTICE && (mti->flags & eModifierTypeFlag_AcceptsLattice) == 0) {
278 if (!((mti->flags & eModifierTypeFlag_AcceptsCVs) ||
279 (ob->type == OB_MESH && (mti->flags & eModifierTypeFlag_AcceptsMesh))))
287 void BKE_object_link_modifiers(Scene *scene, struct Object *ob_dst, const struct Object *ob_src)
290 BKE_object_free_modifiers(ob_dst, 0);
292 if (!ELEM(ob_dst->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
293 /* only objects listed above can have modifiers and linking them to objects
294 * which doesn't have modifiers stack is quite silly */
298 for (md = ob_src->modifiers.first; md; md = md->next) {
299 ModifierData *nmd = NULL;
303 eModifierType_Collision))
308 if (!BKE_object_support_modifier_type_check(ob_dst, md->type))
312 case eModifierType_Softbody:
313 BKE_object_copy_softbody(ob_dst, ob_src, 0);
315 case eModifierType_Skin:
316 /* ensure skin-node customdata exists */
317 BKE_mesh_ensure_skin_customdata(ob_dst->data);
321 nmd = modifier_new(md->type);
322 BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
324 if (md->type == eModifierType_Multires) {
325 /* Has to be done after mod creation, but *before* we actually copy its settings! */
326 multiresModifier_sync_levels_ex(scene, ob_dst, (MultiresModifierData *)md, (MultiresModifierData *)nmd);
329 modifier_copyData(md, nmd);
330 BLI_addtail(&ob_dst->modifiers, nmd);
331 modifier_unique_name(&ob_dst->modifiers, nmd);
334 BKE_object_copy_particlesystems(ob_dst, ob_src, 0);
336 /* TODO: smoke?, cloth? */
339 /* Copy CCG related data. Used to sync copy of mesh with reshaped original
342 static void copy_ccg_data(Mesh *mesh_destination,
346 BLI_assert(mesh_destination->totloop == mesh_source->totloop);
347 CustomData *data_destination = &mesh_destination->ldata;
348 CustomData *data_source = &mesh_source->ldata;
349 const int num_elements = mesh_source->totloop;
350 if (!CustomData_has_layer(data_source, layer_type)) {
353 const int layer_index = CustomData_get_layer_index(
354 data_destination, layer_type);
355 CustomData_free_layer(
356 data_destination, layer_type, num_elements, layer_index);
357 BLI_assert(!CustomData_has_layer(data_destination, layer_type));
358 CustomData_add_layer(
359 data_destination, layer_type, CD_CALLOC, NULL, num_elements);
360 BLI_assert(CustomData_has_layer(data_destination, layer_type));
361 CustomData_copy_layer_type_data(data_source, data_destination,
362 layer_type, 0, 0, num_elements);
365 static void object_update_from_subsurf_ccg(Object *object)
367 /* Currently CCG is only created for Mesh objects. */
368 if (object->type != OB_MESH) {
371 /* Object was never evaluated, so can not have CCG subdivision surface. */
372 Mesh *mesh_eval = object->runtime.mesh_eval;
373 if (mesh_eval == NULL) {
376 SubdivCCG *subdiv_ccg = mesh_eval->runtime.subdiv_ccg;
377 if (subdiv_ccg == NULL) {
380 /* Check whether there is anything to be reshaped. */
381 if (!subdiv_ccg->dirty.coords && !subdiv_ccg->dirty.hidden) {
384 const int tot_level = mesh_eval->runtime.subdiv_ccg_tot_level;
385 Object *object_orig = DEG_get_original_object(object);
386 Mesh *mesh_orig = (Mesh *)object_orig->data;
387 multiresModifier_reshapeFromCCG(tot_level, mesh_orig, subdiv_ccg);
388 /* NOTE: we need to reshape into an original mesh from main database,
391 * - Update copies of that mesh at any moment.
392 * - Save the file without doing extra reshape.
393 * - All the users of the mesh have updated displacement.
395 * However, the tricky part here is that we only know about sculpted
396 * state of a mesh on an object level, and object is being updated after
397 * mesh datablock is updated. This forces us to:
399 * - Update mesh datablock from object evaluation, which is technically
400 * forbidden, but there is no other place for this yet.
401 * - Reshape to the original mesh from main database, and then copy updated
402 * layer to copy of that mesh (since copy of the mesh has decoupled
403 * custom data layers).
405 * All this is defeating all the designs we need to follow to allow safe
406 * threaded evaluation, but this is as good as we can make it within the
407 * current sculpt//evaluated mesh design. This is also how we've survived
408 * with old DerivedMesh based solutions. So, while this is all wrong and
409 * needs reconsideration, doesn't seem to be a big stopper for real
410 * production artists.
412 /* TODO(sergey): Solve this somehow, to be fully stable for threaded
413 * evaluation environment.
415 /* NOTE: runtime.mesh_orig is what was before assigning mesh_eval,
416 * it is orig as in what was in object_eval->data before evaluating
419 * mesh_cow is a copy-on-written version od object_orig->data.
421 Mesh *mesh_cow = object->runtime.mesh_orig;
422 copy_ccg_data(mesh_cow, mesh_orig, CD_MDISPS);
423 copy_ccg_data(mesh_cow, mesh_orig, CD_GRID_PAINT_MASK);
424 /* Everything is now up-to-date. */
425 subdiv_ccg->dirty.coords = false;
426 subdiv_ccg->dirty.hidden = false;
429 /* free data derived from mesh, called when mesh changes or is freed */
430 void BKE_object_free_derived_caches(Object *ob)
432 /* Also serves as signal to remake texspace.
434 * NOTE: This function can be called from threads on different objects
435 * sharing same data datablock. So we need to ensure atomic nature of
436 * data modification here.
438 if (ob->type == OB_MESH) {
442 atomic_fetch_and_or_int32(&me->bb->flag, BOUNDBOX_DIRTY);
445 else if (ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) {
446 Curve *cu = ob->data;
449 atomic_fetch_and_or_int32(&cu->bb->flag, BOUNDBOX_DIRTY);
453 MEM_SAFE_FREE(ob->runtime.bb);
455 object_update_from_subsurf_ccg(ob);
456 BKE_object_free_derived_mesh_caches(ob);
457 BKE_armature_cached_bbone_deformation_free(ob);
459 if (ob->runtime.mesh_eval != NULL) {
460 Mesh *mesh_eval = ob->runtime.mesh_eval;
461 /* Restore initial pointer. */
462 if (ob->data == mesh_eval) {
463 ob->data = ob->runtime.mesh_orig;
465 /* Evaluated mesh points to edit mesh, but does not own it. */
466 mesh_eval->edit_mesh = NULL;
467 BKE_mesh_free(mesh_eval);
468 BKE_libblock_free_data(&mesh_eval->id, false);
469 MEM_freeN(mesh_eval);
470 ob->runtime.mesh_eval = NULL;
472 if (ob->runtime.mesh_deform_eval != NULL) {
473 Mesh *mesh_deform_eval = ob->runtime.mesh_deform_eval;
474 BKE_mesh_free(mesh_deform_eval);
475 BKE_libblock_free_data(&mesh_deform_eval->id, false);
476 MEM_freeN(mesh_deform_eval);
477 ob->runtime.mesh_deform_eval = NULL;
480 BKE_object_free_curve_cache(ob);
482 /* clear grease pencil data */
483 DRW_gpencil_freecache(ob);
486 void BKE_object_free_derived_mesh_caches(struct Object *ob)
488 if (ob->derivedFinal) {
489 ob->derivedFinal->needsFree = 1;
490 ob->derivedFinal->release(ob->derivedFinal);
491 ob->derivedFinal = NULL;
493 if (ob->derivedDeform) {
494 ob->derivedDeform->needsFree = 1;
495 ob->derivedDeform->release(ob->derivedDeform);
496 ob->derivedDeform = NULL;
500 void BKE_object_free_caches(Object *object)
503 short update_flag = 0;
505 /* Free particle system caches holding paths. */
506 if (object->particlesystem.first) {
507 ParticleSystem *psys;
508 for (psys = object->particlesystem.first;
512 psys_free_path_cache(psys, psys->edit);
513 update_flag |= ID_RECALC_PSYS_REDO;
517 /* Free memory used by cached derived meshes in the particle system modifiers. */
518 for (md = object->modifiers.first; md != NULL; md = md->next) {
519 if (md->type == eModifierType_ParticleSystem) {
520 ParticleSystemModifierData *psmd = (ParticleSystemModifierData *) md;
521 if (psmd->mesh_final) {
522 BKE_id_free(NULL, psmd->mesh_final);
523 psmd->mesh_final = NULL;
524 if (psmd->mesh_original) {
525 BKE_id_free(NULL, psmd->mesh_original);
526 psmd->mesh_original = NULL;
528 psmd->flag |= eParticleSystemFlag_file_loaded;
529 update_flag |= ID_RECALC_GEOMETRY;
534 /* NOTE: If object is coming from a duplicator, it might be a temporary
535 * object created by dependency graph, which shares pointers with original
536 * object. In this case we can not free anything.
538 if ((object->base_flag & BASE_FROM_DUPLI) == 0) {
539 BKE_object_free_derived_caches(object);
540 update_flag |= ID_RECALC_GEOMETRY;
543 /* Tag object for update, so once memory critical operation is over and
544 * scene update routines are back to it's business the object will be
545 * guaranteed to be in a known state.
547 if (update_flag != 0) {
548 DEG_id_tag_update(&object->id, update_flag);
552 /** Free (or release) any data used by this object (does not free the object itself). */
553 void BKE_object_free(Object *ob)
555 BKE_animdata_free((ID *)ob, false);
557 DRW_drawdata_free((ID *)ob);
559 /* BKE_<id>_free shall never touch to ID->us. Never ever. */
560 BKE_object_free_modifiers(ob, LIB_ID_CREATE_NO_USER_REFCOUNT);
561 BKE_object_free_shaderfx(ob, LIB_ID_CREATE_NO_USER_REFCOUNT);
563 MEM_SAFE_FREE(ob->mat);
564 MEM_SAFE_FREE(ob->matbits);
565 MEM_SAFE_FREE(ob->iuser);
566 MEM_SAFE_FREE(ob->runtime.bb);
568 BLI_freelistN(&ob->defbase);
569 BLI_freelistN(&ob->fmaps);
571 BKE_pose_free_ex(ob->pose, false);
575 animviz_free_motionpath(ob->mpath);
579 BKE_constraints_free_ex(&ob->constraints, false);
581 BKE_partdeflect_free(ob->pd);
582 BKE_rigidbody_free_object(ob, NULL);
583 BKE_rigidbody_free_constraint(ob);
587 BKE_sculptsession_free(ob);
589 BLI_freelistN(&ob->pc_ids);
591 BLI_freelistN(&ob->lodlevels);
593 /* Free runtime curves data. */
594 if (ob->runtime.curve_cache) {
595 BKE_curve_bevelList_free(&ob->runtime.curve_cache->bev);
596 if (ob->runtime.curve_cache->path)
597 free_path(ob->runtime.curve_cache->path);
598 MEM_freeN(ob->runtime.curve_cache);
599 ob->runtime.curve_cache = NULL;
602 BKE_previewimg_free(&ob->preview);
605 /* actual check for internal data, not context or flags */
606 bool BKE_object_is_in_editmode(const Object *ob)
608 if (ob->data == NULL) {
614 return ((Mesh *)ob->data)->edit_mesh != NULL;
616 return ((bArmature *)ob->data)->edbo != NULL;
618 return ((Curve *)ob->data)->editfont != NULL;
620 return ((MetaBall *)ob->data)->editelems != NULL;
622 return ((Lattice *)ob->data)->editlatt != NULL;
625 return ((Curve *)ob->data)->editnurb != NULL;
631 bool BKE_object_is_in_editmode_vgroup(const Object *ob)
633 return (OB_TYPE_SUPPORT_VGROUP(ob->type) &&
634 BKE_object_is_in_editmode(ob));
637 bool BKE_object_data_is_in_editmode(const ID *id)
639 const short type = GS(id->name);
640 BLI_assert(OB_DATA_SUPPORT_EDITMODE(type));
643 return ((const Mesh *)id)->edit_mesh != NULL;
646 (((const Curve *)id)->editnurb != NULL) ||
647 (((const Curve *)id)->editfont != NULL)
650 return ((const MetaBall *)id)->editelems != NULL;
652 return ((const Lattice *)id)->editlatt != NULL;
654 return ((const bArmature *)id)->edbo != NULL;
661 bool BKE_object_is_in_wpaint_select_vert(const Object *ob)
663 if (ob->type == OB_MESH) {
665 return ((ob->mode & OB_MODE_WEIGHT_PAINT) &&
666 (me->edit_mesh == NULL) &&
667 (ME_EDIT_PAINT_SEL_MODE(me) == SCE_SELECT_VERTEX));
673 bool BKE_object_has_mode_data(const struct Object *ob, eObjectMode object_mode)
675 if (object_mode & OB_MODE_EDIT) {
676 if (BKE_object_is_in_editmode(ob)) {
680 else if (object_mode & OB_MODE_VERTEX_PAINT) {
681 if (ob->sculpt && (ob->sculpt->mode_type == OB_MODE_VERTEX_PAINT)) {
685 else if (object_mode & OB_MODE_WEIGHT_PAINT) {
686 if (ob->sculpt && (ob->sculpt->mode_type == OB_MODE_WEIGHT_PAINT)) {
690 else if (object_mode & OB_MODE_SCULPT) {
691 if (ob->sculpt && (ob->sculpt->mode_type == OB_MODE_SCULPT)) {
695 else if (object_mode & OB_MODE_POSE) {
696 if (ob->pose != NULL) {
703 bool BKE_object_is_mode_compat(const struct Object *ob, eObjectMode object_mode)
705 return ((ob->mode == object_mode) ||
706 (ob->mode & object_mode) != 0);
710 * Return which parts of the object are visible, as evaluated by depsgraph
712 int BKE_object_visibility(const Object *ob, const int dag_eval_mode)
714 if ((ob->base_flag & BASE_VISIBLE) == 0) {
718 /* Test which components the object has. */
719 int visibility = OB_VISIBLE_SELF;
720 if (ob->particlesystem.first) {
721 visibility |= OB_VISIBLE_INSTANCES | OB_VISIBLE_PARTICLES;
723 else if (ob->transflag & OB_DUPLI) {
724 visibility |= OB_VISIBLE_INSTANCES;
727 /* Optional hiding of self if there are particles or instancers. */
728 if (visibility & (OB_VISIBLE_PARTICLES | OB_VISIBLE_INSTANCES)) {
729 switch ((eEvaluationMode)dag_eval_mode) {
730 case DAG_EVAL_VIEWPORT:
731 if (!(ob->duplicator_visibility_flag & OB_DUPLI_FLAG_VIEWPORT)) {
732 visibility &= ~OB_VISIBLE_SELF;
735 case DAG_EVAL_RENDER:
736 if (!(ob->duplicator_visibility_flag & OB_DUPLI_FLAG_RENDER)) {
737 visibility &= ~OB_VISIBLE_SELF;
746 bool BKE_object_exists_check(Main *bmain, const Object *obtest)
750 if (obtest == NULL) return false;
752 ob = bmain->object.first;
754 if (ob == obtest) return true;
760 /* *************************************************** */
762 static const char *get_obdata_defname(int type)
765 case OB_MESH: return DATA_("Mesh");
766 case OB_CURVE: return DATA_("Curve");
767 case OB_SURF: return DATA_("Surf");
768 case OB_FONT: return DATA_("Text");
769 case OB_MBALL: return DATA_("Mball");
770 case OB_CAMERA: return DATA_("Camera");
771 case OB_LAMP: return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "Light");
772 case OB_LATTICE: return DATA_("Lattice");
773 case OB_ARMATURE: return DATA_("Armature");
774 case OB_SPEAKER: return DATA_("Speaker");
775 case OB_EMPTY: return DATA_("Empty");
776 case OB_GPENCIL: return DATA_("GPencil");
778 CLOG_ERROR(&LOG, "Internal error, bad type: %d", type);
779 return DATA_("Empty");
783 void *BKE_object_obdata_add_from_type(Main *bmain, int type, const char *name)
786 name = get_obdata_defname(type);
790 case OB_MESH: return BKE_mesh_add(bmain, name);
791 case OB_CURVE: return BKE_curve_add(bmain, name, OB_CURVE);
792 case OB_SURF: return BKE_curve_add(bmain, name, OB_SURF);
793 case OB_FONT: return BKE_curve_add(bmain, name, OB_FONT);
794 case OB_MBALL: return BKE_mball_add(bmain, name);
795 case OB_CAMERA: return BKE_camera_add(bmain, name);
796 case OB_LAMP: return BKE_light_add(bmain, name);
797 case OB_LATTICE: return BKE_lattice_add(bmain, name);
798 case OB_ARMATURE: return BKE_armature_add(bmain, name);
799 case OB_SPEAKER: return BKE_speaker_add(bmain, name);
800 case OB_LIGHTPROBE:return BKE_lightprobe_add(bmain, name);
801 case OB_GPENCIL: return BKE_gpencil_data_addnew(bmain, name);
802 case OB_EMPTY: return NULL;
804 CLOG_ERROR(&LOG, "Internal error, bad type: %d", type);
809 void BKE_object_init(Object *ob)
811 /* BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(ob, id)); */ /* ob->type is already initialized... */
813 copy_v4_fl(ob->color, 1.0f);
815 ob->scale[0] = ob->scale[1] = ob->scale[2] = 1.0;
816 ob->dscale[0] = ob->dscale[1] = ob->dscale[2] = 1.0;
818 /* objects should default to having Euler XYZ rotations,
819 * but rotations default to quaternions
821 ob->rotmode = ROT_MODE_EUL;
823 unit_axis_angle(ob->rotAxis, &ob->rotAngle);
824 unit_axis_angle(ob->drotAxis, &ob->drotAngle);
829 /* rotation locks should be 4D for 4 component rotations by default... */
830 ob->protectflag = OB_LOCK_ROT4D;
832 unit_m4(ob->constinv);
833 unit_m4(ob->parentinv);
836 ob->empty_drawtype = OB_PLAINAXES;
837 ob->empty_drawsize = 1.0;
838 ob->empty_image_depth = OB_EMPTY_IMAGE_DEPTH_DEFAULT;
839 if (ob->type == OB_EMPTY) {
840 copy_v2_fl(ob->ima_ofs, -0.5f);
843 if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
844 ob->trackflag = OB_NEGZ;
845 ob->upflag = OB_POSY;
848 ob->trackflag = OB_POSY;
849 ob->upflag = OB_POSZ;
852 ob->instance_faces_scale = 1.0;
854 ob->col_group = 0x01;
855 ob->col_mask = 0xffff;
857 ob->duplicator_visibility_flag = OB_DUPLI_FLAG_VIEWPORT | OB_DUPLI_FLAG_RENDER;
859 /* NT fluid sim defaults */
860 ob->fluidsimSettings = NULL;
862 BLI_listbase_clear(&ob->pc_ids);
864 /* Animation Visualization defaults */
865 animviz_settings_init(&ob->avs);
868 /* more general add: creates minimum required data, but without vertices etc. */
869 Object *BKE_object_add_only_object(Main *bmain, int type, const char *name)
874 name = get_obdata_defname(type);
876 ob = BKE_libblock_alloc(bmain, ID_OB, name, 0);
878 /* We increase object user count when linking to Collections. */
881 /* default object vars */
890 static Object *object_add_common(Main *bmain, ViewLayer *view_layer, int type, const char *name)
894 ob = BKE_object_add_only_object(bmain, type, name);
895 ob->data = BKE_object_obdata_add_from_type(bmain, type, name);
896 BKE_view_layer_base_deselect_all(view_layer);
898 DEG_id_tag_update_ex(bmain, &ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
903 * General add: to scene, with layer from area and default name
905 * Object is added to the active Collection.
906 * If there is no linked collection to the active ViewLayer we create a new one.
908 /* creates minimum required data, but without vertices etc. */
909 Object *BKE_object_add(
910 Main *bmain, Scene *UNUSED(scene), ViewLayer *view_layer,
911 int type, const char *name)
915 LayerCollection *layer_collection;
917 ob = object_add_common(bmain, view_layer, type, name);
919 layer_collection = BKE_layer_collection_get_active(view_layer);
920 BKE_collection_object_add(bmain, layer_collection->collection, ob);
922 base = BKE_view_layer_base_find(view_layer, ob);
923 BKE_view_layer_base_select_and_set_active(view_layer, base);
929 * Add a new object, using another one as a reference
931 * \param ob_src: object to use to determine the collections of the new object.
933 Object *BKE_object_add_from(
934 Main *bmain, Scene *scene, ViewLayer *view_layer,
935 int type, const char *name, Object *ob_src)
940 ob = object_add_common(bmain, view_layer, type, name);
941 BKE_collection_object_add_from(bmain, scene, ob_src, ob);
943 base = BKE_view_layer_base_find(view_layer, ob);
944 BKE_view_layer_base_select_and_set_active(view_layer, base);
950 * Add a new object, but assign the given datablock as the ob->data
951 * for the newly created object.
953 * \param data: The datablock to assign as ob->data for the new object.
954 * This is assumed to be of the correct type.
955 * \param do_id_user: If true, id_us_plus() will be called on data when
956 * assigning it to the object.
958 Object *BKE_object_add_for_data(
959 Main *bmain, ViewLayer *view_layer,
960 int type, const char *name, ID *data, bool do_id_user)
964 LayerCollection *layer_collection;
966 /* same as object_add_common, except we don't create new ob->data */
967 ob = BKE_object_add_only_object(bmain, type, name);
969 if (do_id_user) id_us_plus(data);
971 BKE_view_layer_base_deselect_all(view_layer);
972 DEG_id_tag_update_ex(bmain, &ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
974 layer_collection = BKE_layer_collection_get_active(view_layer);
975 BKE_collection_object_add(bmain, layer_collection->collection, ob);
977 base = BKE_view_layer_base_find(view_layer, ob);
978 BKE_view_layer_base_select_and_set_active(view_layer, base);
984 void BKE_object_copy_softbody(struct Object *ob_dst, const struct Object *ob_src, const int flag)
986 SoftBody *sb = ob_src->soft;
988 bool tagged_no_main = ob_dst->id.tag & LIB_TAG_NO_MAIN;
990 ob_dst->softflag = ob_src->softflag;
996 sbn = MEM_dupallocN(sb);
998 if ((flag & LIB_ID_COPY_CACHES) == 0) {
999 sbn->totspring = sbn->totpoint = 0;
1001 sbn->bspring = NULL;
1004 sbn->totspring = sb->totspring;
1005 sbn->totpoint = sb->totpoint;
1010 sbn->bpoint = MEM_dupallocN(sbn->bpoint);
1012 for (i = 0; i < sbn->totpoint; i++) {
1013 if (sbn->bpoint[i].springs)
1014 sbn->bpoint[i].springs = MEM_dupallocN(sbn->bpoint[i].springs);
1019 sbn->bspring = MEM_dupallocN(sb->bspring);
1023 sbn->totkey = sbn->totpointkey = 0;
1025 sbn->scratch = NULL;
1027 if (tagged_no_main == 0) {
1028 sbn->shared = MEM_dupallocN(sb->shared);
1029 sbn->shared->pointcache = BKE_ptcache_copy_list(&sbn->shared->ptcaches, &sb->shared->ptcaches, flag);
1032 if (sb->effector_weights)
1033 sbn->effector_weights = MEM_dupallocN(sb->effector_weights);
1038 ParticleSystem *BKE_object_copy_particlesystem(ParticleSystem *psys, const int flag)
1040 ParticleSystem *psysn = MEM_dupallocN(psys);
1042 psys_copy_particles(psysn, psys);
1045 psysn->clmd = (ClothModifierData *)modifier_new(eModifierType_Cloth);
1046 modifier_copyData_ex((ModifierData *)psys->clmd, (ModifierData *)psysn->clmd, flag);
1047 psys->hair_in_mesh = psys->hair_out_mesh = NULL;
1050 BLI_duplicatelist(&psysn->targets, &psys->targets);
1052 psysn->pathcache = NULL;
1053 psysn->childcache = NULL;
1056 psysn->effectors = NULL;
1058 psysn->bvhtree = NULL;
1059 psysn->batch_cache = NULL;
1061 BLI_listbase_clear(&psysn->pathcachebufs);
1062 BLI_listbase_clear(&psysn->childcachebufs);
1064 if (flag & LIB_ID_CREATE_NO_MAIN) {
1065 BLI_assert((psys->flag & PSYS_SHARED_CACHES) == 0);
1066 psysn->flag |= PSYS_SHARED_CACHES;
1067 BLI_assert(psysn->pointcache != NULL);
1070 psysn->pointcache = BKE_ptcache_copy_list(&psysn->ptcaches, &psys->ptcaches, flag);
1073 /* XXX - from reading existing code this seems correct but intended usage of
1074 * pointcache should /w cloth should be added in 'ParticleSystem' - campbell */
1076 psysn->clmd->point_cache = psysn->pointcache;
1079 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
1080 id_us_plus((ID *)psysn->part);
1086 void BKE_object_copy_particlesystems(Object *ob_dst, const Object *ob_src, const int flag)
1088 ParticleSystem *psys, *npsys;
1091 if (ob_dst->type != OB_MESH) {
1092 /* currently only mesh objects can have soft body */
1096 BLI_listbase_clear(&ob_dst->particlesystem);
1097 for (psys = ob_src->particlesystem.first; psys; psys = psys->next) {
1098 npsys = BKE_object_copy_particlesystem(psys, flag);
1100 BLI_addtail(&ob_dst->particlesystem, npsys);
1102 /* need to update particle modifiers too */
1103 for (md = ob_dst->modifiers.first; md; md = md->next) {
1104 if (md->type == eModifierType_ParticleSystem) {
1105 ParticleSystemModifierData *psmd = (ParticleSystemModifierData *)md;
1106 if (psmd->psys == psys)
1109 else if (md->type == eModifierType_DynamicPaint) {
1110 DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
1112 if (pmd->brush->psys == psys) {
1113 pmd->brush->psys = npsys;
1117 else if (md->type == eModifierType_Smoke) {
1118 SmokeModifierData *smd = (SmokeModifierData *) md;
1120 if (smd->type == MOD_SMOKE_TYPE_FLOW) {
1122 if (smd->flow->psys == psys)
1123 smd->flow->psys = npsys;
1131 static void copy_object_pose(Object *obn, const Object *ob, const int flag)
1135 /* note: need to clear obn->pose pointer first,
1136 * so that BKE_pose_copy_data works (otherwise there's a crash) */
1138 BKE_pose_copy_data_ex(&obn->pose, ob->pose, flag, true); /* true = copy constraints */
1140 for (chan = obn->pose->chanbase.first; chan; chan = chan->next) {
1143 chan->flag &= ~(POSE_LOC | POSE_ROT | POSE_SIZE);
1145 /* XXX Remapping object pointing onto itself should be handled by generic BKE_library_remap stuff, but...
1146 * the flush_constraint_targets callback am not sure about, so will delay that for now. */
1147 for (con = chan->constraints.first; con; con = con->next) {
1148 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
1149 ListBase targets = {NULL, NULL};
1150 bConstraintTarget *ct;
1152 if (cti && cti->get_constraint_targets) {
1153 cti->get_constraint_targets(con, &targets);
1155 for (ct = targets.first; ct; ct = ct->next) {
1160 if (cti->flush_constraint_targets)
1161 cti->flush_constraint_targets(con, &targets, 0);
1167 static void copy_object_lod(Object *obn, const Object *ob, const int UNUSED(flag))
1169 BLI_duplicatelist(&obn->lodlevels, &ob->lodlevels);
1171 obn->currentlod = (LodLevel *)obn->lodlevels.first;
1174 bool BKE_object_pose_context_check(const Object *ob)
1177 (ob->type == OB_ARMATURE) &&
1179 (ob->mode & OB_MODE_POSE))
1188 Object *BKE_object_pose_armature_get(Object *ob)
1193 if (BKE_object_pose_context_check(ob))
1196 ob = modifiers_isDeformedByArmature(ob);
1198 /* Only use selected check when non-active. */
1199 if (BKE_object_pose_context_check(ob))
1205 Object *BKE_object_pose_armature_get_visible(Object *ob, ViewLayer *view_layer, View3D *v3d)
1207 Object *ob_armature = BKE_object_pose_armature_get(ob);
1209 Base *base = BKE_view_layer_base_find(view_layer, ob_armature);
1211 if (BASE_VISIBLE(v3d, base)) {
1220 * Access pose array with special check to get pose object when in weight paint mode.
1222 Object **BKE_object_pose_array_get_ex(ViewLayer *view_layer, View3D *v3d, uint *r_objects_len, bool unique)
1224 Object *ob_active = OBACT(view_layer);
1225 Object *ob_pose = BKE_object_pose_armature_get(ob_active);
1226 Object **objects = NULL;
1227 if (ob_pose == ob_active) {
1228 objects = BKE_view_layer_array_from_objects_in_mode(
1229 view_layer, v3d, r_objects_len, {
1230 .object_mode = OB_MODE_POSE,
1231 .no_dup_data = unique,
1234 else if (ob_pose != NULL) {
1236 objects = MEM_mallocN(sizeof(*objects), __func__);
1237 objects[0] = ob_pose;
1241 objects = MEM_mallocN(0, __func__);
1245 Object **BKE_object_pose_array_get_unique(ViewLayer *view_layer, View3D *v3d, uint *r_objects_len)
1247 return BKE_object_pose_array_get_ex(view_layer, v3d, r_objects_len, true);
1249 Object **BKE_object_pose_array_get(ViewLayer *view_layer, View3D *v3d, uint *r_objects_len)
1251 return BKE_object_pose_array_get_ex(view_layer, v3d, r_objects_len, false);
1254 Base **BKE_object_pose_base_array_get_ex(ViewLayer *view_layer, View3D *v3d, uint *r_bases_len, bool unique)
1256 Base *base_active = BASACT(view_layer);
1257 Object *ob_pose = base_active ? BKE_object_pose_armature_get(base_active->object) : NULL;
1258 Base *base_pose = NULL;
1259 Base **bases = NULL;
1262 if (ob_pose == base_active->object) {
1263 base_pose = base_active;
1266 base_pose = BKE_view_layer_base_find(view_layer, ob_pose);
1270 if (base_active && (base_pose == base_active)) {
1271 bases = BKE_view_layer_array_from_bases_in_mode(
1272 view_layer, v3d, r_bases_len, {
1273 .object_mode = OB_MODE_POSE,
1274 .no_dup_data = unique,
1277 else if (base_pose != NULL) {
1279 bases = MEM_mallocN(sizeof(*bases), __func__);
1280 bases[0] = base_pose;
1284 bases = MEM_mallocN(0, __func__);
1288 Base **BKE_object_pose_base_array_get_unique(ViewLayer *view_layer, View3D *v3d, uint *r_bases_len)
1290 return BKE_object_pose_base_array_get_ex(view_layer, v3d, r_bases_len, true);
1292 Base **BKE_object_pose_base_array_get(ViewLayer *view_layer, View3D *v3d, uint *r_bases_len)
1294 return BKE_object_pose_base_array_get_ex(view_layer, v3d, r_bases_len, false);
1297 void BKE_object_transform_copy(Object *ob_tar, const Object *ob_src)
1299 copy_v3_v3(ob_tar->loc, ob_src->loc);
1300 copy_v3_v3(ob_tar->rot, ob_src->rot);
1301 copy_v3_v3(ob_tar->quat, ob_src->quat);
1302 copy_v3_v3(ob_tar->rotAxis, ob_src->rotAxis);
1303 ob_tar->rotAngle = ob_src->rotAngle;
1304 ob_tar->rotmode = ob_src->rotmode;
1305 copy_v3_v3(ob_tar->scale, ob_src->scale);
1309 * Only copy internal data of Object ID from source to already allocated/initialized destination.
1310 * You probably never want to use that directly, use BKE_id_copy or BKE_id_copy_ex for typical needs.
1312 * WARNING! This function will not handle ID user count!
1314 * \param flag: Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
1316 void BKE_object_copy_data(Main *bmain, Object *ob_dst, const Object *ob_src, const int flag)
1319 GpencilModifierData *gmd;
1322 /* Do not copy runtime data. */
1323 BKE_object_runtime_reset_on_copy(ob_dst, flag);
1325 /* We never handle usercount here for own data. */
1326 const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
1328 if (ob_src->totcol) {
1329 ob_dst->mat = MEM_dupallocN(ob_src->mat);
1330 ob_dst->matbits = MEM_dupallocN(ob_src->matbits);
1331 ob_dst->totcol = ob_src->totcol;
1333 else if (ob_dst->mat != NULL || ob_dst->matbits != NULL) {
1334 /* This shall not be needed, but better be safe than sorry. */
1335 BLI_assert(!"Object copy: non-NULL material pointers with zero counter, should not happen.");
1337 ob_dst->matbits = NULL;
1340 if (ob_src->iuser) ob_dst->iuser = MEM_dupallocN(ob_src->iuser);
1342 if (ob_src->runtime.bb) ob_dst->runtime.bb = MEM_dupallocN(ob_src->runtime.bb);
1344 BLI_listbase_clear(&ob_dst->modifiers);
1346 for (md = ob_src->modifiers.first; md; md = md->next) {
1347 ModifierData *nmd = modifier_new(md->type);
1348 BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
1349 modifier_copyData_ex(md, nmd, flag_subdata);
1350 BLI_addtail(&ob_dst->modifiers, nmd);
1353 BLI_listbase_clear(&ob_dst->greasepencil_modifiers);
1355 for (gmd = ob_src->greasepencil_modifiers.first; gmd; gmd = gmd->next) {
1356 GpencilModifierData *nmd = BKE_gpencil_modifier_new(gmd->type);
1357 BLI_strncpy(nmd->name, gmd->name, sizeof(nmd->name));
1358 BKE_gpencil_modifier_copyData_ex(gmd, nmd, flag_subdata);
1359 BLI_addtail(&ob_dst->greasepencil_modifiers, nmd);
1362 BLI_listbase_clear(&ob_dst->shader_fx);
1364 for (fx = ob_src->shader_fx.first; fx; fx = fx->next) {
1365 ShaderFxData *nfx = BKE_shaderfx_new(fx->type);
1366 BLI_strncpy(nfx->name, fx->name, sizeof(nfx->name));
1367 BKE_shaderfx_copyData_ex(fx, nfx, flag_subdata);
1368 BLI_addtail(&ob_dst->shader_fx, nfx);
1372 copy_object_pose(ob_dst, ob_src, flag_subdata);
1373 /* backwards compat... non-armatures can get poses in older files? */
1374 if (ob_src->type == OB_ARMATURE) {
1375 const bool do_pose_id_user = (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0;
1376 BKE_pose_rebuild(bmain, ob_dst, ob_dst->data, do_pose_id_user);
1379 defgroup_copy_list(&ob_dst->defbase, &ob_src->defbase);
1380 BKE_object_facemap_copy_list(&ob_dst->fmaps, &ob_src->fmaps);
1381 BKE_constraints_copy_ex(&ob_dst->constraints, &ob_src->constraints, flag_subdata, true);
1383 ob_dst->mode = ob_dst->type != OB_GPENCIL ? OB_MODE_OBJECT : ob_dst->mode;
1384 ob_dst->sculpt = NULL;
1387 ob_dst->pd = MEM_dupallocN(ob_src->pd);
1388 if (ob_dst->pd->rng) {
1389 ob_dst->pd->rng = MEM_dupallocN(ob_src->pd->rng);
1392 BKE_object_copy_softbody(ob_dst, ob_src, flag_subdata);
1393 ob_dst->rigidbody_object = BKE_rigidbody_copy_object(ob_src, flag_subdata);
1394 ob_dst->rigidbody_constraint = BKE_rigidbody_copy_constraint(ob_src, flag_subdata);
1396 BKE_object_copy_particlesystems(ob_dst, ob_src, flag_subdata);
1398 ob_dst->derivedDeform = NULL;
1399 ob_dst->derivedFinal = NULL;
1401 BLI_listbase_clear((ListBase *)&ob_dst->drawdata);
1402 BLI_listbase_clear(&ob_dst->pc_ids);
1404 ob_dst->avs = ob_src->avs;
1405 ob_dst->mpath = animviz_copy_motionpath(ob_src->mpath);
1407 copy_object_lod(ob_dst, ob_src, flag_subdata);
1409 /* Do not copy object's preview (mostly due to the fact renderers create temp copy of objects). */
1410 if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0 && false) { /* XXX TODO temp hack */
1411 BKE_previewimg_id_copy(&ob_dst->id, &ob_src->id);
1414 ob_dst->preview = NULL;
1418 /* copy objects, will re-initialize cached simulation data */
1419 Object *BKE_object_copy(Main *bmain, const Object *ob)
1422 BKE_id_copy(bmain, &ob->id, (ID **)&ob_copy);
1424 /* We increase object user count when linking to Collections. */
1425 id_us_min(&ob_copy->id);
1430 void BKE_object_make_local_ex(Main *bmain, Object *ob, const bool lib_local, const bool clear_proxy)
1432 bool is_local = false, is_lib = false;
1434 /* - only lib users: do nothing (unless force_local is set)
1435 * - only local users: set flag
1436 * - mixed: make copy
1437 * In case we make a whole lib's content local, we always want to localize, and we skip remapping (done later).
1440 if (!ID_IS_LINKED(ob)) {
1444 BKE_library_ID_test_usages(bmain, ob, &is_local, &is_lib);
1446 if (lib_local || is_local) {
1448 id_clear_lib_data(bmain, &ob->id);
1449 BKE_id_expand_local(bmain, &ob->id);
1451 if (ob->proxy_from != NULL) {
1452 ob->proxy_from->proxy = NULL;
1453 ob->proxy_from->proxy_group = NULL;
1455 ob->proxy = ob->proxy_from = ob->proxy_group = NULL;
1459 Object *ob_new = BKE_object_copy(bmain, ob);
1462 ob_new->proxy = ob_new->proxy_from = ob_new->proxy_group = NULL;
1464 /* setting newid is mandatory for complex make_lib_local logic... */
1465 ID_NEW_SET(ob, ob_new);
1468 BKE_libblock_remap(bmain, ob, ob_new, ID_REMAP_SKIP_INDIRECT_USAGE);
1474 void BKE_object_make_local(Main *bmain, Object *ob, const bool lib_local)
1476 BKE_object_make_local_ex(bmain, ob, lib_local, true);
1479 /* Returns true if the Object is from an external blend file (libdata) */
1480 bool BKE_object_is_libdata(const Object *ob)
1482 return (ob && ID_IS_LINKED(ob));
1485 /* Returns true if the Object data is from an external blend file (libdata) */
1486 bool BKE_object_obdata_is_libdata(const Object *ob)
1488 /* Linked objects with local obdata are forbidden! */
1489 BLI_assert(!ob || !ob->data || (ID_IS_LINKED(ob) ? ID_IS_LINKED(ob->data) : true));
1490 return (ob && ob->data && ID_IS_LINKED(ob->data));
1493 /* *************** PROXY **************** */
1495 /* when you make proxy, ensure the exposed layers are extern */
1496 static void armature_set_id_extern(Object *ob)
1498 bArmature *arm = ob->data;
1499 bPoseChannel *pchan;
1500 unsigned int lay = arm->layer_protected;
1502 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1503 if (!(pchan->bone->layer & lay))
1504 id_lib_extern((ID *)pchan->custom);
1509 void BKE_object_copy_proxy_drivers(Object *ob, Object *target)
1511 if ((target->adt) && (target->adt->drivers.first)) {
1514 /* add new animdata block */
1516 ob->adt = BKE_animdata_add_id(&ob->id);
1518 /* make a copy of all the drivers (for now), then correct any links that need fixing */
1519 free_fcurves(&ob->adt->drivers);
1520 copy_fcurves(&ob->adt->drivers, &target->adt->drivers);
1522 for (fcu = ob->adt->drivers.first; fcu; fcu = fcu->next) {
1523 ChannelDriver *driver = fcu->driver;
1526 for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
1528 DRIVER_TARGETS_LOOPER_BEGIN(dvar)
1531 if ((Object *)dtar->id == target)
1532 dtar->id = (ID *)ob;
1534 /* only on local objects because this causes indirect links
1535 * 'a -> b -> c', blend to point directly to a.blend
1536 * when a.blend has a proxy thats linked into c.blend */
1537 if (!ID_IS_LINKED(ob))
1538 id_lib_extern((ID *)dtar->id);
1542 DRIVER_TARGETS_LOOPER_END;
1548 /* proxy rule: lib_object->proxy_from == the one we borrow from, set temporally while object_update */
1549 /* local_object->proxy == pointer to library object, saved in files and read */
1550 /* local_object->proxy_group == pointer to collection dupli-object, saved in files and read */
1552 void BKE_object_make_proxy(Main *bmain, Object *ob, Object *target, Object *cob)
1554 /* paranoia checks */
1555 if (ID_IS_LINKED(ob) || !ID_IS_LINKED(target)) {
1556 CLOG_ERROR(&LOG, "cannot make proxy");
1561 ob->proxy_group = cob;
1562 id_lib_extern(&target->id);
1564 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1565 DEG_id_tag_update(&target->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1568 * - cob means this proxy comes from a collection, just apply the matrix
1569 * so the object wont move from its dupli-transform.
1571 * - no cob means this is being made from a linked object,
1572 * this is closer to making a copy of the object - in-place. */
1574 ob->rotmode = target->rotmode;
1575 mul_m4_m4m4(ob->obmat, cob->obmat, target->obmat);
1576 if (cob->instance_collection) { /* should always be true */
1578 mul_v3_mat3_m4v3(tvec, ob->obmat, cob->instance_collection->instance_offset);
1579 sub_v3_v3(ob->obmat[3], tvec);
1581 BKE_object_apply_mat4(ob, ob->obmat, false, true);
1584 BKE_object_transform_copy(ob, target);
1585 ob->parent = target->parent; /* libdata */
1586 copy_m4_m4(ob->parentinv, target->parentinv);
1589 /* copy animdata stuff - drivers only for now... */
1590 BKE_object_copy_proxy_drivers(ob, target);
1592 /* skip constraints? */
1593 /* FIXME: this is considered by many as a bug */
1595 /* set object type and link to data */
1596 ob->type = target->type;
1597 ob->data = target->data;
1598 id_us_plus((ID *)ob->data); /* ensures lib data becomes LIB_TAG_EXTERN */
1600 /* copy vertex groups */
1601 defgroup_copy_list(&ob->defbase, &target->defbase);
1603 /* copy material and index information */
1604 ob->actcol = ob->totcol = 0;
1605 if (ob->mat) MEM_freeN(ob->mat);
1606 if (ob->matbits) MEM_freeN(ob->matbits);
1609 if ((target->totcol) && (target->mat) && OB_TYPE_SUPPORT_MATERIAL(ob->type)) {
1612 ob->actcol = target->actcol;
1613 ob->totcol = target->totcol;
1615 ob->mat = MEM_dupallocN(target->mat);
1616 ob->matbits = MEM_dupallocN(target->matbits);
1617 for (i = 0; i < target->totcol; i++) {
1618 /* don't need to run test_object_materials
1619 * since we know this object is new and not used elsewhere */
1620 id_us_plus((ID *)ob->mat[i]);
1624 /* type conversions */
1625 if (target->type == OB_ARMATURE) {
1626 copy_object_pose(ob, target, 0); /* data copy, object pointers in constraints */
1627 BKE_pose_rest(ob->pose); /* clear all transforms in channels */
1628 BKE_pose_rebuild(bmain, ob, ob->data, true); /* set all internal links */
1630 armature_set_id_extern(ob);
1632 else if (target->type == OB_EMPTY) {
1633 ob->empty_drawtype = target->empty_drawtype;
1634 ob->empty_drawsize = target->empty_drawsize;
1637 /* copy IDProperties */
1638 if (ob->id.properties) {
1639 IDP_FreeProperty(ob->id.properties);
1640 MEM_freeN(ob->id.properties);
1641 ob->id.properties = NULL;
1643 if (target->id.properties) {
1644 ob->id.properties = IDP_CopyProperty(target->id.properties);
1647 /* copy drawtype info */
1648 ob->dt = target->dt;
1652 * Use with newly created objects to set their size
1653 * (used to apply scene-scale).
1655 void BKE_object_obdata_size_init(struct Object *ob, const float size)
1657 /* apply radius as a scale to types that support it */
1661 ob->empty_drawsize *= size;
1666 Curve *cu = ob->data;
1672 Camera *cam = ob->data;
1673 cam->drawsize *= size;
1678 Light *lamp = ob->data;
1680 lamp->area_size *= size;
1681 lamp->area_sizey *= size;
1682 lamp->area_sizez *= size;
1685 /* Only lattice (not mesh, curve, mball...),
1686 * because its got data when newly added */
1689 struct Lattice *lt = ob->data;
1693 scale_m4_fl(mat, size);
1695 BKE_lattice_transform(lt, (float (*)[4])mat, false);
1701 /* *************** CALC ****************** */
1703 void BKE_object_scale_to_mat3(Object *ob, float mat[3][3])
1706 mul_v3_v3v3(vec, ob->scale, ob->dscale);
1707 size_to_mat3(mat, vec);
1710 void BKE_object_rot_to_mat3(Object *ob, float mat[3][3], bool use_drot)
1712 float rmat[3][3], dmat[3][3];
1714 /* 'dmat' is the delta-rotation matrix, which will get (pre)multiplied
1715 * with the rotation matrix to yield the appropriate rotation
1718 /* rotations may either be quats, eulers (with various rotation orders), or axis-angle */
1719 if (ob->rotmode > 0) {
1720 /* euler rotations (will cause gimble lock, but this can be alleviated a bit with rotation orders) */
1721 eulO_to_mat3(rmat, ob->rot, ob->rotmode);
1722 eulO_to_mat3(dmat, ob->drot, ob->rotmode);
1724 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
1725 /* axis-angle - not really that great for 3D-changing orientations */
1726 axis_angle_to_mat3(rmat, ob->rotAxis, ob->rotAngle);
1727 axis_angle_to_mat3(dmat, ob->drotAxis, ob->drotAngle);
1730 /* quats are normalized before use to eliminate scaling issues */
1733 normalize_qt_qt(tquat, ob->quat);
1734 quat_to_mat3(rmat, tquat);
1736 normalize_qt_qt(tquat, ob->dquat);
1737 quat_to_mat3(dmat, tquat);
1740 /* combine these rotations */
1742 mul_m3_m3m3(mat, dmat, rmat);
1744 copy_m3_m3(mat, rmat);
1747 void BKE_object_mat3_to_rot(Object *ob, float mat[3][3], bool use_compat)
1749 BLI_ASSERT_UNIT_M3(mat);
1751 switch (ob->rotmode) {
1755 mat3_normalized_to_quat(ob->quat, mat);
1756 normalize_qt_qt(dquat, ob->dquat);
1757 invert_qt_normalized(dquat);
1758 mul_qt_qtqt(ob->quat, dquat, ob->quat);
1761 case ROT_MODE_AXISANGLE:
1766 /* without drot we could apply 'mat' directly */
1767 mat3_normalized_to_quat(quat, mat);
1768 axis_angle_to_quat(dquat, ob->drotAxis, ob->drotAngle);
1769 invert_qt_normalized(dquat);
1770 mul_qt_qtqt(quat, dquat, quat);
1771 quat_to_axis_angle(ob->rotAxis, &ob->rotAngle, quat);
1774 default: /* euler */
1779 /* without drot we could apply 'mat' directly */
1780 mat3_normalized_to_quat(quat, mat);
1781 eulO_to_quat(dquat, ob->drot, ob->rotmode);
1782 invert_qt_normalized(dquat);
1783 mul_qt_qtqt(quat, dquat, quat);
1784 /* end drot correction */
1786 if (use_compat) quat_to_compatible_eulO(ob->rot, ob->rot, ob->rotmode, quat);
1787 else quat_to_eulO(ob->rot, ob->rotmode, quat);
1793 void BKE_object_tfm_protected_backup(const Object *ob,
1794 ObjectTfmProtectedChannels *obtfm)
1797 #define TFMCPY(_v) (obtfm->_v = ob->_v)
1798 #define TFMCPY3D(_v) copy_v3_v3(obtfm->_v, ob->_v)
1799 #define TFMCPY4D(_v) copy_v4_v4(obtfm->_v, ob->_v)
1820 void BKE_object_tfm_protected_restore(Object *ob,
1821 const ObjectTfmProtectedChannels *obtfm,
1822 const short protectflag)
1826 for (i = 0; i < 3; i++) {
1827 if (protectflag & (OB_LOCK_LOCX << i)) {
1828 ob->loc[i] = obtfm->loc[i];
1829 ob->dloc[i] = obtfm->dloc[i];
1832 if (protectflag & (OB_LOCK_SCALEX << i)) {
1833 ob->scale[i] = obtfm->scale[i];
1834 ob->dscale[i] = obtfm->dscale[i];
1837 if (protectflag & (OB_LOCK_ROTX << i)) {
1838 ob->rot[i] = obtfm->rot[i];
1839 ob->drot[i] = obtfm->drot[i];
1841 ob->quat[i + 1] = obtfm->quat[i + 1];
1842 ob->dquat[i + 1] = obtfm->dquat[i + 1];
1844 ob->rotAxis[i] = obtfm->rotAxis[i];
1845 ob->drotAxis[i] = obtfm->drotAxis[i];
1849 if ((protectflag & OB_LOCK_ROT4D) && (protectflag & OB_LOCK_ROTW)) {
1850 ob->quat[0] = obtfm->quat[0];
1851 ob->dquat[0] = obtfm->dquat[0];
1853 ob->rotAngle = obtfm->rotAngle;
1854 ob->drotAngle = obtfm->drotAngle;
1858 void BKE_object_tfm_copy(Object *object_dst, const Object *object_src)
1860 #define TFMCPY(_v) (object_dst->_v = object_src->_v)
1861 #define TFMCPY3D(_v) copy_v3_v3(object_dst->_v, object_src->_v)
1862 #define TFMCPY4D(_v) copy_v4_v4(object_dst->_v, object_src->_v)
1882 void BKE_object_to_mat3(Object *ob, float mat[3][3]) /* no parent */
1889 BKE_object_scale_to_mat3(ob, smat);
1892 BKE_object_rot_to_mat3(ob, rmat, true);
1893 mul_m3_m3m3(mat, rmat, smat);
1896 void BKE_object_to_mat4(Object *ob, float mat[4][4])
1900 BKE_object_to_mat3(ob, tmat);
1902 copy_m4_m3(mat, tmat);
1904 add_v3_v3v3(mat[3], ob->loc, ob->dloc);
1907 void BKE_object_matrix_local_get(struct Object *ob, float mat[4][4])
1910 float par_imat[4][4];
1912 BKE_object_get_parent_matrix(ob, ob->parent, par_imat);
1913 invert_m4(par_imat);
1914 mul_m4_m4m4(mat, par_imat, ob->obmat);
1917 copy_m4_m4(mat, ob->obmat);
1922 * \param depsgraph: Used for dupli-frame time.
1923 * \return success if \a mat is set.
1925 static bool ob_parcurve(Object *ob, Object *par, float mat[4][4])
1927 Curve *cu = par->data;
1928 float vec[4], dir[3], quat[4], radius, ctime;
1930 /* NOTE: Curve cache is supposed to be evaluated here already, however there
1931 * are cases where we can not guarantee that. This includes, for example,
1932 * dependency cycles. We can't correct anything from here, since that would
1933 * cause a threading conflicts.
1935 * TODO(sergey): Somce of the legit looking cases like T56619 need to be
1936 * looked into, and maybe curve cache (and other dependencies) are to be
1937 * evaluated prior to conversion. */
1938 if (par->runtime.curve_cache == NULL) {
1941 if (par->runtime.curve_cache->path == NULL) {
1945 /* ctime is now a proper var setting of Curve which gets set by Animato like any other var that's animated,
1946 * but this will only work if it actually is animated...
1948 * we divide the curvetime calculated in the previous step by the length of the path, to get a time
1949 * factor, which then gets clamped to lie within 0.0 - 1.0 range
1952 ctime = cu->ctime / cu->pathlen;
1957 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 CLOG_ERROR(&LOG, "Object %s with Bone parent: bone %s doesn't exist", 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_mesh;
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);
2077 CLOG_ERROR(&LOG, "Evaluated mesh is needed to solve parenting, "
2078 "object position can be wrong now");
2081 else if (ELEM(par->type, OB_CURVE, OB_SURF)) {
2084 /* Unless there's some weird depsgraph failure the cache should exist. */
2085 BLI_assert(par->runtime.curve_cache != NULL);
2087 if (par->runtime.curve_cache->deformed_nurbs.first != NULL) {
2088 nurb = &par->runtime.curve_cache->deformed_nurbs;
2091 Curve *cu = par->data;
2092 nurb = BKE_curve_nurbs_get(cu);
2095 BKE_nurbList_index_get_co(nurb, nr, vec);
2097 else if (par->type == OB_LATTICE) {
2098 Lattice *latt = par->data;
2099 DispList *dl = par->runtime.curve_cache ? BKE_displist_find(&par->runtime.curve_cache->disp, DL_VERTS) : NULL;
2100 float (*co)[3] = dl ? (float (*)[3])dl->verts : NULL;
2103 if (latt->editlatt) latt = latt->editlatt->latt;
2105 tot = latt->pntsu * latt->pntsv * latt->pntsw;
2107 /* ensure dl is correct size */
2108 BLI_assert(dl == NULL || dl->nr == tot);
2112 copy_v3_v3(vec, co[nr]);
2115 copy_v3_v3(vec, latt->def[nr].vec);
2121 static void ob_parvert3(Object *ob, Object *par, float mat[4][4])
2124 /* in local ob space */
2125 if (OB_TYPE_SUPPORT_PARVERT(par->type)) {
2126 float cmat[3][3], v1[3], v2[3], v3[3], q[4];
2128 give_parvert(par, ob->par1, v1);
2129 give_parvert(par, ob->par2, v2);
2130 give_parvert(par, ob->par3, v3);
2132 tri_to_quat(q, v1, v2, v3);
2133 quat_to_mat3(cmat, q);
2134 copy_m4_m3(mat, cmat);
2136 mid_v3_v3v3v3(mat[3], v1, v2, v3);
2143 void BKE_object_get_parent_matrix(Object *ob, Object *par, float parentmat[4][4])
2149 switch (ob->partype & PARTYPE) {
2152 if (par->type == OB_CURVE) {
2153 if ((((Curve *)par->data)->flag & CU_PATH) &&
2154 (ob_parcurve(ob, par, tmat)))
2160 if (ok) mul_m4_m4m4(parentmat, par->obmat, tmat);
2161 else copy_m4_m4(parentmat, par->obmat);
2165 ob_parbone(ob, par, tmat);
2166 mul_m4_m4m4(parentmat, par->obmat, tmat);
2171 give_parvert(par, ob->par1, vec);
2172 mul_v3_m4v3(parentmat[3], par->obmat, vec);
2175 ob_parvert3(ob, par, tmat);
2177 mul_m4_m4m4(parentmat, par->obmat, tmat);
2181 copy_m4_m4(parentmat, par->obmat);
2187 * \param r_originmat: Optional matrix that stores the space the object is in (without its own matrix applied)
2189 static void solve_parenting(Object *ob, Object *par, float obmat[4][4],
2190 float r_originmat[3][3], const bool set_origin)
2196 BKE_object_to_mat4(ob, locmat);
2198 BKE_object_get_parent_matrix(ob, par, totmat);
2201 mul_m4_m4m4(tmat, totmat, ob->parentinv);
2202 mul_m4_m4m4(obmat, tmat, locmat);
2205 /* usable originmat */
2206 copy_m3_m4(r_originmat, tmat);
2209 /* origin, for help line */
2211 if ((ob->partype & PARTYPE) == PARSKEL) {
2212 copy_v3_v3(ob->runtime.parent_display_origin, par->obmat[3]);
2215 copy_v3_v3(ob->runtime.parent_display_origin, totmat[3]);
2220 /* note, scene is the active scene while actual_scene is the scene the object resides in */
2221 static void object_where_is_calc_ex(
2222 Depsgraph *depsgraph, Scene *scene, Object *ob, float ctime,
2223 RigidBodyWorld *rbw, float r_originmat[3][3])
2226 Object *par = ob->parent;
2228 /* calculate parent matrix */
2229 solve_parenting(ob, par, ob->obmat, r_originmat, true);
2232 BKE_object_to_mat4(ob, ob->obmat);
2235 /* try to fall back to the scene rigid body world if none given */
2236 rbw = rbw ? rbw : scene->rigidbody_world;
2237 /* read values pushed into RBO from sim/cache... */
2238 BKE_rigidbody_sync_transforms(rbw, ob, ctime);
2240 /* solve constraints */
2241 if (ob->constraints.first && !(ob->transflag & OB_NO_CONSTRAINTS)) {
2243 cob = BKE_constraints_make_evalob(depsgraph, scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2244 BKE_constraints_solve(depsgraph, &ob->constraints, cob, ctime);
2245 BKE_constraints_clear_evalob(cob);
2248 /* set negative scale flag in object */
2249 if (is_negative_m4(ob->obmat)) ob->transflag |= OB_NEG_SCALE;
2250 else ob->transflag &= ~OB_NEG_SCALE;
2253 void BKE_object_where_is_calc_time(Depsgraph *depsgraph, Scene *scene, Object *ob, float ctime)
2255 /* Execute drivers and animation. */
2256 BKE_animsys_evaluate_animdata(depsgraph, scene, &ob->id, ob->adt, ctime, ADT_RECALC_ALL);
2257 object_where_is_calc_ex(depsgraph, scene, ob, ctime, NULL, NULL);
2260 /* get object transformation matrix without recalculating dependencies and
2261 * constraints -- assume dependencies are already solved by depsgraph.
2262 * no changes to object and it's parent would be done.
2263 * used for bundles orientation in 3d space relative to parented blender camera */
2264 void BKE_object_where_is_calc_mat4(Object *ob, float obmat[4][4])
2267 Object *par = ob->parent;
2268 solve_parenting(ob, par, obmat, NULL, false);
2271 BKE_object_to_mat4(ob, obmat);
2275 void BKE_object_where_is_calc_ex(Depsgraph *depsgraph, Scene *scene, RigidBodyWorld *rbw, Object *ob, float r_originmat[3][3])
2277 float ctime = DEG_get_ctime(depsgraph);
2278 object_where_is_calc_ex(depsgraph, scene, ob, ctime, rbw, r_originmat);
2280 void BKE_object_where_is_calc(Depsgraph *depsgraph, Scene *scene, Object *ob)
2282 float ctime = DEG_get_ctime(depsgraph);
2283 object_where_is_calc_ex(depsgraph, scene, ob, ctime, NULL, NULL);
2287 * For calculation of the inverse parent transform, only used for editor.
2289 * It assumes the object parent is already in the depsgraph.
2290 * Otherwise, after changing ob->parent you need to call:
2291 * - #DEG_relations_tag_update(bmain);
2292 * - #BKE_scene_graph_update_tagged(depsgraph, bmain);
2294 void BKE_object_workob_calc_parent(Depsgraph *depsgraph, Scene *scene, Object *ob, Object *workob)
2296 Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
2297 BKE_object_workob_clear(workob);
2299 unit_m4(workob->obmat);
2300 unit_m4(workob->parentinv);
2301 unit_m4(workob->constinv);
2303 /* Since this is used while calculating parenting, at this moment ob_eval->parent is still NULL. */
2304 workob->parent = DEG_get_evaluated_object(depsgraph, ob->parent);
2306 workob->trackflag = ob_eval->trackflag;
2307 workob->upflag = ob_eval->upflag;
2309 workob->partype = ob_eval->partype;
2310 workob->par1 = ob_eval->par1;
2311 workob->par2 = ob_eval->par2;
2312 workob->par3 = ob_eval->par3;
2314 workob->constraints = ob_eval->constraints;
2316 BLI_strncpy(workob->parsubstr, ob_eval->parsubstr, sizeof(workob->parsubstr));
2318 BKE_object_where_is_calc(depsgraph, scene, workob);
2322 * Applies the global transformation \a mat to the \a ob using a relative parent space if supplied.
2324 * \param mat: the global transformation mat that the object should be set object to.
2325 * \param parent: the parent space in which this object will be set relative to (should probably always be parent_eval).
2326 * \param use_compat: true to ensure that rotations are set using the min difference between the old and new orientation.
2328 void BKE_object_apply_mat4_ex(Object *ob, float mat[4][4], Object *parent, float parentinv[4][4], const bool use_compat)
2330 /* see BKE_pchan_apply_mat4() for the equivalent 'pchan' function */
2334 if (parent != NULL) {
2335 float rmat[4][4], diff_mat[4][4], imat[4][4], parent_mat[4][4];
2337 BKE_object_get_parent_matrix(ob, parent, parent_mat);
2339 mul_m4_m4m4(diff_mat, parent_mat, parentinv);
2340 invert_m4_m4(imat, diff_mat);
2341 mul_m4_m4m4(rmat, imat, mat); /* get the parent relative matrix */
2343 /* same as below, use rmat rather than mat */
2344 mat4_to_loc_rot_size(ob->loc, rot, ob->scale, rmat);
2347 mat4_to_loc_rot_size(ob->loc, rot, ob->scale, mat);
2350 BKE_object_mat3_to_rot(ob, rot, use_compat);
2352 sub_v3_v3(ob->loc, ob->dloc);
2354 if (ob->dscale[0] != 0.0f) ob->scale[0] /= ob->dscale[0];
2355 if (ob->dscale[1] != 0.0f) ob->scale[1] /= ob->dscale[1];
2356 if (ob->dscale[2] != 0.0f) ob->scale[2] /= ob->dscale[2];
2358 /* BKE_object_mat3_to_rot handles delta rotations */
2361 /* XXX: should be removed after COW operators port to use BKE_object_apply_mat4_ex directly */
2362 void BKE_object_apply_mat4(Object *ob, float mat[4][4], const bool use_compat, const bool use_parent)
2364 BKE_object_apply_mat4_ex(ob, mat, use_parent ? ob->parent : NULL, ob->parentinv, use_compat);
2367 BoundBox *BKE_boundbox_alloc_unit(void)
2370 const float min[3] = {-1.0f, -1.0f, -1.0f}, max[3] = {1.0f, 1.0f, 1.0f};
2372 bb = MEM_callocN(sizeof(BoundBox), "OB-BoundBox");
2373 BKE_boundbox_init_from_minmax(bb, min, max);
2378 void BKE_boundbox_init_from_minmax(BoundBox *bb, const float min[3], const float max[3])
2380 bb->vec[0][0] = bb->vec[1][0] = bb->vec[2][0] = bb->vec[3][0] = min[0];
2381 bb->vec[4][0] = bb->vec[5][0] = bb->vec[6][0] = bb->vec[7][0] = max[0];
2383 bb->vec[0][1] = bb->vec[1][1] = bb->vec[4][1] = bb->vec[5][1] = min[1];
2384 bb->vec[2][1] = bb->vec[3][1] = bb->vec[6][1] = bb->vec[7][1] = max[1];
2386 bb->vec[0][2] = bb->vec[3][2] = bb->vec[4][2] = bb->vec[7][2] = min[2];
2387 bb->vec[1][2] = bb->vec[2][2] = bb->vec[5][2] = bb->vec[6][2] = max[2];
2390 void BKE_boundbox_calc_center_aabb(const BoundBox *bb, float r_cent[3])
2392 r_cent[0] = 0.5f * (bb->vec[0][0] + bb->vec[4][0]);
2393 r_cent[1] = 0.5f * (bb->vec[0][1] + bb->vec[2][1]);
2394 r_cent[2] = 0.5f * (bb->vec[0][2] + bb->vec[1][2]);
2397 void BKE_boundbox_calc_size_aabb(const BoundBox *bb, float r_size[3])
2399 r_size[0] = 0.5f * fabsf(bb->vec[0][0] - bb->vec[4][0]);
2400 r_size[1] = 0.5f * fabsf(bb->vec[0][1] - bb->vec[2][1]);
2401 r_size[2] = 0.5f * fabsf(bb->vec[0][2] - bb->vec[1][2]);
2404 void BKE_boundbox_minmax(const BoundBox *bb, float obmat[4][4], float r_min[3], float r_max[3])
2407 for (i = 0; i < 8; i++) {
2409 mul_v3_m4v3(vec, obmat, bb->vec[i]);
2410 minmax_v3v3_v3(r_min, r_max, vec);
2414 BoundBox *BKE_object_boundbox_get(Object *ob)
2416 BoundBox *bb = NULL;
2420 bb = BKE_mesh_boundbox_get(ob);
2425 bb = BKE_curve_boundbox_get(ob);
2428 bb = BKE_mball_boundbox_get(ob);
2431 bb = BKE_lattice_boundbox_get(ob);
2434 bb = BKE_armature_boundbox_get(ob);
2437 bb = BKE_gpencil_boundbox_get(ob);
2445 /* used to temporally disable/enable boundbox */
2446 void BKE_object_boundbox_flag(Object *ob, int flag, const bool set)
2448 BoundBox *bb = BKE_object_boundbox_get(ob);
2450 if (set) bb->flag |= flag;
2451 else bb->flag &= ~flag;
2455 void BKE_object_boundbox_calc_from_mesh(struct Object *ob, struct Mesh *me_eval)
2457 float min[3], max[3];
2459 INIT_MINMAX(min, max);
2461 if (!BKE_mesh_minmax(me_eval, min, max)) {
2466 if (ob->runtime.bb == NULL) {
2467 ob->runtime.bb = MEM_callocN(sizeof(BoundBox), "DM-BoundBox");
2470 BKE_boundbox_init_from_minmax(ob->runtime.bb, min, max);
2472 ob->runtime.bb->flag &= ~BOUNDBOX_DIRTY;
2475 void BKE_object_dimensions_get(Object *ob, float vec[3])
2477 BoundBox *bb = NULL;
2479 bb = BKE_object_boundbox_get(ob);
2483 mat4_to_size(scale, ob->obmat);
2485 vec[0] = fabsf(scale[0]) * (bb->vec[4][0] - bb->vec[0][0]);
2486 vec[1] = fabsf(scale[1]) * (bb->vec[2][1] - bb->vec[0][1]);
2487 vec[2] = fabsf(scale[2]) * (bb->vec[1][2] - bb->vec[0][2]);
2494 void BKE_object_dimensions_set(Object *ob, const float value[3], int axis_mask)
2496 BoundBox *bb = NULL;
2498 bb = BKE_object_boundbox_get(ob);
2502 len[0] = bb->vec[4][0] - bb->vec[0][0];
2503 len[1] = bb->vec[2][1] - bb->vec[0][1];
2504 len[2] = bb->vec[1][2] - bb->vec[0][2];
2506 for (int i = 0; i < 3; i++) {
2507 if (((1 << i) & axis_mask) == 0) {
2508 if (len[i] > 0.0f) {
2509 ob->scale[i] = copysignf(value[i] / len[i], ob->scale[i]);
2516 void BKE_object_minmax(Object *ob, float min_r[3], float max_r[3], const bool use_hidden)
2520 bool changed = false;
2527 bb = *BKE_curve_boundbox_get(ob);
2528 BKE_boundbox_minmax(&bb, ob->obmat, min_r, max_r);
2534 Lattice *lt = ob->data;
2535 BPoint *bp = lt->def;
2538 for (w = 0; w < lt->pntsw; w++) {
2539 for (v = 0; v < lt->pntsv; v++) {
2540 for (u = 0; u < lt->pntsu; u++, bp++) {
2541 mul_v3_m4v3(vec, ob->obmat, bp->vec);
2542 minmax_v3v3_v3(min_r, max_r, vec);
2551 changed = BKE_pose_minmax(ob, min_r, max_r, use_hidden, false);
2556 Mesh *me = BKE_mesh_from_object(ob);
2559 bb = *BKE_mesh_boundbox_get(ob);
2560 BKE_boundbox_minmax(&bb, ob->obmat, min_r, max_r);
2567 float ob_min[3], ob_max[3];
2569 changed = BKE_mball_minmax_ex(ob->data, ob_min, ob_max, ob->obmat, 0);
2571 minmax_v3v3_v3(min_r, max_r, ob_min);
2572 minmax_v3v3_v3(min_r, max_r, ob_max);
2578 if (changed == false) {
2581 copy_v3_v3(size, ob->scale);
2582 if (ob->type == OB_EMPTY) {
2583 mul_v3_fl(size, ob->empty_drawsize);
2586 minmax_v3v3_v3(min_r, max_r, ob->obmat[3]);
2588 copy_v3_v3(vec, ob->obmat[3]);
2589 add_v3_v3(vec, size);
2590 minmax_v3v3_v3(min_r, max_r, vec);
2592 copy_v3_v3(vec, ob->obmat[3]);
2593 sub_v3_v3(vec, size);
2594 minmax_v3v3_v3(min_r, max_r, vec);
2598 void BKE_object_empty_draw_type_set(Object *ob, const int value)
2600 ob->empty_drawtype = value;
2602 if (ob->type == OB_EMPTY && ob->empty_drawtype == OB_EMPTY_IMAGE) {
2604 ob->iuser = MEM_callocN(sizeof(ImageUser), "image user");
2606 ob->iuser->flag |= IMA_ANIM_ALWAYS;
2607 ob->iuser->frames = 100;
2608 ob->iuser->sfra = 1;
2613 MEM_freeN(ob->iuser);
2619 bool BKE_object_empty_image_is_visible_in_view3d(const Object *ob, const RegionView3D *rv3d)
2621 char visibility_flag = ob->empty_image_visibility_flag;
2623 if ((visibility_flag & (OB_EMPTY_IMAGE_HIDE_BACK | OB_EMPTY_IMAGE_HIDE_FRONT)) != 0) {
2625 if (rv3d->is_persp) {
2626 /* Note, we could normalize the 'view_dir' then use 'eps'
2627 * however the issue with empty objects being visible when viewed from the side
2628 * is only noticeable in orthographic views. */
2630 sub_v3_v3v3(view_dir, rv3d->viewinv[3], ob->obmat[3]);
2631 dot = dot_v3v3(ob->obmat[2], view_dir);
2635 dot = dot_v3v3(ob->obmat[2], rv3d->viewinv[2]);
2638 if (visibility_flag & OB_EMPTY_IMAGE_HIDE_BACK) {
2643 if (visibility_flag & OB_EMPTY_IMAGE_HIDE_FRONT) {
2650 if (rv3d->is_persp) {
2651 return (visibility_flag & OB_EMPTY_IMAGE_HIDE_PERSPECTIVE) == 0;
2654 return (visibility_flag & OB_EMPTY_IMAGE_HIDE_ORTHOGRAPHIC) == 0;
2658 bool BKE_object_minmax_dupli(Depsgraph *depsgraph, Scene *scene, Object *ob, float r_min[3], float r_max[3], const bool use_hidden)
2661 if ((ob->transflag & OB_DUPLI) == 0) {
2667 lb = object_duplilist(depsgraph, scene, ob);
2668 for (dob = lb->first; dob; dob = dob->next) {
2669 if ((use_hidden == false) && (dob->no_draw != 0)) {
2673 BoundBox *bb = BKE_object_boundbox_get(dob->ob);
2677 for (i = 0; i < 8; i++) {
2679 mul_v3_m4v3(vec, dob->mat, bb->vec[i]);
2680 minmax_v3v3_v3(r_min, r_max, vec);
2687 free_object_duplilist(lb); /* does restore */
2693 void BKE_object_foreach_display_point(
2694 Object *ob, float obmat[4][4],
2695 void (*func_cb)(const float[3], void *), void *user_data)
2699 if (ob->runtime.mesh_eval) {
2700 const Mesh *me = ob->runtime.mesh_eval;
2701 const MVert *mv = me->mvert;
2702 const int totvert = me->totvert;
2703 for (int i = 0; i < totvert; i++, mv++) {
2704 mul_v3_m4v3(co, obmat, mv->co);
2705 func_cb(co, user_data);
2708 else if (ob->runtime.curve_cache && ob->runtime.curve_cache->disp.first) {
2711 for (dl = ob->runtime.curve_cache->disp.first; dl; dl = dl->next) {
2712 const float *v3 = dl->verts;
2713 int totvert = dl->nr;
2716 for (i = 0; i < totvert; i++, v3 += 3) {
2717 mul_v3_m4v3(co, obmat, v3);
2718 func_cb(co, user_data);
2724 void BKE_scene_foreach_display_point(
2725 Depsgraph *depsgraph,
2726 void (*func_cb)(const float[3], void *), void *user_data)
2728 DEG_OBJECT_ITER_BEGIN(
2730 DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY |
2731 DEG_ITER_OBJECT_FLAG_VISIBLE |
2732 DEG_ITER_OBJECT_FLAG_DUPLI)
2734 if ((ob->base_flag & BASE_SELECTED) != 0) {
2735 BKE_object_foreach_display_point(ob, ob->obmat, func_cb, user_data);
2738 DEG_OBJECT_ITER_END;
2741 /* copied from DNA_object_types.h */
2742 typedef struct ObTfmBack {
2743 float loc[3], dloc[3];
2744 /** scale and delta scale. */
2745 float scale[3], dscale[3];
2746 /** euler rotation. */
2747 float rot[3], drot[3];
2748 /** quaternion rotation. */
2749 float quat[4], dquat[4];
2750 /** axis angle rotation - axis part. */
2751 float rotAxis[3], drotAxis[3];
2752 /** axis angle rotation - angle part. */
2753 float rotAngle, drotAngle;
2754 /** final worldspace matrix with constraints & animsys applied. */
2756 /** inverse result of parent, so that object doesn't 'stick' to parent. */
2757 float parentinv[4][4];
2758 /** inverse result of constraints. doesn't include effect of parent or object local transform. */
2759 float constinv[4][4];
2760 /** inverse matrix of 'obmat' for during render, temporally: ipokeys of transform. */
2764 void *BKE_object_tfm_backup(Object *ob)
2766 ObTfmBack *obtfm = MEM_mallocN(sizeof(ObTfmBack), "ObTfmBack");
2767 copy_v3_v3(obtfm->loc, ob->loc);
2768 copy_v3_v3(obtfm->dloc, ob->dloc);
2769 copy_v3_v3(obtfm->scale, ob->scale);
2770 copy_v3_v3(obtfm->dscale, ob->dscale);
2771 copy_v3_v3(obtfm->rot, ob->rot);
2772 copy_v3_v3(obtfm->drot, ob->drot);
2773 copy_qt_qt(obtfm->quat, ob->quat);
2774 copy_qt_qt(obtfm->dquat, ob->dquat);
2775 copy_v3_v3(obtfm->rotAxis, ob->rotAxis);
2776 copy_v3_v3(obtfm->drotAxis, ob->drotAxis);
2777 obtfm->rotAngle = ob->rotAngle;
2778 obtfm->drotAngle = ob->drotAngle;
2779 copy_m4_m4(obtfm->obmat, ob->obmat);
2780 copy_m4_m4(obtfm->parentinv, ob->parentinv);
2781 copy_m4_m4(obtfm->constinv, ob->constinv);
2782 copy_m4_m4(obtfm->imat, ob->imat);
2784 return (void *)obtfm;
2787 void BKE_object_tfm_restore(Object *ob, void *obtfm_pt)
2789 ObTfmBack *obtfm = (ObTfmBack *)obtfm_pt;
2790 copy_v3_v3(ob->loc, obtfm->loc);
2791 copy_v3_v3(ob->dloc, obtfm->dloc);
2792 copy_v3_v3(ob->scale, obtfm->scale);
2793 copy_v3_v3(ob->dscale, obtfm->dscale);
2794 copy_v3_v3(ob->rot, obtfm->rot);
2795 copy_v3_v3(ob->drot, obtfm->drot);
2796 copy_qt_qt(ob->quat, obtfm->quat);
2797 copy_qt_qt(ob->dquat, obtfm->dquat);
2798 copy_v3_v3(ob->rotAxis, obtfm->rotAxis);
2799 copy_v3_v3(ob->drotAxis, obtfm->drotAxis);
2800 ob->rotAngle = obtfm->rotAngle;
2801 ob->drotAngle = obtfm->drotAngle;
2802 copy_m4_m4(ob->obmat, obtfm->obmat);
2803 copy_m4_m4(ob->parentinv, obtfm->parentinv);
2804 copy_m4_m4(ob->constinv, obtfm->constinv);
2805 copy_m4_m4(ob->imat, obtfm->imat);
2808 bool BKE_object_parent_loop_check(const Object *par, const Object *ob)
2810 /* test if 'ob' is a parent somewhere in par's parents */
2811 if (par == NULL) return false;
2812 if (ob == par) return true;
2813 return BKE_object_parent_loop_check(par->parent, ob);
2816 static void object_handle_update_proxy(Depsgraph *depsgraph,
2819 const bool do_proxy_update)
2821 /* The case when this is a collection proxy, object_update is called in collection.c */
2822 if (object->proxy == NULL) {
2825 /* set pointer in library proxy target, for copying, but restore it */
2826 object->proxy->proxy_from = object;
2827 // printf("set proxy pointer for later collection stuff %s\n", ob->id.name);
2829 /* the no-group proxy case, we call update */
2830 if (object->proxy_group == NULL) {
2831 if (do_proxy_update) {
2832 // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
2833 BKE_object_handle_update(depsgraph, scene, object->proxy);
2838 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
2839 /* local_object->proxy == pointer to library object, saved in files and read */
2841 /* function below is polluted with proxy exceptions, cleanup will follow! */
2843 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
2844 /* requires flags to be set! */
2845 /* Ideally we shouldn't have to pass the rigid body world, but need bigger restructuring to avoid id */
2846 void BKE_object_handle_update_ex(Depsgraph *depsgraph,
2847 Scene *scene, Object *ob,
2848 RigidBodyWorld *rbw,
2849 const bool do_proxy_update)
2851 const ID *object_data = ob->data;
2852 const bool recalc_object = (ob->id.recalc & ID_RECALC_ALL) != 0;
2853 const bool recalc_data =
2854 (object_data != NULL) ? ((object_data->recalc & ID_RECALC_ALL) != 0)
2856 if (!recalc_object && ! recalc_data) {
2857 object_handle_update_proxy(depsgraph, scene, ob, do_proxy_update);
2860 /* Speed optimization for animation lookups. */
2861 if (ob->pose != NULL) {
2862 BKE_pose_channels_hash_make(ob->pose);
2863 if (ob->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
2864 BKE_pose_update_constraint_flags(ob->pose);
2868 if (ob->type == OB_ARMATURE) {
2869 /* this happens for reading old files and to match library armatures
2870 * with poses we do it ahead of BKE_object_where_is_calc to ensure animation
2871 * is evaluated on the rebuilt pose, otherwise we get incorrect poses
2873 if (ob->pose == NULL || (ob->pose->flag & POSE_RECALC)) {
2874 /* No need to pass bmain here, we assume we do not need to rebuild DEG from here... */
2875 BKE_pose_rebuild(NULL, ob, ob->data, true);
2879 /* XXX new animsys warning: depsgraph tag ID_RECALC_GEOMETRY should not skip drivers,
2880 * which is only in BKE_object_where_is_calc now */
2881 /* XXX: should this case be ID_RECALC_TRANSFORM instead? */
2882 if (recalc_object || recalc_data) {
2883 if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) {
2884 printf("recalcob %s\n", ob->id.name + 2);
2886 /* Handle proxy copy for target. */
2887 if (!BKE_object_eval_proxy_copy(depsgraph, ob)) {
2888 BKE_object_where_is_calc_ex(depsgraph, scene, rbw, ob, NULL);
2893 BKE_object_handle_data_update(depsgraph, scene, ob);
2896 ob->id.recalc &= ID_RECALC_ALL;
2898 object_handle_update_proxy(depsgraph, scene, ob, do_proxy_update);
2901 /* WARNING: "scene" here may not be the scene object actually resides in.
2902 * When dealing with background-sets, "scene" is actually the active scene.
2903 * e.g. "scene" <-- set 1 <-- set 2 ("ob" lives here) <-- set 3 <-- ... <-- set n
2904 * rigid bodies depend on their world so use BKE_object_handle_update_ex() to also pass along the current rigid body world
2906 void BKE_object_handle_update(Depsgraph *depsgraph, Scene *scene, Object *ob)
2908 BKE_object_handle_update_ex(depsgraph, scene, ob, NULL, true);
2911 void BKE_object_sculpt_data_create(Object *ob)
2913 BLI_assert((ob->sculpt == NULL) && (ob->mode & OB_MODE_ALL_SCULPT));
2914 ob->sculpt = MEM_callocN(sizeof(SculptSession), __func__);
2915 ob->sculpt->mode_type = ob->mode;
2918 void BKE_object_sculpt_modifiers_changed(Object *ob)
2920 SculptSession *ss = ob->sculpt;
2922 if (ss && ss->building_vp_handle == false) {
2924 /* we free pbvh on changes, except during sculpt since it can't deal with
2925 * changing PVBH node organization, we hope topology does not change in
2926 * the meantime .. weak */
2928 BKE_pbvh_free(ss->pbvh);
2932 BKE_sculptsession_free_deformMats(ob->sculpt);
2934 /* In vertex/weight paint, force maps to be rebuilt. */
2935 BKE_sculptsession_free_vwpaint_data(ob->sculpt);
2941 BKE_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
2943 for (n = 0; n < totnode; n++)
2944 BKE_pbvh_node_mark_update(nodes[n]);
2951 int BKE_object_obdata_texspace_get(Object *ob, short **r_texflag, float **r_loc, float **r_size, float **r_rot)
2954 if (ob->data == NULL)
2957 switch (GS(((ID *)ob->data)->name)) {
2960 BKE_mesh_texspace_get_reference((Mesh *)ob->data, r_texflag, r_loc, r_rot, r_size);
2965 Curve *cu = ob->data;
2966 if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_DIRTY)) {
2967 BKE_curve_texspace_calc(cu);
2969 if (r_texflag) *r_texflag = &cu->texflag;
2970 if (r_loc) *r_loc = cu->loc;
2971 if (r_size) *r_size = cu->size;
2972 if (r_rot) *r_rot = cu->rot;
2977 MetaBall *mb = ob->data;
2978 if (r_texflag) *r_texflag = &mb->texflag;
2979 if (r_loc) *r_loc = mb->loc;
2980 if (r_size) *r_size = mb->size;
2981 if (r_rot) *r_rot = mb->rot;
2990 /** Get evaluated mesh for given (main, original) object and depsgraph. */
2991 Mesh *BKE_object_get_evaluated_mesh(const Depsgraph *depsgraph, Object *ob)
2993 Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
2994 return ob_eval->runtime.mesh_eval;
2997 /* Get object's mesh with all modifiers applied. */
2998 Mesh *BKE_object_get_final_mesh(Object *object)
3000 if (object->runtime.mesh_eval != NULL) {
3001 BLI_assert((object->id.tag & LIB_TAG_COPIED_ON_WRITE) != 0);
3002 BLI_assert(object->runtime.mesh_eval == object->data);
3003 BLI_assert((object->runtime.mesh_eval->id.tag & LIB_TAG_COPIED_ON_WRITE_EVAL_RESULT) != 0);
3004 return object->runtime.mesh_eval;
3006 /* Wasn't evaluated yet. */
3007 return object->data;
3010 /* Get mesh which is not affected by modifiers:
3011 * - For original objects it will be same as object->data, and it is a mesh
3012 * which is in the corresponding bmain.
3013 * - For copied-on-write objects it will give pointer to a copied-on-write
3014 * mesh which corresponds to original object's mesh.
3016 Mesh *BKE_object_get_pre_modified_mesh(Object *object)
3018 if (object->runtime.mesh_orig != NULL) {
3019 BLI_assert(object->id.tag & LIB_TAG_COPIED_ON_WRITE);
3020 BLI_assert(object->id.orig_id != NULL);
3021 BLI_assert(object->runtime.mesh_orig->id.orig_id == ((Object *)object->id.orig_id)->data);
3022 Mesh *result = object->runtime.mesh_orig;
3023 BLI_assert((result->id.tag & LIB_TAG_COPIED_ON_WRITE) != 0);
3024 BLI_assert((result->id.tag & LIB_TAG_COPIED_ON_WRITE_EVAL_RESULT) == 0);
3027 BLI_assert((object->id.tag & LIB_TAG_COPIED_ON_WRITE) == 0);
3028 return object->data;
3031 /* Get a mesh which corresponds to very very original mesh from bmain.
3032 * - For original objects it will be object->data.
3033 * - For evaluated objects it will be same mesh as corresponding original
3034 * object uses as data.
3036 Mesh *BKE_object_get_original_mesh(Object *object)
3038 Mesh *result = NULL;
3039 if (object->id.orig_id == NULL) {
3040 BLI_assert((object->id.tag & LIB_TAG_COPIED_ON_WRITE) == 0);
3041 result = object->data;
3044 BLI_assert((object->id.tag & LIB_TAG_COPIED_ON_WRITE) != 0);
3045 result = ((Object *)object->id.orig_id)->data;
3047 BLI_assert(result != NULL);
3048 BLI_assert((result->id.tag & (LIB_TAG_COPIED_ON_WRITE | LIB_TAG_COPIED_ON_WRITE_EVAL_RESULT)) == 0);
3052 static int pc_cmp(const void *a, const void *b)
3054 const LinkData *ad = a, *bd = b;
3055 if (POINTER_AS_INT(ad->data) > POINTER_AS_INT(bd->data))
3060 int BKE_object_insert_ptcache(Object *ob)
3062 LinkData *link = NULL;
3065 BLI_listbase_sort(&ob->pc_ids, pc_cmp);
3067 for (link = ob->pc_ids.first, i = 0; link; link = link->next, i++) {
3068 int index = POINTER_AS_INT(link->data);
3074 link = MEM_callocN(sizeof(LinkData), "PCLink");
3075 link->data = POINTER_FROM_INT(i);
3076 BLI_addtail(&ob->pc_ids, link);
3081 static int pc_findindex(ListBase *listbase, int index)
3083 LinkData *link = NULL;
3086 if (listbase == NULL) return -1;
3088 link = listbase->first;
3090 if (POINTER_AS_INT(link->data) == index)
3100 void BKE_object_delete_ptcache(Object *ob, int index)
3102 int list_index = pc_findindex(&ob->pc_ids, index);
3103 LinkData *link = BLI_findlink(&ob->pc_ids, list_index);
3104 BLI_freelinkN(&ob->pc_ids, link);
3107 /* shape key utility function */
3109 /************************* Mesh ************************/
3110 static KeyBlock *insert_meshkey(Main *bmain, Object *ob, const char *name, const bool from_mix)
3112 Mesh *me = ob->data;
3118 key = me->key = BKE_key_add(bmain, (ID *)me);
3119 key->type = KEY_RELATIVE;
3123 if (newkey || from_mix == false) {
3124 /* create from mesh */
3125 kb = BKE_keyblock_add_ctime(key, name, false);
3126 BKE_keyblock_convert_from_mesh(me, key, kb);
3129 /* copy from current values */
3131 float *data = BKE_key_evaluate_object(ob, &totelem);
3133 /* create new block with prepared data */
3134 kb = BKE_keyblock_add_ctime(key, name, false);
3136 kb->totelem = totelem;
3141 /************************* Lattice ************************/
3142 static KeyBlock *insert_lattkey(Main *bmain, Object *ob, const char *name, const bool from_mix)
3144 Lattice *lt = ob->data;
3150 key = lt->key = BKE_key_add(bmain, (ID *)lt);
3151 key->type = KEY_RELATIVE;
3155 if (newkey || from_mix == false) {
3156 kb = BKE_keyblock_add_ctime(key, name, false);
3158 KeyBlock *basekb = (KeyBlock *)key->block.first;
3159 kb->data = MEM_dupallocN(basekb->data);
3160 kb->totelem = basekb->totelem;
3163 BKE_keyblock_convert_from_lattice(lt, kb);
3167 /* copy from current values */
3169 float *data = BKE_key_evaluate_object(ob, &totelem);
3171 /* create new block with prepared data */
3172 kb = BKE_keyblock_add_ctime(key, name, false);
3173 kb->totelem = totelem;
3179 /************************* Curve ************************/
3180 static KeyBlock *insert_curvekey(Main *bmain, Object *ob, const char *name, const bool from_mix)
3182 Curve *cu = ob->data;
3185 ListBase *lb = BKE_curve_nurbs_get(cu);
3189 key = cu->key = BKE_key_add(bmain, (ID *)cu);
3190 key->type = KEY_RELATIVE;
3194 if (newkey || from_mix == false) {
3195 /* create from curve */
3196 kb = BKE_keyblock_add_ctime(key, name, false);
3198 KeyBlock *basekb = (KeyBlock *)key->block.first;
3199 kb->data = MEM_dupallocN(basekb->data);
3200 kb->totelem = basekb->totelem;
3203 BKE_keyblock_convert_from_curve(cu, kb, lb);
3207 /* copy from current values */
3209 float *data = BKE_key_evaluate_object(ob, &totelem);
3211 /* create new block with prepared data */
3212 kb = BKE_keyblock_add_ctime(key, name, false);
3213 kb->totelem = totelem;
3220 KeyBlock *BKE_object_shapekey_insert(Main *bmain, Object *ob, const char *name, const bool from_mix)
3224 return insert_meshkey(bmain, ob, name, from_mix);
3227 return insert_curvekey(bmain, ob, name, from_mix);
3229 return insert_lattkey(bmain, ob, name, from_mix);
3236 bool BKE_object_shapekey_free(Main *bmain, Object *ob)
3240 key_p = BKE_key_from_object_p(ob);
3241 if (ELEM(NULL, key_p, *key_p)) {
3248 BKE_id_free_us(bmain, key);
3253 bool BKE_object_shapekey_remove(Main *bmain, Object *ob, KeyBlock *kb)
3256 Key *key = BKE_key_from_object(ob);
3263 kb_index = BLI_findindex(&key->block, kb);
3264 BLI_assert(kb_index != -1);
3266 for (rkb = key->block.first; rkb; rkb = rkb->next) {
3267 if (rkb->relative == kb_index) {
3268 /* remap to the 'Basis' */
3271 else if (rkb->relative >= kb_index) {
3272 /* Fix positional shift of the keys when kb is deleted from the list */
3277 BLI_remlink(&key->block, kb);
3279 if (key->refkey == kb) {
3280 key->refkey = key->block.first;
3283 /* apply new basis key on original data */
3286 BKE_keyblock_convert_to_mesh(key->refkey, ob->data);
3290 BKE_keyblock_convert_to_curve(key->refkey, ob->data, BKE_curve_nurbs_get(ob->data));