OpenVR modules with VTK and Python

Good day everyone, I’m having some issues when trying to use the Open VR modules to display something in my python program. My code looks as follows (all of this is done in the Spyder IDE):

import vtk


def main():
    colors = vtk.vtkNamedColors()

    file_name = 'DicomTest.vtk'

    # Read the source file.
    reader = vtk.vtkXMLUnstructuredGridReader()
    reader.SetFileName(file_name)
    reader.Update()  # Needed because of GetScalarRange
    output = reader.GetOutput()
    # scalar_range = output.GetScalarRange()

    # Create the mapper that corresponds the objects of the vtk.vtk file
    # into graphics elements
    mapper = vtk.vtkDataSetMapper()
    mapper.SetInputData(output)
    # mapper.SetScalarRange(scalar_range)
    mapper.ScalarVisibilityOff()

    # Create the Actor
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().EdgeVisibilityOn()
    actor.GetProperty().SetLineWidth(2.0)

    backface = vtk.vtkProperty()
    backface.SetColor(colors.GetColor3d("tomato"))
    actor.SetBackfaceProperty(backface)

    # Create the Renderer
    renderer = vtk.vtkOpenVRRenderer()
    renderer.AddActor(actor)
    renderer.SetBackground(1, 1, 1)  # Set background to white
    renderer.SetBackground(colors.GetColor3d("Wheat"))
    camera = vtk.vtkOpenVRCamera
    renderer.SetActiveCamera(camera)
    

    # Create the RendererWindow
    renderer_window = vtk.vtkOpenVRRenderWindow()
    renderer_window.AddRenderer(renderer)
    

    # Create the RendererWindowInteractor and display the vtk_file
    interactor = vtk.vtkOpenVRRenderWindowInteractor()
    interactor.SetRenderWindow(renderer_window)
    
        
    interactor.Initialize()
    interactor.Start()


if __name__ == '__main__':
    main()

However, I’m getting the following error: AttributeError: module ‘vtk’ has no attribute ‘vtkOpenVRRenderer’

How do I get these missing modules? Thanks for your time.

The VTK you’re using will need to be built with OpenVR support.

Thanks for your reply, Ben. Mind if I ask how do I do that? The way I did it was simply using the cmd command: pip install vtk

One option is to use VTK OpenVR binaries bundled with Paraview (for engineering applications) or 3D Slicer (for medical applications).

OpenVR classes are not Python-wrapped (at least in 3D Slicer) but interface classes are added that are Python-wrapped, which can be used to set up the virtual reality view (and they also provide continuous rendering while you run Python scripts or interaction the Python terminal). You can use Slicer’s Python environment as any other Python environment (you can pip install any packages, etc.). If you have any questions about using or Python scripting in Slicer, you can post it to https://discourse.slicer.org.