OSDN Git Service

implement converter
[meshio/pymeshio.git] / pymeshio / pmx / __init__.py
index 42b15f6..4db73d1 100644 (file)
@@ -1,10 +1,22 @@
 #!/usr/bin/env python\r
 # coding: utf-8\r
 """\r
-pmx file io library.\r
+========================\r
+MikuMikuDance PMX format\r
+========================\r
+\r
+file format\r
+~~~~~~~~~~~\r
+* PMDEditor's Lib/PMX仕様/PMX仕様.txt\r
+\r
+specs\r
+~~~~~\r
+* textencoding: unicode\r
+* coordinate: left handed y-up(DirectX)\r
+* uv origin: \r
+* face: only triangle\r
+* backculling: \r
 \r
-pmx file format:\r
-    PMDEditor's Lib/PMX仕様/PMX仕様.txt\r
 """\r
 __author__="ousttrue"\r
 __license__="zlib"\r
@@ -18,7 +30,42 @@ from pymeshio import common
 \r
 \r
 \r
-class Ik(object):\r
+class DifferenceException(Exception):\r
+    pass\r
+\r
+\r
+class Diff(object):\r
+    def _diff(self, rhs, key):\r
+        l=getattr(self, key)\r
+        r=getattr(rhs, key)\r
+        if l!=r:\r
+            print(l)\r
+            print(r)\r
+            raise DifferenceException(key)\r
+\r
+    def _diff_array(self, rhs, key):\r
+        la=getattr(self, key)\r
+        ra=getattr(rhs, key)\r
+        if len(la)!=len(ra):\r
+            raise DifferenceException("%s diffrence %d with %d" % (key, len(la), len(ra)))\r
+        for i, (l, r) in enumerate(zip(la, ra)):\r
+            if isinstance(l, Diff):\r
+                try:\r
+                    l.diff(r)\r
+                except DifferenceException as e:\r
+                    print(i)\r
+                    print(l)\r
+                    print(r)\r
+                    raise DifferenceException("{0}: {1}".format(key, e.message))\r
+            else:\r
+                if l!=r:\r
+                    print(i)\r
+                    print(l)\r
+                    print(r)\r
+                    raise DifferenceException("{0}".format(key))\r
+\r
+\r
+class Ik(Diff):\r
     """ik info\r
     """\r
     __slots__=[\r
@@ -27,14 +74,28 @@ class Ik(object):
             'limit_radian',\r
             'link',\r
             ]\r
-    def __init__(self, target_index, loop, limit_radian):\r
+    def __init__(self, target_index, loop, limit_radian, link=[]):\r
         self.target_index=target_index\r
         self.loop=loop\r
         self.limit_radian=limit_radian\r
-        self.link=[]\r
+        self.link=link\r
+\r
+    def __eq__(self, rhs):\r
+        return (\r
+                self.target_index==rhs.target_index\r
+                and self.loop==rhs.loop\r
+                and self.limit_radian==rhs.limit_radian\r
+                and self.link==rhs.link\r
+                )\r
 \r
+    def diff(self, rhs):\r
+        self._diff(rhs, 'target_index')\r
+        self._diff(rhs, 'loop')\r
+        self._diff(rhs, 'limit_radian')\r
+        self._diff_array(rhs, 'link')\r
 \r
-class IkLink(object):\r
+\r
+class IkLink(Diff):\r
     """ik link info\r
     """\r
     __slots__=[\r
@@ -43,14 +104,28 @@ class IkLink(object):
             'limit_min',\r
             'limit_max',\r
             ]\r
-    def __init__(self, bone_index, limit_angle):\r
+    def __init__(self, bone_index, limit_angle, limit_min=common.Vector3(), limit_max=common.Vector3()):\r
         self.bone_index=bone_index\r
         self.limit_angle=limit_angle\r
-        self.limit_min=None\r
-        self.limit_max=None\r
+        self.limit_min=limit_min\r
+        self.limit_max=limit_max\r
 \r
+    def __eq__(self, rhs):\r
+        return (\r
+                self.bone_index==rhs.bone_index\r
+                and self.limit_angle==rhs.limit_angle\r
+                and self.limit_min==rhs.limit_min\r
+                and self.limit_max==rhs.limit_max\r
+                )\r
 \r
-class Bone(object):\r
+    def diff(self, rhs):\r
+        self._diff(rhs, 'bone_index')\r
+        self._diff(rhs, 'limit_angle')\r
+        self._diff(rhs, 'limit_min')\r
+        self._diff(rhs, 'limit_max')\r
+\r
+\r
+class Bone(Diff):\r
     """material\r
 \r
     Bone: see __init__\r
@@ -63,7 +138,7 @@ class Bone(object):
             'layer',\r
             'flag',\r
 \r
-            'tail_positoin',\r
+            'tail_position',\r
             'tail_index',\r
             'effect_index',\r
             'effect_factor',\r
@@ -79,7 +154,16 @@ class Bone(object):
             position,\r
             parent_index,\r
             layer,\r
-            flag\r
+            flag,\r
+            tail_position=common.Vector3(),\r
+            tail_index=-1,\r
+            effect_index=-1,\r
+            effect_factor=0.0,\r
+            fixed_axis=common.Vector3(),\r
+            local_x_vector=common.Vector3(),\r
+            local_z_vector=common.Vector3(),\r
+            external_key=-1,\r
+            ik=None\r
             ):\r
         self.name=name\r
         self.english_name=english_name\r
@@ -87,6 +171,15 @@ class Bone(object):
         self.parent_index=parent_index\r
         self.layer=layer\r
         self.flag=flag\r
+        self.tail_position=tail_position\r
+        self.tail_index=tail_index\r
+        self.effect_index=effect_index\r
+        self.effect_factor=effect_factor\r
+        self.fixed_axis=fixed_axis\r
+        self.local_x_vector=local_x_vector\r
+        self.local_z_vector=local_z_vector\r
+        self.external_key=external_key\r
+        self.ik=ik\r
 \r
     def __eq__(self, rhs):\r
         return (\r
@@ -98,6 +191,29 @@ class Bone(object):
                 and self.flag==rhs.flag\r
                 )\r
 \r
+    def __ne__(self, rhs):\r
+        return not self.__eq__(rhs)\r
+\r
+    def diff(self, rhs):\r
+        self._diff(rhs, 'name')\r
+        self._diff(rhs, 'english_name')\r
+        self._diff(rhs, 'position')\r
+        self._diff(rhs, 'parent_index')\r
+        #self._diff(rhs, 'layer')\r
+        self._diff(rhs, 'flag')\r
+        self._diff(rhs, 'tail_position')\r
+        self._diff(rhs, 'tail_index')\r
+        #self._diff(rhs, 'effect_index')\r
+        #self._diff(rhs, 'effect_factor')\r
+        #self._diff(rhs, 'fixed_axis')\r
+        self._diff(rhs, 'local_x_vector')\r
+        self._diff(rhs, 'local_z_vector')\r
+        self._diff(rhs, 'external_key')\r
+        if self.ik and rhs.ik:\r
+            self.ik.diff(rhs.ik)\r
+        else:\r
+            self._diff(rhs, 'ik')\r
+\r
     def getConnectionFlag(self):\r
         return self.flag & 0x0001\r
 \r
@@ -120,7 +236,7 @@ class Bone(object):
         return (self.flag &  0x2000) >> 13\r
 \r
  \r
-class Material(object):\r
+class Material(Diff):\r
     """material\r
 \r
     Attributes: see __init__\r
@@ -149,8 +265,8 @@ class Material(object):
             english_name,\r
             diffuse_color,\r
             alpha,\r
-            specular_color,\r
             specular_factor,\r
+            specular_color,\r
             ambient_color,\r
             flag,\r
             edge_color,\r
@@ -158,7 +274,10 @@ class Material(object):
             texture_index,\r
             sphere_texture_index,\r
             sphere_mode,\r
-            toon_sharing_flag\r
+            toon_sharing_flag,\r
+            toon_texture_index=0,\r
+            comment=common.unicode(""),\r
+            vertex_count=0,\r
             ):\r
         self.name=name\r
         self.english_name=english_name\r
@@ -174,10 +293,9 @@ class Material(object):
         self.sphere_texture_index=sphere_texture_index\r
         self.sphere_mode=sphere_mode\r
         self.toon_sharing_flag=toon_sharing_flag\r
-        #\r
-        self.toon_texture_index=None\r
-        self.comment=name.__class__() # unicode\r
-        self.vertex_count=0\r
+        self.toon_texture_index=toon_texture_index\r
+        self.comment=comment\r
+        self.vertex_count=vertex_count\r
 \r
     def __eq__(self, rhs):\r
         return (\r
@@ -200,17 +318,35 @@ class Material(object):
                 and self.vertex_count==rhs.vertex_count\r
                 )\r
 \r
+    def diff(self, rhs):\r
+        #self._diff(rhs, "name")\r
+        self._diff(rhs, "english_name")\r
+        self._diff(rhs, "diffuse_color")\r
+        self._diff(rhs, "alpha")\r
+        self._diff(rhs, "specular_color")\r
+        self._diff(rhs, "specular_factor")\r
+        self._diff(rhs, "ambient_color")\r
+        self._diff(rhs, "flag")\r
+        self._diff(rhs, "edge_color")\r
+        self._diff(rhs, "edge_size")\r
+        self._diff(rhs, "texture_index")\r
+        self._diff(rhs, "sphere_texture_index")\r
+        self._diff(rhs, "sphere_mode")\r
+        self._diff(rhs, "toon_sharing_flag")\r
+        self._diff(rhs, "toon_texture_index")\r
+        self._diff(rhs, "comment")\r
+        self._diff(rhs, "vertex_count")\r
+\r
+    def __ne__(self, rhs):\r
+        return not self.__eq__(rhs)\r
+\r
     def __str__(self):\r
         return ("<pmx.Material {name}>".format(\r
             name=self.english_name\r
             ))\r
 \r
 \r
-class Deform(object):\r
-    pass\r
-\r
-\r
-class Bdef1(object):\r
+class Bdef1(Diff):\r
     """bone deform. use a weight\r
 \r
     Attributes: see __init__\r
@@ -219,11 +355,17 @@ class Bdef1(object):
     def __init__(self, index0):\r
         self.index0=index0\r
 \r
+    def __str__(self):\r
+        return "<Bdef1 {0}>".format(self.index0)\r
+\r
     def __eq__(self, rhs):\r
         return self.index0==rhs.index0\r
 \r
+    def __ne__(self, rhs):\r
+        return not self.__eq__(rhs)\r
+\r
 \r
-class Bdef2(object):\r
+class Bdef2(Diff):\r
     """bone deform. use two weights\r
 \r
     Attributes: see __init__\r
@@ -237,18 +379,38 @@ class Bdef2(object):
         self.index1=index1\r
         self.weight0=weight0\r
 \r
+    def __str__(self):\r
+        return "<Bdef2 {0}, {1}, {2}>".format(self.index0, self.index1, self.weight0)\r
+\r
     def __eq__(self, rhs):\r
         return (\r
                 self.index0==rhs.index0\r
                 and self.index1==rhs.index1\r
-                and self.weight0==rhs.weight0\r
+                #and self.weight0==rhs.weight0\r
+                and abs(self.weight0-rhs.weight0)<1e-5\r
                 )\r
 \r
+    def __ne__(self, rhs):\r
+        return not self.__eq__(rhs)\r
 \r
-class Vertex(object):\r
-    """pmx vertex\r
 \r
-    Attributes: see __init__\r
+class Vertex(Diff):\r
+    """\r
+    ==========\r
+    pmx vertex\r
+    ==========\r
+\r
+    :IVariables:\r
+        position\r
+            Vector3\r
+        normal \r
+            Vector3\r
+        uv \r
+            Vector2\r
+        deform\r
+            Bdef1, Bdef2 or Bdef4\r
+        edge_factor\r
+            float\r
     """\r
     __slots__=[ 'position', 'normal', 'uv', 'deform', 'edge_factor' ]\r
     def __init__(self, \r
@@ -263,6 +425,11 @@ class Vertex(object):
         self.deform=deform\r
         self.edge_factor=edge_factor\r
 \r
+    def __str__(self):\r
+        return "<Vertex position:{0}, normal:{1}, uv:{2}, deform:{3}, edge:{4}".format(\r
+                self.position, self.normal, self.uv, self.deform, self.edge_factor\r
+                )\r
+\r
     def __eq__(self, rhs):\r
         return (\r
                 self.position==rhs.position\r
@@ -272,8 +439,18 @@ class Vertex(object):
                 and self.edge_factor==rhs.edge_factor\r
                 )\r
 \r
+    def __ne__(self, rhs):\r
+        return not self.__eq__(rhs)\r
 \r
-class Morph(object):\r
+    def diff(self, rhs):\r
+        self._diff(rhs, "position")\r
+        self._diff(rhs, "normal")\r
+        self._diff(rhs, "uv")\r
+        self._diff(rhs, "deform")\r
+        self._diff(rhs, "edge_factor")\r
+\r
+\r
+class Morph(Diff):\r
     """pmx morph\r
 \r
     Attributes:\r
@@ -290,12 +467,12 @@ class Morph(object):
             'morph_type',\r
             'offsets',\r
             ]\r
-    def __init__(self, name, english_name, panel, morph_type):\r
+    def __init__(self, name, english_name, panel, morph_type, offsets=[]):\r
         self.name=name\r
         self.english_name=english_name\r
         self.panel=panel\r
         self.morph_type=morph_type\r
-        self.offsets=[]\r
+        self.offsets=offsets\r
 \r
     def __eq__(self, rhs):\r
         return (\r
@@ -306,8 +483,18 @@ class Morph(object):
                 and self.offsets==rhs.offsets\r
                 )\r
 \r
+    def __ne__(self, rhs):\r
+        return not self.__eq__(rhs)\r
+\r
+    def diff(self, rhs):\r
+        self._diff(rhs, 'name')\r
+        self._diff(rhs, 'english_name')\r
+        #self._diff(rhs, 'panel')\r
+        self._diff(rhs, 'morph_type')\r
+        self._diff_array(rhs, 'offsets')\r
 \r
-class VerexMorphOffset(object):\r
+\r
+class VerexMorphOffset(Diff):\r
     """pmx vertex morph offset\r
 \r
     Attributes:\r
@@ -328,8 +515,15 @@ class VerexMorphOffset(object):
                 and self.position_offset==rhs.position_offset\r
                 )\r
 \r
+    def __ne__(self, rhs):\r
+        return not self.__eq__(rhs)\r
+\r
+    def diff(self, rhs):\r
+        self._diff(rhs, 'vertex_index')\r
+        self._diff(rhs, 'position_offset')\r
+\r
 \r
-class DisplaySlot(object):\r
+class DisplaySlot(Diff):\r
     """pmx display slot\r
 \r
     Attributes:\r
@@ -344,11 +538,11 @@ class DisplaySlot(object):
             'special_flag',\r
             'refrences',\r
             ]\r
-    def __init__(self, name, english_name, special_flag):\r
+    def __init__(self, name, english_name, special_flag, refrences=[]):\r
         self.name=name\r
         self.english_name=english_name\r
         self.special_flag=special_flag\r
-        self.refrences=[]\r
+        self.refrences=refrences\r
 \r
     def __eq__(self, rhs):\r
         return (\r
@@ -358,8 +552,17 @@ class DisplaySlot(object):
                 and self.refrences==rhs.refrences\r
                 )\r
 \r
+    def __ne__(self, rhs):\r
+        return not self.__eq__(rhs)\r
 \r
-class RigidBodyParam(object):\r
+    def diff(self, rhs):\r
+        self._diff(rhs, 'name')\r
+        self._diff(rhs, 'english_name')\r
+        self._diff(rhs, 'special_flag')\r
+        #self._diff_array(rhs, 'refrences')\r
+\r
+\r
+class RigidBodyParam(Diff):\r
     """pmx rigidbody param(for bullet)\r
 \r
     Attributes:\r
@@ -393,8 +596,18 @@ class RigidBodyParam(object):
                 and self.friction==rhs.friction\r
                 )\r
 \r
+    def __ne__(self, rhs):\r
+        return not self.__eq__(rhs)\r
+\r
+    def diff(self, rhs):\r
+        self._diff(rhs, 'mass')\r
+        self._diff(rhs, 'linear_damping')\r
+        self._diff(rhs, 'angular_damping')\r
+        self._diff_array(rhs, 'restitution')\r
+        self._diff_array(rhs, 'friction')\r
 \r
-class RigidBody(object):\r
+\r
+class RigidBody(Diff):\r
     """pmx rigidbody\r
 \r
     Attributes:\r
@@ -464,8 +677,24 @@ class RigidBody(object):
                 and self.mode==rhs.mode\r
                 )\r
 \r
+    def __ne__(self, rhs):\r
+        return not self.__eq__(rhs)\r
+\r
+    def diff(self, rhs):\r
+        self._diff(rhs, 'name')\r
+        self._diff(rhs, 'english_name')\r
+        self._diff(rhs, 'bone_index')\r
+        self._diff(rhs, 'collision_group')\r
+        self._diff(rhs, 'no_collision_group')\r
+        self._diff(rhs, 'shape_type')\r
+        self._diff(rhs, 'shape_size')\r
+        #self._diff(rhs, 'shape_position')\r
+        self._diff(rhs, 'shape_rotation')\r
+        self._diff(rhs, 'param')\r
+        self._diff(rhs, 'mode')\r
 \r
-class Joint(object):\r
+\r
+class Joint(Diff):\r
     """pmx joint\r
 \r
     Attributes:\r
@@ -542,31 +771,65 @@ class Joint(object):
                 and self.spring_constant_rotation==rhs.spring_constant_rotation\r
                 )\r
 \r
-\r
-class Model(object):\r
-    """pmx data representation\r
-\r
-    Attributes:\r
-        version: pmx version(expected 2.0)\r
-        name: \r
-        english_name: \r
-        comment: \r
-        english_comment: \r
-        vertices:\r
-        textures:\r
-        materials:\r
-        bones:\r
-        morph:\r
-        display_slots:\r
-        rigidbodies:\r
-        joints:\r
+    def __ne__(self, rhs):\r
+        return not self.__eq__(rhs)\r
+\r
+    def diff(self, rhs):\r
+        self._diff(rhs, 'name')\r
+        self._diff(rhs, 'joint_type')\r
+        self._diff(rhs, 'rigidbody_index_a')\r
+        self._diff(rhs, 'rigidbody_index_b')\r
+        self._diff(rhs, 'position')\r
+        self._diff(rhs, 'rotation')\r
+        self._diff(rhs, 'translation_limit_min')\r
+        self._diff(rhs, 'translation_limit_max')\r
+        self._diff(rhs, 'rotation_limit_min')\r
+        self._diff(rhs, 'rotation_limit_max')\r
+        self._diff(rhs, 'spring_constant_translation')\r
+        self._diff(rhs, 'spring_constant_rotation')\r
+\r
+\r
+class Model(Diff):\r
+    """\r
+    ==========\r
+    pmx model\r
+    ==========\r
+\r
+    :IVariables:\r
+        version\r
+            pmx version(expected 2.0)\r
+        name \r
+            model name\r
+        english_name \r
+            model name\r
+        comment \r
+            comment\r
+        english_comment \r
+            comment\r
+        vertices\r
+            vertex list\r
+        textures\r
+            texture list\r
+        materials\r
+            material list\r
+        bones\r
+            bone list\r
+        morph\r
+            morph list\r
+        display_slots\r
+            display list for bone/morph grouping\r
+        rigidbodies\r
+            bullet physics rigidbody list\r
+        joints\r
+            bullet physics joint list\r
     """\r
     __slots__=[\r
-            'version', # pmx version\r
-            'name', # model name\r
-            'english_name', # model name in english\r
-            'comment', # model comment\r
-            'english_comment', # model comment in english\r
+            'path',\r
+            'version',\r
+            'name',\r
+            'english_name',\r
+            'comment',\r
+            'english_comment',\r
             'vertices',\r
             'indices',\r
             'textures',\r
@@ -577,7 +840,8 @@ class Model(object):
             'rigidbodies',\r
             'joints',\r
             ]\r
-    def __init__(self, version):\r
+    def __init__(self, version=2.0):\r
+        self.path=''\r
         self.version=version\r
         self.name=''\r
         self.english_name=''\r
@@ -618,3 +882,22 @@ class Model(object):
                 and self.joints==rhs.joints\r
                 )\r
 \r
+    def __ne__(self, rhs):\r
+        return not self.__eq__(rhs)\r
+\r
+    def diff(self, rhs):\r
+        self._diff(rhs, "version")\r
+        self._diff(rhs, "name")\r
+        self._diff(rhs, "english_name")\r
+        self._diff(rhs, "comment")\r
+        self._diff(rhs, "english_comment")\r
+        self._diff_array(rhs, "vertices")\r
+        self._diff_array(rhs, "indices")\r
+        self._diff_array(rhs, "textures")\r
+        self._diff_array(rhs, "materials")\r
+        self._diff_array(rhs, "bones")\r
+        self._diff_array(rhs, "morphs")\r
+        self._diff_array(rhs, "display_slots")\r
+        self._diff_array(rhs, "rigidbodies")\r
+        self._diff_array(rhs, "joints")\r
+\r