# Python Wrappers The Python wrappers for VTK are produced algorithmically from the VTK header files, and using VTK through Python is very much like using VTK through C++. Nearly all of the same functionality is available, with Python types converted automatically to C++ types and vice-versa. This document assumes that you are already familiar with VTK itself, and want to know more details about how to use VTK through Python. # Installation {#installation} The most convenient way to install VTK for Python is with pip, which is a package manager that comes with 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 {#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 {#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 implmentation 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` ## Obsolete VTK Import {#obsolete-import} There are many VTK programs that still use the old '`vtk`' module, instead of using the '`vtkmodules`' package introduced in VTK 8.2: import vtk Although this works, it is not recommended. The '`vtk`' module is only for backwards-compatibility and is, in fact, just the '`vtkmodules.all`' with a different name. Because of the details of how the renaming is done, the '`import vtk`' statement can confuse IDEs like PyCharm and make them unable to index the module. Instead, if you desire a module named '`vtk`', either for personal preference or a need for backwards compatibility, then use this: import vtkmodules.all as vtk This will make it clear to people who read your code that '`vtk`' is simply a different name for '`vtkmodules.all`'. It will also keep your IDE from becoming confused about what exactly the '`vtk`' module is. # VTK Classes and Objects {#classes-and-objects} ## Classes Derived from vtkObjectBase {#vtkobject-classes} 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) {#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 contructor. 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 {#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