OSDN Git Service

60ec6e073cf4216f2cfa0a40d9dbbb6286ce7b6d
[meshio/meshio.git] / swig / blender24 / pmd_export.py
1 #!BPY
2 # coding: utf-8
3 """
4  Name: 'MikuMikuDance model (.pmd)...'
5  Blender: 248
6  Group: 'Export'
7  Tooltip: 'Export PMD file for MikuMikuDance.'
8 """
9 __author__= ["ousttrue"]
10 __version__= "0.2"
11 __url__=()
12 __bpydoc__="""
13 0.1 20100318 first implementation.
14 0.2 20100519 refactoring. use C extension.
15 """
16 import Blender
17 import os
18 import sys
19
20 from meshio import pmd, englishmap
21
22
23 # ファイルシステムの文字コード
24 # 改造版との共用のため
25 FS_ENCODING=sys.getfilesystemencoding()
26 if os.path.exists(os.path.dirname(sys.argv[0])+"/utf8"):
27     INTERNAL_ENCODING='utf-8'
28 else:
29     INTERNAL_ENCODING=FS_ENCODING
30
31
32 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
33 EXTENSION = u'.pmd'
34
35
36 class Node(object):
37     __slots__=['o', 'children']
38     def __init__(self, o):
39         self.o=o
40         self.children=[]
41
42
43 ###############################################################################
44 # Blenderのメッシュをワンスキンメッシュ化する
45 ###############################################################################
46 def near(x, y, EPSILON=1e-5):
47     d=x-y
48     return d>=-EPSILON and d<=EPSILON
49
50
51 class VertexKey(object):
52     """
53     重複頂点の検索キー
54     """
55     __slots__=[
56             'x', 'y', 'z', # 位置
57             'nx', 'ny', 'nz', # 法線
58             'u', 'v', # uv
59             ]
60
61     def __init__(self, x, y, z, nx, ny, nz, u, v):
62         self.x=x
63         self.y=y
64         self.z=z
65         self.nx=nx
66         self.ny=ny
67         self.nz=nz
68         self.u=u
69         self.v=v
70
71     def __str__(self):
72         return "<vkey: %f, %f, %f, %f, %f, %f, %f, %f>" % (
73                 self.x, self.y, self.z, self.nx, self.ny, self.nz, self.u, self.v)
74
75     def __hash__(self):
76         #return int((self.x+self.y+self.z+self.nx+self.ny+self.nz+self.u+self.v)*100)
77         return int((self.x+self.y+self.z)*100)
78
79     def __eq__(self, rhs):
80         #return near(self.x, rhs.x) and near(self.y, rhs.y) and near(self.z, rhs.z) and near(self.nx, rhs.nx) and near(self.ny, rhs.ny) and near(self.nz, rhs.nz) and near(self.u, rhs.u) and near(self.v, rhs.v)
81         #return near(self.x, rhs.x) and near(self.y, rhs.y) and near(self.z, rhs.z)
82         return self.x==rhs.x and self.y==rhs.y and self.z==rhs.z
83
84
85 class VertexArray(object):
86     """
87     頂点配列
88     """
89     def __init__(self):
90         # マテリアル毎に分割したインデックス配列
91         self.indexArrays={}
92         # 頂点属性
93         self.vertices=[]
94         self.normals=[]
95         self.uvs=[]
96         # skinning属性
97         self.b0=[]
98         self.b1=[]
99         self.weight=[]
100
101         self.vertexMap={}
102         self.indexMap={}
103
104     def __str__(self):
105         return "<VertexArray %d vertices, %d indexArrays>" % (
106                 len(self.vertices), len(self.indexArrays))
107
108     def zip(self):
109         return zip(
110                 self.vertices, self.normals, self.uvs,
111                 self.b0, self.b1, self.weight)
112
113     def __getIndex(self, base_index, pos, normal, uv, b0, b1, weight0):
114         """
115         頂点属性からその頂点のインデックスを得る
116         """
117         key=VertexKey(
118                 pos[0], pos[1], pos[2],
119                 normal[0], normal[1], normal[2],
120                 uv[0], uv[1])
121         if key in self.vertexMap:
122             # 同じ頂点を登録済み
123             index=self.vertexMap[key]
124         else:
125             index=len(self.vertices)
126             # 新規頂点
127             self.vertexMap[key]=index
128             # append...
129             self.vertices.append((pos.x, pos.y, pos.z))
130             self.normals.append((normal.x, normal.y, normal.z))
131             self.uvs.append((uv.x, uv.y))
132             self.b0.append(b0)
133             self.b1.append(b1)
134             self.weight.append(weight0)
135             
136         # indexのマッピングを保存する
137         if not base_index in self.indexMap:
138             self.indexMap[base_index]=set()
139         self.indexMap[base_index].add(index)
140
141         assert(index<=65535)
142         return index
143
144     def getMappedIndices(self, base_index):
145         return self.indexMap[base_index]
146
147     def addTriangle(self,
148             material,
149             base_index0, base_index1, base_index2,
150             pos0, pos1, pos2,
151             n0, n1, n2,
152             uv0, uv1, uv2,
153             b0_0, b0_1, b0_2,
154             b1_0, b1_1, b1_2,
155             weight0, weight1, weight2
156             ):
157         if not material in self.indexArrays:
158             self.indexArrays[material]=[]
159
160         index0=self.__getIndex(base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
161         index1=self.__getIndex(base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
162         index2=self.__getIndex(base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
163
164         self.indexArrays[material]+=[index0, index1, index2]
165
166
167 class Morph(object):
168     __slots__=['name', 'type', 'offsets']
169     def __init__(self, name, type):
170         self.name=name
171         self.type=type
172         self.offsets=[]
173
174     def add(self, index, offset):
175         self.offsets.append((index, offset))
176
177     def sort(self):
178         self.offsets.sort(lambda l, r: l[0]-r[0])
179
180
181 class IKSolver(object):
182     __slots__=['target', 'effector', 'length', 'iterations', 'weight']
183     def __init__(self, target, effector, length, iterations, weight):
184         self.target=target
185         self.effector=effector
186         self.length=length
187         self.iterations=iterations
188         self.weight=weight
189
190
191 class OneSkinMesh(object):
192     __slots__=['vertexArray', 'morphList']
193     def __init__(self):
194         self.vertexArray=VertexArray()
195         self.morphList=[]
196
197     def __str__(self):
198         return "<OneSkinMesh %s, morph:%d>" % (
199                 self.vertexArray,
200                 len(self.morphList))
201
202     def addMesh(self, obj):
203         if obj.restrictDisplay:
204             # 非表示
205             return
206
207         print("export", obj.name)
208
209         ############################################################
210         # bone weight
211         ############################################################
212         mesh=obj.getData(mesh=True)
213         weightMap={}
214         secondWeightMap={}
215         for name in mesh.getVertGroupNames():
216             for i, w in mesh.getVertsFromGroup(name, 1):
217                 if w>0:
218                     if i in weightMap:
219                         if i in secondWeightMap:
220                             # 上位2つのweightを採用する
221                             if w<secondWeightMap[i]:
222                                 pass
223                             elif w<weightMap[i]:
224                                 # 2つ目を入れ替え
225                                 secondWeightMap[i]=(name, w)
226                             else:
227                                 # 1つ目を入れ替え
228                                 weightMap[i]=(name, w)
229                         else:
230                             if w>weightMap[i][1]:
231                                 # 多い方をweightMapに
232                                 secondWeightMap[i]=weightMap[i]
233                                 weightMap[i]=(name, w)
234                             else:
235                                 secondWeightMap[i]=(name, w)
236                     else:
237                         weightMap[i]=(name, w)
238
239         # 合計値が1になるようにする
240         for i in xrange(len(mesh.verts)):
241         #for i, name_weight in weightMap.items():
242             if i in secondWeightMap:
243                 secondWeightMap[i]=(secondWeightMap[i][0], 1.0-weightMap[i][1])
244             elif i in weightMap:
245                 weightMap[i]=(weightMap[i][0], 1.0)
246                 secondWeightMap[i]=("", 0)
247             else:
248                 print "no weight vertex"
249                 weightMap[i]=("", 0)
250                 secondWeightMap[i]=("", 0)
251                 
252
253         ############################################################
254         # faces
255         # 新たにメッシュを生成する
256         ############################################################
257         mesh=Blender.Mesh.New()
258         # not applied modifiers
259         mesh.getFromObject(obj.name, 1)
260         # apply object transform
261         mesh.transform(obj.getMatrix())
262         if len(mesh.verts)==0:
263             return
264
265         for face in mesh.faces:
266             faceVertexCount=len(face.v)
267             material=mesh.materials[face.mat]
268             if faceVertexCount==3:
269                 v0=face.v[0]
270                 v1=face.v[1]
271                 v2=face.v[2]
272                 # triangle
273                 self.vertexArray.addTriangle(
274                         material.name,
275                         v0.index, v1.index, v2.index,
276                         v0.co, v1.co, v2.co,
277                         # ToDo vertex normal
278                         #v0.no, v1.no, v2.no,
279                         face.no, face.no, face.no,
280                         face.uv[0], face.uv[1], face.uv[2],
281                         weightMap[v0.index][0],
282                         weightMap[v1.index][0],
283                         weightMap[v2.index][0],
284                         secondWeightMap[v0.index][0],
285                         secondWeightMap[v1.index][0],
286                         secondWeightMap[v2.index][0],
287                         weightMap[v0.index][1],
288                         weightMap[v1.index][1],
289                         weightMap[v2.index][1]
290                         )
291             elif faceVertexCount==4:
292                 v0=face.v[0]
293                 v1=face.v[1]
294                 v2=face.v[2]
295                 v3=face.v[3]
296                 # quadrangle
297                 self.vertexArray.addTriangle(
298                         material.name,
299                         v0.index, v1.index, v2.index,
300                         v0.co, v1.co, v2.co,
301                         #v0.no, v1.no, v2.no,
302                         face.no, face.no, face.no,
303                         face.uv[0], face.uv[1], face.uv[2],
304                         weightMap[v0.index][0],
305                         weightMap[v1.index][0],
306                         weightMap[v2.index][0],
307                         secondWeightMap[v0.index][0],
308                         secondWeightMap[v1.index][0],
309                         secondWeightMap[v2.index][0],
310                         weightMap[v0.index][1],
311                         weightMap[v1.index][1],
312                         weightMap[v2.index][1]
313                         )
314                 self.vertexArray.addTriangle(
315                         material.name,
316                         v2.index, v3.index, v0.index,
317                         v2.co, v3.co, v0.co,
318                         #v2.no, v3.no, v0.no,
319                         face.no, face.no, face.no,
320                         face.uv[2], face.uv[3], face.uv[0],
321                         weightMap[v2.index][0],
322                         weightMap[v3.index][0],
323                         weightMap[v0.index][0],
324                         secondWeightMap[v2.index][0],
325                         secondWeightMap[v3.index][0],
326                         secondWeightMap[v0.index][0],
327                         weightMap[v2.index][1],
328                         weightMap[v3.index][1],
329                         weightMap[v0.index][1]
330                         )
331
332         ############################################################
333         # skin
334         ############################################################
335         # base
336         indexRelativeMap={}
337         blenderMesh=obj.getData(mesh=True)
338         baseMorph=None
339         if blenderMesh.key:
340             for b in blenderMesh.key.blocks:
341                 if b.name=='Basis':
342                     print(b.name)
343                     baseMorph=self.__getOrCreateMorph('base', 0)
344                     relativeIndex=0
345                     basis=b
346                     for index in blenderMesh.getVertsFromGroup(
347                             MMD_SHAPE_GROUP_NAME):
348                         v=b.data[index]
349                         pos=[v[0], v[1], v[2]]
350                         indices=self.vertexArray.getMappedIndices(index)
351                         for i in indices:
352                             baseMorph.add(i, pos)
353                             indexRelativeMap[i]=relativeIndex
354                             relativeIndex+=1
355                     break
356             print(len(baseMorph.offsets))
357             baseMorph.sort()
358             assert(basis)
359
360             # shape keys
361             vg=obj.getData(mesh=True).getVertsFromGroup(
362                         MMD_SHAPE_GROUP_NAME)
363             for b in obj.getData(mesh=True).key.blocks:
364                 if b.name=='Basis':
365                     continue
366
367                 print(b.name)
368                 morph=self.__getOrCreateMorph(b.name, 4)
369                 for index, src, dst in zip(
370                         xrange(len(blenderMesh.verts)),
371                         basis.data,
372                         b.data):
373                     offset=[dst[0]-src[0], dst[1]-src[1], dst[2]-src[2]]
374                     if index in vg:
375                         indices=self.vertexArray.getMappedIndices(index)
376                         for i in indices:
377                             morph.add(indexRelativeMap[i], offset)
378                 assert(len(morph.offsets)==len(baseMorph.offsets))
379
380     def __getOrCreateMorph(self, name, type):
381         for m in self.morphList:
382             if m.name==name:
383                 return m
384         m=Morph(name, type)
385         self.morphList.append(m)
386         return m
387
388     def getVertexCount(self):
389         return len(self.vertexArray.vertices)
390
391
392 class Bone(object):
393     __slots__=['index', 'name', 
394             'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
395     def __init__(self, name, pos, tail):
396         self.index=-1
397         self.name=name
398         self.pos=pos
399         self.tail=tail
400         self.parent_index=None
401         self.tail_index=None
402         self.type=0
403         self.isConnect=False
404
405     def __eq__(self, rhs):
406         return self.index==rhs.index
407
408     def __str__(self):
409         return "<Bone %s %d>" % (self.name, self.type)
410
411 class BoneBuilder(object):
412     __slots__=['bones', 'boneMap', 'ik_list']
413     def __init__(self):
414         self.bones=[]
415         self.boneMap={}
416         self.ik_list=[]
417
418     def build(self, armatureObj):
419         if not armatureObj:
420             return
421
422         print("gather bones")
423         armature=armatureObj.getData()
424         for b in armature.bones.values():
425             if b.name=='center':
426                 # root bone
427                 bone=Bone(b.name, 
428                         b.head['ARMATURESPACE'][0:3], 
429                         b.tail['ARMATURESPACE'][0:3])
430                 self.__addBone(bone)
431                 self.__getBone(bone, b)
432
433         for b in armature.bones.values():
434             if not b.parent and b.name!='center':
435                 # root bone
436                 bone=Bone(b.name, 
437                         b.head['ARMATURESPACE'][0:3], 
438                         b.tail['ARMATURESPACE'][0:3])
439                 self.__addBone(bone)
440                 self.__getBone(bone, b)
441
442         print("check connection")
443         for b in armature.bones.values():
444             if not b.parent:
445                 self.__checkConnection(b, None)
446
447         print("create tail")
448         before=self.bones[:]
449         for b in before:
450             if not b.isConnect:
451                 # 末端の非表示ボーン
452                 if not b.tail:
453                     print(b.name)
454                     assert(False)
455                 bone=Bone(b.name+'_t', b.tail, None)
456                 bone.type=7
457                 self.__addBone(bone)
458                 bone.parent_index=b.index
459                 b.tail_index=bone.index
460
461         print("gather ik")
462         pose = armatureObj.getPose()
463         cSetting=Blender.Constraint.Settings
464         for b in pose.bones.values():
465             for c in b.constraints:
466                 if c.type==Blender.Constraint.Type.IKSOLVER:
467                     ####################
468                     # IK effector
469                     ####################
470                     # IK 接続先
471                     link=self.__boneByName(b.name)
472                     link_tail=self.bones[link.tail_index]
473                     if link_tail.type==7:
474                         # replace ...先 to IK接続先
475                         link_tail.type=6
476
477                     # IK chain
478                     e=b
479                     chainLength=c[cSetting.CHAINLEN]
480                     for i in range(chainLength):
481                         # IK影響下
482                         self.__boneByName(e.name).type=4
483                         e=e.parent
484
485                     ####################
486                     # IK target
487                     ####################
488                     assert(c[cSetting.TARGET]==armatureObj)
489                     target=self.__boneByName(
490                             c[Blender.Constraint.Settings.BONE])
491                     target.type=2
492
493                     self.ik_list.append(
494                             IKSolver(target, link_tail, chainLength, 
495                                 c[cSetting.ITERATIONS], c.influence))
496                     # 非末端IK
497                     target_tail=self.bones[target.tail_index]
498                     if target_tail.type!=7:
499                         target_bone=armature.bones[target.name]
500                         bone=Bone(target.name+'_t', 
501                                 target_bone.tail['ARMATURESPACE'][0:3],
502                                 None)
503                         bone.type=7
504                         self.__addBone(bone)
505                         bone.parent_index=target.index
506                         target.tail_index=bone.index
507
508     def __checkConnection(self, b, p):
509         if Blender.Armature.CONNECTED in b.options:
510             parent=self.__boneByName(p.name)
511             parent.isConnect=True
512
513         for c in b.children:
514             self.__checkConnection(c, b)
515
516     def sortBy(self, boneMap):
517         """
518         boneMap順に並べ替える
519         """
520         original=self.bones[:]
521         def getIndex(bone):
522             for i, k_v in enumerate(boneMap):
523                 if k_v[0]==bone.name:
524                     return i
525             print(bone)
526
527         self.bones.sort(lambda l, r: getIndex(l)-getIndex(r))
528         sortMap={}
529         for i, b in enumerate(self.bones):
530             src=original.index(b)
531             sortMap[src]=i
532         for b in self.bones:
533             b.index=sortMap[b.index]
534             if b.parent_index:
535                 b.parent_index=sortMap[b.parent_index]
536             if b.tail_index:
537                 b.tail_index=sortMap[b.tail_index]
538
539     def getIndex(self, bone):
540         for i, b in enumerate(self.bones):
541             if b==bone:
542                 return i
543         assert(false)
544
545     def __boneByName(self, name):
546         return self.bones[self.boneMap[name]]
547                     
548     def __getBone(self, parent, b):
549         for i, c in enumerate(b.children):
550             bone=Bone(c.name, 
551                     c.head['ARMATURESPACE'][0:3], 
552                     c.tail['ARMATURESPACE'][0:3])
553             self.__addBone(bone)
554             if parent:
555                 bone.parent_index=parent.index
556                 if i==0:
557                     parent.tail_index=bone.index
558             self.__getBone(bone, c)
559
560     def __addBone(self, bone):
561         bone.index=len(self.bones)
562         self.bones.append(bone)
563         self.boneMap[bone.name]=bone.index
564
565
566 class PmdExporter(object):
567
568     def __init__(self):
569         self.armatureObj=None
570
571     def setup(self, scene):
572         # 木構造を構築する
573         object_node_map={}
574         for o in scene.objects:
575             object_node_map[o]=Node(o)
576         for node in object_node_map.values():
577             if node.o.parent:
578                 object_node_map[node.o.parent].children.append(node)
579         # ルートを得る
580         root=object_node_map[scene.objects.active]
581
582         # ワンスキンメッシュを作る
583         self.oneSkinMesh=OneSkinMesh()
584         self.__createOneSkinMesh(root)
585         print(self.oneSkinMesh)
586         self.name=root.o.name
587
588         # skeleton
589         self.builder=BoneBuilder()
590         self.builder.build(self.armatureObj)
591         self.builder.sortBy(englishmap.boneMap)
592
593     def __createOneSkinMesh(self, node):
594         ############################################################
595         # search armature modifier
596         ############################################################
597         for m in node.o.modifiers:
598             if m.name=="Armature":
599                 armatureObj=m[Blender.Modifier.Settings.OBJECT]
600                 if not self.armatureObj:
601                     self.armatureObj=armatureObj
602                 elif self.armatureObj!=armatureObj:
603                     print "warning! found multiple armature. ignored.", armatureObj.name
604
605         if node.o.getType()=='Mesh':
606             self.oneSkinMesh.addMesh(node.o)
607
608         for child in node.children:
609             self.__createOneSkinMesh(child)
610
611     def write(self, path):
612         io=pmd.IO()
613         io.name=self.name
614         io.comment="blender export"
615         io.version=1.0
616
617         # 頂点
618         for pos, normal, uv, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
619             # convert right-handed z-up to left-handed y-up
620             v=io.addVertex()
621             v.pos.x=pos[0]
622             v.pos.y=pos[2]
623             v.pos.z=pos[1]
624             v.normal.x=normal[0]
625             v.normal.y=normal[2]
626             v.normal.z=normal[1]
627             v.uv.x=uv[0]
628             v.uv.y=uv[1]
629             v.bone0=self.builder.boneMap[b0] if b0 in self.builder.boneMap else 0
630             v.bone1=self.builder.boneMap[b1] if b1 in self.builder.boneMap else 0
631             v.weight0=int(100*weight)
632             v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
633
634         # 面とマテリアル
635         vertexCount=self.oneSkinMesh.getVertexCount()
636         for m, indices in self.oneSkinMesh.vertexArray.indexArrays.items():
637             m=Blender.Material.Get(m)
638             # マテリアル
639             material=io.addMaterial()
640             material.diffuse.r=m.R
641             material.diffuse.g=m.G
642             material.diffuse.b=m.B
643             material.diffuse.a=m.alpha
644             material.sinness=0 if m.spec<1e-5 else m.spec*10
645             material.specular.r=m.specR
646             material.specular.g=m.specG
647             material.specular.b=m.specB
648             material.ambient.r=m.mirR
649             material.ambient.g=m.mirG
650             material.ambient.b=m.mirB
651             material.vertex_count=len(indices)
652             material.toon_index=0
653             material.flag=1 if m.enableSSS else 0
654             # ToDo
655             material.texture=""
656             # 面
657             for i in indices:
658                 assert(i<vertexCount)
659             for i in xrange(0, len(indices), 3):
660                 # reverse triangle
661                 io.indices.append(indices[i+2])
662                 io.indices.append(indices[i+1])
663                 io.indices.append(indices[i])
664
665                 #io.indices.append(indices[i])
666                 #io.indices.append(indices[i+1])
667                 #io.indices.append(indices[i+2])
668
669         # bones
670         for b in self.builder.bones:
671             if b.name.endswith("_t"):
672                 if b.name.startswith("arm twist1_") or b.name.startswith("arm twist2_"):
673                     # skip
674                     print "skip %s" % b.name
675                     continue
676
677             bone=io.addBone()
678
679             v=englishmap.getUnicodeBoneName(b.name)
680             assert(v)
681             cp932=v[1].encode('cp932')
682             bone_name="%s\n" % cp932
683             assert(len(bone_name)<20)
684             bone.name=bone_name
685
686             bone_english_name="%s\n" % b.name
687             assert(len(bone_english_name)<20)
688             bone.english_name=bone_english_name
689
690             if len(v)>=3:
691                 bone.type=v[2]
692             else:
693                 bone.type=b.type
694             bone.parent_index=b.parent_index if b.parent_index!=None else 0xFFFF
695
696             if b.tail_index!=None:
697                 if bone.type==9:
698                     bone.tail_index=0
699                 else:
700                     bone.tail_index=b.tail_index
701             else:
702                 bone.tail_index=0
703             # ToDo
704             bone.ik_index=0xFFFF
705             # convert right-handed z-up to left-handed y-up
706             bone.pos.x=b.pos[0]
707             bone.pos.y=b.pos[2]
708             bone.pos.z=b.pos[1]
709
710         # IK
711         for ik in self.builder.ik_list:
712             solver=io.addIK()
713             solver.index=self.builder.getIndex(ik.target)
714             solver.target=self.builder.getIndex(ik.effector)
715             solver.length=ik.length
716             b=self.builder.bones[ik.effector.parent_index]
717             for i in xrange(solver.length):
718                 solver.children.append(self.builder.getIndex(b))
719                 b=self.builder.bones[b.parent_index]
720             solver.iterations=ik.iterations
721             solver.weight=ik.weight
722
723         # 表情
724         for i, m in enumerate(self.oneSkinMesh.morphList):
725             # morph
726             morph=io.addMorph()
727
728             unicode=englishmap.getUnicodeSkinName(m.name)
729             if unicode:
730                 cp932=unicode.encode('cp932')
731             else:
732                 cp932=m.name
733             morph.name="%s\n" % cp932
734
735             morph.english_name="%s\n" % m.name
736             morph.type=m.type
737             for index, offset in m.offsets:
738                 # convert right-handed z-up to left-handed y-up
739                 morph.append(index, offset[0], offset[2], offset[1])
740             morph.vertex_count=len(m.offsets)
741
742             # 表情枠
743             if i>0:
744                 io.face_list.append(i)
745
746         # ボーン表示枠
747         boneDisplayName=io.addBoneDisplayName()
748         boneDisplayName.name="bones\n"
749         boneDisplayName.english_name="bones\n"
750         displayIndex=1
751         for i, b in enumerate(self.builder.bones):
752             if b.type in [6, 7]:
753                 io.addBoneDisplay(i, displayIndex)
754
755         # English
756         io.english_name="blender export"
757         io.english_coment="blender export"
758
759         for i in range(10):
760             io.getToonTexture(i).name="toon%02d.bmp\n" % i
761
762         # 書き込み
763         return io.write(path.encode(FS_ENCODING))
764
765
766 def export_pmd(filename):
767     filename=filename.decode(INTERNAL_ENCODING)
768
769     Blender.Window.WaitCursor(1) 
770     t = Blender.sys.time() 
771
772     if not filename.lower().endswith(EXTENSION):
773         filename += EXTENSION
774     print "pmd exporter: %s" % filename
775
776     exporter=PmdExporter()
777
778     # 情報収集
779     exporter.setup(Blender.Scene.GetCurrent())
780
781     # 出力
782     exporter.write(filename)
783
784     print 'finished in %.2f seconds' % (Blender.sys.time()-t) 
785     Blender.Redraw()
786     Blender.Window.WaitCursor(0) 
787  
788
789 Blender.Window.FileSelector(
790         export_pmd,
791         'Export Metasequoia PMD',
792         Blender.sys.makename(ext=EXTENSION))
793