X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=pymeshio%2Fpmx%2F__init__.py;h=4db73d17a79d6dff8fd7c8b2c1da7f73298e6f84;hb=274167c4b811b059f7c5fdca47c2113ca185399c;hp=6d8a831bc4eff08690ac2739c5177477b858bdd3;hpb=9cbcf2ff293043fd2c46785621fb9e150786a064;p=meshio%2Fpymeshio.git diff --git a/pymeshio/pmx/__init__.py b/pymeshio/pmx/__init__.py index 6d8a831..4db73d1 100644 --- a/pymeshio/pmx/__init__.py +++ b/pymeshio/pmx/__init__.py @@ -1,10 +1,22 @@ #!/usr/bin/env python # coding: utf-8 """ -pmx file io library. +======================== +MikuMikuDance PMX format +======================== + +file format +~~~~~~~~~~~ +* PMDEditor's Lib/PMX仕様/PMX仕様.txt + +specs +~~~~~ +* textencoding: unicode +* coordinate: left handed y-up(DirectX) +* uv origin: +* face: only triangle +* backculling: -pmx file format: - PMDEditor's Lib/PMX仕様/PMX仕様.txt """ __author__="ousttrue" __license__="zlib" @@ -18,7 +30,42 @@ from pymeshio import common -class Ik(object): +class DifferenceException(Exception): + pass + + +class Diff(object): + def _diff(self, rhs, key): + l=getattr(self, key) + r=getattr(rhs, key) + if l!=r: + print(l) + print(r) + raise DifferenceException(key) + + def _diff_array(self, rhs, key): + la=getattr(self, key) + ra=getattr(rhs, key) + if len(la)!=len(ra): + raise DifferenceException("%s diffrence %d with %d" % (key, len(la), len(ra))) + for i, (l, r) in enumerate(zip(la, ra)): + if isinstance(l, Diff): + try: + l.diff(r) + except DifferenceException as e: + print(i) + print(l) + print(r) + raise DifferenceException("{0}: {1}".format(key, e.message)) + else: + if l!=r: + print(i) + print(l) + print(r) + raise DifferenceException("{0}".format(key)) + + +class Ik(Diff): """ik info """ __slots__=[ @@ -27,14 +74,28 @@ class Ik(object): 'limit_radian', 'link', ] - def __init__(self, target_index, loop, limit_radian): + def __init__(self, target_index, loop, limit_radian, link=[]): self.target_index=target_index self.loop=loop self.limit_radian=limit_radian - self.link=[] + self.link=link + + def __eq__(self, rhs): + return ( + self.target_index==rhs.target_index + and self.loop==rhs.loop + and self.limit_radian==rhs.limit_radian + and self.link==rhs.link + ) + def diff(self, rhs): + self._diff(rhs, 'target_index') + self._diff(rhs, 'loop') + self._diff(rhs, 'limit_radian') + self._diff_array(rhs, 'link') -class IkLink(object): + +class IkLink(Diff): """ik link info """ __slots__=[ @@ -43,14 +104,28 @@ class IkLink(object): 'limit_min', 'limit_max', ] - def __init__(self, bone_index, limit_angle): + def __init__(self, bone_index, limit_angle, limit_min=common.Vector3(), limit_max=common.Vector3()): self.bone_index=bone_index self.limit_angle=limit_angle - self.limit_min=None - self.limit_max=None + self.limit_min=limit_min + self.limit_max=limit_max + + def __eq__(self, rhs): + return ( + self.bone_index==rhs.bone_index + and self.limit_angle==rhs.limit_angle + and self.limit_min==rhs.limit_min + and self.limit_max==rhs.limit_max + ) + + def diff(self, rhs): + self._diff(rhs, 'bone_index') + self._diff(rhs, 'limit_angle') + self._diff(rhs, 'limit_min') + self._diff(rhs, 'limit_max') -class Bone(object): +class Bone(Diff): """material Bone: see __init__ @@ -63,7 +138,7 @@ class Bone(object): 'layer', 'flag', - 'tail_positoin', + 'tail_position', 'tail_index', 'effect_index', 'effect_factor', @@ -74,43 +149,94 @@ class Bone(object): 'ik', ] def __init__(self, - name: str, - english_name: str, - position: common.Vector3, - parent_index: int, - layer: int, - flag: int + name, + english_name, + position, + parent_index, + layer, + flag, + tail_position=common.Vector3(), + tail_index=-1, + effect_index=-1, + effect_factor=0.0, + fixed_axis=common.Vector3(), + local_x_vector=common.Vector3(), + local_z_vector=common.Vector3(), + external_key=-1, + ik=None ): - self.name=name, + self.name=name self.english_name=english_name self.position=position self.parent_index=parent_index self.layer=layer self.flag=flag - - def getConnectionFlag(self) -> int: + self.tail_position=tail_position + self.tail_index=tail_index + self.effect_index=effect_index + self.effect_factor=effect_factor + self.fixed_axis=fixed_axis + self.local_x_vector=local_x_vector + self.local_z_vector=local_z_vector + self.external_key=external_key + self.ik=ik + + def __eq__(self, rhs): + return ( + self.name==rhs.name + and self.english_name==rhs.english_name + and self.position==rhs.position + and self.parent_index==rhs.parent_index + and self.layer==rhs.layer + and self.flag==rhs.flag + ) + + def __ne__(self, rhs): + return not self.__eq__(rhs) + + def diff(self, rhs): + self._diff(rhs, 'name') + self._diff(rhs, 'english_name') + self._diff(rhs, 'position') + self._diff(rhs, 'parent_index') + #self._diff(rhs, 'layer') + self._diff(rhs, 'flag') + self._diff(rhs, 'tail_position') + self._diff(rhs, 'tail_index') + #self._diff(rhs, 'effect_index') + #self._diff(rhs, 'effect_factor') + #self._diff(rhs, 'fixed_axis') + self._diff(rhs, 'local_x_vector') + self._diff(rhs, 'local_z_vector') + self._diff(rhs, 'external_key') + if self.ik and rhs.ik: + self.ik.diff(rhs.ik) + else: + self._diff(rhs, 'ik') + + def getConnectionFlag(self): return self.flag & 0x0001 - def getIkFlag(self) -> int: + def getIkFlag(self): return (self.flag & 0x0020) >> 5 - def getRotationFlag(self) -> int: + def getRotationFlag(self): return (self.flag & 0x0100) >> 8 - def getTranslationFlag(self) -> int: + def getTranslationFlag(self): return (self.flag & 0x0200) >> 9 - def getFixedAxisFlag(self) -> int: + def getFixedAxisFlag(self): return (self.flag & 0x0400) >> 10 - def getLocalCoordinateFlag(self) -> int: + def getLocalCoordinateFlag(self): return (self.flag & 0x0800) >> 11 - def getExternalParentDeformFlag(self) -> int: + def getExternalParentDeformFlag(self): return (self.flag & 0x2000) >> 13 -class Material(object): +class Material(Diff): """material Attributes: see __init__ @@ -119,7 +245,7 @@ class Material(object): 'name', 'english_name', 'diffuse_color', - 'diffuse_alpha', + 'alpha', 'specular_color', 'specular_factor', 'ambient_color', @@ -127,33 +253,36 @@ class Material(object): 'edge_color', 'edge_size', 'texture_index', - 'sphia_texture_index', - 'sphia_mode', + 'sphere_texture_index', + 'sphere_mode', 'toon_sharing_flag', 'toon_texture_index', 'comment', - 'index_count', + 'vertex_count', ] def __init__(self, - name: str, - english_name: str, - diffuse_color: common.RGB, - diffuse_alpha: float, - specular_color: common.RGB, - specular_factor: float, - ambient_color: common.RGB, - flag: int, - edge_color: common.RGBA, - edge_size: float, - texture_index: int, - sphia_texture_index: int, - sphia_mode: int, - toon_sharing_flag: int + name, + english_name, + diffuse_color, + alpha, + specular_factor, + specular_color, + ambient_color, + flag, + edge_color, + edge_size, + texture_index, + sphere_texture_index, + sphere_mode, + toon_sharing_flag, + toon_texture_index=0, + comment=common.unicode(""), + vertex_count=0, ): self.name=name self.english_name=english_name self.diffuse_color=diffuse_color - self.diffuse_alpha=diffuse_alpha + self.alpha=alpha self.specular_color=specular_color self.specular_factor=specular_factor self.ambient_color=ambient_color @@ -161,91 +290,559 @@ class Material(object): self.edge_color=edge_color self.edge_size=edge_size self.texture_index=texture_index - self.sphia_texture_index=sphia_texture_index - self.sphia_mode=sphia_mode + self.sphere_texture_index=sphere_texture_index + self.sphere_mode=sphere_mode self.toon_sharing_flag=toon_sharing_flag - # - self.toon_texture_index=None - self.comment='' - self.index_count=0 - - -class Deform(object): - pass - - -class Bdef1(object): + self.toon_texture_index=toon_texture_index + self.comment=comment + self.vertex_count=vertex_count + + def __eq__(self, rhs): + return ( + self.name==rhs.name + and self.english_name==rhs.english_name + and self.diffuse_color==rhs.diffuse_color + and self.alpha==rhs.alpha + and self.specular_color==rhs.specular_color + and self.specular_factor==rhs.specular_factor + and self.ambient_color==rhs.ambient_color + and self.flag==rhs.flag + and self.edge_color==rhs.edge_color + and self.edge_size==rhs.edge_size + and self.texture_index==rhs.texture_index + and self.sphere_texture_index==rhs.sphere_texture_index + and self.sphere_mode==rhs.sphere_mode + and self.toon_sharing_flag==rhs.toon_sharing_flag + and self.toon_texture_index==rhs.toon_texture_index + and self.comment==rhs.comment + and self.vertex_count==rhs.vertex_count + ) + + def diff(self, rhs): + #self._diff(rhs, "name") + self._diff(rhs, "english_name") + self._diff(rhs, "diffuse_color") + self._diff(rhs, "alpha") + self._diff(rhs, "specular_color") + self._diff(rhs, "specular_factor") + self._diff(rhs, "ambient_color") + self._diff(rhs, "flag") + self._diff(rhs, "edge_color") + self._diff(rhs, "edge_size") + self._diff(rhs, "texture_index") + self._diff(rhs, "sphere_texture_index") + self._diff(rhs, "sphere_mode") + self._diff(rhs, "toon_sharing_flag") + self._diff(rhs, "toon_texture_index") + self._diff(rhs, "comment") + self._diff(rhs, "vertex_count") + + def __ne__(self, rhs): + return not self.__eq__(rhs) + + def __str__(self): + return ("".format( + name=self.english_name + )) + + +class Bdef1(Diff): """bone deform. use a weight Attributes: see __init__ """ - __slots__=[ 'bone_index'] - def __init__(self, bone_index: int): - self.bone_index=bone_index + __slots__=[ 'index0'] + def __init__(self, index0): + self.index0=index0 + + def __str__(self): + return "".format(self.index0) + + def __eq__(self, rhs): + return self.index0==rhs.index0 + + def __ne__(self, rhs): + return not self.__eq__(rhs) -class Bdef2(object): +class Bdef2(Diff): """bone deform. use two weights Attributes: see __init__ """ __slots__=[ 'index0', 'index1', 'weight0'] def __init__(self, - index0: int, - index1: int, - weight0: float): + index0, + index1, + weight0): self.index0=index0 self.index1=index1 self.weight0=weight0 + def __str__(self): + return "".format(self.index0, self.index1, self.weight0) -class Vertex(object): - """pmx vertex + def __eq__(self, rhs): + return ( + self.index0==rhs.index0 + and self.index1==rhs.index1 + #and self.weight0==rhs.weight0 + and abs(self.weight0-rhs.weight0)<1e-5 + ) - Attributes: see __init__ + def __ne__(self, rhs): + return not self.__eq__(rhs) + + +class Vertex(Diff): + """ + ========== + pmx vertex + ========== + + :IVariables: + position + Vector3 + normal + Vector3 + uv + Vector2 + deform + Bdef1, Bdef2 or Bdef4 + edge_factor + float """ __slots__=[ 'position', 'normal', 'uv', 'deform', 'edge_factor' ] def __init__(self, - position: common.Vector3, - normal: common.Vector3, - uv: common.Vector2, - deform: Deform, - edge_factor: float): + position, + normal, + uv, + deform, + edge_factor): self.position=position self.normal=normal self.uv=uv self.deform=deform self.edge_factor=edge_factor + def __str__(self): + return "'.format( + version=self.version, + name=self.english_name, + vertices=len(self.vertices) + )) + + def __eq__(self, rhs): + return ( + self.version==rhs.version + and self.name==rhs.name + and self.english_name==rhs.english_name + and self.comment==rhs.comment + and self.english_comment==rhs.english_comment + and self.vertices==rhs.vertices + and self.indices==rhs.indices + and self.textures==rhs.textures + and self.materials==rhs.materials + and self.bones==rhs.bones + and self.morphs==rhs.morphs + and self.display_slots==rhs.display_slots + and self.rigidbodies==rhs.rigidbodies + and self.joints==rhs.joints + ) + + def __ne__(self, rhs): + return not self.__eq__(rhs) + + def diff(self, rhs): + self._diff(rhs, "version") + self._diff(rhs, "name") + self._diff(rhs, "english_name") + self._diff(rhs, "comment") + self._diff(rhs, "english_comment") + self._diff_array(rhs, "vertices") + self._diff_array(rhs, "indices") + self._diff_array(rhs, "textures") + self._diff_array(rhs, "materials") + self._diff_array(rhs, "bones") + self._diff_array(rhs, "morphs") + self._diff_array(rhs, "display_slots") + self._diff_array(rhs, "rigidbodies") + self._diff_array(rhs, "joints")