studio: share ImGui and ImPlot contexts across python child extension modules.

Each pybind extension module holds its own copy of the ImGui/ImPlot globals (extension modules are loaded RTLD_LOCAL), so the viewer that owns the contexts must share the pointers explicitly. Without this, client plotting GUIs and user plotting scripts (e.g. the implot sample) crash on null context pointers.

PiperOrigin-RevId: 951917287
Change-Id: I639a173c2ae9f1af4feaf57aff7b5c81854b7393
This commit is contained in:
Matija Kecman
2026-07-21 23:40:32 -07:00
committed by Copybara-Service
parent 91bb075108
commit e0224c6440
5 changed files with 54 additions and 19 deletions
@@ -27,6 +27,7 @@ from mujoco.experimental.studio import ux
from mujoco.experimental.studio import viewer_protocol
from mujoco.experimental.dear_imgui import dear_imgui as imgui
from mujoco.experimental.implot import implot
class NativeViewer(viewer_protocol.Viewer):
@@ -85,6 +86,9 @@ class NativeViewer(viewer_protocol.Viewer):
ctx = self._viewer.GetImGuiContext()
imgui.SetCurrentContext(ctx)
ux.set_imgui_context(ctx)
ux.set_implot_context(self._viewer.GetImPlotContext())
implot.set_imgui_context(ctx)
implot.set_implot_context(self._viewer.GetImPlotContext())
# Dispatch lifecycle event so handlers can cache the viewer reference.
self.dispatch(viewer_protocol.ViewerInitEvent(viewer=self))