Cleanup: Simplify functions for adding and removing color attributes

The specific functions for vertex colors and and sculpt vertex colors
can be replaced by more generic attribute functions internally.
Also remove a paramter from one function.
This commit is contained in:
Hans Goudey 2022-08-02 14:43:36 -05:00
parent 44aa9e40ff
commit c5b361335a
4 changed files with 12 additions and 87 deletions

View File

@ -552,14 +552,10 @@ void ED_mesh_uv_loop_reset_ex(struct Mesh *me, int layernum);
bool ED_mesh_color_ensure(struct Mesh *me, const char *name);
int ED_mesh_color_add(
struct Mesh *me, const char *name, bool active_set, bool do_init, struct ReportList *reports);
bool ED_mesh_color_remove_index(struct Mesh *me, int n);
bool ED_mesh_color_remove_active(struct Mesh *me);
bool ED_mesh_color_remove_named(struct Mesh *me, const char *name);
int ED_mesh_sculpt_color_add(
struct Mesh *me, const char *name, bool active_set, bool do_init, struct ReportList *reports);
bool ED_mesh_sculpt_color_remove_index(struct Mesh *me, int n);
bool ED_mesh_sculpt_color_remove_named(struct Mesh *me, const char *name);
int ED_mesh_sculpt_color_add(struct Mesh *me,
const char *name,
bool do_init,
struct ReportList *reports);
void ED_mesh_report_mirror(struct wmOperator *op, int totmirr, int totfail);
void ED_mesh_report_mirror_ex(struct wmOperator *op, int totmirr, int totfail, char selectmode);

View File

@ -444,44 +444,6 @@ bool ED_mesh_color_ensure(Mesh *me, const char *name)
return (layer != nullptr);
}
bool ED_mesh_color_remove_index(Mesh *me, const int n)
{
CustomData *ldata = GET_CD_DATA(me, ldata);
CustomDataLayer *cdl;
int index;
index = CustomData_get_layer_index_n(ldata, CD_PROP_BYTE_COLOR, n);
cdl = (index == -1) ? nullptr : &ldata->layers[index];
if (!cdl) {
return false;
}
delete_customdata_layer(me, cdl);
DEG_id_tag_update(&me->id, 0);
WM_main_add_notifier(NC_GEOM | ND_DATA, me);
return true;
}
bool ED_mesh_color_remove_active(Mesh *me)
{
CustomData *ldata = GET_CD_DATA(me, ldata);
const int n = CustomData_get_active_layer(ldata, CD_PROP_BYTE_COLOR);
if (n != -1) {
return ED_mesh_color_remove_index(me, n);
}
return false;
}
bool ED_mesh_color_remove_named(Mesh *me, const char *name)
{
CustomData *ldata = GET_CD_DATA(me, ldata);
const int n = CustomData_get_named_layer(ldata, CD_PROP_BYTE_COLOR, name);
if (n != -1) {
return ED_mesh_color_remove_index(me, n);
}
return false;
}
/*********************** General poll ************************/
static bool layers_poll(bContext *C)
@ -494,8 +456,7 @@ static bool layers_poll(bContext *C)
/*********************** Sculpt Vertex colors operators ************************/
int ED_mesh_sculpt_color_add(
Mesh *me, const char *name, const bool active_set, const bool do_init, ReportList *reports)
int ED_mesh_sculpt_color_add(Mesh *me, const char *name, const bool do_init, ReportList *reports)
{
/* NOTE: keep in sync with #ED_mesh_uv_add. */
@ -519,7 +480,7 @@ int ED_mesh_sculpt_color_add(
const int layernum_dst = CustomData_get_active_layer(&em->bm->vdata, CD_PROP_COLOR);
BM_data_layer_copy(em->bm, &em->bm->vdata, CD_PROP_COLOR, layernum_dst, layernum);
}
if (active_set || layernum == 0) {
if (layernum == 0) {
CustomData_set_layer_active(&em->bm->vdata, CD_PROP_COLOR, layernum);
}
}
@ -542,7 +503,7 @@ int ED_mesh_sculpt_color_add(
&me->vdata, CD_PROP_COLOR, CD_DEFAULT, nullptr, me->totvert, name);
}
if (active_set || layernum == 0) {
if (layernum == 0) {
CustomData_set_layer_active(&me->vdata, CD_PROP_COLOR, layernum);
}
@ -555,35 +516,6 @@ int ED_mesh_sculpt_color_add(
return layernum;
}
bool ED_mesh_sculpt_color_remove_index(Mesh *me, const int n)
{
CustomData *vdata = GET_CD_DATA(me, vdata);
CustomDataLayer *cdl;
int index;
index = CustomData_get_layer_index_n(vdata, CD_PROP_COLOR, n);
cdl = (index == -1) ? nullptr : &vdata->layers[index];
if (!cdl) {
return false;
}
delete_customdata_layer(me, cdl);
DEG_id_tag_update(&me->id, 0);
WM_main_add_notifier(NC_GEOM | ND_DATA, me);
return true;
}
bool ED_mesh_sculpt_color_remove_named(Mesh *me, const char *name)
{
CustomData *vdata = GET_CD_DATA(me, vdata);
const int n = CustomData_get_named_layer(vdata, CD_PROP_COLOR, name);
if (n != -1) {
return ED_mesh_sculpt_color_remove_index(me, n);
}
return false;
}
/*********************** UV texture operators ************************/
static bool uv_texture_remove_poll(bContext *C)

View File

@ -21,6 +21,7 @@
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "BKE_attribute.h"
#include "BKE_context.h"
#include "BKE_deform.h"
#include "BKE_dynamicpaint.h"
@ -233,7 +234,7 @@ static int output_toggle_exec(bContext *C, wmOperator *op)
ED_mesh_color_add(ob->data, name, true, true, op->reports);
}
else {
ED_mesh_color_remove_named(ob->data, name);
BKE_id_attribute_remove(ob->data, name, NULL);
}
}
/* Vertex Weight Layer */

View File

@ -1608,9 +1608,7 @@ static void rna_Mesh_vertex_color_remove(struct Mesh *me,
ReportList *reports,
CustomDataLayer *layer)
{
if (ED_mesh_color_remove_named(me, layer->name) == false) {
BKE_reportf(reports, RPT_ERROR, "Vertex color '%s' not found", layer->name);
}
BKE_id_attribute_remove(&me->id, layer->name, reports);
}
static PointerRNA rna_Mesh_sculpt_vertex_color_new(struct Mesh *me,
@ -1621,7 +1619,7 @@ static PointerRNA rna_Mesh_sculpt_vertex_color_new(struct Mesh *me,
PointerRNA ptr;
CustomData *vdata;
CustomDataLayer *cdl = NULL;
int index = ED_mesh_sculpt_color_add(me, name, false, do_init, reports);
int index = ED_mesh_sculpt_color_add(me, name, do_init, reports);
if (index != -1) {
vdata = rna_mesh_vdata_helper(me);
@ -1636,9 +1634,7 @@ static void rna_Mesh_sculpt_vertex_color_remove(struct Mesh *me,
ReportList *reports,
CustomDataLayer *layer)
{
if (ED_mesh_sculpt_color_remove_named(me, layer->name) == false) {
BKE_reportf(reports, RPT_ERROR, "Sculpt vertex color '%s' not found", layer->name);
}
BKE_id_attribute_remove(&me->id, layer->name, reports);
}
# define DEFINE_CUSTOMDATA_PROPERTY_API( \