1

我有一个脚本,可以成功地使用 VisPy(以“坏”方式)为简单的画布设置动画。但是,当我将动画导出到文件时,无论是 .gif 还是视频格式(.MP4 等),大多数情况下它都会产生一个完全黑色的图像,不会改变。其他看似随机的时间实际上确实保存了正确的动画。我真的尝试了我能想到的一切。

import vispy
import vispy.scene
from make_shape import Make_shape
from vispy.scene import visuals
from vispy import app, gloo
from moviepy.editor import VideoClip
from vispy.gloo.util import _screenshot


# Canvas setup
canvas = vispy.scene.SceneCanvas(keys='interactive', show=True, resizable=True)
view = canvas.central_widget.add_view()

# Initial shape data
cube = Make_shape(3)
verts = cube.verts()
linepos = cube.frame(verts)

# Adds initial visuals to shape data
# lines can be done as a function with LinePlot 
scatter = visuals.Markers()
lines = visuals.Line()
axis = visuals.XYZAxis(parent=view.scene)
scatter.set_data(verts, edge_color=None, face_color=(1, 1, 1, .5), size=10)
lines.set_data(pos=linepos, connect="segments")
view.add(scatter)
view.add(lines)

# Animation of visuals
def update(ev):
    global verts, scatter
    verts += 0.0005
    linepos = cube.frame(verts)
    scatter.set_data(pos=verts)
    lines.set_data(pos=linepos)

#Ttime = 5
#intv = 0.000001
#intr = int(Ttime / intv)
timer = app.Timer()
timer.connect(update)
timer.start(interval=.01, iterations=500)

# Setup of camera
# view.camera = 'turntable'
# view.camera = 'perspective'
view.camera = 'arcball'
view.camera.center = (0.5, 0.5, 0.5)
view.camera.distance = 100
#view.camera.set_range(x=(-0.5, 1)) 
view.camera.fov = (1) 

# Runs animation
if __name__ == '__main__':
    import sys
    if sys.flags.interactive != 1:
        vispy.app.run()


# Writes animation to file
def make_frame(t):
    update(t)
    canvas.on_draw(None)
    return _screenshot((0, 0, canvas.size[0], canvas.size[1]))[:, :, :3]


animation = VideoClip(make_frame, duration=5)
animation.write_gif("test_animation2.gif", fps=24)  # export as GIF (slow)
# animation.write_videofile(fname+".mp4", fps=24)  # export as video
4

0 回答 0