.mesh importer?

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
eriger777
Posts: 0
Joined: Wed Jul 17, 2013 4:47 pm

.mesh importer?

Post by eriger777 » Fri Oct 11, 2013 9:41 pm

Hey guys! I'm trying to make a .mesh importer. However, I've tried using the orge tools. But the .mesh is not support for w.e reason. It's from a game. I have an export script for it, I was wondering if somebody could help me make an importer?

Code: Select all

import bpy
from Mathutils import RotationMatrix, ScaleMatrix, Vector

def get_face_vertex_array(face, uvface, mesh, matrix):
	varray = []
	for inx, verti in enumerate(face.verts):
		tcoord = (matrix * mesh.verts[verti].co).to_tuple(3)
		tnorm = (matrix.rotation_part() * face.normal).normalize().to_tuple(3)
		if len(uvface.uv) > 0:
			tuv = (uvface.uv[inx][0], uvface.uv[inx][1], 0)
		else:
			tuv = (0,0,0)
		varray.append((tcoord, tnorm, tuv))
	return varray

def write_tuple_3(tuple, file, flip):
	if flip:
		file.write('['+str(tuple[0])+','+str(tuple[2])+','+str(tuple[1])+']')
	else:
		file.write('['+str(tuple[0])+','+str(tuple[1])+','+str(tuple[2])+']')
	
def write_verts(varray, nverts, file):
	file.write(str(nverts) + '\n')
	for vert in varray:
		write_tuple_3(vert[0], file, True)
		write_tuple_3(vert[1], file, True)
		write_tuple_3(vert[2], file, False)
	file.write('\n')

def do_export(path):
	file = open(path, 'w')
	file.write('version 1.00\n')
	tmp_mesh_verts = []
	tmp_mesh_size = 0
	invert_rot = False
	for obj in bpy.data.objects:
		if obj.type == 'MESH':
			tmp_mesh = obj.create_mesh(True, 'PREVIEW')
			for index, face in enumerate(tmp_mesh.faces):
				verts = get_face_vertex_array(face, tmp_mesh.active_uv_texture.data[index], tmp_mesh, obj.matrix)
				if len(verts) > 3:
					if invert_rot:
						tmp_mesh_verts.extend([verts[2], verts[1], verts[0], verts[3], verts[2], verts[0]])
					else:
						tmp_mesh_verts.extend([verts[0], verts[1], verts[2], verts[0], verts[2], verts[3]])
					tmp_mesh_size += 2
				else:
					if invert_rot:
						tmp_mesh_verts.extend([verts[2], verts[1], verts[0]])
					else:
						tmp_mesh_verts.extend([verts[0], verts[1], verts[2]])
					tmp_mesh_size += 1
	write_verts(tmp_mesh_verts, tmp_mesh_size, file)
	file.close()

class RbxExporter(bpy.types.Operator):
	bl_idname = 'export.mesh'
	bl_label = 'Export RBXMesh'
	path = bpy.props.StringProperty(name="File Path", description="File path used for exporting the RBXMesh file", maxlen= 1024, default= "")

	def execute(self, context):
		print('RBX::Execute')
		do_export(self.properties.path)
		return {'FINISHED'}

	def invoke(self, context, event):
		print('RBX::Invoke')
		wm = context.manager
		wm.add_fileselect(self)
		return {'RUNNING_MODAL'}
		
def menu_func(self, context):
	print('RBX::MenuFunc')
	default_path = bpy.data.filename.replace(".blend", ".mesh")
	self.layout.operator(RbxExporter.bl_idname, text="Roblox Mesh (.mesh)").path = default_path

def register():
	print('RBX::Register')
	bpy.types.register(RbxExporter)
	bpy.types.INFO_MT_file_export.append(menu_func)

def unregister():
	print('RBX::Unregister')
	bpy.types.unregister(RbxExporter)
	bpy.types.INFO_MT_file_export.remove(menu_func)
	
	

Post Reply