0
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Your indent is off. Please also add what you have tried. $\endgroup$ – J. Bakker Mar 26 '18 at 6:15
  • $\begingroup$ I've fixed the indent, as of now not tried anything as unsure what to do. Any help would be appreciated. $\endgroup$ – A S Mar 26 '18 at 13:48
  • $\begingroup$ Indent doesn't look fixed to me. Paste code from your editor, select it in bse editor and hit the code {} button or ctrl - K. $\endgroup$ – batFINGER Mar 26 '18 at 14:38
0
$\begingroup$

Using API methods

In main in the following, saves the current mode, sets mode to 'OBJECT', iterates through all mesh objects in context.selected_objects either uses existing vertex grouped named after object, or creates a new one and assigns all vertices to it with a weight of 1. Then returns context.object back to its original mode.

import bpy
def main(context):
    mode = context.object.mode
    bpy.ops.object.mode_set(mode='OBJECT')
    meshobs = [o for o in context.selected_objects
            if o.type == 'MESH']
    for mo in meshobs:
        me = mo.data
        vg = (mo.vertex_groups.get(mo.name)
                or mo.vertex_groups.new(mo.name))
        # add all verts
        vg.add([i for i in range(len(me.vertices))], 1, 'REPLACE')
    bpy.ops.object.mode_set(mode=mode)


main(bpy.context)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.