moleculekit.viewer.molstar.render module#
Headless Mol* rendering: turn a Molecule into a PNG with no display.
A singleton chromium is started on first use and reused, driven over the devtools protocol (see cdp.py). The scene comes from build_scene (see scene.py), the same description the interactive viewer builds from, and the browser applies it with applyScene (see viewer-frontend/src/scene.ts) so a render matches what view() shows. This path never imports molviewspec.
Rendering runs on the GPU when one is reachable and falls back to a software rasteriser when it is not, which is what lets the same code work inside containers that expose no graphics device. MOLECULEKIT_RENDER_GL pins the choice. The fallback is not free: a 1200x900 “fast” render measured 0.16s on a GPU against 1.2s on a 20-core software rasteriser, and software time scales with pixels and with how many cores it has.
This module is not thread-safe: the singleton browser’s devtools session has one socket and one frame reader, so concurrent render() calls from different threads can consume each other’s responses. Call render() from one thread at a time.
- moleculekit.viewer.molstar.render.find_chromium()#
Locate a chromium or chrome binary to render with.
Resolution order is the
MOLECULEKIT_CHROMIUMenvironment variable, thenchromium,chromium-browser,google-chromeandchromeon PATH.- Returns:
path – Path to the browser executable.
- Return type:
- Raises:
RuntimeError – If no usable browser is found.
- moleculekit.viewer.molstar.render.render(mol, output=None, *, size=(1200, 900), quality='fast', center=None, rotate=None, zoom=None, background='white', transparent=False, fog=None, clip=None, server=None, timeout=300.0)#
Render
molto a PNG with no display and no browser window.The scene is the one
view()would show: representations come frommol.reps, and the frame rendered ismol.frame.- Parameters:
mol (
moleculekit.molecule.MoleculeorVolumeorlist) – The object to render, or several to draw together in one picture. Each keeps its own representations, so objects loaded separately stay separate. Amoleculekit.volume.Volumedraws its isosurfaces alongside the molecules; at least one molecule is needed, since a volume has no atoms for the camera to frame.output (
str|None) – Path to write the PNG to. When None the PNG bytes are returned.quality (
str) – One of the keys ofQUALITY_PRESETS."high"enables ambient occlusion, which is close to free on a GPU and costs roughly three times the render time on the software fallback.center (
str|ndarray|None) – Atom selection to frame the camera on. None frames the whole structure.rotate (
strortupleoffloatorNone, optional) – Camera orientation, as a preset name or(rx, ry, rz)in degrees.zoom (
float|None) – Camera tightness. Larger values move the camera closer.background (
str) – Background colour as an SVG colour name or hex string.transparent (
bool) – Render onto a transparent background, ignoringbackground.fog (
float|None) – Depth cueing strength, from 0 for none to 100 for the strongest. Fog fades distant geometry into the background colour, which reads as depth on a crowded structure. None uses Mol*’s own strength.clip (
float|None) – Half-thickness in Angstrom of the slab drawn around what the camera frames: geometry nearer to or further from the camera than this is cut away, which is how you see into a buried pocket. None draws the whole structure.server (
str|None) – Base URL of a render server, such as"http://gpuhost:8080", to draw the image on instead of starting a browser here. None falls back to theMOLECULEKIT_RENDER_SERVERenvironment variable, and then to rendering locally.timeout (
float) – Seconds to allow for a single render before giving up.
- Returns:
result – The PNG bytes when
outputis None, otherwiseoutput.- Return type:
- Raises:
ValueError – If
qualitynames no known preset, ifsizeis not at least one pixel in each dimension, ifcentermatches no atoms, ifzoomis not positive, ifrotateis a string that names no known orientation preset, iffogfalls outside 0 to 100, or ifclipis not positive.RuntimeError – If no browser is found, if WebGL is unavailable, if the page fails, or if the rendered image does not match the requested size.
- moleculekit.viewer.molstar.render.render_png(payload, *, width, height, quality='fast', transparent=False, timeout=300.0)#
Draw one already-built scene in the local browser.
This is the half of
render()that needs a browser. Everything above it, turning a Molecule into a structure and a scene description, is plain Python, which is what lets a render run on another machine: the render server (see renderserver.py) calls this with what a client sent it.- Parameters:
payload (
dict) – What to draw:structureandscenefor one object, orobjectsandglobalsfor several drawn together.width (
int) – Image width in pixels.height (
int) – Image height in pixels.quality (
str) – One of the keys ofQUALITY_PRESETS.transparent (
bool) – Render onto a transparent background.timeout (
float) – Seconds to allow before giving up.
- Returns:
png – The rendered image.
- Return type:
- Raises:
RuntimeError – If the rendered image does not match the requested size.