Blender V5.3
armature_skinning.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "DNA_armature_types.h"
12#include "DNA_mesh_types.h"
13#include "DNA_object_types.h"
14
15#include "MEM_guardedalloc.h"
16
17#include "BLI_math_matrix_c.hh"
18#include "BLI_math_vector_c.hh"
19#include "BLI_string_utils.hh"
20
21#include "BKE_action.hh"
22#include "BKE_armature.hh"
23#include "BKE_attribute.hh"
24#include "BKE_deform.hh"
25#include "BKE_mesh_iterators.hh"
26#include "BKE_modifier.hh"
27#include "BKE_object.hh"
28#include "BKE_object_deform.h"
29#include "BKE_report.hh"
30#include "BKE_subdiv_mesh.hh"
31
32#include "DEG_depsgraph.hh"
34
35#include "ED_armature.hh"
36#include "ED_mesh.hh"
37#include "ED_object_vgroup.hh"
38
40
41#include "armature_intern.hh"
42#include "meshlaplacian.h"
43
44namespace blender {
45
46/* ******************************* Bone Skinning *********************************************** */
47
48static int bone_skinnable_cb(Object * /*ob*/, Bone *bone, void *datap)
49{
50 /* Bones that are deforming
51 * are regarded to be "skinnable" and are eligible for
52 * auto-skinning.
53 *
54 * This function performs 2 functions:
55 *
56 * a) It returns 1 if the bone is skinnable.
57 * If we loop over all bones with this
58 * function, we can count the number of
59 * skinnable bones.
60 * b) If the pointer data is non null,
61 * it is treated like a handle to a
62 * bone pointer -- the bone pointer
63 * is set to point at this bone, and
64 * the pointer the handle points to
65 * is incremented to point to the
66 * next member of an array of pointers
67 * to bones. This way we can loop using
68 * this function to construct an array of
69 * pointers to bones that point to all
70 * skinnable bones.
71 */
72 Bone ***hbone;
73 int a, segments;
74 struct Arg {
75 Object *armob;
76 void *list;
77 int heat;
78 bool is_weight_paint;
79 } *data = static_cast<Arg *>(datap);
80
81 bPoseChannel *pose_bone = BKE_pose_channel_find_name(data->armob->pose, bone->name);
82 if (!pose_bone) {
83 return 0;
84 }
85
86 if (!(data->is_weight_paint) || !(pose_bone->drawflag & PCHAN_DRAW_HIDDEN)) {
87 if (!(bone->flag & BONE_NO_DEFORM)) {
88 if (data->heat && data->armob->pose && pose_bone) {
89 segments = bone->segments;
90 }
91 else {
92 segments = 1;
93 }
94
95 if (data->list != nullptr) {
96 hbone = reinterpret_cast<Bone ***>(&data->list);
97
98 for (a = 0; a < segments; a++) {
99 **hbone = bone;
100 (*hbone)++;
101 }
102 }
103 return segments;
104 }
105 }
106 return 0;
107}
108
109static int vgroup_add_unique_bone_cb(Object *ob, Bone *bone, void * /*ptr*/)
110{
111 /* This group creates a vertex group to ob that has the
112 * same name as bone (provided the bone is skinnable).
113 * If such a vertex group already exist the routine exits.
114 */
115 if (!(bone->flag & BONE_NO_DEFORM)) {
116 if (!BKE_object_defgroup_find_name(ob, bone->name)) {
118 return 1;
119 }
120 }
121 return 0;
122}
123
124static int dgroup_skinnable_cb(Object *ob, Bone *bone, void *datap)
125{
126 /* Bones that are deforming
127 * are regarded to be "skinnable" and are eligible for
128 * auto-skinning.
129 *
130 * This function performs 2 functions:
131 *
132 * a) If the bone is skinnable, it creates
133 * a vertex group for ob that has
134 * the name of the skinnable bone
135 * (if one doesn't exist already).
136 * b) If the pointer data is non null,
137 * it is treated like a handle to a
138 * bDeformGroup pointer -- the
139 * bDeformGroup pointer is set to point
140 * to the deform group with the bone's
141 * name, and the pointer the handle
142 * points to is incremented to point to the
143 * next member of an array of pointers
144 * to bDeformGroups. This way we can loop using
145 * this function to construct an array of
146 * pointers to bDeformGroups, all with names
147 * of skinnable bones.
148 */
149 bDeformGroup ***hgroup, *defgroup = nullptr;
150 int a, segments;
151 struct Arg {
152 Object *armob;
153 void *list;
154 int heat;
155 bool is_weight_paint;
156 } *data = static_cast<Arg *>(datap);
157
158 if (bone->flag & BONE_NO_DEFORM) {
159 return 0;
160 }
161
162 bArmature *arm = id_cast<bArmature *>(data->armob->data);
163 const bPoseChannel *pose_bone = BKE_pose_channel_find_name(data->armob->pose, bone->name);
164 if (!pose_bone) {
165 return 0;
166 }
167
168 if (data->is_weight_paint && (pose_bone->drawflag & PCHAN_DRAW_HIDDEN)) {
169 return 0;
170 }
171
172 if (data->heat) {
173 segments = bone->segments;
174 }
175 else {
176 segments = 1;
177 }
178
179 if (!data->is_weight_paint ||
180 (ANIM_bone_in_visible_collection(arm, bone) && (pose_bone->flag & POSE_SELECTED)))
181 {
182 if (!(defgroup = BKE_object_defgroup_find_name(ob, bone->name))) {
183 defgroup = BKE_object_defgroup_add_name(ob, bone->name);
184 }
185 else if (defgroup->flag & DG_LOCK_WEIGHT) {
186 /* In case vgroup already exists and is locked, do not modify it here. See #43814. */
187 defgroup = nullptr;
188 }
189 }
190
191 if (data->list != nullptr) {
192 hgroup = reinterpret_cast<bDeformGroup ***>(&data->list);
193
194 for (a = 0; a < segments; a++) {
195 **hgroup = defgroup;
196 (*hgroup)++;
197 }
198 }
199 return segments;
200}
201
203 Mesh *mesh,
204 const Span<float3> verts,
205 int numbones,
206 Bone **bonelist,
207 bDeformGroup **dgrouplist,
208 bDeformGroup **dgroupflip,
209 float (*root)[3],
210 float (*tip)[3],
211 const bool *selected,
212 float scale)
213{
214 /* Create vertex group weights from envelopes */
215
216 bool use_topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
217 bool use_mask = false;
218
219 if ((ob->mode & OB_MODE_WEIGHT_PAINT) &&
221 {
222 use_mask = true;
223 }
224
225 const bke::AttributeAccessor attributes = mesh->attributes();
226 const VArray select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
227
228 /* for each vertex in the mesh */
229 for (int i = 0; i < mesh->verts_num; i++) {
230
231 if (use_mask && !(select_vert && select_vert[i])) {
232 continue;
233 }
234
235 int iflip = (dgroupflip) ? mesh_get_x_mirror_vert(ob, nullptr, i, use_topology) : -1;
236
237 /* for each skinnable bone */
238 for (int j = 0; j < numbones; j++) {
239 if (selected[j] == false) {
240 continue;
241 }
242
243 Bone *bone = bonelist[j];
244 bDeformGroup *dgroup = dgrouplist[j];
245
246 /* store the distance-factor from the vertex to the bone */
247 float distance = distfactor_to_bone(verts[i],
248 root[j],
249 tip[j],
250 bone->rad_head * scale,
251 bone->rad_tail * scale,
252 bone->dist * scale);
253
254 /* add the vert to the deform group if (weight != 0.0) */
255 if (distance != 0.0f) {
257 }
258 else {
259 ed::object::vgroup_vert_remove(ob, dgroup, i);
260 }
261
262 /* do same for mirror */
263 if (dgroupflip && dgroupflip[j] && iflip != -1) {
264 if (distance != 0.0f) {
265 ed::object::vgroup_vert_add(ob, dgroupflip[j], iflip, distance, WEIGHT_REPLACE);
266 }
267 else {
268 ed::object::vgroup_vert_remove(ob, dgroupflip[j], iflip);
269 }
270 }
271 }
272 }
273}
274
276 ReportList *reports, Scene * /*scene*/, Object *ob, Object *par, int heat, const bool mirror)
277{
278 /* This functions implements the automatic computation of vertex group
279 * weights, either through envelopes or using a heat equilibrium.
280 *
281 * This function can be called both when parenting a mesh to an armature,
282 * or in weight-paint + pose-mode. In the latter case selection is taken
283 * into account and vertex weights can be mirrored.
284 *
285 * The mesh vertex positions used are either the final deformed coords
286 * from the evaluated mesh in weight-paint mode, the final sub-surface coords
287 * when parenting, or simply the original mesh coords.
288 */
289
290 bArmature *arm = id_cast<bArmature *>(par->data);
291 Bone **bonelist, *bone;
292 bDeformGroup **dgrouplist, **dgroupflip;
293 bDeformGroup *dgroup;
294 bPoseChannel *pchan;
295 Mesh *mesh;
296 Mat4 bbone_array[MAX_BBONE_SUBDIV], *bbone = nullptr;
297 float (*root)[3], (*tip)[3];
298 Array<float3> verts;
299 bool *selected;
300 int numbones, vertsfilled = 0, segments = 0;
301 const bool wpmode = (ob->mode & OB_MODE_WEIGHT_PAINT);
302 struct {
303 Object *armob;
304 void *list;
305 int heat;
306 bool is_weight_paint;
307 } looper_data;
308
309 looper_data.armob = par;
310 looper_data.heat = heat;
311 looper_data.list = nullptr;
312 looper_data.is_weight_paint = wpmode;
313
314 if (!par->pose) {
315 BKE_pose_rebuild(nullptr, par, arm, false);
316 }
318 /* count the number of skinnable bones */
319 numbones = bone_looper(ob, arm->bonebase.first(), &looper_data, bone_skinnable_cb);
320
321 if (numbones == 0) {
322 return;
323 }
324
325 if (BKE_object_defgroup_data_create(ob->data) == nullptr) {
326 return;
327 }
328
329 /* create an array of pointer to bones that are skinnable
330 * and fill it with all of the skinnable bones */
331 bonelist = MEM_new_array_zeroed<Bone *>(numbones, "bonelist");
332 looper_data.list = bonelist;
333 bone_looper(ob, arm->bonebase.first(), &looper_data, bone_skinnable_cb);
334
335 /* create an array of pointers to the deform groups that
336 * correspond to the skinnable bones (creating them
337 * as necessary). */
338 dgrouplist = MEM_new_array_zeroed<bDeformGroup *>(numbones, "dgrouplist");
339 dgroupflip = MEM_new_array_zeroed<bDeformGroup *>(numbones, "dgroupflip");
340
341 looper_data.list = dgrouplist;
342 bone_looper(ob, arm->bonebase.first(), &looper_data, dgroup_skinnable_cb);
343
344 /* create an array of root and tip positions transformed into
345 * global coords */
346 root = MEM_new_array_zeroed<float[3]>(numbones, "root");
347 tip = MEM_new_array_zeroed<float[3]>(numbones, "tip");
348 selected = MEM_new_array_zeroed<bool>(numbones, "selected");
349
350 for (int j = 0; j < numbones; j++) {
351 bone = bonelist[j];
352 dgroup = dgrouplist[j];
353
354 /* handle bbone */
355 if (heat) {
356 if (segments == 0) {
357 segments = 1;
358 bbone = nullptr;
359
360 if ((par->pose) && (pchan = BKE_pose_channel_find_name(par->pose, bone->name))) {
361 if (bone->segments > 1) {
362 segments = bone->segments;
363 BKE_pchan_bbone_spline_setup({pchan, bone}, *arm, true, false, bbone_array);
364 bbone = bbone_array;
365 }
366 }
367 }
368
369 segments--;
370 }
371
372 /* compute root and tip */
373 if (bbone) {
374 mul_v3_m4v3(root[j], bone->arm_mat, bbone[segments].mat[3]);
375 if ((segments + 1) < bone->segments) {
376 mul_v3_m4v3(tip[j], bone->arm_mat, bbone[segments + 1].mat[3]);
377 }
378 else {
379 copy_v3_v3(tip[j], bone->arm_tail);
380 }
381 }
382 else {
383 copy_v3_v3(root[j], bone->arm_head);
384 copy_v3_v3(tip[j], bone->arm_tail);
385 }
386
387 mul_m4_v3(par->object_to_world().ptr(), root[j]);
388 mul_m4_v3(par->object_to_world().ptr(), tip[j]);
389
390 /* set selected */
391 if (wpmode) {
392 if (ANIM_bone_in_visible_collection(arm, bone)) {
393 if ((pchan = BKE_pose_channel_find_name(par->pose, bone->name))) {
394 selected[j] = pchan->flag & POSE_SELECTED;
395 }
396 }
397 }
398 else {
399 selected[j] = true;
400 }
401
402 /* find flipped group */
403 if (dgroup && mirror) {
404 char name_flip[MAXBONENAME];
405
406 BLI_string_flip_side_name(name_flip, dgroup->name, false, sizeof(name_flip));
407 dgroupflip[j] = BKE_object_defgroup_find_name(ob, name_flip);
408 }
409 }
410
411 /* create verts */
412 mesh = id_cast<Mesh *>(ob->data);
413 verts.reinitialize(mesh->verts_num);
414
416 /* Is subdivision-surface on? Lets use the verts on the limit surface then.
417 * = same amount of vertices as mesh, but vertices moved to the
418 * subdivision-surfaced position, like for 'optimal'. */
420 vertsfilled = 1;
421 }
422
423 /* transform verts to global space */
424 const Span<float3> positions = mesh->vert_positions();
425 for (int i = 0; i < mesh->verts_num; i++) {
426 if (!vertsfilled) {
427 copy_v3_v3(verts[i], positions[i]);
428 }
429 mul_m4_v3(ob->object_to_world().ptr(), verts[i]);
430 }
431
432 /* compute the weights based on gathered vertices and bones */
433 if (heat) {
434 const char *error = nullptr;
435
437 mesh,
438 reinterpret_cast<float (*)[3]>(verts.data()),
439 numbones,
440 dgrouplist,
441 dgroupflip,
442 root,
443 tip,
444 selected,
445 &error);
446 if (error) {
447 BKE_report(reports, RPT_WARNING, error);
448 }
449 }
450 else {
452 mesh,
453 verts,
454 numbones,
455 bonelist,
456 dgrouplist,
457 dgroupflip,
458 root,
459 tip,
460 selected,
461 mat4_to_scale(par->object_to_world().ptr()));
462 }
463
464 /* only generated in some cases but can call anyway */
466
467 /* free the memory allocated */
468 MEM_delete(bonelist);
469 MEM_delete(dgrouplist);
470 MEM_delete(dgroupflip);
471 MEM_delete(root);
472 MEM_delete(tip);
473 MEM_delete(selected);
474}
475
477 Depsgraph * /* depsgraph */,
478 Scene *scene,
479 Object *ob,
480 Object *par,
481 const int mode,
482 const bool mirror)
483{
484 /* Lets try to create some vertex groups
485 * based on the bones of the parent armature.
486 */
487 bArmature *arm = id_cast<bArmature *>(par->data);
488
489 if (mode == ARM_GROUPS_NAME) {
490 const int defbase_tot = BKE_object_defgroup_count(ob);
491 int defbase_add;
492 /* Traverse the bone list, trying to create empty vertex
493 * groups corresponding to the bone.
494 */
495 defbase_add = bone_looper(ob, arm->bonebase.first(), nullptr, vgroup_add_unique_bone_cb);
496
497 if (defbase_add) {
498 /* It's possible there are DWeights outside the range of the current
499 * object's deform groups. In this case the new groups won't be empty #33889. */
501 }
502 }
503 else if (ELEM(mode, ARM_GROUPS_ENVELOPE, ARM_GROUPS_AUTO)) {
504 /* Traverse the bone list, trying to create vertex groups
505 * that are populated with the vertices for which the
506 * bone is closest.
507 */
508 add_verts_to_dgroups(reports, scene, ob, par, (mode == ARM_GROUPS_AUTO), mirror);
509 }
510}
511
512} // namespace blender
C++ functions to deal with Armature collections (i.e. the successor of bone layers).
Blender kernel action and pose functionality.
#define MAX_BBONE_SUBDIV
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
Object is a sort of wrapper for general info.
#define ARM_GROUPS_ENVELOPE
#define ARM_GROUPS_NAME
#define ARM_GROUPS_AUTO
#define WEIGHT_REPLACE
Read Blender memory allocation and freeing.
#define ELEM(...)
const T * data() const
Definition BLI_array.hh:323
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
GAttributeReader lookup(const StringRef name) const
float distance(float, float) RET
float3x3 scale(float3x3 mat, float2 scale)
void calculate_limit_positions(Mesh *mesh, MutableSpan< float3 > limit_positions)
void vgroup_data_clamp_range(ID *id, int total)
void vgroup_vert_add(Object *ob, bDeformGroup *dg, int vertnum, float weight, int assignmode)
void vgroup_vert_remove(Object *ob, bDeformGroup *dg, int vertnum)
BMesh Iterators.
struct MDeformVert * BKE_object_defgroup_data_create(struct ID *id)
void BKE_pose_rebuild(Main *bmain, Object *ob, bArmature *arm, bool do_id_user)
@ RPT_WARNING
Definition BKE_report.hh:40
MINLINE void copy_v3_v3(float r[3], const float a[3])
int BKE_object_defgroup_count(const Object *ob)
Definition deform.cc:608
bDeformGroup * BKE_object_defgroup_find_name(const Object *ob, StringRef name)
Definition deform.cc:530
float mat4_to_scale(const float mat[4][4])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
static int vgroup_add_unique_bone_cb(Object *ob, Bone *bone, void *)
void ED_mesh_mirror_spatial_table_end(Object *ob)
static void envelope_bone_weighting(Object *ob, Mesh *mesh, const Span< float3 > verts, int numbones, Bone **bonelist, bDeformGroup **dgrouplist, bDeformGroup **dgroupflip, float(*root)[3], float(*tip)[3], const bool *selected, float scale)
bool ANIM_bone_in_visible_collection(const bArmature *armature, const Bone *bone)
int mesh_get_x_mirror_vert(Object *ob, Mesh *mesh_eval, int index, bool use_topology)
Definition meshtools.cc:297
void BKE_pose_channels_hash_ensure(bPose *pose) ATTR_NONNULL(1)
float distfactor_to_bone(const float3 &position, const float3 &head, const float3 &tail, float radius_head, float radius_tail, float falloff_distance)
void mul_m4_v3(const float M[4][4], float r[3])
void BKE_pchan_bbone_spline_setup(bke::PChanBone pchanbone, const bArmature &armature, bool rest, bool for_deform, Mat4 *result_array)
@ ME_EDIT_PAINT_VERT_SEL
@ ME_EDIT_PAINT_FACE_SEL
@ ME_EDIT_MIRROR_TOPO
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
static void add_verts_to_dgroups(ReportList *reports, Scene *, Object *ob, Object *par, int heat, const bool mirror)
struct bDeformGroup * BKE_object_defgroup_add_name(struct Object *ob, const char *name)
static int bone_skinnable_cb(Object *, Bone *bone, void *datap)
ModifierData * BKE_modifiers_findby_type(const Object *ob, ModifierType type)
void heat_bone_weighting(Object *ob, Mesh *mesh, float(*verts)[3], int numbones, bDeformGroup **dgrouplist, bDeformGroup **dgroupflip, float(*root)[3], float(*tip)[3], const bool *selected, const char **r_error_str)
static int dgroup_skinnable_cb(Object *ob, Bone *bone, void *datap)
void ED_object_vgroup_calc_from_armature(ReportList *reports, Depsgraph *, Scene *scene, Object *ob, Object *par, const int mode, const bool mirror)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
int bone_looper(Object *ob, Bone *bone, void *data, int(*bone_func)(Object *, Bone *, void *))
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
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
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float mat[4][4]
struct bPose * pose
ListBaseT< Bone > bonebase
ebDeformGroup_Flag flag