From b3fe97414ae7133d38e7416dc4df8f77488bfa96 Mon Sep 17 00:00:00 2001 From: Pratik Borhade Date: Wed, 27 Mar 2024 12:28:16 +0100 Subject: [PATCH] Fix: Sculpt paint crash after converting active color attribute Painting after converting active color attribute in sculpt mode causes crash. Fix is same as 3641b4b884ab4b117cab3c800d16b2ac5263a7a1 with some cleanup Pull Request: https://projects.blender.org/blender/blender/pulls/119780 --- source/blender/blenkernel/BKE_attribute.h | 1 + source/blender/blenkernel/intern/attribute.cc | 16 ++++++++++++++ source/blender/blenkernel/intern/paint.cc | 2 +- source/blender/editors/mesh/mesh_data.cc | 11 ++-------- .../editors/sculpt_paint/paint_vertex.cc | 22 ++----------------- 5 files changed, 22 insertions(+), 30 deletions(-) diff --git a/source/blender/blenkernel/BKE_attribute.h b/source/blender/blenkernel/BKE_attribute.h index 867d84f137c..98c73be3cc4 100644 --- a/source/blender/blenkernel/BKE_attribute.h +++ b/source/blender/blenkernel/BKE_attribute.h @@ -112,6 +112,7 @@ void BKE_id_attributes_active_color_set(struct ID *id, const char *name); void BKE_id_attributes_default_color_set(struct ID *id, const char *name); const struct CustomDataLayer *BKE_id_attributes_color_find(const struct ID *id, const char *name); +bool BKE_color_attribute_supported(const struct Mesh &mesh, const blender::StringRef name); std::string BKE_id_attribute_calc_unique_name(const struct ID &id, const blender::StringRef name); diff --git a/source/blender/blenkernel/intern/attribute.cc b/source/blender/blenkernel/intern/attribute.cc index 970a7a3cd39..b42eae462a9 100644 --- a/source/blender/blenkernel/intern/attribute.cc +++ b/source/blender/blenkernel/intern/attribute.cc @@ -933,6 +933,22 @@ const CustomDataLayer *BKE_id_attributes_color_find(const ID *id, const char *na const_cast(id), name, CD_MASK_COLOR_ALL, ATTR_DOMAIN_MASK_COLOR); } +bool BKE_color_attribute_supported(const Mesh &mesh, const blender::StringRef name) +{ + std::optional meta_data = mesh.attributes().lookup_meta_data( + name); + + if (!meta_data) { + return false; + } + if (!(ATTR_DOMAIN_AS_MASK(meta_data->domain) & ATTR_DOMAIN_MASK_COLOR) || + !(CD_TYPE_AS_MASK(meta_data->data_type) & CD_MASK_COLOR_ALL)) + { + return false; + } + return true; +} + const char *BKE_uv_map_vert_select_name_get(const char *uv_map_name, char *buffer) { BLI_assert(strlen(UV_VERTSEL_NAME) == 2); diff --git a/source/blender/blenkernel/intern/paint.cc b/source/blender/blenkernel/intern/paint.cc index 1e4d9ca8043..dabdda5a9b9 100644 --- a/source/blender/blenkernel/intern/paint.cc +++ b/source/blender/blenkernel/intern/paint.cc @@ -1909,7 +1909,7 @@ void BKE_sculpt_color_layer_create_if_needed(Object *object) using namespace blender::bke; Mesh *orig_me = BKE_object_get_original_mesh(object); - if (orig_me->attributes().contains(orig_me->active_color_attribute)) { + if (BKE_color_attribute_supported(*orig_me, orig_me->active_color_attribute)) { return; } diff --git a/source/blender/editors/mesh/mesh_data.cc b/source/blender/editors/mesh/mesh_data.cc index a86adb8e7a1..ef8bba926e7 100644 --- a/source/blender/editors/mesh/mesh_data.cc +++ b/source/blender/editors/mesh/mesh_data.cc @@ -430,15 +430,8 @@ bool ED_mesh_color_ensure(Mesh *mesh, const char *name) { using namespace blender; BLI_assert(mesh->runtime->edit_mesh == nullptr); - const bke::AttributeAccessor attributes = mesh->attributes(); - if (const std::optional meta_data = attributes.lookup_meta_data( - mesh->active_color_attribute)) - { - if ((ATTR_DOMAIN_AS_MASK(meta_data->domain) & ATTR_DOMAIN_MASK_COLOR) && - (CD_TYPE_AS_MASK(meta_data->data_type) & CD_MASK_COLOR_ALL)) - { - return true; - } + if (BKE_color_attribute_supported(*mesh, mesh->active_color_attribute)) { + return true; } const std::string unique_name = BKE_id_attribute_calc_unique_name(mesh->id, name); diff --git a/source/blender/editors/sculpt_paint/paint_vertex.cc b/source/blender/editors/sculpt_paint/paint_vertex.cc index 77838e68234..0e349fe5f11 100644 --- a/source/blender/editors/sculpt_paint/paint_vertex.cc +++ b/source/blender/editors/sculpt_paint/paint_vertex.cc @@ -600,24 +600,6 @@ void smooth_brush_toggle_on(const bContext *C, Paint *paint, StrokeCache *cache) /** \} */ } // namespace blender::ed::sculpt_paint::vwpaint -static bool color_attribute_supported(const std::optional meta_data) -{ - if (!meta_data) { - return false; - } - if (!(ATTR_DOMAIN_AS_MASK(meta_data->domain) & ATTR_DOMAIN_MASK_COLOR) || - !(CD_TYPE_AS_MASK(meta_data->data_type) & CD_MASK_COLOR_ALL)) - { - return false; - } - return true; -} - -static bool color_attribute_supported(const Mesh &mesh, const StringRef name) -{ - return color_attribute_supported(mesh.attributes().lookup_meta_data(name)); -} - bool vertex_paint_mode_poll(bContext *C) { const Object *ob = CTX_data_active_object(C); @@ -630,7 +612,7 @@ bool vertex_paint_mode_poll(bContext *C) return false; } - if (!color_attribute_supported(*mesh, mesh->active_color_attribute)) { + if (!BKE_color_attribute_supported(*mesh, mesh->active_color_attribute)) { return false; } @@ -1013,7 +995,7 @@ static bool vpaint_stroke_test_start(bContext *C, wmOperator *op, const float mo const std::optional meta_data = *mesh->attributes().lookup_meta_data( mesh->active_color_attribute); - if (!color_attribute_supported(meta_data)) { + if (!BKE_color_attribute_supported(*mesh, mesh->active_color_attribute)) { return false; }