# Extract centerline Python scripting

**URL:** https://discourse.slicer.org/t/extract-centerline-python-scripting/21275
**Category:** Support
**Tags:** centerlines
**Created:** [December 30, 2021, 12:28pm UTC](https://discourse.slicer.org/t/extract-centerline-python-scripting/21275 "2021-12-30T12:28:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)
#### Post date: [December 30, 2021, 12:28pm UTC](https://discourse.slicer.org/t/extract-centerline-python-scripting/21275/1 "2021-12-30T12:28:46Z")

</div>

Hi,

I am trying to automatize the process of extracting a centerline.  
I want an automatic configuration that looks like:

 ![imagen](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/e/8ea204f3dd0b9a634922bf229e5a3442ae10e0e9.png)

I have some lines of code based on:  
import ExtractCenterline

slicer.util.selectModule(‘ExtractCenterline’)  
ec = ExtractCenterline.ExtractCenterlineLogic()  
s = segmentationNode.GetSegmentation()  
ss = s.GetSegment(s.GetSegmentIdBySegmentName(“aortaCompleta”)).GetRepresentation(‘Closed surface’)  
fids = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLMarkupsFiducialNode”)  
centerlinePolyData,voronoiDiagramPolyData = ec.extractCenterline(ss, fids)  
But I can’t find the centerline after this

I also tryied to modify the ParameterNode  
ec.getParameterNode().SetParameter(“InputSurface”, segmentID)  
but I don’t know how to see changes on GUI and modify the “Tree” section.

Thanks 🙂

---

<div class="post-metadata">

### Author: ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)
#### Post date: [December 30, 2021, 2:48pm UTC](https://discourse.slicer.org/t/extract-centerline-python-scripting/21275/2 "2021-12-30T14:48:45Z")

</div>

You seem to mix up things here.

\1. If you wish to work with the logic class of ‘ExtractCenterline’ module, [this](https://discourse.slicer.org/t/automatic-centerline/14829/22) post is very instructive. The logic class won’t modify the UI in itself.

\2. If you wish to work with the ‘ExtractCenterline’ module’s GUI, you can access all UI widgets this way :

```auto
ecWidgetRepresentation = slicer.modules.extractcenterline.widgetRepresentation()
ecUI = ecWidgetRepresentation.ui

```

For example, to get a handle to the output curve combobox, you would use _ecUI.outputCenterlineCurveSelector_.

---

<div class="post-metadata">

### Author: ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)
#### Post date: [December 31, 2021, 8:52am UTC](https://discourse.slicer.org/t/extract-centerline-python-scripting/21275/3 "2021-12-31T08:52:21Z")

</div>

Hi,

This gives me an error

```auto
ecWidgetRepresentation = slicer.modules.extractcenterline.widgetRepresentation()
ecUI = ecWidgetRepresentation.ui

```

![imagen](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/2/023e214afc48cb46397f699437162095271b896c.png)

But I tryied with this and seems to work fine

```auto
ecUI = slicer.modules.ExtractCenterlineWidget.ui

```

However I don’t know how to change options. Could you give me an example of how to change (for instance) the input surface? I am quite noob with slicer.

Thanks

---

<div class="post-metadata">

### Author: ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)
#### Post date: [December 31, 2021, 11:16am UTC](https://discourse.slicer.org/t/extract-centerline-python-scripting/21275/4 "2021-12-31T11:16:58Z")

</div>

> [@bserrano](#):
>
> This gives me an error
> 
> ```auto
> ecWidgetRepresentation = slicer.modules.extractcenterline.widgetRepresentation()
> ecUI = ecWidgetRepresentation.ui
> 
> ```

You’re right, it should be :  
`ecUI = ecWidgetRepresentation.self().ui`

As for _slicer.modules.ExtractCenterlineWidget.ui_, _slicer.modules.ExtractCenterlineWidget_ is available only if you have shown the ‘ExtractCenterline’ module once, or if you have run _slicer.modules.extractcenterline.widgetRepresentation()_ once. That is, the widget representation needs to be created.

> [@bserrano](#):
>
> Could you give me an example of how to change (for instance) the input surface?

```auto
segmentation = slicer.util.getNode("Segmentation")
ecUI.inputSurfaceSelector.setCurrentNode(segmentation)

```

In a real application, you won’t use getNode() of course. Either you create a node, or you get it from a UI widget following its API.
