ya2 · news · projects · code · about

better arrangement of the side panel
[pmachines.git] / pmachines / pmachines.py
1 import argparse
2 import simplepbr
3 import gltf
4 from sys import platform, argv
5 from logging import info
6 from os.path import exists
7 from os import makedirs
8 from panda3d.core import Filename, load_prc_file_data, AntialiasAttrib
9 from panda3d.bullet import BulletWorld, BulletDebugNode
10 from direct.showbase.ShowBase import ShowBase
11 from pmachines.scene import Scene
12 from pmachines.music import MusicMgr
13 from lib.dictfile import DctFile
14 from lib.lib.p3d.p3d import LibP3d
15
16
17 class Pmachines:
18
19 def __init__(self):
20 self._configure()
21 self.base = ShowBase()
22 info('platform: %s' % platform)
23 info('exists main.py: %s' % exists('main.py'))
24 args = self._parse_args()
25 self._prepare_window(args)
26 self.updating = args.update
27 self.version = args.version
28 if args.update:
29 return
30 MusicMgr(self._options['settings']['volume'])
31 self._set_physics()
32 self._scene = Scene(self.world)
33
34 def _configure(self):
35 load_prc_file_data('', 'window-title pmachines')
36 load_prc_file_data('', 'framebuffer-multisample 1')
37 load_prc_file_data('', 'multisamples 4')
38 load_prc_file_data('', 'framebuffer-srgb true')
39
40 def _parse_args(self):
41 parser = argparse.ArgumentParser()
42 parser.add_argument('--update', action='store_true')
43 parser.add_argument('--version', action='store_true')
44 parser.add_argument('--optfile')
45 cmd_line = [arg for arg in iter(argv[1:]) if not arg.startswith('-psn_')]
46 args = parser.parse_args(cmd_line)
47 return args
48
49 def _prepare_window(self, args):
50 data_path = ''
51 if (platform.startswith('win') or platform.startswith('linux')) and (
52 not exists('main.py') or __file__.startswith('/app/bin/')):
53 # it is the deployed version for windows
54 data_path = str(Filename.get_user_appdata_directory()) + '/pmachines'
55 home = '/home/flavio' # we must force this for wine
56 if data_path.startswith('/c/users/') and exists(home + '/.wine/'):
57 data_path = home + '/.wine/drive_' + data_path[1:]
58 info('creating dirs: %s' % data_path)
59 makedirs(data_path, exist_ok=True)
60 optfile = args.optfile if args.optfile else 'options.ini'
61 info('data path: %s' % data_path)
62 info('option file: %s' % optfile)
63 info('fixed path: %s' % LibP3d.fixpath(data_path + '/' + optfile))
64 default_opt = {
65 'settings': {
66 'volume': 1},
67 'development': {
68 'simplepbr': 1,
69 'verbose_log': 0,
70 'physics_debug': 0}}
71 opt_path = LibP3d.fixpath(data_path + '/' + optfile) if data_path else optfile
72 opt_exists = exists(opt_path)
73 self._options = DctFile(
74 LibP3d.fixpath(data_path + '/' + optfile) if data_path else optfile,
75 default_opt)
76 if not opt_exists:
77 self._options.store()
78 gltf.patch_loader(base.loader)
79 if self._options['development']['simplepbr']:
80 pipeline = simplepbr.init(
81 use_normal_maps=True,
82 use_emission_maps=False,
83 use_occlusion_maps=True
84 )
85 render.setAntialias(AntialiasAttrib.MAuto)
86 self.base.set_background_color(0, 0, 0, 1)
87 self.base.disable_mouse()
88 #self.base.accept('window-event', self._on_win_evt)
89 self.base.accept('aspectRatioChanged', self._on_aspect_ratio_changed)
90
91 def _set_physics(self):
92 if self._options['development']['physics_debug']:
93 debug_node = BulletDebugNode('Debug')
94 debug_node.show_wireframe(True)
95 debug_node.show_constraints(True)
96 debug_node.show_bounding_boxes(True)
97 debug_node.show_normals(True)
98 debug_np = render.attach_new_node(debug_node)
99 debug_np.show()
100 self.world = BulletWorld()
101 self.world.set_gravity((0, 0, -9.81))
102 if self._options['development']['physics_debug']:
103 self.world.set_debug_node(debug_np.node())
104 def update(task):
105 dt = globalClock.get_dt()
106 self.world.do_physics(dt)
107 return task.cont
108 taskMgr.add(update, 'update')
109
110 def _on_aspect_ratio_changed(self):
111 self._scene.on_aspect_ratio_changed()