首页 > 解决方案 > 搅拌机; 自定义导出“平面”三角形顶点

问题描述

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

因为我的目标程序使用指定的对象欧拉角旋转三角形,所以我需要将每个三角形的顶点“展平”为 XY 坐标,就好像三角形是 2D 一样。

这是我的第一个 Blender 脚本和第一个 Python 脚本,所以它非常基础,没有任何错误检查。假设我的对象绝对都是三角形 :) 它目前以 3D 形式“按原样”导出顶点。

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 的简单导出器,以便人们可以从中学习——我花了一整天的时间才让它工作,因为没有太多关于这方面的最新信息)

标签: pythongeometryexportblender

解决方案


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

从以下评论中添加答案:

1:围绕其中心点 P 旋转三角形,其中 P 是 3 个顶点的平均位置 (V0+V1+V2)/3,旋转 R 是从用向量的点积计算的面法线得出的V1-V0 和 V2-V0 在这里详细说明https://stackoverflow.com/a/1516330/15786521顶点需要由 P 的负数移动,由 R 的负数旋转,然后由 P 或在矩阵运算中移回其中 V 是顶点位置:

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

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

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


推荐阅读