OSDN Git Service

implementing export_pmx...
[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.reader\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     t=time.time()\r
14     model=pymeshio.mqo.reader.read_from_file(path)\r
15     if not model:\r
16         return\r
17     print(time.time()-t, "sec")\r
18     # build\r
19     basedir=os.path.dirname(path)\r
20     vertexArrayMap=opengl.vertexarraymap.VertexArrayMapWithUV()\r
21     for m in model.materials:\r
22         material=opengl.material.MQOMaterial()\r
23         material.rgba=(m.color.r, m.color.g, m.color.b, m.color.a)\r
24         if m.tex:\r
25             texturepath=os.path.join(basedir, m.tex.decode('cp932'))\r
26             material.texture=opengl.texture.Texture(texturepath)\r
27         vertexArrayMap.addMaterial(material)\r
28 \r
29     for o in model.objects:\r
30         # skip mikoto objects\r
31         if o.name.startswith(b"anchor"):\r
32             continue\r
33         if o.name.startswith(b"bone:"):\r
34             continue\r
35         if o.name.startswith(b"MCS:"):\r
36             continue\r
37 \r
38         for f in o.faces:\r
39             if f.index_count==3:\r
40                 vertexArrayMap.addTriangle(\r
41                         f.material_index,\r
42                         o.vertices[f.indices[0]],\r
43                         o.vertices[f.indices[1]],\r
44                         o.vertices[f.indices[2]],\r
45                         f.uv[0], f.uv[1], f.uv[2]\r
46                         )\r
47             elif f.index_count==4:\r
48                 # triangle 1\r
49                 vertexArrayMap.addTriangle(\r
50                         f.material_index,\r
51                         o.vertices[f.indices[0]],\r
52                         o.vertices[f.indices[1]],\r
53                         o.vertices[f.indices[2]],\r
54                         f.uv[0], f.uv[1], f.uv[2]\r
55                         )\r
56                 # triangle 2\r
57                 vertexArrayMap.addTriangle(\r
58                         f.material_index,\r
59                         o.vertices[f.indices[2]],\r
60                         o.vertices[f.indices[3]],\r
61                         o.vertices[f.indices[0]],\r
62                         f.uv[2], f.uv[3], f.uv[0]\r
63                         )\r
64 \r
65     vertexArrayMap.optimize()\r
66     return vertexArrayMap\r
67 \r