Repository navigation
Expand file tree
/
Copy pathlaunch_sim.py
More file actions
116 lines (96 loc) · 3.92 KB
/
Copy pathlaunch_sim.py
File metadata and controls
116 lines (96 loc) · 3.92 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
"""
SAFAR — Universal Cross-Platform Simulation Launcher
Detects Webots installation, pre-populates asset cache if needed, and launches the simulation.
Works out-of-the-box on any machine running Windows, macOS, or Linux.
"""
import os
import sys
import shutil
import platform
import subprocess
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parent
WORLD_FILE = PROJECT_ROOT / "webots_sim" / "worlds" / "safar_pothole_track.wbt"
# Ensure offline assets are extracted
from setup_simulation_assets import setup_webots_assets, verify_yolo_weights
def find_webots_executable() -> Path:
"""Finds Webots executable across common operating system locations."""
system = platform.system()
# 1. Check WEBOTS_HOME environment variable
webots_home = os.environ.get("WEBOTS_HOME")
if webots_home:
home_path = Path(webots_home)
candidates = [
home_path / "msys64" / "mingw64" / "bin" / "webotsw.exe",
home_path / "msys64" / "mingw64" / "bin" / "webots.exe",
home_path / "webots",
home_path / "Webots.app" / "Contents" / "MacOS" / "webots"
]
for c in candidates:
if c.exists():
return c
# 2. Check system PATH
which_webots = shutil.which("webots") or shutil.which("webots.exe") or shutil.which("webotsw.exe")
if which_webots:
return Path(which_webots)
# 3. Check standard OS paths
if system == "Windows":
local_app = os.environ.get("LOCALAPPDATA", "")
prog_files = os.environ.get("ProgramFiles", "C:\\Program Files")
standard_windows_paths = [
Path(local_app) / "Programs" / "Webots" / "msys64" / "mingw64" / "bin" / "webotsw.exe",
Path(local_app) / "Programs" / "Webots" / "msys64" / "mingw64" / "bin" / "webots.exe",
Path(prog_files) / "Webots" / "msys64" / "mingw64" / "bin" / "webotsw.exe",
Path(prog_files) / "Webots" / "msys64" / "mingw64" / "bin" / "webots.exe",
Path("C:/Program Files/Webots/msys64/mingw64/bin/webotsw.exe")
]
for p in standard_windows_paths:
if p.exists():
return p
elif system == "Darwin": # macOS
mac_paths = [
Path("/Applications/Webots.app/Contents/MacOS/webots"),
Path.home() / "Applications/Webots.app/Contents/MacOS/webots"
]
for p in mac_paths:
if p.exists():
return p
else: # Linux
linux_paths = [
Path("/usr/local/webots/webots"),
Path("/usr/bin/webots"),
Path("/snap/bin/webots")
]
for p in linux_paths:
if p.exists():
return p
return None
def main():
print("==================================================")
print(" SAFAR WEBOTS SIMULATION LAUNCHER ")
print("==================================================")
# 1. Verify / extract assets so other devices don't hit "Cannot load asset"
setup_webots_assets(force=False)
verify_yolo_weights()
# 2. Locate Webots executable
webots_exe = find_webots_executable()
if not webots_exe:
print("\n[ERROR] Webots executable could not be found automatically.")
print("Please install Webots from: https://cyberbotics.com")
print("Or set the WEBOTS_HOME environment variable to your Webots folder.")
sys.exit(1)
if not WORLD_FILE.exists():
print(f"\n[ERROR] Simulation world not found: {WORLD_FILE}")
sys.exit(1)
print(f"\n[SAFAR] Webots found : {webots_exe}")
print(f"[SAFAR] Launching track: {WORLD_FILE.name}")
print("==================================================\n")
# Launch Webots
try:
subprocess.Popen([str(webots_exe), str(WORLD_FILE)])
except Exception as e:
print(f"[ERROR] Failed to launch Webots: {e}")
sys.exit(1)
if __name__ == "__main__":
main()