I have a blender file which often has multiple objects in, which when they need editing together I have to assign a new vertex group to to easily select them.

Right now my process is;

  1. Click the object,
  2. Hit tab to select all vertices,
  3. Go to Vertex Groups and hit add,
  4. Rename it for that object,
  5. Hit assign and be done.

Sometimes I have 20 objects so this gets repetitive. Is it possible to create a script in python that can do this for me, and possibly in a batch edit so it does it for all objects selected? If so, how would I go about it?



3 Answers 3


The code below is loops trough every selected object:

  • delete all vertex groups
  • create a new vertex group named as the object itself
  • then assigns every vertices in the object to the group

So all you need to do is, select your objects then Run Script.

import bpy
import sys
import logging
# config for logging

def createVertexGroupForObject(aObject):
    # set to object mide : "VertexGroup.add(): cannot be called while object is in edit mode"

    # remove all vertex groups
    for vertexGroup in aObject.vertex_groups:

    # add new vertex group with the name of object        
    newGroup = aObject.vertex_groups.new(aObject.name)

    # assign objects vertices to vertex group
    verticesToAdd = []
    for vertex in aObject.data.vertices:

        newGroup.add(verticesToAdd, 1.0, "ADD")
        logging.debug("\t\tvertex group added")

# get selected objects
selected = bpy.context.selected_objects

for obj in selected:
    logging.debug("\tselected: " + obj.name)

  • $\begingroup$ Would make remove all other vertex groups optional. $\endgroup$
    – batFINGER
    Sep 22, 2018 at 7:38
  • 1
    $\begingroup$ Feel free to edit the code, The comments (text begins with #) eg: "# remove all vertex groups" are describes what that part does. So if you remove those 2 lines under that comment, your existing vertex groups will stay intact. $\endgroup$ Sep 22, 2018 at 14:26
  • $\begingroup$ Thankyou I'll keep that in mind as a way to optionally not run code. My point is the OP doesn't ask for all existing vgs to be removed. To me it seems an unnecessary addition to adding a vertex group containing all vertices. $\endgroup$
    – batFINGER
    Sep 22, 2018 at 15:10
  • $\begingroup$ You are right, but my experience is: when I run scripts, I often have to run them multiple times, and if I mess up something (eg: didn't select something), this way I don"t have to search for the cause, just hit select all, and re run the code, the result always going to be the same, and i wont have any duplicates. I use to run MySql scripts, and it's a must have to write your code this kind of way $\endgroup$ Sep 23, 2018 at 19:56
  • 1
    $\begingroup$ Your clearly commented answer really helped me! Collecting the verts in a list (verticesToAdd) was the step my script was missing. I thought my_object.data.vertices was a list already, but I guess it's some other type of data. I learned from your use of logging too, and it helped me catch the source of an error. $\endgroup$
    – Mentalist
    Nov 10, 2020 at 2:24

Just created it for myself now, then found this thread here :) Its similar to Tamas-Katona's answer, but little different form.

For all selected objects it create new vertex group with name=prefix+obj_name and assign all vertices to that group. (it is assumed that you select only mesh-objects)

import bpy

def get_all_vertsID(obj):
    verts = []
    for vert in obj.data.vertices:

sel_obj = bpy.context.selected_objects
for obj in sel_obj:
    all_verts_id = get_all_vertsID(obj)
    v_group_name = 'prefix_'+obj.name
    v_group = obj.vertex_groups.new( name = v_group_name )
    v_group.add(all_verts_id, 1.0, 'ADD')

You only need two lines of code, one to create a vertex group with a specific name and the other to assign the selected vertices to this new group.

import bpy

# your code here

# New Vertex group from selected vertices


here my full post: How do I create a vertex group from selected vertices in edit Mode with Python (Blender 2.8)?


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .