Open meshcat-server before code execution to improve easiness in visualization (v1.5.0)?

Question:

I’m learning pydrake via their tutorials executing it locally in my pc (pydrake pip version). I’ve changed the code from simple pendulum to use Meshcat locally instead of in the notebook with:

from pydrake.systems.framework import DiagramBuilder
from pydrake.examples.pendulum import PendulumPlant, PendulumGeometry
from pydrake.geometry import MeshcatVisualizer
from pydrake.systems.analysis import Simulator
from pydrake.geometry import Meshcat
import webbrowser

builder = builder.AddSystem(PendulumPlant())
scene_graph = builder.AddSystem(SceneGraph())
PendulumGeometry.AddToBuilder(builder, pendulum.get_state_output_port(), scene_graph)
meshcat = Meshcat(port=7001)
MeshcatVisualizer.AddToBuilder(builder, scene_graph, meshcat)
meshcat.Set2dRenderMode(X_WC=RigidTransform(RotationMatrix.MakeZRotation(np.pi), [0, 1, 0]))
diagram = builder.Build()
simulator = Simulator(diagram)
context = simulator.get_mutable_context()
context.SetContinuousState([0.5, 0])
simulator.set_target_realtime_rate(1.0)
while True:
    simulator.AdvanceTo(simulator.get_context().get_time() + 1.0)

But there is an inconvenience with the server (or the web page) popping up. I need to refresh each time I execute the code to be able to see the simulation. Is there a way to see the simulation automatically without the need to refresh the webpage? (version 1.5.0 of drake via pip)

I’m looking to connect like in a previous version with meshcat-server terminal command and later connecting it in code.
Right now I’m using the webbrowser module to open a new tab each time I execute the code, but I end up with a lot of tabs at the end of some testing.

Asked By: Aurelie Navir

||

Answers:

We have https://github.com/RobotLocomotion/drake/pull/17685 underway so that the browser will automatically reconnect when the server restarts.

That will probably be an easier solution that a long-running server, though we’ve also considered supporting a server mode.

Answered By: jwnimmer-tri
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.