Blender V5.3
paint_vertex_color_ops.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_mesh_types.h"
10#include "DNA_object_types.h"
11
12#include "BLI_array.hh"
13#include "BLI_color.hh"
14#include "BLI_function_ref.hh"
15#include "BLI_listbase.hh"
16#include "BLI_math_base_c.hh"
17#include "BLI_math_color_c.hh"
18#include "BLI_vector.hh"
19
20#include "BLT_translation.hh"
21
22#include "BKE_attribute_math.hh"
23#include "BKE_context.hh"
24#include "BKE_geometry_set.hh"
25#include "BKE_mesh.hh"
26
27#include "DEG_depsgraph.hh"
28
29#include "RNA_access.hh"
30#include "RNA_define.hh"
31
32#include "WM_api.hh"
33#include "WM_types.hh"
34
35#include "ED_mesh.hh"
36
37#include "../paint_intern.hh" /* own include */
38#include "sculpt_intern.hh"
39
40namespace blender {
41
42/* -------------------------------------------------------------------- */
45
47{
50 return (ob && ELEM(ob->mode, OB_MODE_VERTEX_PAINT, OB_MODE_WEIGHT_PAINT)) &&
51 (mesh && mesh->faces_num && !mesh->deform_verts().is_empty());
52}
53
54static void tag_object_after_update(Object &object)
55{
56 BLI_assert(object.type == OB_MESH);
57 Mesh &mesh = *id_cast<Mesh *>(object.data);
59 /* NOTE: Original mesh is used for display, so tag it directly here. */
61}
62
64
65/* -------------------------------------------------------------------- */
68
70{
71 Mesh *mesh;
72 if ((mesh = BKE_mesh_from_object(&ob)) == nullptr ||
73 ED_mesh_color_ensure(mesh, nullptr) == false)
74 {
75 return false;
76 }
77
78 if (!mesh->attributes().contains(mesh->active_color_attribute)) {
80 return false;
81 }
82
83 const int active_vertex_group_index = mesh->vertex_group_active_index - 1;
84 const bDeformGroup *deform_group = static_cast<const bDeformGroup *>(
85 BLI_findlink(&mesh->vertex_group_names, active_vertex_group_index));
86 if (deform_group == nullptr) {
88 return false;
89 }
90
91 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
92
93 bke::GAttributeWriter color_attribute = attributes.lookup_for_write(
94 mesh->active_color_attribute);
95 if (!color_attribute) {
97 return false;
98 }
99
100 /* Retrieve the vertex group with the domain and type of the existing color
101 * attribute, in order to let the attribute API handle both conversions. */
102 const GVArray vertex_group = *attributes.lookup(
103 deform_group->name,
105 bke::cpp_type_to_attribute_type(color_attribute.varray.type()));
106 if (!vertex_group) {
108 return false;
109 }
110
111 GVArraySpan interpolated{
112 attributes.adapt_domain(vertex_group, bke::AttrDomain::Point, color_attribute.domain)};
113
114 color_attribute.varray.set_all(interpolated.data());
115 color_attribute.finish();
117
118 return true;
119}
120
130
132{
133 /* identifiers */
134 ot->name = "Vertex Color from Weight";
135 ot->idname = "PAINT_OT_vertex_color_from_weight";
136 ot->description = "Convert active weight into gray scale vertex colors";
137
138 /* API callbacks. */
141
142 /* flags */
144
145 /* TODO: invert, alpha */
146}
147
149
150/* -------------------------------------------------------------------- */
153
155 const bke::AttrDomain domain,
156 IndexMaskMemory &memory)
157{
158 const bke::AttributeAccessor attributes = mesh.attributes();
159
160 /* Hidden should never count as selected. */
162 *attributes.lookup_or_default<bool>(".hide_poly", domain, false), memory);
163
164 if (mesh.editflag & ME_EDIT_PAINT_FACE_SEL) {
165 const VArray<bool> selection = *attributes.lookup_or_default<bool>(
166 ".select_poly", domain, false);
167 return IndexMask::from_bools(visible, selection, memory);
168 }
169 if (mesh.editflag & ME_EDIT_PAINT_VERT_SEL) {
170 const VArray<bool> selection = *attributes.lookup_or_default<bool>(
171 ".select_vert", domain, false);
172 return IndexMask::from_bools(visible, selection, memory);
173 }
174
175 return visible;
176}
177
179{
180 const StringRef name = mesh.active_color_attribute;
181 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
183 if (!attribute) {
185 return;
186 }
187 if (attribute.domain != bke::AttrDomain::Point) {
188 GVArray color_attribute_point = *attributes.lookup(name, bke::AttrDomain::Point);
189 GVArray color_attribute_corner = attributes.adapt_domain(
190 color_attribute_point, bke::AttrDomain::Point, bke::AttrDomain::Corner);
191 color_attribute_corner.materialize(selection, attribute.span.data());
192 }
193 attribute.finish();
194}
195
197{
198 Mesh *mesh;
199 if (((mesh = BKE_mesh_from_object(&ob)) == nullptr) ||
200 (ED_mesh_color_ensure(mesh, nullptr) == false))
201 {
202 return false;
203 }
204
205 IndexMaskMemory memory;
206 const IndexMask selection = get_selected_indices(*mesh, bke::AttrDomain::Corner, memory);
207
209
211
212 return true;
213}
214
216{
218 if (vertex_color_smooth(*obact)) {
220 return OPERATOR_FINISHED;
221 }
222 return OPERATOR_CANCELLED;
223}
224
226{
227 /* identifiers */
228 ot->name = "Smooth Vertex Colors";
229 ot->idname = "PAINT_OT_vertex_color_smooth";
230 ot->description = "Smooth colors across vertices";
231
232 /* API callbacks. */
235
236 /* flags */
238}
239
241
242/* -------------------------------------------------------------------- */
245
247 Mesh &mesh, const FunctionRef<void(ColorGeometry4f &color)> transform_fn)
248{
249 const StringRef name = mesh.active_color_attribute;
250 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
251 if (!attributes.contains(name)) {
253 return;
254 }
255
256 bke::GAttributeWriter color_attribute = attributes.lookup_for_write(name);
257 if (!color_attribute) {
259 return;
260 }
261
262 IndexMaskMemory memory;
263 const IndexMask selection = get_selected_indices(mesh, color_attribute.domain, memory);
264
265 selection.foreach_segment(
266 [&](const IndexMaskSegment segment) {
268 [&]<typename T>() {
269 for ([[maybe_unused]] const int i : segment) {
270 if constexpr (std::is_same_v<T, ColorGeometry4f>) {
271 ColorGeometry4f color = color_attribute.varray.get<ColorGeometry4f>(i);
272 transform_fn(color);
273 color_attribute.varray.set_by_copy(i, &color);
274 }
275 else if constexpr (std::is_same_v<T, ColorGeometry4b>) {
277 color_attribute.varray.get<ColorGeometry4b>(i));
278 transform_fn(color);
279 ColorGeometry4b color_encoded = color::encode(color);
280 color_attribute.varray.set_by_copy(i, &color_encoded);
281 }
282 }
283 });
284 },
286
287 color_attribute.finish();
288
290}
291
293 const FunctionRef<void(ColorGeometry4f &color)> transform_fn)
294{
295 using namespace blender::ed::sculpt_paint;
297
298 /* Ensure valid sculpt state. */
300
302
303 IndexMaskMemory memory;
304 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
305
306 Mesh &mesh = *id_cast<Mesh *>(obact.data);
307 transform_active_color_data(mesh, transform_fn);
308
309 pbvh.tag_attribute_changed(node_mask, mesh.active_color_attribute);
310
312}
313
315{
317
318 float gain, offset;
319 {
320 float brightness = RNA_float_get(op->ptr, "brightness");
321 float contrast = RNA_float_get(op->ptr, "contrast");
322 brightness /= 100.0f;
323 float delta = contrast / 200.0f;
324 /*
325 * The algorithm is by Werner D. Streidt
326 * (http://visca.com/ffactory/archives/5-99/msg00021.html)
327 * Extracted of OpenCV `demhist.c`.
328 */
329 if (contrast > 0) {
330 gain = 1.0f - delta * 2.0f;
331 gain = 1.0f / max_ff(gain, FLT_EPSILON);
332 offset = gain * (brightness - delta);
333 }
334 else {
335 delta *= -1;
336 gain = max_ff(1.0f - delta * 2.0f, 0.0f);
337 offset = gain * brightness + delta;
338 }
339 }
340
341 Mesh *mesh;
342 if (((mesh = BKE_mesh_from_object(obact)) == nullptr) ||
343 (ED_mesh_color_ensure(mesh, nullptr) == false))
344 {
345 return OPERATOR_CANCELLED;
346 }
347
349 for (int i = 0; i < 3; i++) {
350 color[i] = gain * color[i] + offset;
351 }
352 });
353
354 return OPERATOR_FINISHED;
355}
356
358{
359 PropertyRNA *prop;
360
361 /* identifiers */
362 ot->name = "Vertex Paint Brightness/Contrast";
363 ot->idname = "PAINT_OT_vertex_color_brightness_contrast";
364 ot->description = "Adjust vertex color brightness/contrast";
365
366 /* API callbacks. */
369
370 /* flags */
372
373 /* params */
374 const float min = -100, max = +100;
375 prop = RNA_def_float(ot->srna, "brightness", 0.0f, min, max, "Brightness", "", min, max);
376 prop = RNA_def_float(ot->srna, "contrast", 0.0f, min, max, "Contrast", "", min, max);
377 RNA_def_property_ui_range(prop, min, max, 1, 1);
378}
379
381{
383
384 const float hue = RNA_float_get(op->ptr, "h");
385 const float sat = RNA_float_get(op->ptr, "s");
386 const float val = RNA_float_get(op->ptr, "v");
387
388 Mesh *mesh;
389 if (((mesh = BKE_mesh_from_object(obact)) == nullptr) ||
390 (ED_mesh_color_ensure(mesh, nullptr) == false))
391 {
392 return OPERATOR_CANCELLED;
393 }
394
396 float hsv[3];
397 rgb_to_hsv_v(color, hsv);
398
399 hsv[0] += (hue - 0.5f);
400 if (hsv[0] > 1.0f) {
401 hsv[0] -= 1.0f;
402 }
403 else if (hsv[0] < 0.0f) {
404 hsv[0] += 1.0f;
405 }
406 hsv[1] *= sat;
407 hsv[2] *= val;
408
409 hsv_to_rgb_v(hsv, color);
410 });
411
412 return OPERATOR_FINISHED;
413}
414
416{
417 /* identifiers */
418 ot->name = "Vertex Paint Hue/Saturation/Value";
419 ot->idname = "PAINT_OT_vertex_color_hsv";
420 ot->description = "Adjust vertex color Hue/Saturation/Value";
421
422 /* API callbacks. */
425
426 /* flags */
428
429 /* params */
430 RNA_def_float(ot->srna, "h", 0.5f, 0.0f, 1.0f, "Hue", "", 0.0f, 1.0f);
431 RNA_def_float(ot->srna, "s", 1.0f, 0.0f, 2.0f, "Saturation", "", 0.0f, 2.0f);
432
433 ot->prop = RNA_def_float(ot->srna, "v", 1.0f, 0.0f, 2.0f, "Value", "", 0.0f, 2.0f);
435}
436
438{
440
441 Mesh *mesh;
442 if (((mesh = BKE_mesh_from_object(obact)) == nullptr) ||
443 (ED_mesh_color_ensure(mesh, nullptr) == false))
444 {
445 return OPERATOR_CANCELLED;
446 }
447
449 for (int i = 0; i < 3; i++) {
450 color[i] = 1.0f - color[i];
451 }
452 });
453
454 return OPERATOR_FINISHED;
455}
456
458{
459 /* identifiers */
460 ot->name = "Vertex Paint Invert";
461 ot->idname = "PAINT_OT_vertex_color_invert";
462 ot->description = "Invert RGB values";
463
464 /* API callbacks. */
467
468 /* flags */
470}
471
473{
475
476 const float gain = RNA_float_get(op->ptr, "gain");
477 const float offset = RNA_float_get(op->ptr, "offset");
478
479 Mesh *mesh;
480 if (((mesh = BKE_mesh_from_object(obact)) == nullptr) ||
481 (ED_mesh_color_ensure(mesh, nullptr) == false))
482 {
483 return OPERATOR_CANCELLED;
484 }
485
487 for (int i = 0; i < 3; i++) {
488 color[i] = gain * (color[i] + offset);
489 }
490 });
491
493
494 return OPERATOR_FINISHED;
495}
496
498{
499 /* identifiers */
500 ot->name = "Vertex Paint Levels";
501 ot->idname = "PAINT_OT_vertex_color_levels";
502 ot->description = "Adjust levels of vertex colors";
503
504 /* API callbacks. */
507
508 /* flags */
510
511 /* params */
513 ot->srna, "offset", 0.0f, -1.0f, 1.0f, "Offset", "Value to add to colors", -1.0f, 1.0f);
515 ot->srna, "gain", 1.0f, 0.0f, FLT_MAX, "Gain", "Value to multiply colors by", 0.0f, 10.0f);
516}
517
519
520} // namespace blender
#define BLT_I18NCONTEXT_COLOR
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define ND_DRAW
Definition WM_types.hh:502
#define NC_OBJECT
Definition WM_types.hh:392
#define BLI_assert_unreachable()
#define ELEM(...)
btScalar max
void to_static_type(Fn &&fn) const
const void * data() const
void materialize(void *dst) const
void get(int64_t index, void *r_value) const
void set_all(const void *src)
void set_by_copy(int64_t index, const void *value)
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
static IndexMask from_bools_inverse(Span< bool > bools, LinearAllocator<> &memory)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
bool contains(StringRef name) const
GVArray adapt_domain(const GVArray &varray, const AttrDomain from_domain, const AttrDomain to_domain) const
GAttributeReader lookup(const StringRef name) const
GAttributeWriter lookup_for_write(StringRef name)
GSpanAttributeWriter lookup_for_write_span(StringRef name)
void foreach_segment(Fn &&fn, Mode mode=exec_mode::serial) const
#define FLT_MAX
#define FLT_EPSILON
#define attribute(slot)
#define T
pbvh::Tree * pbvh_get(Object &object)
IndexMask all_leaf_nodes(const Tree &pbvh, IndexMaskMemory &memory)
Definition pbvh.cc:2522
AttrType cpp_type_to_attribute_type(const CPPType &type)
BLI_INLINE ColorSceneLinear4f< Alpha > decode(const ColorSceneLinearByteEncoded4b< Alpha > &color)
Definition BLI_color.hh:77
BLI_INLINE ColorSceneLinearByteEncoded4b< Alpha > encode(const ColorSceneLinear4f< Alpha > &color)
Definition BLI_color.hh:50
constexpr ParallelGrainSize grain_size(int grain_size)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static bool vertex_paint_from_weight(Object &ob)
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
static wmOperatorStatus vertex_paint_from_weight_exec(bContext *C, wmOperator *)
static wmOperatorStatus vertex_color_smooth_exec(bContext *C, wmOperator *)
void RNA_def_property_ui_range(PropertyRNA *prop, double min, double max, double step, int precision)
static void transform_active_color(bContext *C, const FunctionRef< void(ColorGeometry4f &color)> transform_fn)
static wmOperatorStatus vertex_color_levels_exec(bContext *C, wmOperator *op)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
@ ME_EDIT_PAINT_VERT_SEL
@ ME_EDIT_PAINT_FACE_SEL
static void face_corner_color_equalize_verts(Mesh &mesh, const IndexMask selection)
void PAINT_OT_vertex_color_hsv(wmOperatorType *ot)
void DEG_id_tag_update(ID *id, unsigned int flags)
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition math_color.cc:61
BMesh const char void * data
Mesh * BKE_mesh_from_object(Object *ob)
void BKE_mesh_batch_cache_dirty_tag(Mesh *mesh, eMeshBatchDirtyMode mode)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
static void transform_active_color_data(Mesh &mesh, const FunctionRef< void(ColorGeometry4f &color)> transform_fn)
void PAINT_OT_vertex_color_from_weight(wmOperatorType *ot)
void PAINT_OT_vertex_color_levels(wmOperatorType *ot)
static void tag_object_after_update(Object &object)
void PAINT_OT_vertex_color_invert(wmOperatorType *ot)
static bool vertex_color_smooth(Object &ob)
bool ED_mesh_color_ensure(Mesh *mesh, const char *name)
Definition mesh_data.cc:331
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
void PAINT_OT_vertex_color_smooth(wmOperatorType *ot)
static wmOperatorStatus vertex_color_invert_exec(bContext *C, wmOperator *)
bool vertex_paint_mode_poll(bContext *C)
MINLINE float max_ff(float a, float b)
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
static wmOperatorStatus vertex_color_hsv_exec(bContext *C, wmOperator *op)
void BKE_sculptsession_update_for_edit(Depsgraph *depsgraph, Object *ob_orig, bool is_paint_tool)
static IndexMask get_selected_indices(const Mesh &mesh, const bke::AttrDomain domain, IndexMaskMemory &memory)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
void PAINT_OT_vertex_color_brightness_contrast(wmOperatorType *ot)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ BKE_MESH_BATCH_DIRTY_ALL
Definition BKE_mesh.h:48
static wmOperatorStatus vertex_color_brightness_contrast_exec(bContext *C, wmOperator *op)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
Object * CTX_data_active_object(const bContext *C)
float RNA_float_get(PointerRNA *ptr, const char *name)
static bool vertex_weight_paint_mode_poll(bContext *C)
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
#define min(a, b)
Definition sort.cc:38
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