OSDN Git Service

refactoring
[meshio/pymeshio.git] / pymeshio / mqo / __init__.py
diff --git a/pymeshio/mqo/__init__.py b/pymeshio/mqo/__init__.py
new file mode 100644 (file)
index 0000000..e986d42
--- /dev/null
@@ -0,0 +1,240 @@
+# coding: utf-8\r
+""" \r
+MQOの読み込み\r
+http://www.metaseq.net/metaseq/format.html\r
+"""\r
+\r
+import os\r
+import sys\r
+import math\r
+import pymeshio.common\r
+import pymeshio.mqo.loader\r
+\r
+\r
+"""\r
+MQO loader\r
+"""\r
+class Material(object):\r
+    """mqo material\r
+\r
+    Attributes:\r
+        name: cp932\r
+        shader: \r
+        color: rgba\r
+        diffuse:\r
+        ambient:\r
+        emit:\r
+        specular:\r
+        power:\r
+        tex: cp932 windows file path\r
+    """\r
+    __slots__=[\r
+            "name", "shader", "color", "diffuse", \r
+            "ambient", "emit", "specular", "power",\r
+            "tex",\r
+            ]\r
+    def __init__(self, name):\r
+        self.name=name\r
+        self.shader=3\r
+        self.color=pymeshio.common.RGBA(0.5, 0.5, 0.5, 1.0)\r
+        self.diffuse=1.0\r
+        self.ambient=0.0\r
+        self.emit=0.0\r
+        self.specular=0.0\r
+        self.power=5.0\r
+        self.tex=""\r
+\r
+    def getName(self): return self.name\r
+    def getTexture(self): return self.tex\r
+\r
+    def parse(self, line):\r
+        offset=0\r
+        while True:\r
+            leftParenthesis=line.find("(", offset)\r
+            if leftParenthesis==-1:\r
+                break\r
+            key=line[offset:leftParenthesis]\r
+            rightParenthesis=line.find(")", leftParenthesis+1)\r
+            if rightParenthesis==-1:\r
+                raise ValueError("assert")\r
+\r
+            param=line[leftParenthesis+1:rightParenthesis]\r
+            if key=="shader":\r
+                self.shader=int(param)\r
+            elif key=="col":\r
+                self.color=pymeshio.common.RGBA(*[float(e) for e in param.split()])\r
+            elif key=="dif":\r
+                self.diffuse=float(param)\r
+            elif key=="amb":\r
+                self.ambient=float(param)\r
+            elif key=="emi":\r
+                self.emit=float(param)\r
+            elif key=="spc":\r
+                self.specular=float(param)\r
+            elif key=="power":\r
+                self.power=float(param)\r
+            elif key=="tex":\r
+                self.tex=param[1:-1]\r
+            else:\r
+                print(\r
+                        "%s#parse" % self.name, \r
+                        "unknown key: %s" %  key\r
+                        )\r
+\r
+            offset=rightParenthesis+2\r
+\r
+    def __str__(self):\r
+        return "<Material %s shader: %d [%f, %f, %f, %f] %f>" % (\r
+                self.name, self.shader,\r
+                self.color[0], self.color[1], self.color[2], self.color[3],\r
+                self.diffuse)\r
+\r
+\r
+class Obj(object):\r
+    """mqo object\r
+\r
+    Attributes:\r
+        name: cp932\r
+        depth: object hierarchy \r
+        folding: \r
+        scale:\r
+        rotation:\r
+        translation:\r
+        visible:\r
+        locking:\r
+        shading:\r
+        facet: smoothing threshold\r
+        color:\r
+        color_type:\r
+        mirror: mirroring\r
+        mirror_axis:\r
+        vertices:\r
+        faces:\r
+        edges:\r
+        smoothing:\r
+    """\r
+    __slots__=["name", "depth", "folding", \r
+            "scale", "rotation", "translation",\r
+            "visible", "locking", "shading", "facet",\r
+            "color", "color_type", "mirror", "mirror_axis",\r
+            "vertices", "faces", "edges", "smoothing",\r
+            ]\r
+\r
+    def __init__(self, name):\r
+        self.name=name\r
+        self.vertices=[]\r
+        self.faces=[]\r
+        self.edges=[]\r
+        self.depth=0\r
+        self.folding=0\r
+        self.scale=[1, 1, 1]\r
+        self.rotation=[0, 0, 0]\r
+        self.translation=[0, 0, 0]\r
+        self.visible=15\r
+        self.locking=0\r
+        self.shading=0\r
+        self.facet=59.5\r
+        self.color=[1, 1, 1]\r
+        self.color_type=0\r
+        self.mirror=0\r
+        self.smoothing=0\r
+\r
+    def getName(self): return self.name\r
+\r
+    def addVertex(self, x, y, z):\r
+        self.vertices.append(pymeshio.common.Vector3(x, y, z))\r
+\r
+    def addFace(self, face):\r
+        if face.index_count==2:\r
+            self.edges.append(face)\r
+        else:\r
+            self.faces.append(face)\r
+\r
+    def __str__(self):\r
+        return "<Object %s, %d vertices, %d faces>" % (\r
+                self.name, len(self.vertices), len(self.faces))\r
+\r
+\r
+class Face(object):\r
+    """mqo face\r
+\r
+    Attributes:\r
+        index_count: 2 or 3 or 4\r
+        indices: index x index_count\r
+        material_index:\r
+        col: vertex_color x index_count\r
+        uv: Vector2 x index_count\r
+    """\r
+    __slots__=[\r
+            "index_count",\r
+            "indices", "material_index", "col", "uv",\r
+            ]\r
+    def __init__(self, index_count, line):\r
+        if index_count<2 or index_count>4:\r
+            raise ValueError("invalid vertex count: %d" % index_count)\r
+        self.material_index=0\r
+        self.col=[]\r
+        self.uv=[pymeshio.common.Vector2(0, 0)]*4\r
+        self.index_count=index_count\r
+        offset=0\r
+        while True:\r
+            leftParenthesis=line.find("(", offset)\r
+            if leftParenthesis==-1:\r
+                break\r
+            key=line[offset:leftParenthesis]\r
+            rightParenthesis=line.find(")", leftParenthesis+1)\r
+            if rightParenthesis==-1:\r
+                raise ValueError("assert")\r
+            params=line[leftParenthesis+1:rightParenthesis].split()\r
+            if key=="V":\r
+                self.indices=[int(e) for e in params]\r
+            elif key=="M":\r
+                self.material_index=int(params[0])\r
+            elif key=="UV":\r
+                uv_list=[float(e) for e in params]\r
+                self.uv=[]\r
+                for i in range(0, len(uv_list), 2):\r
+                    self.uv.append(pymeshio.common.Vector2(uv_list[i], uv_list[i+1]))\r
+            elif key=="COL":\r
+                for n in params:\r
+                    d=int(n)\r
+                    # R\r
+                    d, m=divmod(d, 256)\r
+                    self.col.append(m)\r
+                    # G\r
+                    d, m=divmod(d, 256)\r
+                    self.col.append(m)\r
+                    # B\r
+                    d, m=divmod(d, 256)\r
+                    self.col.append(m)\r
+                    # A\r
+                    d, m=divmod(d, 256)\r
+                    self.col.append(m)\r
+            else:\r
+                print("Face#__init__:unknown key: %s" % key)\r
+\r
+            offset=rightParenthesis+2\r
+\r
+    def getIndex(self, i): return self.indices[i]\r
+    def getUV(self, i): return self.uv[i] if i<len(self.uv) else pymeshio.common.Vector2(0, 0)\r
+\r
+\r
+class Model(object):\r
+    def __init__(self):\r
+        self.has_mikoto=False\r
+        self.materials=[]\r
+        self.objects=[]\r
+\r
+\r
+class IO(object):\r
+    def __init__(self):\r
+        pass\r
+\r
+    def read(self, path):\r
+        model=pymeshio.mqo.loader.load(path)\r
+        if model:\r
+            self.has_mikoto=model.has_mikoto\r
+            self.materials=model.materials\r
+            self.objects=model.objects\r
+            return True\r
+\r