Search code examples
pythonvtk

How to interact with vtkplotter using custom functions?


I am trying to change the position of a plane at regular time interval in vtkplotter 3D plot. I could not find a builtin method to add this functionality.

I tried implementing it using threads but it appears that the position update only happens on mouseclick events.

import threading
import time
from vtkplotter import Plane, show

class Vol:
    def __init__(self):
        self.a = Plane(normal=(0, 0, 1), sx=10, sy=10)
        self.b = Plane(normal=(0, 1, 0), sx=10, sy=10)
        self.c = Plane(normal=(1, 0, 0), sx=10, sy=10)
        self.thread = threading.Thread(target=self.background)
        self.thread.start()
        show(self.a, self.b, self.c, bg="w")

    def background(self):
        while True:
            if hasattr(self, "a"):
                for i in range(-5, 6):
                    self.a.SetPosition(0, 0, i)
                    time.sleep(0.5)

if __name__ == "__main__":
    Vol()

How to implement this correctly ?


Solution

  • You need to render the scene in the loop. I would try something easier like:

    import time
    from vedo import *
    
    a = Plane(normal=(0, 0, 1), sx=10, sy=10)
    b = Plane(normal=(0, 1, 0), sx=10, sy=10)
    c = Plane(normal=(1, 0, 0), sx=10, sy=10)
    
    vp = show(a, b, c, bg="w", viewup='z', interactive=False)
    
    for i in range(-5, 6):
        a.z(i).color(i)
        time.sleep(0.5)
        show()
    
    interactive()