OSDN Git Service

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