-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
OSCLeash.py
97 lines (76 loc) · 3.27 KB
/
OSCLeash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from threading import Thread
import json
import sys
import os
import time
from Controllers.DataController import DefaultConfig, ConfigSettings, Leash
from Controllers.PackageController import Package
from Controllers.ThreadController import Program
# Make sure to change this to the correct version number on releases.
__version__ = "v"+"2.2.0"
def createDefaultConfigFile(configPath): # Creates a default config
try:
with open(configPath, "w") as cf:
json.dump(DefaultConfig, cf, indent=4)
print("Default config file created\n")
time.sleep(2)
except Exception as e:
print(e)
os.system("pause")
exit()
if __name__ == "__main__":
#*************Setup*************#
program = Program()
program.setWindowTitle()
program.cls()
print('\x1b[1;32;40m' + f"OSCLeash {__version__}" + '\x1b[0m')
# Test if Config file exists. Create the default if it does not.
configRelativePath = "./Config.json"
if not os.path.exists(configRelativePath):
# print error message in red
print('\x1b[1;31;40m' + "Config file was not found...", "\nCreating default config file..." + '\x1b[0m')
createDefaultConfigFile(configRelativePath)
else:
print("Config file found\n")
# load settings
try:
configData = json.load(open(configRelativePath))
except Exception as e:
print('\x1b[1;31;40m' + 'Malformed Config.json file. Fix or delete it to generate a new one.' + '\x1b[0m')
print(f"{e}\nDefault Config will be loaded.\n")
configData = DefaultConfig
os.system("pause")
settings = ConfigSettings(configData)
time.sleep(1)
# TODO: Remove Xbox support if not needed
if settings.XboxJoystickMovement:
try:
import vgamepad as vg
settings.addGamepadControls(vg.VX360Gamepad(), vg.XUSB_BUTTON.XUSB_GAMEPAD_LEFT_SHOULDER) # Add emulated gamepad
except Exception as e:
print('\x1b[1;31;40m' + f'Error: {e}\nWarning: Switching to default OSC settings. Please wait...\n Check documentation for controller emulator tool.' + '\x1b[0m')
settings.XboxJoystickMovement = False
os.system("pause")
# Collect Data for leash
leashes = []
for leashName in configData["PhysboneParameters"]:
leashes.append(Leash(leashName, configData["DirectionalParameters"], settings))
try:
# Manage data coming in
if len(leashes) == 0: raise Exception("No leashes found. Please update config file.")
package = Package(leashes, configData['UseOSCQuery'])
package.listen()
# Start server
serverThread = Thread(target=package.runServer, args=(settings.IP, settings.ListeningPort))
serverThread.start()
time.sleep(.1)
#initialize input
if serverThread.is_alive():
leashes[0].Active = True
print("Started, awaiting input...")
Thread(target=program.leashRun, args=(leashes[0],)).start()
else: raise Exception()
except Exception as e:
print(e)
os.system("pause")
os.execl(sys.executable, sys.executable, *sys.argv)