OSDN Git Service

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