Check out these developer tutorials to get started with customizing and extending 3D Slicer using Python scripting or C++.
Majority of Slicer core modules and all basic infrastructure are implemented in C++. Documentation of these classes are available at: https://apidocs.slicer.org/main
Native Python documentation
Python-style documentation is available for the following packages:
Slicer core infrastructure is mostly implemented in C++ and it is made available in Python in the
Documentation of these classes is available at: https://apidocs.slicer.org/main/
C++ classes are made available in Python using two mechanisms: PythonQt and VTK Python wrapper. They have a few slight differences:
This documentation is generated using the Doxygen tool, which uses C++ syntax. The following rules can help in interpreting this documentation for Python:
Public member functions: They can be accessed as
objectName.memberFunctionName(arguments)for example a method of the
slicer.mrmlSceneobject can be called as:
slicer.mrmlScene.GetNumberOfNodesByClass('vtkMRMLTransformNode'). In Qt classes, only methods that have
Q_INVOKABLEkeyword next to them are available from Python.
overridespecifiers can be ignored (they just indicate that the method can be or is overridden in a child class).
className(for Qt classes): constructor, shows the arguments that can be passed when an object is created. Qt objects can be instantiated using
qt.QSomeObject(). For example
myObj = qt.QComboBox().
New(for VTK classes): constructor, never needs an argument. VTK objects can be instantiated using
vtk.vtkSomeObject(). For example
myObj = vtk.vtkPolyData().
~className: destructor, can be ignored, Python calls it automatically when needed (when there are no more references to an object). If a variable holds the last reference to an object then it can be deleted by calling
delor setting the variable to a new value. For example:
myObj = None.
SafeDownCast(for VTK classes): not needed for Python, as type conversions are automatic.
Static Public Member Functions: can be accessed as
Properties (for Qt classes): these are values that are accessible as object attributes in Python and can be read and written as
objectName.propertyName. For example:
>>> w = slicer.qSlicerMarkupsPlaceWidget() >>> w.deleteAllControlPointsOptionVisible True >>> w.deleteAllControlPointsOptionVisible=False >>> w.deleteAllControlPointsOptionVisible False
Public slots (for Qt classes): publicly available methods. Note that
setSomePropertymethods show up in the documentation but in Python these methods are not available and instead property values can be set using
someProperty = ....
Signals (for Qt classes): signals that can be connected to Python methods
def someFunction(): print("clicked!") b = qt.QPushButton("MyButton") b.connect("clicked()", someFunction) # someFunction will be called when the button is clicked b.show()
Public Types: most commonly used for specifying enumerated values (indicated by
enumtype). These values can be accessed as
slicer.className.typeName, for example
Protected Slots, Member Functions, Attributes: for internal use only, not accessible in Python.
Mapping commonly used data types from C++ documentation to Python:
void-> Python: if the return value of a method is this type then it means that no value is returned
someClass*(object pointer) -> Python: since Python takes care of reference counting, it can be simply interpreted in Python as
someClass. The called method can modify the object.
unsignedprefix) -> Python:
double-> Python: initialize a variable before the method call as
point = np.zeros(3)(or
point = [0.0, 0.0, 0.0]) and use it as argument in the function
const char *,
const QString&-> Python: