75 lines
2.6 KiB
Python
75 lines
2.6 KiB
Python
import bpy
|
|
import os
|
|
import struct
|
|
from bpy_extras.io_utils import ExportHelper
|
|
from bpy.types import Operator
|
|
from bpy.props import StringProperty
|
|
|
|
bl_info = {
|
|
"name": "FunnyMesh export",
|
|
"author": "kotofyt",
|
|
"version": (1, 0),
|
|
"blender": (4, 3, 0),
|
|
"location": "File > Export > FunnyMesh (.fmesh_c)",
|
|
"description": "Export meshes as FunnyMeshes",
|
|
"category": "Import-Export",
|
|
}
|
|
|
|
|
|
class ExportFunnyMesh(Operator, ExportHelper):
|
|
"""Export to Funnymesh"""
|
|
bl_idname = "export_scene.fmesh_c"
|
|
bl_label = "Export Funnymesh"
|
|
filename_ext = ".fmesh_c"
|
|
|
|
filter_glob: StringProperty(
|
|
default="*.fmesh_c",
|
|
options={'HIDDEN'},
|
|
)
|
|
|
|
def execute(self, context):
|
|
return export_my_format(self.filepath)
|
|
|
|
def export_my_format(filepath):
|
|
with open(filepath, 'wb') as f:
|
|
|
|
for obj in bpy.context.scene.objects:
|
|
if obj.type == 'MESH':
|
|
mesh = obj.to_mesh()
|
|
mesh.calc_loop_triangles()
|
|
uv_layer = mesh.uv_layers.active.data if mesh.uv_layers.active else None
|
|
for tri in mesh.loop_triangles:
|
|
for loop_index in tri.loops:
|
|
vert = mesh.vertices[mesh.loops[loop_index].vertex_index]
|
|
world_pos = obj.matrix_world @ vert.co
|
|
world_normal = obj.matrix_world @ vert.normal
|
|
f.write(struct.pack('f', world_pos.x))
|
|
f.write(struct.pack('f', world_pos.z))
|
|
f.write(struct.pack('f', world_pos.y))
|
|
if uv_layer:
|
|
uv = uv_layer[loop_index].uv
|
|
f.write(struct.pack('f', uv.x))
|
|
f.write(struct.pack('f', uv.y))
|
|
else:
|
|
f.write(struct.pack('f', 0))
|
|
f.write(struct.pack('f', 0))
|
|
f.write(struct.pack('f', world_normal.x))
|
|
f.write(struct.pack('f', world_normal.z))
|
|
f.write(struct.pack('f', world_normal.y))
|
|
|
|
return {'FINISHED'}
|
|
|
|
def menu_func_export(self, context):
|
|
self.layout.operator(ExportFunnyMesh.bl_idname, text="FunnyMesh (.fmesh_c)")
|
|
|
|
def register():
|
|
bpy.utils.register_class(ExportFunnyMesh)
|
|
bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
|
|
|
|
def unregister():
|
|
bpy.utils.unregister_class(ExportFunnyMesh)
|
|
bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
|
|
|
|
if __name__ == "__main__":
|
|
register()
|