From cab4d6f724af701239cf2d00f44ca94fca623c1f Mon Sep 17 00:00:00 2001 From: Sean Kim Date: Mon, 12 Aug 2024 18:25:45 +0200 Subject: [PATCH] Refactor: Restructure sculpt shape key updates Pull Request: https://projects.blender.org/blender/blender/pulls/126161 --- .../editors/sculpt_paint/mesh_brush_common.hh | 10 +++-- source/blender/editors/sculpt_paint/sculpt.cc | 40 +++++++++---------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/source/blender/editors/sculpt_paint/mesh_brush_common.hh b/source/blender/editors/sculpt_paint/mesh_brush_common.hh index 64b349df879..754e3e9f0b8 100644 --- a/source/blender/editors/sculpt_paint/mesh_brush_common.hh +++ b/source/blender/editors/sculpt_paint/mesh_brush_common.hh @@ -383,10 +383,12 @@ void clip_and_lock_translations(const Sculpt &sd, * shape key positions must be kept in sync, and shape keys dependent on the active key must also * be modified. */ -void apply_translations_to_shape_keys(Object &object, - Span verts, - Span translations, - MutableSpan positions_mesh); +void update_shape_keys(Object &object, + const Mesh &mesh, + const KeyBlock &active_key, + Span verts, + Span translations, + Span positions_orig); /** * Currently the pbvh::Tree owns its own copy of deformed positions that needs to be updated to diff --git a/source/blender/editors/sculpt_paint/sculpt.cc b/source/blender/editors/sculpt_paint/sculpt.cc index a23d1fa0b82..bfc1ae01c2f 100644 --- a/source/blender/editors/sculpt_paint/sculpt.cc +++ b/source/blender/editors/sculpt_paint/sculpt.cc @@ -1493,9 +1493,9 @@ void restore_position_from_undo_step(Object &object) } } - if (BKE_keyblock_from_object(&object)) { - /* Update dependent shape keys back to their original */ - apply_translations_to_shape_keys(object, verts, tls.translations, positions_orig); + if (const KeyBlock *active_key = BKE_keyblock_from_object(&object)) { + update_shape_keys( + object, mesh, *active_key, verts, tls.translations, positions_orig); } BKE_pbvh_node_mark_positions_update(node); @@ -7246,19 +7246,15 @@ void clip_and_lock_translations(const Sculpt &sd, } } -void apply_translations_to_shape_keys(Object &object, - const Span verts, - const Span translations, - const MutableSpan positions_orig) +void update_shape_keys(Object &object, + const Mesh &mesh, + const KeyBlock &active_key, + const Span verts, + const Span translations, + const Span positions_orig) { - Mesh &mesh = *static_cast(object.data); - KeyBlock *active_key = BKE_keyblock_from_object(&object); - if (!active_key) { - return; - } - - MutableSpan active_key_data(static_cast(active_key->data), active_key->totelem); - if (active_key == mesh.key->refkey) { + const MutableSpan active_key_data(static_cast(active_key.data), active_key.totelem); + if (&active_key == mesh.key->refkey) { for (const int vert : verts) { active_key_data[vert] = positions_orig[vert]; } @@ -7267,11 +7263,10 @@ void apply_translations_to_shape_keys(Object &object, apply_translations(translations, verts, active_key_data); } - /* For relative keys editing of base should update other keys. */ if (bool *dependent = BKE_keyblock_get_dependent_keys(mesh.key, object.shapenr - 1)) { int i; LISTBASE_FOREACH_INDEX (KeyBlock *, other_key, &mesh.key->block, i) { - if ((other_key != active_key) && dependent[i]) { + if ((other_key != &active_key) && dependent[i]) { MutableSpan data(static_cast(other_key->data), other_key->totelem); apply_translations(translations, verts, data); } @@ -7313,12 +7308,17 @@ void write_translations(const Sculpt &sd, const Mesh &mesh = *static_cast(object.data); const KeyBlock *active_key = BKE_keyblock_from_object(&object); - const bool relative_shapekey_active = active_key != nullptr && active_key != mesh.key->refkey; - if (!relative_shapekey_active) { + if (!active_key) { + apply_translations(translations, verts, positions_orig); + return; + } + + const bool basis_shapekey_active = active_key == mesh.key->refkey; + if (basis_shapekey_active) { apply_translations(translations, verts, positions_orig); } - apply_translations_to_shape_keys(object, verts, translations, positions_orig); + update_shape_keys(object, mesh, *active_key, verts, translations, positions_orig); } void scale_translations(const MutableSpan translations, const Span factors)