@@ -244,36 +244,40 @@ def build_mesh(self, vt_table: "VTTable") -> bpy.types.Mesh:
244244 """
245245 mesh_idxes = vt_table .idxes [self .start_idx : self .start_idx + self .count ]
246246
247+ vertex_map_old_to_new = {}
248+ vertices = []
249+ for idx in mesh_idxes :
250+ if idx not in vertex_map_old_to_new :
251+ vertices .append (vt_table .vertices [idx ])
252+ vertex_map_old_to_new [idx ] = len (vertices ) - 1
253+ else :
254+ pass
255+ idxes = [vertex_map_old_to_new [idx ] for idx in mesh_idxes ]
256+ normals = [(v .nx , v .ny , v .nz ) for v in vertices ]
257+ uvs = [Vector ((v .s , v .t )) for v in vertices ]
258+
247259 # Thanks senderle, https://stackoverflow.com/a/22045226
248260 def chunk (it , size ):
249261 it = iter (it )
250262 return iter (lambda : tuple (itertools .islice (it , size )), ())
251263
252- py_vertices = [(v .x , v .y , v .z ) for v in vt_table . vertices ]
264+ py_vertices = [(v .x , v .y , v .z ) for v in vertices ]
253265 py_faces : List [Tuple [int , int , int ]] = [
254266 # We reverse the winding order to reverse the faces
255267 face [::- 1 ]
256- for face in chunk (mesh_idxes , 3 )
268+ for face in chunk (idxes , 3 )
257269 ]
258270
259- normals = [(v .nx , v .ny , v .nz ) for v in vt_table .vertices ]
260- uvs = [(v .s , v .t ) for v in vt_table .vertices ]
261-
262- print (f"len(py_vertices)" , len (py_vertices ))
263- print (f"len(py_faces)" , len (py_faces ))
264-
265271 me = bpy .data .meshes .new (self .name )
266272 me .from_pydata (py_vertices , [], py_faces )
267273
268- if True or not me .validate (verbose = True ):
274+ if not me .validate (verbose = True ):
269275 # Thanks Dave Prue and their "Import X-Plane Object" addon for the API example
270276 me .uv_layers .new ()
271- me .uv_layers [- 1 ].data .foreach_set (
272- "uv" ,
273- [uv for pair in [uvs [l .vertex_index ] for l in me .loops ] for uv in pair ],
274- )
275277
276- # Is this right?
278+ for mesh_uv_loop , mesh_loop in zip (me .uv_layers [- 1 ].data , me .loops ):
279+ mesh_uv_loop .uv = uvs [mesh_loop .vertex_index ]
280+
277281 for i , vertex in enumerate (me .vertices ):
278282 vertex .normal = normals [i ]
279283
0 commit comments