[Bf-blender-cvs] [56ca4fe5bb3] refactor-vertex-group-names: Fixes from review comments

Hans Goudey noreply at git.blender.org
Tue Jul 13 01:41:13 CEST 2021


Commit: 56ca4fe5bb3c3d8cd92a1c99fc83856aaefdd0c2
Author: Hans Goudey
Date:   Mon Jul 5 13:58:23 2021 -0500
Branches: refactor-vertex-group-names
https://developer.blender.org/rB56ca4fe5bb3c3d8cd92a1c99fc83856aaefdd0c2

Fixes from review comments

===================================================================

M	source/blender/blenkernel/intern/deform.c
M	source/blender/blenkernel/intern/lattice_deform.c
M	source/blender/blenkernel/intern/mesh.c
M	source/blender/editors/gpencil/gpencil_fill.c
M	source/blender/editors/object/object_vgroup.c
M	source/blender/makesdna/DNA_object_types.h

===================================================================

diff --git a/source/blender/blenkernel/intern/deform.c b/source/blender/blenkernel/intern/deform.c
index 20c2b0425b8..f7ef84728b6 100644
--- a/source/blender/blenkernel/intern/deform.c
+++ b/source/blender/blenkernel/intern/deform.c
@@ -488,28 +488,14 @@ void BKE_defvert_flip_merged(MDeformVert *dvert, const int *flip_map, const int
   }
 }
 
-bDeformGroup *BKE_object_defgroup_find_name(const Object *ob, const char *name)
-{
-  if (name == NULL || name[0] == '\0') {
-    return NULL;
-  }
-  const ListBase *defbase = BKE_object_defgroup_list(ob);
-  return BLI_findstring(defbase, name, offsetof(bDeformGroup, name));
-}
-
-int BKE_object_defgroup_name_index(const Object *ob, const char *name)
+bool BKE_object_supports_vertex_groups(const Object *ob)
 {
-  if (name == NULL || name[0] == '\0') {
-    return -1;
+  const ID *id = (const ID *)ob->data;
+  if (id == NULL) {
+    return false;
   }
-  const ListBase *defbase = BKE_object_defgroup_list(ob);
-  return BLI_findstringindex(defbase, name, offsetof(bDeformGroup, name));
-}
 
-int BKE_id_defgroup_name_index(const ID *id, const char *name)
-{
-  const ListBase *defbase = BKE_id_defgroup_list_get(id);
-  return BLI_findstringindex(defbase, name, offsetof(bDeformGroup, name));
+  return ELEM(GS(id->name), ID_ME, ID_LT, ID_GD);
 }
 
 const ListBase *BKE_id_defgroup_list_get(const ID *id)
@@ -534,31 +520,64 @@ const ListBase *BKE_id_defgroup_list_get(const ID *id)
   return NULL;
 }
 
+static const int *object_defgroup_active_index_get_p(const Object *ob)
+{
+  BLI_assert(BKE_object_supports_vertex_groups(ob));
+  switch (ob->type) {
+    case OB_MESH: {
+      const Mesh *mesh = (const Mesh *)ob->data;
+      return &mesh->vertex_group_active_index;
+    }
+    case OB_LATTICE: {
+      const Lattice *lattice = (const Lattice *)ob->data;
+      return &lattice->vertex_group_active_index;
+    }
+    case OB_GPENCIL: {
+      const bGPdata *gpd = (const bGPdata *)ob->data;
+      return &gpd->vertex_group_active_index;
+    }
+  }
+  return NULL;
+}
+
 ListBase *BKE_id_defgroup_list_get_mutable(ID *id)
 {
   /* Cast away const just for the accessor. */
   return (ListBase *)BKE_id_defgroup_list_get(id);
 }
 
-bool BKE_object_supports_vertex_groups(const Object *ob)
+bDeformGroup *BKE_object_defgroup_find_name(const Object *ob, const char *name)
 {
-  const ID *id = (const ID *)ob->data;
-  if (id == NULL) {
-    return false;
+  if (name == NULL || name[0] == '\0') {
+    return NULL;
   }
+  const ListBase *defbase = BKE_object_defgroup_list(ob);
+  return BLI_findstring(defbase, name, offsetof(bDeformGroup, name));
+}
 
-  return ELEM(GS(id->name), ID_ME, ID_LT, ID_GD);
+int BKE_id_defgroup_name_index(const ID *id, const char *name)
+{
+  if (name == NULL || name[0] == '\0') {
+    return -1;
+  }
+  const ListBase *defbase = BKE_id_defgroup_list_get(id);
+  return BLI_findstringindex(defbase, name, offsetof(bDeformGroup, name));
 }
 
 const ListBase *BKE_object_defgroup_list(const Object *ob)
 {
-  BLI_assert(ob->data != NULL);
+  BLI_assert(BKE_object_supports_vertex_groups(ob));
   return BKE_id_defgroup_list_get((const ID *)ob->data);
 }
 
+int BKE_object_defgroup_name_index(const Object *ob, const char *name)
+{
+  return BKE_id_defgroup_name_index((ID *)ob->data, name);
+}
+
 ListBase *BKE_object_defgroup_list_mutable(Object *ob)
 {
-  BLI_assert(ob->data != NULL);
+  BLI_assert(BKE_object_supports_vertex_groups(ob));
   return BKE_id_defgroup_list_get_mutable((ID *)ob->data);
 }
 
@@ -572,22 +591,7 @@ int BKE_object_defgroup_count(const Object *ob)
  */
 int BKE_object_defgroup_active_index_get(const Object *ob)
 {
-  switch (ob->type) {
-    case OB_MESH: {
-      const Mesh *mesh = (const Mesh *)ob->data;
-      return mesh->vertex_group_active_index;
-    }
-    case OB_LATTICE: {
-      const Lattice *lattice = (const Lattice *)ob->data;
-      return lattice->vertex_group_active_index;
-    }
-    case OB_GPENCIL: {
-      const bGPdata *gpd = (const bGPdata *)ob->data;
-      return gpd->vertex_group_active_index;
-    }
-  }
-  BLI_assert_unreachable();
-  return -1;
+  return *object_defgroup_active_index_get_p(ob);
 }
 
 /**
@@ -595,26 +599,9 @@ int BKE_object_defgroup_active_index_get(const Object *ob)
  */
 void BKE_object_defgroup_active_index_set(Object *ob, const int new_index)
 {
-  switch (ob->type) {
-    case OB_MESH: {
-      Mesh *mesh = (Mesh *)ob->data;
-      mesh->vertex_group_active_index = new_index;
-      break;
-    }
-    case OB_LATTICE: {
-      Lattice *lattice = (Lattice *)ob->data;
-      lattice->vertex_group_active_index = new_index;
-      break;
-    }
-    case OB_GPENCIL: {
-      bGPdata *gpd = (bGPdata *)ob->data;
-      gpd->vertex_group_active_index = new_index;
-      break;
-    }
-    default: {
-      BLI_assert_unreachable();
-    }
-  }
+  /* Cast away const just for the accessor. */
+  int *index = (int *)object_defgroup_active_index_get_p(ob);
+  *index = new_index;
 }
 
 /**
diff --git a/source/blender/blenkernel/intern/lattice_deform.c b/source/blender/blenkernel/intern/lattice_deform.c
index d6e5eca95fc..1856a08e198 100644
--- a/source/blender/blenkernel/intern/lattice_deform.c
+++ b/source/blender/blenkernel/intern/lattice_deform.c
@@ -364,7 +364,7 @@ static void lattice_deform_coords_impl(const Object *ob_lattice,
    * We want either a Mesh/Lattice with no derived data, or derived data with deformverts.
    */
   if (defgrp_name && defgrp_name[0] && ob_target && ELEM(ob_target->type, OB_MESH, OB_LATTICE)) {
-    defgrp_index = BKE_id_defgroup_name_index(&((Lattice *)ob_target->data)->id, defgrp_name);
+    defgrp_index = BKE_id_defgroup_name_index((ID *)ob_target->data, defgrp_name);
 
     if (defgrp_index != -1) {
       /* if there's derived data without deformverts, don't use vgroups */
diff --git a/source/blender/blenkernel/intern/mesh.c b/source/blender/blenkernel/intern/mesh.c
index ce12cd5c08b..c4186ad3180 100644
--- a/source/blender/blenkernel/intern/mesh.c
+++ b/source/blender/blenkernel/intern/mesh.c
@@ -932,10 +932,9 @@ void BKE_mesh_copy_parameters(Mesh *me_dst, const Mesh *me_src)
   copy_v3_v3(me_dst->loc, me_src->loc);
   copy_v3_v3(me_dst->size, me_src->size);
 
-  /* Copy vertex group names, only when they haven't already been copied. */
-  if (BLI_listbase_is_empty(&me_dst->vertex_group_names)) {
-    BKE_defgroup_copy_list(&me_dst->vertex_group_names, &me_src->vertex_group_names);
-  }
+  /* Some callers call this on existing meshes, so free the existing vertex groups first. */
+  BLI_freelistN(&me_dst->vertex_group_names);
+  BKE_defgroup_copy_list(&me_dst->vertex_group_names, &me_src->vertex_group_names);
 
   me_dst->vertex_group_active_index = me_src->vertex_group_active_index;
 }
diff --git a/source/blender/editors/gpencil/gpencil_fill.c b/source/blender/editors/gpencil/gpencil_fill.c
index d223f326bbb..67e1bd5294b 100644
--- a/source/blender/editors/gpencil/gpencil_fill.c
+++ b/source/blender/editors/gpencil/gpencil_fill.c
@@ -1523,8 +1523,7 @@ static void gpencil_stroke_from_buffer(tGPDfill *tgpf)
   point2D = (tGPspoint *)tgpf->sbuffer;
 
   const int def_nr = tgpf->gpd->vertex_group_active_index - 1;
-  const ListBase *defbase = BKE_object_defgroup_list(tgpf->ob);
-  const bool have_weight = (bool)BLI_findlink(defbase, def_nr);
+  const bool have_weight = (bool)BLI_findlink(&tgpf->gpd->vertex_group_names, def_nr);
 
   if ((ts->gpencil_flags & GP_TOOL_FLAG_CREATE_WEIGHTS) && (have_weight)) {
     BKE_gpencil_dvert_ensure(gps);
diff --git a/source/blender/editors/object/object_vgroup.c b/source/blender/editors/object/object_vgroup.c
index e05344760f4..4ea599fd30e 100644
--- a/source/blender/editors/object/object_vgroup.c
+++ b/source/blender/editors/object/object_vgroup.c
@@ -2843,9 +2843,10 @@ static bool vertex_group_vert_select_unlocked_poll(bContext *C)
     return false;
   }
 
-  if (BKE_object_defgroup_active_index_get(ob) != 0) {
+  const int def_nr = BKE_object_defgroup_active_index_get(ob);
+  if (def_nr != 0) {
     const ListBase *defbase = BKE_object_defgroup_list(ob);
-    const bDeformGroup *dg = BLI_findlink(defbase, BKE_object_defgroup_active_index_get(ob) - 1);
+    const bDeformGroup *dg = BLI_findlink(defbase, def_nr - 1);
     if (dg) {
       return !(dg->flag & DG_LOCK_WEIGHT);
     }
diff --git a/source/blender/makesdna/DNA_object_types.h b/source/blender/makesdna/DNA_object_types.h
index 1577e727127..3600f36fa7a 100644
--- a/source/blender/makesdna/DNA_object_types.h
+++ b/source/blender/makesdna/DNA_object_types.h
@@ -58,7 +58,6 @@ struct SoftBody;
 struct bGPdata;
 
 /* Vertex Groups - Name Info */
-/* TODO: Move this. */
 typedef struct bDeformGroup {
   struct bDeformGroup *next, *prev;
   /** MAX_VGROUP_NAME. */
@@ -279,8 +278,7 @@ typedef struct Object {
 
   ListBase constraintChannels DNA_DEPRECATED; /* XXX deprecated... old animation system */
   ListBase effect DNA_DEPRECATED;             /* XXX deprecated... keep for readfile */
-  /** List of bDeformGroup (vertex groups) names and flag only. */
-  ListBase defbase DNA_DEPRECATED;
+  ListBase defbase DNA_DEPRECATED;            /* Only for versioning, moved to object data. */
   /** List of ModifierData structures. */
   ListBase modifiers;
   /** List of GpencilModifierData structures. */



More information about the Bf-blender-cvs mailing list