Advertisement
fs495

Batch Delete Vertex Groups script改の2.9x改造

Aug 29th, 2021
1,362
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bl_info = {
  2.     "name": "Remove Unused Vertex Groups",
  3.     "author": "ave, CoDEmanX, あんじっぷ",
  4.     "version": (1, 0),
  5.     #"blender": (2, 70, 0),
  6.     "blender": (2, 80, 0),
  7.     "location": "Properties Editor > Object data > Vertex Groups > Specials menu",
  8.     "description": "Delete Vertex Groups with no assigned weight of active object",
  9.     "warning": "",
  10.    "wiki_url": "",
  11.     "category": "Mesh"}
  12.  
  13. # taken from here:
  14. # https://blenderartists.org/t/batch-delete-vertex-groups-script/449881/5
  15.  
  16.  
  17. import bpy
  18. from bpy.types import Operator
  19.  
  20.  
  21. class OBJECT_OT_remove_unused_vertex_group(Operator):
  22.     bl_idname = "object.remove_unused_vertex_group"
  23.     bl_label = "Remove unused vertex groups"
  24.     bl_options = {'REGISTER', 'UNDO'}
  25.  
  26.     @classmethod
  27.     def poll(cls, context):
  28.         return (context.object is not None and
  29.                 context.object.type == 'MESH')
  30.  
  31.     def execute(self, context):
  32.  
  33.         suffix_map = {".L": ".R", ".l": ".r", "_L": "_R", "_l": "_r",
  34.                       "-L": "-R", "-l": "-r",
  35.                       ".R": ".L", ".r": ".l", "_R": "_L", "_r": "_l",
  36.                       "-R": "-L", "-r": "-l"}
  37.  
  38.         ob = context.object
  39.         ob.update_from_editmode()
  40.  
  41.         has_mirror = len([x for x in ob.modifiers if x.type == 'MIRROR']) > 0
  42.         suffix = suffix_map.keys()
  43.         vgroups = ob.vertex_groups
  44.         vgroup_used = {i: False for i, k in enumerate(vgroups)}
  45.  
  46.         for v in ob.data.vertices:
  47.             for g in v.groups:
  48.                 if g.weight > 0.0:
  49.                     vgroup_used[g.group] = True
  50.                     if has_mirror:
  51.                         name = vgroups[g.group].name
  52.                         if name[-2:] in suffix:
  53.                             i = vgroups.find(name[:-2] + suffix_map[name[-2:]])
  54.                             vgroup_used[i] = True
  55.  
  56.         for i, used in sorted(vgroup_used.items(), reverse=True):
  57.             if not used:
  58.                 vgroups.remove(vgroups[i])
  59.  
  60.         return {'FINISHED'}
  61.  
  62.  
  63. def menu_fn(self, context):
  64.     self.layout.operator(
  65.         OBJECT_OT_remove_unused_vertex_group.bl_idname,
  66.         icon='X'
  67.     )
  68.  
  69.  
  70. def register():
  71.     from bpy.utils import register_class
  72.     register_class(OBJECT_OT_remove_unused_vertex_group)
  73.     bpy.types.MESH_MT_vertex_group_context_menu.prepend(menu_fn)
  74.  
  75.  
  76. def unregister():
  77.     from bpy.utils import unregister_class
  78.     unregister_class(OBJECT_OT_remove_unused_vertex_group)
  79.     bpy.types.MESH_MT_vertex_group_context_menu.remove(menu_fn)
  80.  
  81. if __name__ == "__main__":
  82.     register()
  83.  
Advertisement
RAW Paste Data Copied
Advertisement