A simple embedded programming editor for 3D Slicer, based on monaco editor.
The ScriptEditor extension provides the open-source Monaco editor as a module inside the Slicer. It supports auto-completion, and syntax highlighting and allows the scripts to be directly sent to the 3D Slicer's built-in Python console.
- Script Editor: This is the only module that the extension provides.
The most typical use case for Script Editor is to develop, debug, and execute Python scripts directly within the 3D Slicer environment. This allows users to automate tasks, create custom analyses, and enhance the functionality of 3D Slicer through scripting.
- Open 3D Slicer.
- Go to the
Extension Managerby clicking on theViewmenu and selectingExtension Manager. - Search for
ScriptEditor. - Click
Installnext to theScriptEditorextension. - Restart 3D Slicer to activate the extension.
- After restarting, hit
Ctrl+F(or click the search icon in the toolbar). - Select
Script Editorfrom the list of modules.
- In the
Script Editormodule, you will see a combo box labeledScript Node. - You can create a new Python text node by selecting "(Create New Python Text Node)" from the combo box.
- Alternatively, you can select an existing Python text node if you have previously created one.
- Once a text node is selected, the Monaco Editor will be enabled.
- Write your Python script in the editor. This script can interact with the 3D Slicer API to manipulate data, perform analyses, or automate tasks.
- After writing your script, you can save it by clicking the "Save" button.
- The script will be saved in the selected text node.
- To execute your script, ensure it is selected in the text node combo box.
- Click the "Run" button (if implemented), or use the Slicer Python Interactor to execute the script manually by copying and pasting the code.
- You can copy the code from the editor to the clipboard by clicking the "Copy" button.
- This is useful if you want to run the script in the Slicer Python Interactor or share it with others.
Here's an example script that can be used to load a sample volume and apply a Gaussian smoothing filter:
import SampleData
import sitkUtils
import SimpleITK as sitk
# Load sample volume
sampleVolume = SampleData.SampleDataLogic().downloadMRHead()
# Apply Gaussian smoothing
inputImage = sitkUtils.PullVolumeFromSlicer(sampleVolume)
smoothedImage = sitk.SmoothingRecursiveGaussian(inputImage, sigma=2.0)
# Push the result back to Slicer
smoothedVolume = sitkUtils.PushVolumeToSlicer(smoothedImage, name='SmoothedVolume')
slicer.util.setSliceViewerLayers(background=smoothedVolume)- Debugging: Use
printstatements to output information to the Slicer Python Interactor for debugging purposes. - Node Management: Utilize the Slicer module widgets to manage nodes and visualize results effectively.
- Resources: Refer to the 3D Slicer Script Repository for more script examples and API documentation.
The Script Editor extension has undergone significant development and improvements over its versions. A previous iteration of the extension can be found in the repository at pieper/SlicerEditor.
This project was undertaken during the 3D Slicer Project Week, a collaborative event where developers and researchers work on Slicer-related projects. The specific undertaking for Script Editor was part of the 3D Slicer Project Week 41, held at MIT in 2024. The goal of this project was to create a simple and effective editor for Python scripting within 3D Slicer, leveraging the Monaco Editor for enhanced user experience.
ScriptEditor is created and made available by funding from National Science Foundation (MorphoCloud: DBI/2301405; Imageomics Institute: OAC/2118240)


