I have an xml file which contains a list of objects in space ("sectors") each sector has a list of more objects ("seats") which can be the same model or not. For each "sector" I create an object and for each seat in that sector I create a face (made up of 3 verts) from which a particle system will instantiate a mesh using its normal. Till here no issue, my problem arrives when I try to put each face (seat) into a vertex group which i create and which will contain all the faces "seats" representing each seat model.
I can`t manage to assign a list of vertices into a vertex group, I'been struggling with the vertex_group.add, it always returns me this error:
group.add(selected_verts, 1.0, 'ADD' )
AttributeError: 'bpy_prop_collection' object has no attribute 'add'
location: <unknown location>:-1
Here is the code, I hope it is more self explicatory than my description:
import bpy
import os
import bmesh
import math
import mathutils
from xml.etree import ElementTree
from bpy.props import StringProperty, BoolProperty
from bpy_extras.io_utils import ImportHelper
from bpy.types import Operator
def createMonkeyFromData(tree):
for estadio in tree:
#Create Venue parent
O = bpy.data.objects.new('venue', None)
bpy.context.scene.objects.link(O)
for Sector in estadio.findall('Sector'):
#Create Sector
mesh = bpy.data.meshes.new('mesh')
sector = bpy.data.objects.new(Sector.get('id'), mesh)
bpy.context.scene.objects.link(sector)
bpy.context.scene.objects.active = sector
bm = bmesh.new()
total_seats = 0
#Create Tiers
tier = str(Sector.get('tier'))
if bpy.data.objects.get(tier) is None:
tier_O = bpy.data.objects.new(tier, None)
bpy.context.scene.objects.link(tier_O)
tier_O.parent = O
# Parent sector to venue empty
sector.parent = tier_O
for Seat in Sector:
seat_type = str(Seat.get('prefab'))
if 'px' in Seat.keys():
# Get Position Data
posX, posY, posZ = float(Seat.get('px')), float(Seat.get('py')), float(Seat.get('pz'))
rotX, rotY, rotZ = float(Seat.get('rx')), float(Seat.get('ry')), float(Seat.get('rz'))
#Total number of seats for each sector
total_seats = total_seats + 1
#Create vertex and face for each position and orient it
v1 = bm.verts.new((posX-0.01, posZ, posY-0.01))
v2 = bm.verts.new((posX+0.01, posZ, posY-0.01))
v3 = bm.verts.new((posX, posZ, posY+0.01))
f1 = bm.faces.new([v1,v2,v3])
vert_list = [v1,v2,v3]
mat_rot = mathutils.Matrix.Rotation(math.radians(-rotY), 3, 'Z')
bmesh.ops.rotate(bm, cent= (posX, posZ, posY), matrix = mat_rot, verts= vert_list)
selected_verts = [v.index for v in vert_list if v.select]
bpy.ops.object.mode_set(mode='OBJECT')
#Detect seat and create vertex group
if bpy.context.object.vertex_groups.get(seat_type) is None:
group = bpy.context.object.vertex_groups
group.new(name=seat_type)
group.add(selected_verts, 1.0, 'ADD' )
bm.to_mesh(mesh)
bm.free()
The vertex groups are created with no problems but vertices wont get assign to its corresponding group, any idea on what I'm missing?
EDIT
As @sambler suggested I've tried selecting the vertices inside a loop but still not working, which is very strange because the script @sambler shared works fine. Right now the code has been updated like this:
#Detect seat and create vertex group
for v in f1.verts:
if bpy.context.object.vertex_groups.get(seat_type) is None:
group = bpy.context.object.vertex_groups.new(name=seat_type)
f1.select = True
group.add([v.index], 1.0, 'ADD' )
The group.add
is not assigning the vertices to its vertex_group