Blender V5.3
paint_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10
11#include "DNA_brush_types.h"
12#include "DNA_mesh_types.h"
13#include "DNA_object_types.h"
14#include "DNA_scene_types.h"
15
16#include "BLI_listbase.hh"
17#include "BLI_math_color_c.hh"
18#include "BLI_math_matrix.hh"
19#include "BLI_math_matrix_c.hh"
20#include "BLI_rect.hh"
21
22#include "BLT_translation.hh"
23
24#include "BKE_brush.hh"
25#include "BKE_bvhutils.hh"
26#include "BKE_colortools.hh"
27#include "BKE_context.hh"
28#include "BKE_curves.hh"
29#include "BKE_layer.hh"
30#include "BKE_mesh.hh"
31#include "BKE_mesh_sample.hh"
32#include "BKE_object.hh"
33#include "BKE_paint.hh"
34#include "BKE_paint_types.hh"
35#include "BKE_report.hh"
36#include "DNA_curves_types.h"
38
39#include "RNA_access.hh"
40#include "RNA_define.hh"
41#include "RNA_prototypes.hh"
42
43#include "RE_texture.h"
44
45#include "ED_mesh.hh" /* for face mask functions */
46#include "ED_screen.hh"
47#include "ED_select_utils.hh"
48#include "ED_view3d.hh"
49
50#include "WM_api.hh"
51#include "WM_types.hh"
52
53#include "paint_intern.hh"
54
55namespace blender {
56
58{
59 const Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob);
60 if (!mesh_eval) {
61 mesh_eval = id_cast<const Mesh *>(ob->data);
62 }
63
64 const std::optional<Bounds<float3>> bounds = mesh_eval->bounds_min_max();
65 if (!bounds) {
66 return float3(0.0f);
67 }
68
69 return math::midpoint(bounds->min, bounds->max);
70}
71
73{
74 const Curves &curves = *id_cast<const Curves *>(ob->data);
75 const std::optional<Bounds<float3>> bounds = curves.geometry.wrap().bounds_min_max();
76 if (bounds.has_value()) {
77 return math::midpoint(bounds->min, bounds->max);
78 }
79 return float3(0);
80}
81
82static float3 paint_init_pivot_grease_pencil(Object *ob, const int frame)
83{
84 const GreasePencil &grease_pencil = *id_cast<const GreasePencil *>(ob->data);
85 const std::optional<Bounds<float3>> bounds = grease_pencil.bounds_min_max(frame);
86 if (bounds.has_value()) {
87 return math::midpoint(bounds->min, bounds->max);
88 }
89 return float3(0.0f);
90}
91
93{
94 bke::PaintRuntime &paint_runtime = *paint->runtime;
95
96 float3 location;
97 switch (ob->type) {
98 case OB_MESH:
99 location = paint_init_pivot_mesh(ob);
100 break;
101 case OB_CURVES:
102 location = paint_init_pivot_curves(ob);
103 break;
104 case OB_GREASE_PENCIL:
105 location = paint_init_pivot_grease_pencil(ob, scene->r.cfra);
106 break;
107 default:
109 paint_runtime.last_stroke_valid = false;
110 return;
111 }
112
113 mul_m4_v3(ob->object_to_world().ptr(), location);
114
116}
117
119 const float3 &center,
120 const float pixel_radius)
121{
122 Object *ob = vc.obact;
123 float delta[3], scale, loc[3];
124 const float xy_delta[2] = {pixel_radius, 0.0f};
125
126 mul_v3_m4v3(loc, ob->object_to_world().ptr(), center);
127
128 const float zfac = ED_view3d_calc_zfac(vc.rv3d, loc);
129 ED_view3d_win_to_delta(vc.region, xy_delta, zfac, delta);
130
131 scale = fabsf(mat4_to_scale(ob->object_to_world().ptr()));
132 scale = (scale == 0.0f) ? 1.0f : scale;
133
134 return len_v3(delta) / scale;
135}
136
137bool paint_get_tex_pixel(const MTex *mtex,
138 float u,
139 float v,
140 ImagePool *pool,
141 int thread,
142 /* Return arguments. */
143 float *r_intensity,
144 float r_rgba[4])
145{
146 const float co[3] = {u, v, 0.0f};
147 float intensity;
148 const bool has_rgb = RE_texture_evaluate(
149 mtex, co, thread, pool, false, false, &intensity, r_rgba);
150 *r_intensity = intensity;
151
152 if (!has_rgb) {
153 r_rgba[0] = intensity;
154 r_rgba[1] = intensity;
155 r_rgba[2] = intensity;
156 r_rgba[3] = 1.0f;
157 }
158
159 return has_rgb;
160}
161
163{
164 static const EnumPropertyItem stroke_mode_items[] = {
165 {int(BrushStrokeMode::Normal), "NORMAL", 0, "Regular", "Apply brush normally"},
167 "INVERT",
168 0,
169 "Invert",
170 "Invert action of brush for duration of stroke"},
171 {0},
172 };
173
174 static const EnumPropertyItem temporary_brush_toggle_items[] = {
175 {int(BrushSwitchMode::None), "None", 0, "None", "Apply brush normally"},
177 "SMOOTH",
178 0,
179 "Smooth",
180 "Switch to smooth brush for duration of stroke"},
182 "ERASE",
183 0,
184 "Erase",
185 "Switch to erase brush for duration of stroke"},
187 "MASK",
188 0,
189 "Mask",
190 "Switch to mask brush for duration of stroke"},
191 {0},
192 };
193
194 PropertyRNA *prop;
195
196 prop = RNA_def_collection_runtime(ot->srna, "stroke", RNA_OperatorStrokeElement, "Stroke", "");
198
199 prop = RNA_def_enum(ot->srna,
200 "mode",
201 stroke_mode_items,
203 "Stroke Mode",
204 "Action taken when a paint stroke is made");
207
208 prop = RNA_def_enum(ot->srna,
209 "brush_toggle",
210 temporary_brush_toggle_items,
212 "Temporary Brush Toggle Type",
213 "Brush to use for duration of stroke");
216
217 /* TODO: Pen flip logic should likely be combined into the stroke mode logic instead of being
218 * an entirely separate concept. */
219 prop = RNA_def_boolean(
220 ot->srna, "pen_flip", false, "Pen Flip", "Whether a tablet's eraser mode is being used");
222}
223
224/* face-select ops */
231
233{
234 ot->name = "Select Linked";
235 ot->description = "Select linked faces";
236 ot->idname = "PAINT_OT_face_select_linked";
237
240
242}
243
254
256{
257 ot->name = "Select Linked Pick";
258 ot->description = "Select linked faces under the cursor";
259 ot->idname = "PAINT_OT_face_select_linked_pick";
260
263
265
266 RNA_def_boolean(ot->srna, "deselect", false, "Deselect", "Deselect rather than select items");
267}
268
278
280{
281 ot->name = "(De)select All";
282 ot->description = "Change selection for all faces";
283 ot->idname = "PAINT_OT_face_select_all";
284
287
289
291}
292
294{
297 if (mesh == nullptr || mesh->faces_num == 0) {
298 return OPERATOR_CANCELLED;
299 }
300
301 const bool face_step = RNA_boolean_get(op->ptr, "face_step");
303 paintface_flush_flags(C, ob, true, false);
304
306 return OPERATOR_FINISHED;
307}
308
310{
311 ot->name = "Select More";
312 ot->description = "Select Faces connected to existing selection";
313 ot->idname = "PAINT_OT_face_select_more";
314
317
319
321 ot->srna, "face_step", true, "Face Step", "Also select faces that only touch on a corner");
322}
323
325{
328 if (mesh == nullptr || mesh->faces_num == 0) {
329 return OPERATOR_CANCELLED;
330 }
331
332 const bool face_step = RNA_boolean_get(op->ptr, "face_step");
334 paintface_flush_flags(C, ob, true, false);
335
337 return OPERATOR_FINISHED;
338}
339
341{
342 ot->name = "Select Less";
343 ot->description = "Deselect Faces connected to existing selection";
344 ot->idname = "PAINT_OT_face_select_less";
345
348
350
352 ot->srna, "face_step", true, "Face Step", "Also deselect faces that only touch on a corner");
353}
354
356 wmOperator *op,
357 const wmEvent *event)
358{
359 const bool select = RNA_boolean_get(op->ptr, "select");
360 const bool extend = RNA_boolean_get(op->ptr, "extend");
361 if (!extend) {
363 }
367 return OPERATOR_FINISHED;
368}
369
371{
372 ot->name = "Select Loop";
373 ot->description = "Select face loop under the cursor";
374 ot->idname = "PAINT_OT_face_select_loop";
375
378
380
381 RNA_def_boolean(ot->srna, "select", true, "Select", "If false, faces will be deselected");
382 RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the selection");
383}
384
386 wmOperator *op,
387 const wmEvent *event)
388{
389 const bool select = RNA_boolean_get(op->ptr, "select");
390 const bool extend = RNA_boolean_get(op->ptr, "extend");
391 if (!extend) {
393 }
397 return OPERATOR_FINISHED;
398}
399
401{
402 ot->name = "Select Loop";
403 ot->description = "Select vertex loop under the cursor";
404 ot->idname = "PAINT_OT_vert_select_loop";
405
407 ot->poll = vert_paint_poll;
408
410
411 RNA_def_boolean(ot->srna, "select", true, "Select", "If false, vertices will be deselected");
412 RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the selection");
413}
414
423
425{
426 ot->name = "(De)select All";
427 ot->description = "Change selection for all vertices";
428 ot->idname = "PAINT_OT_vert_select_all";
429
431 ot->poll = vert_paint_poll;
432
434
436}
437
439{
441 Mesh *mesh = id_cast<Mesh *>(ob->data);
442
443 if (mesh->vertex_group_names.is_empty() || mesh->deform_verts().is_empty()) {
444 BKE_report(op->reports, RPT_ERROR, "No weights/vertex groups on object");
445 return OPERATOR_CANCELLED;
446 }
447
448 paintvert_select_ungrouped(ob, RNA_boolean_get(op->ptr, "extend"), true);
451 return OPERATOR_FINISHED;
452}
453
455{
456 /* identifiers */
457 ot->name = "Select Ungrouped";
458 ot->idname = "PAINT_OT_vert_select_ungrouped";
459 ot->description = "Select vertices without a group";
460
461 /* API callbacks. */
463 ot->poll = vert_paint_poll;
464
465 /* flags */
467
468 RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the selection");
469}
470
477
479{
480 ot->name = "Select Linked Vertices";
481 ot->description = "Select linked vertices";
482 ot->idname = "PAINT_OT_vert_select_linked";
483
485 ot->poll = vert_paint_poll;
486
488}
489
501
503{
504 ot->name = "Select Linked Vertices Pick";
505 ot->description = "Select linked vertices under the cursor";
506 ot->idname = "PAINT_OT_vert_select_linked_pick";
507
509 ot->poll = vert_paint_poll;
510
512
514 "select",
515 true,
516 "Select",
517 "Whether to select or deselect linked vertices under the cursor");
518}
519
521{
524 if (mesh == nullptr || mesh->faces_num == 0) {
525 return OPERATOR_CANCELLED;
526 }
527
528 const bool face_step = RNA_boolean_get(op->ptr, "face_step");
530
534
535 return OPERATOR_FINISHED;
536}
537
539{
540 ot->name = "Select More";
541 ot->description = "Select Vertices connected to existing selection";
542 ot->idname = "PAINT_OT_vert_select_more";
543
545 ot->poll = vert_paint_poll;
546
548
550 ot->srna, "face_step", true, "Face Step", "Also select faces that only touch on a corner");
551}
552
554{
557 if (mesh == nullptr || mesh->faces_num == 0) {
558 return OPERATOR_CANCELLED;
559 }
560
561 const bool face_step = RNA_boolean_get(op->ptr, "face_step");
563
567
568 return OPERATOR_FINISHED;
569}
570
572{
573 ot->name = "Select Less";
574 ot->description = "Deselect Vertices connected to existing selection";
575 ot->idname = "PAINT_OT_vert_select_less";
576
578 ot->poll = vert_paint_poll;
579
581
583 ot->srna, "face_step", true, "Face Step", "Also deselect faces that only touch on a corner");
584}
585
587{
588 const bool unselected = RNA_boolean_get(op->ptr, "unselected");
590 paintface_hide(C, ob, unselected);
592 return OPERATOR_FINISHED;
593}
594
596{
597 ot->name = "Face Select Hide";
598 ot->description = "Hide selected faces";
599 ot->idname = "PAINT_OT_face_select_hide";
600
603
605
607 ot->srna, "unselected", false, "Unselected", "Hide unselected rather than selected objects");
608}
609
611{
612 const bool unselected = RNA_boolean_get(op->ptr, "unselected");
614 paintvert_hide(C, ob, unselected);
616 return OPERATOR_FINISHED;
617}
618
620{
621 ot->name = "Vertex Select Hide";
622 ot->description = "Hide selected vertices";
623 ot->idname = "PAINT_OT_vert_select_hide";
624
626 ot->poll = vert_paint_poll;
627
629
631 "unselected",
632 false,
633 "Unselected",
634 "Hide unselected rather than selected vertices");
635}
636
638{
639 const bool select = RNA_boolean_get(op->ptr, "select");
641
644 }
645 else {
647 }
648
650 return OPERATOR_FINISHED;
651}
652
654{
656
657 /* Allow using this operator when no selection is enabled but hiding is applied. */
659}
660
662{
663 ot->name = "Reveal Faces/Vertices";
664 ot->description = "Reveal hidden faces and vertices";
665 ot->idname = "PAINT_OT_face_vert_reveal";
666
669
671
673 "select",
674 true,
675 "Select",
676 "Specifies whether the newly revealed geometry should be selected");
677}
678
679} // namespace blender
Low-level operations for curves.
General operations, lookup, etc. for blender objects.
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define BLI_assert_unreachable()
float3x3 scale(float3x3 mat, float2 scale)
void stroke_set_location(Paint &paint, float3 location)
T midpoint(const T &a, const T &b)
BMesh Iterators.
static wmOperatorStatus face_vert_reveal_exec(bContext *C, wmOperator *op)
static wmOperatorStatus paintvert_select_loop_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void paintface_hide(bContext *C, Object *ob, bool unselected)
Definition editface.cc:153
void PAINT_OT_face_select_linked(wmOperatorType *ot)
void paintvert_hide(bContext *C, Object *ob, bool unselected)
Definition editface.cc:1136
int RNA_enum_get(PointerRNA *ptr, const char *name)
static wmOperatorStatus paintvert_select_linked_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void paintvert_tag_select_update(bContext *C, Object *ob)
Definition editface.cc:1026
void PAINT_OT_vert_select_ungrouped(wmOperatorType *ot)
static wmOperatorStatus vert_select_all_exec(bContext *C, wmOperator *op)
static wmOperatorStatus paintvert_select_more_exec(bContext *C, wmOperator *op)
float mat4_to_scale(const float mat[4][4])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
static wmOperatorStatus paintface_select_loop_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void PAINT_OT_face_vert_reveal(wmOperatorType *ot)
static float3 paint_init_pivot_curves(Object *ob)
void paintface_select_less(Mesh *mesh, bool face_step)
Definition editface.cc:570
bool paintvert_deselect_all_visible(Object *ob, int action, bool flush_flags)
Definition editface.cc:1032
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
static wmOperatorStatus vert_select_ungrouped_exec(bContext *C, wmOperator *op)
void PAINT_OT_vert_select_linked(wmOperatorType *ot)
void view3d_operator_needs_gpu(const bContext *C)
PropertyRNA * RNA_def_collection_runtime(StructOrFunctionRNA *cont, const char *identifier, StructRNA *type, const char *ui_name, const char *ui_description)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void PAINT_OT_face_select_linked_pick(wmOperatorType *ot)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
void paintvert_select_loop(bContext *C, Object *ob, const int mval[2], const bool select)
Definition editface.cc:1264
void WM_operator_properties_select_all(wmOperatorType *ot)
void PAINT_OT_vert_select_more(wmOperatorType *ot)
void paintface_select_more(Mesh *mesh, bool face_step)
Definition editface.cc:518
void mul_m4_v3(const float M[4][4], float r[3])
bool vert_paint_poll(bContext *C)
static wmOperatorStatus paint_select_linked_exec(bContext *C, wmOperator *)
void paintface_reveal(bContext *C, Object *ob, bool select)
Definition editface.cc:186
static wmOperatorStatus paintvert_select_linked_exec(bContext *C, wmOperator *)
void paintvert_select_more(Mesh *mesh, bool face_step)
Definition editface.cc:917
void PAINT_OT_face_select_loop(wmOperatorType *ot)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
Mesh * BKE_mesh_from_object(Object *ob)
bool BKE_paint_select_elem_test(const Object *ob)
static bool face_vert_reveal_poll(bContext *C)
void PAINT_OT_vert_select_loop(wmOperatorType *ot)
bool BKE_paint_always_hide_test(const Object *ob)
float ED_view3d_calc_zfac(const RegionView3D *rv3d, const float co[3])
static wmOperatorStatus face_select_all_exec(bContext *C, wmOperator *op)
bool paintface_deselect_all_visible(bContext *C, Object *ob, int action, bool flush_flags)
Definition editface.cc:613
void PAINT_OT_face_select_all(wmOperatorType *ot)
bool paint_get_tex_pixel(const MTex *mtex, float u, float v, ImagePool *pool, int thread, float *r_intensity, float r_rgba[4])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
static wmOperatorStatus vert_select_hide_exec(bContext *C, wmOperator *op)
void paintvert_flush_flags(Object *ob)
Definition editface.cc:774
void paint_stroke_operator_properties(wmOperatorType *ot)
bool RE_texture_evaluate(const MTex *mtex, const float vec[3], const int thread, ImagePool *pool, const bool skip_load_image, const bool texnode_preview, float *r_intensity, float r_rgba[4])
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
void PAINT_OT_face_select_less(wmOperatorType *ot)
static wmOperatorStatus paint_select_more_exec(bContext *C, wmOperator *op)
void paintvert_select_linked(bContext *C, Object *ob)
Definition editface.cc:895
void paintface_select_loop(bContext *C, Object *ob, const int mval[2], bool select)
Definition editface.cc:414
static wmOperatorStatus paintvert_select_less_exec(bContext *C, wmOperator *op)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
void paint_init_pivot(Object *ob, Scene *scene, Paint *paint)
static wmOperatorStatus paint_select_less_exec(bContext *C, wmOperator *op)
void paintface_select_linked(bContext *C, Object *ob, const int mval[2], bool select)
Definition editface.cc:308
float paint_calc_object_space_radius(const ViewContext &vc, const float3 &center, float pixel_radius)
void PAINT_OT_vert_select_all(wmOperatorType *ot)
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
void paintvert_select_ungrouped(Object *ob, bool extend, bool flush_flags)
Definition editface.cc:1099
void PAINT_OT_face_select_more(wmOperatorType *ot)
void paintface_flush_flags(bContext *C, Object *ob, bool flush_selection, bool flush_hidden)
Definition editface.cc:42
void PAINT_OT_vert_select_hide(wmOperatorType *ot)
bool facemask_paint_poll(bContext *C)
static float3 paint_init_pivot_grease_pencil(Object *ob, const int frame)
void PAINT_OT_vert_select_less(wmOperatorType *ot)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
static wmOperatorStatus paint_select_linked_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void PAINT_OT_face_select_hide(wmOperatorType *ot)
VecBase< float, 3 > float3
bool BKE_paint_select_vert_test(const Object *ob)
static float3 paint_init_pivot_mesh(Object *ob)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
static wmOperatorStatus face_select_hide_exec(bContext *C, wmOperator *op)
void paintvert_reveal(bContext *C, Object *ob, bool select)
Definition editface.cc:1169
void paintvert_select_linked_pick(bContext *C, Object *ob, const int region_coordinates[2], bool select)
Definition editface.cc:881
void paintvert_select_less(Mesh *mesh, bool face_step)
Definition editface.cc:973
Object * CTX_data_active_object(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
void ED_view3d_win_to_delta(const ARegion *region, const float xy_delta[2], float zfac, float r_out[3], bool precise=false)
void PAINT_OT_vert_select_linked_pick(wmOperatorType *ot)
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
Definition object.cc:4436
static BMFace * face_step(BMEdge *edge, BMFace *f)
#define fabsf
struct RenderData r
RegionView3D * rv3d
Definition ED_view3d.hh:86
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports