python-2.7loggingvtkenthoughtmayavi

Disable or Catch VTK warnings in vtkOutputWindow when embedding Mayavi


I'd like to either disable the VTK warning window or, better yet, catch them to handle with my application's logging system. My application is using an embedded mayavi view, and I don't want error windows popping up that I have no control over. The following code demonstrates the warning window.

import numpy as np
from mayavi import mlab

x1 = np.array([1, 1, 2, 3])
y1 = np.array([1, 1, 4, 2])
z1 = np.array([1, 1, 5, 1])

mlab.plot3d(x1, y1, z1)

mlab.show()

Ok, I've done some research and discovered that vtk.vtkObject.GlobalWarningDisplayOff() will disable the window completely, which is nice. Better yet the followingcode will log the warnings to a file (found it here):

def redirect_vtk_messages ():
    """ Can be used to redirect VTK related error messages to a
    file."""
    import tempfile
    tempfile.template = 'vtk-err'
    f = tempfile.mktemp('.log')
    log = vtkpython.vtkFileOutputWindow()
    log.SetFlush(1)
    log.SetFileName(f)
    log.SetInstance(log)

So while this is nice, I'm still unable to pipe the warnings directly into a logging handler. I'd rather not have to have a vtk_log file next to my regular log files. Also I might want to handle the warnings in my GUI somehow, or give the user options on how to handle them and constantly watching a log file for changes seems like a poor way to do that.

Any suggestions on a robust pythonic way to handle vtk warnings in an application which embeds mayavi/vtk?


Solution

  • I guess this partially answer your question, but you could implement an error observer in python as explained here http://public.kitware.com/pipermail/vtkusers/2012-June/074703.html and add it to the vtk class you are interested.

    In c++ I find much simpler to redirect the output to stderr (this example is for windows):

    vtkSmartPointer<vtkWin32OutputWindow> myOutputWindow = vtkSmartPointer<vtkWin32OutputWindow>::New(); 
    myOutputWindow->SetSendToStdErr(true);
    vtkOutputWindow::SetInstance(myOutputWindow);
    

    In python I tried

    ow = vtk.vtkOutputWindow()
    ow.SendToStdErrOn()
    

    it sends the error to console, but I still see the vtk window and it doesn't really seem catching the errors.

    Another option could be to recompile vtk with VTK_USE_DISPLAY turned off ( http://osdir.com/ml/python-enthought-devel/2009-11/msg00164.html). I am not going to try this because I am using the vtk distribution already compiled in paraview