Oldrich Oldrich - 1 year ago 110
Python Question

controlling VTK through Matlab

I have a Matlab function that takes a 3D binary object as input, saves it as temporary file, then loads a python script through cmd (I made this before Matlab's python integration). The python script loads and reorganizes the 3D data and displays them through VTK.

The python script also creates some VTK controls that I would like to control through Matlab GUI sliders rather than VTK sliders.

Is there a way to open a realtime data flow between VTK and Matlab, either through Matlab's new python integration or python's Matlab engine API (or any other way)? I haven't found any way to control VTK actors other than VTK-created controls right in the VTK interactor window so far.

EDIT: I also ran into an odd issue when trying to figure this out. When I run the VTK visualization with

system(['pythonw ' folder '\vizualize.pyw" &']);
(and adding the
so that the script executes itself) everything runs smooth, but when I do it with
and attempt to close the vizualization window, it doesn't close but it does return control to matlab command line. On the second attempt, it kills the matlab window instead and then after a while itself. The scripts are identical.

EDIT 2: Adding
right after
fixed this issue for some reason.

Thanks for any answers or ideas!

Answer Source

I kind of got it to work. Here's what I did:

  1. matlab session running the GUI converts to shared session through matlab.engine.shareEngine('shared_matlab_session') and keeps track of the matlab GUI slider position in matlab_slider_pos variable

  2. the python script connects to the session through MatEng=matlab.engine.connect_matlab('shared_matlab_session') before initializing VTK objects

  3. the VTK render window adds an observer for TimerEvent that triggers SliderSync python callback function every 0.1 seconds

  4. the SliderSync function checks if the current VTK slider value corresponds with result of calling MatEng.eval('matlab_slider_pos') and if it doesn't, update the VTK slider value along with the rest of VTK pipeline. Of course at this point the VTK slider is no longer needed and the function can directly update what needs updating.

I really hope there is a more elegant way to view a 3D volume in matlab than the abomination I created (it works pretty smooth though). Comments are still welcome!