OSDN Git Service

implemnet RigidBody
[meshio/pymeshio.git] / pymeshio / pmx / __init__.py
1 #!/usr/bin/env python\r
2 # coding: utf-8\r
3 """\r
4 pmx file io library.\r
5 \r
6 pmx file format:\r
7     PMDEditor's Lib/PMX仕様/PMX仕様.txt\r
8 """\r
9 __author__="ousttrue"\r
10 __license__="zlib"\r
11 __versioon__="1.0.0"\r
12 \r
13 \r
14 import io\r
15 import os\r
16 import struct\r
17 from pymeshio import common\r
18 \r
19 \r
20 \r
21 class Ik(object):\r
22     """ik info\r
23     """\r
24     __slots__=[\r
25             'target_index',\r
26             'loop',\r
27             'limit_radian',\r
28             'link',\r
29             ]\r
30     def __init__(self, target_index, loop, limit_radian):\r
31         self.target_index=target_index\r
32         self.loop=loop\r
33         self.limit_radian=limit_radian\r
34         self.link=[]\r
35 \r
36 \r
37 class IkLink(object):\r
38     """ik link info\r
39     """\r
40     __slots__=[\r
41             'bone_index',\r
42             'limit_angle',\r
43             'limit_min',\r
44             'limit_max',\r
45             ]\r
46     def __init__(self, bone_index, limit_angle):\r
47         self.bone_index=bone_index\r
48         self.limit_angle=limit_angle\r
49         self.limit_min=None\r
50         self.limit_max=None\r
51 \r
52 \r
53 class Bone(object):\r
54     """material\r
55 \r
56     Bone: see __init__\r
57     """\r
58     __slots__=[\r
59             'name',\r
60             'english_name',\r
61             'position',\r
62             'parent_index',\r
63             'layer',\r
64             'flag',\r
65 \r
66             'tail_positoin',\r
67             'tail_index',\r
68             'effect_index',\r
69             'effect_factor',\r
70             'fixed_axis',\r
71             'local_x_vector',\r
72             'local_z_vector',\r
73             'external_key',\r
74             'ik',\r
75             ]\r
76     def __init__(self,\r
77             name: str,\r
78             english_name: str,\r
79             position: common.Vector3,\r
80             parent_index: int,\r
81             layer: int,\r
82             flag: int\r
83             ):\r
84         self.name=name,\r
85         self.english_name=english_name\r
86         self.position=position\r
87         self.parent_index=parent_index\r
88         self.layer=layer\r
89         self.flag=flag\r
90 \r
91     def getConnectionFlag(self) -> int:\r
92         return self.flag & 0x0001\r
93 \r
94     def getIkFlag(self) -> int:\r
95         return (self.flag & 0x0020) >> 5\r
96 \r
97     def getRotationFlag(self) -> int:\r
98         return (self.flag & 0x0100) >> 8\r
99 \r
100     def getTranslationFlag(self) -> int:\r
101         return (self.flag & 0x0200) >> 9\r
102 \r
103     def getFixedAxisFlag(self) -> int:\r
104         return (self.flag & 0x0400) >> 10\r
105 \r
106     def getLocalCoordinateFlag(self) -> int:\r
107         return (self.flag &  0x0800) >> 11\r
108     \r
109     def getExternalParentDeformFlag(self) -> int:\r
110         return (self.flag &  0x2000) >> 13\r
111 \r
112  \r
113 class Material(object):\r
114     """material\r
115 \r
116     Attributes: see __init__\r
117     """\r
118     __slots__=[\r
119             'name',\r
120             'english_name',\r
121             'diffuse_color',\r
122             'diffuse_alpha',\r
123             'specular_color',\r
124             'specular_factor',\r
125             'ambient_color',\r
126             'flag',\r
127             'edge_color',\r
128             'edge_size',\r
129             'texture_index',\r
130             'sphia_texture_index',\r
131             'sphia_mode',\r
132             'toon_sharing_flag',\r
133             'toon_texture_index',\r
134             'comment',\r
135             'index_count',\r
136             ]\r
137     def __init__(self,\r
138             name: str,\r
139             english_name: str,\r
140             diffuse_color: common.RGB,\r
141             diffuse_alpha: float,\r
142             specular_color: common.RGB,\r
143             specular_factor: float,\r
144             ambient_color: common.RGB,\r
145             flag: int,\r
146             edge_color: common.RGBA,\r
147             edge_size: float,\r
148             texture_index: int,\r
149             sphia_texture_index: int,\r
150             sphia_mode: int,\r
151             toon_sharing_flag: int\r
152             ):\r
153         self.name=name\r
154         self.english_name=english_name\r
155         self.diffuse_color=diffuse_color\r
156         self.diffuse_alpha=diffuse_alpha\r
157         self.specular_color=specular_color\r
158         self.specular_factor=specular_factor\r
159         self.ambient_color=ambient_color\r
160         self.flag=flag\r
161         self.edge_color=edge_color\r
162         self.edge_size=edge_size\r
163         self.texture_index=texture_index\r
164         self.sphia_texture_index=sphia_texture_index\r
165         self.sphia_mode=sphia_mode\r
166         self.toon_sharing_flag=toon_sharing_flag\r
167         #\r
168         self.toon_texture_index=None\r
169         self.comment=''\r
170         self.index_count=0\r
171 \r
172 \r
173 class Deform(object):\r
174     pass\r
175 \r
176 \r
177 class Bdef1(object):\r
178     """bone deform. use a weight\r
179 \r
180     Attributes: see __init__\r
181     """\r
182     __slots__=[ 'bone_index']\r
183     def __init__(self, bone_index: int):\r
184         self.bone_index=bone_index\r
185 \r
186 \r
187 class Bdef2(object):\r
188     """bone deform. use two weights\r
189 \r
190     Attributes: see __init__\r
191     """\r
192     __slots__=[ 'index0', 'index1', 'weight0']\r
193     def __init__(self, \r
194             index0: int,\r
195             index1: int,\r
196             weight0: float):\r
197         self.index0=index0\r
198         self.index1=index1\r
199         self.weight0=weight0\r
200 \r
201 \r
202 class Vertex(object):\r
203     """pmx vertex\r
204 \r
205     Attributes: see __init__\r
206     """\r
207     __slots__=[ 'position', 'normal', 'uv', 'deform', 'edge_factor' ]\r
208     def __init__(self, \r
209             position: common.Vector3, \r
210             normal: common.Vector3, \r
211             uv: common.Vector2, \r
212             deform: Deform, \r
213             edge_factor: float):\r
214         self.position=position \r
215         self.normal=normal\r
216         self.uv=uv\r
217         self.deform=deform\r
218         self.edge_factor=edge_factor\r
219 \r
220 \r
221 class Morph(object):\r
222     """pmx morph\r
223 \r
224     Attributes:\r
225         name: \r
226         english_name: \r
227         panel:\r
228         morph_type:\r
229         offsets:\r
230     """\r
231     __slots__=[\r
232             'name',\r
233             'english_name',\r
234             'panel',\r
235             'morph_type',\r
236             'offsets',\r
237             ]\r
238     def __init__(self, name, english_name, panel, morph_type):\r
239         self.name=name\r
240         self.english_name=english_name\r
241         self.panel=panel\r
242         self.morph_type=morph_type\r
243         self.offsets=[]\r
244 \r
245 \r
246 class VerexMorphOffset(object):\r
247     """pmx vertex morph offset\r
248 \r
249     Attributes:\r
250         vertex_index:\r
251         position_offset: Vector3\r
252     """\r
253     __slots__=[\r
254             'vertex_index',\r
255             'position_offset',\r
256             ]\r
257     def __init__(self, vertex_index, position_offset):\r
258         self.vertex_index=vertex_index\r
259         self.position_offset=position_offset\r
260 \r
261 \r
262 class DisplaySlot(object):\r
263     """pmx display slot\r
264 \r
265     Attributes:\r
266         name: \r
267         english_name: \r
268         special_flag:\r
269         refrences: list of (ref_type, ref_index)\r
270     """\r
271     __slots__=[\r
272             'name',\r
273             'english_name',\r
274             'special_flag',\r
275             'refrences',\r
276             ]\r
277     def __init__(self, name, english_name, special_flag):\r
278         self.name=name\r
279         self.english_name=english_name\r
280         self.special_flag=special_flag\r
281         self.refrences=[]\r
282 \r
283 \r
284 class Shape(object):\r
285     pass\r
286 \r
287 \r
288 class SphereShape(Shape):\r
289     __slots__=['radius']\r
290     def __init__(self, radius):\r
291         self.radius=radius\r
292 \r
293 \r
294 class CapsuleShape(Shape):\r
295     __slots__=['short_radius', 'long_radius']\r
296     def __init__(self, short_radius, long_radius): \r
297         self.short_radius=short_radius\r
298         self.long_radius=long_radius\r
299 \r
300 \r
301 class BoxShape(Shape):\r
302     __slots__=['x', 'y', 'z']\r
303     def __init__(self, x, y, z):\r
304         self.x=x\r
305         self.y=y\r
306         self.z=z\r
307 \r
308 \r
309 class RigidBodyParam(object):\r
310     """pmx rigidbody param(for bullet)\r
311 \r
312     Attributes:\r
313         mass:\r
314         linear_damping:\r
315         angular_damping:\r
316         restitution:\r
317         friction:\r
318     """\r
319     __slots__=[\r
320             'mass',\r
321             'linear_damping',\r
322             'angular_damping',\r
323             'restitution',\r
324             'friction',\r
325             ]\r
326     def __init__(self, mass, \r
327             linear_damping, angular_damping, restitution, friction):\r
328         self.mass=mass\r
329         self.linear_damping=linear_damping\r
330         self.angular_damping=angular_damping\r
331         self.restitution=restitution\r
332         self.friction=friction\r
333 \r
334 class RigidBody(object):\r
335     """pmx rigidbody\r
336 \r
337     Attributes:\r
338         name: \r
339         english_name: \r
340         bone_index:\r
341         collision_group:\r
342         no_collision_flag:\r
343         shape:\r
344         param:\r
345         mode:\r
346     """\r
347     __slots__=[\r
348             'name',\r
349             'english_name',\r
350             'bone_index',\r
351             'collision_group',\r
352             'no_collision_flag',\r
353             'shape',\r
354             'param',\r
355             'mode',\r
356             ]\r
357     def __init__(self,\r
358             name,\r
359             english_name,\r
360             bone_index,\r
361             collision_group,\r
362             no_collision_flag,\r
363             shape_type,\r
364             shape_size,\r
365             shape_position,\r
366             shape_rotation,\r
367             mass,\r
368             linear_damping,\r
369             angular_damping,\r
370             restitution,\r
371             friction,\r
372             mode\r
373             ):\r
374         self.name=name\r
375         self.english_name=english_name\r
376         self.bone_index=bone_index\r
377         self.collision_group=collision_group\r
378         self.no_collision_flag=no_collision_flag\r
379         if shape_type==0:\r
380             self.shape=SphereShape(shape_size.x)\r
381         elif shape_type==1:\r
382             self.shape=BoxShape(shape_size.x, shape_size.y, shape_size.z)\r
383         elif shape_type==2:\r
384             self.shape=CapsuleShape(shape_size.x, shape_size.y)\r
385         else:\r
386             raise pymeshio.common.ParseException(\r
387                     "unknown shape_type: {0}".format(shape_type))\r
388         self.param=RigidBodyParam(mass,\r
389                 linear_damping, angular_damping,\r
390                 restitution, friction)\r
391         self.mode=mode\r
392 \r
393 \r
394 class Model(object):\r
395     """pmx data representation\r
396 \r
397     Attributes:\r
398         version: pmx version(expected 2.0)\r
399         name: \r
400         english_name: \r
401         comment: \r
402         english_comment: \r
403         vertices:\r
404         textures:\r
405         materials:\r
406         bones:\r
407         morph:\r
408         display_slots:\r
409         rigidbodies:\r
410     """\r
411     __slots__=[\r
412             'version', # pmx version\r
413             'name', # model name\r
414             'english_name', # model name in english\r
415             'comment', # model comment\r
416             'english_comment', # model comment in english\r
417             'vertices',\r
418             'indices',\r
419             'textures',\r
420             'materials',\r
421             'bones',\r
422             'morphs',\r
423             'display_slots',\r
424             'rigidbodies',\r
425             ]\r
426     def __init__(self, version):\r
427         self.version=version\r
428         self.name=''\r
429         self.english_name=''\r
430         self.comment=''\r
431         self.english_comment=''\r
432         self.vertices=[]\r
433         self.indices=[]\r
434         self.textures=[]\r
435         self.materials=[]\r
436         self.bones=[]\r
437         self.rigidbodies=[]\r
438 \r