OSDN Git Service

implement pmx.writer
[meshio/pymeshio.git] / pymeshio / pmx / __init__.py
index f267af1..42b15f6 100644 (file)
@@ -74,39 +74,49 @@ class Bone(object):
             'ik',\r
             ]\r
     def __init__(self,\r
-            name: str,\r
-            english_name: str,\r
-            position: common.Vector3,\r
-            parent_index: int,\r
-            layer: int,\r
-            flag: int\r
+            name,\r
+            english_name,\r
+            position,\r
+            parent_index,\r
+            layer,\r
+            flag\r
             ):\r
-        self.name=name,\r
+        self.name=name\r
         self.english_name=english_name\r
         self.position=position\r
         self.parent_index=parent_index\r
         self.layer=layer\r
         self.flag=flag\r
 \r
-    def getConnectionFlag(self) -> int:\r
+    def __eq__(self, rhs):\r
+        return (\r
+                self.name==rhs.name\r
+                and self.english_name==rhs.english_name\r
+                and self.position==rhs.position\r
+                and self.parent_index==rhs.parent_index\r
+                and self.layer==rhs.layer\r
+                and self.flag==rhs.flag\r
+                )\r
+\r
+    def getConnectionFlag(self):\r
         return self.flag & 0x0001\r
 \r
-    def getIkFlag(self) -> int:\r
+    def getIkFlag(self):\r
         return (self.flag & 0x0020) >> 5\r
 \r
-    def getRotationFlag(self) -> int:\r
+    def getRotationFlag(self):\r
         return (self.flag & 0x0100) >> 8\r
 \r
-    def getTranslationFlag(self) -> int:\r
+    def getTranslationFlag(self):\r
         return (self.flag & 0x0200) >> 9\r
 \r
-    def getFixedAxisFlag(self) -> int:\r
+    def getFixedAxisFlag(self):\r
         return (self.flag & 0x0400) >> 10\r
 \r
-    def getLocalCoordinateFlag(self) -> int:\r
+    def getLocalCoordinateFlag(self):\r
         return (self.flag &  0x0800) >> 11\r
     \r
-    def getExternalParentDeformFlag(self) -> int:\r
+    def getExternalParentDeformFlag(self):\r
         return (self.flag &  0x2000) >> 13\r
 \r
  \r
@@ -119,7 +129,7 @@ class Material(object):
             'name',\r
             'english_name',\r
             'diffuse_color',\r
-            'diffuse_alpha',\r
+            'alpha',\r
             'specular_color',\r
             'specular_factor',\r
             'ambient_color',\r
@@ -127,33 +137,33 @@ class Material(object):
             'edge_color',\r
             'edge_size',\r
             'texture_index',\r
-            'sphia_texture_index',\r
-            'sphia_mode',\r
+            'sphere_texture_index',\r
+            'sphere_mode',\r
             'toon_sharing_flag',\r
             'toon_texture_index',\r
             'comment',\r
-            'index_count',\r
+            'vertex_count',\r
             ]\r
     def __init__(self,\r
-            name: str,\r
-            english_name: str,\r
-            diffuse_color: common.RGB,\r
-            diffuse_alpha: float,\r
-            specular_color: common.RGB,\r
-            specular_factor: float,\r
-            ambient_color: common.RGB,\r
-            flag: int,\r
-            edge_color: common.RGBA,\r
-            edge_size: float,\r
-            texture_index: int,\r
-            sphia_texture_index: int,\r
-            sphia_mode: int,\r
-            toon_sharing_flag: int\r
+            name,\r
+            english_name,\r
+            diffuse_color,\r
+            alpha,\r
+            specular_color,\r
+            specular_factor,\r
+            ambient_color,\r
+            flag,\r
+            edge_color,\r
+            edge_size,\r
+            texture_index,\r
+            sphere_texture_index,\r
+            sphere_mode,\r
+            toon_sharing_flag\r
             ):\r
         self.name=name\r
         self.english_name=english_name\r
         self.diffuse_color=diffuse_color\r
-        self.diffuse_alpha=diffuse_alpha\r
+        self.alpha=alpha\r
         self.specular_color=specular_color\r
         self.specular_factor=specular_factor\r
         self.ambient_color=ambient_color\r
@@ -161,13 +171,39 @@ class Material(object):
         self.edge_color=edge_color\r
         self.edge_size=edge_size\r
         self.texture_index=texture_index\r
-        self.sphia_texture_index=sphia_texture_index\r
-        self.sphia_mode=sphia_mode\r
+        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=''\r
-        self.index_count=0\r
+        self.comment=name.__class__() # unicode\r
+        self.vertex_count=0\r
+\r
+    def __eq__(self, rhs):\r
+        return (\r
+                self.name==rhs.name\r
+                and self.english_name==rhs.english_name\r
+                and self.diffuse_color==rhs.diffuse_color\r
+                and self.alpha==rhs.alpha\r
+                and self.specular_color==rhs.specular_color\r
+                and self.specular_factor==rhs.specular_factor\r
+                and self.ambient_color==rhs.ambient_color\r
+                and self.flag==rhs.flag\r
+                and self.edge_color==rhs.edge_color\r
+                and self.edge_size==rhs.edge_size\r
+                and self.texture_index==rhs.texture_index\r
+                and self.sphere_texture_index==rhs.sphere_texture_index\r
+                and self.sphere_mode==rhs.sphere_mode\r
+                and self.toon_sharing_flag==rhs.toon_sharing_flag\r
+                and self.toon_texture_index==rhs.toon_texture_index\r
+                and self.comment==rhs.comment\r
+                and self.vertex_count==rhs.vertex_count\r
+                )\r
+\r
+    def __str__(self):\r
+        return ("<pmx.Material {name}>".format(\r
+            name=self.english_name\r
+            ))\r
 \r
 \r
 class Deform(object):\r
@@ -179,9 +215,12 @@ class Bdef1(object):
 \r
     Attributes: see __init__\r
     """\r
-    __slots__=[ 'bone_index']\r
-    def __init__(self, bone_index: int):\r
-        self.bone_index=bone_index\r
+    __slots__=[ 'index0']\r
+    def __init__(self, index0):\r
+        self.index0=index0\r
+\r
+    def __eq__(self, rhs):\r
+        return self.index0==rhs.index0\r
 \r
 \r
 class Bdef2(object):\r
@@ -191,13 +230,20 @@ class Bdef2(object):
     """\r
     __slots__=[ 'index0', 'index1', 'weight0']\r
     def __init__(self, \r
-            index0: int,\r
-            index1: int,\r
-            weight0: float):\r
+            index0,\r
+            index1,\r
+            weight0):\r
         self.index0=index0\r
         self.index1=index1\r
         self.weight0=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
+                )\r
+\r
 \r
 class Vertex(object):\r
     """pmx vertex\r
@@ -206,17 +252,26 @@ class Vertex(object):
     """\r
     __slots__=[ 'position', 'normal', 'uv', 'deform', 'edge_factor' ]\r
     def __init__(self, \r
-            position: common.Vector3\r
-            normal: common.Vector3\r
-            uv: common.Vector2\r
-            deform: Deform\r
-            edge_factor: float):\r
+            position, \r
+            normal, \r
+            uv, \r
+            deform, \r
+            edge_factor):\r
         self.position=position \r
         self.normal=normal\r
         self.uv=uv\r
         self.deform=deform\r
         self.edge_factor=edge_factor\r
 \r
+    def __eq__(self, rhs):\r
+        return (\r
+                self.position==rhs.position\r
+                and self.normal==rhs.normal\r
+                and self.uv==rhs.uv\r
+                and self.deform==rhs.deform\r
+                and self.edge_factor==rhs.edge_factor\r
+                )\r
+\r
 \r
 class Morph(object):\r
     """pmx morph\r
@@ -242,6 +297,15 @@ class Morph(object):
         self.morph_type=morph_type\r
         self.offsets=[]\r
 \r
+    def __eq__(self, rhs):\r
+        return (\r
+                self.name==rhs.name\r
+                and self.english_name==rhs.english_name\r
+                and self.panel==rhs.panel\r
+                and self.morph_type==rhs.morph_type\r
+                and self.offsets==rhs.offsets\r
+                )\r
+\r
 \r
 class VerexMorphOffset(object):\r
     """pmx vertex morph offset\r
@@ -258,6 +322,12 @@ class VerexMorphOffset(object):
         self.vertex_index=vertex_index\r
         self.position_offset=position_offset\r
 \r
+    def __eq__(self, rhs):\r
+        return (\r
+                self.vertex_index==rhs.vertex_index \r
+                and self.position_offset==rhs.position_offset\r
+                )\r
+\r
 \r
 class DisplaySlot(object):\r
     """pmx display slot\r
@@ -280,30 +350,13 @@ class DisplaySlot(object):
         self.special_flag=special_flag\r
         self.refrences=[]\r
 \r
-\r
-class Shape(object):\r
-    pass\r
-\r
-\r
-class SphereShape(Shape):\r
-    __slots__=['radius']\r
-    def __init__(self, radius):\r
-        self.radius=radius\r
-\r
-\r
-class CapsuleShape(Shape):\r
-    __slots__=['short_radius', 'long_radius']\r
-    def __init__(self, short_radius, long_radius): \r
-        self.short_radius=short_radius\r
-        self.long_radius=long_radius\r
-\r
-\r
-class BoxShape(Shape):\r
-    __slots__=['x', 'y', 'z']\r
-    def __init__(self, x, y, z):\r
-        self.x=x\r
-        self.y=y\r
-        self.z=z\r
+    def __eq__(self, rhs):\r
+        return (\r
+                self.name==rhs.name\r
+                and self.english_name==rhs.english_name\r
+                and self.special_flag==rhs.special_flag\r
+                and self.refrences==rhs.refrences\r
+                )\r
 \r
 \r
 class RigidBodyParam(object):\r
@@ -331,6 +384,16 @@ class RigidBodyParam(object):
         self.restitution=restitution\r
         self.friction=friction\r
 \r
+    def __eq__(self, rhs):\r
+        return (\r
+                self.mass==rhs.mass\r
+                and self.linear_damping==rhs.linear_damping\r
+                and self.angular_damping==rhs.angular_damping\r
+                and self.restitution==rhs.restitution\r
+                and self.friction==rhs.friction\r
+                )\r
+\r
+\r
 class RigidBody(object):\r
     """pmx rigidbody\r
 \r
@@ -339,7 +402,7 @@ class RigidBody(object):
         english_name: \r
         bone_index:\r
         collision_group:\r
-        no_collision_flag:\r
+        no_collision_group:\r
         shape:\r
         param:\r
         mode:\r
@@ -349,8 +412,11 @@ class RigidBody(object):
             'english_name',\r
             'bone_index',\r
             'collision_group',\r
-            'no_collision_flag',\r
-            'shape',\r
+            'no_collision_group',\r
+            'shape_type',\r
+            'shape_size',\r
+            'shape_position',\r
+            'shape_rotation',\r
             'param',\r
             'mode',\r
             ]\r
@@ -359,7 +425,7 @@ class RigidBody(object):
             english_name,\r
             bone_index,\r
             collision_group,\r
-            no_collision_flag,\r
+            no_collision_group,\r
             shape_type,\r
             shape_size,\r
             shape_position,\r
@@ -375,21 +441,107 @@ class RigidBody(object):
         self.english_name=english_name\r
         self.bone_index=bone_index\r
         self.collision_group=collision_group\r
-        self.no_collision_flag=no_collision_flag\r
-        if shape_type==0:\r
-            self.shape=SphereShape(shape_size.x)\r
-        elif shape_type==1:\r
-            self.shape=BoxShape(shape_size.x, shape_size.y, shape_size.z)\r
-        elif shape_type==2:\r
-            self.shape=CapsuleShape(shape_size.x, shape_size.y)\r
-        else:\r
-            raise pymeshio.common.ParseException(\r
-                    "unknown shape_type: {0}".format(shape_type))\r
+        self.no_collision_group=no_collision_group\r
+        self.shape_type=shape_type\r
+        self.shape_size=shape_size\r
+        self.shape_position=shape_position\r
+        self.shape_rotation=shape_rotation\r
         self.param=RigidBodyParam(mass,\r
                 linear_damping, angular_damping,\r
                 restitution, friction)\r
         self.mode=mode\r
 \r
+    def __eq__(self, rhs):\r
+        return (\r
+                self.name==rhs.name\r
+                and self.english_name==rhs.english_name\r
+                and self.bone_index==rhs.bone_index\r
+                and self.collision_group==rhs.collision_group\r
+                and self.no_collision_group==rhs.no_collision_group\r
+                and self.shape_type==rhs.shape_type\r
+                and self.shape_size==rhs.shape_size\r
+                and self.param==rhs.param\r
+                and self.mode==rhs.mode\r
+                )\r
+\r
+\r
+class Joint(object):\r
+    """pmx joint\r
+\r
+    Attributes:\r
+        name: \r
+        english_name: \r
+        joint_type:\r
+        rigidbody_index_a:\r
+        rigidbody_index_b:\r
+        position: Vector3\r
+        rotation: Vector3\r
+        translation_limit_min: Vector3\r
+        translation_limit_max: Vector3\r
+        rotation_limit_min: Vector3\r
+        rotation_limit_max: Vector3\r
+        spring_constant_translation: Vector3\r
+        spring_constant_rotation: Vector3\r
+    """\r
+    __slots__=[\r
+            'name',\r
+            'english_name',\r
+            'joint_type',\r
+            'rigidbody_index_a',\r
+            'rigidbody_index_b',\r
+            'position',\r
+            'rotation',\r
+            'translation_limit_min',\r
+            'translation_limit_max',\r
+            'rotation_limit_min',\r
+            'rotation_limit_max',\r
+            'spring_constant_translation',\r
+            'spring_constant_rotation',\r
+            ]\r
+    def __init__(self, name, english_name,\r
+            joint_type,\r
+            rigidbody_index_a,\r
+            rigidbody_index_b,\r
+            position,\r
+            rotation,\r
+            translation_limit_min,\r
+            translation_limit_max,\r
+            rotation_limit_min,\r
+            rotation_limit_max,\r
+            spring_constant_translation,\r
+            spring_constant_rotation\r
+            ):\r
+        self.name=name\r
+        self.english_name=english_name\r
+        self.joint_type=joint_type\r
+        self.rigidbody_index_a=rigidbody_index_a\r
+        self.rigidbody_index_b=rigidbody_index_b\r
+        self.position=position\r
+        self.rotation=rotation\r
+        self.translation_limit_min=translation_limit_min\r
+        self.translation_limit_max=translation_limit_max\r
+        self.rotation_limit_min=rotation_limit_min\r
+        self.rotation_limit_max=rotation_limit_max\r
+        self.spring_constant_translation=spring_constant_translation\r
+        self.spring_constant_rotation=spring_constant_rotation\r
+\r
+    def __eq__(self, rhs):\r
+        return (\r
+                self.name==rhs.name\r
+                and self.english_name==rhs.english_name\r
+                and self.joint_type==rhs.joint_type\r
+                and self.rigidbody_index_a==rhs.rigidbody_index_a\r
+                and self.rigidbody_index_b==rhs.rigidbody_index_b\r
+                and self.position==rhs.position\r
+                and self.rotation==rhs.rotation\r
+                and self.translation_limit_min==rhs.translation_limit_min\r
+                and self.translation_limit_max==rhs.translation_limit_max\r
+                and self.rotation_limit_min==rhs.rotation_limit_min\r
+                and self.rotation_limit_max==rhs.rotation_limit_max\r
+                and self.spring_constant_translation==rhs.spring_constant_translation\r
+                and self.spring_constant_rotation==rhs.spring_constant_rotation\r
+                )\r
+\r
 \r
 class Model(object):\r
     """pmx data representation\r
@@ -407,6 +559,7 @@ class Model(object):
         morph:\r
         display_slots:\r
         rigidbodies:\r
+        joints:\r
     """\r
     __slots__=[\r
             'version', # pmx version\r
@@ -422,6 +575,7 @@ class Model(object):
             'morphs',\r
             'display_slots',\r
             'rigidbodies',\r
+            'joints',\r
             ]\r
     def __init__(self, version):\r
         self.version=version\r
@@ -434,5 +588,33 @@ class Model(object):
         self.textures=[]\r
         self.materials=[]\r
         self.bones=[]\r
+        self.morphs=[]\r
+        self.display_slots=[]\r
         self.rigidbodies=[]\r
+        self.joints=[]\r
+\r
+    def __str__(self):\r
+        return ('<pmx-{version} "{name}" {vertices}vertices>'.format(\r
+            version=self.version,\r
+            name=self.english_name,\r
+            vertices=len(self.vertices)\r
+            ))\r
+\r
+    def __eq__(self, rhs):\r
+        return (\r
+                self.version==rhs.version\r
+                and self.name==rhs.name\r
+                and self.english_name==rhs.english_name\r
+                and self.comment==rhs.comment\r
+                and self.english_comment==rhs.english_comment\r
+                and self.vertices==rhs.vertices\r
+                and self.indices==rhs.indices\r
+                and self.textures==rhs.textures\r
+                and self.materials==rhs.materials\r
+                and self.bones==rhs.bones\r
+                and self.morphs==rhs.morphs\r
+                and self.display_slots==rhs.display_slots\r
+                and self.rigidbodies==rhs.rigidbodies\r
+                and self.joints==rhs.joints\r
+                )\r
 \r