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

Source Code for Package pymeshio.mqo

  1  # coding: utf-8
 
  2  """ 
 
  3  ======================
 
  4  Metasequioa MQO format
 
  5  ======================
 
  6  
 
  7  file format
 
  8  ~~~~~~~~~~~
 
  9  * http://www.metaseq.net/metaseq/format.html
 
 10  
 
 11  specs
 
 12  ~~~~~
 
 13  * textencoding: bytes(cp932)
 
 14  * coordinate: right handed y-up
 
 15  * uv origin: 
 
 16  * face: edge(2), triangle(3), quadrangle(4)
 
 17  * backculling: enable
 
 18  
 
 19  """ 
 20  
 
 21  import os 
 22  import sys 
 23  import math 
 24  import warnings 
 25  from .. import common 
 26  
 
 27  
 
 28  """
 
 29  MQO loader
 
 30  """ 
31 -class Material(object):
32 """mqo material 33 34 Attributes: 35 name: cp932 36 shader: 37 color: rgba 38 diffuse: 39 ambient: 40 emit: 41 specular: 42 power: 43 tex: cp932 windows file path 44 """ 45 __slots__=[ 46 "name", "shader", "color", "diffuse", 47 "ambient", "emit", "specular", "power", 48 "tex", 49 ]
50 - def __init__(self, name):
51 self.name=name 52 self.shader=3 53 self.color=common.RGBA(0.5, 0.5, 0.5, 1.0) 54 self.diffuse=1.0 55 self.ambient=0.0 56 self.emit=0.0 57 self.specular=0.0 58 self.power=5.0 59 self.tex=b""
60
61 - def getName(self): return self.name
62 - def getTexture(self): return self.tex
63
64 - def parse(self, line):
65 offset=0 66 while True: 67 leftParenthesis=line.find(b"(", offset) 68 if leftParenthesis==-1: 69 break 70 key=line[offset:leftParenthesis] 71 rightParenthesis=line.find(b")", leftParenthesis+1) 72 if rightParenthesis==-1: 73 raise ValueError("assert") 74 75 param=line[leftParenthesis+1:rightParenthesis] 76 if key==b"shader": 77 self.shader=int(param) 78 elif key==b"col": 79 self.color=common.RGBA(*[float(e) for e in param.split()]) 80 elif key==b"dif": 81 self.diffuse=float(param) 82 elif key==b"amb": 83 self.ambient=float(param) 84 elif key==b"emi": 85 self.emit=float(param) 86 elif key==b"spc": 87 self.specular=float(param) 88 elif key==b"power": 89 self.power=float(param) 90 elif key==b"tex": 91 self.tex=param[1:-1] 92 else: 93 print( 94 "%s#parse" % self.name, 95 "unknown key: %s" % key 96 ) 97 98 offset=rightParenthesis+2
99
100 - def __str__(self):
101 return "<Material %s shader: %d [%f, %f, %f, %f] %f>" % ( 102 self.name, self.shader, 103 self.color[0], self.color[1], self.color[2], self.color[3], 104 self.diffuse)
105 106
107 -class Obj(object):
108 """mqo object 109 110 Attributes: 111 name: cp932 112 depth: object hierarchy 113 folding: 114 scale: 115 rotation: 116 translation: 117 visible: 118 locking: 119 shading: 120 facet: smoothing threshold 121 color: 122 color_type: 123 mirror: mirroring 124 mirror_axis: 125 vertices: 126 faces: 127 edges: 128 smoothing: 129 """ 130 __slots__=["name", "depth", "folding", 131 "scale", "rotation", "translation", 132 "visible", "locking", "shading", "facet", 133 "color", "color_type", "mirror", "mirror_axis", 134 "vertices", "faces", "edges", "smoothing", 135 ] 136
137 - def __init__(self, name):
138 self.name=name 139 self.vertices=[] 140 self.faces=[] 141 self.edges=[] 142 self.depth=0 143 self.folding=0 144 self.scale=[1, 1, 1] 145 self.rotation=[0, 0, 0] 146 self.translation=[0, 0, 0] 147 self.visible=15 148 self.locking=0 149 self.shading=0 150 self.facet=59.5 151 self.color=[1, 1, 1] 152 self.color_type=0 153 self.mirror=0 154 self.smoothing=0
155
156 - def getName(self): return self.name
157
158 - def addVertex(self, x, y, z):
159 self.vertices.append(common.Vector3(x, y, z))
160
161 - def addFace(self, face):
162 if face.index_count==2: 163 self.edges.append(face) 164 else: 165 self.faces.append(face)
166
167 - def __str__(self):
168 return "<Object %s, %d vertices, %d faces>" % ( 169 self.name, len(self.vertices), len(self.faces))
170 171
172 -class Face(object):
173 """mqo face 174 175 Attributes: 176 index_count: 2 or 3 or 4 177 indices: index x index_count 178 material_index: 179 col: vertex_color x index_count 180 uv: Vector2 x index_count 181 """ 182 __slots__=[ 183 "index_count", 184 "indices", "material_index", "col", "uv", 185 ]
186 - def __init__(self, index_count, line):
187 if index_count<2 or index_count>4: 188 raise ValueError("invalid vertex count: %d" % index_count) 189 self.material_index=0 190 self.col=[] 191 self.uv=[common.Vector2(0, 0)]*4 192 self.index_count=index_count 193 offset=0 194 while True: 195 leftParenthesis=line.find(b"(", offset) 196 if leftParenthesis==-1: 197 break 198 key=line[offset:leftParenthesis] 199 rightParenthesis=line.find(b")", leftParenthesis+1) 200 if rightParenthesis==-1: 201 raise ValueError("assert") 202 params=line[leftParenthesis+1:rightParenthesis].split() 203 if key==b"V": 204 self.indices=[int(e) for e in params] 205 elif key==b"M": 206 self.material_index=int(params[0]) 207 elif key==b"UV": 208 uv_list=[float(e) for e in params] 209 self.uv=[] 210 for i in range(0, len(uv_list), 2): 211 self.uv.append(common.Vector2(uv_list[i], uv_list[i+1])) 212 elif key==b"COL": 213 for n in params: 214 d=int(n) 215 # R 216 d, m=divmod(d, 256) 217 self.col.append(m) 218 # G 219 d, m=divmod(d, 256) 220 self.col.append(m) 221 # B 222 d, m=divmod(d, 256) 223 self.col.append(m) 224 # A 225 d, m=divmod(d, 256) 226 self.col.append(m) 227 else: 228 print("Face#__init__:unknown key: %s" % key) 229 230 offset=rightParenthesis+2
231
232 - def getIndex(self, i): return self.indices[i]
233 - def getUV(self, i): return self.uv[i] if i<len(self.uv) else common.Vector2(0, 0)
234 235
236 -class Model(object):
237 - def __init__(self):
238 self.has_mikoto=False 239 self.materials=[] 240 self.objects=[]
241