ya2 · news · projects · code · about

box
[pmachines.git] / pmachines / pmachines.py
CommitLineData
8ee66edd 1import argparse
4894bb48
FC
2import simplepbr
3import gltf
8ee66edd
FC
4from sys import platform, argv
5from logging import info
6from os.path import exists
7from os import makedirs
1be87278
FC
8from panda3d.core import Filename, load_prc_file_data, AntialiasAttrib
9from panda3d.bullet import BulletWorld, BulletDebugNode
8ee66edd 10from direct.showbase.ShowBase import ShowBase
1be87278 11from pmachines.scene import Scene
8ee66edd
FC
12
13
14class Pmachines:
15
16 def __init__(self):
17 self._configure()
18 self.base = ShowBase()
19 info('platform: %s' % platform)
20 info('exists main.py: %s' % exists('main.py'))
1be87278 21 self._prepare_window()
8ee66edd
FC
22 args = self._parse_args()
23 self.updating = args.update
24 self.version = args.version
25 if args.update:
26 return
1be87278
FC
27 self._set_physics()
28 Scene(self.world)
8ee66edd
FC
29
30 def _configure(self):
31 load_prc_file_data('', 'window-title pmachines')
4894bb48
FC
32 load_prc_file_data('', 'framebuffer-multisample 1')
33 load_prc_file_data('', 'multisamples 4')
34 load_prc_file_data('', 'framebuffer-srgb true')
8ee66edd
FC
35
36 def _parse_args(self):
37 parser = argparse.ArgumentParser()
38 parser.add_argument('--update', action='store_true')
39 parser.add_argument('--version', action='store_true')
40 cmd_line = [arg for arg in iter(argv[1:]) if not arg.startswith('-psn_')]
41 args = parser.parse_args(cmd_line)
42 return args
43
1be87278 44 def _prepare_window(self):
8ee66edd
FC
45 data_path = ''
46 if (platform.startswith('win') or platform.startswith('linux')) and (
47 not exists('main.py') or __file__.startswith('/app/bin/')):
48 # it is the deployed version for windows
49 data_path = str(Filename.get_user_appdata_directory()) + '/pmachines'
50 home = '/home/flavio' # we must force this for wine
51 if data_path.startswith('/c/users/') and exists(home + '/.wine/'):
52 data_path = home + '/.wine/drive_' + data_path[1:]
53 info('creating dirs: %s' % data_path)
54 makedirs(data_path, exist_ok=True)
4894bb48
FC
55 gltf.patch_loader(base.loader)
56 pipeline = simplepbr.init(
57 use_normal_maps=True,
58 use_emission_maps=False,
59 use_occlusion_maps=True,
60 msaa_samples=4)
61 render.setAntialias(AntialiasAttrib.MAuto)
1be87278
FC
62 self.base.set_background_color(0, 0, 0, 1)
63 self.base.disable_mouse()
64
65 def _set_physics(self):
66 debug_node = BulletDebugNode('Debug')
67 debug_node.show_wireframe(True)
68 debug_node.show_constraints(True)
69 debug_node.show_bounding_boxes(True)
70 debug_node.show_normals(True)
71 debug_np = render.attach_new_node(debug_node)
72 debug_np.show()
73 self.world = BulletWorld()
74 self.world.set_gravity((0, 0, -9.81))
75 self.world.set_debug_node(debug_np.node())
76 def update(task):
77 dt = globalClock.get_dt()
78 self.world.do_physics(dt)
79 return task.cont
80 taskMgr.add(update, 'update')