OSDN Git Service

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