# How to write a recursive script to crop volumes with ROI?

**URL:** <https://discourse.slicer.org/t/how-to-write-a-recursive-script-to-crop-volumes-with-roi/4998>\
**Category:** Support\
**Created:** [December 6, 2018, 6:19pm UTC](https://discourse.slicer.org/t/how-to-write-a-recursive-script-to-crop-volumes-with-roi/4998 "2018-12-06T18:19:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenaflex](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@jenaflex](https://discourse.slicer.org/u/jenaflex)\
**Post date:** [December 6, 2018, 6:19pm UTC](https://discourse.slicer.org/t/how-to-write-a-recursive-script-to-crop-volumes-with-roi/4998/1 "2018-12-06T18:19:00Z")

</div>

1. How to write recursive script to run ‘crop volume’ (like attached screenshot) on all 3D volumes (3D volumes extracted from 4D nifti)? I have ‘UtBox.acsv’ ROI file already.

2. (Am I supposed to, OR, ) how to make function call ‘slicer.modules.cropvolume’ in built-in Python console to crop 3D volumes with ROI (instead of using GUI).  
The documentation seems to be very limited.  

Thanks!

---

<div class="post-metadata">

**Author:** ![jenaflex](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@jenaflex](https://discourse.slicer.org/u/jenaflex)\
**Post date:** [February 8, 2019, 12:31am UTC](https://discourse.slicer.org/t/how-to-write-a-recursive-script-to-crop-volumes-with-roi/4998/2 "2019-02-08T00:31:29Z")

</div>

Up. Please help. Thanks!

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [February 8, 2019, 1:52pm UTC](https://discourse.slicer.org/t/how-to-write-a-recursive-script-to-crop-volumes-with-roi/4998/3 "2019-02-08T13:52:56Z")

</div>

You could start by looking at the [SlicerCaseIterator](https://github.com/JoostJM/SlicerCaseIterator). Once you can iterate through your volumes you could add some code to do the cropping. I agree it’s not well documented but the building blocks are there and you can see [example scripts](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository).

---

<div class="post-metadata">

**Author:** ![jenaflex](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@jenaflex](https://discourse.slicer.org/u/jenaflex)\
**Post date:** [February 8, 2019, 4:34pm UTC](https://discourse.slicer.org/t/how-to-write-a-recursive-script-to-crop-volumes-with-roi/4998/4 "2019-02-08T16:34:28Z")

</div>

Thanks!  
I think I should be able to figure out iteration method.  
My main concern is how to call cropvolume function with input ROI being UtBox ROI file (like the screenshot).

---

<div class="post-metadata">

**Author:** ![simonoxen](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/simonoxen/32/66398_2.png) [@simonoxen](https://discourse.slicer.org/u/simonoxen)\
**Post date:** [February 8, 2019, 5:54pm UTC](https://discourse.slicer.org/t/how-to-write-a-recursive-script-to-crop-volumes-with-roi/4998/5 "2019-02-08T17:54:28Z")

</div>

Hi, this is a part of a script I used to call cropvolume from a python script:

```
parameters = slicer.vtkMRMLCropVolumeParametersNode()
slicer.mrmlScene.AddNode(parameters)

parameters.SetInputVolumeNodeID(input.GetID())
parameters.SetOutputVolumeNodeID(output.GetID())
parameters.SetROINodeID(roiNode.GetID())
slicer.modules.cropvolume.logic().Apply(parameters)
```

---

<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 17, 2020, 11:26pm UTC](https://discourse.slicer.org/t/how-to-write-a-recursive-script-to-crop-volumes-with-roi/4998/6 "2020-04-17T23:26:33Z")

</div>

A post was merged into an existing topic: [Volume Cropping from Scripted Module](https://discourse.slicer.org/t/volume-cropping-from-scripted-module/11169/2)
