2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2012-09-05 02:51:55 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
2012-09-05 02:51:55 +00:00
|
|
|
*/
|
|
|
|
|
|
2023-07-22 11:27:25 +10:00
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstring>
|
2012-09-05 02:51:55 +00:00
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2015-08-16 17:32:01 +10:00
|
|
|
#include "BLT_translation.h"
|
2014-11-18 23:52:17 +01:00
|
|
|
|
2012-09-05 02:51:55 +00:00
|
|
|
#include "BLI_ghash.h"
|
2014-02-08 06:07:10 +11:00
|
|
|
#include "BLI_listbase.h"
|
2023-10-18 17:15:30 +02:00
|
|
|
#include "BLI_string_utils.hh"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2012-09-05 02:51:55 +00:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
#include "DNA_armature_types.h"
|
|
|
|
|
#include "DNA_cloth_types.h"
|
|
|
|
|
#include "DNA_curve_types.h"
|
2023-03-13 10:42:51 +01:00
|
|
|
#include "DNA_gpencil_legacy_types.h"
|
2024-01-31 17:45:59 +01:00
|
|
|
#include "DNA_grease_pencil_types.h"
|
2014-11-18 23:52:17 +01:00
|
|
|
#include "DNA_lattice_types.h"
|
|
|
|
|
#include "DNA_mesh_types.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_meshdata_types.h"
|
2014-11-18 23:52:17 +01:00
|
|
|
#include "DNA_modifier_types.h"
|
2018-02-07 11:14:08 +11:00
|
|
|
#include "DNA_object_force_types.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_object_types.h"
|
2014-11-18 23:52:17 +01:00
|
|
|
#include "DNA_particle_types.h"
|
|
|
|
|
#include "DNA_scene_types.h"
|
|
|
|
|
|
2012-09-05 03:45:32 +00:00
|
|
|
#include "BKE_action.h"
|
2024-01-29 18:57:16 -05:00
|
|
|
#include "BKE_deform.hh"
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_editmesh.hh"
|
2023-03-13 10:42:51 +01:00
|
|
|
#include "BKE_gpencil_legacy.h"
|
2024-01-31 17:45:59 +01:00
|
|
|
#include "BKE_grease_pencil_vertex_groups.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_mesh.hh"
|
2023-11-14 09:30:40 +01:00
|
|
|
#include "BKE_modifier.hh"
|
2023-10-09 23:41:53 +02:00
|
|
|
#include "BKE_object.hh"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BKE_object_deform.h" /* own include */
|
2012-09-05 02:51:55 +00:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2014-11-18 23:52:17 +01:00
|
|
|
/** \name Misc helpers
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static Lattice *object_defgroup_lattice_get(ID *id)
|
|
|
|
|
{
|
|
|
|
|
Lattice *lt = (Lattice *)id;
|
|
|
|
|
BLI_assert(GS(id->name) == ID_LT);
|
|
|
|
|
return (lt->editlatt) ? lt->editlatt->latt : lt;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 11:27:09 +02:00
|
|
|
void BKE_object_defgroup_remap_update_users(Object *ob, const int *map)
|
2014-11-18 23:52:17 +01:00
|
|
|
{
|
|
|
|
|
/* these cases don't use names to refer to vertex groups, so when
|
|
|
|
|
* they get removed the numbers get out of sync, this corrects that */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
if (ob->soft) {
|
|
|
|
|
ob->soft->vertgroup = map[ob->soft->vertgroup];
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-08-04 08:51:13 +10:00
|
|
|
LISTBASE_FOREACH (ModifierData *, md, &ob->modifiers) {
|
2014-11-18 23:52:17 +01:00
|
|
|
if (md->type == eModifierType_Explode) {
|
|
|
|
|
ExplodeModifierData *emd = (ExplodeModifierData *)md;
|
|
|
|
|
emd->vgroup = map[emd->vgroup];
|
|
|
|
|
}
|
|
|
|
|
else if (md->type == eModifierType_Cloth) {
|
|
|
|
|
ClothModifierData *clmd = (ClothModifierData *)md;
|
|
|
|
|
ClothSimSettings *clsim = clmd->sim_parms;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
if (clsim) {
|
|
|
|
|
clsim->vgroup_mass = map[clsim->vgroup_mass];
|
|
|
|
|
clsim->vgroup_bend = map[clsim->vgroup_bend];
|
|
|
|
|
clsim->vgroup_struct = map[clsim->vgroup_struct];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-08-04 08:51:13 +10:00
|
|
|
LISTBASE_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
|
|
|
|
|
for (int a = 0; a < PSYS_TOT_VG; a++) {
|
2014-11-18 23:52:17 +01:00
|
|
|
psys->vgroup[a] = map[psys->vgroup[a]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-14 15:49:31 +11:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
/** \} */
|
|
|
|
|
|
2012-09-05 02:51:55 +00:00
|
|
|
/* -------------------------------------------------------------------- */
|
2014-11-18 23:52:17 +01:00
|
|
|
/** \name Group creation
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
bDeformGroup *BKE_object_defgroup_add_name(Object *ob, const char *name)
|
|
|
|
|
{
|
|
|
|
|
bDeformGroup *defgroup;
|
|
|
|
|
|
2019-04-22 09:39:35 +10:00
|
|
|
if (!ob || !OB_TYPE_SUPPORT_VGROUP(ob->type)) {
|
2023-07-17 10:46:26 +02:00
|
|
|
return nullptr;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2014-11-18 23:52:17 +01:00
|
|
|
|
2020-03-06 12:50:56 +11:00
|
|
|
defgroup = BKE_object_defgroup_new(ob, name);
|
2021-07-13 12:10:34 -04:00
|
|
|
BKE_object_defgroup_active_index_set(ob, BKE_object_defgroup_count(ob));
|
2014-11-18 23:52:17 +01:00
|
|
|
|
|
|
|
|
return defgroup;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-17 17:05:51 +02:00
|
|
|
bDeformGroup *BKE_object_defgroup_add(Object *ob)
|
2014-11-18 23:52:17 +01:00
|
|
|
{
|
|
|
|
|
return BKE_object_defgroup_add_name(ob, DATA_("Group"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MDeformVert *BKE_object_defgroup_data_create(ID *id)
|
|
|
|
|
{
|
|
|
|
|
if (GS(id->name) == ID_ME) {
|
2023-12-12 20:45:16 -05:00
|
|
|
return ((Mesh *)id)->deform_verts_for_write().data();
|
2014-11-18 23:52:17 +01:00
|
|
|
}
|
2020-08-07 12:30:43 +02:00
|
|
|
if (GS(id->name) == ID_LT) {
|
2014-11-18 23:52:17 +01:00
|
|
|
Lattice *lt = (Lattice *)id;
|
2023-07-17 10:46:26 +02:00
|
|
|
lt->dvert = static_cast<MDeformVert *>(MEM_callocN(
|
|
|
|
|
sizeof(MDeformVert) * lt->pntsu * lt->pntsv * lt->pntsw, "lattice deformVert"));
|
2014-11-18 23:52:17 +01:00
|
|
|
return lt->dvert;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
return nullptr;
|
2014-11-18 23:52:17 +01:00
|
|
|
}
|
2021-12-14 15:49:31 +11:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
/** \} */
|
|
|
|
|
|
2012-09-05 02:51:55 +00:00
|
|
|
/* -------------------------------------------------------------------- */
|
2014-11-18 23:52:17 +01:00
|
|
|
/** \name Group clearing
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
bool BKE_object_defgroup_clear(Object *ob, bDeformGroup *dg, const bool use_selection)
|
|
|
|
|
{
|
|
|
|
|
MDeformVert *dv;
|
2021-07-13 12:10:34 -04:00
|
|
|
const ListBase *defbase = BKE_object_defgroup_list(ob);
|
|
|
|
|
const int def_nr = BLI_findindex(defbase, dg);
|
2014-11-18 23:52:17 +01:00
|
|
|
bool changed = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
if (ob->type == OB_MESH) {
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(ob->data);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
if (mesh->edit_mesh) {
|
|
|
|
|
BMEditMesh *em = mesh->edit_mesh;
|
2014-11-18 23:52:17 +01:00
|
|
|
const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
if (cd_dvert_offset != -1) {
|
|
|
|
|
BMVert *eve;
|
|
|
|
|
BMIter iter;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
|
2023-07-17 10:46:26 +02:00
|
|
|
dv = static_cast<MDeformVert *>(BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
if (dv && dv->dw && (!use_selection || BM_elem_flag_test(eve, BM_ELEM_SELECT))) {
|
2020-03-06 12:50:56 +11:00
|
|
|
MDeformWeight *dw = BKE_defvert_find_index(dv, def_nr);
|
2023-07-17 10:46:26 +02:00
|
|
|
BKE_defvert_remove_group(dv, dw); /* dw can be nullptr */
|
2014-11-18 23:52:17 +01:00
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-12-12 20:45:16 -05:00
|
|
|
if (!mesh->deform_verts().data()) {
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 09:38:37 -05:00
|
|
|
const bool *select_vert = (const bool *)CustomData_get_layer_named(
|
2023-12-08 16:40:06 -05:00
|
|
|
&mesh->vert_data, CD_PROP_BOOL, ".select_vert");
|
2014-11-18 23:52:17 +01:00
|
|
|
int i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-12 20:45:16 -05:00
|
|
|
dv = mesh->deform_verts_for_write().data();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-20 02:21:48 +01:00
|
|
|
for (i = 0; i < mesh->verts_num; i++, dv++) {
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 09:38:37 -05:00
|
|
|
if (dv->dw && (!use_selection || (select_vert && select_vert[i]))) {
|
2020-03-06 12:50:56 +11:00
|
|
|
MDeformWeight *dw = BKE_defvert_find_index(dv, def_nr);
|
2023-07-17 10:46:26 +02:00
|
|
|
BKE_defvert_remove_group(dv, dw); /* dw can be nullptr */
|
2015-07-21 14:35:48 +10:00
|
|
|
changed = true;
|
2014-11-18 23:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (ob->type == OB_LATTICE) {
|
|
|
|
|
Lattice *lt = object_defgroup_lattice_get((ID *)(ob->data));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
if (lt->dvert) {
|
|
|
|
|
BPoint *bp;
|
|
|
|
|
int i, tot = lt->pntsu * lt->pntsv * lt->pntsw;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
for (i = 0, bp = lt->def; i < tot; i++, bp++) {
|
|
|
|
|
if (!use_selection || (bp->f1 & SELECT)) {
|
|
|
|
|
MDeformWeight *dw;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
dv = <->dvert[i];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-03-06 12:50:56 +11:00
|
|
|
dw = BKE_defvert_find_index(dv, def_nr);
|
2023-07-17 10:46:26 +02:00
|
|
|
BKE_defvert_remove_group(dv, dw); /* dw can be nullptr */
|
2014-11-18 23:52:17 +01:00
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-31 17:45:59 +01:00
|
|
|
else if (ob->type == OB_GREASE_PENCIL) {
|
|
|
|
|
GreasePencil *grease_pencil = static_cast<GreasePencil *>(ob->data);
|
|
|
|
|
changed = blender::bke::greasepencil::remove_from_vertex_group(
|
|
|
|
|
*grease_pencil, dg->name, use_selection);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
return changed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BKE_object_defgroup_clear_all(Object *ob, const bool use_selection)
|
|
|
|
|
{
|
|
|
|
|
bool changed = false;
|
|
|
|
|
|
2021-07-13 12:10:34 -04:00
|
|
|
const ListBase *defbase = BKE_object_defgroup_list(ob);
|
|
|
|
|
|
2023-08-04 08:51:13 +10:00
|
|
|
LISTBASE_FOREACH (bDeformGroup *, dg, defbase) {
|
2014-11-18 23:52:17 +01:00
|
|
|
if (BKE_object_defgroup_clear(ob, dg, use_selection)) {
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
|
}
|
2021-12-14 15:49:31 +11:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
/** \} */
|
|
|
|
|
|
2012-09-05 02:51:55 +00:00
|
|
|
/* -------------------------------------------------------------------- */
|
2014-11-18 23:52:17 +01:00
|
|
|
/** \name Group removal
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static void object_defgroup_remove_update_users(Object *ob, const int idx)
|
|
|
|
|
{
|
2021-07-13 12:10:34 -04:00
|
|
|
int i, defbase_tot = BKE_object_defgroup_count(ob) + 1;
|
2023-07-17 10:46:26 +02:00
|
|
|
int *map = static_cast<int *>(MEM_mallocN(sizeof(int) * defbase_tot, "vgroup del"));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
map[idx] = map[0] = 0;
|
|
|
|
|
for (i = 1; i < idx; i++) {
|
|
|
|
|
map[i] = i;
|
|
|
|
|
}
|
|
|
|
|
for (i = idx + 1; i < defbase_tot; i++) {
|
|
|
|
|
map[i] = i - 1;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
BKE_object_defgroup_remap_update_users(ob, map);
|
|
|
|
|
MEM_freeN(map);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void object_defgroup_remove_common(Object *ob, bDeformGroup *dg, const int def_nr)
|
|
|
|
|
{
|
|
|
|
|
object_defgroup_remove_update_users(ob, def_nr + 1);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
/* Remove the group */
|
2021-07-13 12:10:34 -04:00
|
|
|
ListBase *defbase = BKE_object_defgroup_list_mutable(ob);
|
|
|
|
|
|
|
|
|
|
BLI_freelinkN(defbase, dg);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
/* Update the active deform index if necessary */
|
2021-07-13 12:10:34 -04:00
|
|
|
const int active_index = BKE_object_defgroup_active_index_get(ob);
|
|
|
|
|
if (active_index > def_nr) {
|
|
|
|
|
BKE_object_defgroup_active_index_set(ob, active_index - 1);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-20 21:17:09 +10:00
|
|
|
/* Remove all deform-verts. */
|
2021-07-13 12:10:34 -04:00
|
|
|
if (BLI_listbase_is_empty(defbase)) {
|
2014-11-18 23:52:17 +01:00
|
|
|
if (ob->type == OB_MESH) {
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(ob->data);
|
2023-12-20 02:21:48 +01:00
|
|
|
CustomData_free_layer_active(&mesh->vert_data, CD_MDEFORMVERT, mesh->verts_num);
|
2014-11-18 23:52:17 +01:00
|
|
|
}
|
|
|
|
|
else if (ob->type == OB_LATTICE) {
|
|
|
|
|
Lattice *lt = object_defgroup_lattice_get((ID *)(ob->data));
|
2021-08-06 13:59:38 +10:00
|
|
|
MEM_SAFE_FREE(lt->dvert);
|
2014-11-18 23:52:17 +01:00
|
|
|
}
|
2024-01-31 17:45:59 +01:00
|
|
|
else if (ob->type == OB_GREASE_PENCIL) {
|
|
|
|
|
GreasePencil *grease_pencil = static_cast<GreasePencil *>(ob->data);
|
|
|
|
|
blender::bke::greasepencil::clear_vertex_groups(*grease_pencil);
|
|
|
|
|
}
|
2014-11-18 23:52:17 +01:00
|
|
|
}
|
2021-07-13 12:10:34 -04:00
|
|
|
else if (BKE_object_defgroup_active_index_get(ob) < 1) {
|
|
|
|
|
/* Keep a valid active index if we still have some vgroups. */
|
|
|
|
|
BKE_object_defgroup_active_index_set(ob, 1);
|
2014-11-18 23:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void object_defgroup_remove_object_mode(Object *ob, bDeformGroup *dg)
|
|
|
|
|
{
|
2023-07-17 10:46:26 +02:00
|
|
|
MDeformVert *dvert_array = nullptr;
|
2014-11-18 23:52:17 +01:00
|
|
|
int dvert_tot = 0;
|
2021-07-13 12:10:34 -04:00
|
|
|
const ListBase *defbase = BKE_object_defgroup_list(ob);
|
|
|
|
|
|
|
|
|
|
const int def_nr = BLI_findindex(defbase, dg);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
BLI_assert(def_nr != -1);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-01-31 17:45:59 +01:00
|
|
|
if (ob->type == OB_GREASE_PENCIL) {
|
|
|
|
|
GreasePencil *grease_pencil = static_cast<GreasePencil *>(ob->data);
|
|
|
|
|
blender::bke::greasepencil::remove_from_vertex_group(*grease_pencil, dg->name, false);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
BKE_object_defgroup_array_get(static_cast<ID *>(ob->data), &dvert_array, &dvert_tot);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-01-31 17:45:59 +01:00
|
|
|
if (dvert_array) {
|
|
|
|
|
int i, j;
|
|
|
|
|
MDeformVert *dv;
|
|
|
|
|
for (i = 0, dv = dvert_array; i < dvert_tot; i++, dv++) {
|
|
|
|
|
MDeformWeight *dw;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-01-31 17:45:59 +01:00
|
|
|
dw = BKE_defvert_find_index(dv, def_nr);
|
|
|
|
|
BKE_defvert_remove_group(dv, dw); /* dw can be nullptr */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-01-31 17:45:59 +01:00
|
|
|
/* inline, make into a function if anything else needs to do this */
|
|
|
|
|
for (j = 0; j < dv->totweight; j++) {
|
|
|
|
|
if (dv->dw[j].def_nr > def_nr) {
|
|
|
|
|
dv->dw[j].def_nr--;
|
|
|
|
|
}
|
2014-11-18 23:52:17 +01:00
|
|
|
}
|
2024-01-31 17:45:59 +01:00
|
|
|
/* done */
|
2014-11-18 23:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
object_defgroup_remove_common(ob, dg, def_nr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void object_defgroup_remove_edit_mode(Object *ob, bDeformGroup *dg)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2021-07-13 12:10:34 -04:00
|
|
|
const ListBase *defbase = BKE_object_defgroup_list(ob);
|
|
|
|
|
const int def_nr = BLI_findindex(defbase, dg);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
BLI_assert(def_nr != -1);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-27 12:07:07 +10:00
|
|
|
/* Make sure that no verts are using this group - if none were removed,
|
|
|
|
|
* we can skip next per-vert update. */
|
2014-11-18 23:52:17 +01:00
|
|
|
if (!BKE_object_defgroup_clear(ob, dg, false)) {
|
|
|
|
|
/* Nothing to do. */
|
|
|
|
|
}
|
|
|
|
|
/* Else, make sure that any groups with higher indices are adjusted accordingly */
|
|
|
|
|
else if (ob->type == OB_MESH) {
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(ob->data);
|
|
|
|
|
BMEditMesh *em = mesh->edit_mesh;
|
2014-11-18 23:52:17 +01:00
|
|
|
const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
BMIter iter;
|
|
|
|
|
BMVert *eve;
|
|
|
|
|
MDeformVert *dvert;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
|
2023-07-17 10:46:26 +02:00
|
|
|
dvert = static_cast<MDeformVert *>(BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
if (dvert) {
|
|
|
|
|
for (i = 0; i < dvert->totweight; i++) {
|
|
|
|
|
if (dvert->dw[i].def_nr > def_nr) {
|
|
|
|
|
dvert->dw[i].def_nr--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (ob->type == OB_LATTICE) {
|
|
|
|
|
Lattice *lt = ((Lattice *)(ob->data))->editlatt->latt;
|
|
|
|
|
BPoint *bp;
|
|
|
|
|
MDeformVert *dvert = lt->dvert;
|
|
|
|
|
int a, tot;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
if (dvert) {
|
|
|
|
|
tot = lt->pntsu * lt->pntsv * lt->pntsw;
|
|
|
|
|
for (a = 0, bp = lt->def; a < tot; a++, bp++, dvert++) {
|
|
|
|
|
for (i = 0; i < dvert->totweight; i++) {
|
|
|
|
|
if (dvert->dw[i].def_nr > def_nr) {
|
|
|
|
|
dvert->dw[i].def_nr--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-31 17:45:59 +01:00
|
|
|
else if (ob->type == OB_GREASE_PENCIL) {
|
|
|
|
|
GreasePencil *grease_pencil = static_cast<GreasePencil *>(ob->data);
|
|
|
|
|
blender::bke::greasepencil::remove_from_vertex_group(*grease_pencil, dg->name, false);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
object_defgroup_remove_common(ob, dg, def_nr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_object_defgroup_remove(Object *ob, bDeformGroup *defgroup)
|
|
|
|
|
{
|
2023-03-08 12:35:58 +01:00
|
|
|
if (ob->type == OB_GPENCIL_LEGACY) {
|
2018-07-31 10:22:19 +02:00
|
|
|
BKE_gpencil_vgroup_remove(ob, defgroup);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2019-04-22 09:39:35 +10:00
|
|
|
if (BKE_object_is_in_editmode_vgroup(ob)) {
|
2018-07-31 10:22:19 +02:00
|
|
|
object_defgroup_remove_edit_mode(ob, defgroup);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2018-07-31 10:22:19 +02:00
|
|
|
object_defgroup_remove_object_mode(ob, defgroup);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-01-31 17:45:59 +01:00
|
|
|
if (ob->type == OB_GREASE_PENCIL) {
|
|
|
|
|
blender::bke::greasepencil::validate_drawing_vertex_groups(
|
|
|
|
|
*static_cast<GreasePencil *>(ob->data));
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 13:36:30 -03:00
|
|
|
BKE_object_batch_cache_dirty_tag(ob);
|
2018-07-31 10:22:19 +02:00
|
|
|
}
|
2014-11-18 23:52:17 +01:00
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
void BKE_object_defgroup_remove_all_ex(Object *ob, bool only_unlocked)
|
2014-11-18 23:52:17 +01:00
|
|
|
{
|
2021-07-13 12:10:34 -04:00
|
|
|
ListBase *defbase = BKE_object_defgroup_list_mutable(ob);
|
|
|
|
|
|
|
|
|
|
bDeformGroup *dg = (bDeformGroup *)defbase->first;
|
2014-11-18 23:52:17 +01:00
|
|
|
const bool edit_mode = BKE_object_is_in_editmode_vgroup(ob);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
if (dg) {
|
|
|
|
|
while (dg) {
|
|
|
|
|
bDeformGroup *next_dg = dg->next;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-10-31 15:31:47 +01:00
|
|
|
if (!only_unlocked || (dg->flag & DG_LOCK_WEIGHT) == 0) {
|
2019-04-22 09:39:35 +10:00
|
|
|
if (edit_mode) {
|
2016-10-31 15:31:47 +01:00
|
|
|
object_defgroup_remove_edit_mode(ob, dg);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2016-10-31 15:31:47 +01:00
|
|
|
object_defgroup_remove_object_mode(ob, dg);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2016-10-31 15:31:47 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
dg = next_dg;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-20 21:17:09 +10:00
|
|
|
else { /* `defbase` is empty. */
|
|
|
|
|
/* Remove all deform-verts. */
|
2014-11-18 23:52:17 +01:00
|
|
|
if (ob->type == OB_MESH) {
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(ob->data);
|
2023-12-20 02:21:48 +01:00
|
|
|
CustomData_free_layer_active(&mesh->vert_data, CD_MDEFORMVERT, mesh->verts_num);
|
2014-11-18 23:52:17 +01:00
|
|
|
}
|
|
|
|
|
else if (ob->type == OB_LATTICE) {
|
|
|
|
|
Lattice *lt = object_defgroup_lattice_get((ID *)(ob->data));
|
2021-08-06 13:59:38 +10:00
|
|
|
MEM_SAFE_FREE(lt->dvert);
|
2014-11-18 23:52:17 +01:00
|
|
|
}
|
2024-01-31 17:45:59 +01:00
|
|
|
else if (ob->type == OB_GREASE_PENCIL) {
|
|
|
|
|
GreasePencil *grease_pencil = static_cast<GreasePencil *>(ob->data);
|
|
|
|
|
blender::bke::greasepencil::clear_vertex_groups(*grease_pencil);
|
|
|
|
|
}
|
2014-11-18 23:52:17 +01:00
|
|
|
/* Fix counters/indices */
|
2021-07-13 12:10:34 -04:00
|
|
|
BKE_object_defgroup_active_index_set(ob, 0);
|
2014-11-18 23:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
void BKE_object_defgroup_remove_all(Object *ob)
|
2016-10-31 15:31:47 +01:00
|
|
|
{
|
|
|
|
|
BKE_object_defgroup_remove_all_ex(ob, false);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 09:57:41 +11:00
|
|
|
int *BKE_object_defgroup_index_map_create(Object *ob_src, Object *ob_dst, int *r_map_len)
|
|
|
|
|
{
|
2021-07-13 12:10:34 -04:00
|
|
|
const ListBase *src_defbase = BKE_object_defgroup_list(ob_src);
|
|
|
|
|
const ListBase *dst_defbase = BKE_object_defgroup_list(ob_dst);
|
|
|
|
|
|
2018-03-06 09:57:41 +11:00
|
|
|
/* Build src to merged mapping of vgroup indices. */
|
2021-07-13 12:10:34 -04:00
|
|
|
if (BLI_listbase_is_empty(src_defbase) || BLI_listbase_is_empty(dst_defbase)) {
|
2018-03-06 09:57:41 +11:00
|
|
|
*r_map_len = 0;
|
2023-07-17 10:46:26 +02:00
|
|
|
return nullptr;
|
2018-03-06 09:57:41 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-03-06 09:57:41 +11:00
|
|
|
bDeformGroup *dg_src;
|
2021-07-13 12:10:34 -04:00
|
|
|
*r_map_len = BLI_listbase_count(src_defbase);
|
2023-07-17 10:46:26 +02:00
|
|
|
int *vgroup_index_map = static_cast<int *>(
|
|
|
|
|
MEM_malloc_arrayN(*r_map_len, sizeof(*vgroup_index_map), "defgroup index map create"));
|
2018-03-06 09:57:41 +11:00
|
|
|
bool is_vgroup_remap_needed = false;
|
|
|
|
|
int i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
for (dg_src = static_cast<bDeformGroup *>(src_defbase->first), i = 0; dg_src;
|
|
|
|
|
dg_src = dg_src->next, i++)
|
|
|
|
|
{
|
2020-03-06 12:50:56 +11:00
|
|
|
vgroup_index_map[i] = BKE_object_defgroup_name_index(ob_dst, dg_src->name);
|
2018-03-06 09:57:41 +11:00
|
|
|
is_vgroup_remap_needed = is_vgroup_remap_needed || (vgroup_index_map[i] != i);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-03-06 09:57:41 +11:00
|
|
|
if (!is_vgroup_remap_needed) {
|
|
|
|
|
MEM_freeN(vgroup_index_map);
|
2023-07-17 10:46:26 +02:00
|
|
|
vgroup_index_map = nullptr;
|
2018-03-06 09:57:41 +11:00
|
|
|
*r_map_len = 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-03-06 09:57:41 +11:00
|
|
|
return vgroup_index_map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_object_defgroup_index_map_apply(MDeformVert *dvert,
|
|
|
|
|
int dvert_len,
|
|
|
|
|
const int *map,
|
|
|
|
|
int map_len)
|
|
|
|
|
{
|
2023-07-17 10:46:26 +02:00
|
|
|
if (map == nullptr || map_len == 0) {
|
2018-03-06 09:57:41 +11:00
|
|
|
return;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-03-06 09:57:41 +11:00
|
|
|
MDeformVert *dv = dvert;
|
|
|
|
|
for (int i = 0; i < dvert_len; i++, dv++) {
|
|
|
|
|
int totweight = dv->totweight;
|
|
|
|
|
for (int j = 0; j < totweight; j++) {
|
|
|
|
|
int def_nr = dv->dw[j].def_nr;
|
2023-07-18 14:18:07 +10:00
|
|
|
if (uint(def_nr) < uint(map_len) && map[def_nr] != -1) {
|
2018-03-06 09:57:41 +11:00
|
|
|
dv->dw[j].def_nr = map[def_nr];
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
totweight--;
|
|
|
|
|
dv->dw[j] = dv->dw[totweight];
|
|
|
|
|
j--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (totweight != dv->totweight) {
|
|
|
|
|
if (totweight) {
|
2023-07-17 10:46:26 +02:00
|
|
|
dv->dw = static_cast<MDeformWeight *>(MEM_reallocN(dv->dw, sizeof(*dv->dw) * totweight));
|
2018-03-06 09:57:41 +11:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
MEM_SAFE_FREE(dv->dw);
|
|
|
|
|
}
|
|
|
|
|
dv->totweight = totweight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-31 15:31:47 +01:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
bool BKE_object_defgroup_array_get(ID *id, MDeformVert **dvert_arr, int *dvert_tot)
|
|
|
|
|
{
|
|
|
|
|
if (id) {
|
|
|
|
|
switch (GS(id->name)) {
|
|
|
|
|
case ID_ME: {
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = (Mesh *)id;
|
2023-12-12 20:45:16 -05:00
|
|
|
*dvert_arr = mesh->deform_verts_for_write().data();
|
2023-12-20 02:21:48 +01:00
|
|
|
*dvert_tot = mesh->verts_num;
|
2014-11-18 23:52:17 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
case ID_LT: {
|
|
|
|
|
Lattice *lt = object_defgroup_lattice_get(id);
|
|
|
|
|
*dvert_arr = lt->dvert;
|
|
|
|
|
*dvert_tot = lt->pntsu * lt->pntsv * lt->pntsw;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-01-31 17:45:59 +01:00
|
|
|
case ID_GP:
|
|
|
|
|
/* Should not be used with grease pencil objects.*/
|
|
|
|
|
BLI_assert_unreachable();
|
|
|
|
|
break;
|
2017-08-28 11:19:58 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
2014-11-18 23:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
*dvert_arr = nullptr;
|
2014-11-18 23:52:17 +01:00
|
|
|
*dvert_tot = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-12-14 15:49:31 +11:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
/** \} */
|
2012-09-05 02:51:55 +00:00
|
|
|
|
|
|
|
|
/* --- functions for getting vgroup aligned maps --- */
|
|
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
bool *BKE_object_defgroup_lock_flags_get(Object *ob, const int defbase_tot)
|
2012-09-05 02:51:55 +00:00
|
|
|
{
|
2013-05-15 15:52:48 +00:00
|
|
|
bool is_locked = false;
|
2012-09-05 02:51:55 +00:00
|
|
|
int i;
|
2021-07-13 12:10:34 -04:00
|
|
|
ListBase *defbase = BKE_object_defgroup_list_mutable(ob);
|
2023-07-17 10:46:26 +02:00
|
|
|
bool *lock_flags = static_cast<bool *>(MEM_mallocN(defbase_tot * sizeof(bool), "defflags"));
|
2012-09-05 02:51:55 +00:00
|
|
|
bDeformGroup *defgroup;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
for (i = 0, defgroup = static_cast<bDeformGroup *>(defbase->first); i < defbase_tot && defgroup;
|
|
|
|
|
defgroup = defgroup->next, i++)
|
|
|
|
|
{
|
2012-09-05 02:51:55 +00:00
|
|
|
lock_flags[i] = ((defgroup->flag & DG_LOCK_WEIGHT) != 0);
|
|
|
|
|
is_locked |= lock_flags[i];
|
|
|
|
|
}
|
|
|
|
|
if (is_locked) {
|
|
|
|
|
return lock_flags;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-05 02:51:55 +00:00
|
|
|
MEM_freeN(lock_flags);
|
2023-07-17 10:46:26 +02:00
|
|
|
return nullptr;
|
2012-09-05 02:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
bool *BKE_object_defgroup_validmap_get(Object *ob, const int defbase_tot)
|
2012-09-05 02:51:55 +00:00
|
|
|
{
|
|
|
|
|
bDeformGroup *dg;
|
|
|
|
|
ModifierData *md;
|
2014-11-18 23:52:17 +01:00
|
|
|
bool *defgroup_validmap;
|
2012-09-05 02:51:55 +00:00
|
|
|
GHash *gh;
|
|
|
|
|
int i, step1 = 1;
|
2021-07-13 12:10:34 -04:00
|
|
|
const ListBase *defbase = BKE_object_defgroup_list(ob);
|
2023-07-27 12:04:18 +10:00
|
|
|
VirtualModifierData virtual_modifier_data;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-07-13 12:10:34 -04:00
|
|
|
if (BLI_listbase_is_empty(defbase)) {
|
2023-07-17 10:46:26 +02:00
|
|
|
return nullptr;
|
2012-09-05 02:51:55 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
gh = BLI_ghash_str_new_ex(__func__, defbase_tot);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-05 02:51:55 +00:00
|
|
|
/* add all names to a hash table */
|
2023-08-04 08:51:13 +10:00
|
|
|
LISTBASE_FOREACH (bDeformGroup *, dg, defbase) {
|
2023-07-17 10:46:26 +02:00
|
|
|
BLI_ghash_insert(gh, dg->name, nullptr);
|
2012-09-05 02:51:55 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-02-15 23:36:11 +11:00
|
|
|
BLI_assert(BLI_ghash_len(gh) == defbase_tot);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-05 02:51:55 +00:00
|
|
|
/* now loop through the armature modifiers and identify deform bones */
|
2023-07-17 10:46:26 +02:00
|
|
|
for (md = static_cast<ModifierData *>(ob->modifiers.first); md;
|
|
|
|
|
md = !md->next && step1 ? (step1 = 0),
|
2023-07-27 12:04:18 +10:00
|
|
|
BKE_modifiers_get_virtual_modifierlist(ob, &virtual_modifier_data) :
|
2023-07-17 10:46:26 +02:00
|
|
|
md->next)
|
2013-08-19 09:05:34 +00:00
|
|
|
{
|
2019-04-22 09:39:35 +10:00
|
|
|
if (!(md->mode & (eModifierMode_Realtime | eModifierMode_Virtual))) {
|
2012-09-05 02:51:55 +00:00
|
|
|
continue;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-05 02:51:55 +00:00
|
|
|
if (md->type == eModifierType_Armature) {
|
|
|
|
|
ArmatureModifierData *amd = (ArmatureModifierData *)md;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-05 02:51:55 +00:00
|
|
|
if (amd->object && amd->object->pose) {
|
|
|
|
|
bPose *pose = amd->object->pose;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-08-04 08:51:13 +10:00
|
|
|
LISTBASE_FOREACH (bPoseChannel *, chan, &pose->chanbase) {
|
2013-08-18 03:41:39 +00:00
|
|
|
void **val_p;
|
2019-04-22 09:39:35 +10:00
|
|
|
if (chan->bone->flag & BONE_NO_DEFORM) {
|
2012-09-05 02:51:55 +00:00
|
|
|
continue;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-08-18 03:41:39 +00:00
|
|
|
val_p = BLI_ghash_lookup_p(gh, chan->name);
|
|
|
|
|
if (val_p) {
|
2018-09-19 12:05:58 +10:00
|
|
|
*val_p = POINTER_FROM_INT(1);
|
2012-09-05 02:51:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-09-05 02:51:55 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
defgroup_validmap = static_cast<bool *>(
|
|
|
|
|
MEM_mallocN(sizeof(*defgroup_validmap) * defbase_tot, "wpaint valid map"));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-05 02:51:55 +00:00
|
|
|
/* add all names to a hash table */
|
2023-07-17 10:46:26 +02:00
|
|
|
for (dg = static_cast<bDeformGroup *>(defbase->first), i = 0; dg; dg = dg->next, i++) {
|
|
|
|
|
defgroup_validmap[i] = (BLI_ghash_lookup(gh, dg->name) != nullptr);
|
2012-09-05 02:51:55 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-02-15 23:36:11 +11:00
|
|
|
BLI_assert(i == BLI_ghash_len(gh));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
BLI_ghash_free(gh, nullptr, nullptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
return defgroup_validmap;
|
2012-09-05 02:51:55 +00:00
|
|
|
}
|
2012-09-05 03:45:32 +00:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
bool *BKE_object_defgroup_selected_get(Object *ob, int defbase_tot, int *r_dg_flags_sel_tot)
|
2012-09-05 03:45:32 +00:00
|
|
|
{
|
2023-07-17 10:46:26 +02:00
|
|
|
bool *dg_selection = static_cast<bool *>(MEM_mallocN(defbase_tot * sizeof(bool), __func__));
|
2012-09-05 03:45:32 +00:00
|
|
|
bDeformGroup *defgroup;
|
2022-09-26 10:56:05 +10:00
|
|
|
uint i;
|
2012-09-05 03:45:32 +00:00
|
|
|
Object *armob = BKE_object_pose_armature_get(ob);
|
|
|
|
|
(*r_dg_flags_sel_tot) = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-07-13 12:10:34 -04:00
|
|
|
const ListBase *defbase = BKE_object_defgroup_list(ob);
|
|
|
|
|
|
2012-09-05 03:45:32 +00:00
|
|
|
if (armob) {
|
|
|
|
|
bPose *pose = armob->pose;
|
2023-07-17 10:46:26 +02:00
|
|
|
for (i = 0, defgroup = static_cast<bDeformGroup *>(defbase->first);
|
|
|
|
|
i < defbase_tot && defgroup;
|
|
|
|
|
defgroup = defgroup->next, i++)
|
|
|
|
|
{
|
2012-09-05 03:45:32 +00:00
|
|
|
bPoseChannel *pchan = BKE_pose_channel_find_name(pose, defgroup->name);
|
|
|
|
|
if (pchan && (pchan->bone->flag & BONE_SELECTED)) {
|
2014-04-01 11:34:00 +11:00
|
|
|
dg_selection[i] = true;
|
2012-09-05 03:45:32 +00:00
|
|
|
(*r_dg_flags_sel_tot) += 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2014-04-01 11:34:00 +11:00
|
|
|
dg_selection[i] = false;
|
2012-09-05 03:45:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2014-04-01 11:34:00 +11:00
|
|
|
memset(dg_selection, false, sizeof(*dg_selection) * defbase_tot);
|
2012-09-05 03:45:32 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-05 03:45:32 +00:00
|
|
|
return dg_selection;
|
|
|
|
|
}
|
2014-11-18 23:52:17 +01:00
|
|
|
|
2018-10-07 18:25:51 +03:00
|
|
|
bool BKE_object_defgroup_check_lock_relative(const bool *lock_flags,
|
|
|
|
|
const bool *validmap,
|
|
|
|
|
int index)
|
|
|
|
|
{
|
|
|
|
|
return validmap && validmap[index] && !(lock_flags && lock_flags[index]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BKE_object_defgroup_check_lock_relative_multi(int defbase_tot,
|
|
|
|
|
const bool *lock_flags,
|
|
|
|
|
const bool *selected,
|
|
|
|
|
int sel_tot)
|
|
|
|
|
{
|
2023-07-17 10:46:26 +02:00
|
|
|
if (lock_flags == nullptr) {
|
2018-10-07 18:25:51 +03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
if (selected == nullptr || sel_tot <= 1) {
|
2018-10-07 18:25:51 +03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < defbase_tot; i++) {
|
|
|
|
|
if (selected[i] && lock_flags[i]) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 16:06:07 +05:30
|
|
|
bool BKE_object_defgroup_active_is_locked(const Object *ob)
|
|
|
|
|
{
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(ob->data);
|
2023-07-27 16:06:07 +05:30
|
|
|
bDeformGroup *dg = static_cast<bDeformGroup *>(
|
2023-12-08 16:40:06 -05:00
|
|
|
BLI_findlink(&mesh->vertex_group_names, mesh->vertex_group_active_index - 1));
|
2023-07-27 16:06:07 +05:30
|
|
|
return dg->flag & DG_LOCK_WEIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 18:25:51 +03:00
|
|
|
void BKE_object_defgroup_split_locked_validmap(
|
|
|
|
|
int defbase_tot, const bool *locked, const bool *deform, bool *r_locked, bool *r_unlocked)
|
|
|
|
|
{
|
|
|
|
|
if (!locked) {
|
|
|
|
|
if (r_unlocked != deform) {
|
|
|
|
|
memcpy(r_unlocked, deform, sizeof(bool) * defbase_tot);
|
|
|
|
|
}
|
|
|
|
|
if (r_locked) {
|
|
|
|
|
memset(r_locked, 0, sizeof(bool) * defbase_tot);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < defbase_tot; i++) {
|
|
|
|
|
bool is_locked = locked[i];
|
|
|
|
|
bool is_deform = deform[i];
|
|
|
|
|
|
|
|
|
|
r_locked[i] = is_deform && is_locked;
|
|
|
|
|
r_unlocked[i] = is_deform && !is_locked;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
void BKE_object_defgroup_mirror_selection(Object *ob,
|
2016-01-21 08:03:15 +11:00
|
|
|
int defbase_tot,
|
|
|
|
|
const bool *dg_selection,
|
|
|
|
|
bool *dg_flags_sel,
|
|
|
|
|
int *r_dg_flags_sel_tot)
|
|
|
|
|
{
|
2021-07-13 12:10:34 -04:00
|
|
|
const ListBase *defbase = BKE_object_defgroup_list(ob);
|
|
|
|
|
|
2016-01-21 08:03:15 +11:00
|
|
|
bDeformGroup *defgroup;
|
2022-09-26 10:56:05 +10:00
|
|
|
uint i;
|
2016-01-25 16:55:08 +01:00
|
|
|
int i_mirr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
for (i = 0, defgroup = static_cast<bDeformGroup *>(defbase->first); i < defbase_tot && defgroup;
|
|
|
|
|
defgroup = defgroup->next, i++)
|
|
|
|
|
{
|
2016-01-21 08:03:15 +11:00
|
|
|
if (dg_selection[i]) {
|
|
|
|
|
char name_flip[MAXBONENAME];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-01-16 20:34:13 +01:00
|
|
|
BLI_string_flip_side_name(name_flip, defgroup->name, false, sizeof(name_flip));
|
2020-03-06 12:50:56 +11:00
|
|
|
i_mirr = STREQ(name_flip, defgroup->name) ? i :
|
|
|
|
|
BKE_object_defgroup_name_index(ob, name_flip);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-01-21 08:03:15 +11:00
|
|
|
if ((i_mirr >= 0 && i_mirr < defbase_tot) && (dg_flags_sel[i_mirr] == false)) {
|
|
|
|
|
dg_flags_sel[i_mirr] = true;
|
|
|
|
|
(*r_dg_flags_sel_tot) += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-18 23:52:17 +01:00
|
|
|
|
|
|
|
|
bool *BKE_object_defgroup_subset_from_select_type(Object *ob,
|
|
|
|
|
eVGroupSelect subset_type,
|
|
|
|
|
int *r_defgroup_tot,
|
|
|
|
|
int *r_subset_count)
|
|
|
|
|
{
|
2023-07-17 10:46:26 +02:00
|
|
|
bool *defgroup_validmap = nullptr;
|
2021-07-13 12:10:34 -04:00
|
|
|
|
|
|
|
|
*r_defgroup_tot = BKE_object_defgroup_count(ob);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
switch (subset_type) {
|
|
|
|
|
case WT_VGROUP_ACTIVE: {
|
2021-07-13 12:10:34 -04:00
|
|
|
const int def_nr_active = BKE_object_defgroup_active_index_get(ob) - 1;
|
2023-07-17 10:46:26 +02:00
|
|
|
defgroup_validmap = static_cast<bool *>(
|
|
|
|
|
MEM_mallocN(*r_defgroup_tot * sizeof(*defgroup_validmap), __func__));
|
2014-11-18 23:52:17 +01:00
|
|
|
memset(defgroup_validmap, false, *r_defgroup_tot * sizeof(*defgroup_validmap));
|
|
|
|
|
if ((def_nr_active >= 0) && (def_nr_active < *r_defgroup_tot)) {
|
|
|
|
|
*r_subset_count = 1;
|
|
|
|
|
defgroup_validmap[def_nr_active] = true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
*r_subset_count = 0;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case WT_VGROUP_BONE_SELECT: {
|
|
|
|
|
defgroup_validmap = BKE_object_defgroup_selected_get(ob, *r_defgroup_tot, r_subset_count);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case WT_VGROUP_BONE_DEFORM: {
|
|
|
|
|
int i;
|
|
|
|
|
defgroup_validmap = BKE_object_defgroup_validmap_get(ob, *r_defgroup_tot);
|
|
|
|
|
*r_subset_count = 0;
|
|
|
|
|
for (i = 0; i < *r_defgroup_tot; i++) {
|
|
|
|
|
if (defgroup_validmap[i] == true) {
|
|
|
|
|
*r_subset_count += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case WT_VGROUP_BONE_DEFORM_OFF: {
|
|
|
|
|
int i;
|
|
|
|
|
defgroup_validmap = BKE_object_defgroup_validmap_get(ob, *r_defgroup_tot);
|
|
|
|
|
*r_subset_count = 0;
|
|
|
|
|
for (i = 0; i < *r_defgroup_tot; i++) {
|
|
|
|
|
defgroup_validmap[i] = !defgroup_validmap[i];
|
|
|
|
|
if (defgroup_validmap[i] == true) {
|
|
|
|
|
*r_subset_count += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case WT_VGROUP_ALL:
|
|
|
|
|
default: {
|
2023-07-17 10:46:26 +02:00
|
|
|
defgroup_validmap = static_cast<bool *>(
|
|
|
|
|
MEM_mallocN(*r_defgroup_tot * sizeof(*defgroup_validmap), __func__));
|
2014-11-18 23:52:17 +01:00
|
|
|
memset(defgroup_validmap, true, *r_defgroup_tot * sizeof(*defgroup_validmap));
|
|
|
|
|
*r_subset_count = *r_defgroup_tot;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-18 23:52:17 +01:00
|
|
|
return defgroup_validmap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_object_defgroup_subset_to_index_array(const bool *defgroup_validmap,
|
|
|
|
|
const int defgroup_tot,
|
|
|
|
|
int *r_defgroup_subset_map)
|
|
|
|
|
{
|
|
|
|
|
int i, j = 0;
|
|
|
|
|
for (i = 0; i < defgroup_tot; i++) {
|
|
|
|
|
if (defgroup_validmap[i]) {
|
|
|
|
|
r_defgroup_subset_map[j++] = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|