1 /*
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.
6 *
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.
11 *
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.
15 *
16 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17 * All rights reserved.
18 */
19
20 /** \file
21 * \ingroup bke
22 */
23
24 #include <math.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "CLG_log.h"
29
30 #include "MEM_guardedalloc.h"
31
32 #include "DNA_anim_types.h"
33 #include "DNA_armature_types.h"
34 #include "DNA_camera_types.h"
35 #include "DNA_collection_types.h"
36 #include "DNA_constraint_types.h"
37 #include "DNA_defaults.h"
38 #include "DNA_fluid_types.h"
39 #include "DNA_gpencil_modifier_types.h"
40 #include "DNA_gpencil_types.h"
41 #include "DNA_key_types.h"
42 #include "DNA_lattice_types.h"
43 #include "DNA_light_types.h"
44 #include "DNA_lightprobe_types.h"
45 #include "DNA_material_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_meta_types.h"
49 #include "DNA_movieclip_types.h"
50 #include "DNA_object_types.h"
51 #include "DNA_pointcloud_types.h"
52 #include "DNA_rigidbody_types.h"
53 #include "DNA_scene_types.h"
54 #include "DNA_screen_types.h"
55 #include "DNA_sequence_types.h"
56 #include "DNA_shader_fx_types.h"
57 #include "DNA_space_types.h"
58 #include "DNA_view3d_types.h"
59 #include "DNA_world_types.h"
60
61 #include "BLI_blenlib.h"
62 #include "BLI_kdtree.h"
63 #include "BLI_linklist.h"
64 #include "BLI_listbase.h"
65 #include "BLI_math.h"
66 #include "BLI_threads.h"
67 #include "BLI_utildefines.h"
68
69 #include "BLT_translation.h"
70
71 #include "BKE_DerivedMesh.h"
72 #include "BKE_action.h"
73 #include "BKE_anim_data.h"
74 #include "BKE_anim_path.h"
75 #include "BKE_anim_visualization.h"
76 #include "BKE_animsys.h"
77 #include "BKE_armature.h"
78 #include "BKE_camera.h"
79 #include "BKE_collection.h"
80 #include "BKE_constraint.h"
81 #include "BKE_curve.h"
82 #include "BKE_deform.h"
83 #include "BKE_displist.h"
84 #include "BKE_duplilist.h"
85 #include "BKE_editmesh.h"
86 #include "BKE_editmesh_cache.h"
87 #include "BKE_effect.h"
88 #include "BKE_fcurve.h"
89 #include "BKE_fcurve_driver.h"
90 #include "BKE_font.h"
91 #include "BKE_global.h"
92 #include "BKE_gpencil.h"
93 #include "BKE_gpencil_geom.h"
94 #include "BKE_gpencil_modifier.h"
95 #include "BKE_hair.h"
96 #include "BKE_icons.h"
97 #include "BKE_idprop.h"
98 #include "BKE_idtype.h"
99 #include "BKE_image.h"
100 #include "BKE_key.h"
101 #include "BKE_lattice.h"
102 #include "BKE_layer.h"
103 #include "BKE_lib_id.h"
104 #include "BKE_lib_query.h"
105 #include "BKE_lib_remap.h"
106 #include "BKE_light.h"
107 #include "BKE_lightprobe.h"
108 #include "BKE_linestyle.h"
109 #include "BKE_main.h"
110 #include "BKE_material.h"
111 #include "BKE_mball.h"
112 #include "BKE_mesh.h"
113 #include "BKE_mesh_wrapper.h"
114 #include "BKE_modifier.h"
115 #include "BKE_multires.h"
116 #include "BKE_node.h"
117 #include "BKE_object.h"
118 #include "BKE_object_facemap.h"
119 #include "BKE_paint.h"
120 #include "BKE_particle.h"
121 #include "BKE_pbvh.h"
122 #include "BKE_pointcache.h"
123 #include "BKE_pointcloud.h"
124 #include "BKE_rigidbody.h"
125 #include "BKE_scene.h"
126 #include "BKE_sequencer.h"
127 #include "BKE_shader_fx.h"
128 #include "BKE_softbody.h"
129 #include "BKE_speaker.h"
130 #include "BKE_subdiv_ccg.h"
131 #include "BKE_subsurf.h"
132 #include "BKE_volume.h"
133
134 #include "DEG_depsgraph.h"
135 #include "DEG_depsgraph_query.h"
136
137 #include "DRW_engine.h"
138
139 #ifdef WITH_PYTHON
140 # include "BPY_extern.h"
141 #endif
142
143 #include "CCGSubSurf.h"
144 #include "atomic_ops.h"
145
146 static CLG_LogRef LOG = {"bke.object"};
147
148 /**
149 * Vertex parent modifies original BMesh which is not safe for threading.
150 * Ideally such a modification should be handled as a separate DAG update
151 * callback for mesh datablock, but for until it is actually supported use
152 * simpler solution with a mutex lock.
153 * - sergey -
154 */
155 #define VPARENT_THREADING_HACK
156
157 #ifdef VPARENT_THREADING_HACK
158 static ThreadMutex vparent_lock = BLI_MUTEX_INITIALIZER;
159 #endif
160
161 static void copy_object_pose(Object *obn, const Object *ob, const int flag);
162
object_init_data(ID * id)163 static void object_init_data(ID *id)
164 {
165 Object *ob = (Object *)id;
166 BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(ob, id));
167
168 MEMCPY_STRUCT_AFTER(ob, DNA_struct_default_get(Object), id);
169
170 ob->type = OB_EMPTY;
171
172 ob->trackflag = OB_POSY;
173 ob->upflag = OB_POSZ;
174
175 /* Animation Visualization defaults */
176 animviz_settings_init(&ob->avs);
177 }
178
object_copy_data(Main * bmain,ID * id_dst,const ID * id_src,const int flag)179 static void object_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const int flag)
180 {
181 Object *ob_dst = (Object *)id_dst;
182 const Object *ob_src = (const Object *)id_src;
183
184 /* Do not copy runtime data. */
185 BKE_object_runtime_reset_on_copy(ob_dst, flag);
186
187 /* We never handle usercount here for own data. */
188 const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
189
190 if (ob_src->totcol) {
191 ob_dst->mat = MEM_dupallocN(ob_src->mat);
192 ob_dst->matbits = MEM_dupallocN(ob_src->matbits);
193 ob_dst->totcol = ob_src->totcol;
194 }
195 else if (ob_dst->mat != NULL || ob_dst->matbits != NULL) {
196 /* This shall not be needed, but better be safe than sorry. */
197 BLI_assert(!"Object copy: non-NULL material pointers with zero counter, should not happen.");
198 ob_dst->mat = NULL;
199 ob_dst->matbits = NULL;
200 }
201
202 if (ob_src->iuser) {
203 ob_dst->iuser = MEM_dupallocN(ob_src->iuser);
204 }
205
206 if (ob_src->runtime.bb) {
207 ob_dst->runtime.bb = MEM_dupallocN(ob_src->runtime.bb);
208 }
209
210 BLI_listbase_clear(&ob_dst->modifiers);
211
212 LISTBASE_FOREACH (ModifierData *, md, &ob_src->modifiers) {
213 ModifierData *nmd = BKE_modifier_new(md->type);
214 BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
215 BKE_modifier_copydata_ex(md, nmd, flag_subdata);
216 BLI_addtail(&ob_dst->modifiers, nmd);
217 }
218
219 BLI_listbase_clear(&ob_dst->greasepencil_modifiers);
220
221 LISTBASE_FOREACH (GpencilModifierData *, gmd, &ob_src->greasepencil_modifiers) {
222 GpencilModifierData *nmd = BKE_gpencil_modifier_new(gmd->type);
223 BLI_strncpy(nmd->name, gmd->name, sizeof(nmd->name));
224 BKE_gpencil_modifier_copydata_ex(gmd, nmd, flag_subdata);
225 BLI_addtail(&ob_dst->greasepencil_modifiers, nmd);
226 }
227
228 BLI_listbase_clear(&ob_dst->shader_fx);
229
230 LISTBASE_FOREACH (ShaderFxData *, fx, &ob_src->shader_fx) {
231 ShaderFxData *nfx = BKE_shaderfx_new(fx->type);
232 BLI_strncpy(nfx->name, fx->name, sizeof(nfx->name));
233 BKE_shaderfx_copydata_ex(fx, nfx, flag_subdata);
234 BLI_addtail(&ob_dst->shader_fx, nfx);
235 }
236
237 if (ob_src->pose) {
238 copy_object_pose(ob_dst, ob_src, flag_subdata);
239 /* backwards compat... non-armatures can get poses in older files? */
240 if (ob_src->type == OB_ARMATURE) {
241 const bool do_pose_id_user = (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0;
242 BKE_pose_rebuild(bmain, ob_dst, ob_dst->data, do_pose_id_user);
243 }
244 }
245 BKE_defgroup_copy_list(&ob_dst->defbase, &ob_src->defbase);
246 BKE_object_facemap_copy_list(&ob_dst->fmaps, &ob_src->fmaps);
247 BKE_constraints_copy_ex(&ob_dst->constraints, &ob_src->constraints, flag_subdata, true);
248
249 ob_dst->mode = ob_dst->type != OB_GPENCIL ? OB_MODE_OBJECT : ob_dst->mode;
250 ob_dst->sculpt = NULL;
251
252 if (ob_src->pd) {
253 ob_dst->pd = MEM_dupallocN(ob_src->pd);
254 if (ob_dst->pd->rng) {
255 ob_dst->pd->rng = MEM_dupallocN(ob_src->pd->rng);
256 }
257 }
258 BKE_object_copy_softbody(ob_dst, ob_src, flag_subdata);
259 BKE_rigidbody_object_copy(bmain, ob_dst, ob_src, flag_subdata);
260
261 BKE_object_copy_particlesystems(ob_dst, ob_src, flag_subdata);
262
263 BLI_listbase_clear((ListBase *)&ob_dst->drawdata);
264 BLI_listbase_clear(&ob_dst->pc_ids);
265
266 ob_dst->avs = ob_src->avs;
267 ob_dst->mpath = animviz_copy_motionpath(ob_src->mpath);
268
269 /* Do not copy object's preview
270 * (mostly due to the fact renderers create temp copy of objects). */
271 if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0 && false) { /* XXX TODO temp hack */
272 BKE_previewimg_id_copy(&ob_dst->id, &ob_src->id);
273 }
274 else {
275 ob_dst->preview = NULL;
276 }
277 }
278
object_free_data(ID * id)279 static void object_free_data(ID *id)
280 {
281 Object *ob = (Object *)id;
282
283 DRW_drawdata_free((ID *)ob);
284
285 /* BKE_<id>_free shall never touch to ID->us. Never ever. */
286 BKE_object_free_modifiers(ob, LIB_ID_CREATE_NO_USER_REFCOUNT);
287 BKE_object_free_shaderfx(ob, LIB_ID_CREATE_NO_USER_REFCOUNT);
288
289 MEM_SAFE_FREE(ob->mat);
290 MEM_SAFE_FREE(ob->matbits);
291 MEM_SAFE_FREE(ob->iuser);
292 MEM_SAFE_FREE(ob->runtime.bb);
293
294 BLI_freelistN(&ob->defbase);
295 BLI_freelistN(&ob->fmaps);
296 if (ob->pose) {
297 BKE_pose_free_ex(ob->pose, false);
298 ob->pose = NULL;
299 }
300 if (ob->mpath) {
301 animviz_free_motionpath(ob->mpath);
302 ob->mpath = NULL;
303 }
304
305 BKE_constraints_free_ex(&ob->constraints, false);
306
307 BKE_partdeflect_free(ob->pd);
308 BKE_rigidbody_free_object(ob, NULL);
309 BKE_rigidbody_free_constraint(ob);
310
311 sbFree(ob);
312
313 BKE_sculptsession_free(ob);
314
315 BLI_freelistN(&ob->pc_ids);
316
317 /* Free runtime curves data. */
318 if (ob->runtime.curve_cache) {
319 BKE_curve_bevelList_free(&ob->runtime.curve_cache->bev);
320 if (ob->runtime.curve_cache->path) {
321 free_path(ob->runtime.curve_cache->path);
322 }
323 MEM_freeN(ob->runtime.curve_cache);
324 ob->runtime.curve_cache = NULL;
325 }
326
327 BKE_previewimg_free(&ob->preview);
328 }
329
object_make_local(Main * bmain,ID * id,const int flags)330 static void object_make_local(Main *bmain, ID *id, const int flags)
331 {
332 Object *ob = (Object *)id;
333 const bool lib_local = (flags & LIB_ID_MAKELOCAL_FULL_LIBRARY) != 0;
334 const bool clear_proxy = (flags & LIB_ID_MAKELOCAL_OBJECT_NO_PROXY_CLEARING) == 0;
335 bool is_local = false, is_lib = false;
336
337 /* - only lib users: do nothing (unless force_local is set)
338 * - only local users: set flag
339 * - mixed: make copy
340 * In case we make a whole lib's content local,
341 * we always want to localize, and we skip remapping (done later).
342 */
343
344 if (!ID_IS_LINKED(ob)) {
345 return;
346 }
347
348 BKE_library_ID_test_usages(bmain, ob, &is_local, &is_lib);
349
350 if (lib_local || is_local) {
351 if (!is_lib) {
352 BKE_lib_id_clear_library_data(bmain, &ob->id);
353 BKE_lib_id_expand_local(bmain, &ob->id);
354 if (clear_proxy) {
355 if (ob->proxy_from != NULL) {
356 ob->proxy_from->proxy = NULL;
357 ob->proxy_from->proxy_group = NULL;
358 }
359 ob->proxy = ob->proxy_from = ob->proxy_group = NULL;
360 }
361 }
362 else {
363 Object *ob_new = (Object *)BKE_id_copy(bmain, &ob->id);
364 id_us_min(&ob_new->id);
365
366 ob_new->proxy = ob_new->proxy_from = ob_new->proxy_group = NULL;
367
368 /* setting newid is mandatory for complex make_lib_local logic... */
369 ID_NEW_SET(ob, ob_new);
370
371 if (!lib_local) {
372 BKE_libblock_remap(bmain, ob, ob_new, ID_REMAP_SKIP_INDIRECT_USAGE);
373 }
374 }
375 }
376 }
377
library_foreach_modifiersForeachIDLink(void * user_data,Object * UNUSED (object),ID ** id_pointer,int cb_flag)378 static void library_foreach_modifiersForeachIDLink(void *user_data,
379 Object *UNUSED(object),
380 ID **id_pointer,
381 int cb_flag)
382 {
383 LibraryForeachIDData *data = (LibraryForeachIDData *)user_data;
384 BKE_lib_query_foreachid_process(data, id_pointer, cb_flag);
385 }
386
library_foreach_gpencil_modifiersForeachIDLink(void * user_data,Object * UNUSED (object),ID ** id_pointer,int cb_flag)387 static void library_foreach_gpencil_modifiersForeachIDLink(void *user_data,
388 Object *UNUSED(object),
389 ID **id_pointer,
390 int cb_flag)
391 {
392 LibraryForeachIDData *data = (LibraryForeachIDData *)user_data;
393 BKE_lib_query_foreachid_process(data, id_pointer, cb_flag);
394 }
395
library_foreach_shaderfxForeachIDLink(void * user_data,Object * UNUSED (object),ID ** id_pointer,int cb_flag)396 static void library_foreach_shaderfxForeachIDLink(void *user_data,
397 Object *UNUSED(object),
398 ID **id_pointer,
399 int cb_flag)
400 {
401 LibraryForeachIDData *data = (LibraryForeachIDData *)user_data;
402 BKE_lib_query_foreachid_process(data, id_pointer, cb_flag);
403 }
404
library_foreach_constraintObjectLooper(bConstraint * UNUSED (con),ID ** id_pointer,bool is_reference,void * user_data)405 static void library_foreach_constraintObjectLooper(bConstraint *UNUSED(con),
406 ID **id_pointer,
407 bool is_reference,
408 void *user_data)
409 {
410 LibraryForeachIDData *data = (LibraryForeachIDData *)user_data;
411 const int cb_flag = is_reference ? IDWALK_CB_USER : IDWALK_CB_NOP;
412 BKE_lib_query_foreachid_process(data, id_pointer, cb_flag);
413 }
414
library_foreach_particlesystemsObjectLooper(ParticleSystem * UNUSED (psys),ID ** id_pointer,void * user_data,int cb_flag)415 static void library_foreach_particlesystemsObjectLooper(ParticleSystem *UNUSED(psys),
416 ID **id_pointer,
417 void *user_data,
418 int cb_flag)
419 {
420 LibraryForeachIDData *data = (LibraryForeachIDData *)user_data;
421 BKE_lib_query_foreachid_process(data, id_pointer, cb_flag);
422 }
423
object_foreach_id(ID * id,LibraryForeachIDData * data)424 static void object_foreach_id(ID *id, LibraryForeachIDData *data)
425 {
426 Object *object = (Object *)id;
427
428 /* Object is special, proxies make things hard... */
429 const int proxy_cb_flag = ((BKE_lib_query_foreachid_process_flags_get(data) &
430 IDWALK_NO_INDIRECT_PROXY_DATA_USAGE) == 0 &&
431 (object->proxy || object->proxy_group)) ?
432 IDWALK_CB_INDIRECT_USAGE :
433 0;
434
435 /* object data special case */
436 if (object->type == OB_EMPTY) {
437 /* empty can have NULL or Image */
438 BKE_LIB_FOREACHID_PROCESS_ID(data, object->data, proxy_cb_flag | IDWALK_CB_USER);
439 }
440 else {
441 /* when set, this can't be NULL */
442 if (object->data) {
443 BKE_LIB_FOREACHID_PROCESS_ID(
444 data, object->data, proxy_cb_flag | IDWALK_CB_USER | IDWALK_CB_NEVER_NULL);
445 }
446 }
447
448 BKE_LIB_FOREACHID_PROCESS(data, object->parent, IDWALK_CB_NEVER_SELF);
449 BKE_LIB_FOREACHID_PROCESS(data, object->track, IDWALK_CB_NEVER_SELF);
450 /* object->proxy is refcounted, but not object->proxy_group... *sigh* */
451 BKE_LIB_FOREACHID_PROCESS(data, object->proxy, IDWALK_CB_USER | IDWALK_CB_NEVER_SELF);
452 BKE_LIB_FOREACHID_PROCESS(data, object->proxy_group, IDWALK_CB_NOP);
453
454 /* Special case!
455 * Since this field is set/owned by 'user' of this ID (and not ID itself),
456 * it is only indirect usage if proxy object is linked... Twisted. */
457 {
458 const int cb_flag_orig = BKE_lib_query_foreachid_process_callback_flag_override(
459 data,
460 (object->proxy_from != NULL && ID_IS_LINKED(object->proxy_from)) ?
461 IDWALK_CB_INDIRECT_USAGE :
462 0,
463 true);
464 BKE_LIB_FOREACHID_PROCESS(data, object->proxy_from, IDWALK_CB_LOOPBACK | IDWALK_CB_NEVER_SELF);
465 BKE_lib_query_foreachid_process_callback_flag_override(data, cb_flag_orig, true);
466 }
467
468 BKE_LIB_FOREACHID_PROCESS(data, object->poselib, IDWALK_CB_USER);
469
470 for (int i = 0; i < object->totcol; i++) {
471 BKE_LIB_FOREACHID_PROCESS(data, object->mat[i], proxy_cb_flag | IDWALK_CB_USER);
472 }
473
474 /* Note that ob->gpd is deprecated, so no need to handle it here. */
475 BKE_LIB_FOREACHID_PROCESS(data, object->instance_collection, IDWALK_CB_USER);
476
477 if (object->pd) {
478 BKE_LIB_FOREACHID_PROCESS(data, object->pd->tex, IDWALK_CB_USER);
479 BKE_LIB_FOREACHID_PROCESS(data, object->pd->f_source, IDWALK_CB_NOP);
480 }
481 /* Note that ob->effect is deprecated, so no need to handle it here. */
482
483 if (object->pose) {
484 const int cb_flag_orig = BKE_lib_query_foreachid_process_callback_flag_override(
485 data, proxy_cb_flag, false);
486 LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
487 IDP_foreach_property(
488 pchan->prop, IDP_TYPE_FILTER_ID, BKE_lib_query_idpropertiesForeachIDLink_callback, data);
489 BKE_LIB_FOREACHID_PROCESS(data, pchan->custom, IDWALK_CB_USER);
490 BKE_constraints_id_loop(&pchan->constraints, library_foreach_constraintObjectLooper, data);
491 }
492 BKE_lib_query_foreachid_process_callback_flag_override(data, cb_flag_orig, true);
493 }
494
495 if (object->rigidbody_constraint) {
496 BKE_LIB_FOREACHID_PROCESS(data, object->rigidbody_constraint->ob1, IDWALK_CB_NEVER_SELF);
497 BKE_LIB_FOREACHID_PROCESS(data, object->rigidbody_constraint->ob2, IDWALK_CB_NEVER_SELF);
498 }
499
500 BKE_modifiers_foreach_ID_link(object, library_foreach_modifiersForeachIDLink, data);
501 BKE_gpencil_modifiers_foreach_ID_link(
502 object, library_foreach_gpencil_modifiersForeachIDLink, data);
503 BKE_constraints_id_loop(&object->constraints, library_foreach_constraintObjectLooper, data);
504 BKE_shaderfx_foreach_ID_link(object, library_foreach_shaderfxForeachIDLink, data);
505
506 LISTBASE_FOREACH (ParticleSystem *, psys, &object->particlesystem) {
507 BKE_particlesystem_id_loop(psys, library_foreach_particlesystemsObjectLooper, data);
508 }
509
510 if (object->soft) {
511 BKE_LIB_FOREACHID_PROCESS(data, object->soft->collision_group, IDWALK_CB_NOP);
512
513 if (object->soft->effector_weights) {
514 BKE_LIB_FOREACHID_PROCESS(data, object->soft->effector_weights->group, IDWALK_CB_NOP);
515 }
516 }
517 }
518
519 IDTypeInfo IDType_ID_OB = {
520 .id_code = ID_OB,
521 .id_filter = FILTER_ID_OB,
522 .main_listbase_index = INDEX_ID_OB,
523 .struct_size = sizeof(Object),
524 .name = "Object",
525 .name_plural = "objects",
526 .translation_context = BLT_I18NCONTEXT_ID_OBJECT,
527 .flags = 0,
528
529 .init_data = object_init_data,
530 .copy_data = object_copy_data,
531 .free_data = object_free_data,
532 .make_local = object_make_local,
533 .foreach_id = object_foreach_id,
534 .foreach_cache = NULL,
535
536 .blend_write = NULL,
537 .blend_read_data = NULL,
538 .blend_read_lib = NULL,
539 .blend_read_expand = NULL,
540 };
541
BKE_object_workob_clear(Object * workob)542 void BKE_object_workob_clear(Object *workob)
543 {
544 memset(workob, 0, sizeof(Object));
545
546 workob->scale[0] = workob->scale[1] = workob->scale[2] = 1.0f;
547 workob->dscale[0] = workob->dscale[1] = workob->dscale[2] = 1.0f;
548 workob->rotmode = ROT_MODE_EUL;
549 }
550
BKE_object_free_particlesystems(Object * ob)551 void BKE_object_free_particlesystems(Object *ob)
552 {
553 ParticleSystem *psys;
554
555 while ((psys = BLI_pophead(&ob->particlesystem))) {
556 psys_free(ob, psys);
557 }
558 }
559
BKE_object_free_softbody(Object * ob)560 void BKE_object_free_softbody(Object *ob)
561 {
562 sbFree(ob);
563 }
564
BKE_object_free_curve_cache(Object * ob)565 void BKE_object_free_curve_cache(Object *ob)
566 {
567 if (ob->runtime.curve_cache) {
568 BKE_displist_free(&ob->runtime.curve_cache->disp);
569 BKE_curve_bevelList_free(&ob->runtime.curve_cache->bev);
570 if (ob->runtime.curve_cache->path) {
571 free_path(ob->runtime.curve_cache->path);
572 }
573 BKE_nurbList_free(&ob->runtime.curve_cache->deformed_nurbs);
574 MEM_freeN(ob->runtime.curve_cache);
575 ob->runtime.curve_cache = NULL;
576 }
577 }
578
BKE_object_free_modifiers(Object * ob,const int flag)579 void BKE_object_free_modifiers(Object *ob, const int flag)
580 {
581 ModifierData *md;
582 GpencilModifierData *gp_md;
583
584 while ((md = BLI_pophead(&ob->modifiers))) {
585 BKE_modifier_free_ex(md, flag);
586 }
587
588 while ((gp_md = BLI_pophead(&ob->greasepencil_modifiers))) {
589 BKE_gpencil_modifier_free_ex(gp_md, flag);
590 }
591 /* particle modifiers were freed, so free the particlesystems as well */
592 BKE_object_free_particlesystems(ob);
593
594 /* same for softbody */
595 BKE_object_free_softbody(ob);
596
597 /* modifiers may have stored data in the DM cache */
598 BKE_object_free_derived_caches(ob);
599 }
600
BKE_object_free_shaderfx(Object * ob,const int flag)601 void BKE_object_free_shaderfx(Object *ob, const int flag)
602 {
603 ShaderFxData *fx;
604
605 while ((fx = BLI_pophead(&ob->shader_fx))) {
606 BKE_shaderfx_free_ex(fx, flag);
607 }
608 }
609
BKE_object_modifier_hook_reset(Object * ob,HookModifierData * hmd)610 void BKE_object_modifier_hook_reset(Object *ob, HookModifierData *hmd)
611 {
612 /* reset functionality */
613 if (hmd->object) {
614 bPoseChannel *pchan = BKE_pose_channel_find_name(hmd->object->pose, hmd->subtarget);
615
616 if (hmd->subtarget[0] && pchan) {
617 float imat[4][4], mat[4][4];
618
619 /* Calculate the world-space matrix for the pose-channel target first,
620 * then carry on as usual. */
621 mul_m4_m4m4(mat, hmd->object->obmat, pchan->pose_mat);
622
623 invert_m4_m4(imat, mat);
624 mul_m4_m4m4(hmd->parentinv, imat, ob->obmat);
625 }
626 else {
627 invert_m4_m4(hmd->object->imat, hmd->object->obmat);
628 mul_m4_m4m4(hmd->parentinv, hmd->object->imat, ob->obmat);
629 }
630 }
631 }
632
BKE_object_modifier_gpencil_hook_reset(Object * ob,HookGpencilModifierData * hmd)633 void BKE_object_modifier_gpencil_hook_reset(Object *ob, HookGpencilModifierData *hmd)
634 {
635 if (hmd->object == NULL) {
636 return;
637 }
638 /* reset functionality */
639 bPoseChannel *pchan = BKE_pose_channel_find_name(hmd->object->pose, hmd->subtarget);
640
641 if (hmd->subtarget[0] && pchan) {
642 float imat[4][4], mat[4][4];
643
644 /* Calculate the world-space matrix for the pose-channel target first,
645 * then carry on as usual. */
646 mul_m4_m4m4(mat, hmd->object->obmat, pchan->pose_mat);
647
648 invert_m4_m4(imat, mat);
649 mul_m4_m4m4(hmd->parentinv, imat, ob->obmat);
650 }
651 else {
652 invert_m4_m4(hmd->object->imat, hmd->object->obmat);
653 mul_m4_m4m4(hmd->parentinv, hmd->object->imat, ob->obmat);
654 }
655 }
656
BKE_object_support_modifier_type_check(const Object * ob,int modifier_type)657 bool BKE_object_support_modifier_type_check(const Object *ob, int modifier_type)
658 {
659 const ModifierTypeInfo *mti;
660
661 mti = BKE_modifier_get_info(modifier_type);
662
663 /* Only geometry objects should be able to get modifiers T25291. */
664 if (ob->type == OB_HAIR) {
665 return (mti->modifyHair != NULL) || (mti->flags & eModifierTypeFlag_AcceptsVertexCosOnly);
666 }
667 if (ob->type == OB_POINTCLOUD) {
668 return (mti->modifyPointCloud != NULL) ||
669 (mti->flags & eModifierTypeFlag_AcceptsVertexCosOnly);
670 }
671 if (ob->type == OB_VOLUME) {
672 return (mti->modifyVolume != NULL);
673 }
674 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
675 if (ob->type == OB_LATTICE && (mti->flags & eModifierTypeFlag_AcceptsVertexCosOnly) == 0) {
676 return false;
677 }
678
679 if (!((mti->flags & eModifierTypeFlag_AcceptsCVs) ||
680 (ob->type == OB_MESH && (mti->flags & eModifierTypeFlag_AcceptsMesh)))) {
681 return false;
682 }
683
684 return true;
685 }
686
687 return false;
688 }
689
BKE_object_copy_modifier(struct Object * ob_dst,const struct Object * ob_src,ModifierData * md)690 bool BKE_object_copy_modifier(struct Object *ob_dst, const struct Object *ob_src, ModifierData *md)
691 {
692 ModifierData *nmd = NULL;
693
694 if (ELEM(md->type, eModifierType_Hook, eModifierType_Collision)) {
695 return false;
696 }
697
698 if (!BKE_object_support_modifier_type_check(ob_dst, md->type)) {
699 return false;
700 }
701
702 switch (md->type) {
703 case eModifierType_Softbody:
704 BKE_object_copy_softbody(ob_dst, ob_src, 0);
705 break;
706 case eModifierType_Skin:
707 /* ensure skin-node customdata exists */
708 BKE_mesh_ensure_skin_customdata(ob_dst->data);
709 break;
710 }
711
712 nmd = BKE_modifier_new(md->type);
713 BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
714
715 if (md->type == eModifierType_Multires) {
716 /* Has to be done after mod creation, but *before* we actually copy its settings! */
717 multiresModifier_sync_levels_ex(
718 ob_dst, (MultiresModifierData *)md, (MultiresModifierData *)nmd);
719 }
720
721 BKE_modifier_copydata(md, nmd);
722 BLI_addtail(&ob_dst->modifiers, nmd);
723 BKE_modifier_unique_name(&ob_dst->modifiers, nmd);
724
725 return true;
726 }
727
BKE_object_copy_gpencil_modifier(struct Object * ob_dst,GpencilModifierData * md)728 bool BKE_object_copy_gpencil_modifier(struct Object *ob_dst, GpencilModifierData *md)
729 {
730 GpencilModifierData *nmd = NULL;
731
732 nmd = BKE_gpencil_modifier_new(md->type);
733 BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
734
735 const GpencilModifierTypeInfo *mti = BKE_gpencil_modifier_get_info(md->type);
736 mti->copyData(md, nmd);
737
738 BLI_addtail(&ob_dst->greasepencil_modifiers, nmd);
739 BKE_gpencil_modifier_unique_name(&ob_dst->greasepencil_modifiers, nmd);
740
741 return true;
742 }
743
BKE_object_link_modifiers(struct Object * ob_dst,const struct Object * ob_src)744 void BKE_object_link_modifiers(struct Object *ob_dst, const struct Object *ob_src)
745 {
746 BKE_object_free_modifiers(ob_dst, 0);
747
748 if (!ELEM(ob_dst->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE, OB_GPENCIL)) {
749 /* only objects listed above can have modifiers and linking them to objects
750 * which doesn't have modifiers stack is quite silly */
751 return;
752 }
753
754 /* No grease pencil modifiers. */
755 if ((ob_src->type != OB_GPENCIL) && (ob_dst->type != OB_GPENCIL)) {
756 LISTBASE_FOREACH (ModifierData *, md, &ob_src->modifiers) {
757 BKE_object_copy_modifier(ob_dst, ob_src, md);
758 }
759 }
760
761 /* Copy grease pencil modifiers. */
762 if ((ob_src->type == OB_GPENCIL) && (ob_dst->type == OB_GPENCIL)) {
763 LISTBASE_FOREACH (GpencilModifierData *, md, &ob_src->greasepencil_modifiers) {
764 BKE_object_copy_gpencil_modifier(ob_dst, md);
765 }
766 }
767
768 BKE_object_copy_particlesystems(ob_dst, ob_src, 0);
769
770 /* TODO: smoke?, cloth? */
771 }
772
773 /**
774 * Copy CCG related data. Used to sync copy of mesh with reshaped original mesh.
775 */
copy_ccg_data(Mesh * mesh_destination,Mesh * mesh_source,int layer_type)776 static void copy_ccg_data(Mesh *mesh_destination, Mesh *mesh_source, int layer_type)
777 {
778 BLI_assert(mesh_destination->totloop == mesh_source->totloop);
779 CustomData *data_destination = &mesh_destination->ldata;
780 CustomData *data_source = &mesh_source->ldata;
781 const int num_elements = mesh_source->totloop;
782 if (!CustomData_has_layer(data_source, layer_type)) {
783 return;
784 }
785 const int layer_index = CustomData_get_layer_index(data_destination, layer_type);
786 CustomData_free_layer(data_destination, layer_type, num_elements, layer_index);
787 BLI_assert(!CustomData_has_layer(data_destination, layer_type));
788 CustomData_add_layer(data_destination, layer_type, CD_CALLOC, NULL, num_elements);
789 BLI_assert(CustomData_has_layer(data_destination, layer_type));
790 CustomData_copy_layer_type_data(data_source, data_destination, layer_type, 0, 0, num_elements);
791 }
792
object_update_from_subsurf_ccg(Object * object)793 static void object_update_from_subsurf_ccg(Object *object)
794 {
795 /* Currently CCG is only created for Mesh objects. */
796 if (object->type != OB_MESH) {
797 return;
798 }
799 /* If object does not own evaluated mesh we can not access it since it might be freed already
800 * (happens on dependency graph free where order of CoW-ed IDs free is undefined).
801 *
802 * Good news is: such mesh does not have modifiers applied, so no need to worry about CCG. */
803 if (!object->runtime.is_data_eval_owned) {
804 return;
805 }
806 /* Object was never evaluated, so can not have CCG subdivision surface. */
807 Mesh *mesh_eval = BKE_object_get_evaluated_mesh(object);
808 if (mesh_eval == NULL) {
809 return;
810 }
811 SubdivCCG *subdiv_ccg = mesh_eval->runtime.subdiv_ccg;
812 if (subdiv_ccg == NULL) {
813 return;
814 }
815 /* Check whether there is anything to be reshaped. */
816 if (!subdiv_ccg->dirty.coords && !subdiv_ccg->dirty.hidden) {
817 return;
818 }
819 const int tot_level = mesh_eval->runtime.subdiv_ccg_tot_level;
820 Object *object_orig = DEG_get_original_object(object);
821 Mesh *mesh_orig = (Mesh *)object_orig->data;
822 multiresModifier_reshapeFromCCG(tot_level, mesh_orig, subdiv_ccg);
823 /* NOTE: we need to reshape into an original mesh from main database,
824 * allowing:
825 *
826 * - Update copies of that mesh at any moment.
827 * - Save the file without doing extra reshape.
828 * - All the users of the mesh have updated displacement.
829 *
830 * However, the tricky part here is that we only know about sculpted
831 * state of a mesh on an object level, and object is being updated after
832 * mesh datablock is updated. This forces us to:
833 *
834 * - Update mesh datablock from object evaluation, which is technically
835 * forbidden, but there is no other place for this yet.
836 * - Reshape to the original mesh from main database, and then copy updated
837 * layer to copy of that mesh (since copy of the mesh has decoupled
838 * custom data layers).
839 *
840 * All this is defeating all the designs we need to follow to allow safe
841 * threaded evaluation, but this is as good as we can make it within the
842 * current sculpt//evaluated mesh design. This is also how we've survived
843 * with old DerivedMesh based solutions. So, while this is all wrong and
844 * needs reconsideration, doesn't seem to be a big stopper for real
845 * production artists.
846 */
847 /* TODO(sergey): Solve this somehow, to be fully stable for threaded
848 * evaluation environment.
849 */
850 /* NOTE: runtime.data_orig is what was before assigning mesh_eval,
851 * it is orig as in what was in object_eval->data before evaluating
852 * modifier stack.
853 *
854 * mesh_cow is a copy-on-written version od object_orig->data.
855 */
856 Mesh *mesh_cow = (Mesh *)object->runtime.data_orig;
857 copy_ccg_data(mesh_cow, mesh_orig, CD_MDISPS);
858 copy_ccg_data(mesh_cow, mesh_orig, CD_GRID_PAINT_MASK);
859 /* Everything is now up-to-date. */
860 subdiv_ccg->dirty.coords = false;
861 subdiv_ccg->dirty.hidden = false;
862 }
863
864 /**
865 * Assign #Object.data after modifier stack evaluation.
866 */
BKE_object_eval_assign_data(Object * object_eval,ID * data_eval,bool is_owned)867 void BKE_object_eval_assign_data(Object *object_eval, ID *data_eval, bool is_owned)
868 {
869 BLI_assert(object_eval->id.tag & LIB_TAG_COPIED_ON_WRITE);
870 BLI_assert(object_eval->runtime.data_eval == NULL);
871 BLI_assert(data_eval->tag & LIB_TAG_NO_MAIN);
872
873 if (is_owned) {
874 /* Set flag for debugging. */
875 data_eval->tag |= LIB_TAG_COPIED_ON_WRITE_EVAL_RESULT;
876 }
877
878 /* Assigned evaluated data. */
879 object_eval->runtime.data_eval = data_eval;
880 object_eval->runtime.is_data_eval_owned = is_owned;
881
882 /* Overwrite data of evaluated object, if the datablock types match. */
883 ID *data = object_eval->data;
884 if (GS(data->name) == GS(data_eval->name)) {
885 /* NOTE: we are not supposed to invoke evaluation for original objects,
886 * but some areas are still being ported, so we play safe here. */
887 if (object_eval->id.tag & LIB_TAG_COPIED_ON_WRITE) {
888 object_eval->data = data_eval;
889 }
890 }
891 }
892
893 /**
894 * Free data derived from mesh, called when mesh changes or is freed.
895 */
BKE_object_free_derived_caches(Object * ob)896 void BKE_object_free_derived_caches(Object *ob)
897 {
898 MEM_SAFE_FREE(ob->runtime.bb);
899
900 object_update_from_subsurf_ccg(ob);
901
902 if (ob->runtime.data_eval != NULL) {
903 if (ob->runtime.is_data_eval_owned) {
904 ID *data_eval = ob->runtime.data_eval;
905 if (GS(data_eval->name) == ID_ME) {
906 BKE_mesh_eval_delete((Mesh *)data_eval);
907 }
908 else {
909 BKE_libblock_free_datablock(data_eval, 0);
910 MEM_freeN(data_eval);
911 }
912 }
913 ob->runtime.data_eval = NULL;
914 }
915 if (ob->runtime.mesh_deform_eval != NULL) {
916 Mesh *mesh_deform_eval = ob->runtime.mesh_deform_eval;
917 BKE_mesh_eval_delete(mesh_deform_eval);
918 ob->runtime.mesh_deform_eval = NULL;
919 }
920
921 /* Restore initial pointer for copy-on-write datablocks, object->data
922 * might be pointing to an evaluated datablock data was just freed above. */
923 if (ob->runtime.data_orig != NULL) {
924 ob->data = ob->runtime.data_orig;
925 }
926
927 BKE_object_to_mesh_clear(ob);
928 BKE_object_free_curve_cache(ob);
929
930 /* Clear grease pencil data. */
931 if (ob->runtime.gpd_eval != NULL) {
932 BKE_gpencil_eval_delete(ob->runtime.gpd_eval);
933 ob->runtime.gpd_eval = NULL;
934 }
935 }
936
BKE_object_free_caches(Object * object)937 void BKE_object_free_caches(Object *object)
938 {
939 ModifierData *md;
940 short update_flag = 0;
941
942 /* Free particle system caches holding paths. */
943 if (object->particlesystem.first) {
944 ParticleSystem *psys;
945 for (psys = object->particlesystem.first; psys != NULL; psys = psys->next) {
946 psys_free_path_cache(psys, psys->edit);
947 update_flag |= ID_RECALC_PSYS_REDO;
948 }
949 }
950
951 /* Free memory used by cached derived meshes in the particle system modifiers. */
952 for (md = object->modifiers.first; md != NULL; md = md->next) {
953 if (md->type == eModifierType_ParticleSystem) {
954 ParticleSystemModifierData *psmd = (ParticleSystemModifierData *)md;
955 if (psmd->mesh_final) {
956 BKE_id_free(NULL, psmd->mesh_final);
957 psmd->mesh_final = NULL;
958 if (psmd->mesh_original) {
959 BKE_id_free(NULL, psmd->mesh_original);
960 psmd->mesh_original = NULL;
961 }
962 psmd->flag |= eParticleSystemFlag_file_loaded;
963 update_flag |= ID_RECALC_GEOMETRY;
964 }
965 }
966 }
967
968 /* NOTE: If object is coming from a duplicator, it might be a temporary
969 * object created by dependency graph, which shares pointers with original
970 * object. In this case we can not free anything.
971 */
972 if ((object->base_flag & BASE_FROM_DUPLI) == 0) {
973 BKE_object_free_derived_caches(object);
974 update_flag |= ID_RECALC_GEOMETRY;
975 }
976
977 /* Tag object for update, so once memory critical operation is over and
978 * scene update routines are back to its business the object will be
979 * guaranteed to be in a known state.
980 */
981 if (update_flag != 0) {
982 DEG_id_tag_update(&object->id, update_flag);
983 }
984 }
985
986 /**
987 * Actual check for internal data, not context or flags.
988 */
BKE_object_is_in_editmode(const Object * ob)989 bool BKE_object_is_in_editmode(const Object *ob)
990 {
991 if (ob->data == NULL) {
992 return false;
993 }
994
995 switch (ob->type) {
996 case OB_MESH:
997 return ((Mesh *)ob->data)->edit_mesh != NULL;
998 case OB_ARMATURE:
999 return ((bArmature *)ob->data)->edbo != NULL;
1000 case OB_FONT:
1001 return ((Curve *)ob->data)->editfont != NULL;
1002 case OB_MBALL:
1003 return ((MetaBall *)ob->data)->editelems != NULL;
1004 case OB_LATTICE:
1005 return ((Lattice *)ob->data)->editlatt != NULL;
1006 case OB_SURF:
1007 case OB_CURVE:
1008 return ((Curve *)ob->data)->editnurb != NULL;
1009 case OB_GPENCIL:
1010 /* Grease Pencil object has no edit mode data. */
1011 return GPENCIL_EDIT_MODE((bGPdata *)ob->data);
1012 default:
1013 return false;
1014 }
1015 }
1016
BKE_object_is_in_editmode_vgroup(const Object * ob)1017 bool BKE_object_is_in_editmode_vgroup(const Object *ob)
1018 {
1019 return (OB_TYPE_SUPPORT_VGROUP(ob->type) && BKE_object_is_in_editmode(ob));
1020 }
1021
BKE_object_data_is_in_editmode(const ID * id)1022 bool BKE_object_data_is_in_editmode(const ID *id)
1023 {
1024 const short type = GS(id->name);
1025 BLI_assert(OB_DATA_SUPPORT_EDITMODE(type));
1026 switch (type) {
1027 case ID_ME:
1028 return ((const Mesh *)id)->edit_mesh != NULL;
1029 case ID_CU:
1030 return ((((const Curve *)id)->editnurb != NULL) || (((const Curve *)id)->editfont != NULL));
1031 case ID_MB:
1032 return ((const MetaBall *)id)->editelems != NULL;
1033 case ID_LT:
1034 return ((const Lattice *)id)->editlatt != NULL;
1035 case ID_AR:
1036 return ((const bArmature *)id)->edbo != NULL;
1037 default:
1038 BLI_assert(0);
1039 return false;
1040 }
1041 }
1042
BKE_object_data_editmode_flush_ptr_get(struct ID * id)1043 char *BKE_object_data_editmode_flush_ptr_get(struct ID *id)
1044 {
1045 const short type = GS(id->name);
1046 switch (type) {
1047 case ID_ME: {
1048 BMEditMesh *em = ((Mesh *)id)->edit_mesh;
1049 if (em != NULL) {
1050 return &em->needs_flush_to_id;
1051 }
1052 break;
1053 }
1054 case ID_CU: {
1055 if (((Curve *)id)->vfont != NULL) {
1056 EditFont *ef = ((Curve *)id)->editfont;
1057 if (ef != NULL) {
1058 return &ef->needs_flush_to_id;
1059 }
1060 }
1061 else {
1062 EditNurb *editnurb = ((Curve *)id)->editnurb;
1063 if (editnurb) {
1064 return &editnurb->needs_flush_to_id;
1065 }
1066 }
1067 break;
1068 }
1069 case ID_MB: {
1070 MetaBall *mb = (MetaBall *)id;
1071 return &mb->needs_flush_to_id;
1072 }
1073 case ID_LT: {
1074 EditLatt *editlatt = ((Lattice *)id)->editlatt;
1075 if (editlatt) {
1076 return &editlatt->needs_flush_to_id;
1077 }
1078 break;
1079 }
1080 case ID_AR: {
1081 bArmature *arm = (bArmature *)id;
1082 return &arm->needs_flush_to_id;
1083 }
1084 default:
1085 BLI_assert(0);
1086 return NULL;
1087 }
1088 return NULL;
1089 }
1090
BKE_object_is_in_wpaint_select_vert(const Object * ob)1091 bool BKE_object_is_in_wpaint_select_vert(const Object *ob)
1092 {
1093 if (ob->type == OB_MESH) {
1094 Mesh *me = ob->data;
1095 return ((ob->mode & OB_MODE_WEIGHT_PAINT) && (me->edit_mesh == NULL) &&
1096 (ME_EDIT_PAINT_SEL_MODE(me) == SCE_SELECT_VERTEX));
1097 }
1098
1099 return false;
1100 }
1101
BKE_object_has_mode_data(const struct Object * ob,eObjectMode object_mode)1102 bool BKE_object_has_mode_data(const struct Object *ob, eObjectMode object_mode)
1103 {
1104 if (object_mode & OB_MODE_EDIT) {
1105 if (BKE_object_is_in_editmode(ob)) {
1106 return true;
1107 }
1108 }
1109 else if (object_mode & OB_MODE_VERTEX_PAINT) {
1110 if (ob->sculpt && (ob->sculpt->mode_type == OB_MODE_VERTEX_PAINT)) {
1111 return true;
1112 }
1113 }
1114 else if (object_mode & OB_MODE_WEIGHT_PAINT) {
1115 if (ob->sculpt && (ob->sculpt->mode_type == OB_MODE_WEIGHT_PAINT)) {
1116 return true;
1117 }
1118 }
1119 else if (object_mode & OB_MODE_SCULPT) {
1120 if (ob->sculpt && (ob->sculpt->mode_type == OB_MODE_SCULPT)) {
1121 return true;
1122 }
1123 }
1124 else if (object_mode & OB_MODE_POSE) {
1125 if (ob->pose != NULL) {
1126 return true;
1127 }
1128 }
1129 return false;
1130 }
1131
BKE_object_is_mode_compat(const struct Object * ob,eObjectMode object_mode)1132 bool BKE_object_is_mode_compat(const struct Object *ob, eObjectMode object_mode)
1133 {
1134 return ((ob->mode == object_mode) || (ob->mode & object_mode) != 0);
1135 }
1136
1137 /**
1138 * Return which parts of the object are visible, as evaluated by depsgraph
1139 */
BKE_object_visibility(const Object * ob,const int dag_eval_mode)1140 int BKE_object_visibility(const Object *ob, const int dag_eval_mode)
1141 {
1142 if ((ob->base_flag & BASE_VISIBLE_DEPSGRAPH) == 0) {
1143 return 0;
1144 }
1145
1146 /* Test which components the object has. */
1147 int visibility = OB_VISIBLE_SELF;
1148 if (ob->particlesystem.first) {
1149 visibility |= OB_VISIBLE_INSTANCES | OB_VISIBLE_PARTICLES;
1150 }
1151 else if (ob->transflag & OB_DUPLI) {
1152 visibility |= OB_VISIBLE_INSTANCES;
1153 }
1154
1155 /* Optional hiding of self if there are particles or instancers. */
1156 if (visibility & (OB_VISIBLE_PARTICLES | OB_VISIBLE_INSTANCES)) {
1157 switch ((eEvaluationMode)dag_eval_mode) {
1158 case DAG_EVAL_VIEWPORT:
1159 if (!(ob->duplicator_visibility_flag & OB_DUPLI_FLAG_VIEWPORT)) {
1160 visibility &= ~OB_VISIBLE_SELF;
1161 }
1162 break;
1163 case DAG_EVAL_RENDER:
1164 if (!(ob->duplicator_visibility_flag & OB_DUPLI_FLAG_RENDER)) {
1165 visibility &= ~OB_VISIBLE_SELF;
1166 }
1167 break;
1168 }
1169 }
1170
1171 return visibility;
1172 }
1173
BKE_object_exists_check(Main * bmain,const Object * obtest)1174 bool BKE_object_exists_check(Main *bmain, const Object *obtest)
1175 {
1176 Object *ob;
1177
1178 if (obtest == NULL) {
1179 return false;
1180 }
1181
1182 ob = bmain->objects.first;
1183 while (ob) {
1184 if (ob == obtest) {
1185 return true;
1186 }
1187 ob = ob->id.next;
1188 }
1189 return false;
1190 }
1191
1192 /* *************************************************** */
1193
get_obdata_defname(int type)1194 static const char *get_obdata_defname(int type)
1195 {
1196 switch (type) {
1197 case OB_MESH:
1198 return DATA_("Mesh");
1199 case OB_CURVE:
1200 return DATA_("Curve");
1201 case OB_SURF:
1202 return DATA_("Surf");
1203 case OB_FONT:
1204 return DATA_("Text");
1205 case OB_MBALL:
1206 return DATA_("Mball");
1207 case OB_CAMERA:
1208 return DATA_("Camera");
1209 case OB_LAMP:
1210 return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "Light");
1211 case OB_LATTICE:
1212 return DATA_("Lattice");
1213 case OB_ARMATURE:
1214 return DATA_("Armature");
1215 case OB_SPEAKER:
1216 return DATA_("Speaker");
1217 case OB_HAIR:
1218 return DATA_("Hair");
1219 case OB_POINTCLOUD:
1220 return DATA_("PointCloud");
1221 case OB_VOLUME:
1222 return DATA_("Volume");
1223 case OB_EMPTY:
1224 return DATA_("Empty");
1225 case OB_GPENCIL:
1226 return DATA_("GPencil");
1227 case OB_LIGHTPROBE:
1228 return DATA_("LightProbe");
1229 default:
1230 CLOG_ERROR(&LOG, "Internal error, bad type: %d", type);
1231 return DATA_("Empty");
1232 }
1233 }
1234
object_init(Object * ob,const short ob_type)1235 static void object_init(Object *ob, const short ob_type)
1236 {
1237 object_init_data(&ob->id);
1238
1239 ob->type = ob_type;
1240
1241 if (ob->type != OB_EMPTY) {
1242 zero_v2(ob->ima_ofs);
1243 }
1244
1245 if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1246 ob->trackflag = OB_NEGZ;
1247 ob->upflag = OB_POSY;
1248 }
1249
1250 if (ob->type == OB_GPENCIL) {
1251 ob->dtx |= OB_USE_GPENCIL_LIGHTS;
1252 }
1253 }
1254
BKE_object_obdata_add_from_type(Main * bmain,int type,const char * name)1255 void *BKE_object_obdata_add_from_type(Main *bmain, int type, const char *name)
1256 {
1257 if (name == NULL) {
1258 name = get_obdata_defname(type);
1259 }
1260
1261 switch (type) {
1262 case OB_MESH:
1263 return BKE_mesh_add(bmain, name);
1264 case OB_CURVE:
1265 return BKE_curve_add(bmain, name, OB_CURVE);
1266 case OB_SURF:
1267 return BKE_curve_add(bmain, name, OB_SURF);
1268 case OB_FONT:
1269 return BKE_curve_add(bmain, name, OB_FONT);
1270 case OB_MBALL:
1271 return BKE_mball_add(bmain, name);
1272 case OB_CAMERA:
1273 return BKE_camera_add(bmain, name);
1274 case OB_LAMP:
1275 return BKE_light_add(bmain, name);
1276 case OB_LATTICE:
1277 return BKE_lattice_add(bmain, name);
1278 case OB_ARMATURE:
1279 return BKE_armature_add(bmain, name);
1280 case OB_SPEAKER:
1281 return BKE_speaker_add(bmain, name);
1282 case OB_LIGHTPROBE:
1283 return BKE_lightprobe_add(bmain, name);
1284 case OB_GPENCIL:
1285 return BKE_gpencil_data_addnew(bmain, name);
1286 case OB_HAIR:
1287 return BKE_hair_add(bmain, name);
1288 case OB_POINTCLOUD:
1289 return BKE_pointcloud_add_default(bmain, name);
1290 case OB_VOLUME:
1291 return BKE_volume_add(bmain, name);
1292 case OB_EMPTY:
1293 return NULL;
1294 default:
1295 CLOG_ERROR(&LOG, "Internal error, bad type: %d", type);
1296 return NULL;
1297 }
1298 }
1299
1300 /**
1301 * Return -1 on failure.
1302 */
BKE_object_obdata_to_type(const ID * id)1303 int BKE_object_obdata_to_type(const ID *id)
1304 {
1305 /* Keep in sync with #OB_DATA_SUPPORT_ID macro. */
1306 switch (GS(id->name)) {
1307 case ID_ME:
1308 return OB_MESH;
1309 case ID_CU:
1310 return BKE_curve_type_get((const Curve *)id);
1311 case ID_MB:
1312 return OB_MBALL;
1313 case ID_LA:
1314 return OB_LAMP;
1315 case ID_SPK:
1316 return OB_SPEAKER;
1317 case ID_CA:
1318 return OB_CAMERA;
1319 case ID_LT:
1320 return OB_LATTICE;
1321 case ID_GD:
1322 return OB_GPENCIL;
1323 case ID_AR:
1324 return OB_ARMATURE;
1325 case ID_LP:
1326 return OB_LIGHTPROBE;
1327 case ID_HA:
1328 return OB_HAIR;
1329 case ID_PT:
1330 return OB_POINTCLOUD;
1331 case ID_VO:
1332 return OB_VOLUME;
1333 default:
1334 return -1;
1335 }
1336 }
1337
1338 /**
1339 * More general add: creates minimum required data, but without vertices etc.
1340 */
BKE_object_add_only_object(Main * bmain,int type,const char * name)1341 Object *BKE_object_add_only_object(Main *bmain, int type, const char *name)
1342 {
1343 Object *ob;
1344
1345 if (!name) {
1346 name = get_obdata_defname(type);
1347 }
1348
1349 /* We cannot use #BKE_id_new here as we need some custom initialization code. */
1350 ob = BKE_libblock_alloc(bmain, ID_OB, name, 0);
1351
1352 /* We increase object user count when linking to Collections. */
1353 id_us_min(&ob->id);
1354
1355 /* default object vars */
1356 object_init(ob, type);
1357
1358 return ob;
1359 }
1360
object_add_common(Main * bmain,ViewLayer * view_layer,int type,const char * name)1361 static Object *object_add_common(Main *bmain, ViewLayer *view_layer, int type, const char *name)
1362 {
1363 Object *ob;
1364
1365 ob = BKE_object_add_only_object(bmain, type, name);
1366 ob->data = BKE_object_obdata_add_from_type(bmain, type, name);
1367 BKE_view_layer_base_deselect_all(view_layer);
1368
1369 DEG_id_tag_update_ex(
1370 bmain, &ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1371 return ob;
1372 }
1373
1374 /**
1375 * General add: to scene, with layer from area and default name
1376 *
1377 * Object is added to the active #Collection.
1378 * If there is no linked collection to the active #ViewLayer we create a new one.
1379 *
1380 * \note Creates minimum required data, but without vertices etc.
1381 */
BKE_object_add(Main * bmain,ViewLayer * view_layer,int type,const char * name)1382 Object *BKE_object_add(Main *bmain, ViewLayer *view_layer, int type, const char *name)
1383 {
1384 Object *ob;
1385 Base *base;
1386 LayerCollection *layer_collection;
1387
1388 ob = object_add_common(bmain, view_layer, type, name);
1389
1390 layer_collection = BKE_layer_collection_get_active(view_layer);
1391 BKE_collection_object_add(bmain, layer_collection->collection, ob);
1392
1393 base = BKE_view_layer_base_find(view_layer, ob);
1394 BKE_view_layer_base_select_and_set_active(view_layer, base);
1395
1396 return ob;
1397 }
1398
1399 /**
1400 * Add a new object, using another one as a reference
1401 *
1402 * \param ob_src: object to use to determine the collections of the new object.
1403 */
BKE_object_add_from(Main * bmain,Scene * scene,ViewLayer * view_layer,int type,const char * name,Object * ob_src)1404 Object *BKE_object_add_from(
1405 Main *bmain, Scene *scene, ViewLayer *view_layer, int type, const char *name, Object *ob_src)
1406 {
1407 Object *ob;
1408 Base *base;
1409
1410 ob = object_add_common(bmain, view_layer, type, name);
1411 BKE_collection_object_add_from(bmain, scene, ob_src, ob);
1412
1413 base = BKE_view_layer_base_find(view_layer, ob);
1414 BKE_view_layer_base_select_and_set_active(view_layer, base);
1415
1416 return ob;
1417 }
1418
1419 /**
1420 * Add a new object, but assign the given datablock as the ob->data
1421 * for the newly created object.
1422 *
1423 * \param data: The datablock to assign as ob->data for the new object.
1424 * This is assumed to be of the correct type.
1425 * \param do_id_user: If true, id_us_plus() will be called on data when
1426 * assigning it to the object.
1427 */
BKE_object_add_for_data(Main * bmain,ViewLayer * view_layer,int type,const char * name,ID * data,bool do_id_user)1428 Object *BKE_object_add_for_data(
1429 Main *bmain, ViewLayer *view_layer, int type, const char *name, ID *data, bool do_id_user)
1430 {
1431 Object *ob;
1432 Base *base;
1433 LayerCollection *layer_collection;
1434
1435 /* same as object_add_common, except we don't create new ob->data */
1436 ob = BKE_object_add_only_object(bmain, type, name);
1437 ob->data = data;
1438 if (do_id_user) {
1439 id_us_plus(data);
1440 }
1441
1442 BKE_view_layer_base_deselect_all(view_layer);
1443 DEG_id_tag_update_ex(
1444 bmain, &ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1445
1446 layer_collection = BKE_layer_collection_get_active(view_layer);
1447 BKE_collection_object_add(bmain, layer_collection->collection, ob);
1448
1449 base = BKE_view_layer_base_find(view_layer, ob);
1450 BKE_view_layer_base_select_and_set_active(view_layer, base);
1451
1452 return ob;
1453 }
1454
BKE_object_copy_softbody(struct Object * ob_dst,const struct Object * ob_src,const int flag)1455 void BKE_object_copy_softbody(struct Object *ob_dst, const struct Object *ob_src, const int flag)
1456 {
1457 SoftBody *sb = ob_src->soft;
1458 SoftBody *sbn;
1459 bool tagged_no_main = ob_dst->id.tag & LIB_TAG_NO_MAIN;
1460
1461 ob_dst->softflag = ob_src->softflag;
1462 if (sb == NULL) {
1463 ob_dst->soft = NULL;
1464 return;
1465 }
1466
1467 sbn = MEM_dupallocN(sb);
1468
1469 if ((flag & LIB_ID_COPY_CACHES) == 0) {
1470 sbn->totspring = sbn->totpoint = 0;
1471 sbn->bpoint = NULL;
1472 sbn->bspring = NULL;
1473 }
1474 else {
1475 sbn->totspring = sb->totspring;
1476 sbn->totpoint = sb->totpoint;
1477
1478 if (sbn->bpoint) {
1479 int i;
1480
1481 sbn->bpoint = MEM_dupallocN(sbn->bpoint);
1482
1483 for (i = 0; i < sbn->totpoint; i++) {
1484 if (sbn->bpoint[i].springs) {
1485 sbn->bpoint[i].springs = MEM_dupallocN(sbn->bpoint[i].springs);
1486 }
1487 }
1488 }
1489
1490 if (sb->bspring) {
1491 sbn->bspring = MEM_dupallocN(sb->bspring);
1492 }
1493 }
1494
1495 sbn->keys = NULL;
1496 sbn->totkey = sbn->totpointkey = 0;
1497
1498 sbn->scratch = NULL;
1499
1500 if (tagged_no_main == 0) {
1501 sbn->shared = MEM_dupallocN(sb->shared);
1502 sbn->shared->pointcache = BKE_ptcache_copy_list(
1503 &sbn->shared->ptcaches, &sb->shared->ptcaches, flag);
1504 }
1505
1506 if (sb->effector_weights) {
1507 sbn->effector_weights = MEM_dupallocN(sb->effector_weights);
1508 }
1509
1510 ob_dst->soft = sbn;
1511 }
1512
BKE_object_copy_particlesystem(ParticleSystem * psys,const int flag)1513 ParticleSystem *BKE_object_copy_particlesystem(ParticleSystem *psys, const int flag)
1514 {
1515 ParticleSystem *psysn = MEM_dupallocN(psys);
1516
1517 psys_copy_particles(psysn, psys);
1518
1519 if (psys->clmd) {
1520 psysn->clmd = (ClothModifierData *)BKE_modifier_new(eModifierType_Cloth);
1521 BKE_modifier_copydata_ex((ModifierData *)psys->clmd, (ModifierData *)psysn->clmd, flag);
1522 psys->hair_in_mesh = psys->hair_out_mesh = NULL;
1523 }
1524
1525 BLI_duplicatelist(&psysn->targets, &psys->targets);
1526
1527 psysn->pathcache = NULL;
1528 psysn->childcache = NULL;
1529 psysn->edit = NULL;
1530 psysn->pdd = NULL;
1531 psysn->effectors = NULL;
1532 psysn->tree = NULL;
1533 psysn->bvhtree = NULL;
1534 psysn->batch_cache = NULL;
1535
1536 BLI_listbase_clear(&psysn->pathcachebufs);
1537 BLI_listbase_clear(&psysn->childcachebufs);
1538
1539 if (flag & LIB_ID_CREATE_NO_MAIN) {
1540 BLI_assert((psys->flag & PSYS_SHARED_CACHES) == 0);
1541 psysn->flag |= PSYS_SHARED_CACHES;
1542 BLI_assert(psysn->pointcache != NULL);
1543 }
1544 else {
1545 psysn->pointcache = BKE_ptcache_copy_list(&psysn->ptcaches, &psys->ptcaches, flag);
1546 }
1547
1548 /* XXX - from reading existing code this seems correct but intended usage of
1549 * pointcache should /w cloth should be added in 'ParticleSystem' - campbell */
1550 if (psysn->clmd) {
1551 psysn->clmd->point_cache = psysn->pointcache;
1552 }
1553
1554 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
1555 id_us_plus((ID *)psysn->part);
1556 }
1557
1558 return psysn;
1559 }
1560
BKE_object_copy_particlesystems(Object * ob_dst,const Object * ob_src,const int flag)1561 void BKE_object_copy_particlesystems(Object *ob_dst, const Object *ob_src, const int flag)
1562 {
1563 ParticleSystem *psys, *npsys;
1564 ModifierData *md;
1565
1566 if (ob_dst->type != OB_MESH) {
1567 /* currently only mesh objects can have soft body */
1568 return;
1569 }
1570
1571 BLI_listbase_clear(&ob_dst->particlesystem);
1572 for (psys = ob_src->particlesystem.first; psys; psys = psys->next) {
1573 npsys = BKE_object_copy_particlesystem(psys, flag);
1574
1575 BLI_addtail(&ob_dst->particlesystem, npsys);
1576
1577 /* need to update particle modifiers too */
1578 for (md = ob_dst->modifiers.first; md; md = md->next) {
1579 if (md->type == eModifierType_ParticleSystem) {
1580 ParticleSystemModifierData *psmd = (ParticleSystemModifierData *)md;
1581 if (psmd->psys == psys) {
1582 psmd->psys = npsys;
1583 }
1584 }
1585 else if (md->type == eModifierType_DynamicPaint) {
1586 DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
1587 if (pmd->brush) {
1588 if (pmd->brush->psys == psys) {
1589 pmd->brush->psys = npsys;
1590 }
1591 }
1592 }
1593 else if (md->type == eModifierType_Fluid) {
1594 FluidModifierData *fmd = (FluidModifierData *)md;
1595
1596 if (fmd->type == MOD_FLUID_TYPE_FLOW) {
1597 if (fmd->flow) {
1598 if (fmd->flow->psys == psys) {
1599 fmd->flow->psys = npsys;
1600 }
1601 }
1602 }
1603 }
1604 }
1605 }
1606 }
1607
copy_object_pose(Object * obn,const Object * ob,const int flag)1608 static void copy_object_pose(Object *obn, const Object *ob, const int flag)
1609 {
1610 bPoseChannel *chan;
1611
1612 /* note: need to clear obn->pose pointer first,
1613 * so that BKE_pose_copy_data works (otherwise there's a crash) */
1614 obn->pose = NULL;
1615 BKE_pose_copy_data_ex(&obn->pose, ob->pose, flag, true); /* true = copy constraints */
1616
1617 for (chan = obn->pose->chanbase.first; chan; chan = chan->next) {
1618 bConstraint *con;
1619
1620 chan->flag &= ~(POSE_LOC | POSE_ROT | POSE_SIZE);
1621
1622 /* XXX Remapping object pointing onto itself should be handled by generic
1623 * BKE_library_remap stuff, but...
1624 * the flush_constraint_targets callback am not sure about, so will delay that for now. */
1625 for (con = chan->constraints.first; con; con = con->next) {
1626 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
1627 ListBase targets = {NULL, NULL};
1628 bConstraintTarget *ct;
1629
1630 if (cti && cti->get_constraint_targets) {
1631 cti->get_constraint_targets(con, &targets);
1632
1633 for (ct = targets.first; ct; ct = ct->next) {
1634 if (ct->tar == ob) {
1635 ct->tar = obn;
1636 }
1637 }
1638
1639 if (cti->flush_constraint_targets) {
1640 cti->flush_constraint_targets(con, &targets, 0);
1641 }
1642 }
1643 }
1644 }
1645 }
1646
BKE_object_pose_context_check(const Object * ob)1647 bool BKE_object_pose_context_check(const Object *ob)
1648 {
1649 if ((ob) && (ob->type == OB_ARMATURE) && (ob->pose) && (ob->mode & OB_MODE_POSE)) {
1650 return true;
1651 }
1652
1653 return false;
1654 }
1655
BKE_object_pose_armature_get(Object * ob)1656 Object *BKE_object_pose_armature_get(Object *ob)
1657 {
1658 if (ob == NULL) {
1659 return NULL;
1660 }
1661
1662 if (BKE_object_pose_context_check(ob)) {
1663 return ob;
1664 }
1665
1666 ob = BKE_modifiers_is_deformed_by_armature(ob);
1667
1668 /* Only use selected check when non-active. */
1669 if (BKE_object_pose_context_check(ob)) {
1670 return ob;
1671 }
1672
1673 return NULL;
1674 }
1675
BKE_object_pose_armature_get_visible(Object * ob,ViewLayer * view_layer,View3D * v3d)1676 Object *BKE_object_pose_armature_get_visible(Object *ob, ViewLayer *view_layer, View3D *v3d)
1677 {
1678 Object *ob_armature = BKE_object_pose_armature_get(ob);
1679 if (ob_armature) {
1680 Base *base = BKE_view_layer_base_find(view_layer, ob_armature);
1681 if (base) {
1682 if (BASE_VISIBLE(v3d, base)) {
1683 return ob_armature;
1684 }
1685 }
1686 }
1687 return NULL;
1688 }
1689
1690 /**
1691 * Access pose array with special check to get pose object when in weight paint mode.
1692 */
BKE_object_pose_array_get_ex(ViewLayer * view_layer,View3D * v3d,uint * r_objects_len,bool unique)1693 Object **BKE_object_pose_array_get_ex(ViewLayer *view_layer,
1694 View3D *v3d,
1695 uint *r_objects_len,
1696 bool unique)
1697 {
1698 Object *ob_active = OBACT(view_layer);
1699 Object *ob_pose = BKE_object_pose_armature_get(ob_active);
1700 Object **objects = NULL;
1701 if (ob_pose == ob_active) {
1702 objects = BKE_view_layer_array_from_objects_in_mode(view_layer,
1703 v3d,
1704 r_objects_len,
1705 {
1706 .object_mode = OB_MODE_POSE,
1707 .no_dup_data = unique,
1708 });
1709 }
1710 else if (ob_pose != NULL) {
1711 *r_objects_len = 1;
1712 objects = MEM_mallocN(sizeof(*objects), __func__);
1713 objects[0] = ob_pose;
1714 }
1715 else {
1716 *r_objects_len = 0;
1717 objects = MEM_mallocN(0, __func__);
1718 }
1719 return objects;
1720 }
BKE_object_pose_array_get_unique(ViewLayer * view_layer,View3D * v3d,uint * r_objects_len)1721 Object **BKE_object_pose_array_get_unique(ViewLayer *view_layer, View3D *v3d, uint *r_objects_len)
1722 {
1723 return BKE_object_pose_array_get_ex(view_layer, v3d, r_objects_len, true);
1724 }
BKE_object_pose_array_get(ViewLayer * view_layer,View3D * v3d,uint * r_objects_len)1725 Object **BKE_object_pose_array_get(ViewLayer *view_layer, View3D *v3d, uint *r_objects_len)
1726 {
1727 return BKE_object_pose_array_get_ex(view_layer, v3d, r_objects_len, false);
1728 }
1729
BKE_object_pose_base_array_get_ex(ViewLayer * view_layer,View3D * v3d,uint * r_bases_len,bool unique)1730 Base **BKE_object_pose_base_array_get_ex(ViewLayer *view_layer,
1731 View3D *v3d,
1732 uint *r_bases_len,
1733 bool unique)
1734 {
1735 Base *base_active = BASACT(view_layer);
1736 Object *ob_pose = base_active ? BKE_object_pose_armature_get(base_active->object) : NULL;
1737 Base *base_pose = NULL;
1738 Base **bases = NULL;
1739
1740 if (base_active) {
1741 if (ob_pose == base_active->object) {
1742 base_pose = base_active;
1743 }
1744 else {
1745 base_pose = BKE_view_layer_base_find(view_layer, ob_pose);
1746 }
1747 }
1748
1749 if (base_active && (base_pose == base_active)) {
1750 bases = BKE_view_layer_array_from_bases_in_mode(view_layer,
1751 v3d,
1752 r_bases_len,
1753 {
1754 .object_mode = OB_MODE_POSE,
1755 .no_dup_data = unique,
1756 });
1757 }
1758 else if (base_pose != NULL) {
1759 *r_bases_len = 1;
1760 bases = MEM_mallocN(sizeof(*bases), __func__);
1761 bases[0] = base_pose;
1762 }
1763 else {
1764 *r_bases_len = 0;
1765 bases = MEM_mallocN(0, __func__);
1766 }
1767 return bases;
1768 }
BKE_object_pose_base_array_get_unique(ViewLayer * view_layer,View3D * v3d,uint * r_bases_len)1769 Base **BKE_object_pose_base_array_get_unique(ViewLayer *view_layer, View3D *v3d, uint *r_bases_len)
1770 {
1771 return BKE_object_pose_base_array_get_ex(view_layer, v3d, r_bases_len, true);
1772 }
BKE_object_pose_base_array_get(ViewLayer * view_layer,View3D * v3d,uint * r_bases_len)1773 Base **BKE_object_pose_base_array_get(ViewLayer *view_layer, View3D *v3d, uint *r_bases_len)
1774 {
1775 return BKE_object_pose_base_array_get_ex(view_layer, v3d, r_bases_len, false);
1776 }
1777
BKE_object_transform_copy(Object * ob_tar,const Object * ob_src)1778 void BKE_object_transform_copy(Object *ob_tar, const Object *ob_src)
1779 {
1780 copy_v3_v3(ob_tar->loc, ob_src->loc);
1781 copy_v3_v3(ob_tar->rot, ob_src->rot);
1782 copy_v4_v4(ob_tar->quat, ob_src->quat);
1783 copy_v3_v3(ob_tar->rotAxis, ob_src->rotAxis);
1784 ob_tar->rotAngle = ob_src->rotAngle;
1785 ob_tar->rotmode = ob_src->rotmode;
1786 copy_v3_v3(ob_tar->scale, ob_src->scale);
1787 }
1788
1789 /**
1790 * Perform deep-copy of object and its 'children' data-blocks (obdata, materials, actions, etc.).
1791 *
1792 * \param dupflag: Controls which sub-data are also duplicated
1793 * (see #eDupli_ID_Flags in DNA_userdef_types.h).
1794 *
1795 * \note This function does not do any remapping to new IDs, caller must do it
1796 * (\a #BKE_libblock_relink_to_newid()).
1797 * \note Caller MUST free \a newid pointers itself (#BKE_main_id_clear_newpoins()) and call updates
1798 * of DEG too (#DAG_relations_tag_update()).
1799 */
BKE_object_duplicate(Main * bmain,Object * ob,eDupli_ID_Flags dupflag,const eLibIDDuplicateFlags duplicate_options)1800 Object *BKE_object_duplicate(Main *bmain,
1801 Object *ob,
1802 eDupli_ID_Flags dupflag,
1803 const eLibIDDuplicateFlags duplicate_options)
1804 {
1805 const bool is_subprocess = (duplicate_options & LIB_ID_DUPLICATE_IS_SUBPROCESS) != 0;
1806
1807 if (!is_subprocess) {
1808 BKE_main_id_tag_all(bmain, LIB_TAG_NEW, false);
1809 BKE_main_id_clear_newpoins(bmain);
1810 /* In case root duplicated ID is linked, assume we want to get a local copy of it and duplicate
1811 * all expected linked data. */
1812 if (ID_IS_LINKED(ob)) {
1813 dupflag |= USER_DUP_LINKED_ID;
1814 }
1815 }
1816
1817 Material ***matarar;
1818
1819 Object *obn = (Object *)BKE_id_copy(bmain, &ob->id);
1820 id_us_min(&obn->id);
1821 if (is_subprocess) {
1822 ID_NEW_SET(ob, obn);
1823 }
1824
1825 /* 0 == full linked. */
1826 if (dupflag == 0) {
1827 return obn;
1828 }
1829
1830 BKE_animdata_duplicate_id_action(bmain, &obn->id, dupflag);
1831
1832 if (dupflag & USER_DUP_MAT) {
1833 for (int i = 0; i < obn->totcol; i++) {
1834 BKE_id_copy_for_duplicate(bmain, (ID *)obn->mat[i], dupflag);
1835 }
1836 }
1837 if (dupflag & USER_DUP_PSYS) {
1838 ParticleSystem *psys;
1839 for (psys = obn->particlesystem.first; psys; psys = psys->next) {
1840 BKE_id_copy_for_duplicate(bmain, (ID *)psys->part, dupflag);
1841 }
1842 }
1843
1844 ID *id_old = obn->data;
1845 ID *id_new = NULL;
1846 const bool need_to_duplicate_obdata = (id_old != NULL) && (id_old->newid == NULL);
1847
1848 switch (obn->type) {
1849 case OB_MESH:
1850 if (dupflag & USER_DUP_MESH) {
1851 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1852 }
1853 break;
1854 case OB_CURVE:
1855 if (dupflag & USER_DUP_CURVE) {
1856 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1857 }
1858 break;
1859 case OB_SURF:
1860 if (dupflag & USER_DUP_SURF) {
1861 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1862 }
1863 break;
1864 case OB_FONT:
1865 if (dupflag & USER_DUP_FONT) {
1866 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1867 }
1868 break;
1869 case OB_MBALL:
1870 if (dupflag & USER_DUP_MBALL) {
1871 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1872 }
1873 break;
1874 case OB_LAMP:
1875 if (dupflag & USER_DUP_LAMP) {
1876 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1877 }
1878 break;
1879 case OB_ARMATURE:
1880 if (dupflag & USER_DUP_ARM) {
1881 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1882 }
1883 break;
1884 case OB_LATTICE:
1885 if (dupflag != 0) {
1886 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1887 }
1888 break;
1889 case OB_CAMERA:
1890 if (dupflag != 0) {
1891 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1892 }
1893 break;
1894 case OB_LIGHTPROBE:
1895 if (dupflag & USER_DUP_LIGHTPROBE) {
1896 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1897 }
1898 break;
1899 case OB_SPEAKER:
1900 if (dupflag != 0) {
1901 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1902 }
1903 break;
1904 case OB_GPENCIL:
1905 if (dupflag & USER_DUP_GPENCIL) {
1906 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1907 }
1908 break;
1909 case OB_HAIR:
1910 if (dupflag & USER_DUP_HAIR) {
1911 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1912 }
1913 break;
1914 case OB_POINTCLOUD:
1915 if (dupflag & USER_DUP_POINTCLOUD) {
1916 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1917 }
1918 break;
1919 case OB_VOLUME:
1920 if (dupflag & USER_DUP_VOLUME) {
1921 id_new = BKE_id_copy_for_duplicate(bmain, id_old, dupflag);
1922 }
1923 break;
1924 }
1925
1926 /* If obdata has been copied, we may also have to duplicate the materials assigned to it. */
1927 if (need_to_duplicate_obdata && !ELEM(id_new, NULL, id_old)) {
1928 if (dupflag & USER_DUP_MAT) {
1929 matarar = BKE_object_material_array_p(obn);
1930 if (matarar) {
1931 for (int i = 0; i < obn->totcol; i++) {
1932 BKE_id_copy_for_duplicate(bmain, (ID *)(*matarar)[i], dupflag);
1933 }
1934 }
1935 }
1936 }
1937
1938 if (!is_subprocess) {
1939 /* This code will follow into all ID links using an ID tagged with LIB_TAG_NEW.*/
1940 BKE_libblock_relink_to_newid(&obn->id);
1941
1942 #ifndef NDEBUG
1943 /* Call to `BKE_libblock_relink_to_newid` above is supposed to have cleared all those flags. */
1944 ID *id_iter;
1945 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
1946 BLI_assert((id_iter->tag & LIB_TAG_NEW) == 0);
1947 }
1948 FOREACH_MAIN_ID_END;
1949 #endif
1950
1951 /* Cleanup. */
1952 BKE_main_id_tag_all(bmain, LIB_TAG_NEW, false);
1953 BKE_main_id_clear_newpoins(bmain);
1954 }
1955
1956 if (obn->type == OB_ARMATURE) {
1957 DEG_id_tag_update(&obn->id, ID_RECALC_GEOMETRY);
1958 if (obn->pose) {
1959 BKE_pose_tag_recalc(bmain, obn->pose);
1960 }
1961 // BKE_pose_rebuild(bmain, obn, obn->data, true);
1962 }
1963
1964 if (obn->data != NULL) {
1965 DEG_id_tag_update_ex(bmain, (ID *)obn->data, ID_RECALC_EDITORS);
1966 }
1967
1968 return obn;
1969 }
1970
1971 /**
1972 * Returns true if the Object is from an external blend file (libdata).
1973 */
BKE_object_is_libdata(const Object * ob)1974 bool BKE_object_is_libdata(const Object *ob)
1975 {
1976 return (ob && ID_IS_LINKED(ob));
1977 }
1978
1979 /**
1980 * Returns true if the Object data is from an external blend file (libdata).
1981 */
BKE_object_obdata_is_libdata(const Object * ob)1982 bool BKE_object_obdata_is_libdata(const Object *ob)
1983 {
1984 /* Linked objects with local obdata are forbidden! */
1985 BLI_assert(!ob || !ob->data || (ID_IS_LINKED(ob) ? ID_IS_LINKED(ob->data) : true));
1986 return (ob && ob->data && ID_IS_LINKED(ob->data));
1987 }
1988
1989 /* -------------------------------------------------------------------- */
1990 /** \name Object Proxy API
1991 * \{ */
1992
1993 /* when you make proxy, ensure the exposed layers are extern */
armature_set_id_extern(Object * ob)1994 static void armature_set_id_extern(Object *ob)
1995 {
1996 bArmature *arm = ob->data;
1997 bPoseChannel *pchan;
1998 unsigned int lay = arm->layer_protected;
1999
2000 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2001 if (!(pchan->bone->layer & lay)) {
2002 id_lib_extern((ID *)pchan->custom);
2003 }
2004 }
2005 }
2006
BKE_object_copy_proxy_drivers(Object * ob,Object * target)2007 void BKE_object_copy_proxy_drivers(Object *ob, Object *target)
2008 {
2009 if ((target->adt) && (target->adt->drivers.first)) {
2010 FCurve *fcu;
2011
2012 /* add new animdata block */
2013 if (!ob->adt) {
2014 ob->adt = BKE_animdata_add_id(&ob->id);
2015 }
2016
2017 /* make a copy of all the drivers (for now), then correct any links that need fixing */
2018 BKE_fcurves_free(&ob->adt->drivers);
2019 BKE_fcurves_copy(&ob->adt->drivers, &target->adt->drivers);
2020
2021 for (fcu = ob->adt->drivers.first; fcu; fcu = fcu->next) {
2022 ChannelDriver *driver = fcu->driver;
2023 DriverVar *dvar;
2024
2025 for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
2026 /* all drivers */
2027 DRIVER_TARGETS_LOOPER_BEGIN (dvar) {
2028 if (dtar->id) {
2029 if ((Object *)dtar->id == target) {
2030 dtar->id = (ID *)ob;
2031 }
2032 else {
2033 /* only on local objects because this causes indirect links
2034 * 'a -> b -> c', blend to point directly to a.blend
2035 * when a.blend has a proxy that's linked into c.blend */
2036 if (!ID_IS_LINKED(ob)) {
2037 id_lib_extern((ID *)dtar->id);
2038 }
2039 }
2040 }
2041 }
2042 DRIVER_TARGETS_LOOPER_END;
2043 }
2044 }
2045 }
2046 }
2047
2048 /**
2049 * Proxy rule:
2050 * - lib_object->proxy_from == the one we borrow from, set temporally while object_update.
2051 * - local_object->proxy == pointer to library object, saved in files and read.
2052 * - local_object->proxy_group == pointer to collection dupli-object, saved in files and read.
2053 */
BKE_object_make_proxy(Main * bmain,Object * ob,Object * target,Object * cob)2054 void BKE_object_make_proxy(Main *bmain, Object *ob, Object *target, Object *cob)
2055 {
2056 /* paranoia checks */
2057 if (ID_IS_LINKED(ob) || !ID_IS_LINKED(target)) {
2058 CLOG_ERROR(&LOG, "cannot make proxy");
2059 return;
2060 }
2061
2062 ob->proxy = target;
2063 id_us_plus(&target->id);
2064 ob->proxy_group = cob;
2065
2066 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
2067 DEG_id_tag_update(&target->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
2068
2069 /* copy transform
2070 * - cob means this proxy comes from a collection, just apply the matrix
2071 * so the object wont move from its dupli-transform.
2072 *
2073 * - no cob means this is being made from a linked object,
2074 * this is closer to making a copy of the object - in-place. */
2075 if (cob) {
2076 ob->rotmode = target->rotmode;
2077 mul_m4_m4m4(ob->obmat, cob->obmat, target->obmat);
2078 if (cob->instance_collection) { /* should always be true */
2079 float tvec[3];
2080 mul_v3_mat3_m4v3(tvec, ob->obmat, cob->instance_collection->instance_offset);
2081 sub_v3_v3(ob->obmat[3], tvec);
2082 }
2083 BKE_object_apply_mat4(ob, ob->obmat, false, true);
2084 }
2085 else {
2086 BKE_object_transform_copy(ob, target);
2087 ob->parent = target->parent; /* libdata */
2088 copy_m4_m4(ob->parentinv, target->parentinv);
2089 }
2090
2091 /* copy animdata stuff - drivers only for now... */
2092 BKE_object_copy_proxy_drivers(ob, target);
2093
2094 /* skip constraints? */
2095 /* FIXME: this is considered by many as a bug */
2096
2097 /* set object type and link to data */
2098 ob->type = target->type;
2099 ob->data = target->data;
2100 id_us_plus((ID *)ob->data); /* ensures lib data becomes LIB_TAG_EXTERN */
2101
2102 /* copy vertex groups */
2103 BKE_defgroup_copy_list(&ob->defbase, &target->defbase);
2104
2105 /* copy material and index information */
2106 ob->actcol = ob->totcol = 0;
2107 if (ob->mat) {
2108 MEM_freeN(ob->mat);
2109 }
2110 if (ob->matbits) {
2111 MEM_freeN(ob->matbits);
2112 }
2113 ob->mat = NULL;
2114 ob->matbits = NULL;
2115 if ((target->totcol) && (target->mat) && OB_TYPE_SUPPORT_MATERIAL(ob->type)) {
2116 int i;
2117
2118 ob->actcol = target->actcol;
2119 ob->totcol = target->totcol;
2120
2121 ob->mat = MEM_dupallocN(target->mat);
2122 ob->matbits = MEM_dupallocN(target->matbits);
2123 for (i = 0; i < target->totcol; i++) {
2124 /* don't need to run BKE_object_materials_test
2125 * since we know this object is new and not used elsewhere */
2126 id_us_plus((ID *)ob->mat[i]);
2127 }
2128 }
2129
2130 /* type conversions */
2131 if (target->type == OB_ARMATURE) {
2132 copy_object_pose(ob, target, 0); /* data copy, object pointers in constraints */
2133 BKE_pose_rest(ob->pose, false); /* clear all transforms in channels */
2134 BKE_pose_rebuild(bmain, ob, ob->data, true); /* set all internal links */
2135
2136 armature_set_id_extern(ob);
2137 }
2138 else if (target->type == OB_EMPTY) {
2139 ob->empty_drawtype = target->empty_drawtype;
2140 ob->empty_drawsize = target->empty_drawsize;
2141 }
2142
2143 /* copy IDProperties */
2144 if (ob->id.properties) {
2145 IDP_FreeProperty(ob->id.properties);
2146 ob->id.properties = NULL;
2147 }
2148 if (target->id.properties) {
2149 ob->id.properties = IDP_CopyProperty(target->id.properties);
2150 }
2151
2152 /* copy drawtype info */
2153 ob->dt = target->dt;
2154 }
2155
2156 /**
2157 * Use with newly created objects to set their size
2158 * (used to apply scene-scale).
2159 */
BKE_object_obdata_size_init(struct Object * ob,const float size)2160 void BKE_object_obdata_size_init(struct Object *ob, const float size)
2161 {
2162 /* apply radius as a scale to types that support it */
2163 switch (ob->type) {
2164 case OB_EMPTY: {
2165 ob->empty_drawsize *= size;
2166 break;
2167 }
2168 case OB_FONT: {
2169 Curve *cu = ob->data;
2170 cu->fsize *= size;
2171 break;
2172 }
2173 case OB_CAMERA: {
2174 Camera *cam = ob->data;
2175 cam->drawsize *= size;
2176 break;
2177 }
2178 case OB_LAMP: {
2179 Light *lamp = ob->data;
2180 lamp->dist *= size;
2181 lamp->area_size *= size;
2182 lamp->area_sizey *= size;
2183 lamp->area_sizez *= size;
2184 break;
2185 }
2186 /* Only lattice (not mesh, curve, mball...),
2187 * because its got data when newly added */
2188 case OB_LATTICE: {
2189 struct Lattice *lt = ob->data;
2190 float mat[4][4];
2191
2192 unit_m4(mat);
2193 scale_m4_fl(mat, size);
2194
2195 BKE_lattice_transform(lt, (float(*)[4])mat, false);
2196 break;
2197 }
2198 }
2199 }
2200
2201 /** \} */
2202
2203 /* -------------------------------------------------------------------- */
2204 /** \name Object Matrix Get/Set API
2205 * \{ */
2206
BKE_object_scale_to_mat3(Object * ob,float mat[3][3])2207 void BKE_object_scale_to_mat3(Object *ob, float mat[3][3])
2208 {
2209 float vec[3];
2210 mul_v3_v3v3(vec, ob->scale, ob->dscale);
2211 size_to_mat3(mat, vec);
2212 }
2213
BKE_object_rot_to_mat3(const Object * ob,float mat[3][3],bool use_drot)2214 void BKE_object_rot_to_mat3(const Object *ob, float mat[3][3], bool use_drot)
2215 {
2216 float rmat[3][3], dmat[3][3];
2217
2218 /* 'dmat' is the delta-rotation matrix, which will get (pre)multiplied
2219 * with the rotation matrix to yield the appropriate rotation
2220 */
2221
2222 /* rotations may either be quats, eulers (with various rotation orders), or axis-angle */
2223 if (ob->rotmode > 0) {
2224 /* Euler rotations
2225 * (will cause gimble lock, but this can be alleviated a bit with rotation orders). */
2226 eulO_to_mat3(rmat, ob->rot, ob->rotmode);
2227 eulO_to_mat3(dmat, ob->drot, ob->rotmode);
2228 }
2229 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
2230 /* axis-angle - not really that great for 3D-changing orientations */
2231 axis_angle_to_mat3(rmat, ob->rotAxis, ob->rotAngle);
2232 axis_angle_to_mat3(dmat, ob->drotAxis, ob->drotAngle);
2233 }
2234 else {
2235 /* quats are normalized before use to eliminate scaling issues */
2236 float tquat[4];
2237
2238 normalize_qt_qt(tquat, ob->quat);
2239 quat_to_mat3(rmat, tquat);
2240
2241 normalize_qt_qt(tquat, ob->dquat);
2242 quat_to_mat3(dmat, tquat);
2243 }
2244
2245 /* combine these rotations */
2246 if (use_drot) {
2247 mul_m3_m3m3(mat, dmat, rmat);
2248 }
2249 else {
2250 copy_m3_m3(mat, rmat);
2251 }
2252 }
2253
BKE_object_mat3_to_rot(Object * ob,float mat[3][3],bool use_compat)2254 void BKE_object_mat3_to_rot(Object *ob, float mat[3][3], bool use_compat)
2255 {
2256 BLI_ASSERT_UNIT_M3(mat);
2257
2258 switch (ob->rotmode) {
2259 case ROT_MODE_QUAT: {
2260 float dquat[4];
2261 mat3_normalized_to_quat(ob->quat, mat);
2262 normalize_qt_qt(dquat, ob->dquat);
2263 invert_qt_normalized(dquat);
2264 mul_qt_qtqt(ob->quat, dquat, ob->quat);
2265 break;
2266 }
2267 case ROT_MODE_AXISANGLE: {
2268 float quat[4];
2269 float dquat[4];
2270
2271 /* without drot we could apply 'mat' directly */
2272 mat3_normalized_to_quat(quat, mat);
2273 axis_angle_to_quat(dquat, ob->drotAxis, ob->drotAngle);
2274 invert_qt_normalized(dquat);
2275 mul_qt_qtqt(quat, dquat, quat);
2276 quat_to_axis_angle(ob->rotAxis, &ob->rotAngle, quat);
2277 break;
2278 }
2279 default: /* euler */
2280 {
2281 float quat[4];
2282 float dquat[4];
2283
2284 /* without drot we could apply 'mat' directly */
2285 mat3_normalized_to_quat(quat, mat);
2286 eulO_to_quat(dquat, ob->drot, ob->rotmode);
2287 invert_qt_normalized(dquat);
2288 mul_qt_qtqt(quat, dquat, quat);
2289 /* end drot correction */
2290
2291 if (use_compat) {
2292 quat_to_compatible_eulO(ob->rot, ob->rot, ob->rotmode, quat);
2293 }
2294 else {
2295 quat_to_eulO(ob->rot, ob->rotmode, quat);
2296 }
2297 break;
2298 }
2299 }
2300 }
2301
BKE_object_tfm_protected_backup(const Object * ob,ObjectTfmProtectedChannels * obtfm)2302 void BKE_object_tfm_protected_backup(const Object *ob, ObjectTfmProtectedChannels *obtfm)
2303 {
2304
2305 #define TFMCPY(_v) (obtfm->_v = ob->_v)
2306 #define TFMCPY3D(_v) copy_v3_v3(obtfm->_v, ob->_v)
2307 #define TFMCPY4D(_v) copy_v4_v4(obtfm->_v, ob->_v)
2308
2309 TFMCPY3D(loc);
2310 TFMCPY3D(dloc);
2311 TFMCPY3D(scale);
2312 TFMCPY3D(dscale);
2313 TFMCPY3D(rot);
2314 TFMCPY3D(drot);
2315 TFMCPY4D(quat);
2316 TFMCPY4D(dquat);
2317 TFMCPY3D(rotAxis);
2318 TFMCPY3D(drotAxis);
2319 TFMCPY(rotAngle);
2320 TFMCPY(drotAngle);
2321
2322 #undef TFMCPY
2323 #undef TFMCPY3D
2324 #undef TFMCPY4D
2325 }
2326
BKE_object_tfm_protected_restore(Object * ob,const ObjectTfmProtectedChannels * obtfm,const short protectflag)2327 void BKE_object_tfm_protected_restore(Object *ob,
2328 const ObjectTfmProtectedChannels *obtfm,
2329 const short protectflag)
2330 {
2331 unsigned int i;
2332
2333 for (i = 0; i < 3; i++) {
2334 if (protectflag & (OB_LOCK_LOCX << i)) {
2335 ob->loc[i] = obtfm->loc[i];
2336 ob->dloc[i] = obtfm->dloc[i];
2337 }
2338
2339 if (protectflag & (OB_LOCK_SCALEX << i)) {
2340 ob->scale[i] = obtfm->scale[i];
2341 ob->dscale[i] = obtfm->dscale[i];
2342 }
2343
2344 if (protectflag & (OB_LOCK_ROTX << i)) {
2345 ob->rot[i] = obtfm->rot[i];
2346 ob->drot[i] = obtfm->drot[i];
2347
2348 ob->quat[i + 1] = obtfm->quat[i + 1];
2349 ob->dquat[i + 1] = obtfm->dquat[i + 1];
2350
2351 ob->rotAxis[i] = obtfm->rotAxis[i];
2352 ob->drotAxis[i] = obtfm->drotAxis[i];
2353 }
2354 }
2355
2356 if ((protectflag & OB_LOCK_ROT4D) && (protectflag & OB_LOCK_ROTW)) {
2357 ob->quat[0] = obtfm->quat[0];
2358 ob->dquat[0] = obtfm->dquat[0];
2359
2360 ob->rotAngle = obtfm->rotAngle;
2361 ob->drotAngle = obtfm->drotAngle;
2362 }
2363 }
2364
BKE_object_tfm_copy(Object * object_dst,const Object * object_src)2365 void BKE_object_tfm_copy(Object *object_dst, const Object *object_src)
2366 {
2367 #define TFMCPY(_v) (object_dst->_v = object_src->_v)
2368 #define TFMCPY3D(_v) copy_v3_v3(object_dst->_v, object_src->_v)
2369 #define TFMCPY4D(_v) copy_v4_v4(object_dst->_v, object_src->_v)
2370
2371 TFMCPY3D(loc);
2372 TFMCPY3D(dloc);
2373 TFMCPY3D(scale);
2374 TFMCPY3D(dscale);
2375 TFMCPY3D(rot);
2376 TFMCPY3D(drot);
2377 TFMCPY4D(quat);
2378 TFMCPY4D(dquat);
2379 TFMCPY3D(rotAxis);
2380 TFMCPY3D(drotAxis);
2381 TFMCPY(rotAngle);
2382 TFMCPY(drotAngle);
2383
2384 #undef TFMCPY
2385 #undef TFMCPY3D
2386 #undef TFMCPY4D
2387 }
2388
BKE_object_to_mat3(Object * ob,float r_mat[3][3])2389 void BKE_object_to_mat3(Object *ob, float r_mat[3][3]) /* no parent */
2390 {
2391 float smat[3][3];
2392 float rmat[3][3];
2393 /*float q1[4];*/
2394
2395 /* scale */
2396 BKE_object_scale_to_mat3(ob, smat);
2397
2398 /* rot */
2399 BKE_object_rot_to_mat3(ob, rmat, true);
2400 mul_m3_m3m3(r_mat, rmat, smat);
2401 }
2402
BKE_object_to_mat4(Object * ob,float r_mat[4][4])2403 void BKE_object_to_mat4(Object *ob, float r_mat[4][4])
2404 {
2405 float tmat[3][3];
2406
2407 BKE_object_to_mat3(ob, tmat);
2408
2409 copy_m4_m3(r_mat, tmat);
2410
2411 add_v3_v3v3(r_mat[3], ob->loc, ob->dloc);
2412 }
2413
BKE_object_matrix_local_get(struct Object * ob,float r_mat[4][4])2414 void BKE_object_matrix_local_get(struct Object *ob, float r_mat[4][4])
2415 {
2416 if (ob->parent) {
2417 float par_imat[4][4];
2418
2419 BKE_object_get_parent_matrix(ob, ob->parent, par_imat);
2420 invert_m4(par_imat);
2421 mul_m4_m4m4(r_mat, par_imat, ob->obmat);
2422 }
2423 else {
2424 copy_m4_m4(r_mat, ob->obmat);
2425 }
2426 }
2427
2428 /**
2429 * \return success if \a mat is set.
2430 */
ob_parcurve(Object * ob,Object * par,float r_mat[4][4])2431 static bool ob_parcurve(Object *ob, Object *par, float r_mat[4][4])
2432 {
2433 Curve *cu = par->data;
2434 float vec[4], dir[3], quat[4], radius, ctime;
2435
2436 /* NOTE: Curve cache is supposed to be evaluated here already, however there
2437 * are cases where we can not guarantee that. This includes, for example,
2438 * dependency cycles. We can't correct anything from here, since that would
2439 * cause a threading conflicts.
2440 *
2441 * TODO(sergey): Some of the legit looking cases like T56619 need to be
2442 * looked into, and maybe curve cache (and other dependencies) are to be
2443 * evaluated prior to conversion. */
2444 if (par->runtime.curve_cache == NULL) {
2445 return false;
2446 }
2447 if (par->runtime.curve_cache->path == NULL) {
2448 return false;
2449 }
2450
2451 /* ctime is now a proper var setting of Curve which gets set by Animato like any other var
2452 * that's animated, but this will only work if it actually is animated.
2453 *
2454 * We divide the curve-time calculated in the previous step by the length of the path,
2455 * to get a time factor, which then gets clamped to lie within 0.0 - 1.0 range.
2456 */
2457 if (cu->pathlen) {
2458 ctime = cu->ctime / cu->pathlen;
2459 }
2460 else {
2461 ctime = cu->ctime;
2462 }
2463 CLAMP(ctime, 0.0f, 1.0f);
2464
2465 unit_m4(r_mat);
2466
2467 /* vec: 4 items! */
2468 if (where_on_path(par, ctime, vec, dir, (cu->flag & CU_FOLLOW) ? quat : NULL, &radius, NULL)) {
2469 if (cu->flag & CU_FOLLOW) {
2470 quat_apply_track(quat, ob->trackflag, ob->upflag);
2471 normalize_qt(quat);
2472 quat_to_mat4(r_mat, quat);
2473 }
2474 if (cu->flag & CU_PATH_RADIUS) {
2475 float tmat[4][4], rmat[4][4];
2476 scale_m4_fl(tmat, radius);
2477 mul_m4_m4m4(rmat, tmat, r_mat);
2478 copy_m4_m4(r_mat, rmat);
2479 }
2480 copy_v3_v3(r_mat[3], vec);
2481 }
2482
2483 return true;
2484 }
2485
ob_parbone(Object * ob,Object * par,float r_mat[4][4])2486 static void ob_parbone(Object *ob, Object *par, float r_mat[4][4])
2487 {
2488 bPoseChannel *pchan;
2489 float vec[3];
2490
2491 if (par->type != OB_ARMATURE) {
2492 unit_m4(r_mat);
2493 return;
2494 }
2495
2496 /* Make sure the bone is still valid */
2497 pchan = BKE_pose_channel_find_name(par->pose, ob->parsubstr);
2498 if (!pchan || !pchan->bone) {
2499 CLOG_ERROR(
2500 &LOG, "Object %s with Bone parent: bone %s doesn't exist", ob->id.name + 2, ob->parsubstr);
2501 unit_m4(r_mat);
2502 return;
2503 }
2504
2505 /* get bone transform */
2506 if (pchan->bone->flag & BONE_RELATIVE_PARENTING) {
2507 /* the new option uses the root - expected behavior, but differs from old... */
2508 /* XXX check on version patching? */
2509 copy_m4_m4(r_mat, pchan->chan_mat);
2510 }
2511 else {
2512 copy_m4_m4(r_mat, pchan->pose_mat);
2513
2514 /* but for backwards compatibility, the child has to move to the tail */
2515 copy_v3_v3(vec, r_mat[1]);
2516 mul_v3_fl(vec, pchan->bone->length);
2517 add_v3_v3(r_mat[3], vec);
2518 }
2519 }
2520
give_parvert(Object * par,int nr,float vec[3])2521 static void give_parvert(Object *par, int nr, float vec[3])
2522 {
2523 zero_v3(vec);
2524
2525 if (par->type == OB_MESH) {
2526 Mesh *me = par->data;
2527 BMEditMesh *em = me->edit_mesh;
2528 Mesh *me_eval = (em) ? em->mesh_eval_final : BKE_object_get_evaluated_mesh(par);
2529
2530 if (me_eval) {
2531 int count = 0;
2532 int numVerts = me_eval->totvert;
2533
2534 if (em && me_eval->runtime.wrapper_type == ME_WRAPPER_TYPE_BMESH) {
2535 numVerts = em->bm->totvert;
2536 if (em->bm->elem_table_dirty & BM_VERT) {
2537 #ifdef VPARENT_THREADING_HACK
2538 BLI_mutex_lock(&vparent_lock);
2539 if (em->bm->elem_table_dirty & BM_VERT) {
2540 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
2541 }
2542 BLI_mutex_unlock(&vparent_lock);
2543 #else
2544 BLI_assert(!"Not safe for threading");
2545 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
2546 #endif
2547 }
2548 if (nr < numVerts) {
2549 if (me_eval && me_eval->runtime.edit_data && me_eval->runtime.edit_data->vertexCos) {
2550 add_v3_v3(vec, me_eval->runtime.edit_data->vertexCos[nr]);
2551 }
2552 else {
2553 const BMVert *v = BM_vert_at_index(em->bm, nr);
2554 add_v3_v3(vec, v->co);
2555 }
2556 count++;
2557 }
2558 }
2559 else if (CustomData_has_layer(&me_eval->vdata, CD_ORIGINDEX)) {
2560 const int *index = CustomData_get_layer(&me_eval->vdata, CD_ORIGINDEX);
2561 /* Get the average of all verts with (original index == nr). */
2562 for (int i = 0; i < numVerts; i++) {
2563 if (index[i] == nr) {
2564 add_v3_v3(vec, me_eval->mvert[i].co);
2565 count++;
2566 }
2567 }
2568 }
2569 else {
2570 if (nr < numVerts) {
2571 add_v3_v3(vec, me_eval->mvert[nr].co);
2572 count++;
2573 }
2574 }
2575
2576 if (count == 0) {
2577 /* keep as 0, 0, 0 */
2578 }
2579 else if (count > 0) {
2580 mul_v3_fl(vec, 1.0f / count);
2581 }
2582 else {
2583 /* use first index if its out of range */
2584 if (me_eval->totvert) {
2585 copy_v3_v3(vec, me_eval->mvert[0].co);
2586 }
2587 }
2588 }
2589 else {
2590 CLOG_ERROR(&LOG,
2591 "Evaluated mesh is needed to solve parenting, "
2592 "object position can be wrong now");
2593 }
2594 }
2595 else if (ELEM(par->type, OB_CURVE, OB_SURF)) {
2596 ListBase *nurb;
2597
2598 /* Unless there's some weird depsgraph failure the cache should exist. */
2599 BLI_assert(par->runtime.curve_cache != NULL);
2600
2601 if (par->runtime.curve_cache->deformed_nurbs.first != NULL) {
2602 nurb = &par->runtime.curve_cache->deformed_nurbs;
2603 }
2604 else {
2605 Curve *cu = par->data;
2606 nurb = BKE_curve_nurbs_get(cu);
2607 }
2608
2609 BKE_nurbList_index_get_co(nurb, nr, vec);
2610 }
2611 else if (par->type == OB_LATTICE) {
2612 Lattice *latt = par->data;
2613 DispList *dl = par->runtime.curve_cache ?
2614 BKE_displist_find(&par->runtime.curve_cache->disp, DL_VERTS) :
2615 NULL;
2616 float(*co)[3] = dl ? (float(*)[3])dl->verts : NULL;
2617 int tot;
2618
2619 if (latt->editlatt) {
2620 latt = latt->editlatt->latt;
2621 }
2622
2623 tot = latt->pntsu * latt->pntsv * latt->pntsw;
2624
2625 /* ensure dl is correct size */
2626 BLI_assert(dl == NULL || dl->nr == tot);
2627
2628 if (nr < tot) {
2629 if (co) {
2630 copy_v3_v3(vec, co[nr]);
2631 }
2632 else {
2633 copy_v3_v3(vec, latt->def[nr].vec);
2634 }
2635 }
2636 }
2637 }
2638
ob_parvert3(Object * ob,Object * par,float r_mat[4][4])2639 static void ob_parvert3(Object *ob, Object *par, float r_mat[4][4])
2640 {
2641
2642 /* in local ob space */
2643 if (OB_TYPE_SUPPORT_PARVERT(par->type)) {
2644 float cmat[3][3], v1[3], v2[3], v3[3], q[4];
2645
2646 give_parvert(par, ob->par1, v1);
2647 give_parvert(par, ob->par2, v2);
2648 give_parvert(par, ob->par3, v3);
2649
2650 tri_to_quat(q, v1, v2, v3);
2651 quat_to_mat3(cmat, q);
2652 copy_m4_m3(r_mat, cmat);
2653
2654 mid_v3_v3v3v3(r_mat[3], v1, v2, v3);
2655 }
2656 else {
2657 unit_m4(r_mat);
2658 }
2659 }
2660
BKE_object_get_parent_matrix(Object * ob,Object * par,float r_parentmat[4][4])2661 void BKE_object_get_parent_matrix(Object *ob, Object *par, float r_parentmat[4][4])
2662 {
2663 float tmat[4][4];
2664 float vec[3];
2665 bool ok;
2666
2667 switch (ob->partype & PARTYPE) {
2668 case PAROBJECT:
2669 ok = 0;
2670 if (par->type == OB_CURVE) {
2671 if ((((Curve *)par->data)->flag & CU_PATH) && (ob_parcurve(ob, par, tmat))) {
2672 ok = 1;
2673 }
2674 }
2675
2676 if (ok) {
2677 mul_m4_m4m4(r_parentmat, par->obmat, tmat);
2678 }
2679 else {
2680 copy_m4_m4(r_parentmat, par->obmat);
2681 }
2682
2683 break;
2684 case PARBONE:
2685 ob_parbone(ob, par, tmat);
2686 mul_m4_m4m4(r_parentmat, par->obmat, tmat);
2687 break;
2688
2689 case PARVERT1:
2690 unit_m4(r_parentmat);
2691 give_parvert(par, ob->par1, vec);
2692 mul_v3_m4v3(r_parentmat[3], par->obmat, vec);
2693 break;
2694 case PARVERT3:
2695 ob_parvert3(ob, par, tmat);
2696
2697 mul_m4_m4m4(r_parentmat, par->obmat, tmat);
2698 break;
2699
2700 case PARSKEL:
2701 copy_m4_m4(r_parentmat, par->obmat);
2702 break;
2703 }
2704 }
2705
2706 /** \} */
2707
2708 /* -------------------------------------------------------------------- */
2709 /** \name Object Matrix Evaluation API
2710 * \{ */
2711
2712 /**
2713 * \param r_originmat: Optional matrix that stores the space the object is in
2714 * (without its own matrix applied)
2715 */
solve_parenting(Object * ob,Object * par,const bool set_origin,float r_obmat[4][4],float r_originmat[3][3])2716 static void solve_parenting(
2717 Object *ob, Object *par, const bool set_origin, float r_obmat[4][4], float r_originmat[3][3])
2718 {
2719 float totmat[4][4];
2720 float tmat[4][4];
2721 float locmat[4][4];
2722
2723 BKE_object_to_mat4(ob, locmat);
2724
2725 BKE_object_get_parent_matrix(ob, par, totmat);
2726
2727 /* total */
2728 mul_m4_m4m4(tmat, totmat, ob->parentinv);
2729 mul_m4_m4m4(r_obmat, tmat, locmat);
2730
2731 if (r_originmat) {
2732 /* usable originmat */
2733 copy_m3_m4(r_originmat, tmat);
2734 }
2735
2736 /* origin, for help line */
2737 if (set_origin) {
2738 if ((ob->partype & PARTYPE) == PARSKEL) {
2739 copy_v3_v3(ob->runtime.parent_display_origin, par->obmat[3]);
2740 }
2741 else {
2742 copy_v3_v3(ob->runtime.parent_display_origin, totmat[3]);
2743 }
2744 }
2745 }
2746
2747 /**
2748 * \note scene is the active scene while actual_scene is the scene the object resides in.
2749 */
object_where_is_calc_ex(Depsgraph * depsgraph,Scene * scene,Object * ob,float ctime,RigidBodyWorld * rbw,float r_originmat[3][3])2750 static void object_where_is_calc_ex(Depsgraph *depsgraph,
2751 Scene *scene,
2752 Object *ob,
2753 float ctime,
2754 RigidBodyWorld *rbw,
2755 float r_originmat[3][3])
2756 {
2757 if (ob->parent) {
2758 Object *par = ob->parent;
2759
2760 /* calculate parent matrix */
2761 solve_parenting(ob, par, true, ob->obmat, r_originmat);
2762 }
2763 else {
2764 BKE_object_to_mat4(ob, ob->obmat);
2765 }
2766
2767 /* try to fall back to the scene rigid body world if none given */
2768 rbw = rbw ? rbw : scene->rigidbody_world;
2769 /* read values pushed into RBO from sim/cache... */
2770 BKE_rigidbody_sync_transforms(rbw, ob, ctime);
2771
2772 /* solve constraints */
2773 if (ob->constraints.first && !(ob->transflag & OB_NO_CONSTRAINTS)) {
2774 bConstraintOb *cob;
2775 cob = BKE_constraints_make_evalob(depsgraph, scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2776 BKE_constraints_solve(depsgraph, &ob->constraints, cob, ctime);
2777 BKE_constraints_clear_evalob(cob);
2778 }
2779
2780 /* set negative scale flag in object */
2781 if (is_negative_m4(ob->obmat)) {
2782 ob->transflag |= OB_NEG_SCALE;
2783 }
2784 else {
2785 ob->transflag &= ~OB_NEG_SCALE;
2786 }
2787 }
2788
BKE_object_where_is_calc_time(Depsgraph * depsgraph,Scene * scene,Object * ob,float ctime)2789 void BKE_object_where_is_calc_time(Depsgraph *depsgraph, Scene *scene, Object *ob, float ctime)
2790 {
2791 /* Execute drivers and animation. */
2792 const bool flush_to_original = DEG_is_active(depsgraph);
2793 const AnimationEvalContext anim_eval_context = BKE_animsys_eval_context_construct(depsgraph,
2794 ctime);
2795 BKE_animsys_evaluate_animdata(
2796 &ob->id, ob->adt, &anim_eval_context, ADT_RECALC_ALL, flush_to_original);
2797 object_where_is_calc_ex(depsgraph, scene, ob, ctime, NULL, NULL);
2798 }
2799
2800 /**
2801 * Calculate object transformation matrix without recalculating dependencies and
2802 * constraints -- assume dependencies are already solved by depsgraph.
2803 * No changes to object and its parent would be done.
2804 * Used for bundles orientation in 3d space relative to parented blender camera.
2805 */
BKE_object_where_is_calc_mat4(Object * ob,float r_obmat[4][4])2806 void BKE_object_where_is_calc_mat4(Object *ob, float r_obmat[4][4])
2807 {
2808 if (ob->parent) {
2809 Object *par = ob->parent;
2810 solve_parenting(ob, par, false, r_obmat, NULL);
2811 }
2812 else {
2813 BKE_object_to_mat4(ob, r_obmat);
2814 }
2815 }
2816
BKE_object_where_is_calc_ex(Depsgraph * depsgraph,Scene * scene,RigidBodyWorld * rbw,Object * ob,float r_originmat[3][3])2817 void BKE_object_where_is_calc_ex(
2818 Depsgraph *depsgraph, Scene *scene, RigidBodyWorld *rbw, Object *ob, float r_originmat[3][3])
2819 {
2820 float ctime = DEG_get_ctime(depsgraph);
2821 object_where_is_calc_ex(depsgraph, scene, ob, ctime, rbw, r_originmat);
2822 }
BKE_object_where_is_calc(Depsgraph * depsgraph,Scene * scene,Object * ob)2823 void BKE_object_where_is_calc(Depsgraph *depsgraph, Scene *scene, Object *ob)
2824 {
2825 float ctime = DEG_get_ctime(depsgraph);
2826 object_where_is_calc_ex(depsgraph, scene, ob, ctime, NULL, NULL);
2827 }
2828
2829 /**
2830 * For calculation of the inverse parent transform, only used for editor.
2831 *
2832 * It assumes the object parent is already in the depsgraph.
2833 * Otherwise, after changing ob->parent you need to call:
2834 * - #DEG_relations_tag_update(bmain);
2835 * - #BKE_scene_graph_update_tagged(depsgraph, bmain);
2836 */
BKE_object_workob_calc_parent(Depsgraph * depsgraph,Scene * scene,Object * ob,Object * workob)2837 void BKE_object_workob_calc_parent(Depsgraph *depsgraph, Scene *scene, Object *ob, Object *workob)
2838 {
2839 BKE_object_workob_clear(workob);
2840
2841 unit_m4(workob->obmat);
2842 unit_m4(workob->parentinv);
2843 unit_m4(workob->constinv);
2844
2845 /* Since this is used while calculating parenting,
2846 * at this moment ob_eval->parent is still NULL. */
2847 workob->parent = DEG_get_evaluated_object(depsgraph, ob->parent);
2848
2849 workob->trackflag = ob->trackflag;
2850 workob->upflag = ob->upflag;
2851
2852 workob->partype = ob->partype;
2853 workob->par1 = ob->par1;
2854 workob->par2 = ob->par2;
2855 workob->par3 = ob->par3;
2856
2857 workob->constraints = ob->constraints;
2858
2859 BLI_strncpy(workob->parsubstr, ob->parsubstr, sizeof(workob->parsubstr));
2860
2861 BKE_object_where_is_calc(depsgraph, scene, workob);
2862 }
2863
2864 /**
2865 * Applies the global transformation \a mat to the \a ob using a relative parent space if
2866 * supplied.
2867 *
2868 * \param mat: the global transformation mat that the object should be set object to.
2869 * \param parent: the parent space in which this object will be set relative to
2870 * (should probably always be parent_eval).
2871 * \param use_compat: true to ensure that rotations are set using the
2872 * min difference between the old and new orientation.
2873 */
BKE_object_apply_mat4_ex(Object * ob,const float mat[4][4],Object * parent,const float parentinv[4][4],const bool use_compat)2874 void BKE_object_apply_mat4_ex(Object *ob,
2875 const float mat[4][4],
2876 Object *parent,
2877 const float parentinv[4][4],
2878 const bool use_compat)
2879 {
2880 /* see BKE_pchan_apply_mat4() for the equivalent 'pchan' function */
2881
2882 float rot[3][3];
2883
2884 if (parent != NULL) {
2885 float rmat[4][4], diff_mat[4][4], imat[4][4], parent_mat[4][4];
2886
2887 BKE_object_get_parent_matrix(ob, parent, parent_mat);
2888
2889 mul_m4_m4m4(diff_mat, parent_mat, parentinv);
2890 invert_m4_m4(imat, diff_mat);
2891 mul_m4_m4m4(rmat, imat, mat); /* get the parent relative matrix */
2892
2893 /* same as below, use rmat rather than mat */
2894 mat4_to_loc_rot_size(ob->loc, rot, ob->scale, rmat);
2895 }
2896 else {
2897 mat4_to_loc_rot_size(ob->loc, rot, ob->scale, mat);
2898 }
2899
2900 BKE_object_mat3_to_rot(ob, rot, use_compat);
2901
2902 sub_v3_v3(ob->loc, ob->dloc);
2903
2904 if (ob->dscale[0] != 0.0f) {
2905 ob->scale[0] /= ob->dscale[0];
2906 }
2907 if (ob->dscale[1] != 0.0f) {
2908 ob->scale[1] /= ob->dscale[1];
2909 }
2910 if (ob->dscale[2] != 0.0f) {
2911 ob->scale[2] /= ob->dscale[2];
2912 }
2913
2914 /* BKE_object_mat3_to_rot handles delta rotations */
2915 }
2916
2917 /**
2918 * XXX: should be removed after COW operators port to use BKE_object_apply_mat4_ex directly.
2919 */
BKE_object_apply_mat4(Object * ob,const float mat[4][4],const bool use_compat,const bool use_parent)2920 void BKE_object_apply_mat4(Object *ob,
2921 const float mat[4][4],
2922 const bool use_compat,
2923 const bool use_parent)
2924 {
2925 BKE_object_apply_mat4_ex(ob, mat, use_parent ? ob->parent : NULL, ob->parentinv, use_compat);
2926 }
2927
2928 /** \} */
2929
2930 /* -------------------------------------------------------------------- */
2931 /** \name Object Bounding Box API
2932 * \{ */
2933
BKE_boundbox_alloc_unit(void)2934 BoundBox *BKE_boundbox_alloc_unit(void)
2935 {
2936 BoundBox *bb;
2937 const float min[3] = {-1.0f, -1.0f, -1.0f}, max[3] = {1.0f, 1.0f, 1.0f};
2938
2939 bb = MEM_callocN(sizeof(BoundBox), "OB-BoundBox");
2940 BKE_boundbox_init_from_minmax(bb, min, max);
2941
2942 return bb;
2943 }
2944
BKE_boundbox_init_from_minmax(BoundBox * bb,const float min[3],const float max[3])2945 void BKE_boundbox_init_from_minmax(BoundBox *bb, const float min[3], const float max[3])
2946 {
2947 bb->vec[0][0] = bb->vec[1][0] = bb->vec[2][0] = bb->vec[3][0] = min[0];
2948 bb->vec[4][0] = bb->vec[5][0] = bb->vec[6][0] = bb->vec[7][0] = max[0];
2949
2950 bb->vec[0][1] = bb->vec[1][1] = bb->vec[4][1] = bb->vec[5][1] = min[1];
2951 bb->vec[2][1] = bb->vec[3][1] = bb->vec[6][1] = bb->vec[7][1] = max[1];
2952
2953 bb->vec[0][2] = bb->vec[3][2] = bb->vec[4][2] = bb->vec[7][2] = min[2];
2954 bb->vec[1][2] = bb->vec[2][2] = bb->vec[5][2] = bb->vec[6][2] = max[2];
2955 }
2956
BKE_boundbox_calc_center_aabb(const BoundBox * bb,float r_cent[3])2957 void BKE_boundbox_calc_center_aabb(const BoundBox *bb, float r_cent[3])
2958 {
2959 r_cent[0] = 0.5f * (bb->vec[0][0] + bb->vec[4][0]);
2960 r_cent[1] = 0.5f * (bb->vec[0][1] + bb->vec[2][1]);
2961 r_cent[2] = 0.5f * (bb->vec[0][2] + bb->vec[1][2]);
2962 }
2963
BKE_boundbox_calc_size_aabb(const BoundBox * bb,float r_size[3])2964 void BKE_boundbox_calc_size_aabb(const BoundBox *bb, float r_size[3])
2965 {
2966 r_size[0] = 0.5f * fabsf(bb->vec[0][0] - bb->vec[4][0]);
2967 r_size[1] = 0.5f * fabsf(bb->vec[0][1] - bb->vec[2][1]);
2968 r_size[2] = 0.5f * fabsf(bb->vec[0][2] - bb->vec[1][2]);
2969 }
2970
BKE_boundbox_minmax(const BoundBox * bb,const float obmat[4][4],float r_min[3],float r_max[3])2971 void BKE_boundbox_minmax(const BoundBox *bb,
2972 const float obmat[4][4],
2973 float r_min[3],
2974 float r_max[3])
2975 {
2976 int i;
2977 for (i = 0; i < 8; i++) {
2978 float vec[3];
2979 mul_v3_m4v3(vec, obmat, bb->vec[i]);
2980 minmax_v3v3_v3(r_min, r_max, vec);
2981 }
2982 }
2983
BKE_object_boundbox_get(Object * ob)2984 BoundBox *BKE_object_boundbox_get(Object *ob)
2985 {
2986 BoundBox *bb = NULL;
2987
2988 switch (ob->type) {
2989 case OB_MESH:
2990 bb = BKE_mesh_boundbox_get(ob);
2991 break;
2992 case OB_CURVE:
2993 case OB_SURF:
2994 case OB_FONT:
2995 bb = BKE_curve_boundbox_get(ob);
2996 break;
2997 case OB_MBALL:
2998 bb = BKE_mball_boundbox_get(ob);
2999 break;
3000 case OB_LATTICE:
3001 bb = BKE_lattice_boundbox_get(ob);
3002 break;
3003 case OB_ARMATURE:
3004 bb = BKE_armature_boundbox_get(ob);
3005 break;
3006 case OB_GPENCIL:
3007 bb = BKE_gpencil_boundbox_get(ob);
3008 break;
3009 case OB_HAIR:
3010 bb = BKE_hair_boundbox_get(ob);
3011 break;
3012 case OB_POINTCLOUD:
3013 bb = BKE_pointcloud_boundbox_get(ob);
3014 break;
3015 case OB_VOLUME:
3016 bb = BKE_volume_boundbox_get(ob);
3017 break;
3018 default:
3019 break;
3020 }
3021 return bb;
3022 }
3023
3024 /**
3025 * Use this to temporally disable/enable bound-box.
3026 */
BKE_object_boundbox_flag(Object * ob,int flag,const bool set)3027 void BKE_object_boundbox_flag(Object *ob, int flag, const bool set)
3028 {
3029 BoundBox *bb = BKE_object_boundbox_get(ob);
3030 if (bb) {
3031 if (set) {
3032 bb->flag |= flag;
3033 }
3034 else {
3035 bb->flag &= ~flag;
3036 }
3037 }
3038 }
3039
BKE_object_boundbox_calc_from_mesh(struct Object * ob,struct Mesh * me_eval)3040 void BKE_object_boundbox_calc_from_mesh(struct Object *ob, struct Mesh *me_eval)
3041 {
3042 float min[3], max[3];
3043
3044 INIT_MINMAX(min, max);
3045
3046 if (!BKE_mesh_wrapper_minmax(me_eval, min, max)) {
3047 zero_v3(min);
3048 zero_v3(max);
3049 }
3050
3051 if (ob->runtime.bb == NULL) {
3052 ob->runtime.bb = MEM_callocN(sizeof(BoundBox), "DM-BoundBox");
3053 }
3054
3055 BKE_boundbox_init_from_minmax(ob->runtime.bb, min, max);
3056
3057 ob->runtime.bb->flag &= ~BOUNDBOX_DIRTY;
3058 }
3059
3060 /** \} */
3061
3062 /* -------------------------------------------------------------------- */
3063 /** \name Object Dimension Get/Set
3064 *
3065 * \warning Setting dimensions is prone to feedback loops in evaluation.
3066 * \{ */
3067
BKE_object_dimensions_get(Object * ob,float r_vec[3])3068 void BKE_object_dimensions_get(Object *ob, float r_vec[3])
3069 {
3070 BoundBox *bb = NULL;
3071
3072 bb = BKE_object_boundbox_get(ob);
3073 if (bb) {
3074 float scale[3];
3075
3076 mat4_to_size(scale, ob->obmat);
3077
3078 r_vec[0] = fabsf(scale[0]) * (bb->vec[4][0] - bb->vec[0][0]);
3079 r_vec[1] = fabsf(scale[1]) * (bb->vec[2][1] - bb->vec[0][1]);
3080 r_vec[2] = fabsf(scale[2]) * (bb->vec[1][2] - bb->vec[0][2]);
3081 }
3082 else {
3083 zero_v3(r_vec);
3084 }
3085 }
3086
3087 /**
3088 * The original scale and object matrix can be passed in so any difference
3089 * of the objects matrix and the final matrix can be accounted for,
3090 * typically this caused by parenting, constraints or delta-scale.
3091 *
3092 * Re-using these values from the object causes a feedback loop
3093 * when multiple values are modified at once in some situations. see: T69536.
3094 */
BKE_object_dimensions_set_ex(Object * ob,const float value[3],int axis_mask,const float ob_scale_orig[3],const float ob_obmat_orig[4][4])3095 void BKE_object_dimensions_set_ex(Object *ob,
3096 const float value[3],
3097 int axis_mask,
3098 const float ob_scale_orig[3],
3099 const float ob_obmat_orig[4][4])
3100 {
3101 BoundBox *bb = NULL;
3102
3103 bb = BKE_object_boundbox_get(ob);
3104 if (bb) {
3105 float len[3];
3106
3107 len[0] = bb->vec[4][0] - bb->vec[0][0];
3108 len[1] = bb->vec[2][1] - bb->vec[0][1];
3109 len[2] = bb->vec[1][2] - bb->vec[0][2];
3110
3111 for (int i = 0; i < 3; i++) {
3112 if (((1 << i) & axis_mask) == 0) {
3113
3114 if (ob_scale_orig != NULL) {
3115 const float scale_delta = len_v3(ob_obmat_orig[i]) / ob_scale_orig[i];
3116 if (isfinite(scale_delta)) {
3117 len[i] *= scale_delta;
3118 }
3119 }
3120
3121 const float scale = copysignf(value[i] / len[i], ob->scale[i]);
3122 if (isfinite(scale)) {
3123 ob->scale[i] = scale;
3124 }
3125 }
3126 }
3127 }
3128 }
3129
BKE_object_dimensions_set(Object * ob,const float value[3],int axis_mask)3130 void BKE_object_dimensions_set(Object *ob, const float value[3], int axis_mask)
3131 {
3132 BKE_object_dimensions_set_ex(ob, value, axis_mask, NULL, NULL);
3133 }
3134
BKE_object_minmax(Object * ob,float r_min[3],float r_max[3],const bool use_hidden)3135 void BKE_object_minmax(Object *ob, float r_min[3], float r_max[3], const bool use_hidden)
3136 {
3137 BoundBox bb;
3138 float vec[3];
3139 bool changed = false;
3140
3141 switch (ob->type) {
3142 case OB_CURVE:
3143 case OB_FONT:
3144 case OB_SURF: {
3145 bb = *BKE_curve_boundbox_get(ob);
3146 BKE_boundbox_minmax(&bb, ob->obmat, r_min, r_max);
3147 changed = true;
3148 break;
3149 }
3150 case OB_MESH: {
3151 bb = *BKE_mesh_boundbox_get(ob);
3152 BKE_boundbox_minmax(&bb, ob->obmat, r_min, r_max);
3153 changed = true;
3154 break;
3155 }
3156 case OB_GPENCIL: {
3157 bb = *BKE_gpencil_boundbox_get(ob);
3158 BKE_boundbox_minmax(&bb, ob->obmat, r_min, r_max);
3159 changed = true;
3160 break;
3161 }
3162 case OB_LATTICE: {
3163 Lattice *lt = ob->data;
3164 BPoint *bp = lt->def;
3165 int u, v, w;
3166
3167 for (w = 0; w < lt->pntsw; w++) {
3168 for (v = 0; v < lt->pntsv; v++) {
3169 for (u = 0; u < lt->pntsu; u++, bp++) {
3170 mul_v3_m4v3(vec, ob->obmat, bp->vec);
3171 minmax_v3v3_v3(r_min, r_max, vec);
3172 }
3173 }
3174 }
3175 changed = true;
3176 break;
3177 }
3178 case OB_ARMATURE: {
3179 changed = BKE_pose_minmax(ob, r_min, r_max, use_hidden, false);
3180 break;
3181 }
3182 case OB_MBALL: {
3183 float ob_min[3], ob_max[3];
3184
3185 changed = BKE_mball_minmax_ex(ob->data, ob_min, ob_max, ob->obmat, 0);
3186 if (changed) {
3187 minmax_v3v3_v3(r_min, r_max, ob_min);
3188 minmax_v3v3_v3(r_min, r_max, ob_max);
3189 }
3190 break;
3191 }
3192 case OB_HAIR: {
3193 bb = *BKE_hair_boundbox_get(ob);
3194 BKE_boundbox_minmax(&bb, ob->obmat, r_min, r_max);
3195 changed = true;
3196 break;
3197 }
3198
3199 case OB_POINTCLOUD: {
3200 bb = *BKE_pointcloud_boundbox_get(ob);
3201 BKE_boundbox_minmax(&bb, ob->obmat, r_min, r_max);
3202 changed = true;
3203 break;
3204 }
3205 case OB_VOLUME: {
3206 bb = *BKE_volume_boundbox_get(ob);
3207 BKE_boundbox_minmax(&bb, ob->obmat, r_min, r_max);
3208 changed = true;
3209 break;
3210 }
3211 }
3212
3213 if (changed == false) {
3214 float size[3];
3215
3216 copy_v3_v3(size, ob->scale);
3217 if (ob->type == OB_EMPTY) {
3218 mul_v3_fl(size, ob->empty_drawsize);
3219 }
3220
3221 minmax_v3v3_v3(r_min, r_max, ob->obmat[3]);
3222
3223 copy_v3_v3(vec, ob->obmat[3]);
3224 add_v3_v3(vec, size);
3225 minmax_v3v3_v3(r_min, r_max, vec);
3226
3227 copy_v3_v3(vec, ob->obmat[3]);
3228 sub_v3_v3(vec, size);
3229 minmax_v3v3_v3(r_min, r_max, vec);
3230 }
3231 }
3232
BKE_object_empty_draw_type_set(Object * ob,const int value)3233 void BKE_object_empty_draw_type_set(Object *ob, const int value)
3234 {
3235 ob->empty_drawtype = value;
3236
3237 if (ob->type == OB_EMPTY && ob->empty_drawtype == OB_EMPTY_IMAGE) {
3238 if (!ob->iuser) {
3239 ob->iuser = MEM_callocN(sizeof(ImageUser), "image user");
3240 ob->iuser->ok = 1;
3241 ob->iuser->flag |= IMA_ANIM_ALWAYS;
3242 ob->iuser->frames = 100;
3243 ob->iuser->sfra = 1;
3244 }
3245 }
3246 else {
3247 if (ob->iuser) {
3248 MEM_freeN(ob->iuser);
3249 ob->iuser = NULL;
3250 }
3251 }
3252 }
3253
BKE_object_empty_image_frame_is_visible_in_view3d(const Object * ob,const RegionView3D * rv3d)3254 bool BKE_object_empty_image_frame_is_visible_in_view3d(const Object *ob, const RegionView3D *rv3d)
3255 {
3256 const char visibility_flag = ob->empty_image_visibility_flag;
3257 if (rv3d->is_persp) {
3258 return (visibility_flag & OB_EMPTY_IMAGE_HIDE_PERSPECTIVE) == 0;
3259 }
3260
3261 return (visibility_flag & OB_EMPTY_IMAGE_HIDE_ORTHOGRAPHIC) == 0;
3262 }
3263
BKE_object_empty_image_data_is_visible_in_view3d(const Object * ob,const RegionView3D * rv3d)3264 bool BKE_object_empty_image_data_is_visible_in_view3d(const Object *ob, const RegionView3D *rv3d)
3265 {
3266 /* Caller is expected to check this. */
3267 BLI_assert(BKE_object_empty_image_frame_is_visible_in_view3d(ob, rv3d));
3268
3269 const char visibility_flag = ob->empty_image_visibility_flag;
3270
3271 if ((visibility_flag & (OB_EMPTY_IMAGE_HIDE_BACK | OB_EMPTY_IMAGE_HIDE_FRONT)) != 0) {
3272 float eps, dot;
3273 if (rv3d->is_persp) {
3274 /* Note, we could normalize the 'view_dir' then use 'eps'
3275 * however the issue with empty objects being visible when viewed from the side
3276 * is only noticeable in orthographic views. */
3277 float view_dir[3];
3278 sub_v3_v3v3(view_dir, rv3d->viewinv[3], ob->obmat[3]);
3279 dot = dot_v3v3(ob->obmat[2], view_dir);
3280 eps = 0.0f;
3281 }
3282 else {
3283 dot = dot_v3v3(ob->obmat[2], rv3d->viewinv[2]);
3284 eps = 1e-5f;
3285 }
3286 if (visibility_flag & OB_EMPTY_IMAGE_HIDE_BACK) {
3287 if (dot < eps) {
3288 return false;
3289 }
3290 }
3291 if (visibility_flag & OB_EMPTY_IMAGE_HIDE_FRONT) {
3292 if (dot > -eps) {
3293 return false;
3294 }
3295 }
3296 }
3297
3298 if (visibility_flag & OB_EMPTY_IMAGE_HIDE_NON_AXIS_ALIGNED) {
3299 float proj[3];
3300 project_plane_v3_v3v3(proj, ob->obmat[2], rv3d->viewinv[2]);
3301 const float proj_length_sq = len_squared_v3(proj);
3302 if (proj_length_sq > 1e-5f) {
3303 return false;
3304 }
3305 }
3306
3307 return true;
3308 }
3309
BKE_object_minmax_dupli(Depsgraph * depsgraph,Scene * scene,Object * ob,float r_min[3],float r_max[3],const bool use_hidden)3310 bool BKE_object_minmax_dupli(Depsgraph *depsgraph,
3311 Scene *scene,
3312 Object *ob,
3313 float r_min[3],
3314 float r_max[3],
3315 const bool use_hidden)
3316 {
3317 bool ok = false;
3318 if ((ob->transflag & OB_DUPLI) == 0) {
3319 return ok;
3320 }
3321
3322 ListBase *lb;
3323 DupliObject *dob;
3324 lb = object_duplilist(depsgraph, scene, ob);
3325 for (dob = lb->first; dob; dob = dob->next) {
3326 if ((use_hidden == false) && (dob->no_draw != 0)) {
3327 /* pass */
3328 }
3329 else {
3330 BoundBox *bb = BKE_object_boundbox_get(dob->ob);
3331
3332 if (bb) {
3333 int i;
3334 for (i = 0; i < 8; i++) {
3335 float vec[3];
3336 mul_v3_m4v3(vec, dob->mat, bb->vec[i]);
3337 minmax_v3v3_v3(r_min, r_max, vec);
3338 }
3339
3340 ok = true;
3341 }
3342 }
3343 }
3344 free_object_duplilist(lb); /* does restore */
3345
3346 return ok;
3347 }
3348
BKE_object_foreach_display_point(Object * ob,const float obmat[4][4],void (* func_cb)(const float[3],void *),void * user_data)3349 void BKE_object_foreach_display_point(Object *ob,
3350 const float obmat[4][4],
3351 void (*func_cb)(const float[3], void *),
3352 void *user_data)
3353 {
3354 /* TODO: pointcloud and hair objects support */
3355 Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob);
3356 float co[3];
3357
3358 if (mesh_eval != NULL) {
3359 const MVert *mv = mesh_eval->mvert;
3360 const int totvert = mesh_eval->totvert;
3361 for (int i = 0; i < totvert; i++, mv++) {
3362 mul_v3_m4v3(co, obmat, mv->co);
3363 func_cb(co, user_data);
3364 }
3365 }
3366 else if (ob->runtime.curve_cache && ob->runtime.curve_cache->disp.first) {
3367 DispList *dl;
3368
3369 for (dl = ob->runtime.curve_cache->disp.first; dl; dl = dl->next) {
3370 const float *v3 = dl->verts;
3371 int totvert = dl->nr;
3372 int i;
3373
3374 for (i = 0; i < totvert; i++, v3 += 3) {
3375 mul_v3_m4v3(co, obmat, v3);
3376 func_cb(co, user_data);
3377 }
3378 }
3379 }
3380 }
3381
BKE_scene_foreach_display_point(Depsgraph * depsgraph,void (* func_cb)(const float[3],void *),void * user_data)3382 void BKE_scene_foreach_display_point(Depsgraph *depsgraph,
3383 void (*func_cb)(const float[3], void *),
3384 void *user_data)
3385 {
3386 DEG_OBJECT_ITER_BEGIN (depsgraph,
3387 ob,
3388 DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE |
3389 DEG_ITER_OBJECT_FLAG_DUPLI) {
3390 if ((ob->base_flag & BASE_SELECTED) != 0) {
3391 BKE_object_foreach_display_point(ob, ob->obmat, func_cb, user_data);
3392 }
3393 }
3394 DEG_OBJECT_ITER_END;
3395 }
3396
3397 /**
3398 * Struct members from DNA_object_types.h
3399 */
3400 typedef struct ObTfmBack {
3401 float loc[3], dloc[3];
3402 /** scale and delta scale. */
3403 float scale[3], dscale[3];
3404 /** euler rotation. */
3405 float rot[3], drot[3];
3406 /** quaternion rotation. */
3407 float quat[4], dquat[4];
3408 /** axis angle rotation - axis part. */
3409 float rotAxis[3], drotAxis[3];
3410 /** axis angle rotation - angle part. */
3411 float rotAngle, drotAngle;
3412 /** final worldspace matrix with constraints & animsys applied. */
3413 float obmat[4][4];
3414 /** inverse result of parent, so that object doesn't 'stick' to parent. */
3415 float parentinv[4][4];
3416 /** inverse result of constraints. doesn't include effect of parent or object local transform.
3417 */
3418 float constinv[4][4];
3419 /** inverse matrix of 'obmat' for during render, temporally: ipokeys of transform. */
3420 float imat[4][4];
3421 } ObTfmBack;
3422
BKE_object_tfm_backup(Object * ob)3423 void *BKE_object_tfm_backup(Object *ob)
3424 {
3425 ObTfmBack *obtfm = MEM_mallocN(sizeof(ObTfmBack), "ObTfmBack");
3426 copy_v3_v3(obtfm->loc, ob->loc);
3427 copy_v3_v3(obtfm->dloc, ob->dloc);
3428 copy_v3_v3(obtfm->scale, ob->scale);
3429 copy_v3_v3(obtfm->dscale, ob->dscale);
3430 copy_v3_v3(obtfm->rot, ob->rot);
3431 copy_v3_v3(obtfm->drot, ob->drot);
3432 copy_qt_qt(obtfm->quat, ob->quat);
3433 copy_qt_qt(obtfm->dquat, ob->dquat);
3434 copy_v3_v3(obtfm->rotAxis, ob->rotAxis);
3435 copy_v3_v3(obtfm->drotAxis, ob->drotAxis);
3436 obtfm->rotAngle = ob->rotAngle;
3437 obtfm->drotAngle = ob->drotAngle;
3438 copy_m4_m4(obtfm->obmat, ob->obmat);
3439 copy_m4_m4(obtfm->parentinv, ob->parentinv);
3440 copy_m4_m4(obtfm->constinv, ob->constinv);
3441 copy_m4_m4(obtfm->imat, ob->imat);
3442
3443 return (void *)obtfm;
3444 }
3445
BKE_object_tfm_restore(Object * ob,void * obtfm_pt)3446 void BKE_object_tfm_restore(Object *ob, void *obtfm_pt)
3447 {
3448 ObTfmBack *obtfm = (ObTfmBack *)obtfm_pt;
3449 copy_v3_v3(ob->loc, obtfm->loc);
3450 copy_v3_v3(ob->dloc, obtfm->dloc);
3451 copy_v3_v3(ob->scale, obtfm->scale);
3452 copy_v3_v3(ob->dscale, obtfm->dscale);
3453 copy_v3_v3(ob->rot, obtfm->rot);
3454 copy_v3_v3(ob->drot, obtfm->drot);
3455 copy_qt_qt(ob->quat, obtfm->quat);
3456 copy_qt_qt(ob->dquat, obtfm->dquat);
3457 copy_v3_v3(ob->rotAxis, obtfm->rotAxis);
3458 copy_v3_v3(ob->drotAxis, obtfm->drotAxis);
3459 ob->rotAngle = obtfm->rotAngle;
3460 ob->drotAngle = obtfm->drotAngle;
3461 copy_m4_m4(ob->obmat, obtfm->obmat);
3462 copy_m4_m4(ob->parentinv, obtfm->parentinv);
3463 copy_m4_m4(ob->constinv, obtfm->constinv);
3464 copy_m4_m4(ob->imat, obtfm->imat);
3465 }
3466
BKE_object_parent_loop_check(const Object * par,const Object * ob)3467 bool BKE_object_parent_loop_check(const Object *par, const Object *ob)
3468 {
3469 /* test if 'ob' is a parent somewhere in par's parents */
3470 if (par == NULL) {
3471 return false;
3472 }
3473 if (ob == par) {
3474 return true;
3475 }
3476 return BKE_object_parent_loop_check(par->parent, ob);
3477 }
3478
object_handle_update_proxy(Depsgraph * depsgraph,Scene * scene,Object * object,const bool do_proxy_update)3479 static void object_handle_update_proxy(Depsgraph *depsgraph,
3480 Scene *scene,
3481 Object *object,
3482 const bool do_proxy_update)
3483 {
3484 /* The case when this is a collection proxy, object_update is called in collection.c */
3485 if (object->proxy == NULL) {
3486 return;
3487 }
3488 /* set pointer in library proxy target, for copying, but restore it */
3489 object->proxy->proxy_from = object;
3490 // printf("set proxy pointer for later collection stuff %s\n", ob->id.name);
3491
3492 /* the no-group proxy case, we call update */
3493 if (object->proxy_group == NULL) {
3494 if (do_proxy_update) {
3495 // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
3496 BKE_object_handle_update(depsgraph, scene, object->proxy);
3497 }
3498 }
3499 }
3500
3501 /**
3502 * Proxy rule:
3503 * - lib_object->proxy_from == the one we borrow from, only set temporal and cleared here.
3504 * - local_object->proxy == pointer to library object, saved in files and read.
3505 *
3506 * Function below is polluted with proxy exceptions, cleanup will follow!
3507 *
3508 * The main object update call, for object matrix, constraints, keys and displist (modifiers)
3509 * requires flags to be set!
3510 *
3511 * Ideally we shouldn't have to pass the rigid body world,
3512 * but need bigger restructuring to avoid id.
3513 */
BKE_object_handle_update_ex(Depsgraph * depsgraph,Scene * scene,Object * ob,RigidBodyWorld * rbw,const bool do_proxy_update)3514 void BKE_object_handle_update_ex(Depsgraph *depsgraph,
3515 Scene *scene,
3516 Object *ob,
3517 RigidBodyWorld *rbw,
3518 const bool do_proxy_update)
3519 {
3520 const ID *object_data = ob->data;
3521 const bool recalc_object = (ob->id.recalc & ID_RECALC_ALL) != 0;
3522 const bool recalc_data = (object_data != NULL) ? ((object_data->recalc & ID_RECALC_ALL) != 0) :
3523 0;
3524 if (!recalc_object && !recalc_data) {
3525 object_handle_update_proxy(depsgraph, scene, ob, do_proxy_update);
3526 return;
3527 }
3528 /* Speed optimization for animation lookups. */
3529 if (ob->pose != NULL) {
3530 BKE_pose_channels_hash_make(ob->pose);
3531 if (ob->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
3532 BKE_pose_update_constraint_flags(ob->pose);
3533 }
3534 }
3535 if (recalc_data) {
3536 if (ob->type == OB_ARMATURE) {
3537 /* this happens for reading old files and to match library armatures
3538 * with poses we do it ahead of BKE_object_where_is_calc to ensure animation
3539 * is evaluated on the rebuilt pose, otherwise we get incorrect poses
3540 * on file load */
3541 if (ob->pose == NULL || (ob->pose->flag & POSE_RECALC)) {
3542 /* No need to pass bmain here, we assume we do not need to rebuild DEG from here... */
3543 BKE_pose_rebuild(NULL, ob, ob->data, true);
3544 }
3545 }
3546 }
3547 /* XXX new animsys warning: depsgraph tag ID_RECALC_GEOMETRY should not skip drivers,
3548 * which is only in BKE_object_where_is_calc now */
3549 /* XXX: should this case be ID_RECALC_TRANSFORM instead? */
3550 if (recalc_object || recalc_data) {
3551 if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) {
3552 printf("recalcob %s\n", ob->id.name + 2);
3553 }
3554 /* Handle proxy copy for target. */
3555 if (!BKE_object_eval_proxy_copy(depsgraph, ob)) {
3556 BKE_object_where_is_calc_ex(depsgraph, scene, rbw, ob, NULL);
3557 }
3558 }
3559
3560 if (recalc_data) {
3561 BKE_object_handle_data_update(depsgraph, scene, ob);
3562 }
3563
3564 ob->id.recalc &= ID_RECALC_ALL;
3565
3566 object_handle_update_proxy(depsgraph, scene, ob, do_proxy_update);
3567 }
3568
3569 /**
3570 * \warning "scene" here may not be the scene object actually resides in.
3571 * When dealing with background-sets, "scene" is actually the active scene.
3572 * e.g. "scene" <-- set 1 <-- set 2 ("ob" lives here) <-- set 3 <-- ... <-- set n
3573 * rigid bodies depend on their world so use #BKE_object_handle_update_ex()
3574 * to also pass along the current rigid body world.
3575 */
BKE_object_handle_update(Depsgraph * depsgraph,Scene * scene,Object * ob)3576 void BKE_object_handle_update(Depsgraph *depsgraph, Scene *scene, Object *ob)
3577 {
3578 BKE_object_handle_update_ex(depsgraph, scene, ob, NULL, true);
3579 }
3580
BKE_object_sculpt_data_create(Object * ob)3581 void BKE_object_sculpt_data_create(Object *ob)
3582 {
3583 BLI_assert((ob->sculpt == NULL) && (ob->mode & OB_MODE_ALL_SCULPT));
3584 ob->sculpt = MEM_callocN(sizeof(SculptSession), __func__);
3585 ob->sculpt->mode_type = ob->mode;
3586 }
3587
BKE_object_obdata_texspace_get(Object * ob,short ** r_texflag,float ** r_loc,float ** r_size)3588 bool BKE_object_obdata_texspace_get(Object *ob, short **r_texflag, float **r_loc, float **r_size)
3589 {
3590
3591 if (ob->data == NULL) {
3592 return false;
3593 }
3594
3595 switch (GS(((ID *)ob->data)->name)) {
3596 case ID_ME: {
3597 BKE_mesh_texspace_get_reference((Mesh *)ob->data, r_texflag, r_loc, r_size);
3598 break;
3599 }
3600 case ID_CU: {
3601 Curve *cu = ob->data;
3602 BKE_curve_texspace_ensure(cu);
3603 if (r_texflag) {
3604 *r_texflag = &cu->texflag;
3605 }
3606 if (r_loc) {
3607 *r_loc = cu->loc;
3608 }
3609 if (r_size) {
3610 *r_size = cu->size;
3611 }
3612 break;
3613 }
3614 case ID_MB: {
3615 MetaBall *mb = ob->data;
3616 if (r_texflag) {
3617 *r_texflag = &mb->texflag;
3618 }
3619 if (r_loc) {
3620 *r_loc = mb->loc;
3621 }
3622 if (r_size) {
3623 *r_size = mb->size;
3624 }
3625 break;
3626 }
3627 default:
3628 return false;
3629 }
3630 return true;
3631 }
3632
3633 /** Get evaluated mesh for given object. */
BKE_object_get_evaluated_mesh(Object * object)3634 Mesh *BKE_object_get_evaluated_mesh(Object *object)
3635 {
3636 ID *data_eval = object->runtime.data_eval;
3637 return (data_eval && GS(data_eval->name) == ID_ME) ? (Mesh *)data_eval : NULL;
3638 }
3639
3640 /**
3641 * Get mesh which is not affected by modifiers:
3642 * - For original objects it will be same as `object->data`, and it is a mesh
3643 * which is in the corresponding #Main.
3644 * - For copied-on-write objects it will give pointer to a copied-on-write
3645 * mesh which corresponds to original object's mesh.
3646 */
BKE_object_get_pre_modified_mesh(Object * object)3647 Mesh *BKE_object_get_pre_modified_mesh(Object *object)
3648 {
3649 if (object->type == OB_MESH && object->runtime.data_orig != NULL) {
3650 BLI_assert(object->id.tag & LIB_TAG_COPIED_ON_WRITE);
3651 BLI_assert(object->id.orig_id != NULL);
3652 BLI_assert(object->runtime.data_orig->orig_id == ((Object *)object->id.orig_id)->data);
3653 Mesh *result = (Mesh *)object->runtime.data_orig;
3654 BLI_assert((result->id.tag & LIB_TAG_COPIED_ON_WRITE) != 0);
3655 BLI_assert((result->id.tag & LIB_TAG_COPIED_ON_WRITE_EVAL_RESULT) == 0);
3656 return result;
3657 }
3658 BLI_assert((object->id.tag & LIB_TAG_COPIED_ON_WRITE) == 0);
3659 return object->data;
3660 }
3661
3662 /**
3663 * Get a mesh which corresponds to very very original mesh from #Main.
3664 * - For original objects it will be object->data.
3665 * - For evaluated objects it will be same mesh as corresponding original
3666 * object uses as data.
3667 */
BKE_object_get_original_mesh(Object * object)3668 Mesh *BKE_object_get_original_mesh(Object *object)
3669 {
3670 Mesh *result = NULL;
3671 if (object->id.orig_id == NULL) {
3672 BLI_assert((object->id.tag & LIB_TAG_COPIED_ON_WRITE) == 0);
3673 result = object->data;
3674 }
3675 else {
3676 BLI_assert((object->id.tag & LIB_TAG_COPIED_ON_WRITE) != 0);
3677 result = ((Object *)object->id.orig_id)->data;
3678 }
3679 BLI_assert(result != NULL);
3680 BLI_assert((result->id.tag & (LIB_TAG_COPIED_ON_WRITE | LIB_TAG_COPIED_ON_WRITE_EVAL_RESULT)) ==
3681 0);
3682 return result;
3683 }
3684
pc_cmp(const void * a,const void * b)3685 static int pc_cmp(const void *a, const void *b)
3686 {
3687 const LinkData *ad = a, *bd = b;
3688 if (POINTER_AS_INT(ad->data) > POINTER_AS_INT(bd->data)) {
3689 return 1;
3690 }
3691
3692 return 0;
3693 }
3694
BKE_object_insert_ptcache(Object * ob)3695 int BKE_object_insert_ptcache(Object *ob)
3696 {
3697 LinkData *link = NULL;
3698 int i = 0;
3699
3700 BLI_listbase_sort(&ob->pc_ids, pc_cmp);
3701
3702 for (link = ob->pc_ids.first, i = 0; link; link = link->next, i++) {
3703 int index = POINTER_AS_INT(link->data);
3704
3705 if (i < index) {
3706 break;
3707 }
3708 }
3709
3710 link = MEM_callocN(sizeof(LinkData), "PCLink");
3711 link->data = POINTER_FROM_INT(i);
3712 BLI_addtail(&ob->pc_ids, link);
3713
3714 return i;
3715 }
3716
pc_findindex(ListBase * listbase,int index)3717 static int pc_findindex(ListBase *listbase, int index)
3718 {
3719 LinkData *link = NULL;
3720 int number = 0;
3721
3722 if (listbase == NULL) {
3723 return -1;
3724 }
3725
3726 link = listbase->first;
3727 while (link) {
3728 if (POINTER_AS_INT(link->data) == index) {
3729 return number;
3730 }
3731
3732 number++;
3733 link = link->next;
3734 }
3735
3736 return -1;
3737 }
3738
BKE_object_delete_ptcache(Object * ob,int index)3739 void BKE_object_delete_ptcache(Object *ob, int index)
3740 {
3741 int list_index = pc_findindex(&ob->pc_ids, index);
3742 LinkData *link = BLI_findlink(&ob->pc_ids, list_index);
3743 BLI_freelinkN(&ob->pc_ids, link);
3744 }
3745
3746 /* -------------------------------------------------------------------- */
3747 /** \name Object Data Shape Key Insert
3748 * \{ */
3749
3750 /** Mesh */
insert_meshkey(Main * bmain,Object * ob,const char * name,const bool from_mix)3751 static KeyBlock *insert_meshkey(Main *bmain, Object *ob, const char *name, const bool from_mix)
3752 {
3753 Mesh *me = ob->data;
3754 Key *key = me->key;
3755 KeyBlock *kb;
3756 int newkey = 0;
3757
3758 if (key == NULL) {
3759 key = me->key = BKE_key_add(bmain, (ID *)me);
3760 key->type = KEY_RELATIVE;
3761 newkey = 1;
3762 }
3763
3764 if (newkey || from_mix == false) {
3765 /* create from mesh */
3766 kb = BKE_keyblock_add_ctime(key, name, false);
3767 BKE_keyblock_convert_from_mesh(me, key, kb);
3768 }
3769 else {
3770 /* copy from current values */
3771 int totelem;
3772 float *data = BKE_key_evaluate_object(ob, &totelem);
3773
3774 /* create new block with prepared data */
3775 kb = BKE_keyblock_add_ctime(key, name, false);
3776 kb->data = data;
3777 kb->totelem = totelem;
3778 }
3779
3780 return kb;
3781 }
3782 /** Lattice */
insert_lattkey(Main * bmain,Object * ob,const char * name,const bool from_mix)3783 static KeyBlock *insert_lattkey(Main *bmain, Object *ob, const char *name, const bool from_mix)
3784 {
3785 Lattice *lt = ob->data;
3786 Key *key = lt->key;
3787 KeyBlock *kb;
3788 int newkey = 0;
3789
3790 if (key == NULL) {
3791 key = lt->key = BKE_key_add(bmain, (ID *)lt);
3792 key->type = KEY_RELATIVE;
3793 newkey = 1;
3794 }
3795
3796 if (newkey || from_mix == false) {
3797 kb = BKE_keyblock_add_ctime(key, name, false);
3798 if (!newkey) {
3799 KeyBlock *basekb = (KeyBlock *)key->block.first;
3800 kb->data = MEM_dupallocN(basekb->data);
3801 kb->totelem = basekb->totelem;
3802 }
3803 else {
3804 BKE_keyblock_convert_from_lattice(lt, kb);
3805 }
3806 }
3807 else {
3808 /* copy from current values */
3809 int totelem;
3810 float *data = BKE_key_evaluate_object(ob, &totelem);
3811
3812 /* create new block with prepared data */
3813 kb = BKE_keyblock_add_ctime(key, name, false);
3814 kb->totelem = totelem;
3815 kb->data = data;
3816 }
3817
3818 return kb;
3819 }
3820 /** Curve */
insert_curvekey(Main * bmain,Object * ob,const char * name,const bool from_mix)3821 static KeyBlock *insert_curvekey(Main *bmain, Object *ob, const char *name, const bool from_mix)
3822 {
3823 Curve *cu = ob->data;
3824 Key *key = cu->key;
3825 KeyBlock *kb;
3826 ListBase *lb = BKE_curve_nurbs_get(cu);
3827 int newkey = 0;
3828
3829 if (key == NULL) {
3830 key = cu->key = BKE_key_add(bmain, (ID *)cu);
3831 key->type = KEY_RELATIVE;
3832 newkey = 1;
3833 }
3834
3835 if (newkey || from_mix == false) {
3836 /* create from curve */
3837 kb = BKE_keyblock_add_ctime(key, name, false);
3838 if (!newkey) {
3839 KeyBlock *basekb = (KeyBlock *)key->block.first;
3840 kb->data = MEM_dupallocN(basekb->data);
3841 kb->totelem = basekb->totelem;
3842 }
3843 else {
3844 BKE_keyblock_convert_from_curve(cu, kb, lb);
3845 }
3846 }
3847 else {
3848 /* copy from current values */
3849 int totelem;
3850 float *data = BKE_key_evaluate_object(ob, &totelem);
3851
3852 /* create new block with prepared data */
3853 kb = BKE_keyblock_add_ctime(key, name, false);
3854 kb->totelem = totelem;
3855 kb->data = data;
3856 }
3857
3858 return kb;
3859 }
3860
3861 /** \} */
3862
3863 /* -------------------------------------------------------------------- */
3864 /** \name Object Shape Key API
3865 * \{ */
3866
BKE_object_shapekey_insert(Main * bmain,Object * ob,const char * name,const bool from_mix)3867 KeyBlock *BKE_object_shapekey_insert(Main *bmain,
3868 Object *ob,
3869 const char *name,
3870 const bool from_mix)
3871 {
3872 KeyBlock *key = NULL;
3873
3874 switch (ob->type) {
3875 case OB_MESH:
3876 key = insert_meshkey(bmain, ob, name, from_mix);
3877 break;
3878 case OB_CURVE:
3879 case OB_SURF:
3880 key = insert_curvekey(bmain, ob, name, from_mix);
3881 break;
3882 case OB_LATTICE:
3883 key = insert_lattkey(bmain, ob, name, from_mix);
3884 break;
3885 default:
3886 break;
3887 }
3888
3889 /* Set the first active when none is set when called from RNA. */
3890 if (key != NULL) {
3891 if (ob->shapenr <= 0) {
3892 ob->shapenr = 1;
3893 }
3894 }
3895
3896 return key;
3897 }
3898
BKE_object_shapekey_free(Main * bmain,Object * ob)3899 bool BKE_object_shapekey_free(Main *bmain, Object *ob)
3900 {
3901 Key **key_p, *key;
3902
3903 key_p = BKE_key_from_object_p(ob);
3904 if (ELEM(NULL, key_p, *key_p)) {
3905 return false;
3906 }
3907
3908 key = *key_p;
3909 *key_p = NULL;
3910
3911 BKE_id_free_us(bmain, key);
3912
3913 return true;
3914 }
3915
BKE_object_shapekey_remove(Main * bmain,Object * ob,KeyBlock * kb)3916 bool BKE_object_shapekey_remove(Main *bmain, Object *ob, KeyBlock *kb)
3917 {
3918 KeyBlock *rkb;
3919 Key *key = BKE_key_from_object(ob);
3920 short kb_index;
3921
3922 if (key == NULL) {
3923 return false;
3924 }
3925
3926 kb_index = BLI_findindex(&key->block, kb);
3927 BLI_assert(kb_index != -1);
3928
3929 for (rkb = key->block.first; rkb; rkb = rkb->next) {
3930 if (rkb->relative == kb_index) {
3931 /* remap to the 'Basis' */
3932 rkb->relative = 0;
3933 }
3934 else if (rkb->relative >= kb_index) {
3935 /* Fix positional shift of the keys when kb is deleted from the list */
3936 rkb->relative -= 1;
3937 }
3938 }
3939
3940 BLI_remlink(&key->block, kb);
3941 key->totkey--;
3942 if (key->refkey == kb) {
3943 key->refkey = key->block.first;
3944
3945 if (key->refkey) {
3946 /* apply new basis key on original data */
3947 switch (ob->type) {
3948 case OB_MESH:
3949 BKE_keyblock_convert_to_mesh(key->refkey, ob->data);
3950 break;
3951 case OB_CURVE:
3952 case OB_SURF:
3953 BKE_keyblock_convert_to_curve(key->refkey, ob->data, BKE_curve_nurbs_get(ob->data));
3954 break;
3955 case OB_LATTICE:
3956 BKE_keyblock_convert_to_lattice(key->refkey, ob->data);
3957 break;
3958 }
3959 }
3960 }
3961
3962 if (kb->data) {
3963 MEM_freeN(kb->data);
3964 }
3965 MEM_freeN(kb);
3966
3967 /* Unset active when all are freed. */
3968 if (BLI_listbase_is_empty(&key->block)) {
3969 ob->shapenr = 0;
3970 }
3971 else if (ob->shapenr > 1) {
3972 ob->shapenr--;
3973 }
3974
3975 if (key->totkey == 0) {
3976 BKE_object_shapekey_free(bmain, ob);
3977 }
3978
3979 return true;
3980 }
3981
3982 /** \} */
3983
BKE_object_flag_test_recursive(const Object * ob,short flag)3984 bool BKE_object_flag_test_recursive(const Object *ob, short flag)
3985 {
3986 if (ob->flag & flag) {
3987 return true;
3988 }
3989 if (ob->parent) {
3990 return BKE_object_flag_test_recursive(ob->parent, flag);
3991 }
3992
3993 return false;
3994 }
3995
BKE_object_is_child_recursive(const Object * ob_parent,const Object * ob_child)3996 bool BKE_object_is_child_recursive(const Object *ob_parent, const Object *ob_child)
3997 {
3998 for (ob_child = ob_child->parent; ob_child; ob_child = ob_child->parent) {
3999 if (ob_child == ob_parent) {
4000 return true;
4001 }
4002 }
4003 return false;
4004 }
4005
4006 /**
4007 * Most important if this is modified it should _always_ return true, in certain
4008 * cases false positives are hard to avoid (shape keys for example).
4009 */
BKE_object_is_modified(Scene * scene,Object * ob)4010 int BKE_object_is_modified(Scene *scene, Object *ob)
4011 {
4012 /* Always test on original object since evaluated object may no longer
4013 * have shape keys or modifiers that were used to evaluate it. */
4014 ob = DEG_get_original_object(ob);
4015
4016 int flag = 0;
4017
4018 if (BKE_key_from_object(ob)) {
4019 flag |= eModifierMode_Render | eModifierMode_Realtime;
4020 }
4021 else {
4022 ModifierData *md;
4023 VirtualModifierData virtualModifierData;
4024 /* cloth */
4025 for (md = BKE_modifiers_get_virtual_modifierlist(ob, &virtualModifierData);
4026 md && (flag != (eModifierMode_Render | eModifierMode_Realtime));
4027 md = md->next) {
4028 if ((flag & eModifierMode_Render) == 0 &&
4029 BKE_modifier_is_enabled(scene, md, eModifierMode_Render)) {
4030 flag |= eModifierMode_Render;
4031 }
4032
4033 if ((flag & eModifierMode_Realtime) == 0 &&
4034 BKE_modifier_is_enabled(scene, md, eModifierMode_Realtime)) {
4035 flag |= eModifierMode_Realtime;
4036 }
4037 }
4038 }
4039
4040 return flag;
4041 }
4042
4043 /**
4044 * Check of objects moves in time.
4045 *
4046 * \note This function is currently optimized for usage in combination
4047 * with modifier deformation checks (#eModifierTypeType_OnlyDeform),
4048 * so modifiers can quickly check if their target objects moves
4049 * (causing deformation motion blur) or not.
4050 *
4051 * This makes it possible to give some degree of false-positives here,
4052 * but it's currently an acceptable tradeoff between complexity and check
4053 * speed. In combination with checks of modifier stack and real life usage
4054 * percentage of false-positives shouldn't be that high.
4055 *
4056 * \note This function does not consider physics systems.
4057 */
BKE_object_moves_in_time(const Object * object,bool recurse_parent)4058 bool BKE_object_moves_in_time(const Object *object, bool recurse_parent)
4059 {
4060 /* If object has any sort of animation data assume it is moving. */
4061 if (BKE_animdata_id_is_animated(&object->id)) {
4062 return true;
4063 }
4064 if (!BLI_listbase_is_empty(&object->constraints)) {
4065 return true;
4066 }
4067 if (recurse_parent && object->parent != NULL) {
4068 return BKE_object_moves_in_time(object->parent, true);
4069 }
4070 return false;
4071 }
4072
object_moves_in_time(const Object * object)4073 static bool object_moves_in_time(const Object *object)
4074 {
4075 return BKE_object_moves_in_time(object, true);
4076 }
4077
object_deforms_in_time(Object * object)4078 static bool object_deforms_in_time(Object *object)
4079 {
4080 if (BKE_key_from_object(object) != NULL) {
4081 return true;
4082 }
4083 if (!BLI_listbase_is_empty(&object->modifiers)) {
4084 return true;
4085 }
4086 return object_moves_in_time(object);
4087 }
4088
constructive_modifier_is_deform_modified(ModifierData * md)4089 static bool constructive_modifier_is_deform_modified(ModifierData *md)
4090 {
4091 /* TODO(sergey): Consider generalizing this a bit so all modifier logic
4092 * is concentrated in MOD_{modifier}.c file,
4093 */
4094 if (md->type == eModifierType_Array) {
4095 ArrayModifierData *amd = (ArrayModifierData *)md;
4096 /* TODO(sergey): Check if curve is deformed. */
4097 return (amd->start_cap != NULL && object_moves_in_time(amd->start_cap)) ||
4098 (amd->end_cap != NULL && object_moves_in_time(amd->end_cap)) ||
4099 (amd->curve_ob != NULL && object_moves_in_time(amd->curve_ob)) ||
4100 (amd->offset_ob != NULL && object_moves_in_time(amd->offset_ob));
4101 }
4102 if (md->type == eModifierType_Mirror) {
4103 MirrorModifierData *mmd = (MirrorModifierData *)md;
4104 return mmd->mirror_ob != NULL && object_moves_in_time(mmd->mirror_ob);
4105 }
4106 if (md->type == eModifierType_Screw) {
4107 ScrewModifierData *smd = (ScrewModifierData *)md;
4108 return smd->ob_axis != NULL && object_moves_in_time(smd->ob_axis);
4109 }
4110 if (md->type == eModifierType_MeshSequenceCache) {
4111 /* NOTE: Not ideal because it's unknown whether topology changes or not.
4112 * This will be detected later, so by assuming it's only deformation
4113 * going on here we allow to bake deform-only mesh to Alembic and have
4114 * proper motion blur after that.
4115 */
4116 return true;
4117 }
4118 return false;
4119 }
4120
modifiers_has_animation_check(const Object * ob)4121 static bool modifiers_has_animation_check(const Object *ob)
4122 {
4123 /* TODO(sergey): This is a bit code duplication with depsgraph, but
4124 * would be nicer to solve this as a part of new dependency graph
4125 * work, so we avoid conflicts and so.
4126 */
4127 if (ob->adt != NULL) {
4128 AnimData *adt = ob->adt;
4129 FCurve *fcu;
4130 if (adt->action != NULL) {
4131 for (fcu = adt->action->curves.first; fcu; fcu = fcu->next) {
4132 if (fcu->rna_path && strstr(fcu->rna_path, "modifiers[")) {
4133 return true;
4134 }
4135 }
4136 }
4137 for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
4138 if (fcu->rna_path && strstr(fcu->rna_path, "modifiers[")) {
4139 return true;
4140 }
4141 }
4142 }
4143 return false;
4144 }
4145
4146 /**
4147 * Test if object is affected by deforming modifiers (for motion blur). again
4148 * most important is to avoid false positives, this is to skip computations
4149 * and we can still if there was actual deformation afterwards.
4150 */
BKE_object_is_deform_modified(Scene * scene,Object * ob)4151 int BKE_object_is_deform_modified(Scene *scene, Object *ob)
4152 {
4153 /* Always test on original object since evaluated object may no longer
4154 * have shape keys or modifiers that were used to evaluate it. */
4155 ob = DEG_get_original_object(ob);
4156
4157 ModifierData *md;
4158 VirtualModifierData virtualModifierData;
4159 int flag = 0;
4160 const bool is_modifier_animated = modifiers_has_animation_check(ob);
4161
4162 if (BKE_key_from_object(ob)) {
4163 flag |= eModifierMode_Realtime | eModifierMode_Render;
4164 }
4165
4166 if (ob->type == OB_CURVE) {
4167 Curve *cu = (Curve *)ob->data;
4168 if (cu->taperobj != NULL && object_deforms_in_time(cu->taperobj)) {
4169 flag |= eModifierMode_Realtime | eModifierMode_Render;
4170 }
4171 }
4172
4173 /* cloth */
4174 for (md = BKE_modifiers_get_virtual_modifierlist(ob, &virtualModifierData);
4175 md && (flag != (eModifierMode_Render | eModifierMode_Realtime));
4176 md = md->next) {
4177 const ModifierTypeInfo *mti = BKE_modifier_get_info(md->type);
4178 bool can_deform = mti->type == eModifierTypeType_OnlyDeform || is_modifier_animated;
4179
4180 if (!can_deform) {
4181 can_deform = constructive_modifier_is_deform_modified(md);
4182 }
4183
4184 if (can_deform) {
4185 if (!(flag & eModifierMode_Render) &&
4186 BKE_modifier_is_enabled(scene, md, eModifierMode_Render)) {
4187 flag |= eModifierMode_Render;
4188 }
4189
4190 if (!(flag & eModifierMode_Realtime) &&
4191 BKE_modifier_is_enabled(scene, md, eModifierMode_Realtime)) {
4192 flag |= eModifierMode_Realtime;
4193 }
4194 }
4195 }
4196
4197 return flag;
4198 }
4199
4200 /** Return the number of scenes using (instantiating) that object in their collections. */
BKE_object_scenes_users_get(Main * bmain,Object * ob)4201 int BKE_object_scenes_users_get(Main *bmain, Object *ob)
4202 {
4203 int num_scenes = 0;
4204 for (Scene *scene = bmain->scenes.first; scene != NULL; scene = scene->id.next) {
4205 if (BKE_collection_has_object_recursive(scene->master_collection, ob)) {
4206 num_scenes++;
4207 }
4208 }
4209 return num_scenes;
4210 }
4211
BKE_object_movieclip_get(Scene * scene,Object * ob,bool use_default)4212 MovieClip *BKE_object_movieclip_get(Scene *scene, Object *ob, bool use_default)
4213 {
4214 MovieClip *clip = use_default ? scene->clip : NULL;
4215 bConstraint *con = ob->constraints.first, *scon = NULL;
4216
4217 while (con) {
4218 if (con->type == CONSTRAINT_TYPE_CAMERASOLVER) {
4219 if (scon == NULL || (scon->flag & CONSTRAINT_OFF)) {
4220 scon = con;
4221 }
4222 }
4223
4224 con = con->next;
4225 }
4226
4227 if (scon) {
4228 bCameraSolverConstraint *solver = scon->data;
4229 if ((solver->flag & CAMERASOLVER_ACTIVECLIP) == 0) {
4230 clip = solver->clip;
4231 }
4232 else {
4233 clip = scene->clip;
4234 }
4235 }
4236
4237 return clip;
4238 }
4239
BKE_object_runtime_reset(Object * object)4240 void BKE_object_runtime_reset(Object *object)
4241 {
4242 memset(&object->runtime, 0, sizeof(object->runtime));
4243 }
4244
4245 /**
4246 * Reset all pointers which we don't want to be shared when copying the object.
4247 */
BKE_object_runtime_reset_on_copy(Object * object,const int UNUSED (flag))4248 void BKE_object_runtime_reset_on_copy(Object *object, const int UNUSED(flag))
4249 {
4250 Object_Runtime *runtime = &object->runtime;
4251 runtime->data_eval = NULL;
4252 runtime->gpd_eval = NULL;
4253 runtime->mesh_deform_eval = NULL;
4254 runtime->curve_cache = NULL;
4255 runtime->object_as_temp_mesh = NULL;
4256 }
4257
4258 /**
4259 * Find an associated armature object.
4260 */
obrel_armature_find(Object * ob)4261 static Object *obrel_armature_find(Object *ob)
4262 {
4263 Object *ob_arm = NULL;
4264
4265 if (ob->parent && ob->partype == PARSKEL && ob->parent->type == OB_ARMATURE) {
4266 ob_arm = ob->parent;
4267 }
4268 else {
4269 ModifierData *mod;
4270 for (mod = (ModifierData *)ob->modifiers.first; mod; mod = mod->next) {
4271 if (mod->type == eModifierType_Armature) {
4272 ob_arm = ((ArmatureModifierData *)mod)->object;
4273 }
4274 }
4275 }
4276
4277 return ob_arm;
4278 }
4279
obrel_list_test(Object * ob)4280 static bool obrel_list_test(Object *ob)
4281 {
4282 return ob && !(ob->id.tag & LIB_TAG_DOIT);
4283 }
4284
obrel_list_add(LinkNode ** links,Object * ob)4285 static void obrel_list_add(LinkNode **links, Object *ob)
4286 {
4287 BLI_linklist_prepend(links, ob);
4288 ob->id.tag |= LIB_TAG_DOIT;
4289 }
4290
4291 /**
4292 * Iterates over all objects of the given scene layer.
4293 * Depending on the #eObjectSet flag:
4294 * collect either #OB_SET_ALL, #OB_SET_VISIBLE or #OB_SET_SELECTED objects.
4295 * If #OB_SET_VISIBLE or#OB_SET_SELECTED are collected,
4296 * then also add related objects according to the given \a includeFilter.
4297 */
BKE_object_relational_superset(struct ViewLayer * view_layer,eObjectSet objectSet,eObRelationTypes includeFilter)4298 LinkNode *BKE_object_relational_superset(struct ViewLayer *view_layer,
4299 eObjectSet objectSet,
4300 eObRelationTypes includeFilter)
4301 {
4302 LinkNode *links = NULL;
4303
4304 Base *base;
4305
4306 /* Remove markers from all objects */
4307 for (base = view_layer->object_bases.first; base; base = base->next) {
4308 base->object->id.tag &= ~LIB_TAG_DOIT;
4309 }
4310
4311 /* iterate over all selected and visible objects */
4312 for (base = view_layer->object_bases.first; base; base = base->next) {
4313 if (objectSet == OB_SET_ALL) {
4314 /* as we get all anyways just add it */
4315 Object *ob = base->object;
4316 obrel_list_add(&links, ob);
4317 }
4318 else {
4319 if ((objectSet == OB_SET_SELECTED && BASE_SELECTED_EDITABLE(((View3D *)NULL), base)) ||
4320 (objectSet == OB_SET_VISIBLE && BASE_EDITABLE(((View3D *)NULL), base))) {
4321 Object *ob = base->object;
4322
4323 if (obrel_list_test(ob)) {
4324 obrel_list_add(&links, ob);
4325 }
4326
4327 /* parent relationship */
4328 if (includeFilter & (OB_REL_PARENT | OB_REL_PARENT_RECURSIVE)) {
4329 Object *parent = ob->parent;
4330 if (obrel_list_test(parent)) {
4331
4332 obrel_list_add(&links, parent);
4333
4334 /* recursive parent relationship */
4335 if (includeFilter & OB_REL_PARENT_RECURSIVE) {
4336 parent = parent->parent;
4337 while (obrel_list_test(parent)) {
4338
4339 obrel_list_add(&links, parent);
4340 parent = parent->parent;
4341 }
4342 }
4343 }
4344 }
4345
4346 /* child relationship */
4347 if (includeFilter & (OB_REL_CHILDREN | OB_REL_CHILDREN_RECURSIVE)) {
4348 Base *local_base;
4349 for (local_base = view_layer->object_bases.first; local_base;
4350 local_base = local_base->next) {
4351 if (BASE_EDITABLE(((View3D *)NULL), local_base)) {
4352
4353 Object *child = local_base->object;
4354 if (obrel_list_test(child)) {
4355 if ((includeFilter & OB_REL_CHILDREN_RECURSIVE &&
4356 BKE_object_is_child_recursive(ob, child)) ||
4357 (includeFilter & OB_REL_CHILDREN && child->parent && child->parent == ob)) {
4358 obrel_list_add(&links, child);
4359 }
4360 }
4361 }
4362 }
4363 }
4364
4365 /* include related armatures */
4366 if (includeFilter & OB_REL_MOD_ARMATURE) {
4367 Object *arm = obrel_armature_find(ob);
4368 if (obrel_list_test(arm)) {
4369 obrel_list_add(&links, arm);
4370 }
4371 }
4372 }
4373 }
4374 }
4375
4376 return links;
4377 }
4378
4379 /**
4380 * return all groups this object is a part of, caller must free.
4381 */
BKE_object_groups(Main * bmain,Scene * scene,Object * ob)4382 struct LinkNode *BKE_object_groups(Main *bmain, Scene *scene, Object *ob)
4383 {
4384 LinkNode *collection_linknode = NULL;
4385 Collection *collection = NULL;
4386 while ((collection = BKE_collection_object_find(bmain, scene, collection, ob))) {
4387 BLI_linklist_prepend(&collection_linknode, collection);
4388 }
4389
4390 return collection_linknode;
4391 }
4392
BKE_object_groups_clear(Main * bmain,Scene * scene,Object * ob)4393 void BKE_object_groups_clear(Main *bmain, Scene *scene, Object *ob)
4394 {
4395 Collection *collection = NULL;
4396 while ((collection = BKE_collection_object_find(bmain, scene, collection, ob))) {
4397 BKE_collection_object_remove(bmain, collection, ob, false);
4398 DEG_id_tag_update(&collection->id, ID_RECALC_COPY_ON_WRITE);
4399 }
4400 }
4401
4402 /**
4403 * Return a KDTree_3d from the deformed object (in worldspace)
4404 *
4405 * \note Only mesh objects currently support deforming, others are TODO.
4406 *
4407 * \param ob:
4408 * \param r_tot:
4409 * \return The kdtree or NULL if it can't be created.
4410 */
BKE_object_as_kdtree(Object * ob,int * r_tot)4411 KDTree_3d *BKE_object_as_kdtree(Object *ob, int *r_tot)
4412 {
4413 KDTree_3d *tree = NULL;
4414 unsigned int tot = 0;
4415
4416 switch (ob->type) {
4417 case OB_MESH: {
4418 Mesh *me = ob->data;
4419 unsigned int i;
4420
4421 Mesh *me_eval = ob->runtime.mesh_deform_eval ? ob->runtime.mesh_deform_eval :
4422 ob->runtime.mesh_deform_eval;
4423 const int *index;
4424
4425 if (me_eval && (index = CustomData_get_layer(&me_eval->vdata, CD_ORIGINDEX))) {
4426 MVert *mvert = me_eval->mvert;
4427 uint totvert = me_eval->totvert;
4428
4429 /* tree over-allocs in case where some verts have ORIGINDEX_NONE */
4430 tot = 0;
4431 tree = BLI_kdtree_3d_new(totvert);
4432
4433 /* we don't how how many verts from the DM we can use */
4434 for (i = 0; i < totvert; i++) {
4435 if (index[i] != ORIGINDEX_NONE) {
4436 float co[3];
4437 mul_v3_m4v3(co, ob->obmat, mvert[i].co);
4438 BLI_kdtree_3d_insert(tree, index[i], co);
4439 tot++;
4440 }
4441 }
4442 }
4443 else {
4444 MVert *mvert = me->mvert;
4445
4446 tot = me->totvert;
4447 tree = BLI_kdtree_3d_new(tot);
4448
4449 for (i = 0; i < tot; i++) {
4450 float co[3];
4451 mul_v3_m4v3(co, ob->obmat, mvert[i].co);
4452 BLI_kdtree_3d_insert(tree, i, co);
4453 }
4454 }
4455
4456 BLI_kdtree_3d_balance(tree);
4457 break;
4458 }
4459 case OB_CURVE:
4460 case OB_SURF: {
4461 /* TODO: take deformation into account */
4462 Curve *cu = ob->data;
4463 unsigned int i, a;
4464
4465 Nurb *nu;
4466
4467 tot = BKE_nurbList_verts_count_without_handles(&cu->nurb);
4468 tree = BLI_kdtree_3d_new(tot);
4469 i = 0;
4470
4471 nu = cu->nurb.first;
4472 while (nu) {
4473 if (nu->bezt) {
4474 BezTriple *bezt;
4475
4476 bezt = nu->bezt;
4477 a = nu->pntsu;
4478 while (a--) {
4479 float co[3];
4480 mul_v3_m4v3(co, ob->obmat, bezt->vec[1]);
4481 BLI_kdtree_3d_insert(tree, i++, co);
4482 bezt++;
4483 }
4484 }
4485 else {
4486 BPoint *bp;
4487
4488 bp = nu->bp;
4489 a = nu->pntsu * nu->pntsv;
4490 while (a--) {
4491 float co[3];
4492 mul_v3_m4v3(co, ob->obmat, bp->vec);
4493 BLI_kdtree_3d_insert(tree, i++, co);
4494 bp++;
4495 }
4496 }
4497 nu = nu->next;
4498 }
4499
4500 BLI_kdtree_3d_balance(tree);
4501 break;
4502 }
4503 case OB_LATTICE: {
4504 /* TODO: take deformation into account */
4505 Lattice *lt = ob->data;
4506 BPoint *bp;
4507 unsigned int i;
4508
4509 tot = lt->pntsu * lt->pntsv * lt->pntsw;
4510 tree = BLI_kdtree_3d_new(tot);
4511 i = 0;
4512
4513 for (bp = lt->def; i < tot; bp++) {
4514 float co[3];
4515 mul_v3_m4v3(co, ob->obmat, bp->vec);
4516 BLI_kdtree_3d_insert(tree, i++, co);
4517 }
4518
4519 BLI_kdtree_3d_balance(tree);
4520 break;
4521 }
4522 }
4523
4524 *r_tot = tot;
4525 return tree;
4526 }
4527
BKE_object_modifier_use_time(Object * ob,ModifierData * md)4528 bool BKE_object_modifier_use_time(Object *ob, ModifierData *md)
4529 {
4530 if (BKE_modifier_depends_ontime(md)) {
4531 return true;
4532 }
4533
4534 /* Check whether modifier is animated. */
4535 /* TODO: this should be handled as part of build_animdata() -- Aligorith */
4536 if (ob->adt) {
4537 AnimData *adt = ob->adt;
4538 FCurve *fcu;
4539
4540 char pattern[MAX_NAME + 16];
4541 BLI_snprintf(pattern, sizeof(pattern), "modifiers[\"%s\"]", md->name);
4542
4543 /* action - check for F-Curves with paths containing 'modifiers[' */
4544 if (adt->action) {
4545 for (fcu = (FCurve *)adt->action->curves.first; fcu != NULL; fcu = (FCurve *)fcu->next) {
4546 if (fcu->rna_path && strstr(fcu->rna_path, pattern)) {
4547 return true;
4548 }
4549 }
4550 }
4551
4552 /* This here allows modifier properties to get driven and still update properly
4553 *
4554 * Workaround to get T26764 (e.g. subsurf levels not updating when animated/driven)
4555 * working, without the updating problems (T28525 T28690 T28774 T28777) caused
4556 * by the RNA updates cache introduced in r.38649
4557 */
4558 for (fcu = (FCurve *)adt->drivers.first; fcu != NULL; fcu = (FCurve *)fcu->next) {
4559 if (fcu->rna_path && strstr(fcu->rna_path, pattern)) {
4560 return true;
4561 }
4562 }
4563
4564 /* XXX: also, should check NLA strips, though for now assume that nobody uses
4565 * that and we can omit that for performance reasons... */
4566 }
4567
4568 return false;
4569 }
4570
BKE_object_modifier_gpencil_use_time(Object * ob,GpencilModifierData * md)4571 bool BKE_object_modifier_gpencil_use_time(Object *ob, GpencilModifierData *md)
4572 {
4573 if (BKE_gpencil_modifier_depends_ontime(md)) {
4574 return true;
4575 }
4576
4577 /* Check whether modifier is animated. */
4578 /* TODO(Aligorith): this should be handled as part of build_animdata() */
4579 if (ob->adt) {
4580 AnimData *adt = ob->adt;
4581 FCurve *fcu;
4582
4583 char pattern[MAX_NAME + 32];
4584 BLI_snprintf(pattern, sizeof(pattern), "grease_pencil_modifiers[\"%s\"]", md->name);
4585
4586 /* action - check for F-Curves with paths containing 'grease_pencil_modifiers[' */
4587 if (adt->action) {
4588 for (fcu = adt->action->curves.first; fcu != NULL; fcu = fcu->next) {
4589 if (fcu->rna_path && strstr(fcu->rna_path, pattern)) {
4590 return true;
4591 }
4592 }
4593 }
4594
4595 /* This here allows modifier properties to get driven and still update properly */
4596 for (fcu = adt->drivers.first; fcu != NULL; fcu = fcu->next) {
4597 if (fcu->rna_path && strstr(fcu->rna_path, pattern)) {
4598 return true;
4599 }
4600 }
4601 }
4602
4603 return false;
4604 }
4605
BKE_object_shaderfx_use_time(Object * ob,ShaderFxData * fx)4606 bool BKE_object_shaderfx_use_time(Object *ob, ShaderFxData *fx)
4607 {
4608 if (BKE_shaderfx_depends_ontime(fx)) {
4609 return true;
4610 }
4611
4612 /* Check whether effect is animated. */
4613 /* TODO(Aligorith): this should be handled as part of build_animdata() */
4614 if (ob->adt) {
4615 AnimData *adt = ob->adt;
4616 FCurve *fcu;
4617
4618 char pattern[MAX_NAME + 32];
4619 BLI_snprintf(pattern, sizeof(pattern), "shader_effects[\"%s\"]", fx->name);
4620
4621 /* action - check for F-Curves with paths containing string[' */
4622 if (adt->action) {
4623 for (fcu = adt->action->curves.first; fcu != NULL; fcu = fcu->next) {
4624 if (fcu->rna_path && strstr(fcu->rna_path, pattern)) {
4625 return true;
4626 }
4627 }
4628 }
4629
4630 /* This here allows properties to get driven and still update properly */
4631 for (fcu = adt->drivers.first; fcu != NULL; fcu = fcu->next) {
4632 if (fcu->rna_path && strstr(fcu->rna_path, pattern)) {
4633 return true;
4634 }
4635 }
4636 }
4637
4638 return false;
4639 }
4640
4641 /**
4642 * Set "ignore cache" flag for all caches on this object.
4643 */
object_cacheIgnoreClear(Object * ob,int state)4644 static void object_cacheIgnoreClear(Object *ob, int state)
4645 {
4646 ListBase pidlist;
4647 PTCacheID *pid;
4648 BKE_ptcache_ids_from_object(&pidlist, ob, NULL, 0);
4649
4650 for (pid = pidlist.first; pid; pid = pid->next) {
4651 if (pid->cache) {
4652 if (state) {
4653 pid->cache->flag |= PTCACHE_IGNORE_CLEAR;
4654 }
4655 else {
4656 pid->cache->flag &= ~PTCACHE_IGNORE_CLEAR;
4657 }
4658 }
4659 }
4660
4661 BLI_freelistN(&pidlist);
4662 }
4663
4664 /**
4665 * \note this function should eventually be replaced by depsgraph functionality.
4666 * Avoid calling this in new code unless there is a very good reason for it!
4667 */
BKE_object_modifier_update_subframe(Depsgraph * depsgraph,Scene * scene,Object * ob,bool update_mesh,int parent_recursion,float frame,int type)4668 bool BKE_object_modifier_update_subframe(Depsgraph *depsgraph,
4669 Scene *scene,
4670 Object *ob,
4671 bool update_mesh,
4672 int parent_recursion,
4673 float frame,
4674 int type)
4675 {
4676 const bool flush_to_original = DEG_is_active(depsgraph);
4677 ModifierData *md = BKE_modifiers_findby_type(ob, (ModifierType)type);
4678 bConstraint *con;
4679
4680 if (type == eModifierType_DynamicPaint) {
4681 DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
4682
4683 /* if other is dynamic paint canvas, don't update */
4684 if (pmd && pmd->canvas) {
4685 return true;
4686 }
4687 }
4688 else if (type == eModifierType_Fluid) {
4689 FluidModifierData *fmd = (FluidModifierData *)md;
4690
4691 if (fmd && (fmd->type & MOD_FLUID_TYPE_DOMAIN) != 0) {
4692 return true;
4693 }
4694 }
4695
4696 /* if object has parents, update them too */
4697 if (parent_recursion) {
4698 int recursion = parent_recursion - 1;
4699 bool no_update = false;
4700 if (ob->parent) {
4701 no_update |= BKE_object_modifier_update_subframe(
4702 depsgraph, scene, ob->parent, 0, recursion, frame, type);
4703 }
4704 if (ob->track) {
4705 no_update |= BKE_object_modifier_update_subframe(
4706 depsgraph, scene, ob->track, 0, recursion, frame, type);
4707 }
4708
4709 /* skip subframe if object is parented
4710 * to vertex of a dynamic paint canvas */
4711 if (no_update && (ob->partype == PARVERT1 || ob->partype == PARVERT3)) {
4712 return false;
4713 }
4714
4715 /* also update constraint targets */
4716 for (con = ob->constraints.first; con; con = con->next) {
4717 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
4718 ListBase targets = {NULL, NULL};
4719
4720 if (cti && cti->get_constraint_targets) {
4721 bConstraintTarget *ct;
4722 cti->get_constraint_targets(con, &targets);
4723 for (ct = targets.first; ct; ct = ct->next) {
4724 if (ct->tar) {
4725 BKE_object_modifier_update_subframe(
4726 depsgraph, scene, ct->tar, 0, recursion, frame, type);
4727 }
4728 }
4729 /* free temp targets */
4730 if (cti->flush_constraint_targets) {
4731 cti->flush_constraint_targets(con, &targets, 0);
4732 }
4733 }
4734 }
4735 }
4736
4737 /* was originally ID_RECALC_ALL - TODO - which flags are really needed??? */
4738 /* TODO(sergey): What about animation? */
4739 const AnimationEvalContext anim_eval_context = BKE_animsys_eval_context_construct(depsgraph,
4740 frame);
4741
4742 ob->id.recalc |= ID_RECALC_ALL;
4743 if (update_mesh) {
4744 BKE_animsys_evaluate_animdata(
4745 &ob->id, ob->adt, &anim_eval_context, ADT_RECALC_ANIM, flush_to_original);
4746 /* ignore cache clear during subframe updates
4747 * to not mess up cache validity */
4748 object_cacheIgnoreClear(ob, 1);
4749 BKE_object_handle_update(depsgraph, scene, ob);
4750 object_cacheIgnoreClear(ob, 0);
4751 }
4752 else {
4753 BKE_object_where_is_calc_time(depsgraph, scene, ob, frame);
4754 }
4755
4756 /* for curve following objects, parented curve has to be updated too */
4757 if (ob->type == OB_CURVE) {
4758 Curve *cu = ob->data;
4759 BKE_animsys_evaluate_animdata(
4760 &cu->id, cu->adt, &anim_eval_context, ADT_RECALC_ANIM, flush_to_original);
4761 }
4762 /* and armatures... */
4763 if (ob->type == OB_ARMATURE) {
4764 bArmature *arm = ob->data;
4765 BKE_animsys_evaluate_animdata(
4766 &arm->id, arm->adt, &anim_eval_context, ADT_RECALC_ANIM, flush_to_original);
4767 BKE_pose_where_is(depsgraph, scene, ob);
4768 }
4769
4770 return false;
4771 }
4772
4773 /**
4774 * Updates select_id of all objects in the given \a bmain.
4775 */
BKE_object_update_select_id(struct Main * bmain)4776 void BKE_object_update_select_id(struct Main *bmain)
4777 {
4778 Object *ob = bmain->objects.first;
4779 int select_id = 1;
4780 while (ob) {
4781 ob->runtime.select_id = select_id++;
4782 ob = ob->id.next;
4783 }
4784 }
4785
BKE_object_to_mesh(Depsgraph * depsgraph,Object * object,bool preserve_all_data_layers)4786 Mesh *BKE_object_to_mesh(Depsgraph *depsgraph, Object *object, bool preserve_all_data_layers)
4787 {
4788 BKE_object_to_mesh_clear(object);
4789
4790 Mesh *mesh = BKE_mesh_new_from_object(depsgraph, object, preserve_all_data_layers);
4791 object->runtime.object_as_temp_mesh = mesh;
4792 return mesh;
4793 }
4794
BKE_object_to_mesh_clear(Object * object)4795 void BKE_object_to_mesh_clear(Object *object)
4796 {
4797 if (object->runtime.object_as_temp_mesh == NULL) {
4798 return;
4799 }
4800 BKE_id_free(NULL, object->runtime.object_as_temp_mesh);
4801 object->runtime.object_as_temp_mesh = NULL;
4802 }
4803
BKE_object_check_uuids_unique_and_report(const Object * object)4804 void BKE_object_check_uuids_unique_and_report(const Object *object)
4805 {
4806 BKE_pose_check_uuids_unique_and_report(object->pose);
4807 BKE_modifier_check_uuids_unique_and_report(object);
4808 }
4809