# Python Wrappers ## Introduction This document is a reference for using VTK from Python. It is not a tutorial and provides very little information about VTK itself, but instead describes in detail the features of the Python wrappers and how using VTK from Python differs from using VTK from C++. It assumes that the reader is already somewhat familiar with both Python and VTK. ## Background The Python wrappers are automatically generated from the VTK source code, and for the most part, there is a one-to-one mapping between the VTK classes and methods that you can use from Python and the ones that you can use from C++. More specifically, the wrappers are a package of Python extension modules that interface directly to the VTK C++ libraries. When you use VTK through the wrappers, you are actually executing compiled C++ code, and there is very little performance difference between VTK/C++ and VTK/Python. ## Installation VTK for Python can be installed via either conda or pip, where the conda packages is maintained on conda-forge, while the pip packages are maintained by the VTK developers themselves. If you are first getting started, then pip is probably the most convenient way to install VTK for Python: pip install vtk This will provide a basic installation of VTK that includes all core functionality, but which will not include some of the specialized VTK modules that rely on external libraries. Binary packages for VTK can also be downloaded directly from https://www.vtk.org/download/. Instructions for building VTK from source code are given in the file [Documentation/dev/build.md][vtk-build] within the source repository. [vtk-build]: https://gitlab.kitware.com/vtk/vtk/-/blob/release/Documentation/dev/build.md ## Importing VTK is comprised of over one hundred individual modules. Programs can import just the modules that are needed, in order to reduce load time. from vtkmodules.vtkCommonCore import vtkObject from vtkmodules.vtkFiltersSources import vtkConeSource, vtkSphereSource from vtkmodules.vtkRenderingCore import ( vtkActor, vtkDataSetMapper, vtkRenderer, vtkRenderWindow ) import vtkmodules.vtkRenderingOpenGL2 When getting started, however, it is hard to know what modules you will need. So if you are experimenting with VTK in a Python console, or writing a quick and dirty Python script, it is easiest to simply import everything. There is a special module called '`all`' that allows this to be done: from vtkmodules.all import * After importing the VTK classes, you can check to see which module each of the classes comes from: for c in vtkObject, vtkConeSource, vtkRenderWindow: print(f"from {c.__module__} import {c.__name__}") The output is as follows: from vtkmodules.vtkCommonCore import vtkObject from vtkmodules.vtkFiltersSources import vtkConeSource from vtkmodules.vtkRenderingCore import vtkRenderWindow ### Factories and Implementation Modules In the first 'import' example above, you might be wondering about this line: import vtkmodules.vtkRenderingOpenGL2 This import is needed because `vtkRenderingOpenGL2` provides the OpenGL implementations of the classes in `vtkRenderingCore`. To see this in action, open a new Python console and do the following: >>> from vtkmodules.vtkRenderingCore import vtkRenderWindow >>> renwin = vtkRenderWindow() >>> type(renwin) >>> >>> import vtkmodules.vtkRenderingOpenGL2 >>> renwin2 = vtkRenderWindow() >>> type(renwin2) After `vtkRenderingOpenGL2` has been imported, the `vtkRenderWindow()` constructor magically starts returning a different type of object. This occurs because `vtkRenderWindow` is a *factory* class, which means that the kind of object it produces can be overridden by an *implementation* class. In order for the implementation class to do the override, all that is necessary is that its module is imported. To make things even more confusing, `vtkRenderingOpenGL2` is not the only module that contains implementations for the factory classes in `vtkRenderingCore`. The following modules are often needed, as well: import vtkmodules.vtkInteractionStyle import vtkmodules.vtkRenderingFreeType Although you only need implementations for the factory classes that you use, it can be hard to know which classes are factory classes, or what modules contain implementations for them. Also, it can be difficult to even know what classes you are using, since many VTK classes make use of other VTK classes. An example of this is `vtkDataSetMapper`, which internally uses `vtkPolyDataMapper` to do the rendering. So even though `vtkDataSetMapper` is not a factory class, it needs an OpenGL implementation for `vtkPolyDataMapper`. The simplest approach is to import all the important implementation modules into your program, even if you are not certain that you need them. * For `vtkRenderingCore`, `import vtkRenderingOpenGL2, vtkRenderingFreeType, vtkInteractionStyle` * For `vtkRenderingVolume`, `import vtkRenderingVolumeOpenGL2` * For `vtkCharts`, `import vtkContextOpenGL2` ### Classic VTK Import There are many VTK programs that still import the '`vtk`' module, which has been available since VTK 4.0, rather than using the '`vtkmodules`' package that was introduced in VTK 8.2: import vtk The advantage (and disadvantage) of this is that it imports everything. It requires just one import statement for all of VTK, but it can be slow because VTK has grown to be very large over the years. Also note that, between VTK 8.2 and VTK 9.2.5, the use of the `vtk` module would confuse the auto-completion features of IDEs such as PyCharm. This was fixed in VTK 9.2.6. For 9.2.5 and earlier, the following can be used: import vtkmodules.all as vtk From the programmer's perspective, this is equivalent to '`import vtk`'. ## VTK Classes and Objects ### Classes Derived from vtkObjectBase In C++, classes derived from `vtkObjectBase` are instantiated by calling `New()`. In Python, these classes are instantiated by simply calling the constructor: o = vtkObject() For factory classes, the returned object's type might be a subtype of the class. This occurs because the Python wrappers are actually calling `New()` for you, which allows the VTK factory overrides to occur: >>> a = vtkActor() >>> type(a) When you create a VTK object in Python, you are in fact creating two objects: a C++ object, and a Python object that holds a pointer to the C++ object. The `repr()` of the object shows the memory address of the C++ object (in parentheses) and of the Python object (after the '`at`'): >>> a = vtkFloatArray() >>> a If you call `str()` or `print()` on these objects, the wrappers will call the C++ `PrintSelf()` method. The printed information can be useful for debugging: >>> o = vtkObject() >>> print(o) vtkObject (0x55858308a210) Debug: Off Modified Time: 85 Reference Count: 1 Registered Events: (none) ### Other Classes (Special Types) VTK also uses several classes that aren't derived from `vtkObjectBase`. The most important of these is `vtkVariant`, which can hold any type of object: >>> v1 = vtkVariant('hello') >>> v1 vtkmodules.vtkCommonCore.vtkVariant('hello') >>> v2 = vtkVariant(3.14) >>> v2 vtkmodules.vtkCommonCore.vtkVariant(3.14) The wrapping of these classes is fully automatic, but is done in a slightly different manner than `vtkObjectBase`-derived classes. First, these classes have no `New()` method, and instead the public C++ constructors are wrapped to create an equivalent Python constructor. Second, the Python object contains its own copy of the C++ object, rather than containing just a pointer to the C++ object. The vast majority of these classes are lightweight containers and numerical types. For example, `vtkQuaterniond`, `vtkRectf`, `vtkColor4ub`, etc. Many of them are actually class templates, which are discussed below. When you apply `print()` or `str()` to these objects, the `operator<<` of the underlying C++ object is used to print them. For `repr()`, the name of the type name is printed, followed by the `str()` output in prentheses. The result looks similar to a constructor, though it might look strange depending on what `operator<<` produces. >> v = vtkVariant() >> print(repr(v)) vtkmodules.vtkCommonCore.vtkVariant((invalid)) ### Class Templates There are several C++ templates in VTK, which can be tricky to use from the wrappers since the Python language has no real concept of templates. The wrappers wrap templates as dictionary-like objects that map the template parameters to template instantiations: >>> vtkSOADataArrayTemplate