GPencil: Several Weight Paint additions #106663

Merged
Falk David merged 11 commits from SietseB/blender:gp-weight-paint-additions into main 2023-04-20 07:55:37 +02:00
Member

This patch adds several tools and options to the weight paint mode of Grease Pencil.

More tools

  • Blur tool: smooths out vertex weights, by calculating a gaussian blur of adjacent vertices.
  • Average tool: painting the average weight from all weights under the brush.
  • Smear tool: smudges weights by grabbing the weights under the brush and 'dragging' them.

New weight paint tools.png

Draw tool: switch easily between 'add' and 'subtract'

With the + and - icons in the toolbar, the user can easily switch between adding and subtracting weight while drawing weights.
With shortcut D you can toggle between these two.

New option: auto-normalize

The auto-normalize options ensures that all bone-deforming vertex groups add up to 1.0 while weight painting.

Auto-normalize bone-deformed vertex groups.png

Radial control for weight

With Ctrl-F a radial control for weight is invoked (in addition to the radial controls for brush size and strength).

Sample weight

With Ctrl-RMB the user can sample the weight. This sets the brush Weight from the weight under the cursor.

Quick bone selection

When painting weights in vertex groups for bones, the user can quickly switch to another vertex group by clicking on a bone with Ctrl-LMB.
For this to work, follow these steps:

  • Select the armature and switch to Pose Mode.
  • Select your Grease Pencil object and switch immediately to Weight Paint Mode.
  • Select a bone in the armature with Ctrl-LMB. The corresponding vertex group is automatically activated.

See attached video for a demo.

Technical note

Where possible, the same logic and conventions as in the 3D weight paint section of Blender were followed.

This patch adds several tools and options to the weight paint mode of Grease Pencil. ## More tools * Blur tool: smooths out vertex weights, by calculating a gaussian blur of adjacent vertices. * Average tool: painting the average weight from all weights under the brush. * Smear tool: smudges weights by grabbing the weights under the brush and 'dragging' them. ![New weight paint tools.png](/attachments/8ee6c01e-60d4-497e-989a-4e73129c38ed) ## Draw tool: switch easily between 'add' and 'subtract' With the + and - icons in the toolbar, the user can easily switch between adding and subtracting weight while drawing weights. With shortcut `D` you can toggle between these two. ## New option: auto-normalize The auto-normalize options ensures that all bone-deforming vertex groups add up to 1.0 while weight painting. ![Auto-normalize bone-deformed vertex groups.png](/attachments/a8c503c5-622d-4eee-8564-919d728acc1a) ## Radial control for weight With `Ctrl-F` a radial control for weight is invoked (in addition to the radial controls for brush size and strength). ## Sample weight With `Ctrl-RMB` the user can sample the weight. This sets the brush Weight from the weight under the cursor. ## Quick bone selection When painting weights in vertex groups for bones, the user can quickly switch to another vertex group by clicking on a bone with `Ctrl-LMB`. For this to work, follow these steps: * Select the armature and switch to Pose Mode. * Select your Grease Pencil object and switch immediately to Weight Paint Mode. * Select a bone in the armature with `Ctrl-LMB`. The corresponding vertex group is automatically activated. > See attached video for a demo. ## Technical note Where possible, the same logic and conventions as in the 3D weight paint section of Blender were followed.
Sietse Brouwer added 2 commits 2023-04-07 15:13:55 +02:00
fa8feeec2d Gpencil: several weight paint additions
This patch adds several tools and options to the weight paint mode of
Grease Pencil.

More tools
- Blur tool: smooths out vertex weights, by calculating a gaussian blur
of adjacent vertices.
- Average tool: painting the average weight from all weights under the
brush.
- Smear tool: smudges weights by grabbing the weights under the brush
and 'dragging' them.

Draw tool: switch easily between 'add' and 'subtract'
With the + and - icons in the toolbar, the user can easily switch
between adding and subtracting weight while drawing weights.
With shortcut 'D' you can toggle between these two.

New option: auto-normalize
The auto-normalize options ensures that all bone-deforming vertex
groups add up to 1.0 while weight painting.

Radial control for weight
With Ctrl-F a radial control for weight is invoked (in addition
to the radial controls for brush size and strength).

Sample weight
With Ctrl-RMB the user can sample the weight. This sets the brush
Weight from the weight under the cursor.

Quick bone selection
When painting weights in vertex groups for bones, the user can
quickly switch to another vertex group by clicking on a bone with
Ctrl-LMB.
For this to work, follow these steps:
- Select the armature and switch to Pose Mode.
- Select your Grease Pencil object and switch immediately to
Weight Paint Mode.
- Select a bone in the armature with Ctrl-LMB. The corresponding
vertex group is automatically activated.

Technical note: where possible, the same logic and conventions
as in the 3D weight paint section of Blender were followed.
Sietse Brouwer requested review from Antonio Vazquez 2023-04-07 15:22:09 +02:00
Sietse Brouwer requested review from Falk David 2023-04-07 15:22:09 +02:00
Antonio Vazquez added the
Module
Grease Pencil
label 2023-04-07 16:25:31 +02:00
Antonio Vazquez added this to the (deleted) project 2023-04-07 16:26:22 +02:00
Antonio Vazquez modified the project from (deleted) to Grease Pencil 2023-04-07 16:26:50 +02:00
Member

Thank you for the PR :)
Unfortunately, since the GP 3.0 project has started, we try to not change the current behavior of grease pencil. However, it looks like this code will be easy to port to the new data structure. So I would put this on hold and then integrate it once weight painting is there in GP 3.0.

Thank you for the PR :) Unfortunately, since the GP 3.0 project has started, we try to not change the current behavior of grease pencil. However, it looks like this code will be easy to port to the new data structure. So I would put this on hold and then integrate it once weight painting is there in GP 3.0.

I've already put it on the Grease Pencil workboard.

I've already put it on the Grease Pencil workboard.
Author
Member

I don't know how the GP 3.0 refactor will be organized after the first milestone is reached (core devs only or a community project), but if it is of any use, I could assist in the refactor of weight paint code - since I am acquainted with it now :-)

I don't know how the GP 3.0 refactor will be organized after the first milestone is reached (core devs only or a community project), but if it is of any use, I could assist in the refactor of weight paint code - since I am acquainted with it now :-)
Member

It will be a community effort, so help in getting the weight painting code up to speed will be very welcome!

It will be a community effort, so help in getting the weight painting code up to speed will be very welcome!
Sietse Brouwer added 1 commit 2023-04-08 12:38:11 +02:00
Sietse Brouwer added 1 commit 2023-04-09 15:37:08 +02:00
Falk David requested changes 2023-04-18 15:27:43 +02:00
Falk David left a comment
Member

First review pass on this. Overall the code already looks very good 👍 I still need to do some more testing though.

First review pass on this. Overall the code already looks very good 👍 I still need to do some more testing though.
@ -1417,0 +1418,4 @@
gp_settings = brush.gpencil_settings
text = "" if compact else "Direction"
layout.prop(gp_settings, "direction", expand=True, text=text)
Member

I think in this case it's nicer to inline the if compact else.
layout.prop(gp_settings, "direction", expand=True, text="" if compact else "Direction")

I think in this case it's nicer to inline the `if compact else`. `layout.prop(gp_settings, "direction", expand=True, text="" if compact else "Direction")`
filedescriptor marked this conversation as resolved
@ -45,6 +45,7 @@ def generate_from_enum_ex(
dict(
idname=idname_prefix + name,
label=name,
description=(enum.description),
Member

No need for the parentheses.

No need for the parentheses.
@ -4253,1 +4253,4 @@
if (!MAIN_VERSION_ATLEAST(bmain, 306, 5)) {
/* Rename Grease Pencil weight draw brush. */
do_versions_rename_id(bmain, ID_BR, "Draw Weight", "Weight Draw");
Member

Would like to confirm that this works correctly.

Would like to confirm that this works correctly.
@ -164,2 +235,2 @@
if (buffer_array != NULL) {
memset(buffer_array, 0, sizeof(tGP_Selected) * *buffer_size);
/* Ensure a vertex group for the weight paint brush. */
static void gpencil_vertex_group_ensure(tGP_BrushWeightpaintData *gso)
Member

This function should use the "early return" pattern.
E.g.

if (gso->vrgroup > 0) {
  return;
}
if ...
This function should use the "early return" pattern. E.g. ``` if (gso->vrgroup > 0) { return; } if ... ```
Member

I meant to say that this whole function should use this pattern. It can be rewritten like so:

static void gpencil_vertex_group_ensure(tGP_BrushWeightpaintData *gso)
{
  if (gso->vrgroup >= 0) {
    return;
  }
  if (gso->object == NULL) {
    return;
  }
  
  DEG_relations_tag_update(gso->bmain);
  gso->vrgroup = 0;
    
  Object *ob_armature = BKE_modifiers_is_deformed_by_armature(gso->object);
  if (ob_armature == NULL) {
    BKE_object_defgroup_add(gso->object);
    return;
  }
  Bone *actbone = ((bArmature *)ob_armature->data)->act_bone;
  if (actbone == NULL) {
    return;
  }
  bPoseChannel *pchan = BKE_pose_channel_find_name(ob_armature->pose, actbone->name);
  if (pchan == NULL) {
    return;
  }
  bDeformGroup *dg = BKE_object_defgroup_find_name(gso->object, pchan->name);
  if (dg == NULL) {
    dg = BKE_object_defgroup_add_name(gso->object, pchan->name);
  }
}

There are a few things that are suspicious to me in this. Like tagging all the relations in bmain and the fact that the return value of BKE_object_defgroup_add_name is unused.

I meant to say that this whole function should use this pattern. It can be rewritten like so: ``` static void gpencil_vertex_group_ensure(tGP_BrushWeightpaintData *gso) { if (gso->vrgroup >= 0) { return; } if (gso->object == NULL) { return; } DEG_relations_tag_update(gso->bmain); gso->vrgroup = 0; Object *ob_armature = BKE_modifiers_is_deformed_by_armature(gso->object); if (ob_armature == NULL) { BKE_object_defgroup_add(gso->object); return; } Bone *actbone = ((bArmature *)ob_armature->data)->act_bone; if (actbone == NULL) { return; } bPoseChannel *pchan = BKE_pose_channel_find_name(ob_armature->pose, actbone->name); if (pchan == NULL) { return; } bDeformGroup *dg = BKE_object_defgroup_find_name(gso->object, pchan->name); if (dg == NULL) { dg = BKE_object_defgroup_add_name(gso->object, pchan->name); } } ``` There are a few things that are suspicious to me in this. Like tagging all the relations in `bmain` and the fact that the return value of `BKE_object_defgroup_add_name` is unused.
Author
Member

I could have understood that the first time ;-)
Done. Removed the last dg =.

I didn't write this code myself, just moved it. Don't know much about the depsgraph, so I don't know if the DEG_relations_tag_update is entirely necessary.
I noticed some similarities in ED_wpaint_ensure_data in paint_vertex_weight_utils.c, including the unused dg =, so maybe some of the code was borrowed from there in the past.

I could have understood that the first time ;-) Done. Removed the last `dg =`. I didn't write this code myself, just moved it. Don't know much about the depsgraph, so I don't know if the `DEG_relations_tag_update` is entirely necessary. I noticed some similarities in `ED_wpaint_ensure_data` in `paint_vertex_weight_utils.c`, including the unused `dg =`, so maybe some of the code was borrowed from there in the past.
@ -166,0 +277,4 @@
/* Get boolean array of vertex groups deformed by GP armature modifiers.
* GP equivalent of `BKE_object_defgroup_validmap_get`.
*/
bool *gpencil_vgroup_bone_deformed_map_get(Object *ob, const int defbase_tot)
Member

Should be static bool *

Should be `static bool *`
@ -166,0 +324,4 @@
/* Mark vertex groups with reference in the bone hash table. */
vgroup_bone_deformed = MEM_mallocN(sizeof(*vgroup_bone_deformed) * defbase_tot,
"wpaint bone deformed map");
Member

Should use __func__ for the allocation name.

Should use `__func__` for the allocation name.
@ -248,0 +632,4 @@
/* Calculate the average (=blurred) weight. */
float blur_weight = 0.0f, dist_sum = 0.0f;
int i, count = 0;
Member

Move declaration of i in the for loop(s).

Move declaration of `i` in the `for` loop(s).
@ -248,0 +684,4 @@
float point_dot[8] = {0};
float point_dir[2];
float score_max = 0.0f, dist_min = FLT_MAX, dist_max = 0.0f;
int i_max = -1, count = 0, i;
Member

Same as above, move declaration of i inside the loop.

Same as above, move declaration of `i` inside the loop.
@ -526,3 +1129,4 @@
* required to do some step. Now is not used, but the operator is ready.
*--------------------------------------------------------------------- */
int gps_index = -1;
LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
Member

Should use LISTBASE_FOREACH_INDEX.

Should use `LISTBASE_FOREACH_INDEX`.
@ -3095,2 +3091,2 @@
* are in object mode it might falsely toggle object selection. Alternatively, this could be
* put in the poll function instead. */
if (obact && obact->type == OB_GPENCIL_LEGACY &&
GPENCIL_NON_WEIGHT_MODE((bGPdata *)obact->data)) {
Member

No need for a new macro, this should just be (obact->data && (gpd->flag & GP_DATA_STROKE_WEIGHTMODE == 0))

No need for a new macro, this should just be `(obact->data && (gpd->flag & GP_DATA_STROKE_WEIGHTMODE == 0))`
Author
Member

It's a bit tricky one. Selection is allowed in Weight Mode and Object Mode, so that's the test here. But there is no active way to test for Object Mode, AFAIK. Hence the macro...
All those macro names are a bit obscure, none of them mentions Object Mode. So I followed that convention. But I can easily rename it, of course. Or you even have a better solution...

It's a bit tricky one. Selection is allowed in Weight Mode **and** Object Mode, so that's the test here. But there is no active way to test for Object Mode, AFAIK. Hence the macro... All those macro names are a bit obscure, none of them mentions Object Mode. So I followed that convention. But I can easily rename it, of course. Or you even have a better solution...
Member

Alright, lets keep it like you have it for now.

Alright, lets keep it like you have it for now.
Member

Also needs a merge with main.

Also needs a merge with `main`.
Falk David changed title from Gpencil: several weight paint additions to GPencil: Several Weight Paint additions 2023-04-18 15:32:05 +02:00
Sietse Brouwer added 3 commits 2023-04-18 21:15:19 +02:00
Sietse Brouwer added 1 commit 2023-04-18 21:38:55 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
4a17adf09b
Cleanup: format
Author
Member

I processed the review comments (except for the last one, see my reaction).
And merged with main.

Oh yeah, I discovered a small error in the old code today: a double falloff was applied in the weight brush. I added a fix for that too.

I processed the review comments (except for the last one, see my reaction). And merged with `main`. Oh yeah, I discovered a small error in the old code today: a double falloff was applied in the weight brush. I added a fix for that too.
Falk David requested changes 2023-04-19 12:51:32 +02:00
Falk David left a comment
Member

Second pass. Some more smaller things that stood out to me.

Second pass. Some more smaller things that stood out to me.
@ -170,0 +409,4 @@
return true;
}
for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
Member

Declare i in this loop.

Declare `i` in this loop.
@ -170,0 +434,4 @@
if (sum_lock >= 1.0f - VERTEX_WEIGHT_LOCK_EPSILON) {
/* locked groups make it impossible to fully normalize,
* zero out what we can and return false */
for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
Member

Same as above.

Same as above.
@ -170,0 +448,4 @@
if (sum_unlock != 0.0f) {
fac = (1.0f - sum_lock) / sum_unlock;
for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
Member

Same as above.

Same as above.
@ -170,0 +462,4 @@
fac = (1.0f - sum_lock) / tot;
CLAMP(fac, 0.0f, 1.0f);
for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
Member

Same as above.

Same as above.
@ -911,0 +1642,4 @@
/* Look for two closest points. */
for (int i = 0; i < gps->totpoints; i++) {
bGPDspoint *pt = gps->points + i;
Member

Generally we do not use pointer arithmetic when accessing elements in an array. Use &gps->points[i].

Generally we do not use pointer arithmetic when accessing elements in an array. Use `&gps->points[i]`.
@ -911,0 +1652,4 @@
if ((dist < closest_dist[0]) || (dist < closest_dist[1])) {
/* Get weight. */
MDeformVert *dvert = gps->dvert + i;
Member

Same as above.

Same as above.
Member

@antoniov We need to make sure this gets added to the release notes and also update the manual once we merge.

@antoniov We need to make sure this gets added to the release notes and also update the manual once we merge.
Member

@blender-bot build

@blender-bot build
Sietse Brouwer added 3 commits 2023-04-19 15:47:21 +02:00
Author
Member

Second review processed. And merged with main again.

Second review processed. And merged with `main` again.
Falk David merged commit 397a14deff into main 2023-04-20 07:55:37 +02:00
Sietse Brouwer deleted branch gp-weight-paint-additions 2023-05-03 11:35:48 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#106663
No description provided.