From d0b749beb8334c6557065bff13f7b38bbf231381 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Tue, 1 Jul 2025 12:09:26 -0400 Subject: [PATCH] Cleanup: Use attribute API in texture paint code --- .../editors/sculpt_paint/paint_utils.cc | 46 +++++++++---------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/source/blender/editors/sculpt_paint/paint_utils.cc b/source/blender/editors/sculpt_paint/paint_utils.cc index 985214fa038..e671151397a 100644 --- a/source/blender/editors/sculpt_paint/paint_utils.cc +++ b/source/blender/editors/sculpt_paint/paint_utils.cc @@ -221,44 +221,40 @@ static blender::float2 imapaint_pick_uv(const Mesh *mesh_eval, const int tri_index, const blender::float3 &bary_coord) { + using namespace blender; const ePaintCanvasSource mode = ePaintCanvasSource(scene->toolsettings->imapaint.mode); - const blender::Span tris = mesh_eval->corner_tris(); - const blender::Span tri_faces = mesh_eval->corner_tri_faces(); + const Span tris = mesh_eval->corner_tris(); + const Span tri_faces = mesh_eval->corner_tri_faces(); - const int *material_indices = (const int *)CustomData_get_layer_named( - &mesh_eval->face_data, CD_PROP_INT32, "material_index"); + const bke::AttributeAccessor attributes = mesh_eval->attributes(); + const VArray material_indices = *attributes.lookup_or_default( + "material_index", bke::AttrDomain::Face, 0); /* face means poly here, not triangle, indeed */ const int face_i = tri_faces[tri_index]; - const float(*mloopuv)[2]; + VArraySpan uv_map; if (mode == PAINT_CANVAS_SOURCE_MATERIAL) { const Material *ma; const TexPaintSlot *slot; - ma = BKE_object_material_get(ob_eval, - material_indices == nullptr ? 1 : material_indices[face_i] + 1); + ma = BKE_object_material_get(ob_eval, material_indices[face_i] + 1); slot = &ma->texpaintslot[ma->paint_active_slot]; - - if (!(slot && slot->uvname && - (mloopuv = static_cast(CustomData_get_layer_named( - &mesh_eval->corner_data, CD_PROP_FLOAT2, slot->uvname))))) - { - mloopuv = static_cast( - CustomData_get_layer(&mesh_eval->corner_data, CD_PROP_FLOAT2)); + if (slot && slot->uvname) { + uv_map = *attributes.lookup(slot->uvname, bke::AttrDomain::Face); } } - else { - mloopuv = static_cast( - CustomData_get_layer(&mesh_eval->corner_data, CD_PROP_FLOAT2)); + + if (uv_map.is_empty()) { + const char *active_name = CustomData_get_active_layer_name(&mesh_eval->corner_data, + CD_PROP_FLOAT2); + uv_map = *attributes.lookup(active_name, bke::AttrDomain::Face); } - return blender::bke::mesh_surface_sample::sample_corner_attribute_with_bary_coords( - bary_coord, - tris[tri_index], - blender::Span(reinterpret_cast(mloopuv), mesh_eval->corners_num)); + return bke::mesh_surface_sample::sample_corner_attribute_with_bary_coords( + bary_coord, tris[tri_index], uv_map); } /* returns 0 if not found, otherwise 1 */ @@ -344,8 +340,9 @@ void paint_sample_color( if (ob) { const Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob_eval); - const int *material_indices = (const int *)CustomData_get_layer_named( - &mesh_eval->face_data, CD_PROP_INT32, "material_index"); + const bke::AttributeAccessor attributes = mesh_eval->attributes(); + const VArray material_indices = *attributes.lookup_or_default( + "material_index", bke::AttrDomain::Face, 0); if (CustomData_has_layer(&mesh_eval->corner_data, CD_PROP_FLOAT2)) { ViewContext vc = ED_view3d_viewcontext_init(C, depsgraph); @@ -366,8 +363,7 @@ void paint_sample_color( if (use_material) { /* Image and texture interpolation from material. */ - Material *ma = BKE_object_material_get( - ob_eval, material_indices ? material_indices[faceindex] + 1 : 1); + Material *ma = BKE_object_material_get(ob_eval, material_indices[faceindex] + 1); /* Force refresh since paint slots are not updated when changing interpolation. */ BKE_texpaint_slot_refresh_cache(scene, ma, ob);