GPv3: Vertex Group functions should make use of the VArray API #117753

Open
opened 2024-02-02 13:41:57 +01:00 by Falk David · 8 comments
Member

The functions in grease_pencil_vertex_groups.cc should make use of VArrayImpl_For_VertexWeights (see BKE_deform.hh).

  • bke::varray_for_deform_verts for reading
  • bke::varray_for_mutable_deform_verts for writing
  • Use bke::remove_defgroup_index to remove a group by its index.
The functions in `grease_pencil_vertex_groups.cc` should make use of `VArrayImpl_For_VertexWeights` (see `BKE_deform.hh`). * `bke::varray_for_deform_verts` for reading * `bke::varray_for_mutable_deform_verts` for writing * Use `bke::remove_defgroup_index` to remove a group by its index.
Falk David added the
Type
To Do
label 2024-02-02 13:41:58 +01:00
Falk David added this to the Grease Pencil project 2024-02-02 13:42:00 +01:00
Author
Member

For assign_to_vertex_group here are my changes (since assign_to_vertex_group is dealing with selection, the code should probably be in the editors file, rather than in the kernel):

diff --git a/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc b/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc
index 236a65bb879..46b2d9f94a7 100644
--- a/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc
+++ b/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc
@@ -20,6 +20,8 @@
 
 #include "BLT_translation.h"
 
+#include "ED_curves.hh"
+
 namespace blender::bke::greasepencil {
 
 /* ------------------------------------------------------------------- */
@@ -55,6 +57,19 @@ void validate_drawing_vertex_groups(GreasePencil &grease_pencil)
   }
 }
 
+int ensure_vertex_group(const StringRef name, ListBase &vertex_group_names)
+{
+  int def_nr = BLI_findstringindex(&vertex_group_names, name.data(), offsetof(bDeformGroup, name));
+  if (def_nr < 0) {
+    bDeformGroup *defgroup = MEM_cnew<bDeformGroup>(__func__);
+    STRNCPY(defgroup->name, name.data());
+    BLI_addtail(&vertex_group_names, defgroup);
+    def_nr = BLI_listbase_count(&vertex_group_names) - 1;
+    BLI_assert(def_nr >= 0);
+  }
+  return def_nr;
+}
+
 void assign_to_vertex_group(GreasePencil &grease_pencil, const StringRef name, const float weight)
 {
   for (GreasePencilDrawingBase *base : grease_pencil.drawings()) {
@@ -69,29 +84,20 @@ void assign_to_vertex_group(GreasePencil &grease_pencil, const StringRef name, c
     const VArray<bool> selection = *attributes.lookup_or_default<bool>(
         ".selection", bke::AttrDomain::Point, true);
 
-    /* Look for existing group, otherwise lazy-initialize if any vertex is selected. */
-    int def_nr = BLI_findstringindex(
-        &vertex_group_names, name.data(), offsetof(bDeformGroup, name));
-
-    const MutableSpan<MDeformVert> dverts = curves.deform_verts_for_write();
-    for (const int i : dverts.index_range()) {
-      if (selection[i]) {
-        /* Lazily add the vertex group if any vertex is selected. */
-        if (def_nr < 0) {
-          bDeformGroup *defgroup = MEM_cnew<bDeformGroup>(__func__);
-          STRNCPY(defgroup->name, name.data());
-
-          BLI_addtail(&vertex_group_names, defgroup);
-          def_nr = BLI_listbase_count(&vertex_group_names) - 1;
-          BLI_assert(def_nr >= 0);
-        }
-
-        MDeformWeight *dw = BKE_defvert_ensure_index(&dverts[i], def_nr);
-        if (dw) {
-          dw->weight = weight;
+    if (!ed::curves::has_anything_selected(curves)) {
+      continue;
+    }
+    
+    int def_nr = ensure_vertex_group(vertex_group_names, name);
+    VMutableArray<float> dverts = bke::varray_for_mutable_deform_verts(
+        curves.deform_verts_for_write(), def_nr);
+    threading::parallel_for(curves.points_range(), 1024, [&](const IndexRange range) {
+      for (const int point_i : range) {
+        if (selection[point_i]) {
+          dverts[point_i] = weight;
         }
       }
-    }
+    });
   }
 }
For `assign_to_vertex_group` here are my changes (since `assign_to_vertex_group` is dealing with selection, the code should probably be in the editors file, rather than in the kernel): ```diff diff --git a/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc b/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc index 236a65bb879..46b2d9f94a7 100644 --- a/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc +++ b/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc @@ -20,6 +20,8 @@ #include "BLT_translation.h" +#include "ED_curves.hh" + namespace blender::bke::greasepencil { /* ------------------------------------------------------------------- */ @@ -55,6 +57,19 @@ void validate_drawing_vertex_groups(GreasePencil &grease_pencil) } } +int ensure_vertex_group(const StringRef name, ListBase &vertex_group_names) +{ + int def_nr = BLI_findstringindex(&vertex_group_names, name.data(), offsetof(bDeformGroup, name)); + if (def_nr < 0) { + bDeformGroup *defgroup = MEM_cnew<bDeformGroup>(__func__); + STRNCPY(defgroup->name, name.data()); + BLI_addtail(&vertex_group_names, defgroup); + def_nr = BLI_listbase_count(&vertex_group_names) - 1; + BLI_assert(def_nr >= 0); + } + return def_nr; +} + void assign_to_vertex_group(GreasePencil &grease_pencil, const StringRef name, const float weight) { for (GreasePencilDrawingBase *base : grease_pencil.drawings()) { @@ -69,29 +84,20 @@ void assign_to_vertex_group(GreasePencil &grease_pencil, const StringRef name, c const VArray<bool> selection = *attributes.lookup_or_default<bool>( ".selection", bke::AttrDomain::Point, true); - /* Look for existing group, otherwise lazy-initialize if any vertex is selected. */ - int def_nr = BLI_findstringindex( - &vertex_group_names, name.data(), offsetof(bDeformGroup, name)); - - const MutableSpan<MDeformVert> dverts = curves.deform_verts_for_write(); - for (const int i : dverts.index_range()) { - if (selection[i]) { - /* Lazily add the vertex group if any vertex is selected. */ - if (def_nr < 0) { - bDeformGroup *defgroup = MEM_cnew<bDeformGroup>(__func__); - STRNCPY(defgroup->name, name.data()); - - BLI_addtail(&vertex_group_names, defgroup); - def_nr = BLI_listbase_count(&vertex_group_names) - 1; - BLI_assert(def_nr >= 0); - } - - MDeformWeight *dw = BKE_defvert_ensure_index(&dverts[i], def_nr); - if (dw) { - dw->weight = weight; + if (!ed::curves::has_anything_selected(curves)) { + continue; + } + + int def_nr = ensure_vertex_group(vertex_group_names, name); + VMutableArray<float> dverts = bke::varray_for_mutable_deform_verts( + curves.deform_verts_for_write(), def_nr); + threading::parallel_for(curves.points_range(), 1024, [&](const IndexRange range) { + for (const int point_i : range) { + if (selection[point_i]) { + dverts[point_i] = weight; } } - } + }); } } ```
Member

For that matter, it might be even nicer to use the attribute API here. Once the vertex group is created, it can be accessed as a float attribute on the point domain.

For that matter, it might be even nicer to use the attribute API here. Once the vertex group is created, it can be accessed as a float attribute on the point domain.
Author
Member

@HooglyBoogly True! Would be even better.

Lukas and I noticed that if the user creates a vertex group that has the same name as some attribute, that it would first find the attribute.
I guess the only solution to that would be to make sure that when a vertex group is created, that it doesn't have the same name as any other attribute. Not sure how this would work if the attribute was added afterwards though...

@HooglyBoogly True! Would be even better. Lukas and I noticed that if the user creates a vertex group that has the same name as some attribute, that it would first find the attribute. I guess the only solution to that would be to make sure that when a vertex group is created, that it doesn't have the same name as any other attribute. Not sure how this would work if the attribute was added afterwards though...
Member

Oh there is task already, didn't know that. Perhaps unrelated, I tried some changes (for simplification) during weekend while understanding the new vgroup structure). How about using Indexmask selection instead of looping all points in threading::parellel_for?:

Code Changes
diff --git a/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc b/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc
index 236a65bb879..101c3fa8a84 100644
--- a/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc
+++ b/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc
@@ -13,6 +13,7 @@
 #include "BLI_string.h"
 #include "BLI_string_utils.hh"

+#include "../editors\include\ED_curves.hh"
 #include "BKE_curves.hh"
 #include "BKE_deform.hh"
 #include "BKE_grease_pencil.hh"
@@ -66,32 +67,34 @@ void assign_to_vertex_group(GreasePencil &grease_pencil, const StringRef name, c
     ListBase &vertex_group_names = curves.vertex_group_names;

     const bke::AttributeAccessor attributes = curves.attributes();
-    const VArray<bool> selection = *attributes.lookup_or_default<bool>(
-        ".selection", bke::AttrDomain::Point, true);
+
+    IndexMaskMemory memory;
+    IndexMask selection = ed::curves::retrieve_selected_points(curves, memory);
+    if (selection.is_empty()) {
+      continue;
+    }

     /* Look for existing group, otherwise lazy-initialize if any vertex is selected. */
     int def_nr = BLI_findstringindex(
         &vertex_group_names, name.data(), offsetof(bDeformGroup, name));

     const MutableSpan<MDeformVert> dverts = curves.deform_verts_for_write();
-    for (const int i : dverts.index_range()) {
-      if (selection[i]) {
-        /* Lazily add the vertex group if any vertex is selected. */
-        if (def_nr < 0) {
-          bDeformGroup *defgroup = MEM_cnew<bDeformGroup>(__func__);
-          STRNCPY(defgroup->name, name.data());
-
-          BLI_addtail(&vertex_group_names, defgroup);
-          def_nr = BLI_listbase_count(&vertex_group_names) - 1;
-          BLI_assert(def_nr >= 0);
-        }

-        MDeformWeight *dw = BKE_defvert_ensure_index(&dverts[i], def_nr);
-        if (dw) {
-          dw->weight = weight;
-        }
+    selection.foreach_index([&](int point_i) {
+      if (def_nr < 0) {
+        bDeformGroup *defgroup = MEM_cnew<bDeformGroup>(__func__);
+        STRNCPY(defgroup->name, name.data());
+
+        BLI_addtail(&vertex_group_names, defgroup);
+        def_nr = BLI_listbase_count(&vertex_group_names) - 1;
+        BLI_assert(def_nr >= 0);
       }
-    }
+
+      MDeformWeight *dw = BKE_defvert_ensure_index(&dverts[point_i], def_nr);
+      if (dw) {
+        dw->weight = weight;
+      }
+    });
   }
 }
Oh there is task already, didn't know that. Perhaps unrelated, I tried some changes (for simplification) during weekend while understanding the new vgroup structure). How about using `Indexmask selection` instead of looping all points in `threading::parellel_for`?: <details> <summary> Code Changes </summary> ```Diff diff --git a/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc b/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc index 236a65bb879..101c3fa8a84 100644 --- a/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc +++ b/source/blender/blenkernel/intern/grease_pencil_vertex_groups.cc @@ -13,6 +13,7 @@ #include "BLI_string.h" #include "BLI_string_utils.hh" +#include "../editors\include\ED_curves.hh" #include "BKE_curves.hh" #include "BKE_deform.hh" #include "BKE_grease_pencil.hh" @@ -66,32 +67,34 @@ void assign_to_vertex_group(GreasePencil &grease_pencil, const StringRef name, c ListBase &vertex_group_names = curves.vertex_group_names; const bke::AttributeAccessor attributes = curves.attributes(); - const VArray<bool> selection = *attributes.lookup_or_default<bool>( - ".selection", bke::AttrDomain::Point, true); + + IndexMaskMemory memory; + IndexMask selection = ed::curves::retrieve_selected_points(curves, memory); + if (selection.is_empty()) { + continue; + } /* Look for existing group, otherwise lazy-initialize if any vertex is selected. */ int def_nr = BLI_findstringindex( &vertex_group_names, name.data(), offsetof(bDeformGroup, name)); const MutableSpan<MDeformVert> dverts = curves.deform_verts_for_write(); - for (const int i : dverts.index_range()) { - if (selection[i]) { - /* Lazily add the vertex group if any vertex is selected. */ - if (def_nr < 0) { - bDeformGroup *defgroup = MEM_cnew<bDeformGroup>(__func__); - STRNCPY(defgroup->name, name.data()); - - BLI_addtail(&vertex_group_names, defgroup); - def_nr = BLI_listbase_count(&vertex_group_names) - 1; - BLI_assert(def_nr >= 0); - } - MDeformWeight *dw = BKE_defvert_ensure_index(&dverts[i], def_nr); - if (dw) { - dw->weight = weight; - } + selection.foreach_index([&](int point_i) { + if (def_nr < 0) { + bDeformGroup *defgroup = MEM_cnew<bDeformGroup>(__func__); + STRNCPY(defgroup->name, name.data()); + + BLI_addtail(&vertex_group_names, defgroup); + def_nr = BLI_listbase_count(&vertex_group_names) - 1; + BLI_assert(def_nr >= 0); } - } + + MDeformWeight *dw = BKE_defvert_ensure_index(&dverts[point_i], def_nr); + if (dw) { + dw->weight = weight; + } + }); } } ``` </details>
Author
Member

@PratikPB2123 Generally, code in blenkernel shouldn't call code in the editors. This is why I suggested to move this logic into a static function that's called by assign_to_vertex_group. And this static function would e.g. ensure that the vertex group exists, which can be a kernel function.

@PratikPB2123 Generally, code in `blenkernel` shouldn't call code in the editors. This is why I suggested to move this logic into a static function that's called by `assign_to_vertex_group`. And this static function would e.g. ensure that the vertex group exists, which can be a kernel function.
Member

And I think that it's better assign_to_vertex_group will use retrieve_visible_drawings() instead of assigning to all drawings. I checked, that's the behaviour in GPv2 too.

And I think that it's better `assign_to_vertex_group` will use `retrieve_visible_drawings()` instead of assigning to _all_ drawings. I checked, that's the behaviour in GPv2 too.
Author
Member

@HooglyBoogly I'm wondering if we can make all of this easier by adding a try_create function to CurvesVertexGroupsAttributeProvider. That should essentially replace the ensure_vertex_group no?

@HooglyBoogly I'm wondering if we can make all of this easier by adding a `try_create` function to `CurvesVertexGroupsAttributeProvider`. That should essentially replace the `ensure_vertex_group` no?
Member

We can't do that, since float attributes should be created by default in geometry nodes (and elsewhere in the attribute API) instead of vertex groups. Vertex groups always have to be created explicitly outside of the attribute API, for now anyway.

We can't do that, since float attributes should be created by default in geometry nodes (and elsewhere in the attribute API) instead of vertex groups. Vertex groups always have to be created explicitly outside of the attribute API, for now anyway.
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
4 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#117753
No description provided.