I have the following code, which creates a button in the panel which when clicked creates a vertex group from all vertices in a selected obj. Is there a way I can edit to so that when I click the button it cycles through every obj in the scene and repeats the script? Essentially creating a vertex group for each object with that object's name.
import bpy
def main(context):
bpy.ops.object.editmode_toggle()
bpy.ops.object.vertex_group_add()
bpy.ops.object.vertex_group_assign()
#obj = bpy.context.active_object
sel_obj = bpy.context.selected_objects
for obj in sel_obj:
for g in obj.vertex_groups:
print(g.name)
g.name = obj.name
class CreateVertexGroup(bpy.types.Operator):
"""Tooltip"""
bl_idname = "myops.create_vertex_group"
bl_label = "Create Vertex Group"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
main(context)
return {'FINISHED'}
class CustomVertexGroupScriptsPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Custom Vertex Group Scripts Panel"
bl_idname = "OBJECT_PT_createvertexgroup"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "data"
def draw(self, context):
layout = self.layout
obj = context.object
row = layout.row()
row.label(text="Active object is: " + obj.name)
row = layout.row()
row.operator("myops.create_vertex_group")
def register():
bpy.utils.register_class(CreateVertexGroup)
bpy.utils.register_class(CustomVertexGroupScriptsPanel)
def unregister():
bpy.utils.unregister_class(CreateVertexGroup)
bpy.utils.unregister_class(CustomVertexGroupScriptsPanel)
if __name__ == "__main__":
register()
# test call
bpy.ops.myops.create_vertex_group()
Thanks.