Camera and controls#

When you explore a molecular system, most of your time is spent doing three camera actions: focusing on something, returning to a comfortable view, and bookmarking viewpoints you want to come back to.

This page focuses on camera-related helpers: zooming to a selection, resetting the camera, and saving/restoring a camera view.

The snippets below introduce the calls; the embedded example at the end shows the full sequence in action.

If what you need is the on-canvas UI (buttons, structures bar, and popout), see Viewer UI.

Zoom to a selection#

view.zoom(...) is a quick way to focus the camera on a subset of atoms.

In practice, this means: if you can select something, you can zoom to it.

In this example we zoom to the BENZENE molecule inside the 181L demo system.

import molsysviewer as viewer

view = viewer.demo["181L"]
view.zoom('molecule_name=="BENZENE"', duration="250 ms")

Reset the camera#

If you get “lost” while exploring, reset_camera() returns you to the default view.

This only affects the camera: it does not clear the scene, remove regions, or change representations.

view.reset_camera()

Save and restore a camera view#

You can capture the current camera as a snapshot (a plain Python dict in Mol* format) and restore it later.

To get a meaningful snapshot, first interact with the canvas (rotate/zoom/pan), then call get_camera_snapshot().

If you see None, it usually means the browser has not reported a snapshot yet. Try interacting with the viewer and call get_camera_snapshot() again.

snap = view.get_camera_snapshot()

Restore the camera to the stored values:

view.set_camera_snapshot(snap, duration_ms=0)

Putting it together#

The canvas below shows a full sequence: load 181L, zoom to BENZENE, and apply a camera snapshot.

Feel free to interact with the embedded viewer, then use the code above as a template in your own notebooks.

import molsysviewer as viewer

view = viewer.demo["181L"]
view.zoom('molecule_name=="BENZENE"')
view.show()
camera_snapshot = {
    "mode": "perspective",
    "fov": 0.7853981633974483,
    "position": [33.70566533935736, -2.8023437337061745, 18.882755226605074],
    "up": [0.1445312180996829, 0.8739959558312091, 0.4639415870401301],
    "target": [26.911489856486416, 6.126255176505263, 4.179204023614221],
    "radius": 7.077834488226701,
    "radiusMax": 72.78181150350126,
    "fog": 15,
    "clipFar": True,
    "minNear": 1,
    "minFar": 0,
}
view.set_camera_snapshot(camera_snapshot)

And your view should change to: