OSDN Git Service

fix bone group name.
[meshio/meshio.git] / swig / blender / pmd_export.py
index 1a0f416..43fc7f9 100644 (file)
@@ -21,6 +21,7 @@ This script exports a pmd model.
 1.2 20100616: implement rigid body.
 1.3 20100619: fix rigid body, bone weight.
 1.4 20100626: refactoring.
+1.5 20100629: sphere map.
 """
 
 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
@@ -86,6 +87,10 @@ if isBlender24():
         # flag
         material.flag=1 if m.enableSSS else 0
 
+    def toCP932(s):
+        return s
+
+
 else:
     # for 2.5
     import bpy
@@ -104,7 +109,7 @@ else:
         material.diffuse.b=m.diffuse_color[2]
         material.diffuse.a=m.alpha
         # specular
-        material.sinness=0 if m.specular_hardness<1e-5 else m.specular_hardness*10
+        material.sinness=0 if m.specular_toon_size<1e-5 else m.specular_hardness*10
         material.specular.r=m.specular_color[0]
         material.specular.g=m.specular_color[1]
         material.specular.b=m.specular_color[2]
@@ -114,6 +119,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']
@@ -247,13 +258,12 @@ class VertexArray(object):
             b1_0, b1_1, b1_2,
             weight0, weight1, weight2
             ):
-        if not material in self.indexArrays:
-            self.indexArrays[material]=[]
-
         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]
 
 
@@ -306,24 +316,16 @@ class OneSkinMesh(object):
 
     def addMesh(self, obj):
         if bl.object.isVisible(obj):
-            # 非表示
+            return
+        if not bl.modifier.hasType(obj, 'ARMATURE'):
             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):
@@ -331,9 +333,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:
@@ -355,8 +357,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)):
@@ -371,16 +376,16 @@ 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, 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
@@ -462,9 +467,44 @@ 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
+
+        print("export", 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
+            bpy.ops.object.modifier_apply(modifier=m.name)
+
+        weightMap, secondWeightMap=self.__getWeightMap(copyObj, copyMesh)
+        self.__processFaces(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):
             return
@@ -534,6 +574,7 @@ class OneSkinMesh(object):
                 if v[0]==morph.name:
                     return i
             print(morph)
+            return len(englishmap.skinMap)
         if isBlender24():
             self.morphList.sort(lambda l, r: getIndex(l)-getIndex(r))
         else:
@@ -687,6 +728,7 @@ class BoneBuilder(object):
                 if k_v[0]==bone.name:
                     return i
             print(bone)
+            return len(boneMap)
 
         if isBlender24():
             self.bones.sort(lambda l, r: getIndex(l)-getIndex(r))
@@ -786,6 +828,10 @@ class PmdExporter(object):
         self.oneSkinMesh=OneSkinMesh(scene)
         self.__createOneSkinMesh(root)
         print(self.oneSkinMesh)
+        if len(self.oneSkinMesh.morphList)==0:
+            # create emtpy skin
+            self.oneSkinMesh.createEmptyBasicSkin()
+
         self.name=root.o.name
 
         # skeleton
@@ -828,7 +874,7 @@ class PmdExporter(object):
             v.normal.y=normal[2]
             v.normal.z=normal[1]
             v.uv.x=uv[0]
-            v.uv.y=uv[1]
+            v.uv.y=1.0-uv[1] # reverse vertical
             v.bone0=self.builder.indexByName(b0)
             v.bone1=self.builder.indexByName(b1)
             v.weight0=int(100*weight)
@@ -837,6 +883,7 @@ class PmdExporter(object):
         # 面とマテリアル
         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()
@@ -844,8 +891,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)
@@ -863,15 +914,17 @@ class PmdExporter(object):
             # name
             boneNameMap[b.name]=i
             v=englishmap.getUnicodeBoneName(b.name)
+            if not v:
+                v=[b.name, b.name]
             assert(v)
             cp932=v[1].encode('cp932')
             assert(len(cp932)<20)
             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
@@ -909,6 +962,8 @@ class PmdExporter(object):
             morph=io.addMorph()
 
             v=englishmap.getUnicodeSkinName(m.name)
+            if not v:
+                v=[m.name, m.name, 0]
             assert(v)
             cp932=v[1].encode('cp932')
             morph.setName(cp932)
@@ -937,14 +992,14 @@ class PmdExporter(object):
 
         # ボーン表示枠
         def createBoneDisplayName(name, english):
-            boneDisplayName=io.addBoneDisplayName()
+            boneDisplayName=io.addBoneGroup()
             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("IK\n", "IK\n")
         boneDisplayName=createBoneDisplayName("体(上)\n", "Body[u]\n")
         boneDisplayName=createBoneDisplayName("髪\n", "Hair\n")
         boneDisplayName=createBoneDisplayName("腕\n", "Arms\n")
@@ -1095,7 +1150,7 @@ def getBoneDisplayGroup(bone):
             return index
         index+=1
     print(bone)
-    return -1
+    return 0
 
 
 def __execute(filename, scene):