I need some help rewriting a piece of code that takes a mesh and iterates over its indices to store indicies in arrays for later use while limiting itself to under 2**16 vertices and keeping faces intact (unless I misinterpreted the code).
def _split_meshes_for_vertex_limit(self, divided_meshes):
new_meshes = []
limit = 2**16
for mesh in divided_meshes:
if len(mesh.vertices) > limit:
start_index = 0
while start_index < len(mesh.indices):
vertex_index_mapping = {}
new_indices = []
for i in range(start_index, len(mesh.indices), 3):
start_index += 3
face = mesh.indices[i:i+3]
for face_index in face:
if not face_index in vertex_index_mapping:
new_index = len(vertex_index_mapping)
vertex_index_mapping[face_index] = new_index
new_indices.append(vertex_index_mapping[face_index])
if len(vertex_index_mapping) >= limit-3:
break
verts = [mesh.vertices[v] for v, index in sorted(vertex_index_mapping.items(), key=lambda k: k[1])]
new_meshes.append(Mesh(mesh.material_id, verts, new_indices))
else:
new_meshes.append(mesh)
return new_meshes
The problem with the current code is that iterating over indices will cause the "submesh" to become a long strip, which is not efficient for culling. Instead, I would like to process the data in 2×2 chunks (with infinite height), and if one chunk exceeds the 2**16 limit, it should be divided recursively into smaller 2×2 chunks.
This is an example image with colored faces that would belong into a chunk, just for better visualization (not using the same mesh as above)
My mesh represents a map, similar to what you might find in a game. The decimate tool has been heavily used to optimize the vertices, so they are not uniform.
Keep in mind that I do not want to split the actual mesh. I only want to store the data in my array (with the faces intact) so I can use it later. Since the faces need to remain intact, the chunks do not need to form a perfect grid but should closely resemble one.
I am new to Blender and am learning as I go. I have tried searching online and asking ChatGPT for help, but I haven't found a solution yet. This code is a bit overwhelming for me, to be honest.


