question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating video with pygfx

See original GitHub issue

I’m working on the sphinx gallery for the examples and I got the basic pieces working. At the moment we can generate a gallery and have sphinx-gallery use pygfx to render images and add them. Something like this:

image

There are a few more things to iron out (like giving each example a title so that sphinx can link it automatically), but the basics are there.

One thing that is, perhaps, a bit of a luxury, but is still really useful for us is to show videos/animations in the gallery. This essentially works by creating a GIF that gets shown instead of the usual PNG. For this I have two discussion points:

  • Is there an existing example of capturing a video with pygfx?
  • The code for the video may have to be partly embedded in the example (to say “take a picture here, but do a video for X seconds there”). From a pedagogical perspective, this is suboptimal because we would ideally like to just show the example with the usual run() call so that they can see it nicely once they copy the code. Would an alternative be to say “we always show 5 sec of video for each render”?

I’m not 100% clear yet on how I want to solve this problem, but if you have ideas, I’m all ears 😃

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
FirefoxMetzgercommented, Dec 5, 2022

Using canvas.draw() does the trick. If we are not concerned about timings, we can do:

import pygfx as fx
import imageio.v3 as iio
from wgpu.gui.offscreen import WgpuCanvas


cube = fx.Mesh(
    fx.box_geometry(200, 200, 200),
    fx.MeshPhongMaterial(color="#336699"),
)

def animate():
    rot = fx.linalg.Quaternion().set_from_euler(fx.linalg.Euler(0.005, 0.01))
    cube.rotation.multiply(rot)

disp = fx.Display(WgpuCanvas())
disp.before_render = animate
disp.show(cube)

frames = []
n_frames = 3 * 25  # about 3 second GIF
for _ in range(n_frames):
    frames.append(disp.canvas.draw())

iio.imwrite("output.gif", frames, duration=40, loop=0)

output

This solves my issue. Closing!

0reactions
almarkleincommented, Nov 28, 2022

Another option could be to update the examples not to use the time, but assume that the fps is at 30 (which the GUI backends run at by default). Then we can just take screenshots (which may take much longer on lavapipe) and set the mp4/gif playback speed at 30 fps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Text rendering · Issue #20 · pygfx/pygfx - GitHub
I think our goal should be: Aim for high quality text rendering (though not necessarily perfect). Make sure that there is a default...
Read more >
Integrate pygfx in Qt — pygfx documentation
... from wgpu.gui.qt import WgpuCanvas import pygfx as gfx class Main(QtWidgets. ... _on_button_click) # Create canvas, renderer and a scene object self.
Read more >
Kushal Kolar on Twitter: "@sofroniewn @camachodejay ...
Thanks! I've been looking for people interested in developing a full scientific plotting library with it. Using the `pygfx` render engine directly is...
Read more >
Practical GPU Graphics with wgpu-py and Python: Creating ...
Practical GPU Graphics with wgpu-py and Python: Creating Advanced Graphics on Native Devices and the Web Using wgpu-py: the Next-Generation GPU API for ......
Read more >
Payden Global Fixed Income Fund PYGFX:NASDAQ - CNBC
Get Payden Global Fixed Income Fund (PYGFX:NASDAQ) real-time stock quotes, news, price and financial information from CNBC.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found