Package pymeshio :: Package mqo :: Module reader
[hide private]
[frames] | no frames]

Source Code for Module pymeshio.mqo.reader

  1  # coding: utf-8
 
  2  """
 
  3  mqo reader
 
  4  """ 
  5  import io 
  6  from .. import mqo 
  7  
 
8 -class Reader(object):
9 """mqo reader 10 """ 11 __slots__=[ 12 "has_mikoto", 13 "eof", "ios", "lines", 14 "materials", "objects", 15 ]
16 - def __init__(self, ios):
17 self.ios=ios 18 self.eof=False 19 self.lines=0
20
21 - def __str__(self):
22 return "<MQO %d lines, %d materials, %d objects>" % ( 23 self.lines, len(self.materials), len(self.objects))
24
25 - def getline(self):
26 line=self.ios.readline() 27 self.lines+=1 28 if line==b"": 29 self.eof=True 30 return None 31 return line.strip()
32
33 - def printError(self, method, msg):
34 print("%s:%s:%d" % (method, msg, self.lines))
35
36 - def readObject(self, name):
37 obj=mqo.Obj(name) 38 while(True): 39 line=self.getline() 40 if line==None: 41 # eof 42 break; 43 if line==b"": 44 # empty line 45 continue 46 47 if line==b"}": 48 return obj 49 else: 50 tokens=line.split() 51 key=tokens[0] 52 if key==b"vertex": 53 if not self.readVertex(obj): 54 return False 55 elif key==b"face": 56 if not self.readFace(obj): 57 return False 58 elif key==b"depth": 59 obj.depth=int(tokens[1]) 60 else: 61 print( 62 "%s#readObject" % name, 63 "unknown key: %s" % key 64 ) 65 66 self.printError("readObject", "invalid eof") 67 return False
68
69 - def readFace(self, obj):
70 while(True): 71 line=self.getline() 72 if line==None: 73 # eof 74 break; 75 if line==b"": 76 # empty line 77 continue 78 79 if line==b"}": 80 return True 81 else: 82 # face 83 tokens=line.split(b' ', 1) 84 try: 85 obj.addFace(mqo.Face(int(tokens[0]), tokens[1])) 86 except ValueError as ex: 87 self.printError("readFace", ex) 88 #return False 89 90 self.printError("readFace", "invalid eof") 91 return False
92
93 - def readVertex(self, obj):
94 while(True): 95 line=self.getline() 96 if line==None: 97 # eof 98 break; 99 if line==b"": 100 # empty line 101 continue 102 103 if line==b"}": 104 return True 105 else: 106 # vertex 107 obj.addVertex(*[float(v) for v in line.split()]) 108 109 self.printError("readVertex", "invalid eof") 110 return False
111
112 - def readMaterial(self):
113 materials=[] 114 while(True): 115 line=self.getline() 116 if line==None: 117 # eof 118 break; 119 if line==b"": 120 # empty line 121 continue 122 123 if line==b"}": 124 return materials 125 else: 126 # material 127 secondQuaote=line.find(b'"', 1) 128 material=mqo.Material(line[1:secondQuaote]) 129 try: 130 material.parse(line[secondQuaote+2:]) 131 except ValueError as ex: 132 self.printError("readMaterial", ex) 133 134 materials.append(material) 135 136 self.printError("readMaterial", "invalid eof") 137 return False
138
139 - def readChunk(self):
140 level=1 141 while(True): 142 line=self.getline() 143 if line==None: 144 # eof 145 break; 146 if line==b"": 147 # empty line 148 continue 149 150 if line==b"}": 151 level-=1 152 if level==0: 153 return True 154 elif line.find(b"{")!=-1: 155 level+=1 156 157 self.printError("readChunk", "invalid eof") 158 return False
159 160
161 -def read_from_file(path):
162 """ 163 read from file path, then return the pymeshio.mqo.Model. 164 165 :Parameters: 166 path 167 file path 168 """ 169 with io.open(path, 'rb') as ios: 170 return read(ios)
171 172
173 -def read(ios):
174 """ 175 read from ios, then return the pymeshio.mqo.Model. 176 177 :Parameters: 178 ios 179 input stream (in io.IOBase) 180 """ 181 assert(isinstance(ios, io.IOBase)) 182 reader=Reader(ios) 183 model=mqo.Model() 184 185 line=reader.getline() 186 if line!=b"Metasequoia Document": 187 print("invalid signature") 188 return False 189 190 line=reader.getline() 191 if line!=b"Format Text Ver 1.0": 192 print("unknown version: %s" % line) 193 194 while True: 195 line=reader.getline() 196 if line==None: 197 # eof 198 break; 199 if line==b"": 200 # empty line 201 continue 202 203 tokens=line.split() 204 key=tokens[0] 205 if key==b"Eof": 206 return model 207 elif key==b"Scene": 208 if not reader.readChunk(): 209 return 210 elif key==b"Material": 211 materials=reader.readMaterial() 212 if not materials: 213 return 214 model.materials=materials 215 elif key==b"Object": 216 firstQuote=line.find(b'"') 217 secondQuote=line.find(b'"', firstQuote+1) 218 obj=reader.readObject(line[firstQuote+1:secondQuote]) 219 if not obj: 220 return 221 model.objects.append(obj) 222 elif key==b"BackImage": 223 if not reader.readChunk(): 224 return 225 elif key==b"IncludeXml": 226 firstQuote=line.find(b'"') 227 secondQuote=line.find(b'"', firstQuote+1) 228 print("IncludeXml", line[firstQuote+1:secondQuote]) 229 else: 230 print("unknown key: %s" % key) 231 if not reader.readChunk(): 232 return 233 # error not reach here 234 raise ParseException("invalid eof")
235