8 class Reader(object):
\r
13 "eof", "ios", "lines",
\r
14 "materials", "objects",
\r
16 def __init__(self, ios):
\r
22 return "<MQO %d lines, %d materials, %d objects>" % (
\r
23 self.lines, len(self.materials), len(self.objects))
\r
26 line=self.ios.readline()
\r
33 def printError(self, method, msg):
\r
34 print("%s:%s:%d" % (method, msg, self.lines))
\r
36 def readObject(self, name):
\r
53 if not self.readVertex(obj):
\r
56 if not self.readFace(obj):
\r
59 obj.depth=int(tokens[1])
\r
62 "%s#readObject" % name,
\r
63 "unknown key: %s" % key
\r
66 self.printError("readObject", "invalid eof")
\r
69 def readFace(self, obj):
\r
83 tokens=line.split(b' ', 1)
\r
85 obj.addFace(mqo.Face(int(tokens[0]), tokens[1]))
\r
86 except ValueError as ex:
\r
87 self.printError("readFace", ex)
\r
90 self.printError("readFace", "invalid eof")
\r
93 def readVertex(self, obj):
\r
107 obj.addVertex(*[float(v) for v in line.split()])
\r
109 self.printError("readVertex", "invalid eof")
\r
112 def readMaterial(self):
\r
115 line=self.getline()
\r
127 secondQuaote=line.find(b'"', 1)
\r
128 material=mqo.Material(line[1:secondQuaote])
\r
130 material.parse(line[secondQuaote+2:])
\r
131 except ValueError as ex:
\r
132 self.printError("readMaterial", ex)
\r
134 materials.append(material)
\r
136 self.printError("readMaterial", "invalid eof")
\r
139 def readChunk(self):
\r
142 line=self.getline()
\r
154 elif line.find(b"{")!=-1:
\r
157 self.printError("readChunk", "invalid eof")
\r
161 def read_from_file(path):
\r
163 read from file path, then return the pymeshio.mqo.Model.
\r
169 with io.open(path, 'rb') as ios:
\r
175 read from ios, then return the pymeshio.mqo.Model.
\r
179 input stream (in io.IOBase)
\r
181 assert(isinstance(ios, io.IOBase))
\r
185 line=reader.getline()
\r
186 if line!=b"Metasequoia Document":
\r
187 print("invalid signature")
\r
190 line=reader.getline()
\r
191 if line!=b"Format Text Ver 1.0":
\r
192 print("unknown version: %s" % line)
\r
195 line=reader.getline()
\r
203 tokens=line.split()
\r
207 elif key==b"Scene":
\r
208 if not reader.readChunk():
\r
210 elif key==b"Material":
\r
211 materials=reader.readMaterial()
\r
214 model.materials=materials
\r
215 elif key==b"Object":
\r
216 firstQuote=line.find(b'"')
\r
217 secondQuote=line.find(b'"', firstQuote+1)
\r
218 obj=reader.readObject(line[firstQuote+1:secondQuote])
\r
221 model.objects.append(obj)
\r
222 elif key==b"BackImage":
\r
223 if not reader.readChunk():
\r
225 elif key==b"IncludeXml":
\r
226 firstQuote=line.find(b'"')
\r
227 secondQuote=line.find(b'"', firstQuote+1)
\r
228 print("IncludeXml", line[firstQuote+1:secondQuote])
\r
230 print("unknown key: %s" % key)
\r
231 if not reader.readChunk():
\r
233 # error not reach here
\r
234 raise ParseException("invalid eof")
\r