serhii.net

In the middle of the desert you can say anything you want

05 Sep 2024

Developing a 3D Slicer extension

Debugging

TL;DR Pycharm Professional instructions work

not TL;DR

Basics

Starting Slicer

Loads the module automatically (for some reason doing it manually doesn’t preserve it across restarts?..)

Slicer  --additional-module-path /path/to/my/extension/one/lower/than/what/i/would/pick/in/Slicer/GUI

Logging

  • Not directly mentioned in the help because they state it’s always better to use the debugger2
  • Slicer/Base/Python/slicer/slicerqt.py at main · Slicer/Slicer has the slicer setup
  • I can do logging.warning("test") in the python console and it works
  • Slicer settings allow changing the loglevel of its python console!

Module types: CLI modules

Different types of modules exist: Module Overview — 3D Slicer documentation

In the first tests there was a lot of latency.

Solution: CLI modules (not extensions) that can run in the background and whose status can be queried, which can be added to new extension from extension editor.

Now I understad this better:

  • CLI modules are simple I/O, non-blocking, that’s where the networkig logic should go to
  • scripted are python scripts with GUIs

Looking into the sample code

Module of type scripted

Module of type scriptedCLI

  • I hope it’s the same as type CLI, but only with python

  • I think it’s lassoan/SlicerPythonCLIExample: Example extension for 3D Slicer that demonstrates how to make a Python script available as a CLI module

  • Is a CLI script w/ an XML that has module metadata + examples

    • CLI module goes in the same examles menu
    • and a basic UI has been generated for it? Wow
    • Running it from the UI actually shows a progress bar and it doesn’t block the interface! Woohoo!
  • Adding it added it to CMakeLists.xml: add_subdirectory(my_scripted_cli)

  • python dev tools actually show the CLI used to run a CLI module!

    • the input arguments are filepaths, and slicer automatically generates and then deletes temporary files for this when running
  • can’t debug it the usual way, I guess I’ll have to add the pydevd-pycharm things to the code

[VTK] /opt/3dslicer/bin/python-real /home/.../cli_model_service.py /tmp/Slicer-sh/IACHCI_vtkMRMLScalarVolumeNodeD.nrrd 1 /tmp/Slicer-sh/IACHCI_vtkMRMLScalarVolumeNodeF.nrrd 

(Auto-creating) GUI

Auto-loading the module at startup

Module from console with arguments - Support - 3D Slicer Community

Slicer.exe --python-code "selectModule('SampleData'); getModuleGui('SampleData').setCategoryVisible('BuiltIn', False)"

File operations

Script repository — 3D Slicer documentation:

# Create a new directory where the scene will be saved into
import time
sceneSaveDirectory = slicer.app.temporaryPath + "/saved-scene-" + time.strftime("%Y%m%d-%H%M%S")
if not os.access(sceneSaveDirectory, os.F_OK):
  os.makedirs(sceneSaveDirectory)

Inspiration

Useful bits from documentation

Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus