Blender V5.3
object_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10#include <cstring>
11
12#include "DNA_armature_types.h"
13#include "DNA_curve_types.h"
14#include "DNA_lattice_types.h"
15#include "DNA_meta_types.h"
16#include "DNA_object_types.h"
17
18#include "BLI_listbase.hh"
19#include "BLI_math_matrix_c.hh"
20#include "BLI_math_vector_c.hh"
21
22#include "BKE_action.hh"
23#include "BKE_armature.hh"
24#include "BKE_editmesh.hh"
25#include "BKE_lattice.hh"
26#include "BKE_layer.hh"
27#include "BKE_object.hh"
28#include "BKE_scene.hh"
29
31
32#include "ED_curve.hh"
33#include "ED_object.hh" /* own include */
34
35#include "WM_api.hh"
36
37#include "MEM_guardedalloc.h"
38
39namespace blender::ed::object {
40
41/* -------------------------------------------------------------------- */
44
46{
47 if (ob->totcol > 0) {
48 const short actcol_test = std::clamp(index + 1, 1, ob->totcol);
49 if (ob->actcol != actcol_test) {
50 ob->actcol = actcol_test;
52 return true;
53 }
54 }
55 return false;
56}
57
59
60/* -------------------------------------------------------------------- */
63
64bool calc_active_center_for_editmode(Object *obedit, const bool select_only, float r_center[3])
65{
66 switch (obedit->type) {
67 case OB_MESH: {
70
72 BM_editselection_center(&ese, r_center);
73 return true;
74 }
75 break;
76 }
77 case OB_ARMATURE: {
78 bArmature *arm = id_cast<bArmature *>(obedit->data);
79 EditBone *ebo = arm->act_edbone;
80
81 if (ebo && (!select_only || (ebo->flag & (BONE_SELECTED | BONE_ROOTSEL)))) {
82 copy_v3_v3(r_center, ebo->head);
83 return true;
84 }
85
86 break;
87 }
89 case OB_SURF: {
90 Curve *cu = id_cast<Curve *>(obedit->data);
91
92 if (ED_curve_active_center(cu, r_center)) {
93 return true;
94 }
95 break;
96 }
97 case OB_MBALL: {
98 MetaBall *mb = id_cast<MetaBall *>(obedit->data);
99 MetaElem *ml_act = mb->lastelem;
100
101 if (ml_act && (!select_only || (ml_act->flag & SELECT))) {
102 copy_v3_v3(r_center, &ml_act->x);
103 return true;
104 }
105 break;
106 }
107 case OB_LATTICE: {
108 BPoint *actbp = BKE_lattice_active_point_get(id_cast<Lattice *>(obedit->data));
109
110 if (actbp) {
111 copy_v3_v3(r_center, actbp->vec);
112 return true;
113 }
114 break;
115 }
116 case OB_GREASE_PENCIL: {
117 copy_v3_v3(r_center, obedit->loc);
118 mul_m4_v3(obedit->world_to_object().ptr(), r_center);
119 return true;
120 }
121 default:
122 break;
123 }
124
125 return false;
126}
127
128bool calc_active_center_for_posemode(Object *ob, const bool select_only, float r_center[3])
129{
131 if (pchan && (!select_only || (pchan->flag & POSE_SELECTED))) {
132 const bArmature *arm = id_cast<bArmature *>(ob->data);
133 BKE_pose_channel_transform_location(arm, pchan, r_center);
134 return true;
135 }
136 return false;
137}
138
139bool calc_active_center(Object *ob, const bool select_only, float r_center[3])
140{
141 if (ob->mode & OB_MODE_EDIT) {
142 if (calc_active_center_for_editmode(ob, select_only, r_center)) {
143 mul_m4_v3(ob->object_to_world().ptr(), r_center);
144 return true;
145 }
146 return false;
147 }
148 if (ob->mode & OB_MODE_POSE) {
149 if (calc_active_center_for_posemode(ob, select_only, r_center)) {
150 mul_m4_v3(ob->object_to_world().ptr(), r_center);
151 return true;
152 }
153 return false;
154 }
155 if (!select_only || (ob->base_flag & BASE_SELECTED)) {
156 copy_v3_v3(r_center, ob->object_to_world().location());
157 return true;
158 }
159 return false;
160}
161
163
164/* -------------------------------------------------------------------- */
173
175 float obmat_orig[4][4] = {};
176 float parent_obmat_orig[4][4] = {};
177 float parent_obmat_inv_orig[4][4] = {};
179 float parentinv_orig[4][4] = {};
182};
183
187
189{
190 XFormObjectSkipChild_Container *xcs = MEM_new<XFormObjectSkipChild_Container>(__func__);
191 return xcs;
192}
193
195 const Main &bmain,
196 const Scene *scene,
197 ViewLayer *view_layer,
198 Object **objects,
199 uint objects_len)
200{
201 Set<Object *> objects_in_transdata(Span(objects, objects_len));
202 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
203 ListBaseT<Base> *object_bases = BKE_view_layer_object_bases_get(view_layer);
204 for (Base &base : *object_bases) {
205 Object *ob = base.object;
206 if (ob->parent != nullptr) {
207 if (!objects_in_transdata.contains(ob)) {
208 if (objects_in_transdata.contains(ob->parent)) {
210 xcs, ob, nullptr, XFORM_OB_SKIP_CHILD_PARENT_IS_XFORM);
211 }
212 }
213 else {
214 if (!objects_in_transdata.contains(ob->parent)) {
215 Object *ob_parent_recurse = ob->parent;
216 if (ob_parent_recurse != nullptr) {
217 while (ob_parent_recurse != nullptr) {
218 if (objects_in_transdata.contains(ob_parent_recurse)) {
219 break;
220 }
221 ob_parent_recurse = ob_parent_recurse->parent;
222 }
223
224 if (ob_parent_recurse) {
226 xcs, ob, ob_parent_recurse, XFORM_OB_SKIP_CHILD_PARENT_APPLY);
227 }
228 }
229 }
230 }
231 }
232 }
233
234 for (Base &base : *object_bases) {
235 Object *ob = base.object;
236
237 if (objects_in_transdata.contains(ob)) {
238 /* pass. */
239 }
240 else if (ob->parent != nullptr) {
241 if (objects_in_transdata.contains(ob->parent)) {
242 if (!objects_in_transdata.contains(ob)) {
244 xcs, ob, nullptr, XFORM_OB_SKIP_CHILD_PARENT_IS_XFORM);
245 }
246 }
247 }
248 }
249}
250
255
257 Object *ob,
258 Object *ob_parent_recurse,
259 int mode)
260{
261 xcs->obchild_in_obmode_map.lookup_or_add_cb(ob, [&]() {
262 std::unique_ptr<XFormObjectSkipChild> xf = std::make_unique<XFormObjectSkipChild>();
263 copy_m4_m4(xf->parentinv_orig, ob->parentinv);
264 copy_m4_m4(xf->obmat_orig, ob->object_to_world().ptr());
265 copy_m4_m4(xf->parent_obmat_orig, ob->parent->object_to_world().ptr());
266 invert_m4_m4(xf->parent_obmat_inv_orig, ob->parent->object_to_world().ptr());
267 if (ob_parent_recurse) {
268 copy_m4_m4(xf->parent_recurse_obmat_orig, ob_parent_recurse->object_to_world().ptr());
269 }
270 xf->mode = mode;
271 xf->ob_parent_recurse = ob_parent_recurse;
272 return xf;
273 });
274}
275
277 Main *bmain,
278 Depsgraph *depsgraph)
279{
280 BKE_scene_graph_evaluated_ensure(depsgraph, bmain);
281
282 for (auto item : xcs->obchild_in_obmode_map.items()) {
283 Object *ob = item.key;
284 XFormObjectSkipChild *xf = item.value.get();
285
286 /* The following blocks below assign 'dmat'. */
287 float dmat[4][4];
288
290 /* Parent is transformed, this isn't so compensate. */
291 Object *ob_parent_eval = DEG_get_evaluated(depsgraph, ob->parent);
292 mul_m4_m4m4(dmat, xf->parent_obmat_inv_orig, ob_parent_eval->object_to_world().ptr());
293 invert_m4(dmat);
294 }
296 /* Calculate parent matrix (from the root transform). */
297 Object *ob_parent_recurse_eval = DEG_get_evaluated(depsgraph, xf->ob_parent_recurse);
298 float parent_recurse_obmat_inv[4][4];
299 invert_m4_m4(parent_recurse_obmat_inv, ob_parent_recurse_eval->object_to_world().ptr());
300 mul_m4_m4m4(dmat, xf->parent_recurse_obmat_orig, parent_recurse_obmat_inv);
301 invert_m4(dmat);
302 float parent_obmat_calc[4][4];
303 mul_m4_m4m4(parent_obmat_calc, dmat, xf->parent_obmat_orig);
304
305 /* Apply to the parent inverse matrix. */
306 mul_m4_m4m4(dmat, xf->parent_obmat_inv_orig, parent_obmat_calc);
307 invert_m4(dmat);
308 }
309 else {
311 /* Transform this - without transform data. */
312 Object *ob_parent_recurse_eval = DEG_get_evaluated(depsgraph, xf->ob_parent_recurse);
313 float parent_recurse_obmat_inv[4][4];
314 invert_m4_m4(parent_recurse_obmat_inv, ob_parent_recurse_eval->object_to_world().ptr());
315 mul_m4_m4m4(dmat, xf->parent_recurse_obmat_orig, parent_recurse_obmat_inv);
316 invert_m4(dmat);
317 float obmat_calc[4][4];
318 mul_m4_m4m4(obmat_calc, dmat, xf->obmat_orig);
319 /* obmat_calc is just obmat. */
320
321 /* Get the matrices relative to the parent. */
322 float obmat_parent_relative_orig[4][4];
323 float obmat_parent_relative_calc[4][4];
324 float obmat_parent_relative_inv_orig[4][4];
325
326 mul_m4_m4m4(obmat_parent_relative_orig, xf->parent_obmat_inv_orig, xf->obmat_orig);
327 mul_m4_m4m4(obmat_parent_relative_calc, xf->parent_obmat_inv_orig, obmat_calc);
328 invert_m4_m4(obmat_parent_relative_inv_orig, obmat_parent_relative_orig);
329
330 /* Apply to the parent inverse matrix. */
331 mul_m4_m4m4(dmat, obmat_parent_relative_calc, obmat_parent_relative_inv_orig);
332 }
333
334 mul_m4_m4m4(ob->parentinv, dmat, xf->parentinv_orig);
335
337 }
338}
339
341
342/* -------------------------------------------------------------------- */
348
350 Object *ob = nullptr;
351 float obmat_orig[4][4] = {};
352 std::unique_ptr<XFormObjectData> xod;
353};
354
358
360{
361 xds->obdata_in_obmode_map.lookup_or_add_cb(ob->data, [&]() {
362 auto xf = std::make_unique<XFormObjectData_Extra>();
363 copy_m4_m4(xf->obmat_orig, ob->object_to_world().ptr());
364 xf->ob = ob;
365 /* Result may be nullptr, that's OK. */
366 xf->xod = data_xform_create(ob->data);
367 return xf;
368 });
369}
370
372 Main *bmain,
373 Depsgraph *depsgraph)
374{
375 if (xds->obdata_in_obmode_map.is_empty()) {
376 return;
377 }
378 BKE_scene_graph_evaluated_ensure(depsgraph, bmain);
379
380 for (const auto &item : xds->obdata_in_obmode_map.items()) {
381 ID *id = item.key;
382 XFormObjectData_Extra *xf = item.value.get();
383
384 if (!xf->xod) {
385 continue;
386 }
387
388 Object *ob_eval = DEG_get_evaluated(depsgraph, xf->ob);
389 float4x4 imat, dmat;
390 invert_m4_m4(imat.ptr(), xf->obmat_orig);
391 mul_m4_m4m4(dmat.ptr(), imat.ptr(), ob_eval->object_to_world().ptr());
392 invert_m4(dmat.ptr());
393
394 data_xform_by_mat4(*xf->xod, dmat);
395 if (xf->ob->type == OB_ARMATURE) {
396 /* TODO: none of the current flags properly update armatures, needs investigation. */
397 DEG_id_tag_update(id, 0);
398 }
399 else {
401 }
402 }
403}
404
406{
407 return MEM_new<XFormObjectData_Container>(__func__);
408}
409
411{
412 MEM_delete(xds);
413}
414
416
417/* -------------------------------------------------------------------- */
423
424static bool object_parent_in_set(const Set<Object *> &objects_set, Object *ob)
425{
426 for (Object *parent = ob->parent; parent; parent = parent->parent) {
427 if (objects_set.contains(parent)) {
428 return true;
429 }
430 }
431 return false;
432}
433
434void object_xform_array_m4(Object **objects, uint objects_len, const float matrix[4][4])
435{
436 /* Filter out objects that have parents in `objects_set`. */
437 {
438 Set<Object *> objects_set(Span(objects, objects_len));
439 for (uint i = 0; i < objects_len;) {
440 if (object_parent_in_set(objects_set, objects[i])) {
441 objects[i] = objects[--objects_len];
442 }
443 else {
444 i++;
445 }
446 }
447 }
448
449 /* Detect translation only matrix, prevent rotation/scale channels from being touched at all. */
450 bool is_translation_only;
451 {
452 float test_m4_a[4][4], test_m4_b[4][4];
453 unit_m4(test_m4_a);
454 copy_m4_m4(test_m4_b, matrix);
455 zero_v3(test_m4_b[3]);
456 is_translation_only = equals_m4m4(test_m4_a, test_m4_b);
457 }
458
459 if (is_translation_only) {
460 for (uint i = 0; i < objects_len; i++) {
461 Object *ob = objects[i];
462 add_v3_v3(ob->loc, matrix[3]);
464 }
465 }
466 else {
467 for (uint i = 0; i < objects_len; i++) {
468 float m4[4][4];
469 Object *ob = objects[i];
470 BKE_object_to_mat4(ob, m4);
471 mul_m4_m4m4(m4, matrix, m4);
472 BKE_object_apply_mat4(ob, m4, true, true);
474 }
475 }
476}
477
479
480} // namespace blender::ed::object
Blender kernel action and pose functionality.
#define SELECT
Definition BKE_mask.hh:19
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define NC_MATERIAL
Definition WM_types.hh:393
#define ND_SHADING_LINKS
Definition WM_types.hh:522
bool contains(const Key &key) const
Definition BLI_set.hh:310
constexpr Span()=default
bool calc_active_center(Object *ob, bool select_only, float r_center[3])
static bool object_parent_in_set(const Set< Object * > &objects_set, Object *ob)
@ XFORM_OB_SKIP_CHILD_PARENT_IS_XFORM
Definition ED_object.hh:147
@ XFORM_OB_SKIP_CHILD_PARENT_IS_XFORM_INDIRECT
Definition ED_object.hh:152
void object_xform_array_m4(Object **objects, uint objects_len, const float matrix[4][4])
void data_xform_container_destroy(XFormObjectData_Container *xds)
XFormObjectSkipChild_Container * xform_skip_child_container_create()
void data_xform_container_update_all(XFormObjectData_Container *xds, Main *bmain, Depsgraph *depsgraph)
void data_xform_by_mat4(XFormObjectData &xod, const float4x4 &transform)
void object_xform_skip_child_container_destroy(XFormObjectSkipChild_Container *xcs)
void object_xform_skip_child_container_item_ensure(XFormObjectSkipChild_Container *xcs, Object *ob, Object *ob_parent_recurse, int mode)
bool calc_active_center_for_editmode(Object *obedit, bool select_only, float r_center[3])
bool material_active_index_set(Object *ob, int index)
bool calc_active_center_for_posemode(Object *ob, bool select_only, float r_center[3])
void data_xform_container_item_ensure(XFormObjectData_Container *xds, Object *ob)
void object_xform_skip_child_container_update_all(XFormObjectSkipChild_Container *xcs, Main *bmain, Depsgraph *depsgraph)
void xform_skip_child_container_item_ensure_from_array(XFormObjectSkipChild_Container *xcs, const Main &bmain, const Scene *scene, ViewLayer *view_layer, Object **objects, uint objects_len)
XFormObjectData_Container * data_xform_container_create()
void WM_main_add_notifier(uint type, void *reference)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void BM_editselection_center(BMEditSelection *ese, float r_center[3])
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
bool invert_m4(float mat[4][4])
MatBase< float, 4, 4 > float4x4
void BKE_object_apply_mat4(Object *ob, const float mat[4][4], bool use_compat, bool use_parent)
Definition object.cc:3663
void BKE_scene_graph_evaluated_ensure(Depsgraph *depsgraph, Main *bmain)
Definition scene.cc:2872
void mul_m4_v3(const float M[4][4], float r[3])
void BKE_pose_channel_transform_location(const bArmature *arm, const bPoseChannel *pose_bone, float r_pose_space_pivot[3])
void BKE_object_to_mat4(const Object *ob, float r_mat[4][4])
Definition object.cc:3125
void DEG_id_tag_update(ID *id, unsigned int flags)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
bool equals_m4m4(const float mat1[4][4], const float mat2[4][4])
void unit_m4(float m[4][4])
BPoint * BKE_lattice_active_point_get(Lattice *lt)
Definition lattice.cc:590
MINLINE void zero_v3(float r[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
bPoseChannel * BKE_pose_channel_active_if_bonecoll_visible(Object *ob) ATTR_WARN_UNUSED_RESULT
void copy_m4_m4(float m1[4][4], const float m2[4][4])
bool BM_select_history_active_get(BMesh *bm, BMEditSelection *ese)
ListBaseT< Base > * BKE_view_layer_object_bases_get(ViewLayer *view_layer)
bool ED_curve_active_center(Curve *cu, float center[3])
const c_style_mat & ptr() const
eMetaElem_Flag flag
struct Object * parent
float parentinv[4][4]
struct EditBone * act_edbone
Map< ID *, std::unique_ptr< XFormObjectData_Extra > > obdata_in_obmode_map
std::unique_ptr< XFormObjectData > xod
Map< Object *, std::unique_ptr< XFormObjectSkipChild > > obchild_in_obmode_map