0
$\begingroup$

I need to find a way to remove vertex groups that got a dot on their names, I tried with this code but it didnt work (well, the find part works, but it is not removing the vertex group):

for vertexname in bpy.context.object.vertex_groups:
if vertexname.name.find("\."):
    vertexname.remove(vertexname.index)

ValueError: VertexGroup.remove(): error with argument 1, "index" - sequence expected at dimension 1, not 'int'

I'm not very experienced with python either, i could make that code because of my knowledge with lua, not really sure what does a sequence is in python (i was expecting it to be a table with numbers, something like [0, 1, 5, etc] but even with "vertexname.remove([0])" nothing is being removed).

$\endgroup$
0
$\begingroup$
  • Each item vg when iterating over object.vertex_groups is an instance of a bpy.types.VertexGroup object. (just as each of bpy.data.objects is an instance of an bpy.types.Object object)

  • Remove vg from the objects vertex group collection with ob.vertex_groups.remove(vg) A vertex group object also has a remove method, which takes a sequence of vertex indices as an argument. Eg vg.remove([0, 1]). This prob led to the confusion re error message

  • No need to escape out the period.

    >>> str("\.") 
    '\\.'
    
  • If a substring is not in string then find(...) returns -1

Script:

import bpy
context = bpy.context

ob = context.object
vgs = [vg for vg in ob.vertex_groups
       if vg.name.find(".") != -1]

while(vgs):
    ob.vertex_groups.remove(vgs.pop())

To make the scripts portable (copy paste jobs) I like to break down variables. Eg one edit to test with ob = context.scene.objects.get("Cube") no matter what the context object.

I favour the "while pop" method, a for vg in vgs: ob.vertex_groups.remove(vg) statement can also be used. The rationale being once an object is removed in blender, trying to re-reference it can have dire consequences... (well sometimes crash blender)

$\endgroup$
  • $\begingroup$ thank you so much for the detailed answer, your code also works awesome! thanks for the help $\endgroup$ – Shuuichi Mar 19 at 2:42

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.