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
This commit is contained in:
Kevin Zakka
2026-02-17 18:58:56 -08:00
committed by Copybara-Service
parent f6129596a6
commit fa43e2c03b
+16 -14
View File
@@ -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) {