Slicer API
Tutorials
Check out these developer tutorials to get started with customizing and extending 3D Slicer using Python scripting or C++.
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
Python
Native Python documentation
Python-style documentation is available for the following packages:
Doxygen-style documentation
Slicer core infrastructure is mostly implemented in C++ and it is made available in Python in the slicer
namespace.
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:
Qt classes (class name starts with
q
orQ
): for example, qSlicerMarkupsPlaceWidget. These classes are all derived from QObject and follow Qt conventions. They support properties, signals, and slots.VTK classes (class name starts with
vtk
): for example, vtkMRMLModelDisplayNode. These classes are all derived from vtkObject and follow VTK conventions.
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 theslicer.mrmlScene
object can be called as:slicer.mrmlScene.GetNumberOfNodesByClass('vtkMRMLTransformNode')
. In Qt classes, only methods that haveQ_INVOKABLE
keyword next to them are available from Python.virtual
andoverride
specifiers 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 usingqt.QSomeObject()
. For examplemyObj = qt.QComboBox()
.New
(for VTK classes): constructor, never needs an argument. VTK objects can be instantiated usingvtk.vtkSomeObject()
. For examplemyObj = 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 callingdel
or setting the variable to a new value. For example:del(myObj)
ormyObj = None
.SafeDownCast
(for VTK classes): not needed for Python, as type conversions are automatic.
Static Public Member Functions: can be accessed as
slicer.className.memberFunctionName(arguments)
for example:slicer.vtkMRMLModelDisplayNode.GetSliceDisplayModeAsString(0)
.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
setSomeProperty
methods show up in the documentation but in Python these methods are not available and instead property values can be set usingsomeProperty = ...
.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
enum
type). These values can be accessed asslicer.className.typeName
, for exampleslicer.qSlicerMarkupsPlaceWidget.HidePlaceMultipleMarkupsOption
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 returnedsomeClass*
(object pointer) -> Python: since Python takes care of reference counting, it can be simply interpreted in Python assomeClass
. The called method can modify the object.int
,char
,short
(with optionalsigned
orunsigned
prefix) -> Python:int
float
,double
-> Python:float
double[3]
-> Python: initialize a variable before the method call aspoint = np.zeros(3)
(orpoint = [0.0, 0.0, 0.0]
) and use it as argument in the functionconst char *
,std::string
,QString
,const QString&
-> Python:str
bool
-> Python:bool