[COLOR=gray]import struct as S[/COLOR]
[COLOR=gray]dat = open("import.dat", 'rb')[/COLOR]
[COLOR=gray]obj = open("export.obj", 'w')[/COLOR]
[COLOR=gray]#mtl = open("export.mtl", 'w')[/COLOR]
def HexToDec(h):
return float(S.unpack("<h", S.pack("<H", int((h.encode('hex')), 16)))[0])
def vert(v):
return (HexToDec(v)* 0.01).__str__()
def f():
f = int(HexToDec(dat.read(2))+1).__str__()
nor = int(HexToDec(dat.read(2))+1).__str__()
uv = int(HexToDec(dat.read(2))+1).__str__()
#nor = int(HexToDec(dat.read(2))+1).__str__() #flipped face
if (int(f, 10) >= 0):
return f+"/"+uv+"/"+nor
[COLOR=gray]t0 = dat.read(32)#header[/COLOR]
[COLOR=gray]g = 0[/COLOR]
[COLOR=gray]a = 0[/COLOR]
[COLOR=gray]while (a == 0):[/COLOR]
[COLOR=gray] v = "v "+vert(dat.read(2))+" "+vert(dat.read(2))+" "+vert(dat.read(2))[/COLOR]
[COLOR=gray] if (v == 'v 0.0 0.0 0.0'):[/COLOR]
[COLOR=gray] a = 1#main loop[/COLOR]
[COLOR=gray] while (a == 1):[/COLOR]
[COLOR=gray] s = dat.read(6)[/COLOR]
[COLOR=gray] s = dat.read(1).encode('hex')[/COLOR]
[COLOR=gray] if (s == '00'):[/COLOR]
a = 2
s = dat.seek(1,1)
obj.write("\n")
while (a == 2):#UV's
vt = "vt "+vert(dat.read(2))+" "+vert(dat.read(2))#+" "+vert(dat.read(2))
if (vt == 'vt 0.0 0.0'):# 0.0'):
a = 3
[COLOR=gray] while (a == 3):[/COLOR]
[COLOR=gray] s = dat.read(6)[/COLOR]
[COLOR=gray] s = dat.read(1).encode('hex')[/COLOR]
[COLOR=gray] if (s == '00'):[/COLOR]
[COLOR=gray] a = 3.1[/COLOR]
[COLOR=gray] s = dat.seek(1,1)[/COLOR]
[COLOR=gray] obj.write("\n")[/COLOR]
[COLOR=gray] while (a == 3.1):#normals[/COLOR]
[COLOR=gray] vn = "vn "+vert(dat.read(2))+" "+vert(dat.read(2))+" "+vert(dat.read(2))[/COLOR]
[COLOR=gray] if (vn == 'vn 0.0 0.0 0.0'):[/COLOR]
[COLOR=gray] a = 3.5[/COLOR]
[COLOR=gray] obj.write("\n"+'o object'+g.__str__()+"\n")[/COLOR]
[COLOR=gray] while (a == 3.5):[/COLOR]
[COLOR=gray] g = g+1[/COLOR]
[COLOR=gray] # attempt to ensure processing starts at the beginning of a data section[/COLOR]
[COLOR=gray] #h = dat.read(1).encode('hex')+dat.read(1).encode('hex')+dat.read(1).encode('hex')[/COLOR]
[COLOR=gray] #if (h == '0000B8' or h == '0000A8' or h == '0000B0' or h == '000090' or h == '000098' or h == '0000A0' or h == '000080'): #this may also work and find even more matches, need to update backtrack offset if used[/COLOR]
[COLOR=gray] h = dat.read(1).encode('hex')+dat.read(1).encode('hex')+dat.read(1).encode('hex')+dat.read(1).encode('hex')[/COLOR]
[COLOR=gray] if (h == '0000B800' or h == '0000A800' or h == '0000B000' or h == '00009000' or h == '00009800' or h == '0000A000' or h == '00008000'):[/COLOR]
[COLOR=gray] a = 4[/COLOR]
[COLOR=gray] s = dat.seek(-2,1)[/COLOR]
[COLOR=gray] pos = dat.tell()[/COLOR]
[COLOR=gray] #print pos.__str__()+": "+h[/COLOR]
[COLOR=gray] while (a == 4):[/COLOR]
[COLOR=gray] pos = dat.tell()[/COLOR]
[COLOR=gray] h = dat.read(1).encode('hex') #+dat.read(1).encode('hex')[/COLOR]
[COLOR=gray] #print pos.__str__()+": "+h[/COLOR]
[COLOR=gray] if (h == 'B8' or h == 'A8' or h == 'B0' or h == '90' or h == '98' or h == 'A0' or h == '80'):[/COLOR]
[COLOR=gray] #s = dat.seek(-1,1)[/COLOR]
[COLOR=gray] index = HexToDec(dat.read(2))#index count[/COLOR]
[COLOR=gray] if (h == 'B8'): #point[/COLOR]
[COLOR=gray] while (index > 0):[/COLOR]
[COLOR=gray] f0 = f()[/COLOR]
[COLOR=gray] index = index - 1[/COLOR]
[COLOR=gray] print "f "+f0[/COLOR]
[COLOR=gray] obj.write("f "+f0+"\n")[/COLOR]
[COLOR=gray] elif (h == 'A8'): #line[/COLOR]
[COLOR=gray] while (index > 1):[/COLOR]
[COLOR=gray] f0 = f()[/COLOR]
[COLOR=gray] f1 = f()[/COLOR]
[COLOR=gray] index = index - 2[/COLOR]
[COLOR=gray] print "f "+f0+' '+f1[/COLOR]
[COLOR=gray] obj.write("f "+f0+' '+f1+"\n")[/COLOR]
[COLOR=gray] elif (h == 'B0'): #line strip[/COLOR]
[COLOR=gray] while (index > 1):[/COLOR]
[COLOR=gray] f0 = f()[/COLOR]
[COLOR=gray] f1 = f()[/COLOR]
[COLOR=gray] index = index - 1[/COLOR]
[COLOR=gray] if (index > 1):[/COLOR]
[COLOR=gray] seek = dat.seek(-6,1)[/COLOR]
[COLOR=gray] if (f0 != f1):[/COLOR]
[COLOR=gray] print "f "+f0+' '+f1[/COLOR]
[COLOR=gray] obj.write("f "+f0+' '+f1+"\n")[/COLOR]
[COLOR=gray] elif (h == '90'): #triangle[/COLOR]
[COLOR=gray] while (index > 2):[/COLOR]
[COLOR=gray] f0 = f()[/COLOR]
[COLOR=gray] f1 = f()[/COLOR]
[COLOR=gray] f2 = f()[/COLOR]
[COLOR=gray] index = index - 3[/COLOR]
[COLOR=gray] print "f "+f0+' '+f1+' '+f2[/COLOR]
[COLOR=gray] obj.write("f "+f0+' '+f1+' '+f2+"\n")[/COLOR]
[COLOR=gray] elif (h == '98'): #triangle strip[/COLOR]
[COLOR=gray] flip = 1[/COLOR]
[COLOR=gray] while (index > 2):[/COLOR]
[COLOR=gray] f0 = f()[/COLOR]
[COLOR=gray] f1 = f()[/COLOR]
[COLOR=gray] f2 = f()[/COLOR]
[COLOR=gray] index = index - 1[/COLOR]
[COLOR=gray] if (index > 2):[/COLOR]
[COLOR=gray] seek = dat.seek(-12,1)[/COLOR]
[COLOR=gray] if (f0 != f1 and f1 != f2 and f2 != f0):[/COLOR]
[COLOR=gray] if (flip):[/COLOR]
[COLOR=gray] print "f "+f0+' '+f2+' '+f1[/COLOR]
[COLOR=gray] obj.write("f "+f0+' '+f2+' '+f1+"\n")[/COLOR]
[COLOR=gray] else:[/COLOR]
[COLOR=gray] print "f "+f0+' '+f1+' '+f2[/COLOR]
[COLOR=gray] obj.write("f "+f0+' '+f1+' '+f2+"\n")[/COLOR]
[COLOR=gray] flip = 1 - flip[/COLOR]
[COLOR=gray] elif (h == 'A0'): #triangle fan[/COLOR]
[COLOR=gray] f0 = f()[/COLOR]
[COLOR=gray] index = index - 1[/COLOR]
[COLOR=gray] while (index > 1):[/COLOR]
[COLOR=gray] f1 = f()[/COLOR]
[COLOR=gray] f2 = f()[/COLOR]
[COLOR=gray] index = index - 1[/COLOR]
[COLOR=gray] if (index > 1):[/COLOR]
[COLOR=gray] seek = dat.seek(-6,1)[/COLOR]
[COLOR=gray] if (f0 != f1 and f1 != f2 and f2 != f0):[/COLOR]
[COLOR=gray] print "f "+f0+' '+f1+' '+f2[/COLOR]
[COLOR=gray] obj.write("f "+f0+' '+f1+' '+f2+"\n")[/COLOR]
[COLOR=gray] elif (h == '80'): #quad[/COLOR]
[COLOR=gray] while (index > 3):[/COLOR]
[COLOR=gray] f0 = f()[/COLOR]
[COLOR=gray] f1 = f()[/COLOR]
[COLOR=gray] f2 = f()[/COLOR]
[COLOR=gray] f3 = f()[/COLOR]
[COLOR=gray] index = index - 4[/COLOR]
[COLOR=gray] print "f "+f0+' '+f1+' '+f2+' '+f3[/COLOR]
[COLOR=gray] obj.write("f "+f0+' '+f1+' '+f2+' '+f3+"\n")[/COLOR]
[COLOR=gray] else:[/COLOR]
[COLOR=gray] a = 3 # should no longer get here, but just in case...[/COLOR]
[COLOR=gray] else:[/COLOR]
[COLOR=gray] # backtrack over invalid primitive type bytes[/COLOR]
[COLOR=gray] a = 3[/COLOR]
[COLOR=gray] s = dat.seek(-1,1)[/COLOR]
[COLOR=gray] else: [/COLOR]
[COLOR=gray] #backtrack over trailing bytes so that[/COLOR]
[COLOR=gray] #the entire file is scanned a byte at a time[/COLOR]
[COLOR=gray] s = dat.seek(-3,1)[/COLOR]
[COLOR=gray] else:[/COLOR]
[COLOR=gray] print vn[/COLOR]
[COLOR=gray] obj.write(vn+"\n")[/COLOR]
else:
print vt
obj.write(vt+"\n")
[COLOR=gray] else:[/COLOR]
[COLOR=gray] print v[/COLOR]
[COLOR=gray] obj.write(v+"\n")[/COLOR]