How top crop the volume using python?

I had selected the ROI of a volume.And want to crop it in python.
How can I do this?

Assuming roi and inputVolume are defined :

cropLogic = slicer.modules.cropvolume.logic()
cvpn = slicer.vtkMRMLCropVolumeParametersNode()

cvpn.SetROINodeID(roi.GetID())
cvpn.SetInputVolumeNodeID(inputVolume.GetID())
cropLogic.Apply(cvpn)
1 Like