Given a Blender (ver 2.9) model that is made up of separate objects, each of which is a triangle, I have a script which exports them to a custom format that includes the position, euler rotation and vertices of each triangle.
Because my target program rotates the triangle using the specified object euler angles, I need to 'flatten' the vertices of each triangle into just XY coordinates, as if the triangle was 2D.
This is my first Blender script and first Python script, so it's very basic without any error-checking. Assume that my objects are definitely all triangles :) It currently exports the vertices 'as-is' in 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)
(Additionally, I hope this script represents a simple exporter for use in 2.9 so that people can learn from it - it took me an entire day to get this working as there's not a lot of up-to-date information on this)