I hope someone who knows Python scripting can fix the errors.
I'd love to see how well this script works. Any and all help would be much appreciated!
Code: Select all
# pepakura3 (.PDO) to metasequoia (.MQO) converter
# by MrPotatoHead using python 2.6
# You must specify the imput file with the full directory
# You must also specify the output directory AND filename (no extension)
# Also remember to change the scale
# I couldn't be bothered to invert the face orientation
# Just select all the faces in your cad program and invert them
import struct
import zlib
import codecs
from array import array
import sys
#saveout = sys.stdout
#outfile = open('C:/output.txt', 'w')
#sys.stdout = outfile
h1=codecs.decode('424d','hex')
h2=codecs.decode('000000003600000028000000','hex')
h3=codecs.decode('010018000000000000000000120B0000120B00000000000000000000','hex')
h4=codecs.decode('0000','hex')
#***** Change the Scale here *****
scale = 1
#*********************************
f = open("C:/testfile.pdo", "rb")
outputname = "testfile"
outputdirectory = "C:/temp/"
metafile = outputdirectory+outputname
of = open(metafile+".mqo", 'w')
of.write("Metasequoia Document\n")
of.write("Format Text Ver 1.0\n")
of.write("\n")
of.write("Scene {\n")
of.write("pos -30.5374 -301.0406 878.7299\n")
of.write("lookat 0.0000 0.0000 0.0000\n")
of.write("head -6.8400\n")
of.write("pich 0.1908\n")
of.write("ortho 0\n")
of.write("zoom2 10.0094\n")
of.write("amb 0.490 0.490 0.490\n")
of.write("}\n")
try:
dump = f.read(148)
print ("dump")
nobj = f.read(4)
numobj = struct.unpack("i", nobj)
kk1 = numobj[0]
print ("number of objects =" ,kk1)
if kk1 > 10000: kk1=1
#loop number of objects
for i in range(0,kk1):
dump = f.read(4)
namelen = struct.unpack("i", dump)
dump = f.read(namelen[0])
dump = f.read(1)
dump = f.read(4)
numvert = struct.unpack("i", dump)
kk2 = numvert[0]
if kk2 > 10000: kk2 = 0
#loop number of vertices
for j in range(0, kk2):
x = f.read(8)
y = f.read(8)
z = f.read(8)
dump = f.read(4)
numfaces = struct.unpack("i", dump)
kk3 = numfaces[0]
if kk3 > 10000: kk3 = 0
#loop number of faces
for j in range(0, kk3):
dump = f.read(40)
nvif = f.read(4)
numvif = struct.unpack("i", nvif)
kk4 = numvif[0]
if kk4 > 10: kk4 = 0
#loop number of verticies in face
for k in range(0, kk4):
vnum = f.read(4)
vnumber = struct.unpack("i", vnum)
dump = f.read(8)
dump = f.read(8)
u1 = f.read(8)
uu = struct.unpack("d", u1)
v1 = f.read(8)
vv = struct.unpack("d", v1)
dump = f.read(49)
dump = f.read(4)
numlines = struct.unpack("i", dump)
kk5 = numlines[0]
print ("number of lines", kk5)
if kk5 > 20000: kk5 = 0
for l in range(0, kk5):
dump = f.read(22)
position = f.tell();
#now get materials
print ("position")
dump = f.read(4)
numtx = struct.unpack("i", dump)
numtex = numtx[0]
tstr1 = ("Material"+str(numtex)+" {\n")
of.write(tstr1)
for m in range(0, numtex):
print ("processing material", m+1, "of", numtex)
tstr1 = "\"mat"+str(m+1)+"\" shader(3) col("
of.write(tstr1)
dump = f.read(4)
nameln = struct.unpack("i", dump)
namelen = nameln[0]
dump = f.read(namelen)
dump = f.read(4)
rrrr = struct.unpack("f", dump)
dump = f.read(4)
gggg = struct.unpack("f", dump)
dump = f.read(4)
bbbb = struct.unpack("f", dump)
dump = f.read(4)
aaaa = struct.unpack("f", dump)
tstr1 = ("%.3f" % rrrr)+" "+("%.3f" % gggg)+" "+("%.3f" % bbbb)+" "+("%.3f" % aaaa)
tstr1 += ") dif(0.800) amb(0.600) emi(0.160) spc(0.000) power(5.00)"
of.write(tstr1)
dump = f.read(64)
dump = f.read(1)
d2 = struct.unpack("B", dump[0])
print ("d2[0]")
if d2[0] == 1:
tstr1 = "text(\""+outputname+str(m)+"tex.bmp\")"
of.write(tstr1)
print ("texture found")
dump = f.read(4)
bmpw = struct.unpack("i", dump)
bmpwidth = bmpw[0]
dump = f.read(4)
bmph = struct.unpack("i", dump)
bmpheight = bmph[0]
dump = f.read(4)
zlibl = struct.unpack("i", dump)
zliblen = zlib[0]
numpixels = bmpwidth*bmpheight
print ("numpixels")
#decompress zlib data
dump = f.read(zliblen)
of.write("\n")
except EOFError:
print ("oops")
finally:
f.seek(0,0);
#just to make sure
f.seek(0,0);
of.write("}\n")
out_str = ""
try:
dump = f.read(148)
nobj - f.read(4)
numobj = struct.unpack("i", nobj)
#print numobj[0]
kk1 = numobj[0]
if kk1 > 10000: kk1 = 1
for i in range(0, kk1):
dump = f.read(4)
namelen = struct.unpack("i", dump)
dump = f.read(namelen[0])
dump = f.read(1)
print ("processing object", i+1)
name = "obj"+str(i+1)
of.write("Object \""+name+"\" {\n")
of.write(" depth 0\n")
of.write(" folding 0\n")
of.write(" scale 1.000000 1.000000 1.000000\n")
of.write(" rotation 0.000000 0.000000 0.000000\n")
of.write(" translation 0.000000 0.000000 0.000000\n")
of.write(" visible 15\n")
of.write(" locking 0\n")
of.write(" shading 1\n")
of.write(" facet 59.5\n")
of.write(" color 0.000 0.000 0.000\n")
of.write(" color_type 0\n")
of.write(" verex ")
dump = f.read(4)
numvert = struct.unpack("i", dump)
of.write(str(numvert[0])),
of.write("{\n")
kk2 = numvert[0]
if kk2 > 10000: kk2 = 0
for j in range(0, kk2):
x = f.read(8)
y = f.read(8)
z = f.read(8)
outx = struct.unpack("d",x)
outy = struct.unpack("d",y)
outz = struct.unpack("d",z)
ox = outx[0] * scale
oy = outy[0] * scale
oz = outz[0] * scale
tmpstr = "\t"+("%.4f" % ox)+" "+("%.4f" % oy)+" "+("%.4f" % oz)
of.write(tmpstr+"\n")
of.write(" }\n")
dump = f.read(4)
numfaces = struct.unpack("i", dump)
tmpstr = "\t face"+" "+str(numfaces[0])+" {\n"
of.write(tmpstr)
kk3 = numfaces[0]
if kk3 > 10000: kk3 = 0
for j in range(0, kk3):
mtid = f.read(4)
matid = struct.unpack("i", mtid)
matid2 = matid[0]
matstr = ""
if matid2 != -1:
matstr = "M("+str(matid2)+")"
dump = f.read(36)
nvif = f.read(4)
numvif = struct.unpack("i", nvif)
tmpstr = str(numvif[0])+" V("
of.write(tmpstr)
kk4 = numvif[0]
if kk4 > 10: kk4 = 0
#redo this to reverse the faces: either 3 or 4
for k in range(0, kk4):
vnum = f.read(4)
vnumber = struct.unpack("i", vnum)
of.write(str(vnumber[0])+" "),
dump = f.read(8)
dump = f.read(8)
u1 = f.read(8)
uu = struct.unpack("d", u1)
out_str += ("%.5f" % uu[0])
out_str += " "
v1 = f.read(8)
vv = struct.unpack("d", v1)
out_str += ("%.5f" % vv[0])
out_str += " "
dump = f.read(49)
of.write(")"+matstr+" UV( "+out_str+" )\n")
out_str = ""
of.write("}\n")
of.write("}\n")
dump = f.read(4)
numlines = struct.unpack("i", dump)
kk5 = numlines[0]
if kk5 > 20000: kk5 = 0
for l in range(0, kk5):
dump = f.read(22)
position = f.tell();
of.write("Eof\n")
of.close()
#read number of materials
dump = f.read(4)
numtx = struct.unpack("i", dump)
numtex = numtx[0]
#for k = 1 to numtex do
for m in range(0, numtex):
print ("processing materials",m)
dump = f.read(4)
nameln = struct.unpack("i", dump)
namelen = nameln[0]
dump = f.read(nameln)
dump = f.read(80)
dump = f.read(1)
d2 = struct.unpack("B", dump[0])
print ("d2[0]")
if d2[0] == 1:
print ("doing bitmap")
dump = f.read(4)
bmpw = struct.unpack("i", dump)
bmpwidth = bmpw[0]
dump = f.read(4)
bmph = struct.unpack("i", dump)
bmpheight = bmph[0]
dump = f.read(4)
zlibl = struct.unpack("i", dump)
zliblen = zlib[0]
numpixels = bmpwidth*bmpheight
print ("numpixels")
#decompress zlib data
dump = f.read(zliblen)
out = zlib.decompress(dump)
out = out[::-1]
out2 = ""
for n in range(0, bmpheight):
pstart = int(n*bmpwidth*3)
pend = int((pstart)+(bmpwidth*3))
tout = out[pstart:pend]
outk = ""
for knn in range(0, bmpwidth):
myoutk = ""
kstart = (knn*3)
kend = (knn*3)+3
myoutk = tout[kstart:kend]
outk += myoutk[::-1]
out2 += outk[::-1]
print ("n")
#write bitmap image to file
of2name = metafile+str(m)+"tex.bmp"
of2 = open(of2name, 'wb')
of2.write(h1)
bmpfilesize = "%08x" % ((bmpwidth*bmpheight*3)+56)
print ("bmpfilesize, bmpwidth, bmpheight")
bmpfs = codecs.decode(bmpfilesize,'hex')
bmpfs = bmpfs[::-1]
of2.write(bmpfs)
of2.write(h2)
bmpws = "%08x" % (bmpwidth)
bmpws2 = codecs.decode(bmpws,'hex')
bmpws2 = bmpws2[::-1]
of2.write(str(bmpws2))
bmphs = "%08x" % (bmpheight)
bmphs2 = codecs.decode(bmphs,'hex')
bmphs2 = bmphs2[::-1]
of2.write(str(bmphs2))
of2.write(h3)
of2.write(out2)
of2.write(h4)
of2.close()
except EOFError:
print ("oops")
finally:
f.close()
print ("finished")