From fa43e2c03bd4ce6041c7d871a91396d7f9515805 Mon Sep 17 00:00:00 2001 From: Kevin Zakka Date: Tue, 17 Feb 2026 18:58:56 -0800 Subject: [PATCH] Fix tracking camera detaching when toggling visualization flags, fixes #3108 Toggling visualization flags via keyboard shortcuts (e.g., C for contact points, F for contact forces) causes the SECT_RENDERING event handler to unconditionally re-evaluate the camera selection, which can reset a tracking camera to free mode. This particularly affects users of mujoco.viewer who set tracking cameras programmatically. Guard the camera update logic so it only runs when the camera selector itself is changed, not when other items in the rendering section are modified. PiperOrigin-RevId: 871600340 Change-Id: Ia6ef2a1617c6cdc18c7d10b4b099a8f2c620a824 --- simulate/simulate.cc | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/simulate/simulate.cc b/simulate/simulate.cc index 2580c45a..be7318c5 100644 --- a/simulate/simulate.cc +++ b/simulate/simulate.cc @@ -1584,22 +1584,24 @@ void UiEvent(mjuiState* state) { // rendering section else if (it && it->sectionid==SECT_RENDERING) { - // set camera in mjvCamera - if (sim->camera==0) { - sim->cam.type = mjCAMERA_FREE; - } else if (sim->camera==1) { - if (sim->pert.select>0) { - sim->cam.type = mjCAMERA_TRACKING; - sim->cam.trackbodyid = sim->pert.select; - sim->cam.fixedcamid = -1; - } else { + // only update the camera when the camera itself changed + if (it->pdata == &sim->camera) { + if (sim->camera==0) { sim->cam.type = mjCAMERA_FREE; - sim->camera = 0; - mjui0_update_section(sim, SECT_RENDERING); + } else if (sim->camera==1) { + if (sim->pert.select>0) { + sim->cam.type = mjCAMERA_TRACKING; + sim->cam.trackbodyid = sim->pert.select; + sim->cam.fixedcamid = -1; + } else { + sim->cam.type = mjCAMERA_FREE; + sim->camera = 0; + mjui0_update_section(sim, SECT_RENDERING); + } + } else { + sim->cam.type = mjCAMERA_FIXED; + sim->cam.fixedcamid = sim->camera - 2; } - } else { - sim->cam.type = mjCAMERA_FIXED; - sim->cam.fixedcamid = sim->camera - 2; } // copy camera spec to clipboard (as MJCF element) if (it->itemid == 3) {