2

我编写了一个自定义导出器,将 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()
4

1 回答 1

1

您很可能只是在倾倒顶点。由于每个顶点都用于多种面文件格式,例如 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

我知道它很旧,但是嘿。

于 2014-01-02T08:25:44.527 回答