Skip to content

Instantly share code, notes, and snippets.

@z0w0
Created March 18, 2011 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z0w0/875738 to your computer and use it in GitHub Desktop.
Save z0w0/875738 to your computer and use it in GitHub Desktop.
A python program for converting a Blockland brick files (.blb) to a 3d wavefront model (.obj). @blockland.us
import sys,decimal
def convert(input,output):
input = open(input,"r")
lines = input.readlines()
input.close()
output = open(output,"w")
linecount = -1
posCount = 0
uvCount = 0
normalCount = 0
currentTex = ""
output.write("#Exported by BLB2OBJ (zack0wack0.com)\nmltlib blockTextures.mtl\n")
for line in lines:
linecount = linecount + 1
if len(line) >= 4:
if line[0:4] == "TEX:" and len(lines)-linecount >= 15:
if currentTex != line[4:] and line[4:] != "PRINT":
currentTex = line[4:]
output.write("usemtl " + currentTex)
if lines[linecount+1][0:9].strip() == "POSITION:":
lines[linecount+2] = lines[linecount+2].strip().split()
lines[linecount+2] = lines[linecount+2][0] + " " + lines[linecount+2][1] + " " + str(decimal.Decimal(lines[linecount+2][2]) / 3)
output.write("v " + lines[linecount+2].strip() + "\n")
lines[linecount+3] = lines[linecount+3].strip().split()
lines[linecount+3] = lines[linecount+3][0] + " " + lines[linecount+3][1] + " " + str(decimal.Decimal(lines[linecount+3][2]) / 3)
output.write("v " + lines[linecount+3].strip() + "\n")
lines[linecount+4] = lines[linecount+4].strip().split()
lines[linecount+4] = lines[linecount+4][0] + " " + lines[linecount+4][1] + " " + str(decimal.Decimal(lines[linecount+4][2]) / 3)
output.write("v " + lines[linecount+4].strip() + "\n")
lines[linecount+5] = lines[linecount+5].strip().split()
lines[linecount+5] = lines[linecount+5][0] + " " + lines[linecount+5][1] + " " + str(decimal.Decimal(lines[linecount+5][2]) / 3)
output.write("v " + lines[linecount+5].strip() + "\n")
posCount = posCount + 4
if lines[linecount+6][0:10].strip() == "UV COORDS:":
output.write("vt " + lines[linecount+7].strip() + "\n")
output.write("vt " + lines[linecount+8].strip() + "\n")
output.write("vt " + lines[linecount+9].strip() + "\n")
output.write("vt " + lines[linecount+10].strip() + "\n")
uvCount = uvCount + 4
if lines[linecount+11][0:8].strip() == "NORMALS:":
output.write("vn " + lines[linecount+12].strip() + "\n")
output.write("vn " + lines[linecount+13].strip() + "\n")
output.write("vn " + lines[linecount+14].strip() + "\n")
output.write("vn " + lines[linecount+15].strip() + "\n")
normalCount = normalCount + 4
output.write("f " + str(posCount - 3) + "/" + str(uvCount - 3) + "/" + str(normalCount - 3) + " " + str(posCount - 2) + "/" + str(uvCount - 2) + "/" + str(normalCount - 2) + " " + str(posCount - 1) + "/" + str(uvCount - 1) + "/" + str(normalCount - 1) + " " + str(posCount) + "/" + str(uvCount) + "/" + str(normalCount) + "\n")
output.close()
if(len(sys.argv) > 1):
convert(sys.argv[1],sys.argv[1] + ".obj")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment