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

标签 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()

最佳答案

你很可能只是抛弃了顶点。由于每个顶点都用于多个面文件格式,例如 obj 用“v”表示顶点,用“vn”表示顶点法线,用“vt”表示 UV。但这不是抽签顺序。 “f”前缀表示每个面的顶点顺序。对于

示例 f 1/1/1 2/4/3 3/4/5 使用 顶点 1,2,3 法线 1,4,4 紫外线1,3,5

我知道它很旧,但是嘿。

关于python - 如何在 Blender Python 导出脚本中重新排序指向顶点的索引,以便正确连接我的模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12254228/

相关文章:

python - 在 Windows 上安装 python 库 cffi

python - 在Python中隐藏PDF文件中的信息

python - 反转 python 2.7 中 difflib 的 get_matching_blocks 结果并获取 MISMATCHED block

flutter 显示 3d 对象 flutter_3d_obj

java - LWJGL先进照明系统

java - 如何在实体模式下绘制网格的表面?

sql - 使用 Visio 2010 (Professional Plus) 从 SQL 导出实体关系图

python - numpy:将垂直数组除以其自身元素之一时出现意外结果

javascript - 将谷歌地图热图图层导出为 PDF

c# - 使用 C# 从 Lotus Notes 电子邮件中提取/导出附件