# Batch segment creation - macro

**URL:** <https://discourse.slicer.org/t/batch-segment-creation-macro/11123>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [April 15, 2020, 11:15am UTC](https://discourse.slicer.org/t/batch-segment-creation-macro/11123 "2020-04-15T11:15:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ezio\_Lanza](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ezio_lanza/32/6285_2.png) [@Ezio\_Lanza](https://discourse.slicer.org/u/Ezio_Lanza)\
**Post date:** [April 15, 2020, 11:15am UTC](https://discourse.slicer.org/t/batch-segment-creation-macro/11123/1 "2020-04-15T11:15:55Z")

</div>

Dear all,

Is there a way to instruct segment editor to do the following operations with one click ? Let me offer an example:

I have the segment LUNG which comprises the whole lung volume.  
I would like with one click to order Slicer do the following operations:

a) Create a new segment “Newsegment”  
b) Use the Threshold tool to populate a new segment with the intervals -1000HU-100HU inside LUNG without overwriting other segments.

Is it there an easy way to do it?

Thanks a lot for your help.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [April 15, 2020, 12:29pm UTC](https://discourse.slicer.org/t/batch-segment-creation-macro/11123/2 "2020-04-15T12:29:58Z")

</div>

It should be pretty easy to do with some python scripting. See lots of examples here:  
[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How\_to\_run\_segment\_editor\_effects\_from\_a\_script](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_run_segment_editor_effects_from_a_script)

Once you have a script, if you want it to have its own effect button, then it’s easy to add too.

---

<div class="post-metadata">

**Author:** ![Ezio\_Lanza](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ezio_lanza/32/6285_2.png) [@Ezio\_Lanza](https://discourse.slicer.org/u/Ezio_Lanza)\
**Post date:** [April 22, 2020, 2:27pm UTC](https://discourse.slicer.org/t/batch-segment-creation-macro/11123/3 "2020-04-22T14:27:16Z")

</div>

Hello, thanks for helping and sorry for bothering. I’m really new to python and only have experience on HTML coding. But I’m willing to learn.  
I got to the point where I created a test module using the extension manager.  
I found a useful example here [https://gist.github.com/lassoan/5ad51c89521d3cd9c5faf65767506b37](https://gist.github.com/lassoan/5ad51c89521d3cd9c5faf65767506b37)

But I cannot get past the way to use the current volume that I have selected in the drop down menu.  
How do I access it in the script?

I need to replace the following line

masterVolumeNode = SampleData.SampleDataLogic().downloadCTChest()

with something that fills the variable masterVolumeNode with the volume currently selected in the InputVolume dropdown menu.  
Couldn’t find easy answer until now

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 22, 2020, 2:41pm UTC](https://discourse.slicer.org/t/batch-segment-creation-macro/11123/4 "2020-04-22T14:41:51Z")

</div>

These are explained in detail, with examples in Slicer programming tutorials, such as [this](https://www.slicer.org/wiki/Documentation/Nightly/Training#PerkLab.27s_Slicer_bootcamp_training_materials).

---

<div class="post-metadata">

**Author:** ![Ezio\_Lanza](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ezio_lanza/32/6285_2.png) [@Ezio\_Lanza](https://discourse.slicer.org/u/Ezio_Lanza)\
**Post date:** [April 22, 2020, 6:31pm UTC](https://discourse.slicer.org/t/batch-segment-creation-macro/11123/5 "2020-04-22T18:31:23Z")

</div>

Thanks Andras, your work is amazing.  
Just wanted to let you know that thanks to your indications I am getting super-close to creating what I need.
