#!BPY # """ # Name: 'Scene Graphic (.3dm)...' # Blender: 236 # Group: 'Export' # Tooltip: 'Export an object to the 3DM SceneGraphic file format.' # """ __author__ = "Charles INGELS" __version__ = "V0.0.3 17-06-2005" __url__ = "http://scene-graphic.maisonblv.net" __email__ = "zedraken@maisonblv.net" import sys import os import Blender from Blender import Window, Scene, NMesh, Material, Texture, Draw, BGL normal_vector_toggle = 1 textures_toggle = 1 double_sided_toggle = 1 info_msg = "" def point_by_matrix(p, m): return [p[0] * m[0][0] + p[1] * m[1][0] + p[2] * m[2][0] + m[3][0], p[0] * m[0][1] + p[1] * m[1][1] + p[2] * m[2][1] + m[3][1], p[0] * m[0][2] + p[1] * m[1][2] + p[2] * m[2][2] + m[3][2]] def WindowSelectFunc (filename): print filename export_file = filename if filename: try: file = open (filename, "w") except: print "***ERROR***\tUnable to create file" . export_file sys.exit(1) file.write ("# --\n") file.write ("# This file has been generated by 'export3dm.py' export script under Blender.\n# The 'export3dm' script has been developped by Charles INGELS \n# and is distributed under the General Public License (GPL).\n# The purpose of this script is to export any object created by the\n# marvellous modeller Blender to the 3DM file format,\n# which is the native format of the Scene Graphic library.\n") file.write ("# --\n") file.write ("# Home dir : " + Blender.Get ('homedir') + "\n") file.write ("# Scripts dir : " + Blender.Get ('scriptsdir') + "\n") file.write ("# --\n") scene = Blender.Scene.GetCurrent() objects = scene.getChildren() file.write ("# Number of objects: " + str(len(objects)) + "\n") if scene.getName(): file.write ("# Exported scene : " + scene.getName() + "\n") file.write ("# --\n") # # The object counter. # nobject = 0 # # Loop through all objects in the scene. # for obj in objects: # # Only objects of type 'Mesh' are exported. # if obj.getType() == 'Mesh': # # Begin a new object. # name = obj.getName() if name == "": name = 'NoName%d' % nobject nobject = nobject + 1 buf = 'OBJECT "%s"\n' % name file.write (buf) # # Get mesh contained in the current object. # mesh = obj.getData() # # We get the transformation matrix which will be applied to each # vertex of the mesh. # matrix = obj.getMatrix() # # By default, the 'previous' face has no texture. # prev_texture = "" # # Loop through the faces to count the number of textures used. # This is for information only and that stage can be bypassed. # if textures_toggle == 1: tex = [] for f in mesh.faces: if f.image: if f.image.name not in tex: tex.append (f.image.name) # # The face counter. # nface = 0 # # Loop through all faces of the current mesh. # for face in mesh.faces: Window.DrawProgressBar (nface / len(mesh.faces), "Exporting...") # # Check if the current face has a texture. # if textures_toggle == 1: if face.image: if prev_texture != face.image.filename: buf = 'TEXTURE "%s"\n' % (Blender.sys.basename(face.image.filename)) file.write (buf) prev_texture = face.image.filename else: # # If the current face has no texture but the previous face has one, # then, the 'NO_TEXTURE' keyword is output. # if prev_texture != "": file.write ("NO_TEXTURE\n") prev_texture = "" # # The face must have at least 3 vertices. # if len(face.v) >= 3: file.write ("FACE" + str(nface) + "\n") if face.normal and normal_vector_toggle == 1: buf = "NORMAL %2.3f %2.3f %2.3f\n" % (face.normal[0], face.normal[1], face.normal[2]) file.write (buf) if textures_toggle == 1: if len(face.uv) > 0: buf = "TEXCOORD %2.3f %2.3f\n" % (face.uv[0][0], face.uv[0][1]) file.write (buf) coord = point_by_matrix (face.v[0].co, matrix) buf = "VERTEX %2.3f %2.3f %2.3f\n" % (coord[0], coord[1], coord[2]) file.write (buf) if textures_toggle == 1: if len(face.uv) > 0: buf = "TEXCOORD %2.3f %2.3f\n" % (face.uv[1][0], face.uv[1][1]) file.write (buf) coord = point_by_matrix (face.v[1].co, matrix) buf = "VERTEX %2.3f %2.3f %2.3f\n" % (coord[0], coord[1], coord[2]) file.write (buf) if textures_toggle == 1: if len(face.uv) > 0: buf = "TEXCOORD %2.3f %2.3f\n" % (face.uv[2][0], face.uv[2][1]) file.write (buf) coord = point_by_matrix (face.v[2].co, matrix) buf = "VERTEX %2.3f %2.3f %2.3f\n" % (coord[0], coord[1], coord[2]) file.write (buf) file.write ("END_FACE\n"); if double_sided_toggle == 1: # # Go to the next face. # nface = nface + 1; file.write ("FACE" + str(nface) + "\n") if face.normal and normal_vector_toggle == 1: buf = "NORMAL %2.3f %2.3f %2.3f\n" % (face.normal[0], face.normal[1], face.normal[2]) file.write (buf) if textures_toggle == 1: if len(face.uv): buf = "TEXCOORD %2.3f %2.3f\n" % (face.uv[0][0], face.uv[0][1]) file.write (buf) coord = point_by_matrix (face.v[0].co, matrix) buf = "VERTEX %2.3f %2.3f %2.3f\n" % (coord[0], coord[1], coord[2]) file.write (buf) if textures_toggle == 1: if len(face.uv): buf = "TEXCOORD %2.3f %2.3f\n" % (face.uv[1][0], face.uv[1][1]) file.write (buf) coord = point_by_matrix (face.v[2].co, matrix) buf = "VERTEX %2.3f %2.3f %2.3f\n" % (coord[0], coord[1], coord[2]) file.write (buf) if textures_toggle == 1: if len(face.uv): buf = "TEXCOORD %2.3f %2.3f\n" % (face.uv[2][0], face.uv[2][1]) file.write (buf) coord = point_by_matrix (face.v[1].co, matrix) buf = "VERTEX %2.3f %2.3f %2.3f\n" % (coord[0], coord[1], coord[2]) file.write (buf) file.write ("END_FACE\n"); # # Go to the next face. # nface = nface + 1; # # If the current face is a quad, then it is splitted into two triangles. # if len(face.v) >= 4: file.write ("FACE" + str(nface) + "\n") if face.normal and normal_vector_toggle == 1: buf = "NORMAL %2.3f %2.3f %2.3f\n" % (face.normal[0], face.normal[1], face.normal[2]) file.write (buf) if textures_toggle == 1: if len(face.uv): buf = "TEXCOORD %2.3f %2.3f\n" % (face.uv[0][0], face.uv[0][1]) file.write (buf) coord = point_by_matrix (face.v[0].co, matrix) buf = "VERTEX %2.3f %2.3f %2.3f\n" % (coord[0], coord[1], coord[2]) file.write (buf) if textures_toggle == 1: if len(face.uv): buf = "TEXCOORD %2.3f %2.3f\n" % (face.uv[2][0], face.uv[2][1]) file.write (buf) coord = point_by_matrix (face.v[2].co, matrix) buf = "VERTEX %2.3f %2.3f %2.3f\n" % (coord[0], coord[1], coord[2]) file.write (buf) if textures_toggle == 1: if len(face.uv): buf = "TEXCOORD %2.3f %2.3f\n" % (face.uv[3][0], face.uv[3][1]) file.write (buf) coord = point_by_matrix (face.v[3].co, matrix) buf = "VERTEX %2.3f %2.3f %2.3f\n" % (coord[0], coord[1], coord[2]) file.write (buf) file.write ("END_FACE\n"); if double_sided_toggle == 1: # # Go to the next face. # nface = nface + 1 file.write ("FACE" + str(nface) + "\n") if face.normal and normal_vector_toggle == 1: buf = "NORMAL %2.3f %2.3f %2.3f\n" % (face.normal[0], face.normal[1], face.normal[2]) file.write (buf) if textures_toggle == 1: if len(face.uv): buf = "TEXCOORD %2.3f %2.3f\n" % (face.uv[0][0], face.uv[0][1]) file.write (buf) coord = point_by_matrix (face.v[0].co, matrix) buf = "VERTEX %2.3f %2.3f %2.3f\n" % (coord[0], coord[1], coord[2]) file.write (buf) if textures_toggle == 1: if len(face.uv): buf = "TEXCOORD %2.3f %2.3f\n" % (face.uv[3][0], face.uv[3][1]) file.write (buf) coord = point_by_matrix (face.v[3].co, matrix) buf = "VERTEX %2.3f %2.3f %2.3f\n" % (coord[0], coord[1], coord[2]) file.write (buf) if textures_toggle == 1: if len(face.uv): buf = "TEXCOORD %2.3f %2.3f\n" % (face.uv[2][0], face.uv[2][1]) file.write (buf) coord = point_by_matrix (face.v[2].co, matrix) buf = "VERTEX %2.3f %2.3f %2.3f\n" % (coord[0], coord[1], coord[2]) file.write (buf) file.write ("END_FACE\n"); # # Go to the next face. # nface = nface + 1 file.write ("END_OBJECT\n\n# --------------------------------------------------------------------------------\n\n") file.close () else: print "***ERROR***\t'export_file' undefined\n" def handler_event (event, value): if event == Draw.ESCKEY: Draw.Exit() return def button_event (event): global normal_vector_toggle, textures_toggle, double_sided_toggle, info_msg if event == 1: normal_vector_toggle = 1 - normal_vector_toggle Draw.Redraw(1) if event == 2: textures_toggle = 1 - textures_toggle Draw.Redraw(2) if event == 3: double_sided_toggle = 1 - double_sided_toggle Draw.Redraw(3) if event == 4: Draw.Exit() return if event == 5: ARG = 'all' fname = Blender.sys.makename(ext=".3dm") Blender.Window.FileSelector (WindowSelectFunc, "Scene Graphic (.3dm)", fname) Draw.Exit() return def gui(): global info_msg BGL.glClearColor (0,0,1,1) BGL.glClear (BGL.GL_COLOR_BUFFER_BIT) BGL.glColor3f (1,1,0) BGL.glRasterPos2i (10, 200) Draw.Text ("Scene Graphic export script") Draw.Toggle ("Normal vectors", 1, 10,10,110,20, normal_vector_toggle, "Export normal vectors") Draw.Toggle ("Textures", 2, 10,30,110,20, textures_toggle, "Export textures") Draw.Toggle ("Double sided", 3, 10,50,110,20, double_sided_toggle, "Export front and back faces") Draw.PushButton ("Exit", 4, 150,10,110,30, "Cancel the execution of the script") Draw.PushButton ("Go", 5, 150,40,110,30, "Run the script") BGL.glRasterPos2i (10, 100) Draw.Text (info_msg) Draw.Register (gui, handler_event, button_event) # # End of export script. #