<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.slicer.org/w/index.php?action=history&amp;feed=atom&amp;title=Documentation%2F4.6%2FDevelopers%2FPython_scripting</id>
	<title>Documentation/4.6/Developers/Python scripting - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.slicer.org/w/index.php?action=history&amp;feed=atom&amp;title=Documentation%2F4.6%2FDevelopers%2FPython_scripting"/>
	<link rel="alternate" type="text/html" href="https://www.slicer.org/w/index.php?title=Documentation/4.6/Developers/Python_scripting&amp;action=history"/>
	<updated>2026-04-04T00:00:32Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.33.0</generator>
	<entry>
		<id>https://www.slicer.org/w/index.php?title=Documentation/4.6/Developers/Python_scripting&amp;diff=47734&amp;oldid=prev</id>
		<title>UpdateBot: Nightly -&gt; 4.6</title>
		<link rel="alternate" type="text/html" href="https://www.slicer.org/w/index.php?title=Documentation/4.6/Developers/Python_scripting&amp;diff=47734&amp;oldid=prev"/>
		<updated>2016-11-07T07:17:59Z</updated>

		<summary type="html">&lt;p&gt;Nightly -&amp;gt; 4.6&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;noinclude&amp;gt;{{documentation/versioncheck}}&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Start Here =&lt;br /&gt;
&lt;br /&gt;
Please read [https://docs.google.com/presentation/d/1JXIfs0rAM7DwZAho57Jqz14MRn2BIMrjB17Uj_7Yztc/edit?usp=sharing these slides] and work through the example code.&lt;br /&gt;
&lt;br /&gt;
Refer to [http://www.na-mic.org/Wiki/index.php/2013_Project_Week_Breakout_Session:Slicer4Python this description that includes links to all the documentation].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Background =&lt;br /&gt;
&lt;br /&gt;
This is an evolution of the [[Slicer3:Python|python implementation in slicer3]].  Slicer's APIs are now natively wrapped in python.  &lt;br /&gt;
&lt;br /&gt;
Topics like plotting are still experimental in slicer4.&lt;br /&gt;
&lt;br /&gt;
See [http://www.na-mic.org/Wiki/index.php/AHM2012-Slicer-Python this 2012 presentation on the state of python in slicer4].&lt;br /&gt;
&lt;br /&gt;
'''See [[Documentation/{{documentation/currentversion}}/Training#Slicer4_Programming_Tutorial|the python slicer4 tutorial for more examples]].'''&lt;br /&gt;
&lt;br /&gt;
[[Documentation/{{documentation/version}}/Developers/Tutorials/SelfTestModule|Slicer Self Tests]] can be written in python, and provide a good source of examples for manipulating the data, logic, and gui of slicer.&lt;br /&gt;
&lt;br /&gt;
= Start Here for Scripted Module and Extension Development=&lt;br /&gt;
An extensive tutorial and reference page was created [http://www.na-mic.org/Wiki/index.php/2013_Project_Week_Breakout_Session:Slicer4Python for the Slicer/Python breakout session at the NA-MIC 2013 Summer Project Week].&lt;br /&gt;
&lt;br /&gt;
= Usage options =&lt;br /&gt;
&lt;br /&gt;
==Python Interactor==&lt;br /&gt;
&lt;br /&gt;
Use the Window-&amp;gt;Python Interactor (Control-3 on window/linux, Command-3 on mac) to bring up the Qt-based console with access to the vtk, Qt, and Slicer wrapped APIs.&lt;br /&gt;
&lt;br /&gt;
Most python code can be installed and run from this window, but because it exists in the event driven Qt GUI environment, some operations like, like parallel processing or headless operation, are not easily supported.&lt;br /&gt;
&lt;br /&gt;
=== Examples ===&lt;br /&gt;
&lt;br /&gt;
Start slicer4 and bring up python console.  Load a volume like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; slicer.util.loadVolume(slicer.app.slicerHome + &amp;quot;/share/MRML/Testing/TestData/fixed.nrrd&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Get the volume node for that volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; n = getNode('fixed')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can use Tab to see lists of methods for a class instance.&lt;br /&gt;
&lt;br /&gt;
==== Accessing Volume data as numpy array ====&lt;br /&gt;
&lt;br /&gt;
You can easily inspect and manipulate volume data using numpy and related code.  In slicer you can do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; a = array('fixed')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and a will be a pointer to the appropriate data (no data copying).  Scalar volumes become three-dimensional arrays, while vector volumes become 4D, and tensor volumes are 5D.  All arrays can be manipulated directly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Running a CLI from Python ====&lt;br /&gt;
&lt;br /&gt;
Here's an example to create a model from a volume using the [[Documentation/4.0/Modules/GrayscaleModelMaker|Grayscale Model Maker]]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def grayModel(volumeNode):&lt;br /&gt;
  parameters = {}&lt;br /&gt;
  parameters[&amp;quot;InputVolume&amp;quot;] = volumeNode.GetID()&lt;br /&gt;
  outModel = slicer.vtkMRMLModelNode()&lt;br /&gt;
  slicer.mrmlScene.AddNode( outModel )&lt;br /&gt;
  parameters[&amp;quot;OutputGeometry&amp;quot;] = outModel.GetID()&lt;br /&gt;
  grayMaker = slicer.modules.grayscalemodelmaker&lt;br /&gt;
  return (slicer.cli.run(grayMaker, None, parameters))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To try this, download the MRHead dataset from the [[Documentation/4.0/Modules/SampleData|Sample Data]] and paste the code into the python console and then run this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
v = getNode('MRHead')&lt;br /&gt;
cliNode = grayModel(v)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the CLI module runs in a background thread, so the call to grayModel will return right away.  But the slicer.cli.run call returns a cliNode (an instance of [http://slicer.org/doc/html/classvtkMRMLCommandLineModuleNode.html vtkMRMLCommandLineModuleNode]) which can be used to monitor the progress of the module.&lt;br /&gt;
&lt;br /&gt;
Here is an example for running a CLI module from a scripted module:&lt;br /&gt;
https://github.com/fedorov/ChangeTrackerPy/blob/master/ChangeTracker/ChangeTrackerWizard/ChangeTrackerRegistrationStep.py#L56-L67&lt;br /&gt;
&lt;br /&gt;
'' Passing Fiducials to CLIs via a Python Script ''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
import SampleData&lt;br /&gt;
sampleDataLogic = SampleData.SampleDataLogic()&lt;br /&gt;
head = sampleDataLogic.downloadMRHead()&lt;br /&gt;
volumesLogic = slicer.modules.volumes.logic()&lt;br /&gt;
headLabel = volumesLogic.CreateLabelVolume(slicer.mrmlScene, head, 'head-label')&lt;br /&gt;
&lt;br /&gt;
fiducialNode = slicer.vtkMRMLAnnotationFiducialNode()&lt;br /&gt;
fiducialNode.SetFiducialWorldCoordinates((1,0,5))&lt;br /&gt;
fiducialNode.SetName('Seed Point')&lt;br /&gt;
fiducialNode.Initialize(slicer.mrmlScene)&lt;br /&gt;
fiducialsList = getNode('Fiducials List')&lt;br /&gt;
&lt;br /&gt;
params = {'inputVolume': head.GetID(), 'outputVolume': headLabel.GetID(), 'seed' : fiducialsList.GetID(), 'iterations' : 2} &lt;br /&gt;
&lt;br /&gt;
cliNode = slicer.cli.run(slicer.modules.simpleregiongrowingsegmentation, None, params , wait_for_completion=True)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''Checking Status''&lt;br /&gt;
&lt;br /&gt;
In this example we create a simple callback that will be called whenever the cliNode is modified.  The status will tell you if the nodes is Pending, Running, or Completed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def printStatus(caller, event):&lt;br /&gt;
  print(&amp;quot;Got a %s from a %s&amp;quot; % (event, caller.GetClassName()))&lt;br /&gt;
  if caller.IsA('vtkMRMLCommandLineModuleNode'):&lt;br /&gt;
    print(&amp;quot;Status is %s&amp;quot; % caller.GetStatusString())&lt;br /&gt;
&lt;br /&gt;
cliNode.AddObserver('ModifiedEvent', printStatus)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''Get list of parameter names''&lt;br /&gt;
&lt;br /&gt;
The following script prints all the parameter names of a CLI parameter node:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cliModule = slicer.modules.grayscalemodelmaker&lt;br /&gt;
n=cliModule.cliModuleLogic().CreateNode()&lt;br /&gt;
for groupIndex in xrange(0,n.GetNumberOfParameterGroups()):&lt;br /&gt;
  for parameterIndex in xrange(0,n.GetNumberOfParametersInGroup(groupIndex)):&lt;br /&gt;
    print '  Parameter ({0}/{1}): {2} ({3})'.format(groupIndex, parameterIndex, n.GetParameterName(groupIndex, parameterIndex), n.GetParameterLabel(groupIndex, parameterIndex))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Accessing slice vtkRenderWindows from slice views ====&lt;br /&gt;
&lt;br /&gt;
The example below shows how to get the rendered slice window.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
lm = slicer.app.layoutManager()&lt;br /&gt;
redWidget = lm.sliceWidget('Red')&lt;br /&gt;
redView = redWidget.sliceView()&lt;br /&gt;
wti = vtk.vtkWindowToImageFilter()&lt;br /&gt;
wti.SetInput(redView.renderWindow())&lt;br /&gt;
wti.Update()&lt;br /&gt;
v = vtk.vtkImageViewer()&lt;br /&gt;
v.SetColorWindow(255)&lt;br /&gt;
v.SetColorLevel(128)&lt;br /&gt;
v.SetInputConnection(wti.GetOutputPort())&lt;br /&gt;
v.Render()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Script Repository = &lt;br /&gt;
&lt;br /&gt;
See [[Documentation/Nightly/ScriptRepository|ScriptRepository]]&lt;br /&gt;
&lt;br /&gt;
{{:Documentation/{{documentation/version}}/Developers/FAQ/Python Scripting|Python Scripting}}&lt;/div&gt;</summary>
		<author><name>UpdateBot</name></author>
		
	</entry>
</feed>