I am trying to export the vertices, triangles, vertex normals, and UVs with blender's Python (bpy). I need code that actually works. My program is supposed to run like this:
Loop through and create a face with UVs and verteices for each index,
Loop through vertices and create a new list of vertices and uvs that are not duplicates
Loop through faces and adjust the indices for the newer vertex list
Export
A duplicate vertex is a vertex that shares the same location and uv location as the other (I need some duplicate vertices because they do not share the same uv)
So basicly, I want to remove vertecies that have the same location and uv and create a list of faces that can go with it.
I am not sure what is wrong with my code. I could share images onces I figure out how.
#import bpy
import bpy
#my house
house = bpy.context.object.data
uv_layer = house.uv_layers.active.data
#basics
triangles = []
vertices = []
uv = []
#(the house is already split into triangles)
for i0, tri in enumerate(house.polygons):
triangles.append([])#create a list
for loop_index in range(tri.loop_start, tri.loop_start+tri.loop_total):
i1 = house.loops[loop_index].vertex_index#vertex index
triangles[i0].append(i1)#add vertex index to triangle (all I want)
vertices.append(house.vertices[i1])#add vertex
uv.append(uv_layer[loop_index].uv)#add uv
#new arrays
newvertices = []
newuv = []
skip = []#contain indices that should be skipped
#-1 means it should not be skipped
for i in range(len(vertices)):
skip.append(-1)
#loop through vertices
for i, vert0 in enumerate(vertices):
if skip[i] == -1:
index = len(newvertices)#amount of new vertices
skip[i] = index#this index routs to the newest vertex
#create new vertex and uv
uv0 = uv[i]
newvertices.append(vert0)
newuv.append(uv0)
#loop through remaning vertices
for j in range(i+1, len(vertices)):
#create vertex2 and uv2 for testing
vert1 = vertices[j]
uv1 = uv[j]
#if vertex1 is vertex2
if vert0.co == vert1.co:
#if both vertices have the same uv
if uv0 == uv1:
#mark this index as skippable and rout it to the newest vertex
skip[j] = index
newtriangles = []
#re-order triangles
for i in range(len(triangles)):
newtriangles.append([])
for j in range(len(triangles[i])):
newtriangles[i].append(skip[triangles[i][j]])
vertex_str = ""
normal_str = ""
triangle_str = ""
uv_str = ""
for vertex in newvertices:
vertex_str += "%s,%s,%s,"%vertex.co[:]
normal_str += "%s,%s,%s,"%vertex.normal[:]
for tri in newtriangles:
triangle_str += "%d,"%tri[0]
triangle_str += "%d,"%tri[1]
triangle_str += "%d,"%tri[2]
for uv in newuv:
uv_str += "%s,%s,"%uv[:]
f = open("c:/users/.../desktop/house/triangles.txt", "w")
f.write(triangle_str)
f.close()
f = open("c:/users/.../desktop/house/vertices.txt", "w")
f.write(vertex_str)
f.close()
f = open("c:/users/.../desktop/house/normals.txt", "w")
f.write(normal_str)
f.close()
f = open("c:/users/.../desktop/house/uv.txt", "w")
f.write(uv_str)
[2]: https://i.stack.imgur.com/xAGM0.png
triangle_str += "%d,"%tri[2]
twice. One of them should usetri[1]
. $\endgroup$triangles
holds the Blender vertex indices (loop.vertex_index
), not indices intovertices
. Try replacingtriangles[i0].append(i1)
withtriangles[i0].append(len(vertices))
in the first loop. $\endgroup$