OSDN Git Service

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