如何在Blender Python导出脚本中重新排列指向顶点的索引,以便我的模型能够正确连接?

问题描述:

我写了一个自定义导出器,将Blender网格转储为简单的二进制格式。我可以从我的脚本导出的文件中读取极其简单的模型,例如多维数据集,但更复杂的模型(如Blender中包含的猴子)不起作用。相反,复杂模型有许多错误连接的顶点。我相信,当我循环处理脚本中的顶点索引时,我没有按照正确的顺序进行操作。我如何重新排列指向Blender Python导出脚本中顶点的索引,以便我的顶点能够正确连接?下面是导出脚本(带有解释文件格式的注释)。如何在Blender Python导出脚本中重新排列指向顶点的索引,以便我的模型能够正确连接?

import struct 
import bpy 

def to_index(number): 
    return struct.pack(">I", number) 

def to_GLfloat(number): 
    return struct.pack(">f", number) 

# Output file structure 
# A file is a single mesh 
# 
# A mesh is a list of vertices, normals, and indices 
# 
# index number_of_triangles 
# triangle triangles[number_of_triangles] 
# index number_of_vertices 
# vertex vertices[number_of_vertices] 
# normal vertices[number_of_vertices] 
# 
# A triangles is a 3-tuple of indices pointing to vertices in the corresponding vertex list 
# 
# index vertices[3] 
# 
# A vertex is a 3-tuple of GLfloats 
# 
# GLfloat coordinates[3] 
# 
# A normal is a 3-tuple of GLfloats 
# 
# GLfloat normal[3] 
# 
# A GLfloat is a big endian 4 byte floating point IEEE 754 binary number 
# An index is a big endian unsigned 4 byte binary number 

def write_kmb_file(context, filepath): 

    meshes = bpy.data.meshes 

    if 1 != len(meshes): 
     raise Exception("Expected a single mesh") 

    mesh = meshes[0] 

    faces = mesh.polygons 
    vertex_list = mesh.vertices 

    output = to_index(len(faces)) 

    for face in faces: 
     vertices = face.vertices 
     if len(vertices) != 3: 
      raise Exception("Only triangles were expected") 

     output += to_index(vertices[0]) 
     output += to_index(vertices[1]) 
     output += to_index(vertices[2]) 

    output += to_index(len(vertex_list)) 

    for vertex in vertex_list: 
     x, y, z = vertex.co.to_tuple() 
     output += to_GLfloat(x) 
     output += to_GLfloat(y) 
     output += to_GLfloat(z) 

    for vertex in vertex_list: 
     x, y, z, = vertex.normal.to_tuple() 
     output += to_GLfloat(x) 
     output += to_GLfloat(y) 
     output += to_GLfloat(z) 


    out = open(filepath, 'wb') 
    out.write(output) 
    out.close() 

    return {'FINISHED'} 


from bpy_extras.io_utils import ExportHelper 
from bpy.props import StringProperty 
from bpy.types import Operator 


class ExportKludgyMess(Operator, ExportHelper): 
    bl_idname = "mesh.export_to_kmb" 
    bl_label = "Export KMB" 

    filename_ext = ".kmb" 

    filter_glob = StringProperty(
      default="*.kmb", 
      options={'HIDDEN'}, 
      ) 

    def execute(self, context): 
     return write_kmb_file(context, self.filepath) 

def register(): 
    bpy.utils.register_class(ExportKludgyMess) 

if __name__ == "__main__": 
    register() 

您最有可能只是倾销vert。由于每个vert用于多个面文件格式,如obj指示带'v'的vert,带'vn'的vert法线和带'vt'的UV。然而,这不是抽签顺序。 'f'前缀表示每个脸部的vert的顺序。对于

例F 1/1/1 2/4/3 3/4/5使用 绿党1,2,3 法线1,4,4 UV 1,3,5

我知道它的老,但嘿。