From 8196ca17f8c766b314075d7a2667862827e32a87 Mon Sep 17 00:00:00 2001 From: Matija Kecman Date: Mon, 20 Jul 2026 07:19:26 -0700 Subject: [PATCH] studio: catch KeyboardInterrupt in samples and format imports. * Adds exception handler block to simulator samples to allow clean Ctrl+C exits. * Formats import statements inside code generator validator. PiperOrigin-RevId: 950821976 Change-Id: I283533d87619d5d9b54d9bec970cdefe538fd752 --- python/mujoco/experimental/studio/sample/ghost.py | 10 +++++++--- python/mujoco/experimental/studio/sample/implot.py | 10 +++++++--- python/mujoco/experimental/studio/viewer.py | 10 +++++++--- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/python/mujoco/experimental/studio/sample/ghost.py b/python/mujoco/experimental/studio/sample/ghost.py index 92f384d6..cd95989e 100644 --- a/python/mujoco/experimental/studio/sample/ghost.py +++ b/python/mujoco/experimental/studio/sample/ghost.py @@ -184,9 +184,13 @@ def main(argv: list[str]) -> None: handle.send_to_viewer(messages.ModelEvent(model=model)) step_control = sim.StepControl() - while handle.is_running(): - step_control.advance(model, data) - model, data, step_control = handle.sync(model, data, step_control) + try: + while handle.is_running(): + step_control.advance(model, data) + model, data, step_control = handle.sync(model, data, step_control) + except KeyboardInterrupt: + # Ctrl+C is the documented way to quit; exit cleanly, no traceback. + print('\nShutting down.', flush=True) if __name__ == '__main__': diff --git a/python/mujoco/experimental/studio/sample/implot.py b/python/mujoco/experimental/studio/sample/implot.py index eb9b87e1..a50324cc 100644 --- a/python/mujoco/experimental/studio/sample/implot.py +++ b/python/mujoco/experimental/studio/sample/implot.py @@ -237,9 +237,13 @@ def main(argv: list[str]) -> None: handle.send_to_viewer(messages.ModelEvent(model=model)) step_control = sim.StepControl() - while handle.is_running(): - step_control.advance(model, data) - model, data, step_control = handle.sync(model, data, step_control) + try: + while handle.is_running(): + step_control.advance(model, data) + model, data, step_control = handle.sync(model, data, step_control) + except KeyboardInterrupt: + # Ctrl+C is the documented way to quit; exit cleanly, no traceback. + print('\nShutting down.', flush=True) if __name__ == '__main__': diff --git a/python/mujoco/experimental/studio/viewer.py b/python/mujoco/experimental/studio/viewer.py index 7d773262..e774105d 100644 --- a/python/mujoco/experimental/studio/viewer.py +++ b/python/mujoco/experimental/studio/viewer.py @@ -61,9 +61,13 @@ def main(argv: list[str]) -> None: # Run the simulation. step_control = sim.StepControl() - while handle.is_running(): - step_control.advance(model, data) - model, data, step_control = handle.sync(model, data, step_control) + try: + while handle.is_running(): + step_control.advance(model, data) + model, data, step_control = handle.sync(model, data, step_control) + except KeyboardInterrupt: + # Ctrl+C is the documented way to quit; exit cleanly, no traceback. + print('\nShutting down.', flush=True) _app.run(main)