OSDN Git Service

implement toon textures.
authorousttrue <ousttrue@gmail.com>
Fri, 9 Jul 2010 17:43:13 +0000 (02:43 +0900)
committerousttrue <ousttrue@gmail.com>
Fri, 9 Jul 2010 17:43:13 +0000 (02:43 +0900)
include/pmd.h
src/pmd.cpp
swig/blender/bl24.py
swig/blender/bl25.py
swig/blender/pmd_import.py
swig/pmd.i

index f251728..eab2d79 100644 (file)
@@ -282,6 +282,8 @@ struct BoneGroup
 struct ToonTexture
 {
        char name[100];
+       std::wstring getName()const;
+       void setName(const char *src);
 };
 
 ////////////////////////////////////////////////////////////
index 8367902..5b5e0c2 100755 (executable)
@@ -68,6 +68,13 @@ std::wstring
                                        std::string(name, name+20)));
        }
 
+std::wstring 
+       ToonTexture::getName()const
+       {
+               return text::trim(text::to_WideChar(CP_OEMCP, 
+                                       std::string(name, name+100)));
+       }
+
 // 38bytes
 template<class READER>
        void
@@ -313,7 +320,9 @@ private:
        bool parseToonTextures()
        {
                for(size_t i=0; i<10; ++i){
-                       reader_.getString(100);
+                       text::copyStringAndFillZero(
+                                       io_.toon_textures[i].name,
+                                       reader_.getString(100));
                }
                return true;
        }
@@ -833,6 +842,11 @@ void Material::setTexture(const char *src)
        strncpy(texture, src, 20);
 }
 
+void ToonTexture::setName(const char *src)
+{
+       strncpy(name, src, 100);
+}
+
 
 } // namespace
 } // namespace
index 17af23b..034d461 100755 (executable)
@@ -340,13 +340,34 @@ class material:
         return Blender.Material.Get(material_name)
 
     @staticmethod
-    def addTexture(material, texture):
-        material.mode = material.mode | Blender.Material.Modes.TEXFACE
-        material.setTexture(0, texture, Blender.Texture.TexCo.UV)
-
-    @staticmethod
-    def hasTexture(material):
-        return len(material.getTextures())>0
+    def addTexture(m, texture, enable=True):
+        for i in xrange(10):
+            if m.textures[i]:
+                continue
+            if enable:
+                m.mode = m.mode | Blender.Material.Modes.TEXFACE
+                m.setTexture(i, texture, Blender.Texture.TexCo.UV)
+            else:
+                m.setTexture(i, texture)
+                material.setUseTexture(m, i, False)
+            return i
+
+    @staticmethod
+    def getTexture(m, index):
+        return m.textures[index]
+
+    @staticmethod
+    def hasTexture(m):
+        return len(m.getTextures())>0
+
+    @staticmethod
+    def setUseTexture(m, index, enable):
+        enable_textures=set(m.enabledTextures)
+        if enable:
+            enabledTextures.add(index)
+        else;
+            enabledTextures.remove(index)
+        m.enabledTextures=list(enabledTextures)
 
     @staticmethod
     def eachTexturePath(m, dirname):
index dc66b7b..52a61b1 100755 (executable)
@@ -305,17 +305,39 @@ class material:
         return bpy.data.materials[material_name]
 
     @staticmethod
-    def addTexture(material, texture):
-        material.add_texture(texture, "UV", "COLOR")
-        slot=material.texture_slots[material.active_texture_index]
-        slot.blend_type='MULTIPLY'
-        slot.map_alpha=True
+    def addTexture(material, texture, enable=True):
+        # search free slot
+        index=None
+        for i, slot in enumerate(material.texture_slots):
+            if not slot:
+                index=i
+                break
+        if index==None:
+            return
+
+        if enable:
+            material.add_texture(texture, "UV", "COLOR")
+            slot=material.texture_slots[index]
+            slot.blend_type='MULTIPLY'
+            slot.map_alpha=True
+        else:
+            material.add_texture(texture)
+            material.use_textures[index]=False
+        return index
+
+    @staticmethod
+    def getTexture(material, index):
+        return material.texture_slots[index]
 
     @staticmethod
     def hasTexture(material):
         return material.texture_slots[0]
 
     @staticmethod
+    def setUseTexture(material, index, enable):
+        material.use_textures[index]=enable
+
+    @staticmethod
     def eachTexturePath(m):
         for slot in m.texture_slots:
             if slot and slot.texture:
index fe03acd..51309d0 100755 (executable)
@@ -7,7 +7,7 @@
  Tooltip: 'Import PMD file for MikuMikuDance.'
 """
 __author__= ["ousttrue"]
-__version__= "1.2"
+__version__= "1.8"
 __url__=()
 __bpydoc__="""
 pmd Importer
@@ -30,6 +30,7 @@ This script imports a pmd into Blender for editing.
 1.5 20100626: refactoring.
 1.6 20100629: sphere map.
 1.7 20100703: implement bone group.
+1.8 20100710: implement toon texture.
 """
 
 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
@@ -174,6 +175,7 @@ else:
 def VtoV(v):
     return bl.createVector(v.x, v.y, v.z)
 
+
 def convert_coord(pos):
     """
     Left handed y-up to Right handed z-up
@@ -197,6 +199,19 @@ def get_bone_name(l, index):
     print('invalid bone index', index)
     return l.bones[0].getName()
 
+
+def __importToonTextures(io, tex_dir):
+    mesh, meshObject=bl.mesh.create('ToonTextures')
+    material=bl.material.create('ToonTextures')
+    bl.mesh.addMaterial(mesh, material)
+    for i in range(10):
+        t=io.getToonTexture(i)
+        path=os.path.join(tex_dir, t.getName())
+        texture, image=bl.texture.create(path)
+        bl.material.addTexture(material, texture, False)
+    return meshObject, material
+
+
 def __importShape(obj, l, vertex_map):
     if len(l.morph_list)==0:
         return
@@ -408,7 +423,7 @@ def __importArmature(l):
         
 
 def __import16MaerialAndMesh(meshObject, l, 
-        material_order, face_map, tex_dir):
+        material_order, face_map, tex_dir, toon_material):
 
     mesh=bl.object.getData(meshObject)
     ############################################################
@@ -428,6 +443,10 @@ def __import16MaerialAndMesh(meshObject, l,
             break
 
         material=createPmdMaterial(m, material_index)
+        toon_index=bl.material.addTexture(
+                material, 
+                bl.material.getTexture(toon_material, m.toon_index).texture,
+                False)
 
         texture_name=m.getTexture()
         if texture_name!='':
@@ -568,7 +587,7 @@ def __import16MaerialAndMesh(meshObject, l,
     return vertex_map
 
 
-def __importMaterialAndMesh(io, tex_dir):
+def __importMaterialAndMesh(io, tex_dir, toon_material):
     """
     @param l[in] mmd.PMDLoader
     @param filename[in]
@@ -633,7 +652,7 @@ def __importMaterialAndMesh(io, tex_dir):
         # shapeキーで使われる順に並べなおしたマテリアル16個分の
         # メッシュを作成する
         vertex_map=__import16MaerialAndMesh(
-                meshObject, io, material16, face_map, tex_dir)
+                meshObject, io, material16, face_map, tex_dir, toon_material)
 
         # crete shape key
         __importShape(meshObject, io, vertex_map)
@@ -800,10 +819,12 @@ def _execute(filename):
     root=bl.object.createEmpty(model_name)
 
     # toon textures
-    #__importToonTextures(io)
+    tex_dir=os.path.dirname(filename)
+    toonTextures, toonMaterial=__importToonTextures(io, tex_dir)
+    bl.object.makeParent(root, toonTextures)
 
     # import mesh
-    mesh_objects=__importMaterialAndMesh(io, os.path.dirname(filename))
+    mesh_objects=__importMaterialAndMesh(io, tex_dir, toonMaterial)
     for o in mesh_objects:
         bl.object.makeParent(root, o)
 
index 5e0686a..f858e8b 100644 (file)
@@ -84,6 +84,12 @@ const meshio::pmd::Vertex* _dereferenceVertex(
 // addMaterial
 // addBone
 // addMorph
+// addIK
+// addBoneDisplay
+// addBoneGroup
+// getToonTexture
+// addRigidBody
+// addConstraint
 ///////////////////////////////////////////////////////////////////////////////
 %extend meshio::pmd::IO {