Vedo not clearing the plot and not closing the window (Python)

102 Views Asked by At

I am trying to create some scientific visualisations using Python. I use the vedo library. The plot is not getting cleared and the window is not closing. Please help me diagnose this issue.

I have a set of vtk files in sequential order and I am trying to create an animation. This is my code:

from vedo import *
import os

vtk_files = [file for file in os.listdir() if file.endswith('.vtk')]
vtk_files.sort()  

# Create a Plotter instance
plotter = Plotter()

for i in vtk_files:

    print(i)
 
    mesh = load(i,unpack=True, force=True)

    plotter += mesh

    plotter.show()

    plotter.clear()

    plotter.close()

While going through the loop, a plot window pops up showing the first vtk file and then it freezes. The plot is not getting cleared and the window is not closing. That means the plotter.clear() and plotter.close() commands are not working. When I manually close the plot window the code jumps to the next vtk file, but it doesn't show the plot as the plot window is already closed. I have the same problem with another visualisation package. Is it something with plotting module (Pyqt or any widget) which these libraries use? Please help.

I have checked the vtk and vedo documentation but I can't find what's wrong. I have gone through a lot of questions on stack overflow and github but I still can't find anything.

1

There are 1 best solutions below

0
On

You are closing the plotter inside the loop.. Consider these options:

from vedo import *
meshes = load("letter_*.vtk") # sort is done automatically
plotter = Plotter(axes=1)
for i, m in enumerate(meshes):
    m.pos([i*2000, 0, 0]).color(i)
plotter.show(meshes).close()

enter image description here

Another option:

import time
from vedo import *
meshes = load("letter_*.vtk")
plotter = Plotter(interactive=False)
plotter.show() # show the empty window
for i, m in enumerate(meshes):
    m.color(i)
    plotter.remove(meshes).add(m)
    plotter.reset_camera().render()
    time.sleep(0.5)
plotter.interactive().close()

will visualise your meshes one by one. Note that objects can also be removed by name e.g.:

mesh.name = "jack" then plotter.remove("jack") will remove it.