搅拌机;自定义导出 'flat' 个三角形顶点

Blender; custom export 'flat' triangle vertices

给定一个由单独对象组成的 Blender(ver 2.9)模型,每个对象都是一个三角形,我有一个脚本可以将它们导出为自定义格式,其中包括每个对象的位置、欧拉旋转和顶点三角形。

因为我的目标程序使用指定的对象欧拉角旋转三角形,我需要'flatten'每个三角形的顶点只转换为 XY 坐标,就像三角形是二维的一样。

这是我的第一个 Blender 脚本,也是第一个 Python 脚本,所以它非常基础,没有任何错误检查。假设我的对象肯定都是三角形 :) 它目前在 3D 中导出顶点 'as-is'。

bl_info = {
    "name": "Dave's Export",
    "author": "Dave",
    "version": (1, 2),
    "blender": (2, 83, 0),
    "location": "File > Export > Dave's Export",
    "warning": "",
    "description": "Export active mesh",
    "category": "Import-Export",
}

import bpy
import os

from bpy_extras.io_utils import ExportHelper

class DavesExport(bpy.types.Operator, ExportHelper):

    bl_idname = "export_scene.custom_export"
    bl_label = "Export"
    bl_options = {'PRESET', 'UNDO'}
    filename_ext = ".dave"

    def execute(self, context):

        filepath = self.filepath
        filepath = bpy.path.ensure_ext(filepath, self.filename_ext)

        out = open(filepath, "w")
        scene = bpy.data.scenes[0]

        for obj in scene.objects:

            if obj.type == 'MESH':

                # Note; obj is just a triangle.

                # Position
                out.write(str(obj.location.x) + ' ' + str(obj.location.y) + ' ' + str(obj.location.z))
                out.write('|')

                # Rotation
                out.write(str(obj.rotation_euler.x) + ' ' + str(obj.rotation_euler.y) + ' ' + str(obj.rotation_euler.z))
                out.write('|')

                # Vertices (x3)
                for vertex in obj.data.vertices:

                    out.write(str(vertex.co.x) + ' ' + str(vertex.co.y) + ' ' + str(vertex.co.z))
                    out.write('|')

                out.write('\n')

        out.close()

        return {'FINISHED'}

    # Constructor (?)

    def invoke(self, context, event):

        wm = context.window_manager
        wm.fileselect_add(self)

        return {'RUNNING_MODAL'}

# This function adds an item to the export menu.

def menu_button(self, context):

    self.layout.operator(DavesExport.bl_idname, text="Dave's Export")

# Executes on Blender startup (?)

def register():

    bpy.utils.register_class(DavesExport)
    bpy.types.TOPBAR_MT_file_export.append(menu_button)

# Executes on Blender exit (?)

def unregister():

    bpy.utils.unregister_class(DavesExport)
    bpy.types.TOPBAR_MT_file_export.remove(menu_button)

(另外,我希望这个脚本代表一个在 2.9 中使用的简单导出器,以便人们可以从中学习 - 我花了一整天的时间来完成这个工作,因为没有很多最新信息关于这个)

我不太确定您的目标是什么,但“展平”顶点听起来像是投影操作,您需要变换顶点位置以使 Z 等于零。为此,您可以计算三角形的面方向并将三角形顶点旋转负数。但是,也不清楚旋转将围绕其发生的局部偏移的意义是什么。 您可以尝试详细说明您想要实现的结果、您正在使用的源数据或您到目前为止的尝试,因为解决方案可能只是在导出时忽略三角形顶点的 Z 值,如果它们是已提供与 X 轴和 Y 轴平面对齐的平面。

从下面的评论中添加答案:

1: 将三角形绕其中心点P旋转其中P是3个顶点的平均位置(V0+V1+V2)/3,旋转R是从面法线导出,它是用向量 V1-V0 和 V2-V0 的点积计算的,如此处详述 顶点需要移动 P 的负数,旋转 R 的负数,然后移动返回 P 或矩阵运算,其中 V 是顶点位置:

V' = V x M(-P) x M(-R) x M(P)

但问题是旋转不通勤,这意味着绕 X 轴旋转然后绕 Y 轴旋转的结果与绕 Y 轴旋转然后绕 X 轴旋转的结果不同。 在您的情况下,绕偏航旋转,然后俯仰,并忽略滚动可能会起作用,但我无法判断哪种方式最适合您,因为如果您先按俯仰旋转,则三角形的滚动会有所不同。

2: 如果您只是想将三角形​​映射到纹理或类似的地方,其中只有比例很重要,而 3d 方向无关紧要,因为目标似乎 neutralize/flatten 然后它简单地获取向量 V1-V0 和 V2-V0 的点积和距离,并从中构建三角形的 2D 表示。