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