1
$\begingroup$

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.

my program

#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![enter image description here](https://i.stack.imgur.com/ihNKb.png)

$\endgroup$
5
  • $\begingroup$ You have the line triangle_str += "%d,"%tri[2] twice. One of them should use tri[1]. $\endgroup$ – scurest Jan 24 at 2:39
  • $\begingroup$ I dont in mine, must have been an error putting it up here $\endgroup$ – John Porter Jan 24 at 2:46
  • $\begingroup$ There is a problem with mapping back to the triangles, I get the correct number of faves and nods, but the faces are out of order $\endgroup$ – John Porter Jan 24 at 3:00
  • $\begingroup$ triangles holds the Blender vertex indices (loop.vertex_index), not indices into vertices. Try replacing triangles[i0].append(i1) with triangles[i0].append(len(vertices)) in the first loop. $\endgroup$ – scurest Jan 24 at 3:08
  • $\begingroup$ skip is the triangles indexes ordered, $\endgroup$ – John Porter Jan 24 at 3:53
0
$\begingroup$

I was putting skip into triangle when skip was already the triangle face re-ranged House

$\endgroup$

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.