OSDN Git Service

96feccb06fcdc0534901d955e39efc4995041da7
[meshio/pymeshio.git] / examples / pmdbuilder.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.pmd\r
7 import opengl.material\r
8 import opengl.texture\r
9 import opengl.vertexarray\r
10 \r
11 \r
12 def build(path):\r
13     # load scenee\r
14     t=time.time()\r
15     io=pymeshio.pmd.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     indexedVertexArray=opengl.vertexarray.IndexedVertexArray()\r
22     for v in io.vertices:\r
23         # left-handed y-up to right-handed y-up                \r
24         indexedVertexArray.addVertex(\r
25                 (v.pos[0], v.pos[1], -v.pos[2], 1), \r
26                 (v.normal[0], v.normal[1], -v.normal[2]), \r
27                 (v.uv[0], v.uv[1]), \r
28                 (1, 1, 1, 1),\r
29                 v.bone0, v.bone1, v.weight0)\r
30     \r
31     # material\r
32     textureMap={}\r
33     faceIndex=0\r
34     def indices():\r
35         for i in io.indices:\r
36             yield i\r
37     indexGen=indices()\r
38     for i, m in enumerate(io.materials):\r
39         print(i, m)\r
40         material=opengl.material.MQOMaterial()\r
41         material.vcol=True\r
42         material.rgba=(\r
43                 m.diffuse[0], \r
44                 m.diffuse[1], \r
45                 m.diffuse[2], \r
46                 m.diffuse[3])\r
47         texturefile=m.texture.decode('cp932')\r
48         if texturefile!="":\r
49             texturepath=os.path.join(basedir, texturefile)\r
50             if not texturepath in textureMap:\r
51                 texture=opengl.texture.Texture(texturepath)\r
52                 textureMap[texturepath]=texture\r
53             material.texture=textureMap[texturepath]\r
54         indices=indexedVertexArray.addMaterial(material)\r
55         indices+=[next(indexGen) for n in range(m.vertex_count)]\r
56     indexedVertexArray.optimize()\r
57     return indexedVertexArray\r
58 \r