4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2012 by Nicholas Bishop
21 * All rights reserved.
23 * The Original Code is: all of this file.
27 * ***** END GPL LICENSE BLOCK *****
31 /** \file blender/editors/sculpt_paint/paint_mask.c
35 #include "MEM_guardedalloc.h"
37 #include "DNA_mesh_types.h"
38 #include "DNA_meshdata_types.h"
39 #include "DNA_object_types.h"
44 #include "BKE_context.h"
45 #include "BKE_DerivedMesh.h"
46 #include "BKE_multires.h"
47 #include "BKE_paint.h"
48 #include "BKE_subsurf.h"
50 #include "RNA_access.h"
51 #include "RNA_define.h"
56 #include "ED_screen.h"
58 #include "paint_intern.h"
59 #include "sculpt_intern.h" /* for undo push */
63 static void mask_flood_fill_set_elem(float *elem,
64 PaintMaskFloodMode mode,
68 case PAINT_MASK_FLOOD_VALUE:
71 case PAINT_MASK_INVERT:
72 (*elem) = 1.0f - (*elem);
77 static int mask_flood_fill_exec(bContext *C, wmOperator *op)
79 ARegion *ar = CTX_wm_region(C);
80 Object *ob = CTX_data_active_object(C);
81 PaintMaskFloodMode mode;
88 mode = RNA_enum_get(op->ptr, "mode");
89 value = RNA_float_get(op->ptr, "value");
91 dm = mesh_get_derived_final(CTX_data_scene(C), ob, CD_MASK_BAREMESH);
92 pbvh = dm->getPBVH(ob, dm);
93 ob->sculpt->pbvh = pbvh;
95 BLI_pbvh_search_gather(pbvh, NULL, NULL, &nodes, &totnode);
97 sculpt_undo_push_begin("Mask flood fill");
99 for (i = 0; i < totnode; i++) {
102 sculpt_undo_push_node(ob, nodes[i], SCULPT_UNDO_MASK);
104 BLI_pbvh_vertex_iter_begin(pbvh, nodes[i], vi, PBVH_ITER_UNIQUE) {
105 mask_flood_fill_set_elem(vi.mask, mode, value);
106 } BLI_pbvh_vertex_iter_end;
108 BLI_pbvh_node_mark_update(nodes[i]);
109 if (BLI_pbvh_type(pbvh) == PBVH_GRIDS)
110 multires_mark_as_modified(ob, MULTIRES_COORDS_MODIFIED);
113 sculpt_undo_push_end();
118 ED_region_tag_redraw(ar);
120 return OPERATOR_FINISHED;
123 void PAINT_OT_mask_flood_fill(struct wmOperatorType *ot)
125 static EnumPropertyItem mode_items[] = {
126 {PAINT_MASK_FLOOD_VALUE, "VALUE", 0, "Value", "Set mask to the level specified by the \"value\" property"},
127 {PAINT_MASK_INVERT, "INVERT", 0, "Invert", "Invert the mask"},
131 ot->name = "Mask Flood Fill";
132 ot->idname = "PAINT_OT_mask_flood_fill";
135 ot->exec = mask_flood_fill_exec;
136 ot->poll = sculpt_mode_poll;
138 ot->flag = OPTYPE_REGISTER;
141 RNA_def_enum(ot->srna, "mode", mode_items, PAINT_MASK_FLOOD_VALUE, "Mode", NULL);
142 RNA_def_float(ot->srna, "value", 0, 0, 1, "Value", "Mask level to use when mode is \"Value\"; zero means no masking and one is fully masked", 0, 1);