OSDN Git Service

replace blender25-meshio to blender26-meshio
[meshio/pymeshio.git] / pymeshio / pmd / __init__.py
old mode 100644 (file)
new mode 100755 (executable)
index e8c9a44..86f92be
@@ -1,26 +1,55 @@
 # coding: utf-8
 """
-PMDの読み込み
-http://blog.goo.ne.jp/torisu_tetosuki/e/209ad341d3ece2b1b4df24abf619d6e4
+========================
+MikuMikuDance PMD format
+========================
+
+file format
+~~~~~~~~~~~
+* http://blog.goo.ne.jp/torisu_tetosuki/e/209ad341d3ece2b1b4df24abf619d6e4 
+
+specs
+~~~~~
+* textencoding: bytes(cp932)
+* coordinate: left handed y-up(DirectX)
+* uv origin: 
+* face: only triangle
+* backculling: 
+
 """
 import os
 import sys
 import struct
 import warnings
-import pymeshio.common
+from .. import common
 
 
 class Vertex(object):
-    """pmd vertex struct.
-
-    Attributes:
-        pos: Vector3
-        normal: Vector3
-        uv: Vector2
-        bone0: bone index
-        bone1: bone index
-        weight0: bone0 influence
-        edge_flag: int flag
+    """
+    ==========
+    pmd vertex
+    ==========
+    two bone weighted vertex with normal and uv.
+
+    format
+    ~~~~~~
+    * http://blog.goo.ne.jp/torisu_tetosuki/e/5a1b16e2f61067838dfc66d010389707
+
+    :IVariables:
+        pos
+            Vector3
+        normal 
+            Vector3
+        uv 
+            Vector2
+        bone0 
+            bone index
+        bone1 
+            bone index
+        weight0 
+            bone0 influence.  min: 0, max: 100
+        edge_flag 
+            int flag.  0: edge on, 1: edge off
     """
     __slots__=['pos', 'normal', 'uv', 'bone0', 'bone1', 'weight0', 'edge_flag']
     def __init__(self, pos, normal, uv, 
@@ -63,18 +92,34 @@ class Vertex(object):
 
 
 class Material(object):
-    """pmd material struct.
-
-    Attributes:
-        diffuse_color: RGB
-        alpha: float
-        specular_factor: float
-        specular_color: RGB
-        ambient_color: RGB
-        toon_index: int
-        edge_flag: int
-        vertex_count: indices length
-        texture_file: texture file path
+    """
+    ============
+    pmd material
+    ============
+
+    format
+    ~~~~~~
+    * http://blog.goo.ne.jp/torisu_tetosuki/e/ea0bb1b1d4c6ad98a93edbfe359dac32
+
+    :IVariables:
+        diffuse_color
+            RGB
+        alpha
+            float
+        specular_factor
+            float
+        specular_color
+            RGB
+        ambient_color
+            RGB
+        toon_index
+            int
+        edge_flag
+            int
+        vertex_count
+            indices length
+        texture_file
+            texture file path
     """
     __slots__=[
             'diffuse_color', 'alpha', 
@@ -116,22 +161,40 @@ class Material(object):
 
 
 class Bone(object):
-    """pmd material struct.
-
-    Attributes:
-        _name: 
-        index:
-        type:
-        ik:
-        pos:
-        _english_name:
-        ik_index:
-        parent_index:
-        tail_index:
-
-        parent:
-        tail:
-        children:
+    """
+    ==========
+    pmd bone
+    ==========
+
+    format
+    ~~~~~~
+    * http://blog.goo.ne.jp/torisu_tetosuki/e/638463f52d0ad6ca1c46fd315a9b17d0
+
+    :IVariables:
+        name 
+            bone name
+        english_name
+            bone english_name
+        index
+            boen index(append for internal use)
+        type
+            bone type
+        ik
+            ik(append for internal use)
+        pos
+            bone head position
+        ik_index
+            ik target bone index
+        parent_index
+            parent bone index
+        tail_index
+            tail bone index
+        parent
+            parent bone(append for internal use)
+        tail
+            tail bone(append for internal use)
+        children
+            children bone(append for internal use)
     """
     # kinds
     ROTATE = 0
@@ -154,12 +217,12 @@ class Bone(object):
         self.type=type
         self.parent_index=0xFFFF
         self.tail_index=0
-        self.tail=pymeshio.common.Vector3(0, 0, 0)
+        self.tail=common.Vector3(0, 0, 0)
         self.parent=None
         self.ik_index=0xFFFF
-        self.pos=pymeshio.common.Vector3(0, 0, 0)
+        self.pos=common.Vector3(0, 0, 0)
         self.children=[]
-        self.english_name=''
+        self.english_name=b''
 
     def __eq__(self, rhs):
         return (
@@ -324,12 +387,12 @@ class Morph(object):
         self.type=None
         self.indices=[]
         self.pos_list=[]
-        self.english_name=''
+        self.english_name=b''
         self.vertex_count=0
 
     def append(self, index, x, y, z):
         self.indices.append(index)
-        self.pos_list.append(Vector3(x, y, z))
+        self.pos_list.append(common.Vector3(x, y, z))
 
     def __str__(self):
         return '<Skin name: "%s", type: %d, vertex: %d>' % (
@@ -347,8 +410,13 @@ class Morph(object):
 
 
 class BoneGroup(object):
-    __slots__=['_name', '_english_name']
-    def __init__(self, name='group'): self._name=name; self._english_name='center'
+    __slots__=['name', 'english_name']
+    def __init__(self, name=b'group', english_name=b'center'): 
+        self.name=name
+        self.english_name=english_name
+
+    def __eq__(self, rhs):
+        return self.name==rhs.name and self.english_name==rhs.english_name
 
 
 SHAPE_SPHERE=0
@@ -383,13 +451,13 @@ class RigidBody(object):
             shape_type,
             shape_size,
             shape_position, 
-            shape_rotation, 
+            shape_rotation,
             mass,
             linear_damping, 
             angular_damping, 
             restitution, 
             friction, 
-            mode
+            mode,
             ):
         self.name=name
         self.bone_index=bone_index
@@ -467,24 +535,6 @@ class Joint(object):
                 )
 
 
-class ToonTextures(object):
-    __slots__=['_toon_textures']
-    def __init__(self):
-        self._toon_textures=[]
-        for i in range(10):
-            self._toon_textures.append('toon%02d.bmp' % (i+1))
-
-    def __getitem__(self, key):
-        return from_str(self._toon_textures[key])
-
-    def __setitem__(self, key, value):
-        self._toon_textures[key]=to_str(value)
-
-    def __iter__(self):
-        for toon_texture in self._toon_textures:
-            yield from_str(toon_texture)
-
-
 class Model(object):
     """pmd loader class.
 
@@ -497,18 +547,19 @@ class Model(object):
         _name: internal
     """
     __slots__=[
+            'path',
             'version', 'name', 'comment',
             'english_name', 'english_comment',
             'vertices', 'indices', 'materials', 'bones', 
             'ik_list', 'morphs',
             'morph_indices', 'bone_group_list', 'bone_display_list',
-            'bone_group_english_list',
             'toon_textures',
             'rigidbodies', 'joints',
 
             'no_parent_bones',
             ]
-    def __init__(self, version):
+    def __init__(self, version=1.0):
+        self.path=b''
         self.version=version
         self.name=b''
         self.comment=b''
@@ -524,8 +575,7 @@ class Model(object):
         self.bone_group_list=[]
         self.bone_display_list=[]
         # extend
-        self.bone_group_english_list=[]
-        self.toon_textures=ToonTextures()
+        self.toon_textures=[b'']*10
         self.rigidbodies=[]
         self.joints=[]
         # innner use
@@ -535,9 +585,9 @@ class Model(object):
     def getUV(self, i): return self.vertices[i].uv
 
     def __str__(self):
-        return '<PMDLoader version: %g, model: "%s", vertex: %d, face: %d, material: %d, bone: %d ik: %d, skin: %d>' % (
+        return '<pmd-%g, "%s" vertex: %d, face: %d, material: %d, bone: %d ik: %d, skin: %d>' % (
             self.version, self.name, len(self.vertices), len(self.indices),
-            len(self.materials), len(self.bones), len(self.ik_list), len(self.morph_list))
+            len(self.materials), len(self.bones), len(self.ik_list), len(self.morphs))
 
     def __eq__(self, rhs):
         return (
@@ -554,20 +604,58 @@ class Model(object):
                 and self.morph_indices==rhs.morph_indices
                 and self.bone_group_list==rhs.bone_group_list
                 and self.bone_display_list==rhs.bone_display_list
-                and self.bone_group_english_list==rhs.bone_group_english_list
                 and self.toon_textures==rhs.toon_textures
                 and self.rigidbodies==rhs.rigidbodies
                 and self.joints==rhs.joints
                 )
 
-
-class IO(object):
-    def __init__(self):
-        pass
-
-    def read(self, path):
-        warnings.warn("'pymeshio.mqo.IO.read' will be replaced by 'pymeshio.mqo.loader.load'")
-        model=pymeshio.pmd.loader.load_from_file(path)
-        if model:
-            return True
+    def diff(self, rhs):
+        if self.name!=rhs.name: 
+            print(self.name, rhs.name)
+            return
+        if self.comment!=rhs.comment: 
+            print(self.comment, rhs.comment)
+            return
+        if self.english_name!=rhs.english_name: 
+            print(self.english_name, rhs.english_name)
+            return
+        if self.english_comment!=rhs.english_comment: 
+            print(self.english_comment, rhs.english_comment)
+            return
+        if self.vertices!=rhs.vertices: 
+            print(self.vertices, rhs.vertices)
+            return
+        if self.indices!=rhs.indices: 
+            print(self.indices, rhs.indices)
+            return
+        if self.materials!=rhs.materials: 
+            print(self.materials, rhs.materials)
+            return
+        if self.bones!=rhs.bones: 
+            print(self.bones, rhs.bones)
+            return
+        if self.ik_list!=rhs.ik_list: 
+            print(self.ik_list, rhs.ik_list)
+            return
+        if self.morphs!=rhs.morphs: 
+            print(self.morphs, rhs.morphs)
+            return
+        if self.morph_indices!=rhs.morph_indices: 
+            print(self.morph_indices, rhs.morph_indices)
+            return
+        if self.bone_group_list!=rhs.bone_group_list: 
+            print(self.bone_group_list, rhs.bone_group_list)
+            return
+        if self.bone_display_list!=rhs.bone_display_list: 
+            print(self.bone_display_list, rhs.bone_display_list)
+            return
+        if self.toon_textures!=rhs.toon_textures: 
+            print(self.toon_textures, rhs.toon_textures)
+            return
+        if self.rigidbodies!=rhs.rigidbodies: 
+            print(self.rigidbodies, rhs.rigidbodies)
+            return
+        if self.joints!=rhs.joints: 
+            print(self.joints, rhs.joints)
+            return