Blender V5.3
paint_vertex_weight_utils.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
10
11#include <algorithm>
12
13#include "BLI_listbase.hh"
14#include "BLI_string_utils.hh"
15
16#include "DNA_armature_types.h"
17#include "DNA_mesh_types.h"
18#include "DNA_object_types.h"
19
20#include "BKE_action.hh"
21#include "BKE_context.hh"
22#include "BKE_deform.hh"
23#include "BKE_mesh.hh"
24#include "BKE_modifier.hh"
25#include "BKE_object.hh"
26#include "BKE_object_deform.h"
27#include "BKE_report.hh"
28
30
31/* Only for blend modes. */
32#include "IMB_imbuf.hh"
33
34#include "WM_api.hh"
35#include "WM_types.hh"
36
37#include "../paint_intern.hh" /* own include */
38
39namespace blender {
40
41/* -------------------------------------------------------------------- */
44
46 ReportList *reports,
47 eWPaintFlag flag,
48 WPaintVGroupIndex *vgroup_index)
49{
51 C, CTX_data_main(C), CTX_data_active_object(C), reports, flag, vgroup_index);
52}
54 Main *bmain,
55 Object *ob,
56 ReportList *reports,
57 eWPaintFlag flag,
58 WPaintVGroupIndex *vgroup_index)
59{
61
62 if (vgroup_index) {
63 vgroup_index->active = -1;
64 vgroup_index->mirror = -1;
65 }
66
68 return false;
69 }
70
71 if (mesh == nullptr || mesh->faces_num == 0) {
72 return false;
73 }
74
75 /* If nothing was added yet, we make deform-verts and a vertex deform group. */
76 if (mesh->deform_verts().is_empty()) {
79 }
80
81 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
82
83 /* this happens on a Bone select, when no vgroup existed yet */
84 if (mesh->vertex_group_active_index <= 0) {
85 Object *modob;
86 if ((modob = BKE_modifiers_is_deformed_by_armature(ob))) {
87 Bone *actbone = (id_cast<bArmature *>(modob->data))->act_bone;
88 if (actbone) {
89 bPoseChannel *pchan = BKE_pose_channel_find_name(modob->pose, actbone->name);
90
91 if (pchan) {
93 if (dg == nullptr) {
94 dg = BKE_object_defgroup_add_name(ob, pchan->name); /* sets actdef */
96 }
97 else {
98
99 int actdef = 1 + BLI_findindex(defbase, dg);
100 BLI_assert(actdef >= 0);
101 mesh->vertex_group_active_index = actdef;
102 }
103 }
104 }
105 }
106 }
107 if (defbase->is_empty()) {
110 }
111
112 /* ensure we don't try paint onto an invalid group */
113 if (mesh->vertex_group_active_index <= 0) {
114 BKE_report(reports, RPT_WARNING, "No active vertex group for painting, aborting");
115 return false;
116 }
117
118 if (vgroup_index) {
119 vgroup_index->active = mesh->vertex_group_active_index - 1;
120 }
121
122 if (flag & WPAINT_ENSURE_MIRROR) {
124 int mirror = ED_wpaint_mirror_vgroup_ensure(ob, mesh->vertex_group_active_index - 1);
125 if (vgroup_index) {
126 vgroup_index->mirror = mirror;
127 }
128 }
129 }
130
131 return true;
132}
133
135
136int ED_wpaint_mirror_vgroup_ensure(Object *ob, const int vgroup_active)
137{
138 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
139 bDeformGroup *defgroup = static_cast<bDeformGroup *>(BLI_findlink(defbase, vgroup_active));
140
141 if (defgroup) {
142 int mirrdef;
143 char name_flip[MAXBONENAME];
144
145 BLI_string_flip_side_name(name_flip, defgroup->name, false, sizeof(name_flip));
146 mirrdef = BKE_object_defgroup_name_index(ob, name_flip);
147 if (mirrdef == -1) {
148 if (BKE_object_defgroup_new(ob, name_flip)) {
149 mirrdef = defbase->count() - 1;
150 }
151 }
152
153 /* `mirrdef` shouldn't be -1 unless the object is a light & #BKE_object_defgroup_new fails. */
154 return mirrdef;
155 }
156
157 return -1;
158}
159
160/* -------------------------------------------------------------------- */
163
164BLI_INLINE float wval_blend(const float weight, const float paintval, const float alpha)
165{
166 const float talpha = std::min(alpha, 1.0f); /* blending with values over 1 doesn't make sense */
167 return (paintval * talpha) + (weight * (1.0f - talpha));
168}
169BLI_INLINE float wval_add(const float weight, const float paintval, const float alpha)
170{
171 return weight + (paintval * alpha);
172}
173BLI_INLINE float wval_sub(const float weight, const float paintval, const float alpha)
174{
175 return weight - (paintval * alpha);
176}
177BLI_INLINE float wval_mul(const float weight, const float paintval, const float alpha)
178{ /* first mul, then blend the fac */
179 return ((1.0f - alpha) + (alpha * paintval)) * weight;
180}
181BLI_INLINE float wval_lighten(const float weight, const float paintval, const float alpha)
182{
183 return (weight < paintval) ? wval_blend(weight, paintval, alpha) : weight;
184}
185BLI_INLINE float wval_darken(const float weight, const float paintval, const float alpha)
186{
187 return (weight > paintval) ? wval_blend(weight, paintval, alpha) : weight;
188}
189
190/* mainly for color */
191BLI_INLINE float wval_colordodge(float weight, float paintval, float fac)
192{
193 float mfac, temp;
194 if (fac == 0.0f) {
195 return weight;
196 }
197 mfac = 1.0f - fac;
198 temp = (paintval == 1.0f) ? 1.0f :
199 std::min((weight * (225.0f / 255.0f)) / (1.0f - paintval), 1.0f);
200 return mfac * weight + temp * fac;
201}
202BLI_INLINE float wval_difference(float weight, float paintval, float fac)
203{
204 float mfac, temp;
205 if (fac == 0.0f) {
206 return weight;
207 }
208 mfac = 1.0f - fac;
209 temp = fabsf(weight - paintval);
210 return mfac * weight + temp * fac;
211}
212BLI_INLINE float wval_screen(float weight, float paintval, float fac)
213{
214 float mfac, temp;
215 if (fac == 0.0f) {
216 return weight;
217 }
218 mfac = 1.0f - fac;
219 temp = std::max(1.0f - ((1.0f - weight) * (1.0f - paintval)), 0.0f);
220 return mfac * weight + temp * fac;
221}
222BLI_INLINE float wval_hardlight(float weight, float paintval, float fac)
223{
224 float mfac, temp;
225 if (fac == 0.0f) {
226 return weight;
227 }
228 mfac = 1.0f - fac;
229 if (paintval > 0.5f) {
230 temp = 1.0f - ((1.0f - 2.0f * (paintval - 0.5f)) * (1.0f - weight));
231 }
232 else {
233 temp = (2.0f * paintval * weight);
234 }
235 return mfac * weight + temp * fac;
236}
237BLI_INLINE float wval_overlay(float weight, float paintval, float fac)
238{
239 float mfac, temp;
240 if (fac == 0.0f) {
241 return weight;
242 }
243 mfac = 1.0f - fac;
244 if (weight > 0.5f) {
245 temp = 1.0f - ((1.0f - 2.0f * (weight - 0.5f)) * (1.0f - paintval));
246 }
247 else {
248 temp = (2.0f * paintval * weight);
249 }
250 return mfac * weight + temp * fac;
251}
252BLI_INLINE float wval_softlight(float weight, float paintval, float fac)
253{
254 float mfac, temp;
255 if (fac == 0.0f) {
256 return weight;
257 }
258 mfac = 1.0f - fac;
259 if (weight < 0.5f) {
260 temp = ((2.0f * ((paintval / 2.0f) + 0.25f)) * weight);
261 }
262 else {
263 temp = 1.0f - (2.0f * (1.0f - ((paintval / 2.0f) + 0.25f)) * (1.0f - weight));
264 }
265 return temp * fac + weight * mfac;
266}
267BLI_INLINE float wval_exclusion(float weight, float paintval, float fac)
268{
269 float mfac, temp;
270 if (fac == 0.0f) {
271 return weight;
272 }
273 mfac = 1.0f - fac;
274 temp = 0.5f - (2.0f * (weight - 0.5f) * (paintval - 0.5f));
275 return temp * fac + weight * mfac;
276}
277
278float ED_wpaint_blend_tool(const int tool,
279 const float weight,
280 const float paintval,
281 const float alpha)
282{
283 switch (IMB_BlendMode(tool)) {
284 case IMB_BLEND_MIX:
285 return wval_blend(weight, paintval, alpha);
286 case IMB_BLEND_ADD:
287 return wval_add(weight, paintval, alpha);
288 case IMB_BLEND_SUB:
289 return wval_sub(weight, paintval, alpha);
290 case IMB_BLEND_MUL:
291 return wval_mul(weight, paintval, alpha);
293 return wval_lighten(weight, paintval, alpha);
294 case IMB_BLEND_DARKEN:
295 return wval_darken(weight, paintval, alpha);
296 /* Mostly make sense for color: support anyway. */
298 return wval_colordodge(weight, paintval, alpha);
300 return wval_difference(weight, paintval, alpha);
301 case IMB_BLEND_SCREEN:
302 return wval_screen(weight, paintval, alpha);
304 return wval_hardlight(weight, paintval, alpha);
306 return wval_overlay(weight, paintval, alpha);
308 return wval_softlight(weight, paintval, alpha);
310 return wval_exclusion(weight, paintval, alpha);
311 /* Only for color: just use blend. */
312 default:
313 return wval_blend(weight, paintval, alpha);
314 }
315}
316
318
319} // namespace blender
Blender kernel action and pose functionality.
support for deformation groups and hooks.
General operations, lookup, etc. for blender objects.
Functions for dealing with objects and deform verts, used by painting and tools.
#define MAXBONENAME
#define ME_USING_MIRROR_X_VERTEX_GROUPS(_me)
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define BLI_INLINE
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
struct MDeformVert * BKE_object_defgroup_data_create(struct ID *id)
BLI_INLINE float wval_sub(const float weight, const float paintval, const float alpha)
@ RPT_WARNING
Definition BKE_report.hh:40
BLI_INLINE float wval_darken(const float weight, const float paintval, const float alpha)
bool ED_wpaint_ensure_data(bContext *C, ReportList *reports, eWPaintFlag flag, WPaintVGroupIndex *vgroup_index)
const ListBaseT< bDeformGroup > * BKE_object_defgroup_list(const Object *ob)
Definition deform.cc:591
BLI_INLINE float wval_screen(float weight, float paintval, float fac)
BLI_INLINE float wval_lighten(const float weight, const float paintval, const float alpha)
bDeformGroup * BKE_object_defgroup_find_name(const Object *ob, StringRef name)
Definition deform.cc:530
BLI_INLINE float wval_add(const float weight, const float paintval, const float alpha)
@ IMB_BLEND_HARDLIGHT
Definition IMB_imbuf.hh:213
@ IMB_BLEND_SOFTLIGHT
Definition IMB_imbuf.hh:218
@ IMB_BLEND_SCREEN
Definition IMB_imbuf.hh:217
@ IMB_BLEND_DARKEN
Definition IMB_imbuf.hh:209
@ IMB_BLEND_MUL
Definition IMB_imbuf.hh:207
@ IMB_BLEND_COLORDODGE
Definition IMB_imbuf.hh:216
@ IMB_BLEND_DIFFERENCE
Definition IMB_imbuf.hh:222
@ IMB_BLEND_SUB
Definition IMB_imbuf.hh:206
@ IMB_BLEND_OVERLAY
Definition IMB_imbuf.hh:212
@ IMB_BLEND_EXCLUSION
Definition IMB_imbuf.hh:223
@ IMB_BLEND_MIX
Definition IMB_imbuf.hh:204
@ IMB_BLEND_LIGHTEN
Definition IMB_imbuf.hh:208
@ IMB_BLEND_ADD
Definition IMB_imbuf.hh:205
Object * BKE_modifiers_is_deformed_by_armature(Object *ob)
BLI_INLINE float wval_mul(const float weight, const float paintval, const float alpha)
BLI_INLINE float wval_blend(const float weight, const float paintval, const float alpha)
int ED_wpaint_mirror_vgroup_ensure(Object *ob, const int vgroup_active)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
BLI_INLINE float wval_colordodge(float weight, float paintval, float fac)
BLI_INLINE float wval_hardlight(float weight, float paintval, float fac)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
Mesh * BKE_mesh_from_object(Object *ob)
BLI_INLINE float wval_overlay(float weight, float paintval, float fac)
BLI_INLINE float wval_softlight(float weight, float paintval, float fac)
int BKE_object_defgroup_name_index(const Object *ob, StringRef name)
Definition deform.cc:597
struct bDeformGroup * BKE_object_defgroup_add_name(struct Object *ob, const char *name)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
BLI_INLINE float wval_difference(float weight, float paintval, float fac)
bool BKE_object_is_in_editmode(const Object *ob)
Definition object.cc:1898
Main * CTX_data_main(const bContext *C)
bDeformGroup * BKE_object_defgroup_new(Object *ob, StringRef name)
Definition deform.cc:51
struct bDeformGroup * BKE_object_defgroup_add(struct Object *ob)
float ED_wpaint_blend_tool(const int tool, const float weight, const float paintval, const float alpha)
void DEG_relations_tag_update(Main *bmain)
Object * CTX_data_active_object(const bContext *C)
BLI_INLINE float wval_exclusion(float weight, float paintval, float fac)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
@ WPAINT_ENSURE_MIRROR
size_t BLI_string_flip_side_name(char *name_dst, const char *name_src, bool strip_number, size_t name_dst_maxncpy) ATTR_NONNULL(1
#define fabsf
struct bPose * pose