Python Extensions
Use Python to create Actions, Components, or Viewers with CamiTK’s Python API.
Getting Started with Python Extensions
Python extensions are ideal for rapid prototyping, machine learning integration, and scripting. DevStudio generates a Python file for each action for you to complete.
Steps to Create a Python Extension:
- Open DevStudio from the CamiTK menu.
- Define your extension's name, parameters, and input components.
- DevStudio generates the boilerplate code: a
.pyfor each action for you to edit and a.camitkcontaining the metadata of your extension. - As shown in the devstudio video click on Initialize to generate the boilerplate code for first time and then on Register to integrate it in CamiTK (it will then be automatically available in the corresponding Action menu).
- Complete the
processfunction in the generated Python file.
Python Action code template
When DevStudio generates the template code of an action, you will find four functions in the corresponding Python file. Some Python code is generated when DevStudio creates your action to show you an example of what is expected (see devstudio video). It is now up to you to modify the four functions/methods:
init: this function is automatically called when the extension is loaded by CamiTK. Modify it to enter code to initialize fields, state variables, or open connection to a device.targetDefinedis run when the user changes the input data (called target in an action) in the interface by selecting a component and then click on your action to trigger it. Use this if you need to update the action state/fields depending on the input data.parameterChangedis run when a parameter value is modified is the GUI automatically generated from your action parameters you added in DevStudio. Use this if you need to update the action state/fields depending on the input parameters.processthis is the main code, run when the user clicks on the Apply button. This is where you write the Python code that details your data processing.
Check the API and tutorials to see what is possible and some examples.
CamiTK Python API
Python API Documentation
The CamiTK Python API provides classes and methods to interact with CamiTK's core functionality. Explore the full documentation:
Python Tutorials
Tutorials
Learn by example! Check out our Python extension tutorials to understand how to:
- Create a Sphere MeshComponent.
- Process ImageComponents (e.g., threshold).
- Integrate PyTorch models for segmentation.
Qt and VTK Dependencies
Important Notes
CamiTK is linked to VTK and Qt shared libraries. When using PyQt, PySide, or VTK in your Python extension:
- The Qt and VTK bindings must match the versions used to compile CamiTK. On Debian/Ubuntu, use PySide (not PyQt) because it is synchronized with the system's Qt version.
- CamiTK does not install PyQt/PySide/VTK via pip. Instead, it links to the system-installed versions to ensure compatibility.
- For CamiTK 6.0+, use PySide6 if compiled with Qt6, or PySide2 if compiled with Qt5.
python3-pyside6.qtcore, python3-vtk9).