1
$\begingroup$

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?

Thanks.

$\endgroup$
4
$\begingroup$

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
logging.basicConfig(level=logging.DEBUG)

def createVertexGroupForObject(aObject):
    # set to object mide : "VertexGroup.add(): cannot be called while object is in edit mode"
    bpy.ops.object.mode_set(mode='OBJECT')    

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

    # 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:
        verticesToAdd.append(vertex.index)

    try:
        newGroup.add(verticesToAdd, 1.0, "ADD")
        logging.debug("\t\tvertex group added")
    except:
        logging.error(sys.exc_info())

# get selected objects
selected = bpy.context.selected_objects

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

    createVertexGroupForObject(obj)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Would make remove all other vertex groups optional. $\endgroup$ – batFINGER Sep 22 '18 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$ – Tamás Katona Sep 22 '18 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 '18 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$ – Tamás Katona Sep 23 '18 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 at 2:24

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.