|
3 | 3 | A script to run the project in Webots. |
4 | 4 |
|
5 | 5 | Largely just a shortcut to running the arena world in Webots. |
6 | | -Only functional in releases. |
7 | 6 | """ |
| 7 | +# ruff: noqa: E501 |
| 8 | +from __future__ import annotations |
| 9 | + |
8 | 10 | import sys |
9 | 11 | import traceback |
| 12 | +from os.path import expandvars |
10 | 13 | from pathlib import Path |
11 | 14 | from shutil import which |
12 | 15 | from subprocess import Popen |
13 | 16 |
|
14 | 17 | if sys.platform == "win32": |
15 | 18 | from subprocess import CREATE_NEW_PROCESS_GROUP, DETACHED_PROCESS |
16 | 19 |
|
17 | | -try: |
18 | | - if not (Path(__file__).parent / 'simulator/VERSION').exists(): |
19 | | - print("This script is only functional in releases.") |
20 | | - raise RuntimeError |
| 20 | +if (Path(__file__).parent / 'simulator/VERSION').exists(): |
| 21 | + print("Running in release mode") |
| 22 | + SIM_BASE = Path(__file__).parent.resolve() |
| 23 | +else: |
| 24 | + print("Running in development mode") |
| 25 | + # Assume the script is in the scripts directory |
| 26 | + SIM_BASE = Path(__file__).parents[1].resolve() |
| 27 | + |
| 28 | +POSSIBLE_WEBOTS_PATHS = [ |
| 29 | + ("darwin", "/Applications/Webots.app/Contents/MacOS/webots"), |
| 30 | + ("win32", "C:\\Program Files\\Webots\\msys64\\mingw64\\bin\\webotsw.exe"), |
| 31 | + ("win32", expandvars("%LOCALAPPDATA%\\Programs\\Webots\\msys64\\mingw64\\bin\\webotsw.exe")), |
| 32 | + # Attempt to use the start menu shortcut |
| 33 | + ("win32", expandvars("%ProgramData%\\Microsoft\\Windows\\Start Menu\\Programs\\Cyberbotics\\Webots.lnk")), |
| 34 | + ("win32", expandvars("%APPDATA%\\Microsoft\\Windows\\Start Menu\\Programs\\Cyberbotics\\Webots.lnk")), |
| 35 | + ("linux", "/usr/local/bin/webots"), |
| 36 | + ("linux", "/usr/bin/webots"), |
| 37 | +] |
| 38 | + |
| 39 | + |
| 40 | +def get_webots_parameters() -> tuple[Path, Path]: |
| 41 | + """ |
| 42 | + Get the paths to the Webots executable and the arena world file. |
21 | 43 |
|
22 | | - world_file = Path(__file__).parent / "simulator/worlds/arena.wbt" |
| 44 | + :return: The paths to the Webots executable and the arena world file |
| 45 | + """ |
| 46 | + world_file = SIM_BASE / "simulator/worlds/arena.wbt" |
23 | 47 |
|
| 48 | + if not world_file.exists(): |
| 49 | + raise RuntimeError("World file not found.") |
| 50 | + |
| 51 | + if not (SIM_BASE / "venv").exists(): |
| 52 | + raise RuntimeError("Please run the setup.py script before running the simulator.") |
| 53 | + |
| 54 | + # Check if Webots is in the PATH |
24 | 55 | webots = which("webots") |
25 | 56 |
|
26 | 57 | # Find the webots executable, if it is not in the PATH |
27 | 58 | if webots is None: |
28 | | - if sys.platform == "darwin": |
29 | | - webots = "/Applications/Webots.app/Contents/MacOS/webots" |
30 | | - elif sys.platform == "win32": |
31 | | - webots = "C:\\Program Files\\Webots\\msys64\\mingw64\\bin\\webotsw.exe" |
32 | | - elif sys.platform.startswith("linux"): |
33 | | - possible_paths = ["/usr/local/bin/webots", "/usr/bin/webots"] |
34 | | - for path in possible_paths: |
| 59 | + for system_filter, path in POSSIBLE_WEBOTS_PATHS: |
| 60 | + if sys.platform.startswith(system_filter): |
| 61 | + print(f"Checking {path}") |
35 | 62 | if Path(path).exists(): |
36 | 63 | webots = path |
37 | 64 | break |
38 | | - else: |
39 | | - print("Webots executable not found.") |
40 | | - raise RuntimeError |
| 65 | + |
| 66 | + if webots is None or not Path(webots).exists(): |
| 67 | + raise RuntimeError("Webots executable not found.") |
| 68 | + |
| 69 | + return Path(webots), world_file |
| 70 | + |
| 71 | + |
| 72 | +def main() -> None: |
| 73 | + """Run the project in Webots.""" |
| 74 | + try: |
| 75 | + webots, world_file = get_webots_parameters() |
| 76 | + |
| 77 | + # Run the world file in Webots, |
| 78 | + # detaching the process so it does not close when this script does |
| 79 | + if sys.platform == "win32": |
| 80 | + Popen( |
| 81 | + [str(webots), str(world_file)], |
| 82 | + creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP, |
| 83 | + # shell=True is needed to run from shortcuts |
| 84 | + shell=(webots.suffix == ".lnk"), |
| 85 | + ) |
41 | 86 | else: |
42 | | - print("Unsupported platform.") |
43 | | - raise RuntimeError |
44 | | - |
45 | | - if not Path(webots).exists(): |
46 | | - print("Webots executable not found.") |
47 | | - raise RuntimeError |
48 | | - |
49 | | - if not (Path(__file__).parent / "venv").exists(): |
50 | | - print("Please run the setup.py script before running the simulator.") |
51 | | - raise RuntimeError |
52 | | - |
53 | | - # Run the world file in Webots, |
54 | | - # detaching the process so it does not close when this script does |
55 | | - if sys.platform == "win32": |
56 | | - Popen([webots, world_file], creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP) |
57 | | - else: |
58 | | - Popen([webots, world_file], start_new_session=True) |
59 | | -except RuntimeError: |
60 | | - input("Press enter to continue...") |
61 | | - exit(1) |
62 | | -except Exception as e: |
63 | | - print(f"An error occurred: {e}") |
64 | | - print(traceback.format_exc()) |
65 | | - input("Press enter to continue...") |
66 | | - exit(1) |
| 87 | + Popen([str(webots), str(world_file)], start_new_session=True) |
| 88 | + except RuntimeError as e: |
| 89 | + print(f"An error occurred: {e}") |
| 90 | + input("Press enter to continue...") |
| 91 | + exit(1) |
| 92 | + except Exception as e: |
| 93 | + print(f"An error occurred: {e}") |
| 94 | + print(traceback.format_exc()) |
| 95 | + input("Press enter to continue...") |
| 96 | + exit(1) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + main() |
0 commit comments