Screenshots and Movies with WebGL¶
One can use the REBOUND WebGL ipython widget to capture screenshots of a simulation. These screenshots can then be easily compiled into a movie.
The widget is using the ipywidgets package which needs to be installed and enabled. More information on this can be found in the ipywidgets documentation at https://ipywidgets.readthedocs.io/en/latest/user_install.html. You also need a browser and a graphics card that supports WebGL.
Note that this is a new feature and might not work on all systems. We've tested it on python 3.5.2.
Let's first create a simulation and display it using the REBOUND WebGL widget.
import rebound sim = rebound.Simulation() sim.add(m=1) # add a star for i in range(10): sim.add(m=1e-3,a=0.4+0.1*i,inc=0.03*i,omega=5.*i) # Jupiter mass planets on close orbits sim.move_to_com() # Move to the centre of mass frame w = sim.getWidget() w
You can now drag the widget with your mouse or touchpad to look at the simulation from a different angle. Keep the shift key pressed while you drag to zoom in or out.
To take a single screenshot, all you have to do is call the
takeScreenshot function of the widget.
You will see that there is now a file
screenshot00000.png in the current directory. It shows the same view as the WebGL widget in the notebook. To get a larger image, increase the size of the widget (see the documentation for the widget for all possible options).
We could now rotate the widget or integrate the simulation. If we then execute the same command
takeScreenshot command again, we will get another file
Consider the following code:
# w.takeScreenshot() # sim.integrate(10) # w.takeScreenshot()
This will not produce the desired outcome (in fact it will through an exception). The reason is complex. In short,
ipywidgets provides no blocking calls to wait for updates of a widget because the widget updates make use of the ipython event loop which does not get run during an execution of a cell.
Thus, to capture multiple screenshots at different times, one either needs to take one screenshot per cell, or use the following more convenient way:
times = [0,10,100] w.takeScreenshot(times)
The above code will generate three screenshots at the requested times.