OSDN Git Service

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