Blender V5.3
grease_pencil_vertex_groups.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
10#include "DNA_object_types.h"
11
12#include "BLI_listbase.hh"
13#include "BLI_set.hh"
14
15#include "BKE_curves.hh"
16#include "BKE_deform.hh"
17#include "BKE_grease_pencil.hh"
19
21
22/* ------------------------------------------------------------------- */
25
27{
28 Set<std::string> valid_names;
29 for (const bDeformGroup &defgroup : grease_pencil.vertex_group_names) {
30 valid_names.add_new(defgroup.name);
31 }
32
33 for (GreasePencilDrawingBase *base : grease_pencil.drawings()) {
34 if (base->type != GP_DRAWING) {
35 continue;
36 }
37 Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
38
39 /* Remove unknown vertex groups. */
41 int defgroup_index = 0;
42 for (bDeformGroup &defgroup : curves.vertex_group_names.items_mutable()) {
43 if (!valid_names.contains(defgroup.name)) {
44 remove_defgroup_index(curves.deform_verts_for_write(), defgroup_index);
45
46 BLI_freelinkN(&curves.vertex_group_names, &defgroup);
47 }
48
49 ++defgroup_index;
50 }
51 }
52}
53
54int ensure_vertex_group(const StringRef name, ListBaseT<bDeformGroup> &vertex_group_names)
55{
56 int def_nr = BKE_defgroup_name_index(&vertex_group_names, name);
57 if (def_nr < 0) {
58 bDeformGroup *defgroup = MEM_new<bDeformGroup>(__func__);
59 name.copy_utf8_truncated(defgroup->name);
60 BLI_addtail(&vertex_group_names, defgroup);
61 def_nr = vertex_group_names.count() - 1;
62 BLI_assert(def_nr >= 0);
63 }
64 return def_nr;
65}
66
68 const IndexMask &mask,
69 const StringRef name,
70 const float weight)
71{
72 if (mask.is_empty()) {
73 return;
74 }
75
76 ListBaseT<bDeformGroup> &vertex_group_names = curves.vertex_group_names;
77 /* Look for existing group, otherwise lazy-initialize if any vertex is selected. */
78 int def_nr = BKE_defgroup_name_index(&vertex_group_names, name);
79
80 /* Lazily add the vertex group if any vertex is selected. */
81 if (def_nr < 0) {
82 bDeformGroup *defgroup = MEM_new<bDeformGroup>(__func__);
83 name.copy_utf8_truncated(defgroup->name);
84 BLI_addtail(&vertex_group_names, defgroup);
85 def_nr = vertex_group_names.count() - 1;
86 BLI_assert(def_nr >= 0);
87 }
88
89 const MutableSpan<MDeformVert> dverts = curves.deform_verts_for_write();
90 mask.foreach_index([&](const int point_i) {
91 if (MDeformWeight *dw = BKE_defvert_ensure_index(&dverts[point_i], def_nr)) {
92 dw->weight = weight;
93 }
94 });
95}
96
97void assign_to_vertex_group(Drawing &drawing, const StringRef name, const float weight)
98{
99
101 ListBaseT<bDeformGroup> &vertex_group_names = curves.vertex_group_names;
102
103 const bke::AttributeAccessor attributes = curves.attributes();
104 const VArray<bool> selection = *attributes.lookup_or_default<bool>(
105 ".selection", bke::AttrDomain::Point, true);
106
107 /* Look for existing group, otherwise lazy-initialize if any vertex is selected. */
108 int def_nr = BKE_defgroup_name_index(&vertex_group_names, name);
109
110 const MutableSpan<MDeformVert> dverts = curves.deform_verts_for_write();
111 for (const int i : dverts.index_range()) {
112 if (selection[i]) {
113 /* Lazily add the vertex group if any vertex is selected. */
114 if (def_nr < 0) {
115 bDeformGroup *defgroup = MEM_new<bDeformGroup>(__func__);
116 name.copy_utf8_truncated(defgroup->name);
117
118 BLI_addtail(&vertex_group_names, defgroup);
119 def_nr = vertex_group_names.count() - 1;
120 BLI_assert(def_nr >= 0);
121 }
122
123 MDeformWeight *dw = BKE_defvert_ensure_index(&dverts[i], def_nr);
124 if (dw) {
125 dw->weight = weight;
126 }
127 }
128 }
129}
130
131bool remove_from_vertex_group(Drawing &drawing, const StringRef name, const bool use_selection)
132{
133 bool changed = false;
135 ListBaseT<bDeformGroup> &vertex_group_names = curves.vertex_group_names;
136
137 const int def_nr = BKE_defgroup_name_index(&vertex_group_names, name);
138 if (def_nr < 0) {
139 /* No vertices assigned to the group in this drawing. */
140 return false;
141 }
142
143 const MutableSpan<MDeformVert> dverts = curves.deform_verts_for_write();
144 const bke::AttributeAccessor attributes = curves.attributes();
145 const VArray<bool> selection = *attributes.lookup_or_default<bool>(
146 ".selection", bke::AttrDomain::Point, true);
147 for (const int i : dverts.index_range()) {
148 if (!use_selection || selection[i]) {
149 MDeformVert *dv = &dverts[i];
150 MDeformWeight *dw = BKE_defvert_find_index(dv, def_nr);
152 changed = true;
153 }
154 }
155 return changed;
156}
157
159{
160 for (GreasePencilDrawingBase *base : grease_pencil.drawings()) {
161 if (base->type != GP_DRAWING) {
162 continue;
163 }
164 Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
166
167 for (MDeformVert &dvert : curves.deform_verts_for_write()) {
168 BKE_defvert_clear(&dvert);
169 }
170 }
171}
172
174
175} // namespace blender::bke::greasepencil
Low-level operations for curves.
support for deformation groups and hooks.
Low-level operations for grease pencil.
Utility functions for vertex groups in grease pencil objects.
Object is a sort of wrapper for general info.
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
bool contains(const Key &key) const
Definition BLI_set.hh:310
void add_new(const Key &key)
Definition BLI_set.hh:233
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
bke::CurvesGeometry & strokes_for_write()
void copy_utf8_truncated(char *dst, int64_t dst_size) const
Definition string_ref.cc:28
void clear_vertex_groups(GreasePencil &grease_pencil)
bool remove_from_vertex_group(Drawing &drawing, StringRef name, bool use_selection)
void assign_to_vertex_group(Drawing &drawing, StringRef name, float weight)
int ensure_vertex_group(StringRef name, ListBaseT< bDeformGroup > &vertex_group_names)
void assign_to_vertex_group_from_mask(CurvesGeometry &curves, const IndexMask &mask, StringRef name, float weight)
void validate_drawing_vertex_groups(GreasePencil &grease_pencil)
void remove_defgroup_index(MutableSpan< MDeformVert > dverts, int defgroup_index)
Definition deform.cc:1758
BLI_INLINE Texture * wrap(MTLTexture *texture)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_defvert_clear(MDeformVert *dvert)
Definition deform.cc:918
int BKE_defgroup_name_index(const ListBaseT< bDeformGroup > *defbase, StringRef name)
Definition deform.cc:544
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
MDeformWeight * BKE_defvert_ensure_index(MDeformVert *dv, int defgroup)
Definition deform.cc:830
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BKE_defvert_remove_group(MDeformVert *dvert, MDeformWeight *dw)
Definition deform.cc:885
MDeformWeight * BKE_defvert_find_index(const MDeformVert *dv, int defgroup)
Definition deform.cc:811