################################################################################# ## Ble2BL3 ## Blender 2 Black Legacy Python's scritp converter ## http://Black-Legacy.com Necro_@Black-Legacy.com ## GNU(GPL) ## ---->You have the freedom to run the program, for any purpose. ## ---->You have the freedom to modify the program to suit your needs. ## (To make this freedom effective in practice, you must have access to the source code, ## since making changes in a program without having the source code is exceedingly difficult.) ## ---->You have the freedom to redistribute copies, either gratis or for a fee. ## ---->You have the freedom to distribute modified versions of the program, ## so that the community can benefit from your improvements. ################################################################################## import re import string import sys import math import Blender from Blender import Draw, BGL mystring = "" mymsg = "" toggle = 0 option=Draw.Create(1) def rename_materials(): mat=Blender.Material.Get() newmaterialname=0 for materiales in mat: newname= `newmaterialname` materiales.setName(newname) newmaterialname=newmaterialname+1 def load_bl3(filename): file = open( filename, "r" ) # bl_Scene=file.read().split("\n") # bl_Scene=file.readline() ele_N=re.compile('\n') nObjetos=string.atoi(file.readline()) nMateriales=string.atoi(file.readline()) General=list() Materiales=list() General.append(nObjetos)#0 n objetos Materiales.append(nMateriales)#0 n materiales for countob in range(General[0]): Objetct=list() ObjectNames=list() ObjectMatID=list() file.readline()#LEO EL ID DEL OPBJETO Y NO LO GUARDO ObjectNames.append(ele_N.sub('',file.readline())) ObjectMatID.append(ele_N.sub('',file.readline())) Objetct.append(ObjectNames)#0 nombre Objetct.append(ObjectMatID)#1 nombre Material n_faces=string.atoi(file.readline()) getFaces=list() getVertex=list() getUV=list() for facess in range(n_faces): getFaces.append(string.atoi(file.readline())) getFaces.append(string.atoi(file.readline())) getFaces.append(string.atoi(file.readline())) getFaces.append(ele_N.sub('',file.readline())) Objetct.append(getFaces)#2 Caras n_vertices=string.atoi(file.readline()) for vertices in range(n_vertices): getVertex.append(string.atof(file.readline())) getVertex.append(string.atof(file.readline())) getVertex.append(string.atof(file.readline())) Objetct.append(getVertex) #3 vertices n_uv=string.atoi(file.readline()) for uv in range(n_uv): getUV.append(string.atof(file.readline())) getUV.append(string.atof(file.readline())) Objetct.append(getUV)#4 UV normal=string.atoi(file.readline()) for norls in range(normal): file.readline() file.readline() file.readline() General.append(Objetct) for matws in range(Materiales[0]): matsss=list() mats=list() mats.append(ele_N.sub('',file.readline())) difuse=list() difuse.append(string.atof(file.readline())) difuse.append(string.atof(file.readline())) difuse.append(string.atof(file.readline())) difuse2=list() difuse2.append(string.atof(file.readline())) difuse2.append(string.atof(file.readline())) difuse2.append(string.atof(file.readline())) difuse3=list() difuse3.append(string.atof(file.readline())) matsss.append(mats) matsss.append(difuse) matsss.append(difuse2) matsss.append(difuse3) Materiales.append(matsss) file.close() for mats in range (Materiales[0]): matss=Materiales[mats+1] name=matss[0] RGB=matss[1] R=RGB[0] G=RGB[1] B=RGB[2] print name mat=Blender.Material.New(name[0]) mat.setRGBCol(R,G,B) RGB=matss[2] R=RGB[0] G=RGB[1] B=RGB[2] mat.setSpecCol(R,G,B) RGB=matss[3] mat.setSpec(RGB[0]) for objeto in range (General[0]): poly=Blender.NMesh.GetRaw() objeto_ac=General[objeto+1] for vertex in range (0,len(objeto_ac[3]),3): vdata=objeto_ac[3] v=Blender.NMesh.Vert(vdata[vertex],vdata[vertex+1],vdata[vertex+2]) poly.verts.append(v) vua=list() for vertex in range (0,len(objeto_ac[4]),2): vdata=objeto_ac[4] vua1=list() print vdata[vertex] vua1.append(vdata[vertex]) vua1.append(vdata[vertex+1]) vua.append(vua1) faces2=0 for faces in range (0,len(objeto_ac[2]),4): print faces2 fdata=objeto_ac[2] f=Blender.NMesh.Face() f.v.append(poly.verts[faces2]) f.v.append(poly.verts[faces2+1]) f.v.append(poly.verts[faces2+2]) uv1=vua[faces2] uv2=vua[faces2+1] uv3=vua[faces2+2] f.uv.append((uv1[0],uv1[1])) f.uv.append((uv2[0],uv2[1])) f.uv.append((uv3[0],uv3[1])) f.image=Blender.Image.Load(fdata[faces+3]) poly.faces.append(f) faces2=faces2+3 name= objeto_ac[0] Blender.NMesh.PutRaw(poly,name[0],1) rename=Blender.Object.Get() fdata=objeto_ac[1] matt=Blender.Material.Get(fdata[0]) rename[objeto].setName(name[0]) mart=list() mart.append(matt) rename[objeto].setMaterials(mart) Blender.Redraw() def save_bl3(filename): rename_materials() file = open( filename, "wb" ) Objetos = Blender.Object.Get() cuantos_objetos=len(Objetos) print "Numero de Objetos %i" % cuantos_objetos file.write("%i\n" % len(Objetos))##### Numero de Objetos print "Numero de MAteriales",len(Blender.Material.Get() ) file.write("%i\n" % len(Blender.Material.Get() ))######## Numero de Materiales #objeto_seleccionado=Blender.Object.GetSelected() #print "objeto selecionado",objeto_seleccionado contador_de_objetos=0 for objetos in Objetos: name= objetos.getName() print "ID objeto",contador_de_objetos file.write("%i\n" % contador_de_objetos)##### ID Objeto contador_de_objetos=contador_de_objetos+1 print "Nombre Objeto",name file.write("%s\n" % name) ##### Nombre Objeto material_id= objetos.getMaterials() if len(material_id)>0: print "Material ID",material_id[0].name file.write("%s\n" % material_id[0].name)##### ID Material else: print "Material ID NONE" file.write("none\n" )##### ID Material Default #############MESH############################# if objetos.getType()=="Mesh": Nmesh = Blender.NMesh.GetRawFromObject(name) Meshes =Nmesh.faces print "nvert",len(Nmesh.verts) print "Numero de Caras",len(Meshes) file.write("%i\n" % len(Meshes)) #### Numero de Caras sumador=0 for Caras_Indices in Meshes: Indicesa= Caras_Indices.v[0].index Indicesb= Caras_Indices.v[1].index Indicesc= Caras_Indices.v[2].index file.write("%i\n" % sumador) ####Indice A file.write("%i\n" % (sumador+1)) ####Indice B file.write("%i\n" % (sumador+2)) ####Indice C sumador=sumador+3 try: file.write("%s\n" % Caras_Indices.image.getName()) ####Textura de la Cara except: file.write("none\n") ####Textura de la Cara print "Numero de Vertices",len(Meshes)*3 file.write("%i\n" % (len(Meshes)*3)) #### Numero de Vertice for Caras_Indices in Meshes: Indicesa= Caras_Indices.v[0].co rotaboundingX=objetos.RotY*(-1) rotaboundingY=objetos.RotZ rotaboundingZ=objetos.RotX*(-1) Indicesax=Indicesa.x*objetos.SizeX Indicesay=Indicesa.y*objetos.SizeY Indicesaz=Indicesa.z*objetos.SizeZ # Indicesax=(-1)*Indicesax xa=(math.cos(rotaboundingX)*Indicesax)-(math.sin(rotaboundingX)*Indicesaz) za=(math.sin(rotaboundingX)*Indicesax)+(math.cos(rotaboundingX)*Indicesaz) x=(math.cos(rotaboundingY)*xa)-(math.sin(rotaboundingY)*Indicesay) ya=(math.sin(rotaboundingY)*xa)+(math.cos(rotaboundingY)*Indicesay) z=(math.cos(rotaboundingZ)*za)-(math.sin(rotaboundingZ)*ya) y=(math.sin(rotaboundingZ)*za)+(math.cos(rotaboundingZ)*ya) alfa=(x*objetos.SizeX)+objetos.LocX beta=(y*objetos.SizeY)+objetos.LocY zeta=(z*objetos.SizeZ)+objetos.LocZ alfa=(x)+objetos.LocX beta=(y)+objetos.LocY zeta=(z)+objetos.LocZ ######### Vertices x,y,z ##########3 file.write("%.2f\n" % alfa) ####Vertice X file.write("%.2f\n" % beta) ####Vertice Y file.write("%.2f\n" % zeta) ####Vertice Z Indicesa= Caras_Indices.v[1].co rotaboundingX=objetos.RotY*(-1) rotaboundingY=objetos.RotZ rotaboundingZ=objetos.RotX*(-1) Indicesax=Indicesa.x*objetos.SizeX Indicesay=Indicesa.y*objetos.SizeY Indicesaz=Indicesa.z*objetos.SizeZ # Indicesax=(-1)*Indicesax xa=(math.cos(rotaboundingX)*Indicesax)-(math.sin(rotaboundingX)*Indicesaz) za=(math.sin(rotaboundingX)*Indicesax)+(math.cos(rotaboundingX)*Indicesaz) x=(math.cos(rotaboundingY)*xa)-(math.sin(rotaboundingY)*Indicesay) ya=(math.sin(rotaboundingY)*xa)+(math.cos(rotaboundingY)*Indicesay) z=(math.cos(rotaboundingZ)*za)-(math.sin(rotaboundingZ)*ya) y=(math.sin(rotaboundingZ)*za)+(math.cos(rotaboundingZ)*ya) alfa=(x*objetos.SizeX)+objetos.LocX beta=(y*objetos.SizeY)+objetos.LocY zeta=(z*objetos.SizeZ)+objetos.LocZ alfa=(x)+objetos.LocX beta=(y)+objetos.LocY zeta=(z)+objetos.LocZ ######### Vertices x,y,z ##########3 file.write("%.2f\n" % alfa) ####Vertice X file.write("%.2f\n" % beta) ####Vertice Y file.write("%.2f\n" % zeta) ####Vertice Z Indicesa= Caras_Indices.v[2].co rotaboundingX=objetos.RotY*(-1) rotaboundingY=objetos.RotZ rotaboundingZ=objetos.RotX*(-1) Indicesax=Indicesa.x*objetos.SizeX Indicesay=Indicesa.y*objetos.SizeY Indicesaz=Indicesa.z*objetos.SizeZ # Indicesax=(-1)*Indicesax xa=(math.cos(rotaboundingX)*Indicesax)-(math.sin(rotaboundingX)*Indicesaz) za=(math.sin(rotaboundingX)*Indicesax)+(math.cos(rotaboundingX)*Indicesaz) x=(math.cos(rotaboundingY)*xa)-(math.sin(rotaboundingY)*Indicesay) ya=(math.sin(rotaboundingY)*xa)+(math.cos(rotaboundingY)*Indicesay) z=(math.cos(rotaboundingZ)*za)-(math.sin(rotaboundingZ)*ya) y=(math.sin(rotaboundingZ)*za)+(math.cos(rotaboundingZ)*ya) alfa=(x*objetos.SizeX)+objetos.LocX beta=(y*objetos.SizeY)+objetos.LocY zeta=(z*objetos.SizeZ)+objetos.LocZ alfa=(x)+objetos.LocX beta=(y)+objetos.LocY zeta=(z)+objetos.LocZ ######### Vertices x,y,z ##########3 file.write("%.2f\n" % alfa) ####Vertice X file.write("%.2f\n" % beta) ####Vertice Y file.write("%.2f\n" % zeta) ####Vertice Z print "Numero de UV",len(Meshes)*3 file.write("%i\n" % (len(Meshes)*3)) #### Numero de UV for Caras_Indices in Meshes: try: IndiceUV= Caras_Indices.uv Indicesa= IndiceUV[0] Indicesb= IndiceUV[1] Indicesc= IndiceUV[2] ############ UV ############## file.write("%.2f\n" % Indicesa[0]) ####UV U file.write("%.2f\n" % Indicesa[1]) ####UV V file.write("%.2f\n" % Indicesb[0]) ####UV U file.write("%.2f\n" % Indicesb[1]) ####UV V file.write("%.2f\n" % Indicesc[0]) ####UV U file.write("%.2f\n" % Indicesc[1]) ####UV V except: file.write("0\n") ####UV U file.write("0\n") ####UV V file.write("0\n") ####UV U file.write("0\n") ####UV V file.write("0\n") ####UV U file.write("0\n") ####UV V print "Numero de Normales",len(Meshes)*3 file.write("%i\n" % (len(Meshes)*3)) #### Numero de Normales for Caras_Indices in Meshes: Indicesa= Caras_Indices.v[0].no Indicesb= Caras_Indices.v[1].no Indicesc= Caras_Indices.v[2].no ######### Normales x,y,z ##########3 file.write("%.2f\n" % Indicesa.x) ####Normales X file.write("%.2f\n" % Indicesa.y) ####Normales Y file.write("%.2f\n" % Indicesa.z) ####Normales Z file.write("%.2f\n" % Indicesb.x) ####Normales X file.write("%.2f\n" % Indicesb.y) ####Normales Y file.write("%.2f\n" % Indicesb.z) ####Normales Z file.write("%.2f\n" % Indicesc.x) ####Normales X file.write("%.2f\n" % Indicesc.y) ####Normales Y file.write("%.2f\n" % Indicesc.z) ####Normales Z #####################MATERIALES################### mat=objetos.getMaterials() mat=Blender.Material.Get() for materiales in mat: print "nombre_ID",materiales.getName() file.write("%s\n" % materiales.getName()) ####ID Material print "Difuso ok" Difuso= materiales.getRGBCol() file.write("%.4f\n" % Difuso[0]) ####Color Difuso R file.write("%.4f\n" % Difuso[1]) ####Color Difuso G file.write("%.4f\n" % Difuso[2]) ####Color Difuso B Specular=materiales.getSpecCol() print "Specular ok" file.write("%.4f\n" % Specular[0]) ####Color Especular R file.write("%.4f\n" % Specular[1]) ####Color Especular G file.write("%.4f\n" % Specular[2]) ####Color Especular B file.write("%.4f\n" % materiales.getSpec() ) ####Factor Shinishe file.close() def my_callback(filename): if(filename.find('.bl3', -4) <= 0): filename += '.bl3' save_bl3(filename) def my_callback_load(filename): if(filename.find('.bl3', -4) <= 0): filename += '.bl3' load_bl3(filename) #Blender.Window.FileSelector(my_callback, "Export BL3") def event(evt, val): # the function to handle input events global mystring, mymsg if not val: # val = 0: it's a key/mbutton release if evt in [Draw.LEFTMOUSE, Draw.MIDDLEMOUSE, Draw.RIGHTMOUSE]: mymsg = "You released a mouse button." Draw.Redraw(1) return if evt == Draw.ESCKEY: Draw.Exit() # exit when user presses ESC return elif Draw.AKEY <= evt <= Draw.ZKEY: mystring += chr(evt) elif evt == Draw.SPACEKEY: mystring += ' ' elif evt == Draw.BACKSPACEKEY and len(mystring): mystring = mystring[:-1] else: return # no need to redraw if nothing changed Draw.Redraw(1) def button_event(evt): # the function to handle Draw Button events global mymsg, toggle if evt == 1: mymsg = "You pressed the toggle button." toggle = 1 - toggle Draw.Redraw(1) if option.val == 0: Blender.Window.FileSelector(my_callback, "Export BL3") Draw.Redraw(1) if option.val == 1: Blender.Window.FileSelector(my_callback_load, "Import BL3") Draw.Redraw(1) def gui(): # the function to draw the screen global mystring, mymsg, toggle,option BGL.glClearColor(0.5,0.5,0.5,1) BGL.glClear(BGL.GL_COLOR_BUFFER_BIT) BGL.glColor3f(1,1,1) # Draw.Toggle("Toggle", 1, 10, 10, 55, 20, toggle,"A toggle button") BGL.glRasterPos2i(00, 85) Draw.Text("Blender to Black Legacy Import/Export V 0.9.0", "small") BGL.glRasterPos2i(50, 70) Draw.Text("Http://Black-Legacy.com", "small") BGL.glRasterPos2i(45, 55) Draw.Text("Necro_@Black-Legacy.com", "small") BGL.glRasterPos2i(85, 40) Draw.Text("GNU(GPL)", "small") BGL.glRasterPos2i(80, 25) Draw.Text("2004-04-11", "small") BGL.glRasterPos2i(10, 230) #Draw.Button("Export 3BL",2,10,40,75,20,"Export Blender Model to Black Legacy"); #Draw.Button("Import 3BL",2,10,10,75,20,"Import Black Legacy Model to Blender"); option=Draw.Menu("File Options %t|Export %x0|Import %x1",2,65,100,75,20,0,"File Options"); Draw.Register(gui, event, button_event) # registering the 3 callbacks