#!BPY """ Name: 'Nintendo DS Display List (.c)' Blender: 243 Group: 'Export' Tooltip: 'Export C file for use with Nintendo DS' """ __author__ = "Brian \"Sir Alaran\" Schott" __url__ = ("http://www.hackerpilot.org/dsblender.php") __bpydoc__ = """\ This script exports Netimmerse and Gamebryo .nif files from Blender. """ # Last updated March 8 2008 #~ This program is free software; you can redistribute it and/or #~ modify it under the terms of the GNU General Public License version #~ 2 as published by the Free Software Foundation. #~ This program is distributed in the hope that it will be useful, #~ but WITHOUT ANY WARRANTY; without even the implied warranty of #~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #~ GNU General Public License for more details. #~ You should have received a copy of the GNU General Public License #~ along with this program; if not, write to the Free Software #~ Foundation, Inc., 59 Temple Place - Suite 330, Boston, #~ MA 02111-1307, USA. from Blender import Scene, Mesh, Window, sys import BPyMessages import bpy import Blender class Tri: def __init__(self): pass def set(self, a, b, c, n): self.a = a self.b = b self.c = c self.n = n def createTris(me): trilist = [] for face in me.faces: n = face.no if len(face.verts) == 3: tri = Tri() tri.set(face.verts[0], face.verts[1], face.verts[2], n) trilist = trilist + [tri] else: tri = Tri() tri.set(face.verts[0], face.verts[1], face.verts[2], n) trilist = trilist + [tri] tri2 = Tri() tri2.set(face.verts[0], face.verts[2], face.verts[3], n) trilist = trilist + [tri2] return trilist def saveDisplayList(name, trilist, fileName): file = open(fileName, "w") file.write("#include\n\n/* Generated by DS_export.py */\n\nu32 " + name + " [] =\n") file.write("{\n") file.write("\t" + str(len(trilist) * 13 + 5) + ",\n") file.write("\tFIFO_COMMAND_PACK(FIFO_BEGIN, FIFO_NOP, FIFO_NOP, FIFO_NOP),\n") file.write("\tGL_TRIANGLE, 0, 0, 0,\n") for tri in trilist: file.write("\tFIFO_COMMAND_PACK(FIFO_COLOR, FIFO_NOP, FIFO_NOP, FIFO_NOP),\n") file.write("\tRGB15(15, 15, 15), 0, 0, 0,"), file.write("\tFIFO_COMMAND_PACK(FIFO_NORMAL, FIFO_VERTEX16, FIFO_VERTEX16, FIFO_VERTEX16),\n") file.write("\tNORMAL_PACK(floattov10(" + str(tri.n[0]) +"), floattov10(" + str(tri.n[1]) + "), floattov10(" + str(tri.n[2]) + ")),\n") file.write("\tVERTEX_PACK(floattov16(" + str(tri.a.co[0]) + "), floattov16(" + str(tri.a.co[1]) + ")), VERTEX_PACK(floattov16(" + str(tri.a.co[2]) + "), 0),\n") file.write("\tVERTEX_PACK(floattov16(" + str(tri.b.co[0]) + "), floattov16(" + str(tri.b.co[1]) + ")), VERTEX_PACK(floattov16(" + str(tri.b.co[2]) + "), 0),\n") file.write("\tVERTEX_PACK(floattov16(" + str(tri.c.co[0]) + "), floattov16(" + str(tri.c.co[1]) + ")), VERTEX_PACK(floattov16(" + str(tri.c.co[2]) + "), 0),\n") file.write("};\n") file.close() def main(fileName): # Gets the current scene, there can be many scenes in 1 blend file. sce = bpy.data.scenes.active # Get the active object, there can only ever be 1 # and the active object is always the editmode object. ob_act = sce.objects.active if not ob_act or ob_act.type != 'Mesh': BPyMessages.Error_NoMeshActive() return # Saves the editmode state and go's out of # editmode if its enabled, we cant make # changes to the mesh data while in editmode. is_editmode = Window.EditMode() if is_editmode: Window.EditMode(0) Window.WaitCursor(1) me = ob_act.getData(mesh=1) # old NMesh api is default t = sys.time() # Run the mesh editing function saveDisplayList(me.name, createTris(me), fileName) # Restore editmode if it was enabled if is_editmode: Window.EditMode(1) # Timing the script is a good way to be aware on any speed hits when scripting print 'My Script finished in %.2f seconds' % (sys.time()-t) Window.WaitCursor(0) # This lets you can import the script without running it if __name__ == '__main__': Blender.Window.FileSelector(main, "Save File")