Cleanup: Add helper function to access vertex group index from name

In addition to the existing function which takes an ID argument.
This commit is contained in:
Hans Goudey
2024-09-28 10:38:51 -04:00
parent 751e009898
commit 68be2d9af1
11 changed files with 37 additions and 39 deletions
+1
View File
@@ -44,6 +44,7 @@ void BKE_object_defgroup_active_index_set(Object *ob, int new_index);
*/
const ListBase *BKE_id_defgroup_list_get(const ID *id);
ListBase *BKE_id_defgroup_list_get_mutable(ID *id);
int BKE_defgroup_name_index(const ListBase *defbase, const char *name);
int BKE_id_defgroup_name_index(const ID *id, const char *name);
bool BKE_defgroup_listbase_name_find(const ListBase *defbase,
const char *name,
@@ -516,7 +516,7 @@ static void armature_deform_coords_impl(const Object *ob_arm,
if (BKE_object_supports_vertex_groups(ob_target)) {
/* Collect the vertex group names from the evaluated data. */
armature_def_nr = BLI_findstringindex(defbase, defgrp_name, offsetof(bDeformGroup, name));
armature_def_nr = BKE_defgroup_name_index(defbase, defgrp_name);
defbase_len = BLI_listbase_count(defbase);
/* get a vertex-deform-index to posechannel array */
+7 -2
View File
@@ -523,15 +523,20 @@ bDeformGroup *BKE_object_defgroup_find_name(const Object *ob, const char *name)
return static_cast<bDeformGroup *>(BLI_findstring(defbase, name, offsetof(bDeformGroup, name)));
}
int BKE_id_defgroup_name_index(const ID *id, const char *name)
int BKE_defgroup_name_index(const ListBase *defbase, const char *name)
{
int index;
if (!BKE_id_defgroup_name_find(id, name, &index, nullptr)) {
if (!BKE_defgroup_listbase_name_find(defbase, name, &index, nullptr)) {
return -1;
}
return index;
}
int BKE_id_defgroup_name_index(const ID *id, const char *name)
{
return BKE_defgroup_name_index(BKE_id_defgroup_list_get(id), name);
}
bool BKE_defgroup_listbase_name_find(const ListBase *defbase,
const char *name,
int *r_index,
@@ -366,8 +366,8 @@ class CurvesVertexGroupsAttributeProvider final : public DynamicAttributesProvid
return {};
}
const std::string name = attribute_id;
const int vertex_group_index = BLI_findstringindex(
&curves->vertex_group_names, name.c_str(), offsetof(bDeformGroup, name));
const int vertex_group_index = BKE_defgroup_name_index(&curves->vertex_group_names,
name.c_str());
if (vertex_group_index < 0) {
return {};
}
@@ -396,8 +396,8 @@ class CurvesVertexGroupsAttributeProvider final : public DynamicAttributesProvid
return {};
}
const std::string name = attribute_id;
const int vertex_group_index = BLI_findstringindex(
&curves->vertex_group_names, name.c_str(), offsetof(bDeformGroup, name));
const int vertex_group_index = BKE_defgroup_name_index(&curves->vertex_group_names,
name.c_str());
if (vertex_group_index < 0) {
return {};
}
@@ -893,8 +893,8 @@ class MeshVertexGroupsAttributeProvider final : public DynamicAttributesProvider
return {};
}
const std::string name = attribute_id;
const int vertex_group_index = BLI_findstringindex(
&mesh->vertex_group_names, name.c_str(), offsetof(bDeformGroup, name));
const int vertex_group_index = BKE_defgroup_name_index(&mesh->vertex_group_names,
name.c_str());
if (vertex_group_index < 0) {
return {};
}
@@ -924,8 +924,8 @@ class MeshVertexGroupsAttributeProvider final : public DynamicAttributesProvider
}
const std::string name = attribute_id;
const int vertex_group_index = BLI_findstringindex(
&mesh->vertex_group_names, name.c_str(), offsetof(bDeformGroup, name));
const int vertex_group_index = BKE_defgroup_name_index(&mesh->vertex_group_names,
name.c_str());
if (vertex_group_index < 0) {
return {};
}
@@ -54,7 +54,7 @@ void validate_drawing_vertex_groups(GreasePencil &grease_pencil)
int ensure_vertex_group(const StringRef name, ListBase &vertex_group_names)
{
int def_nr = BLI_findstringindex(&vertex_group_names, name.data(), offsetof(bDeformGroup, name));
int def_nr = BKE_defgroup_name_index(&vertex_group_names, name.data());
if (def_nr < 0) {
bDeformGroup *defgroup = MEM_cnew<bDeformGroup>(__func__);
STRNCPY(defgroup->name, name.data());
@@ -76,7 +76,7 @@ void assign_to_vertex_group_from_mask(bke::CurvesGeometry &curves,
ListBase &vertex_group_names = curves.vertex_group_names;
/* Look for existing group, otherwise lazy-initialize if any vertex is selected. */
int def_nr = BLI_findstringindex(&vertex_group_names, name.data(), offsetof(bDeformGroup, name));
int def_nr = BKE_defgroup_name_index(&vertex_group_names, name.data());
/* Lazily add the vertex group if any vertex is selected. */
if (def_nr < 0) {
@@ -110,8 +110,7 @@ void assign_to_vertex_group(GreasePencil &grease_pencil, const StringRef name, c
".selection", bke::AttrDomain::Point, true);
/* Look for existing group, otherwise lazy-initialize if any vertex is selected. */
int def_nr = BLI_findstringindex(
&vertex_group_names, name.data(), offsetof(bDeformGroup, name));
int def_nr = BKE_defgroup_name_index(&vertex_group_names, name.data());
const MutableSpan<MDeformVert> dverts = curves.deform_verts_for_write();
for (const int i : dverts.index_range()) {
@@ -149,8 +148,7 @@ bool remove_from_vertex_group(GreasePencil &grease_pencil,
bke::CurvesGeometry &curves = drawing.strokes_for_write();
ListBase &vertex_group_names = curves.vertex_group_names;
const int def_nr = BLI_findstringindex(
&vertex_group_names, name.data(), offsetof(bDeformGroup, name));
const int def_nr = BKE_defgroup_name_index(&vertex_group_names, name.data());
if (def_nr < 0) {
/* No vertices assigned to the group in this drawing. */
continue;
@@ -208,8 +206,7 @@ void select_from_group(GreasePencil &grease_pencil,
bke::CurvesGeometry &curves = drawing.strokes_for_write();
ListBase &vertex_group_names = curves.vertex_group_names;
const int def_nr = BLI_findstringindex(
&vertex_group_names, name.data(), offsetof(bDeformGroup, name));
const int def_nr = BKE_defgroup_name_index(&vertex_group_names, name.data());
if (def_nr < 0) {
/* No vertices assigned to the group in this drawing. */
continue;
@@ -295,8 +295,7 @@ static void get_root_and_tips_of_bones(Span<const Bone *> bones,
static int lookup_or_add_deform_group_index(CurvesGeometry &curves, const char *deform_group_name)
{
int def_nr = BLI_findstringindex(
&curves.vertex_group_names, deform_group_name, offsetof(bDeformGroup, name));
int def_nr = BKE_defgroup_name_index(&curves.vertex_group_names, deform_group_name);
/* Lazily add the vertex group. */
if (def_nr == -1) {
@@ -482,10 +481,8 @@ static int weight_sample_invoke(bContext *C, wmOperator * /*op*/, const wmEvent
const bke::greasepencil::Layer &layer = grease_pencil.layer(info.layer_index);
/* Skip drawing when it doesn't use the active vertex group. */
const int drawing_defgroup_nr = BLI_findstringindex(
&info.drawing.strokes().vertex_group_names,
object_defgroup->name,
offsetof(bDeformGroup, name));
const int drawing_defgroup_nr = BKE_defgroup_name_index(
&info.drawing.strokes().vertex_group_names, object_defgroup->name);
if (drawing_defgroup_nr == -1) {
continue;
}
@@ -633,8 +630,8 @@ static int grease_pencil_weight_invert_exec(bContext *C, wmOperator *op)
threading::parallel_for_each(drawings, [&](MutableDrawingInfo info) {
bke::CurvesGeometry &curves = info.drawing.strokes_for_write();
/* Active vgroup index of drawing. */
const int drawing_vgroup_index = BLI_findstringindex(
&curves.vertex_group_names, active_defgroup->name, offsetof(bDeformGroup, name));
const int drawing_vgroup_index = BKE_defgroup_name_index(&curves.vertex_group_names,
active_defgroup->name);
if (drawing_vgroup_index == -1) {
return;
}
@@ -904,8 +901,8 @@ static int vertex_group_normalize_all_exec(bContext *C, wmOperator *op)
/* Get the active vertex group in the drawing when it needs to be locked. */
int active_vertex_group = -1;
if (object_defgroup && lock_active_group) {
active_vertex_group = BLI_findstringindex(
&curves.vertex_group_names, object_defgroup->name, offsetof(bDeformGroup, name));
active_vertex_group = BKE_defgroup_name_index(&curves.vertex_group_names,
object_defgroup->name);
}
/* Put the lock state of every vertex group in a boolean array. */
@@ -1130,7 +1130,7 @@ static int similar_vert_select_exec(bContext *C, wmOperator *op)
GSetIterator gs_iter;
GSET_ITER (gs_iter, gset) {
const char *name = static_cast<const char *>(BLI_gsetIterator_getKey(&gs_iter));
int vgroup_id = BLI_findstringindex(defbase, name, offsetof(bDeformGroup, name));
int vgroup_id = BKE_defgroup_name_index(defbase, name);
if (vgroup_id != -1) {
BLI_BITMAP_ENABLE(defbase_selected, vgroup_id);
found_any = true;
@@ -3607,7 +3607,7 @@ static int vgroup_do_remap(Object *ob, const char *name_array, wmOperator *op)
name = name_array;
for (def = static_cast<const bDeformGroup *>(defbase->first), i = 0; def; def = def->next, i++) {
sort_map[i] = BLI_findstringindex(defbase, name, offsetof(bDeformGroup, name));
sort_map[i] = BKE_defgroup_name_index(defbase, name);
name += MAX_VGROUP_NAME;
BLI_assert(sort_map[i] != -1);
@@ -21,6 +21,7 @@
#include "RNA_access.hh"
#include "BKE_curves.hh"
#include "BKE_deform.hh"
#include "BKE_geometry_set.hh"
#include "BKE_grease_pencil.hh"
#include "BKE_lib_query.hh"
@@ -100,8 +101,7 @@ static void blend_read(BlendDataReader *reader, ModifierData *md)
static int ensure_vertex_group(const StringRefNull name, ListBase &vertex_group_names)
{
int def_nr = BLI_findstringindex(
&vertex_group_names, name.c_str(), offsetof(bDeformGroup, name));
int def_nr = BKE_defgroup_name_index(&vertex_group_names, name.c_str());
if (def_nr < 0) {
bDeformGroup *defgroup = MEM_cnew<bDeformGroup>(__func__);
STRNCPY(defgroup->name, name.c_str());
@@ -115,8 +115,7 @@ static int ensure_vertex_group(const StringRefNull name, ListBase &vertex_group_
static bool target_vertex_group_available(const StringRefNull name,
const ListBase &vertex_group_names)
{
const int def_nr = BLI_findstringindex(
&vertex_group_names, name.c_str(), offsetof(bDeformGroup, name));
const int def_nr = BKE_defgroup_name_index(&vertex_group_names, name.c_str());
if (def_nr < 0) {
return false;
}
@@ -21,6 +21,7 @@
#include "RNA_access.hh"
#include "BKE_curves.hh"
#include "BKE_deform.hh"
#include "BKE_geometry_set.hh"
#include "BKE_grease_pencil.hh"
#include "BKE_lib_query.hh"
@@ -127,8 +128,7 @@ static float get_distance_factor(float3 target_pos,
static int ensure_vertex_group(const StringRefNull name, ListBase &vertex_group_names)
{
int def_nr = BLI_findstringindex(
&vertex_group_names, name.c_str(), offsetof(bDeformGroup, name));
int def_nr = BKE_defgroup_name_index(&vertex_group_names, name.c_str());
if (def_nr < 0) {
bDeformGroup *defgroup = MEM_cnew<bDeformGroup>(__func__);
STRNCPY(defgroup->name, name.c_str());
@@ -142,8 +142,7 @@ static int ensure_vertex_group(const StringRefNull name, ListBase &vertex_group_
static bool target_vertex_group_available(const StringRefNull name,
const ListBase &vertex_group_names)
{
const int def_nr = BLI_findstringindex(
&vertex_group_names, name.c_str(), offsetof(bDeformGroup, name));
const int def_nr = BKE_defgroup_name_index(&vertex_group_names, name.c_str());
if (def_nr < 0) {
return false;
}