OSDN Git Service

implement pmx loading
[meshio/pymeshio.git] / examples / mqobuilder.py
1 #!/usr/bin/env python\r
2 # coding: utf-8\r
3 \r
4 import time\r
5 import os\r
6 import pymeshio.mqo\r
7 import opengl.material\r
8 import opengl.texture\r
9 import opengl.vertexarraymap\r
10 \r
11 \r
12 def build(path):\r
13     # load scenee\r
14     t=time.time()\r
15     io=pymeshio.mqo.IO()\r
16     if not io.read(path):\r
17         return\r
18     print(time.time()-t, "sec")\r
19     # build\r
20     basedir=os.path.dirname(path)\r
21     vertexArrayMap=opengl.vertexarraymap.VertexArrayMapWithUV()\r
22     for m in io.materials:\r
23         material=opengl.material.MQOMaterial()\r
24         material.rgba=(m.color.r, m.color.g, m.color.b, m.color.a)\r
25         if m.tex:\r
26             texturepath=os.path.join(basedir, m.tex)\r
27             material.texture=opengl.texture.Texture(texturepath)\r
28         vertexArrayMap.addMaterial(material)\r
29 \r
30     for o in io.objects:\r
31         # skip mikoto objects\r
32         if o.name.startswith("anchor"):\r
33             continue\r
34         if o.name.startswith("bone:"):\r
35             continue\r
36         if o.name.startswith("MCS:"):\r
37             continue\r
38 \r
39         for f in o.faces:\r
40             if f.index_count==3:\r
41                 vertexArrayMap.addTriangle(\r
42                         f.material_index,\r
43                         o.vertices[f.indices[0]],\r
44                         o.vertices[f.indices[1]],\r
45                         o.vertices[f.indices[2]],\r
46                         f.uv[0], f.uv[1], f.uv[2]\r
47                         )\r
48             elif f.index_count==4:\r
49                 # triangle 1\r
50                 vertexArrayMap.addTriangle(\r
51                         f.material_index,\r
52                         o.vertices[f.indices[0]],\r
53                         o.vertices[f.indices[1]],\r
54                         o.vertices[f.indices[2]],\r
55                         f.uv[0], f.uv[1], f.uv[2]\r
56                         )\r
57                 # triangle 2\r
58                 vertexArrayMap.addTriangle(\r
59                         f.material_index,\r
60                         o.vertices[f.indices[2]],\r
61                         o.vertices[f.indices[3]],\r
62                         o.vertices[f.indices[0]],\r
63                         f.uv[2], f.uv[3], f.uv[0]\r
64                         )\r
65 \r
66     vertexArrayMap.optimize()\r
67     return vertexArrayMap\r
68 \r