OSDN Git Service

fix shift FACTOR
[meshio/pymeshio.git] / blender25-meshio / pymeshio / pmd.py
1 # coding: utf-8
2 """
3 PMDの読み込み
4 http://blog.goo.ne.jp/torisu_tetosuki/e/209ad341d3ece2b1b4df24abf619d6e4
5 """
6 import os
7 import sys
8 import struct
9 from .mmd import *
10
11
12 class Vertex(object):
13     """pmd vertex struct.
14
15     Attributes:
16         pos: Vector3
17         normal: Vector3
18         uv: Vector2
19         bone0: bone index
20         bone1: bone index
21         weight0: bone0 influence
22         edge_flag: int flag
23     """
24     __slots__=['pos', 'normal', 'uv', 'bone0', 'bone1', 'weight0', 'edge_flag']
25     def __init__(self, x=0, y=0, z=0, nx=0, ny=0, nz=0, u=0, v=0,
26             bone0=0, bone1=0, weight0=0, edge_flag=0):
27         self.pos=Vector3(x, y, z)
28         self.normal=Vector3(nx, ny, nz)
29         self.uv=Vector2(u, v)
30         self.bone0=bone0
31         self.bone1=bone1
32         self.weight0=weight0
33         self.edge_flag=edge_flag
34
35     def __str__(self):
36         return "<%s %s %s, (%d, %d, %d)>" % (
37                 str(self.pos), 
38                 str(self.normal), 
39                 str(self.uv), 
40                 self.bone0, self.bone1, self.weight0)
41
42     def __getitem__(self, key):
43         if key==0:
44             return self.pos.x
45         elif key==1:
46             return self.pos.y
47         elif key==2:
48             return self.pos.z
49         else:
50             assert(False)
51
52
53 class Material(object):
54     """pmd material struct.
55
56     Attributes:
57         diffuse: RGBA
58         shinness: float
59         specular: RGB
60         ambient: RGB
61         vertex_count: indices length
62         _texture: texture file path
63         toon_index: int
64         flag: int
65     """
66     __slots__=[
67             'diffuse', 'shinness', 'specular',
68             'ambient', 'vertex_count', '_texture', 'toon_index', 'flag',
69             ]
70     def getTexture(self): return from_str(self._texture)
71     def setTexture(self, texture): self._texture=to_str(texture)
72     texture=property(getTexture, setTexture)
73
74     def __init__(self, dr=0, dg=0, db=0, alpha=1, 
75             specular=0, sr=0, sg=0, sb=0, ar=0, ag=0, ab=0):
76         self.diffuse=RGBA(dr, dg, db, alpha)
77         self.specular=RGBA(sr, sg, sb)
78         self.shinness=specular
79         self.ambient=RGBA(ar, ag, ab)
80         self.vertex_count=0
81         self.texture=''
82         self.toon_index=0
83         self.flag=0
84
85     def __str__(self):
86         return "<Material [%f, %f, %f, %f]>" % (
87                 self.diffuse[0], self.diffuse[1], 
88                 self.diffuse[2], self.diffuse[3],
89                 )
90
91
92 class Bone(object):
93     """pmd material struct.
94
95     Attributes:
96         _name: 
97         index:
98         type:
99         ik:
100         pos:
101         _english_name:
102         ik_index:
103         parent_index:
104         tail_index:
105
106         parent:
107         tail:
108         children:
109     """
110     # kinds
111     ROTATE = 0
112     ROTATE_MOVE = 1
113     IK = 2
114     IK_ROTATE_INFL = 4
115     ROTATE_INFL = 5
116     IK_TARGET = 6
117     UNVISIBLE = 7
118     # since v4.0
119     ROLLING=8 # ?
120     TWEAK=9
121     __slots__=['_name', 'index', 'type', 'parent', 'ik', 'pos',
122             'children', '_english_name', 'ik_index',
123             'parent_index', 'tail_index', 'tail',
124             ]
125     def getName(self): 
126         """
127         return str(multibyte) in python2
128         return bytes in python3
129         """
130         return from_str(self._name)
131     def setName(self, name): self._name=to_str(name)
132     name=property(getName, setName)
133     def getEnglishName(self): return from_str(self._english_name)
134     def setEnglishName(self, english_name): self._english_name=to_str(english_name)
135     english_name=property(getEnglishName, setEnglishName)
136
137     def __init__(self, name='bone', type=0):
138         self.name=name
139         self.index=0
140         self.type=type
141         self.parent_index=0xFFFF
142         self.tail_index=0
143         self.tail=Vector3(0, 0, 0)
144         self.parent=None
145         self.ik_index=0xFFFF
146         self.pos=Vector3(0, 0, 0)
147         self.children=[]
148         self.english_name=''
149
150     def hasParent(self):
151         return self.parent_index!=0xFFFF
152
153     def hasChild(self):
154         return self.tail_index!=0
155
156     def display(self, indent=[]):
157         if len(indent)>0:
158             prefix=''
159             for i, is_end in enumerate(indent):
160                 if i==len(indent)-1:
161                     break
162                 else:
163                     prefix+='  ' if is_end else ' |'
164             uni='%s +%s(%s)' % (prefix, unicode(self), self.english_name)
165             print(uni.encode(ENCODING))
166         else:
167             uni='%s(%s)' % (unicode(self), self.english_name)
168             print(uni.encode(ENCODING))
169
170         child_count=len(self.children)
171         for i in range(child_count):
172             child=self.children[i]
173             if i<child_count-1:
174                 child.display(indent+[False])
175             else:
176                 # last
177                 child.display(indent+[True])
178
179 # 0
180 class Bone_Rotate(Bone):
181     __slots__=[]
182     def __init__(self, name):
183         super(Bone_Rotate, self).__init__(name, 0)
184     def __str__(self):
185         return '<ROTATE %s>' % (self.name)
186 # 1
187 class Bone_RotateMove(Bone):
188     __slots__=[]
189     def __init__(self, name):
190         super(Bone_RotateMove, self).__init__(name, 1)
191     def __str__(self):
192         return '<ROTATE_MOVE %s>' % (self.name)
193 # 2
194 class Bone_IK(Bone):
195     __slots__=[]
196     def __init__(self, name):
197         super(Bone_IK, self).__init__(name, 2)
198     def __str__(self):
199         return '<IK %s>' % (self.name)
200 # 4
201 class Bone_IKRotateInfl(Bone):
202     __slots__=[]
203     def __init__(self, name):
204         super(Bone_IKRotateInfl, self).__init__(name, 4)
205     def __str__(self):
206         return '<IK_ROTATE_INFL %s>' % (self.name)
207 # 5
208 class Bone_RotateInfl(Bone):
209     __slots__=[]
210     def __init__(self, name):
211         super(Bone_RotateInfl, self).__init__(name, 5)
212     def __str__(self):
213         return '<ROTATE_INFL %s>' % (self.name)
214 # 6
215 class Bone_IKTarget(Bone):
216     __slots__=[]
217     def __init__(self, name):
218         super(Bone_IKTarget, self).__init__(name, 6)
219     def __str__(self):
220         return '<IK_TARGET %s>' % (self.name)
221 # 7
222 class Bone_Unvisible(Bone):
223     __slots__=[]
224     def __init__(self, name):
225         super(Bone_Unvisible, self).__init__(name, 7)
226     def __str__(self):
227         return '<UNVISIBLE %s>' % (self.name)
228 # 8
229 class Bone_Rolling(Bone):
230     __slots__=[]
231     def __init__(self, name):
232         super(Bone_Rolling, self).__init__(name, 8)
233     def __str__(self):
234         return '<ROLLING %s>' % (self.name)
235 # 9
236 class Bone_Tweak(Bone):
237     __slots__=[]
238     def __init__(self, name):
239         super(Bone_Tweak, self).__init__(name, 9)
240     def __str__(self):
241         return '<TWEAK %s>' % (self.name)
242
243
244 def createBone(name, type):
245     if type==0:
246         return Bone_Rotate(name)
247     elif type==1:
248         return Bone_RotateMove(name)
249     elif type==2:
250         return Bone_IK(name)
251     elif type==3:
252         raise Exception("no used bone type: 3(%s)" % name)
253     elif type==4:
254         return Bone_IKRotateInfl(name)
255     elif type==5:
256         return Bone_RotateInfl(name)
257     elif type==6:
258         return Bone_IKTarget(name)
259     elif type==7:
260         return Bone_Unvisible(name)
261     elif type==8:
262         return Bone_Rolling(name)
263     elif type==9:
264         return Bone_Tweak(name)
265     else:
266         raise Exception("unknown bone type: %d(%s)", type, name)
267
268
269 class IK(object):
270     __slots__=['index', 'target', 'iterations', 'weight', 'length', 'children']
271     def __init__(self, index=0, target=0):
272         self.index=index
273         self.target=target
274         self.iterations=None
275         self.weight=None
276         self.children=[]
277
278     def __str__(self):
279         return "<IK index: %d, target: %d, iterations: %d, weight: %f, children: %s(%d)>" %(self.index, self.target, self.iterations, self.weight, '-'.join([str(i) for i in self.children]), len(self.children))
280
281
282 class Skin(object):
283     __slots__=['_name', 'type', 'indices', 'pos_list', '_english_name',
284             'vertex_count']
285     def getName(self): return from_str(self._name)
286     def setName(self, name): self._name=to_str(name)
287     name=property(getName, setName)
288     def getEnglishName(self): return from_str(self._english_name)
289     def setEnglishName(self, english_name): self._english_name=to_str(english_name)
290     english_name=property(getEnglishName, setEnglishName)
291
292     def __init__(self, name='skin'):
293         self.name=name
294         self.type=None
295         self.indices=[]
296         self.pos_list=[]
297         self.english_name=''
298         self.vertex_count=0
299
300     def append(self, index, x, y, z):
301         self.indices.append(index)
302         self.pos_list.append(Vector3(x, y, z))
303
304     def __str__(self):
305         return '<Skin name: "%s", type: %d, vertex: %d>' % (
306             self.name, self.type, len(self.indices))
307
308
309 class BoneGroup(object):
310     __slots__=['_name', '_english_name']
311     def getName(self): return from_str(self._name)
312     def setName(self, name): self._name=to_str(name)
313     name=property(getName, setName)
314     def getEnglishName(self): return from_str(self._english_name)
315     def setEnglishName(self, english_name): self._english_name=to_str(english_name)
316     english_name=property(getEnglishName, setEnglishName)
317
318     def __init__(self, name='group'): self._name=name; self._english_name='center'
319
320
321 SHAPE_SPHERE=0
322 SHAPE_BOX=1
323 SHAPE_CAPSULE=2
324
325 RIGIDBODY_KINEMATICS=0
326 RIGIDBODY_PHYSICS=1
327 RIGIDBODY_PHYSICS_WITH_BONE=2
328
329
330 class RigidBody(object):
331     __slots__=['_name', 'boneIndex', 'group', 'target', 'shapeType',
332             'w', 'h', 'd', 'position', 'rotation', 'weight',
333             'linearDamping', 'angularDamping', 'restitution', 'friction', 'processType'
334             ]
335     def getName(self): return from_str(self._name)
336     def setName(self, name): self._name=to_str(name)
337     name=property(getName, setName)
338
339     def __init__(self, name):
340         self.name=name
341         self.position=Vector3()
342         self.rotation=Vector3()
343
344
345 class Constraint(object):
346     __slots__=[ '_name', 'rigidA', 'rigidB', 'pos', 'rot',
347             'constraintPosMin', 'constraintPosMax',
348             'constraintRotMin', 'constraintRotMax',
349             'springPos', 'springRot',
350             ]
351     def getName(self): return from_str(self._name)
352     def setName(self, name): self._name=to_str(name)
353     name=property(getName, setName)
354
355     def __init__(self, name):
356         self.name=name
357         self.pos=Vector3()
358         self.rot=Vector3()
359         self.constraintPosMin=Vector3()
360         self.constraintPosMax=Vector3()
361         self.constraintRotMin=Vector3()
362         self.constraintRotMax=Vector3()
363         self.springPos=Vector3()
364         self.springRot=Vector3()
365
366
367 class ToonTextures(object):
368     __slots__=['_toon_textures']
369     def __init__(self):
370         self._toon_textures=[]
371         for i in range(10):
372             self._toon_textures.append('toon%02d.bmp' % (i+1))
373
374     def __getitem__(self, key):
375         return from_str(self._toon_textures[key])
376
377     def __setitem__(self, key, value):
378         self._toon_textures[key]=to_str(value)
379
380     def __iter__(self):
381         for toon_texture in self._toon_textures:
382             yield from_str(toon_texture)
383
384
385 class IO(object):
386     """pmd loader class.
387
388     Attributes:
389         io: internal use.
390         end: internal use.
391         pos: internal user.
392
393         version: pmd version number
394         _name: internal
395     """
396     __slots__=['io', 'end', 'pos',
397             'version', '_name', '_comment',
398             '_english_name', '_english_comment',
399             'vertices', 'indices', 'materials', 'bones', 
400             'ik_list', 'morph_list',
401             'face_list', 'bone_group_list', 'bone_display_list',
402             'toon_textures',
403             'no_parent_bones',
404             'rigidbodies', 'constraints',
405             ]
406     def getName(self): return from_str(self._name)
407     def setName(self, name): self._name=to_str(name)
408     name=property(getName, setName)
409     def getEnglishName(self): return from_str(self._english_name)
410     def setEnglishName(self, english_name): self._english_name=to_str(english_name)
411     english_name=property(getEnglishName, setEnglishName)
412     def getComment(self): return from_str(self._comment)
413     def setComment(self, comment): self._comment=to_str(comment)
414     comment=property(getComment, setComment)
415     def getEnglishComment(self): return from_str(self._english_comment)
416     def setEnglishComment(self, english_comment): self._english_comment=to_str(english_comment)
417     english_comment=property(getEnglishComment, setEnglishComment)
418
419     def __init__(self):
420         self.version=1.0
421         self.name=''
422         self.comment=''
423         self.english_name=''
424         self.english_comment=''
425         self.vertices=[]
426         self.indices=[]
427         self.materials=[]
428         self.bones=[]
429         self.ik_list=[]
430         self.morph_list=[]
431         self.face_list=[]
432         self.bone_group_list=[]
433         self.bone_display_list=[]
434         # extend
435         self.toon_textures=ToonTextures()
436         self.rigidbodies=[]
437         self.constraints=[]
438         # innner use
439         self.no_parent_bones=[]
440
441     def each_vertex(self): return self.vertices
442     def getUV(self, i): return self.vertices[i].uv
443     def addVertex(self): 
444         v=Vertex()
445         self.vertices.append(v)
446         return v
447     def addMaterial(self):
448         m=Material()
449         self.materials.append(m)
450         return m
451     def addBone(self):
452         b=Bone()
453         self.bones.append(b)
454         return b
455     def addIK(self):
456         ik=IK()
457         self.ik_list.append(ik)
458         return ik
459     def addMorph(self):
460         s=Skin()
461         self.morph_list.append(s)
462         return s
463     def addBoneGroup(self):
464         g=BoneGroup()
465         self.bone_group_list.append(g)
466         return g
467     def addBoneDisplay(self, b, g):
468         self.bone_display_list.append((b, g))
469
470     def __str__(self):
471         return '<PMDLoader version: %g, model: "%s", vertex: %d, face: %d, material: %d, bone: %d ik: %d, skin: %d>' % (
472             self.version, self.name, len(self.vertices), len(self.indices),
473             len(self.materials), len(self.bones), len(self.ik_list), len(self.morph_list))
474
475     def _check_position(self):
476         self.pos=self.io.tell()
477
478     def read(self, path):
479         size=os.path.getsize(path)
480         with open(path, "rb") as f:
481             return self.load(path, f, size)
482
483     def load(self, path, io, end):
484         self.io=io
485         self.pos=self.io.tell()
486         self.end=end
487         self._check_position()
488
489         if not self._loadHeader():
490             return False
491         self._check_position()
492
493         if not self._loadVertex():
494             return False
495         self._check_position()
496
497         if not self._loadFace():
498             return False
499         self._check_position()
500
501         if not self._loadMaterial():
502             return False
503         self._check_position()
504
505         if not self._loadBone():
506             return False
507         self._check_position()
508
509         if not self._loadIK():
510             return False
511         self._check_position()
512
513         if not self._loadSkin():
514             return False
515         self._check_position()
516
517         if not self._loadSkinIndex():
518             return False
519         self._check_position()
520
521         if not self._loadBoneName():
522             return False
523         self._check_position()
524
525         if not self._loadBoneIndex():
526             return False
527         self._check_position()
528
529         if not self._loadExtend():
530             print('fail to loadExtend')
531             return False
532
533         # 終端
534         if self.io.tell()!=self.end:
535             print("can not reach eof.")
536             print("current: %d, end: %d, remain: %d" % (
537                     self.io.tell(), self.end, self.end-self.io.tell()))
538
539         # build bone tree
540         for i, child in enumerate(self.bones):
541             if child.parent_index==0xFFFF:
542                 # no parent
543                 self.no_parent_bones.append(child)
544                 child.parent=None
545             else:
546                 # has parent
547                 parent=self.bones[child.parent_index]
548                 child.parent=parent
549                 parent.children.append(child)
550             # 後位置
551             if child.hasChild():
552                 child.tail=self.bones[child.tail_index].pos
553
554         return True
555
556     def write(self, path):
557         io=open(path, 'wb')
558         if not io:
559             return False
560         # Header
561         io.write(b"Pmd")
562         io.write(struct.pack("f", self.version))
563         io.write(struct.pack("20s", self.name))
564         io.write(struct.pack("256s", self.comment))
565
566         # Vertices
567         io.write(struct.pack("I", len(self.vertices)))
568         sVertex=struct.Struct("=8f2H2B") # 38byte
569         assert(sVertex.size==38)
570         for v in self.vertices:
571             data=sVertex.pack( 
572                 v.pos[0], v.pos[1], v.pos[2],
573                 v.normal[0], v.normal[1], v.normal[2],
574                 v.uv[0], v.uv[1],
575                 v.bone0, v.bone1, v.weight0, v.edge_flag)
576             io.write(data)
577
578         # Faces
579         io.write(struct.pack("I", len(self.indices)))
580         io.write(struct.pack("=%dH" % len(self.indices), *self.indices))
581
582         # material
583         io.write(struct.pack("I", len(self.materials)))
584         sMaterial=struct.Struct("=3fff3f3fBBI20s") # 70byte
585         assert(sMaterial.size==70)
586         for m in self.materials:
587             io.write(sMaterial.pack(
588                 m.diffuse[0], m.diffuse[1], m.diffuse[2], m.diffuse[3],
589                 m.shinness, 
590                 m.specular[0], m.specular[1], m.specular[2],
591                 m.ambient[0], m.ambient[1], m.ambient[2],
592                 m.toon_index, m.flag,
593                 m.vertex_count,
594                 m.texture
595                 ))
596
597         # bone
598         io.write(struct.pack("H", len(self.bones)))
599         sBone=struct.Struct("=20sHHBH3f")
600         assert(sBone.size==39)
601         for b in self.bones:
602             io.write(sBone.pack(
603                 b.name,
604                 b.parent_index, b.tail_index, b.type, b.ik_index,
605                 b.pos[0], b.pos[1], b.pos[2]))
606
607         # IK
608         io.write(struct.pack("H", len(self.ik_list)))
609         for ik in self.ik_list:
610             io.write(struct.pack("=2HBHf", 
611                 ik.index, ik.target, ik.length, ik.iterations, ik.weight
612                 ))
613             for c in ik.children:
614                 io.write(struct.pack("H", c))
615
616         # skin
617         io.write(struct.pack("H", len(self.morph_list)))
618         for s in self.morph_list:
619             io.write(struct.pack("20sIB", 
620                 s.name, len(s.indices), s.type))
621             for i, v in zip(s.indices, s.pos_list):
622                 io.write(struct.pack("I3f", i, v[0], v[1], v[2]))
623
624         # skin disp list
625         io.write(struct.pack("B", len(self.face_list)))
626         for i in self.face_list:
627             io.write(struct.pack("H", i))
628
629         # bone disp list
630         io.write(struct.pack("B", len(self.bone_group_list)))
631         for g in self.bone_group_list:
632             io.write(struct.pack("50s", g.name))
633
634         io.write(struct.pack("I", len(self.bone_display_list)))
635         for l in self.bone_display_list:
636             io.write(struct.pack("=HB", *l))
637
638         ############################################################
639         # extend data
640         ############################################################
641         io.write(struct.pack("B", 1))
642         # english name
643         io.write(struct.pack("=20s", self.english_name))
644         io.write(struct.pack("=256s", self.english_comment))
645         # english bone name
646         for bone in self.bones:
647             io.write(struct.pack("=20s", bone.english_name))
648         # english skin list
649         for skin in self.morph_list:
650             #print(skin.name)
651             if skin.name==b'base':
652                 continue
653             io.write(struct.pack("=20s", skin.english_name))
654         # english bone list
655         for bone_group in self.bone_group_list:
656             io.write(struct.pack("50s", bone_group.english_name))
657         # toon texture
658         for toon_texture in self.toon_textures:
659             io.write(struct.pack("=100s", toon_texture))
660         # rigid
661         io.write(struct.pack("I", len(self.rigidbodies)))
662         for r in self.rigidbodies:
663             io.write(struct.pack("=20sHBHB14fB",
664                 r.name, r.boneIndex, r.group, r.target, r.shapeType,
665                 r.w, r.h, r.d, 
666                 r.position.x, r.position.y, r.position.z, 
667                 r.rotation.x, r.rotation.y, r.rotation.z, 
668                 r.weight,
669                 r.linearDamping, r.angularDamping, r.restitution,
670                 r.friction, r.processType))
671
672         # constraint
673         io.write(struct.pack("I", len(self.constraints)))
674         for c in self.constraints:
675             io.write(struct.pack("=20sII24f",
676                 c.name, c.rigidA, c.rigidB,
677                 c.pos.x, c.pos.y, c.pos.z,
678                 c.rot.x, c.rot.y, c.rot.z,
679                 c.constraintPosMin.x, c.constraintPosMin.y, c.constraintPosMin.z,
680                 c.constraintPosMax.x, c.constraintPosMax.y, c.constraintPosMax.z,
681                 c.constraintRotMin.x, c.constraintRotMin.y, c.constraintRotMin.z,
682                 c.constraintRotMax.x, c.constraintRotMax.y, c.constraintRotMax.z,
683                 c.springPos.x, c.springPos.y, c.springPos.z,
684                 c.springRot.x, c.springRot.y, c.springRot.z
685                 ))
686
687         return True
688
689
690     def _loadExtend(self):
691         ############################################################
692         # extend1: english name
693         ############################################################
694         if self.io.tell()>=self.end:
695             return True
696         if struct.unpack("B", self.io.read(1))[0]==1:
697             if not self.loadEnglishName():
698                 return False
699         self._check_position()
700
701         ############################################################
702         # extend2: toon texture list
703         ############################################################
704         if self.io.tell()>=self.end:
705             return True
706         if not self.loadToonTexture():
707             return False
708         self._check_position()
709
710         ############################################################
711         # extend3: physics
712         ############################################################
713         if self.io.tell()>=self.end:
714             return True
715         if not self.loadPhysics():
716             return False
717         self._check_position()
718
719         return True
720
721     def _loadHeader(self):
722         signature=struct.unpack("3s", self.io.read(3))[0]
723         if signature!=b"Pmd":
724             print("invalid signature", signature)
725             return False
726         self.version=struct.unpack("f", self.io.read(4))[0]
727         self.name = truncate_zero(struct.unpack("20s", self.io.read(20))[0])
728         self.comment = truncate_zero(
729                 struct.unpack("256s", self.io.read(256))[0])
730         return True
731
732     def _loadVertex(self):
733         count = struct.unpack("I", self.io.read(4))[0]
734         for i in range(count):
735             self.vertices.append(Vertex(*struct.unpack("8f2H2B", self.io.read(38))))
736         return True
737
738     def _loadFace(self):
739         count = struct.unpack("I", self.io.read(4))[0]
740         for i in range(0, count, 3):
741             self.indices+=struct.unpack("HHH", self.io.read(6))
742         return True
743
744     def _loadMaterial(self):
745         count = struct.unpack("I", self.io.read(4))[0]
746         for i in range(count):
747             material=Material(*struct.unpack("4ff3f3f", self.io.read(44)))
748             material.toon_index=struct.unpack("B", self.io.read(1))[0]
749             material.flag=struct.unpack("B", self.io.read(1))[0]
750             material.vertex_count=struct.unpack("I", self.io.read(4))[0]
751             texture=truncate_zero(struct.unpack("20s", self.io.read(20))[0])
752             # todo sphere map
753             #material.texture=texture.split('*')[0]
754             material.texture=texture
755             self.materials.append(material)
756         return True
757
758     def _loadBone(self):
759         size = struct.unpack("H", self.io.read(2))[0]
760         for i in range(size):
761             name=truncate_zero(struct.unpack("20s", self.io.read(20))[0])
762             parent_index, tail_index = struct.unpack("HH", self.io.read(4))
763             type = struct.unpack("B", self.io.read(1))[0]
764             bone=createBone(name, type)
765             bone.parent_index=parent_index
766             bone.tail_index=tail_index
767             bone.ik_index = struct.unpack("H", self.io.read(2))[0]
768             bone.pos = Vector3(*struct.unpack("3f", self.io.read(12)))
769             bone.english_name="bone%03d" % len(self.bones)
770             self.bones.append(bone)
771         return True
772
773     def _loadIK(self):
774         size = struct.unpack("H", self.io.read(2))[0]
775         for i in range(size):
776             ik=IK(*struct.unpack("2H", self.io.read(4)))
777             ik.length = struct.unpack("B", self.io.read(1))[0]
778             ik.iterations = struct.unpack("H", self.io.read(2))[0]
779             ik.weight = struct.unpack("f", self.io.read(4))[0]
780             for j in range(ik.length):
781                 ik.children.append(struct.unpack("H", self.io.read(2))[0])
782             self.ik_list.append(ik)
783         return True
784
785     def _loadSkin(self):
786         size = struct.unpack("H", self.io.read(2))[0]
787         for i in range(size):
788             skin=Skin(truncate_zero(struct.unpack("20s", self.io.read(20))[0]))
789             skin_size = struct.unpack("I", self.io.read(4))[0]
790             skin.type = struct.unpack("B", self.io.read(1))[0]
791             for j in range(skin_size):
792                 skin.indices.append(struct.unpack("I", self.io.read(4))[0])
793                 skin.pos_list.append(
794                         Vector3(*struct.unpack("3f", self.io.read(12))))
795             skin.english_name="skin%03d" % len(self.morph_list)
796             self.morph_list.append(skin)
797         return True
798
799     def _loadSkinIndex(self):
800         size = struct.unpack("B", self.io.read(1))[0]
801         for i in range(size):
802             self.face_list.append(struct.unpack("H", self.io.read(2))[0])
803         return True
804
805     def _loadBoneName(self):
806         size = struct.unpack("B", self.io.read(1))[0]
807         for i in range(size):
808             self.bone_group_list.append(BoneGroup(
809                 truncate_zero(struct.unpack("50s", self.io.read(50))[0])))
810         return True
811
812     def _loadBoneIndex(self):
813         size = struct.unpack("I", self.io.read(4))[0]
814         for i in range(size):
815             first=struct.unpack("H", self.io.read(2))[0]
816             second=struct.unpack("B", self.io.read(1))[0]
817             self.bone_display_list.append((first, second))
818         return True
819
820     def loadToonTexture(self):
821         """
822         100bytex10
823         """
824         for i in range(10):
825             self.toon_textures[i]=truncate_zero(struct.unpack("100s", self.io.read(100))[0])
826         return True
827
828     def loadEnglishName(self):
829         # english name
830         self.english_name=truncate_zero(
831                 struct.unpack("20s", self.io.read(20))[0])
832         self.english_comment=truncate_zero(
833                 struct.unpack("256s", self.io.read(256))[0])
834         self._check_position()
835         # english bone name
836         for bone in self.bones:
837             english_name=truncate_zero(
838                     struct.unpack("20s", self.io.read(20))[0])
839             bone.english_name=english_name
840         self._check_position()
841         # english skin list
842         for skin in self.morph_list:
843             if skin.name==b'base':
844                 continue
845             english_name=truncate_zero(
846                     struct.unpack("20s", self.io.read(20))[0])
847             #skin=self.morph_list[index]
848             if english_name!=skin.name:
849                 skin.english_name=english_name
850         self._check_position()
851         # english bone list
852         for i in range(0, len(self.bone_group_list)):
853             self.bone_group_list[i].english_name=truncate_zero(
854                     struct.unpack("50s", self.io.read(50))[0])
855         self._check_position()
856         return True
857
858     def loadPhysics(self):
859         # 剛体リスト
860         count = struct.unpack("I", self.io.read(4))[0]
861         for i in range(count):
862             name=truncate_zero(struct.unpack("20s", self.io.read(20))[0])
863             rigidbody=RigidBody(name)
864             rigidbody.boneIndex=struct.unpack("H", self.io.read(2))[0]
865             rigidbody.group=struct.unpack("B", self.io.read(1))[0]
866             rigidbody.target=struct.unpack("H", self.io.read(2))[0]
867             rigidbody.shapeType=struct.unpack("B", self.io.read(1))[0]
868             rigidbody.w=struct.unpack("f", self.io.read(4))[0]
869             rigidbody.h=struct.unpack("f", self.io.read(4))[0]
870             rigidbody.d=struct.unpack("f", self.io.read(4))[0]
871             rigidbody.position.x=struct.unpack("f", self.io.read(4))[0]
872             rigidbody.position.y=struct.unpack("f", self.io.read(4))[0]
873             rigidbody.position.z=struct.unpack("f", self.io.read(4))[0]
874             rigidbody.rotation.x=struct.unpack("f", self.io.read(4))[0]
875             rigidbody.rotation.y=struct.unpack("f", self.io.read(4))[0]
876             rigidbody.rotation.z=struct.unpack("f", self.io.read(4))[0]
877             rigidbody.weight=struct.unpack("f", self.io.read(4))[0]
878             rigidbody.linearDamping=struct.unpack("f", self.io.read(4))[0]
879             rigidbody.angularDamping=struct.unpack("f", self.io.read(4))[0]
880             rigidbody.restitution=struct.unpack("f", self.io.read(4))[0]
881             rigidbody.friction=struct.unpack("f", self.io.read(4))[0]
882             rigidbody.processType=struct.unpack("B", self.io.read(1))[0]
883             self.rigidbodies.append(rigidbody)
884         self._check_position()
885
886         # ジョイントリスト
887         count = struct.unpack("I", self.io.read(4))[0]
888         for i in range(count):
889             name=truncate_zero(struct.unpack("20s", self.io.read(20))[0])
890             constraint=Constraint(name)
891             constraint.rigidA=struct.unpack("I", self.io.read(4))[0]
892             constraint.rigidB=struct.unpack("I", self.io.read(4))[0]
893             constraint.pos.x=struct.unpack("f", self.io.read(4))[0]
894             constraint.pos.y=struct.unpack("f", self.io.read(4))[0]
895             constraint.pos.z=struct.unpack("f", self.io.read(4))[0]
896             constraint.rot.x=struct.unpack("f", self.io.read(4))[0]
897             constraint.rot.y=struct.unpack("f", self.io.read(4))[0]
898             constraint.rot.z=struct.unpack("f", self.io.read(4))[0]
899             constraint.constraintPosMin.x=struct.unpack("f", self.io.read(4))[0]
900             constraint.constraintPosMin.y=struct.unpack("f", self.io.read(4))[0]
901             constraint.constraintPosMin.z=struct.unpack("f", self.io.read(4))[0]
902             constraint.constraintPosMax.x=struct.unpack("f", self.io.read(4))[0]
903             constraint.constraintPosMax.y=struct.unpack("f", self.io.read(4))[0]
904             constraint.constraintPosMax.z=struct.unpack("f", self.io.read(4))[0]
905             constraint.constraintRotMin.x=struct.unpack("f", self.io.read(4))[0]
906             constraint.constraintRotMin.y=struct.unpack("f", self.io.read(4))[0]
907             constraint.constraintRotMin.z=struct.unpack("f", self.io.read(4))[0]
908             constraint.constraintRotMax.x=struct.unpack("f", self.io.read(4))[0]
909             constraint.constraintRotMax.y=struct.unpack("f", self.io.read(4))[0]
910             constraint.constraintRotMax.z=struct.unpack("f", self.io.read(4))[0]
911             constraint.springPos.x=struct.unpack("f", self.io.read(4))[0]
912             constraint.springPos.y=struct.unpack("f", self.io.read(4))[0]
913             constraint.springPos.z=struct.unpack("f", self.io.read(4))[0]
914             constraint.springRot.x=struct.unpack("f", self.io.read(4))[0]
915             constraint.springRot.y=struct.unpack("f", self.io.read(4))[0]
916             constraint.springRot.z=struct.unpack("f", self.io.read(4))[0]
917             self.constraints.append(constraint)
918         self._check_position()
919
920         return True
921