# Python export addon: assign faces to vertex groups

Hi I am making an export code for a custom model format.

I need the mesh to be divided in parts, each part has to be made of triangle faces with their vertices assigned to 7 vertex groups.

The following code is supposed to store the faces ingroups, once a group of faces has more than 7 vertex groups, it should go to the next group. Every vertex has only ONE vertex group assigned (one bone) for simplicity.

The problen is, this code only works for the first group, then it sets split = 1, and that's it... split is never 2. So I get the first group with its 7 vertex groups, and the second group with the rest (more than 7).

I'm new in python, I am surelly making some big mistake here, thanks a lot!

for face in obj.data.polygons:

#assign group to face
for vert in face.vertices:
for item in obj.data.vertices[vert].groups:
if split == 0:
if item.group not in VGROUP[0]:
VGROUP[0].append(item.group)
if split == 1:
if item.group not in VGROUP[1]:
VGROUP[1].append(item.group)
if split == 2:
if item.group not in VGROUP[2]:
VGROUP[2].append(item.group)
if split == 3:
if item.group not in VGROUP[3]:
VGROUP[3].append(item.group)
if split == 0 and len(VGROUP[0]) == 7:
split = 1
file.write('Hey, vgroup split 1!\n')
if split == 1 and len(VGROUP[1]) == 7:
split = 2
file.write('Hey, vgroup split 2!\n')
if split == 2 and len(VGROUP[2]) == 7:
split = 3
file.write('Hey, vgroup split 3!\n')

for vert in face.vertices: #store vertices in DATA
DATA[split].append(obj.data.vertices[vert].co[0])
#uv, normal, etc...


# update... edit

Ok I'll try to explain in other way.

I want to divide a list in parts, so that every part contains only x different items.

My code tries to take a list, read it item by item, and when it has found x different items, it stores them in another list, and continues reading the original one.

For example:

a = [0,1,0,1,2,1,0,1,3,0,2,2,4,2,2,6,2,1,6,3,2]

for item in a:

#Take a, read it, if the value is new in r0, store it in r0 until r0 has 3 values:
if split == 0:
if item not in r0:
r0.append(item)
if len(r0) > 2:
split = 1

#Continue reading a, if the value is new in r1, store it in r1 until r1 has 3 values:
if split == 1:
if item not in r0:
r0.append(item)
if len(r1) > 2:
split = 2

#Continue reading a, if the value is new in r2, store it in r2 until r2 has 3 values:
if split == 2:
if item not in r0:
r0.append(item)
if len(r2) > 2:
split = 3
#and so on


The results should be:  r0 = [0,1,2] r1 = [1,0,3] r2 = [0,2,4] 

But the results are very different:

 r0 = [0,1,2] #Ok this one worked !! r1 = [1,2,2,1,0,3,4,6] #Mmm.. What? r2 = [0,1,3,2,4,6] #completely lost 

I export vertices from a mesh, each vertex has one bone assigned to it (or one vertex group), so i get a list containing all the vertices with their vertex group, then i want to split the list in parts containing 7 vertex groups.

Thanks again

• I don't get it... may be can you add some details or some file example? – yhoyo Jun 16 '17 at 14:34
• I added an answer because I could not edit my post. – Mills Jun 16 '17 at 17:35
• you can delete your answer – yhoyo Jun 16 '17 at 18:12

Basically you are popping a from the left until 3 have unique value. This "throws out" the second 0, 1 for instance. As opposed to seeking, and removing if unique, from the left until have 3. This would give a a result [0, 1, 2], [0, 1, 3], ...

a = [0,1,0,1,2,1,0,1,3,0,2,2,4,2,2,6,2,1,6,3,2]
grouplen = 3
groups = []
group = []
while a:
e = a.pop(0)

if e not in group:
group.append(e)

if len(group) == grouplen:
groups.append(group)
group = []

# print out the result

for group in groups:
print(group)


result

[0, 1, 2]
[1, 0, 3]
[0, 2, 4]
[2, 6, 1]
[6, 3, 2]


you must check if the element is in and the length of all list.... some fast implementation:

may be you can add some function.

https://repl.it/IpFK/1

a = [0,1,0,1,2,1,0,1,3,0,2,2,4,2,2,6,2,1,6,3,2]

r0 =[]
r1 =[]
r2 =[]
r3 =[]

for item in a:
if item not in r0 and len(r0)<3 :
r0.append(item)
elif item not in r1 and len(r0)==3 and len(r1)<3:
r1.append(item)
elif item not in r2 and len(r0)==3 and len(r1)==3 and len(r2)<3:
r2.append(item)

print(r0,"\n",r1,"\n",r2,"\n",r3)


Well I spent days with this, and suddenly I got it, thanks anyway.

This cuts a ist in parts, for example, my vertices list, without repeating items in every part. I had to use a while, for loops are not working exactly how I thought in python.

split = 0
r0 = []
r1 = []
r2 = []
#and so on...

a = [0,1,0,1,2,1,0,1,3,0,2,2,4,2,2,6,2,1,6,3,2]
i = 0

while i < len(a):
if split == 0:
if a[i] not in r0:
r0.append(a[i])
if len(r0) == 3:
split = 1
i = i + 1

if split == 1:
if a[i] not in r1:
r1.append(a[i])
if len(r1) == 3:
split = 2
i = i + 1

if split == 2:
if a[i] not in r2:
r2.append(a[i])
if len(r2) == 3:
split = 3
i = i + 1

i = i + 1

#the results are:
r0 = [0,1,2]
r1 = [1,0,3]
r2 = [0,2,4]