Skip to content

Commit

Permalink
Add a flag to check if the window was closed manually before the end …
Browse files Browse the repository at this point in the history
…of the routine.
  • Loading branch information
RobinEnjalbert committed Sep 16, 2024
1 parent 974fdf6 commit a32ff13
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/core/demo_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
player.launch()

# Run some steps of the simulation
for _ in range(500):
while player.is_open:
simu.step()
player.render()

Expand Down
2 changes: 1 addition & 1 deletion examples/core/demo_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
viewer.launch()

# Run some steps of the simulation
for _ in range(500):
while viewer.is_open:
simu.step()
viewer.render()

Expand Down
2 changes: 1 addition & 1 deletion examples/sofa/demo_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# SOFA: run a few time steps
# VIEWER: update the rendering
for i in range(300):
while player.is_open:
Sofa.Simulation.animate(root, root.dt.value)
player.render()

Expand Down
2 changes: 1 addition & 1 deletion examples/sofa/demo_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

# SOFA: run a few time steps
# VIEWER: update the rendering
for i in range(300):
while viewer.is_open:
Sofa.Simulation.animate(root, root.dt.value)
viewer.render()

Expand Down
6 changes: 5 additions & 1 deletion src/SimRender/core/local/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def __init__(self, sync: bool):
self.__sync_arr = ndarray(shape=sync_array.shape, dtype=sync_array.dtype, buffer=self.__sync_sm.buf)
self.__sync_arr[...] = sync_array[...]

@property
def is_open(self) -> bool:
return self.__sync_arr[0] == 0

def init(self, batch_key: Optional[int]) -> int:
"""
Initialize the local socket.
Expand Down Expand Up @@ -99,7 +103,7 @@ def __sync(self):
self.__sync_arr[1] = 1

# Wait for the remote process to be done (after several tests, it appears to be the faster way)
self.__remote.recv(4)
a = self.__remote.recv(4)

# Turn the 'do_synchronize' shared flag off
self.__sync_arr[1] = 0
Expand Down
4 changes: 4 additions & 0 deletions src/SimRender/core/local/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def __init__(self, sync: bool = False):
self.__subprocess: Optional[Thread] = None
self._remote_script = viewer.__file__

@property
def is_open(self) -> bool:
return self.__factory.is_open

@property
def objects(self) -> Objects:
"""
Expand Down
4 changes: 4 additions & 0 deletions src/SimRender/core/remote/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ def close(self):
Close the communication with the simulation process.
"""

# If the window is closed manually then turn the do_exit flag to True
if not self.__sync_arr[0]:
self.__sync_arr[0] = 1

# Close the connection with the shared memories (synchronization array and each visual object array)
self.__sync_sm.close()
for memory in self.__memories:
Expand Down
1 change: 1 addition & 0 deletions src/SimRender/core/remote/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def launch(self):
# Launch the visualization window
self.factory.listen()
self.show(axes=4).close()
self.factory.close()

def time_step(self, _) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/SimRender/core/remote/viewer_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def on_close_event(self):
# Executed code when the visualization process is launched
batch_socket_ports = [int(port) for port in sys.argv[1].split(' ')]
win = ViewerBatch(socket_ports=batch_socket_ports)
win.showMaximized()

# sys.exit(app.exec())
app.aboutToQuit.connect(win.on_close_event)
app.exec()

0 comments on commit a32ff13

Please sign in to comment.