OSDN Git Service

9e0e26cffc9e7e34d49091ba336a13f376374588
[meshio/pymeshio.git] / examples / pymeshviewer.py
1 #!/usr/bin/env python\r
2 # coding: utf-8\r
3 \r
4 import sys\r
5 import os\r
6 import tkinter\r
7 import tkinter.filedialog\r
8 import togl\r
9 import opengl\r
10 import opengl.rokuro\r
11 import mqobuilder\r
12 import pmdbuilder\r
13 import pmxbuilder\r
14 \r
15 \r
16 class Frame(tkinter.Frame):\r
17     def __init__(self, width, height, master=None, **kw):\r
18         super(Frame, self).__init__(master, **kw)\r
19         self.master.title('pymeshio viewer')\r
20         self.current='.'\r
21         # setup menu\r
22         menu_bar = tkinter.Menu(self)\r
23         self.master.config(menu=menu_bar)\r
24 \r
25         menu_file = tkinter.Menu(menu_bar, tearoff=False)\r
26         menu_bar.add_cascade(label='File', menu=menu_file, underline=0)\r
27 \r
28         menu_file.add_command(label='Open', under=0, command=self.onOpen)\r
29 \r
30         # setup opengl widget\r
31         self.view=opengl.rokuro.RokuroView()\r
32         self.glworld=opengl.BaseController(self.view)\r
33         self.glwidget=togl.Widget(self, self.glworld, width=width, height=height)\r
34         self.glwidget.pack(fill=tkinter.BOTH, expand=True)\r
35 \r
36         # event binding\r
37         self.bind('<Key>', self.onKeyDown)\r
38         self.bind('<MouseWheel>', lambda e: self.glworld.onWheel(-e.delta) and self.glwidget.onDraw())\r
39 \r
40     def onOpen(self):\r
41         path=tkinter.filedialog.askopenfilename(\r
42                 filetypes=[\r
43                     ('poloygon model files', '*.mqo;*.pmd;*.pmx'),\r
44                     ], \r
45                 initialdir=self.current)\r
46         self.current=os.path.dirname(path)\r
47         self.load(path)\r
48 \r
49     def load(self, path):\r
50         model=self.loadModel(path)\r
51         if not model:\r
52             print('fail to load %s' % path)\r
53             return\r
54         print('load %s' % path)\r
55         print(model)\r
56         self.glworld.setRoot(model)\r
57         bb=model.get_boundingbox()\r
58         print(bb)\r
59         self.view.look_bb(*bb)\r
60         self.glwidget.onDraw()\r
61 \r
62     def loadModel(self, path):\r
63         if path.lower().endswith(".mqo"):\r
64             return mqobuilder.build(path)\r
65         elif path.lower().endswith(".pmd"):\r
66             return pmdbuilder.build(path)\r
67         elif path.lower().endswith(".pmx"):\r
68             return pmxbuilder.build(path)\r
69         else:\r
70             print("unknown file format: {0}".format(path))\r
71 \r
72     def onKeyDown(self, event):\r
73         key=event.keycode\r
74         if key==27:\r
75             # Escape\r
76             sys.exit()\r
77         if key==81:\r
78             # q\r
79             sys.exit()\r
80         else:\r
81             print("keycode: %d" % key)\r
82 \r
83 \r
84 if __name__ == '__main__':\r
85     f = Frame(width=600, height=600)\r
86     f.pack(fill=tkinter.BOTH, expand=True)\r
87     f.focus_set()\r
88     if len(sys.argv)>1:\r
89         f.load(sys.argv[1])\r
90     f.mainloop()\r
91 \r