OSDN Git Service

fix bone tail.
[meshio/meshio.git] / swig / blender / pmd_export.py
index d473d7a..503cc99 100644 (file)
@@ -46,6 +46,7 @@ CONSTRAINT_ROT_MIN='const_rot_min'
 CONSTRAINT_ROT_MAX='const_rot_max'
 CONSTRAINT_SPRING_POS='const_spring_pos'
 CONSTRAINT_SPRING_ROT='const_spring_rot'
+TOON_TEXTURE_OBJECT='ToonTextures'
 
 
 ###############################################################################
@@ -87,6 +88,10 @@ if isBlender24():
         # flag
         material.flag=1 if m.enableSSS else 0
 
+    def toCP932(s):
+        return s
+
+
 else:
     # for 2.5
     import bpy
@@ -115,6 +120,12 @@ else:
         material.ambient.b=m.mirror_color[2]
         # flag
         material.flag=1 if m.subsurface_scattering.enabled else 0
+        # toon
+        material.toon_index=7
+
+    def toCP932(s):
+        return s.encode('cp932')
+
 
 class Node(object):
     __slots__=['o', 'children']
@@ -160,7 +171,8 @@ class VertexKey(object):
 
     def __hash__(self):
         #return int((self.x+self.y+self.z+self.nx+self.ny+self.nz+self.u+self.v)*100)
-        return int((self.x+self.y+self.z)*100)
+        #return int((self.x+self.y+self.z)*100)
+        return self.index*100+self.obj
 
     def __eq__(self, rhs):
         #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)
@@ -173,6 +185,15 @@ class VertexArray(object):
     """
     頂点配列
     """
+    __slots__=[
+            'indexArrays',
+            'vertices',
+            'normals',
+            'uvs',
+            'b0', 'b1', 'weight',
+            'vertexMap',
+            'objectMap',
+            ]
     def __init__(self):
         # マテリアル毎に分割したインデックス配列
         self.indexArrays={}
@@ -186,7 +207,8 @@ class VertexArray(object):
         self.weight=[]
 
         self.vertexMap={}
-        self.indexMap={}
+        #self.indexMap={}
+        self.objectMap={}
 
     def __str__(self):
         return "<VertexArray %d vertices, %d indexArrays>" % (
@@ -228,18 +250,26 @@ class VertexArray(object):
             self.weight.append(weight0)
             
         # indexのマッピングを保存する
-        if not base_index in self.indexMap:
-            self.indexMap[base_index]=set()
-        self.indexMap[base_index].add(index)
+        #if not base_index in self.indexMap:
+        #    self.indexMap[base_index]=set()
+        #self.indexMap[base_index].add(index)
 
         assert(index<=65535)
         return index
 
-    def getMappedIndices(self, base_index):
+    def getMappedIndex(self, obj_name, base_index):
+        key=VertexKey(
+                self.objectMap[obj_name], base_index,
+                None, None, None,
+                None, None, None,
+                None, None)
+        return self.vertexMap[key]
+
+    def getMappedIndices(self, obj, base_index):
         return self.indexMap[base_index]
 
     def addTriangle(self,
-            obj, material,
+            object_name, material,
             base_index0, base_index1, base_index2,
             pos0, pos1, pos2,
             n0, n1, n2,
@@ -248,13 +278,17 @@ class VertexArray(object):
             b1_0, b1_1, b1_2,
             weight0, weight1, weight2
             ):
-        if not material in self.indexArrays:
-            self.indexArrays[material]=[]
-
+        if object_name in self.objectMap:
+            obj=self.objectMap[object_name]
+        else:
+            obj=len(self.objectMap)
+            self.objectMap[object_name]=obj
         index0=self.__getIndex(obj, base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
         index1=self.__getIndex(obj, base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
         index2=self.__getIndex(obj, base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
 
+        if not material in self.indexArrays:
+            self.indexArrays[material]=[]
         self.indexArrays[material]+=[index0, index1, index2]
 
 
@@ -288,7 +322,7 @@ class IKSolver(object):
 
 
 class OneSkinMesh(object):
-    __slots__=['obj_index', 'scene', 'vertexArray', 'morphList', 
+    __slots__=['scene', 'vertexArray', 'morphList', 
             'rigidbodies',
             'constraints',
             ]
@@ -298,7 +332,6 @@ class OneSkinMesh(object):
         self.rigidbodies=[]
         self.constraints=[]
         self.scene=scene
-        self.obj_index=0
 
     def __str__(self):
         return "<OneSkinMesh %s, morph:%d>" % (
@@ -307,24 +340,14 @@ class OneSkinMesh(object):
 
     def addMesh(self, obj):
         if bl.object.isVisible(obj):
-            # 非表示
             return
         self.__mesh(obj)
         self.__skin(obj)
         self.__rigidbody(obj)
         self.__constraint(obj)
 
-    def __mesh(self, obj):
-        if isBlender24():
-            pass
-        else:
-            if RIGID_SHAPE_TYPE in obj:
-                return
-            if CONSTRAINT_A in obj:
-                return
-
-        print("export", obj.name)
-        mesh=bl.object.getData(obj)
+    def __getWeightMap(self, obj, mesh):
+        # bone weight
         weightMap={}
         secondWeightMap={}
         def setWeight(i, name, w):
@@ -332,9 +355,9 @@ class OneSkinMesh(object):
                 if i in weightMap:
                     if i in secondWeightMap:
                         # 上位2つのweightを採用する
-                        if w<secondWeightMap[i]:
+                        if w<secondWeightMap[i][1]:
                             pass
-                        elif w<weightMap[i]:
+                        elif w<weightMap[i][1]:
                             # 2つ目を入れ替え
                             secondWeightMap[i]=(name, w)
                         else:
@@ -356,8 +379,11 @@ class OneSkinMesh(object):
                     setWeight(i, name, w)
         else:
             for i, v in enumerate(mesh.verts):
-                for g in v.groups:
-                    setWeight(i, obj.vertex_groups[g.group].name, g.weight)
+                if len(v.groups)>0:
+                    for g in v.groups:
+                        setWeight(i, obj.vertex_groups[g.group].name, g.weight)
+                else:
+                    setWeight(i, obj.vertex_groups[0].name, 1)
 
         # 合計値が1になるようにする
         for i in xrange(len(mesh.verts)):
@@ -372,21 +398,21 @@ class OneSkinMesh(object):
                 weightMap[i]=("", 0)
                 secondWeightMap[i]=("", 0)
 
-        # メッシュのコピーを生成してオブジェクトの行列を適用する
-        copyMesh, copyObj=bl.object.duplicate(obj)
-        if len(copyMesh.verts)==0:
-            return
+        return weightMap, secondWeightMap
 
-        for i, face in enumerate(copyMesh.faces):
+    def __processFaces(self, obj_name, mesh, weightMap, secondWeightMap):
+        # 各面の処理
+        for i, face in enumerate(mesh.faces):
             faceVertexCount=bl.face.getVertexCount(face)
-            material=copyMesh.materials[bl.face.getMaterialIndex(face)]
-            v=[copyMesh.verts[index] for index in bl.face.getVertices(face)]
-            uv=bl.mesh.getFaceUV(copyMesh, i, face)
+            material=mesh.materials[bl.face.getMaterialIndex(face)]
+            v=[mesh.verts[index] for index in bl.face.getVertices(face)]
+            uv=bl.mesh.getFaceUV(
+                    mesh, i, face, bl.face.getVertexCount(face))
             # flip triangle
             if faceVertexCount==3:
                 # triangle
                 self.vertexArray.addTriangle(
-                        self.obj_index, material.name,
+                        obj_name, material.name,
                         v[2].index, 
                         v[1].index, 
                         v[0].index,
@@ -414,7 +440,7 @@ class OneSkinMesh(object):
             elif faceVertexCount==4:
                 # quadrangle
                 self.vertexArray.addTriangle(
-                        self.obj_index, material.name,
+                        obj_name, material.name,
                         v[2].index, 
                         v[1].index, 
                         v[0].index,
@@ -439,7 +465,7 @@ class OneSkinMesh(object):
                         weightMap[v[0].index][1]
                         )
                 self.vertexArray.addTriangle(
-                        self.obj_index, material.name,
+                        obj_name, material.name,
                         v[0].index, 
                         v[3].index, 
                         v[2].index,
@@ -463,8 +489,50 @@ class OneSkinMesh(object):
                         weightMap[v[3].index][1],
                         weightMap[v[2].index][1]
                         )
+
+    def __mesh(self, obj):
+        if isBlender24():
+            pass
+        else:
+            if RIGID_SHAPE_TYPE in obj:
+                return
+            if CONSTRAINT_A in obj:
+                return
+
+        #if not bl.modifier.hasType(obj, 'ARMATURE'):
+        #    return
+
+        bl.message("export: %s" % obj.name)
+
+        # メッシュのコピーを生成してオブジェクトの行列を適用する
+        copyMesh, copyObj=bl.object.duplicate(obj)
+        if len(copyMesh.verts)==0:
+            return
+        # apply transform
+        copyObj.scale=obj.scale
+        bpy.ops.object.scale_apply()
+        copyObj.rotation_euler=obj.rotation_euler
+        bpy.ops.object.rotation_apply()
+        copyObj.location=obj.location
+        bpy.ops.object.location_apply()
+        # apply modifier
+        for m in [m for m in copyObj.modifiers]:
+            if m.type=='SOLIDFY':
+                continue
+            elif m.type=='ARMATURE':
+                continue
+            elif m.type=='MIRROR':
+                bpy.ops.object.modifier_apply(modifier=m.name)
+            else:
+                print(m.tpye)
+
+        weightMap, secondWeightMap=self.__getWeightMap(copyObj, copyMesh)
+        self.__processFaces(obj.name, copyMesh, weightMap, secondWeightMap)
+
         bl.object.delete(copyObj)
-        self.obj_index+=1
+
+    def createEmptyBasicSkin(self):
+        self.__getOrCreateMorph('base', 0)
 
     def __skin(self, obj):
         if not bl.object.hasShapeKey(obj):
@@ -488,15 +556,19 @@ class OneSkinMesh(object):
                 for index in vg:
                     v=bl.shapekey.getByIndex(b, index)
                     pos=[v[0], v[1], v[2]]
-                    indices=self.vertexArray.getMappedIndices(index)
-                    for i in indices:
-                        if i in used:
-                            continue
-                        used.add(i)
-
-                        baseMorph.add(i, pos)
-                        indexRelativeMap[i]=relativeIndex
-                        relativeIndex+=1
+                    i=self.vertexArray.getMappedIndex(obj.name, index)
+                    baseMorph.add(i, pos)
+                    indexRelativeMap[i]=relativeIndex
+                    relativeIndex+=1
+                    #indices=self.vertexArray.getMappedIndices(index)
+                    #for i in indices:
+                    #    if i in used:
+                    #        continue
+                    #    used.add(i)
+
+                    #    baseMorph.add(i, pos)
+                    #    indexRelativeMap[i]=relativeIndex
+                    #    relativeIndex+=1
 
                 break
         assert(basis)
@@ -521,12 +593,18 @@ class OneSkinMesh(object):
                 if offset[0]==0 and offset[1]==0 and offset[2]==0:
                     continue
                 if index in vg:
-                    indices=self.vertexArray.getMappedIndices(index)
-                    for i in indices:
-                        if i in used:
-                            continue
-                        used.add(i) 
-                        morph.add(indexRelativeMap[i], offset)
+                    i=self.vertexArray.getMappedIndex(obj.name, index)
+                    if i in used:
+                        continue
+                    used.add(i) 
+                    morph.add(indexRelativeMap[i], offset)
+                    #indices=self.vertexArray.getMappedIndices(index)
+                    #for i in indices:
+                    #    if i in used:
+                    #        continue
+                    #    used.add(i) 
+                    #    morph.add(indexRelativeMap[i], offset)
+            assert(len(morph.offsets)<len(baseMorph.offsets))
 
         # sort skinmap
         original=self.morphList[:]
@@ -588,18 +666,37 @@ class Bone(object):
         return "<Bone %s %d>" % (self.name, self.type)
 
 class BoneBuilder(object):
-    __slots__=['bones', 'boneMap', 'ik_list', ]
+    __slots__=['bones', 'boneMap', 'ik_list', 'bone_groups',]
     def __init__(self):
         self.bones=[]
         self.boneMap={}
         self.ik_list=[]
+        self.bone_groups=[]
+
+    def getBoneGroup(self, bone):
+        for i, g in enumerate(self.bone_groups):
+            for b in g[1]:
+                if b==bone.name:
+                    return i+1
+        print('no gorup', bone)
+        return 0
 
     def build(self, armatureObj):
         if not armatureObj:
             return
 
-        print("gather bones")
+        bl.message("build skeleton")
         armature=bl.object.getData(armatureObj)
+
+        ####################
+        # bone group
+        ####################
+        for g in bl.object.boneGroups(armatureObj):
+            self.bone_groups.append((g.name, []))
+
+        ####################
+        # get bones
+        ####################
         for b in armature.bones.values():
             if b.name=='center':
                 # root bone
@@ -618,14 +715,19 @@ class BoneBuilder(object):
                 self.__addBone(bone)
                 self.__getBone(bone, b)
 
-        print("check connection")
         for b in armature.bones.values():
             if not b.parent:
                 self.__checkConnection(b, None)
 
-        print("gather ik")
+        ####################
+        # get IK
+        ####################
         pose = bl.object.getPose(armatureObj)
         for b in pose.bones.values():
+            ####################
+            # assing bone group
+            ####################
+            self.__assignBoneGroup(b, b.bone_group)
             for c in b.constraints:
                 if bl.constraint.isIKSolver(c):
                     ####################
@@ -656,6 +758,8 @@ class BoneBuilder(object):
                                 bl.constraint.ikRotationWeight(c)
                                 ))
 
+        ####################
+
         # boneのsort
         self._sortBy()
         self._fix()
@@ -670,6 +774,12 @@ class BoneBuilder(object):
         else:
             self.ik_list.sort(key=getIndex)
 
+    def __assignBoneGroup(self, poseBone, boneGroup):
+        if boneGroup:
+            for g in self.bone_groups:
+                if g[0]==boneGroup.name:
+                    g[1].append(poseBone.name)
+
     def __checkConnection(self, b, p):
         if bl.bone.isConnected(b):
             parent=self.__boneByName(p.name)
@@ -788,12 +898,16 @@ class PmdExporter(object):
         # ワンスキンメッシュを作る
         self.oneSkinMesh=OneSkinMesh(scene)
         self.__createOneSkinMesh(root)
-        print(self.oneSkinMesh)
+        bl.message(self.oneSkinMesh)
+        if len(self.oneSkinMesh.morphList)==0:
+            # create emtpy skin
+            self.oneSkinMesh.createEmptyBasicSkin()
+
         self.name=root.o.name
 
         # skeleton
-        self.builder=BoneBuilder()
-        self.builder.build(self.armatureObj)
+        self.skeleton=BoneBuilder()
+        self.skeleton.build(self.armatureObj)
 
     def __createOneSkinMesh(self, node):
         ############################################################
@@ -814,7 +928,7 @@ class PmdExporter(object):
         for child in node.children:
             self.__createOneSkinMesh(child)
 
-    def write(self, path):
+    def write(self, path, scene):
         io=pmd.IO()
         io.name=self.name
         io.comment="blender export"
@@ -831,15 +945,16 @@ class PmdExporter(object):
             v.normal.y=normal[2]
             v.normal.z=normal[1]
             v.uv.x=uv[0]
-            v.uv.y=uv[1]
-            v.bone0=self.builder.indexByName(b0)
-            v.bone1=self.builder.indexByName(b1)
+            v.uv.y=1.0-uv[1] # reverse vertical
+            v.bone0=self.skeleton.indexByName(b0)
+            v.bone1=self.skeleton.indexByName(b1)
             v.weight0=int(100*weight)
             v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
 
         # 面とマテリアル
         vertexCount=self.oneSkinMesh.getVertexCount()
         for material_name, indices in self.oneSkinMesh.vertexArray.each():
+            #print('material:', material_name)
             m=bl.material.get(material_name)
             # マテリアル
             material=io.addMaterial()
@@ -847,8 +962,12 @@ class PmdExporter(object):
 
             material.vertex_count=len(indices)
             material.toon_index=0
-            # ToDo
-            material.texture=""
+            textures=[os.path.basename(path) 
+                for path in bl.material.eachTexturePath(m)]
+            if len(textures)>0:
+                material.setTexture(toCP932('*'.join(textures)))
+            else:
+                material.setTexture(toCP932(""))
             # 面
             for i in indices:
                 assert(i<vertexCount)
@@ -860,7 +979,7 @@ class PmdExporter(object):
 
         # bones
         boneNameMap={}
-        for i, b in enumerate(self.builder.bones):
+        for i, b in enumerate(self.skeleton.bones):
             bone=io.addBone()
 
             # name
@@ -874,14 +993,14 @@ class PmdExporter(object):
             bone.setName(cp932)
 
             # english name
-            bone_english_name=b.name
+            bone_english_name=toCP932(b.name)
             assert(len(bone_english_name)<20)
-            bone.english_name=bone_english_name
+            bone.setEnglishName(bone_english_name)
 
             if len(v)>=3:
                 # has type
                 if v[2]==5:
-                    b.ik_index=self.builder.indexByName('eyes')
+                    b.ik_index=self.skeleton.indexByName('eyes')
                 bone.type=v[2]
             else:
                 bone.type=b.type
@@ -896,15 +1015,15 @@ class PmdExporter(object):
             bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
 
         # IK
-        for ik in self.builder.ik_list:
+        for ik in self.skeleton.ik_list:
             solver=io.addIK()
-            solver.index=self.builder.getIndex(ik.target)
-            solver.target=self.builder.getIndex(ik.effector)
+            solver.index=self.skeleton.getIndex(ik.target)
+            solver.target=self.skeleton.getIndex(ik.effector)
             solver.length=ik.length
-            b=self.builder.bones[ik.effector.parent_index]
+            b=self.skeleton.bones[ik.effector.parent_index]
             for i in xrange(solver.length):
-                solver.children.append(self.builder.getIndex(b))
-                b=self.builder.bones[b.parent_index]
+                solver.children.append(self.skeleton.getIndex(b))
+                b=self.skeleton.bones[b.parent_index]
             solver.iterations=ik.iterations
             solver.weight=ik.weight
 
@@ -942,36 +1061,50 @@ class PmdExporter(object):
             if m.type==4:
                 io.face_list.append(i)
 
-        # ボーン表示枠
-        def createBoneDisplayName(name, english):
-            boneDisplayName=io.addBoneDisplayName()
-            if isBlender24():
-                boneDisplayName.name=name.decode('utf-8').encode('cp932')
-                boneDisplayName.english_name=english
-            else:
-                boneDisplayName.setName(name.encode('cp932'))
-                boneDisplayName.setEnglishName(english.encode('cp932'))
-        boneDisplayName=createBoneDisplayName("IK\n", "IK\n")
-        boneDisplayName=createBoneDisplayName("体(上)\n", "Body[u]\n")
-        boneDisplayName=createBoneDisplayName("髪\n", "Hair\n")
-        boneDisplayName=createBoneDisplayName("腕\n", "Arms\n")
-        boneDisplayName=createBoneDisplayName("指\n", "Fingers\n")
-        boneDisplayName=createBoneDisplayName("体(下)\n", "Body[l]\n")
-        boneDisplayName=createBoneDisplayName("足\n", "Legs\n")
-        for i, b in enumerate(self.builder.bones):
+        # ボーングループ
+        for g in self.skeleton.bone_groups:
+            boneDisplayName=io.addBoneGroup()
+            # name
+            name=englishmap.getUnicodeBoneGroupName(g[0])
+            if not name:
+                name=g[0]
+            boneDisplayName.setName(toCP932(name+'\n'))
+            # english
+            englishName=g[0]
+            boneDisplayName.setEnglishName(toCP932(englishName+'\n'))
+
+        # ボーングループメンバー
+        for i, b in enumerate(self.skeleton.bones):
             if i==0:
-                continue
+               continue
             if b.type in [6, 7]:
-                continue
-            io.addBoneDisplay(i, getBoneDisplayGroup(b))
+               continue
+            io.addBoneDisplay(i, self.skeleton.getBoneGroup(b))
+
+        #assert(len(io.bones)==len(io.bone_display_list)+1)
 
         # English
-        io.english_name="blender export"
-        io.english_coment="blender export"
+        io.setEnglishName(toCP932("blender export\n"))
+        io.setEnglishComment(toCP932("blender export comment"))
 
         # toon
-        for i in range(10):
-            io.getToonTexture(i).name="toon%02d.bmp\n" % i
+        toonMeshObject=None
+        for o in scene.objects:
+            if o.name.startswith(TOON_TEXTURE_OBJECT):
+                toonMeshObject=o
+                break
+        if toonMeshObject:
+            toonMesh=bl.object.getData(toonMeshObject)
+            toonMaterial=bl.mesh.getMaterial(toonMesh, 0)
+            for i in range(10):
+                t=bl.material.getTexture(toonMaterial, i)
+                if t:
+                    io.getToonTexture(i).setName(toCP932(t.name))
+                else:
+                    io.getToonTexture(i).setName(toCP932("toon%02d.bmp\n" % i))
+        else:
+            for i in range(10):
+                io.getToonTexture(i).setName(toCP932("toon%02d.bmp\n" % i))
 
         # rigid body
         rigidNameMap={}
@@ -982,9 +1115,9 @@ class PmdExporter(object):
             boneIndex=boneNameMap[obj[RIGID_BONE_NAME]]
             if boneIndex==0:
                 boneIndex=0xFFFF
-                bone=self.builder.bones[0]
+                bone=self.skeleton.bones[0]
             else:
-                bone=self.builder.bones[boneIndex]
+                bone=self.skeleton.bones[boneIndex]
             rigidBody.boneIndex=boneIndex
             #rigidBody.position.x=obj[RIGID_LOCATION][0]
             #rigidBody.position.y=obj[RIGID_LOCATION][1]
@@ -1048,63 +1181,10 @@ class PmdExporter(object):
             constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2]
 
         # 書き込み
-        print('write', path)
+        bl.message('write %s' % path)
         return io.write(path)
 
 
-def getBoneDisplayGroup(bone):
-    boneGroups=[
-            [ # IK
-                "necktie IK", "hair IK_L", "hair IK_R", "leg IK_L", "leg IK_R",
-                "toe IK_L", "toe IK_R", 
-                ],
-            [ # 体(上)
-                "upper body", "neck", "head", "eye_L", "eye_R",
-                "necktie1", "necktie2", "necktie3", "eyes", 
-                "eyelight_L", "eyelight_R",
-                ],
-            [ # 髪
-                "front hair1", "front hair2", "front hair3",
-                "hair1_L", "hair2_L", "hair3_L", 
-                "hair4_L", "hair5_L", "hair6_L",
-                "hair1_R", "hair2_R", "hair3_R", 
-                "hair4_R", "hair5_R", "hair6_R",
-                ],
-            [ # 腕
-                "shoulder_L", "arm_L", "arm twist_L", "elbow_L", 
-                "wrist twist_L", "wrist_L", "sleeve_L", 
-                "shoulder_R", "arm_R", "arm twist_R", "elbow_R", 
-                "wrist twist_R", "wrist_R", "sleeve_R", 
-                ],
-            [ # 指
-                "thumb1_L", "thumb2_L", "fore1_L", "fore2_L", "fore3_L",
-                "middle1_L", "middle2_L", "middle3_L",
-                "third1_L", "third2_L", "third3_L",
-                "little1_L", "little2_L", "little3_L",
-                "thumb1_R", "thumb2_R", "fore1_R", "fore2_R", "fore3_R",
-                "middle1_R", "middle2_R", "middle3_R",
-                "third1_R", "third2_R", "third3_R",
-                "little1_R", "little2_R", "little3_R",
-                ],
-            [ # 体(下)
-                "lower body",  "waist accessory", 
-                "front skirt_L", "back skirt_L",
-                "front skirt_R", "back skirt_R",
-                ],
-            [ # 足
-                "leg_L", "knee_L", "ankle_L",
-                "leg_R", "knee_R", "ankle_R",
-                ],
-            ]
-    index=1
-    for g in boneGroups:
-        if bone.name in g:
-            return index
-        index+=1
-    print(bone)
-    return 0
-
-
 def __execute(filename, scene):
     if not scene.objects.active:
         print("abort. no active object.")
@@ -1113,7 +1193,7 @@ def __execute(filename, scene):
     active=bl.object.getActive()
     exporter=PmdExporter()
     exporter.setup(scene)
-    exporter.write(filename)
+    exporter.write(filename, scene)
     bl.object.activate(active)