Headless 3-D rendering#
The 3-D tier draws through PyVista, and PyVista draws through VTK, which needs an OpenGL context even when
no window is shown. On Windows and macOS the platform provides one. On Linux the stock vtk wheel gets it from
an X server, so a machine with no screen — a server, a container, a remote Jupyter kernel — cannot render a
Scene3D until it is given one. There are three ways to do that.
1. Render off-screen#
Whatever else you do, ask for no window:
from digitalearth.three_d import Scene3D
with Scene3D(off_screen=True) as scene:
scene.terrain(dem)
scene.screenshot("relief.png")
off_screen=None (the default) follows pyvista.OFF_SCREEN, so setting that once at the top of a script does
the same for every scene. Off-screen stops a window being opened; it does not remove the need for an OpenGL
context on Linux — the next two sections are where that comes from.
2. A virtual X server (Xvfb)#
Around the whole process. xvfb-run starts a virtual X server, sets DISPLAY, runs the command and stops
the server afterwards. It is what this project's CI does for every 3-D job:
From inside Python. When you cannot wrap the process — a notebook kernel someone else started, say —
call start_xvfb() once before the first scene is drawn. It needs the Xvfb binary installed (the xvfb
package above) and does nothing where a display already exists, so it is safe to leave in a script that also
runs on a desktop, under xvfb-run, or on Windows and macOS:
from digitalearth.three_d import Scene3D, start_xvfb
server = start_xvfb() # None when no server was needed
with Scene3D(off_screen=True) as scene:
scene.terrain(dem)
scene.screenshot("relief.png")
if server is not None:
server.terminate()
PyVista used to ship a helper of this name and no longer does — the PyVista this tier is tested against has none — which is why Digital-Earth carries its own.
3. EGL or OSMesa — no X server at all#
VTK can also create its context through EGL (GPU drivers, no X server) or OSMesa (Mesa's software
renderer). On Linux, VTK picks a backend at run time and lets you override the choice with the
VTK_DEFAULT_OPENGL_WINDOW environment variable, set before VTK is imported:
| Value | Backend |
|---|---|
vtkXOpenGLRenderWindow |
X server (the default when one is reachable) |
vtkEGLRenderWindow |
EGL — needs a VTK built with EGL and a driver that provides it |
vtkOSOpenGLRenderWindow |
OSMesa — needs libOSMesa.so installed |
Which of these a given vtk build supports, and what each needs installed, is VTK's to document — see
VTK runtime settings. Choosing one is an
installation decision rather than something Digital-Earth switches for you; nothing in the 3d extra pins a
particular backend.
digitalearth.three_d.app.headless.start_xvfb(display=':99', *, window_size=(1024, 768), wait=0.5)
#
Start a virtual X server and point DISPLAY at it, so the 3-D tier can render with no screen.
Call it once, before the first scene is drawn. It does nothing — and returns None — when there is
nothing to do: DISPLAY is already set (a desktop session, or a process run under xvfb-run), or the
platform is Windows or macOS, where VTK renders without an X server. That makes it safe to call
unconditionally at the top of a script meant to run everywhere.
On Linux with no display it launches Xvfb <display> -screen 0 <width>x<height>x24 -nolisten tcp,
waits wait seconds to see that the server stays up, and only then sets DISPLAY.
Stopping it. Call .terminate() on the returned process when you are done, or leave it: the
helper registers an :mod:atexit hook that terminates the server, waits (briefly, and killing it if
it will not go) for it to actually exit, and unsets the DISPLAY it set, so a script that forgets
leaves no stray server behind. The OS does not do this for you — a
subprocess.Popen child outlives the interpreter that started it, and Popen.__del__
deliberately does not terminate a running one (dropping the last reference to a live child only warns
subprocess <pid> is still running). Until the hook existed, a forgotten server went on holding
:99; the next run then found no DISPLAY, asked for :99 again, and failed with the
RuntimeError below — so "safe to call unconditionally" was false from the second run onward
(review M3).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
display
|
str
|
The X display to create, such as |
':99'
|
window_size
|
Sequence[int]
|
|
(1024, 768)
|
wait
|
float
|
Seconds to wait for the server to come up before declaring it started. A server that exits
within that time (a display number already in use, for instance) is reported as an error.
It must be a positive finite number, and anything else is refused by name: |
0.5
|
Returns:
| Type | Description |
|---|---|
Popen | None
|
The |
Popen | None
|
early, or leave it to the |
Raises:
| Type | Description |
|---|---|
ValueError
|
when |
FileNotFoundError
|
when |
RuntimeError
|
when the server exits during the startup wait; |
Examples:
- Start a display before drawing, on a headless Linux box (skipped here, since it launches a real X server):
- A size Xvfb would reject is refused before anything is launched, on every platform:
- So is a startup wait that would not wait: the argument is checked before the platform is, so
these answer the same way on Linux, Windows and macOS:
>>> from digitalearth.three_d import start_xvfb >>> for wait in (0, -1, float("inf"), float("nan")): ... try: ... start_xvfb(wait=wait) ... except ValueError as error: ... print(error) start_xvfb() needs wait as a positive finite number of seconds; got 0 start_xvfb() needs wait as a positive finite number of seconds; got -1 start_xvfb() needs wait as a positive finite number of seconds; got inf start_xvfb() needs wait as a positive finite number of seconds; got nan
See Also
digitalearth.three_d.core.base.Scene3DBase: off_screen= is the other half of rendering headless.
Source code in src/digitalearth/three_d/app/headless.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |