Interactor Start() blocking execution

Is there a way (in python) to start the vtkRenderWindowInteractor in a different process so that the execution flow is not blocked?
Maybe with the vtkMultiThreader class?

The Start() method must be called from the main thread, and it passes execution of the main thread to the event loop. After Start() has been called, the only way to execute code in the main thread is via event callbacks (usually mouse events, keyboard events, or timer events).

1 Like

Is the constraint that Start() has to be called from the main thread, or merely that all vtk calls have to be made from the same thread, even if that’s not the main thread (in python, specifically)? I was hoping to add a vtk-based visualizer to another code, and it would be nice if all the vtk stuff ran in a non-main thread (all one thread), so the existing code wouldn’t have to be modified as much.

If you already have an app with a GUI and whatnot, then a safe approach is to use vtkGenericRenderWindowInteractor. Its Start() method doesn’t block, because it doesn’t have its own event loop. Instead, it expects to be fed events by the GUI framework you are already using. Examples of vtkGenericRenderWindowInteractor in action are,, and

I’ve never tried calling Start() from another thread. It’s certainly not guaranteed to work, and the stuff that Start() does under-the-hood varies tremendously from one operating system to another, so even if it works in one OS, it probably won’t work in all.

Hopefully, if there’s someone who has tried it, they’ll chime in to the conversation.

No, the app has no GUI. I’m thinking of apps that have no visualization, they just do computation, and I want to add a visualizer. I’ll look at those examples, though.

I used to use Tk for that. The Tk event loop and Python’s REPL loop cooperated seamlessly with each other. Without using any multithreading, either. Tk was designed from the ground up to be a GUI for scripting.

Do you mean you would generate image files or show static images while running some processing? Or you would like to show data in an interactive renderer (allow user to zoom/rotate view, etc)?

Interactive, definitely. Initially I don’t need the calculation code to do anything other than pass static information to the viewer (which could then rotate, zoom, etc), but eventually I’d like it to be able to update that information somehow, and perhaps get information back. I can work around the issue by having my viewer take over the main thread, and allow the calculation code to pass a function that is executed in the child thread, but that does require enough refactoring to wrap the entire calculation code in a single function.

Yes, in this case you probably need to do your processing in a background thread or do it in the main thread but in small chunks (and process GUI events after each chunk). You may also write data to file and spawn a new process for visualization.

I tend to stream the data with socket but the start() always block it …How can deal with it in VTK.
Best wishes for you.

You normally use an interactor that integrates with the event loop of your GUI toolkit (e.g., Qt).

If you don’t use a GUI toolkit then you can add timer that regularly calls your callback function that communicates with the other threads, etc.; or you can implement a custom interactor.