diff options
Diffstat (limited to 'source/blender/editors/object/object_vgroup.c')
-rw-r--r-- | source/blender/editors/object/object_vgroup.c | 59 |
1 files changed, 54 insertions, 5 deletions
diff --git a/source/blender/editors/object/object_vgroup.c b/source/blender/editors/object/object_vgroup.c index ccc3e2e8278..6897861c81c 100644 --- a/source/blender/editors/object/object_vgroup.c +++ b/source/blender/editors/object/object_vgroup.c @@ -62,6 +62,7 @@ #include "BKE_depsgraph.h" #include "BKE_mesh_mapping.h" #include "BKE_editmesh.h" +#include "BKE_modifier.h" #include "BKE_report.h" #include "BKE_DerivedMesh.h" #include "BKE_object_deform.h" @@ -2301,7 +2302,6 @@ static void vgroup_blend_subset(Object *ob, const bool *vgroup_validmap, const i if (dvert_array) MEM_freeN(dvert_array); - BLI_SMALLSTACK_FREE(dv_stack); /* not so efficient to get 'dvert_array' again just so unselected verts are NULL'd */ if (use_mirror) { @@ -4324,7 +4324,7 @@ static int vgroup_do_remap(Object *ob, const char *name_array, wmOperator *op) return OPERATOR_FINISHED; } -static int vgroup_sort(void *def_a_ptr, void *def_b_ptr) +static int vgroup_sort_name(void *def_a_ptr, void *def_b_ptr) { bDeformGroup *def_a = (bDeformGroup *)def_a_ptr; bDeformGroup *def_b = (bDeformGroup *)def_b_ptr; @@ -4332,18 +4332,59 @@ static int vgroup_sort(void *def_a_ptr, void *def_b_ptr) return BLI_natstrcmp(def_a->name, def_b->name); } +/* Sorts the weight groups according to the bone hierarchy of the + associated armature (similar to how bones are ordered in the Outliner) */ +static void vgroup_sort_bone_hierarchy(Object *ob, ListBase *bonebase) +{ + if (bonebase == NULL) { + Object *armobj = modifiers_isDeformedByArmature(ob); + if (armobj != NULL) { + bArmature *armature = armobj->data; + bonebase = &armature->bonebase; + } + } + + if (bonebase != NULL) { + Bone *bone; + for (bone = bonebase->last; bone; bone = bone->prev) { + bDeformGroup *dg = defgroup_find_name(ob, bone->name); + vgroup_sort_bone_hierarchy(ob, &bone->childbase); + + if (dg != NULL) { + BLI_remlink(&ob->defbase, dg); + BLI_addhead(&ob->defbase, dg); + } + } + } + + return; +} + +enum { + SORT_TYPE_NAME = 0, + SORT_TYPE_BONEHIERARCHY = 1 +}; + static int vertex_group_sort_exec(bContext *C, wmOperator *op) { Object *ob = ED_object_context(C); char *name_array; int ret; + int sort_type = RNA_enum_get(op->ptr, "sort_type"); /*init remapping*/ name_array = vgroup_init_remap(ob); /*sort vgroup names*/ - BLI_sortlist(&ob->defbase, vgroup_sort); - + switch (sort_type) { + case SORT_TYPE_NAME: + BLI_sortlist(&ob->defbase, vgroup_sort_name); + break; + case SORT_TYPE_BONEHIERARCHY: + vgroup_sort_bone_hierarchy(ob, NULL); + break; + } + /*remap vgroup data to map to correct names*/ ret = vgroup_do_remap(ob, name_array, op); @@ -4359,9 +4400,15 @@ static int vertex_group_sort_exec(bContext *C, wmOperator *op) void OBJECT_OT_vertex_group_sort(wmOperatorType *ot) { + static EnumPropertyItem vgroup_sort_type[] = { + {SORT_TYPE_NAME, "NAME", 0, "Name", ""}, + {SORT_TYPE_BONEHIERARCHY, "BONE_HIERARCHY", 0, "Bone Hierarchy", ""}, + {0, NULL, 0, NULL, NULL} + }; + ot->name = "Sort Vertex Groups"; ot->idname = "OBJECT_OT_vertex_group_sort"; - ot->description = "Sort vertex groups alphabetically"; + ot->description = "Sort vertex groups"; /* api callbacks */ ot->poll = vertex_group_poll; @@ -4369,6 +4416,8 @@ void OBJECT_OT_vertex_group_sort(wmOperatorType *ot) /* flags */ ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + RNA_def_enum(ot->srna, "sort_type", vgroup_sort_type, SORT_TYPE_NAME, "Sort type", "Sort type"); } static int vgroup_move_exec(bContext *C, wmOperator *op) |