OSDN Git Service

implement converter
[meshio/pymeshio.git] / pymeshio / pmd / __init__.py
index c365c07..839b637 100644 (file)
@@ -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, 
@@ -40,6 +69,17 @@ class Vertex(object):
                 str(self.uv), 
                 self.bone0, self.bone1, self.weight0)
 
+    def __eq__(self, rhs):
+        return (
+                self.pos==rhs.pos
+                and self.normal==rhs.normal
+                and self.uv==rhs.uv
+                and self.bone0==rhs.bone0
+                and self.bone1==rhs.bone1
+                and self.weight0==rhs.weight0
+                and self.edge_flag==rhs.edge_flag
+                )
+
     def __getitem__(self, key):
         if key==0:
             return self.pos.x
@@ -52,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', 
@@ -90,24 +146,55 @@ class Material(object):
                 self.diffuse[2], self.diffuse[3],
                 )
 
+    def __eq__(self, rhs):
+        return (
+                self.diffuse_color==rhs.diffuse_color
+                and self.alpha==rhs.alpha
+                and self.specular_factor==rhs.specular_factor
+                and self.specular_color==rhs.specular_color
+                and self.ambient_color==rhs.ambient_color
+                and self.toon_index==rhs.toon_index
+                and self.edge_flag==rhs.edge_flag
+                and self.vertex_count==rhs.vertex_count
+                and self.texture_file==rhs.texture_file
+                )
 
-class Bone(object):
-    """pmd material struct.
 
-    Attributes:
-        _name: 
-        index:
-        type:
-        ik:
-        pos:
-        _english_name:
-        ik_index:
-        parent_index:
-        tail_index:
-
-        parent:
-        tail:
-        children:
+class Bone(object):
+    """
+    ==========
+    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
@@ -130,13 +217,27 @@ 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=''
 
+    def __eq__(self, rhs):
+        return (
+                self.name==rhs.name
+                and self.index==rhs.index
+                and self.type==rhs.type
+                and self.parent_index==rhs.parent_index
+                and self.tail_index==rhs.tail_index
+                and self.tail==rhs.tail
+                and self.ik_index==rhs.ik_index
+                and self.pos==rhs.pos
+                and self.children==rhs.children
+                and self.english_name==rhs.english_name
+                )
+
     def hasParent(self):
         return self.parent_index!=0xFFFF
 
@@ -268,11 +369,20 @@ class IK(object):
     def __str__(self):
         return "<IK index: %d, target: %d, iterations: %d, weight: %f, children: %s(%d)>" %(self.index, self.target, self.iterations, self.weight, '-'.join([str(i) for i in self.children]), len(self.children))
 
+    def __eq__(self, rhs):
+        return (
+                self.index==rhs.index
+                and self.target==rhs.target
+                and self.iterations==rhs.iterations
+                and self.weight==rhs.weight
+                and self.children==rhs.children
+                )
+
 
-class Skin(object):
+class Morph(object):
     __slots__=['name', 'type', 'indices', 'pos_list', 'english_name',
             'vertex_count']
-    def __init__(self, name='skin'):
+    def __init__(self, name):
         self.name=name
         self.type=None
         self.indices=[]
@@ -282,16 +392,31 @@ class Skin(object):
 
     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>' % (
             self.name, self.type, len(self.indices))
 
+    def __eq__(self, rhs):
+        return (
+                self.name==rhs.name
+                and self.type==rhs.type
+                and self.indices==rhs.indices
+                and self.pos_list==rhs.pos_list
+                and self.english_name==rhs.english_name
+                and self.vertex_count==rhs.vertex_count
+                )
+
 
 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
@@ -323,16 +448,16 @@ class RigidBody(object):
             bone_index, 
             collision_group, 
             no_collision_group, 
-            shape_type,
-            shape_size,
-            shape_position, 
-            shape_rotation, 
             mass,
             linear_damping, 
             angular_damping, 
             restitution, 
             friction, 
-            mode
+            mode,
+            shape_type=0,
+            shape_size=common.Vector3(),
+            shape_position=common.Vector3(), 
+            shape_rotation=common.Vector3() 
             ):
         self.name=name
         self.bone_index=bone_index
@@ -349,6 +474,24 @@ class RigidBody(object):
         self.friction=friction
         self.mode=mode
 
+    def __eq__(self, rhs):
+        return (
+                self.name==rhs.name
+                and self.bone_index==rhs.bone_index
+                and self.collision_group==rhs.collision_group
+                and self.no_collision_group==rhs.no_collision_group
+                and self.shape_type==rhs.shape_type
+                and self.shape_size==rhs.shape_size
+                and self.shape_position==rhs.shape_position
+                and self.shape_rotation==rhs.shape_rotation
+                and self.mass==rhs.mass
+                and self.linear_damping==rhs.linear_damping
+                and self.angular_damping==rhs.angular_damping
+                and self.restitution==rhs.restitution
+                and self.friction==rhs.friction
+                and self.mode==rhs.mode
+                )
+
 
 class Joint(object):
     __slots__=[ 'name', 'rigidbody_index_a', 'rigidbody_index_b', 
@@ -376,23 +519,20 @@ class Joint(object):
         self.spring_constant_translation=spring_constant_translation
         self.spring_constant_rotation=spring_constant_rotation
 
-
-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)
+    def __eq__(self, rhs):
+        return (
+                self.name==rhs.name
+                and self.rigidbody_index_a==rhs.rigidbody_index_a
+                and self.rigidbody_index_b==rhs.rigidbody_index_b
+                and self.position==rhs.position
+                and self.rotation==rhs.rotation
+                and self.translation_limit_max==rhs.translation_limit_max
+                and self.translation_limit_min==rhs.translation_limit_min
+                and self.rotation_limit_max==rhs.rotation_limit_max
+                and self.rotation_limit_min==rhs.rotation_limit_min
+                and self.spring_constant_translation==rhs.spring_constant_translation
+                and self.spring_constant_rotation==rhs.spring_constant_rotation
+                )
 
 
 class Model(object):
@@ -413,8 +553,9 @@ class Model(object):
             'ik_list', 'morphs',
             'morph_indices', 'bone_group_list', 'bone_display_list',
             'toon_textures',
-            'no_parent_bones',
             'rigidbodies', 'joints',
+
+            'no_parent_bones',
             ]
     def __init__(self, version):
         self.version=version
@@ -432,7 +573,7 @@ class Model(object):
         self.bone_group_list=[]
         self.bone_display_list=[]
         # extend
-        self.toon_textures=ToonTextures()
+        self.toon_textures=[b'']*10
         self.rigidbodies=[]
         self.joints=[]
         # innner use
@@ -442,18 +583,77 @@ 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))
 
+    def __eq__(self, rhs):
+        return (
+                self.name==rhs.name
+                and self.comment==rhs.comment
+                and self.english_name==rhs.english_name
+                and self.english_comment==rhs.english_comment
+                and self.vertices==rhs.vertices
+                and self.indices==rhs.indices
+                and self.materials==rhs.materials
+                and self.bones==rhs.bones
+                and self.ik_list==rhs.ik_list
+                and self.morphs==rhs.morphs
+                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.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(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