Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bl_info = {
- "name": "Export Model",
- "description": "Export model",
- "author": "Xavier de Boysson",
- "version": (1, 1),
- "blender": (2, 6, 3),
- "location": "File > Export",
- "warning": "",
- "wiki_url": "",
- "tracker_url": "",
- "category": "Import-Export"}
- import bpy
- import re
- from bpy.props import *
- import mathutils, math, struct
- import os
- from os import remove
- import time
- import shutil
- def writeString(file, string):
- file.write(bytes(string, 'UTF-8'))
- def do_export(context, props, filepath):
- texture_list = []
- mat_x90 = mathutils.Matrix.Rotation(-math.pi/2, 4, 'X')
- ob = context.selectable_objects
- file = open(filepath, 'wb')
- tab = ' '
- indices = []
- me = bpy.context.object.data
- for poly in me.polygons:
- for loop_index in poly.loop_indices:
- indices.append(me.loops[loop_index].vertex_index)
- i=0
- for objs in ob:
- if (objs.type == 'MESH'):
- text = objs.active_material.active_texture
- # Make sure that all listed textures are unique
- newTexture = True
- if (len(texture_list) == 0):
- texture_list.append(text.name)
- newTexture = False
- else:
- for n in range(0, len(texture_list)):
- if (texture_list[n] == text.name):
- newTexture = False;
- # Texture Names
- if newTexture:
- texture_list.append(text.name)
- i+=1
- # Header
- writeString(file, '-- <------------------Scene info Start------------------>\r\n')
- writeString(file, '-- Number of Objects: %i\r\n' % i)
- writeString(file, '-- Number of Textures: %i\r\n' % len(texture_list))
- writeString(file, '-- Textures: ')
- for n in range(0, len(texture_list)):
- writeString(file, '%s ' % texture_list[n])
- writeString(file, '\r\n')
- writeString(file, '-- <-------------------Scene info End------------------->\r\n\r\n')
- writeString(file, '\r\n')
- writeString(file, 'Scene = class()\r\n')
- writeString(file, '\r\n')
- writeString(file, 'function Scene:init()\r\n')
- for objs in ob:
- if (objs.type == 'MESH'):
- basename = objs.name.capitalize()
- basename = re.sub(r'[^\w]', '', basename)
- writeString(file, '%sself.mdl%s = %s()\r\n' % (tab, basename, basename))
- writeString(file, 'end\r\n')
- writeString(file, '\r\n')
- writeString(file, 'function Scene:draw()\r\n')
- for objs in ob:
- if (objs.type == 'MESH'):
- basename = objs.name.capitalize()
- basename = re.sub(r'[^\w]', '', basename)
- writeString(file, '%sself.mdl%s:draw()\r\n' % (tab, basename))
- writeString(file, 'end\r\n')
- writeString(file, '\r\n')
- n = 0
- for objs in ob:
- nbVertices = 0
- if (objs.type == 'MESH'):
- n = n+1
- mesh = objs.to_mesh(context.scene, props.apply_modifiers, 'PREVIEW')
- if props.world_space:
- mesh.transform(objs.matrix_world)
- if props.rot_x90:
- mesh.transform(mat_x90)
- basename = objs.name.capitalize()
- basename = re.sub(r'[^\w]', '', basename)
- text = objs.active_material.active_texture
- for face in mesh.tessfaces:
- for index in face.vertices:
- nbVertices+=1
- toto = mesh.getDrawType()
- writeString(file, '-- Position %i \r\n' % toto)
- writeString(file, '-- *** Object %i ***\r\n' % n)
- writeString(file, '-- *** Object %i ***\r\n' % n)
- # Object Name
- writeString(file, '-- name: %s\r\n' % basename)
- # Number of vertices
- writeString(file, '-- vertices: %i\r\n' % nbVertices)
- # Texture Name
- writeString(file, '-- Texture name: %s\r\n' % text.name)
- # Texture Alpha
- writeString(file, '-- texture alpha: %f\r\n\r\n' % objs.active_material.alpha)
- writeString(file, '%s = class()\r\n' % basename)
- writeString(file, '\r\n')
- writeString(file, 'function %s:init()\r\n' % basename)
- writeString(file, '%sself.model = mesh()\r\n' % tab)
- writeString(file, '%sself.model.texture = "Scene:%s"\r\n' % (tab, text.name))
- # Vertices
- writeString(file, '-- Vertices\r\n')
- writeString(file, '%sself.model.vertices = {\r\n' % tab)
- i = 1
- for nb in range(0, len(indices)):
- index = indices[nb]
- vert = mesh.vertices[index]
- writeString(file, '%s%svec3(%f, %f, %f),\r\n' % (tab, tab, vert.co.x, vert.co.y, vert.co.z))
- #writeString(file, '\tvn: %f, %f, %f\r\n' % (vert.normal.x, vert.normal.y, vert.normal.z))
- i+=1
- writeString(file, '%s}\r\n' % tab)
- writeString(file, '\r\n')
- # Texture Coordinates
- writeString(file, '-- Texture Coordinates\r\n')
- writeString(file, '%sself.model.texCoords = {\r\n' % tab)
- if len(mesh.uv_textures) > 0:
- uv_layer = mesh.tessface_uv_textures.active
- for face in mesh.tessfaces:
- faceUV = uv_layer.data[face.index]
- i=0
- for index in face.vertices:
- writeString(file, '%s%svec2(%f, %f),\r\n' % ( tab, tab, faceUV.uv[i][0], faceUV.uv[i][1] ))
- i+=1
- writeString(file, '%s}\r\n' % tab)
- writeString(file, '\r\n')
- writeString(file, '%sself.model:setColors(255,255,255,255)\r\n' % tab)
- writeString(file, 'end\r\n')
- writeString(file, '\r\n')
- writeString(file, 'function %s:draw()\r\n' % basename)
- writeString(file, '%sself.model:draw()\r\n' % tab)
- writeString(file, 'end\r\n')
- writeString(file, '\r\n')
- file.flush()
- file.close()
- return True
- ###### EXPORT OPERATOR #######
- class Export_mdl(bpy.types.Operator):
- '''Exports the active Object as mdl file.'''
- bl_idname = "export_object.mdl"
- bl_label = "Export to map file"
- filepath = StringProperty(subtype='FILE_PATH')
- apply_modifiers = BoolProperty(name="Apply Modifiers",
- description="Applies the Modifiers",
- default=True)
- rot_x90 = BoolProperty(name="Convert to Y-up",
- description="Rotate 90 degrees around X to convert to y-up",
- default=True)
- world_space = BoolProperty(name="Export into Worldspace",
- description="Transform the Vertexcoordinates into Worldspace",
- default=True)
- @classmethod
- def poll(cls, context):
- return context.active_object.type in ['MESH', 'CURVE', 'SURFACE', 'FONT']
- def execute(self, context):
- start_time = time.time()
- print('\r\n_____START_____')
- Filepath = bpy.path.ensure_ext(self.filepath, ".lua")
- exported = do_export(context, self.properties, Filepath)
- if exported:
- print('finished export in %s seconds' %((time.time() - start_time)))
- return {'FINISHED'}
- def invoke(self, context, event):
- wm = context.window_manager
- if True:
- # File selector
- wm.fileselect_add(self) # will run self.execute()
- return {'RUNNING_MODAL'}
- elif True:
- # search the enum
- wm.invoke_search_popup(self)
- return {'RUNNING_MODAL'}
- elif False:
- # Redo popup
- return wm.invoke_props_popup(self, event) #
- elif False:
- return self.execute(context)
- ### REGISTER ###
- def menu_func(self, context):
- self.layout.operator(Export_mdl.bl_idname, text="Export Model")
- def register():
- bpy.utils.register_module(__name__)
- bpy.types.INFO_MT_file_export.append(menu_func)
- def unregister():
- bpy.utils.unregister_module(__name__)
- bpy.types.INFO_MT_file_export.remove(menu_func)
- if __name__ == "__main__":
- register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement