Unreasonable behavior of vtkpython with vtkOutputWindow
When running VTK from the vtkpython executable, vtkOutputWindow steals all of python's output. Example:
$ bin/vtkpython
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import vtk
>>> w = vtk.vtkFileOutputWindow()
>>> w.SetFileName("vtklog.txt")
>>> vtk.vtkOutputWindow.SetInstance(w)
After SetInstance(w), python stops printing to the shell:
>>> print("Hello, World!")
>>> 1.0/0.0
>>>
All output is redirected to the log instead:
$ cat vtklog.txt
Hello, World!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError
:
float division by zero
Calling SetInstance(None) set everything right again:
>>> vtk.vtkOutputWindow.SetInstance(None)
>>> print("Hello, World!")
Hello, World!
>>> 1.0/0.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: float division by zero
>>>
This issue was also seen by the pyvista folks: https://github.com/pyvista/pyvista/discussions/4098
From C++ it is possible to avoid redirecting python's output as shown by !8445 (merged). However, that fix is only usable when vtkPythonInterpreter is embedded in a C++ program. For people who use the vtkpython executable directly, there is no way to control the redirection. It might be best if the vtkpython executable never redirects its output, to make it behave just like native python (which is how most people expect it to behave).