assign vertices to vertex group?

Hi guys,

I’m trying to use skin modifier in the script for 2.64, and wrote this for test.


from bpy import *
import bpy
import math, mathutils, random

bpy.ops.mesh.primitive_cube_add(view_align=False, enter_editmode=False, location=(0, 0, 0), rotation=(0, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))

ob = bpy.context.object

bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.merge(type='CENTER', uvs=True)

bpy.ops.object.modifier_add(type='MIRROR')
bpy.ops.object.modifier_add(type='SKIN')
bpy.ops.object.modifier_add(type='SUBSURF')

for i in range(5):
	extr_y = random.uniform(0.0, 2.0)
	extr_z = random.uniform(-2.0, 2.0)
	bpy.ops.mesh.extrude_region_move(MESH_OT_extrude_region={"mirror":False}, TRANSFORM_OT_translate={"value":(0, extr_y, extr_z), "constraint_axis":(False, False, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "texture_space":False, "release_confirm":False})
	
#bpy.ops.transform.skin_resize(value=(0.3, 0.3, 0.3), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), texture_space=False, release_confirm=False)

bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')

for i, v in enumerate(ob.data.vertices):
	bpy.ops.object.mode_set(mode='EDIT')

	vGroup = 'body' + str(i)
	
	newGroup = ob.vertex_groups.new(vGroup)
	bpy.context.object.data.vertices[i].select = True
	bpy.ops.object.vertex_group_set_active(group=vGroup)
	bpy.ops.object.vertex_group_assign(new=False)

#   bpy.ops.mesh.select_all(action='DESELECT')

A vertice attached skin, mirror and subsurf modifier will extruded well. And I’d like to assign each vertices to vertex groups.(I mean one group has one vertice) In this script, I could make ‘body0’ to ‘body4’ groups, but I couldn’t assign a vertice to the group.

I’m confused, why this script doesn’t work?:frowning:

Hi Shige,

This code snippet makes a new vertex group “ALL” and assigns all vertices to it using the add method, which takes a list of the vertices indices.

http://www.blender.org/documentation/blender_python_api_2_63_release/bpy.types.VertexGroup.html#bpy.types.VertexGroup


        vg = shrink_target.vertex_groups.new(name="ALL")
        verts = []
        for vert in shrink_target.data.vertices:
            verts.append(vert.index)
        vg.add(verts, 1.0, 'ADD')

Also there is an items() method on most collections, which removes the need for enumerate.


for index, vert in mesh.vertices.items():

Thanks batFINGER,

And How to re-select Nth vertice in the list?
I’d like to select one in the list, and extrude like a branch. :slight_smile:

Oh, I mean select a vertice from the verts[] with script.
for example,

bpy.context.object.data.verts[i].select = True

Do you mean like this?