OSDN Git Service

fix bone tail.
[meshio/meshio.git] / swig / blender / pmd_export.py
index 1cc9add..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'
 
 
 ###############################################################################
@@ -170,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)
@@ -183,6 +185,15 @@ class VertexArray(object):
     """
     頂点配列
     """
+    __slots__=[
+            'indexArrays',
+            'vertices',
+            'normals',
+            'uvs',
+            'b0', 'b1', 'weight',
+            'vertexMap',
+            'objectMap',
+            ]
     def __init__(self):
         # マテリアル毎に分割したインデックス配列
         self.indexArrays={}
@@ -196,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>" % (
@@ -238,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,
@@ -258,6 +278,11 @@ class VertexArray(object):
             b1_0, b1_1, b1_2,
             weight0, weight1, weight2
             ):
+        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)
@@ -297,7 +322,7 @@ class IKSolver(object):
 
 
 class OneSkinMesh(object):
-    __slots__=['obj_index', 'scene', 'vertexArray', 'morphList', 
+    __slots__=['scene', 'vertexArray', 'morphList', 
             'rigidbodies',
             'constraints',
             ]
@@ -307,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>" % (
@@ -317,8 +341,6 @@ 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)
@@ -378,7 +400,7 @@ class OneSkinMesh(object):
 
         return weightMap, secondWeightMap
 
-    def __processFaces(self, mesh, weightMap, secondWeightMap):
+    def __processFaces(self, obj_name, mesh, weightMap, secondWeightMap):
         # 各面の処理
         for i, face in enumerate(mesh.faces):
             faceVertexCount=bl.face.getVertexCount(face)
@@ -390,7 +412,7 @@ class OneSkinMesh(object):
             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,
@@ -418,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,
@@ -443,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,
@@ -477,6 +499,9 @@ class OneSkinMesh(object):
             if CONSTRAINT_A in obj:
                 return
 
+        #if not bl.modifier.hasType(obj, 'ARMATURE'):
+        #    return
+
         bl.message("export: %s" % obj.name)
 
         # メッシュのコピーを生成してオブジェクトの行列を適用する
@@ -502,10 +527,9 @@ class OneSkinMesh(object):
                 print(m.tpye)
 
         weightMap, secondWeightMap=self.__getWeightMap(copyObj, copyMesh)
-        self.__processFaces(copyMesh, weightMap, secondWeightMap)
+        self.__processFaces(obj.name, copyMesh, weightMap, secondWeightMap)
 
         bl.object.delete(copyObj)
-        self.obj_index+=1
 
     def createEmptyBasicSkin(self):
         self.__getOrCreateMorph('base', 0)
@@ -532,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)
@@ -565,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[:]
@@ -643,7 +677,7 @@ class BoneBuilder(object):
         for i, g in enumerate(self.bone_groups):
             for b in g[1]:
                 if b==bone.name:
-                    return i
+                    return i+1
         print('no gorup', bone)
         return 0
 
@@ -894,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"
@@ -1042,18 +1076,35 @@ class PmdExporter(object):
         # ボーングループメンバー
         for i, b in enumerate(self.skeleton.bones):
             if i==0:
-                continue
+               continue
             if b.type in [6, 7]:
-                continue
+               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={}
@@ -1142,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)