rdeits / MeshCat.jl

WebGL-based 3D visualizer in Julia
MIT License
233 stars 42 forks source link

Re-using an existing MeshCat visualizer instance #198

Closed ferrolho closed 1 year ago

ferrolho commented 3 years ago

Hi, Robin! Is it possible to re-use an already-existing instance of a MeshCat visualizer?

E.g., if I have a notebook "First Notebook" that starts a visualizer at http://127.0.0.1:8700/, is it possible to start another notebook "Second Notebook" which connects to the same visualizer instance? Thanks!

ferrolho commented 1 year ago

I don't remember why I was asking this ~2 years ago... Maybe because I wanted to call the settransform! method for moving objects in the same scene from different Julia sessions/Jupyter notebooks? I no longer need this use case, but I was intrigued and decided to have a look.

tl;dr - it is not possible to do this with the current version of MeshCat.

The outer constructor of the Visualizer struct calls the inner constructor of the CoreVisualizer struct, which:

  1. creates a CoreVisualizer instance with an empty Set{HTTP.WebSockets.WebSocket} called connections,
  2. tries to find an available port for the given host, and then
  3. starts the server by calling the non-blocking method HTTP.listen!.

The server continuously listens for new incoming connections, and creates a new WebSocket for each of them.

So, it seems like one way of achieving this would be to serve an additional server for listening to incoming sockets at a different port and using a different set of commands specifically tailored for manipulating an existing MeshCat visualiser. The job of that server would be to listen to the commands in those sockets (coming from different Julia sessions) and then calling the respective MeshCat API on the main server where the three.js visualisation being served.

Anyway, I am going to close this issue because that sounds like a lot of work for something that only a few users — more like zero, really — would actually use. 😅