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

时间:2012-09-03 21:31:25

标签: python 3d export blender

我写了一个自定义导出器,它将Blender网格转换为简单的二进制格式。我可以从我的脚本导出的文件中读取非常简单的模型,如立方体,但像Blender附带的猴子这样的复杂模型不起作用。相反,复杂的模型有许多错误连接的顶点。我相信当我在我的脚本中循环遍历顶点索引时,我没有按正确的顺序这样做。如何重新排序指向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()

1 个答案:

答案 0 :(得分:0)

你最有可能只是转储顶点。由于每个顶点用于多个面文件格式,如obj表示带有'v'的顶点,带有'vn'的顶点法线和带有'vt'的UV。然而,这不是绘制顺序。 'f'前缀表示每个面的顶点顺序。对于

例如f 1/1/1 2/4/3 3/4/5使用 顶点1,2,3 法线1,4,4 UV 1,3,5

我知道它很老但是嘿。