diff options
Diffstat (limited to 'source/blender/editors/mesh/mesh_data.c')
-rw-r--r-- | source/blender/editors/mesh/mesh_data.c | 200 |
1 files changed, 179 insertions, 21 deletions
diff --git a/source/blender/editors/mesh/mesh_data.c b/source/blender/editors/mesh/mesh_data.c index 51b699acd63..f608e5ce6a5 100644 --- a/source/blender/editors/mesh/mesh_data.c +++ b/source/blender/editors/mesh/mesh_data.c @@ -121,8 +121,15 @@ static void delete_customdata_layer(Mesh *me, CustomDataLayer *layer) CustomData *data; int layer_index, tot, n; - data = mesh_customdata_get_type( - me, (ELEM(type, CD_MLOOPUV, CD_MLOOPCOL)) ? BM_LOOP : BM_FACE, &tot); + char htype = BM_FACE; + if (ELEM(type, CD_MLOOPCOL, CD_MLOOPUV)) { + htype = BM_LOOP; + } + else if (ELEM(type, CD_PROP_COLOR)) { + htype = BM_VERT; + } + + data = mesh_customdata_get_type(me, htype, &tot); layer_index = CustomData_get_layer_index(data, type); n = (layer - &data->layers[layer_index]); BLI_assert(n >= 0 && (n + layer_index) < data->totlayer); @@ -363,9 +370,7 @@ bool ED_mesh_uv_texture_remove_active(Mesh *me) if (n != -1) { return ED_mesh_uv_texture_remove_index(me, n); } - else { - return false; - } + return false; } bool ED_mesh_uv_texture_remove_named(Mesh *me, const char *name) { @@ -375,9 +380,7 @@ bool ED_mesh_uv_texture_remove_named(Mesh *me, const char *name) if (n != -1) { return ED_mesh_uv_texture_remove_index(me, n); } - else { - return false; - } + return false; } /* note: keep in sync with ED_mesh_uv_texture_add */ @@ -472,9 +475,7 @@ bool ED_mesh_color_remove_active(Mesh *me) if (n != -1) { return ED_mesh_color_remove_index(me, n); } - else { - return false; - } + return false; } bool ED_mesh_color_remove_named(Mesh *me, const char *name) { @@ -483,9 +484,114 @@ bool ED_mesh_color_remove_named(Mesh *me, const char *name) if (n != -1) { return ED_mesh_color_remove_index(me, n); } + return false; +} + +/*********************** Sculpt Vertex colors operators ************************/ + +/* note: keep in sync with ED_mesh_uv_texture_add */ +int ED_mesh_sculpt_color_add(Mesh *me, const char *name, const bool active_set, const bool do_init) +{ + BMEditMesh *em; + int layernum; + + if (me->edit_mesh) { + em = me->edit_mesh; + + layernum = CustomData_number_of_layers(&em->bm->vdata, CD_PROP_COLOR); + if (layernum >= MAX_MCOL) { + return -1; + } + + /* CD_PROP_COLOR */ + BM_data_layer_add_named(em->bm, &em->bm->vdata, CD_PROP_COLOR, name); + /* copy data from active vertex color layer */ + if (layernum && do_init) { + 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) { + CustomData_set_layer_active(&em->bm->vdata, CD_PROP_COLOR, layernum); + } + } else { + layernum = CustomData_number_of_layers(&me->vdata, CD_PROP_COLOR); + if (layernum >= MAX_MCOL) { + return -1; + } + + if (CustomData_has_layer(&me->vdata, CD_PROP_COLOR) && do_init) { + MPropCol *color_data = CustomData_get_layer(&me->vdata, CD_PROP_COLOR); + CustomData_add_layer_named( + &me->vdata, CD_PROP_COLOR, CD_DUPLICATE, color_data, me->totvert, name); + } + else { + CustomData_add_layer_named(&me->vdata, CD_PROP_COLOR, CD_DEFAULT, NULL, me->totvert, name); + } + + if (active_set || layernum == 0) { + CustomData_set_layer_active(&me->vdata, CD_PROP_COLOR, layernum); + } + + BKE_mesh_update_customdata_pointers(me, true); + } + + DEG_id_tag_update(&me->id, 0); + WM_main_add_notifier(NC_GEOM | ND_DATA, me); + + return layernum; +} + +bool ED_mesh_sculpt_color_ensure(struct Mesh *me, const char *name) +{ + BLI_assert(me->edit_mesh == NULL); + + if (me->totvert && !CustomData_has_layer(&me->vdata, CD_PROP_COLOR)) { + CustomData_add_layer_named(&me->vdata, CD_PROP_COLOR, CD_DEFAULT, NULL, me->totvert, name); + BKE_mesh_update_customdata_pointers(me, true); + } + + DEG_id_tag_update(&me->id, 0); + + return (me->mloopcol != NULL); +} + +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) ? NULL : &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_active(Mesh *me) +{ + CustomData *vdata = GET_CD_DATA(me, vdata); + const int n = CustomData_get_active_layer(vdata, CD_PROP_COLOR); + if (n != -1) { + return ED_mesh_sculpt_color_remove_index(me, n); + } + return false; +} +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 ************************/ @@ -619,6 +725,62 @@ void MESH_OT_vertex_color_remove(wmOperatorType *ot) ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } +/*********************** Sculpt Vertex Color Operators ************************/ + +static int mesh_sculpt_vertex_color_add_exec(bContext *C, wmOperator *UNUSED(op)) +{ + Object *ob = ED_object_context(C); + Mesh *me = ob->data; + + if (ED_mesh_sculpt_color_add(me, NULL, true, true) == -1) { + return OPERATOR_CANCELLED; + } + + return OPERATOR_FINISHED; +} + +void MESH_OT_sculpt_vertex_color_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Add Sculpt Vertex Color"; + ot->description = "Add vertex color layer"; + ot->idname = "MESH_OT_sculpt_vertex_color_add"; + + /* api callbacks */ + ot->poll = layers_poll; + ot->exec = mesh_sculpt_vertex_color_add_exec; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + +static int mesh_sculpt_vertex_color_remove_exec(bContext *C, wmOperator *UNUSED(op)) +{ + Object *ob = ED_object_context(C); + Mesh *me = ob->data; + + if (!ED_mesh_sculpt_color_remove_active(me)) { + return OPERATOR_CANCELLED; + } + + return OPERATOR_FINISHED; +} + +void MESH_OT_sculpt_vertex_color_remove(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Remove Sculpt Vertex Color"; + ot->description = "Remove vertex color layer"; + ot->idname = "MESH_OT_sculpt_vertex_color_remove"; + + /* api callbacks */ + ot->exec = mesh_sculpt_vertex_color_remove_exec; + ot->poll = layers_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + /* *** CustomData clear functions, we need an operator for each *** */ static int mesh_customdata_clear_exec__internal(bContext *C, char htype, int type) @@ -643,9 +805,7 @@ static int mesh_customdata_clear_exec__internal(bContext *C, char htype, int typ return OPERATOR_FINISHED; } - else { - return OPERATOR_CANCELLED; - } + return OPERATOR_CANCELLED; } /* Clear Mask */ @@ -681,9 +841,7 @@ static int mesh_customdata_mask_clear_exec(bContext *C, wmOperator *UNUSED(op)) if (ret_a == OPERATOR_FINISHED || ret_b == OPERATOR_FINISHED) { return OPERATOR_FINISHED; } - else { - return OPERATOR_CANCELLED; - } + return OPERATOR_CANCELLED; } void MESH_OT_customdata_mask_clear(wmOperatorType *ot) @@ -1115,7 +1273,7 @@ void ED_mesh_verts_remove(Mesh *mesh, ReportList *reports, int count) BKE_report(reports, RPT_ERROR, "Cannot remove vertices in edit mode"); return; } - else if (count > mesh->totvert) { + if (count > mesh->totvert) { BKE_report(reports, RPT_ERROR, "Cannot remove more vertices than the mesh contains"); return; } @@ -1129,7 +1287,7 @@ void ED_mesh_edges_remove(Mesh *mesh, ReportList *reports, int count) BKE_report(reports, RPT_ERROR, "Cannot remove edges in edit mode"); return; } - else if (count > mesh->totedge) { + if (count > mesh->totedge) { BKE_report(reports, RPT_ERROR, "Cannot remove more edges than the mesh contains"); return; } @@ -1143,7 +1301,7 @@ void ED_mesh_loops_remove(Mesh *mesh, ReportList *reports, int count) BKE_report(reports, RPT_ERROR, "Cannot remove loops in edit mode"); return; } - else if (count > mesh->totloop) { + if (count > mesh->totloop) { BKE_report(reports, RPT_ERROR, "Cannot remove more loops than the mesh contains"); return; } @@ -1157,7 +1315,7 @@ void ED_mesh_polys_remove(Mesh *mesh, ReportList *reports, int count) BKE_report(reports, RPT_ERROR, "Cannot remove polys in edit mode"); return; } - else if (count > mesh->totpoly) { + if (count > mesh->totpoly) { BKE_report(reports, RPT_ERROR, "Cannot remove more polys than the mesh contains"); return; } |