# Loop the slice filter on the 3D dataset

**URL:** https://discourse.paraview.org/t/loop-the-slice-filter-on-the-3d-dataset/3645
**Category:** ParaView Support
**Created:** [February 24, 2020, 1:08pm UTC](https://discourse.paraview.org/t/loop-the-slice-filter-on-the-3d-dataset/3645 "2020-02-24T13:08:46Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Polly\_Hiro](https://discourse.paraview.org/user_avatar/discourse.paraview.org/polly_hiro/32/3035_2.png) [@Polly\_Hiro](https://discourse.paraview.org/u/Polly_Hiro)
#### Post date: [February 24, 2020, 1:08pm UTC](https://discourse.paraview.org/t/loop-the-slice-filter-on-the-3d-dataset/3645/1 "2020-02-24T13:08:47Z")

</div>

Hi.  
I applied slice x normal and y normal on my 3D data to get a set of points coordinates. I set the slice filter as the customize filter. I could run the data and collect the points coordinates one by one using Macros. But when I placed a number of dataset, it couldnt extract the coordinates exactly on the surface.  
It is tedious to do the process individually. Is there any better way that I can loop the process automatically on a huge dataset?

Thanks!

My code…

```
paraview.simple._DisableFirstRenderCameraReset()
reader = LegacyVTKReader(FileNames=['H.vtk'])
renderView1 = GetActiveViewOrCreate('RenderView')
readerDisplay = Show(reader, renderView1)

readerDisplay.ColorArrayName = [None, '']
renderView1.ResetCamera()
a1NTX1 = a1NTX(Input=reader)
a1NTX1Display = Show(a1NTX1, renderView1)
a1NTX1Display.ColorArrayName = [None, '']
Hide(reader, renderView1)

a1NTY1 = a1NTY(Input=a1NTX1)
a1NTY1Display = Show(a1NTY1, renderView1)
a1NTY1Display.ColorArrayName = [None, '']
Hide(a1NTX1, renderView1)
SetActiveSource(reader)

a2LEBX1 = a2LEBX(Input=reader)
a2LEBX1Display = Show(a2LEBX1, renderView1)
a2LEBX1Display.ColorArrayName = [None, '']
Hide(reader, renderView1)

renderView1.CameraPosition = [3.6, -16.08, -1141.25]
renderView1.CameraFocalPoint = [3.60, -16.08, -1519.90]
renderView1.CameraParallelScale = 98.0
```

---

<div class="post-metadata">

### Author: ![nicolas.vuaille](https://discourse.paraview.org/user_avatar/discourse.paraview.org/nicolas.vuaille/32/5873_2.png) [@nicolas.vuaille](https://discourse.paraview.org/u/nicolas.vuaille)
#### Post date: [February 24, 2020, 4:41pm UTC](https://discourse.paraview.org/t/loop-the-slice-filter-on-the-3d-dataset/3645/2 "2020-02-24T16:41:18Z")

</div>

you may add three backquotes before and after the code part, so it will be formated as code.

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [February 24, 2020, 5:42pm UTC](https://discourse.paraview.org/t/loop-the-slice-filter-on-the-3d-dataset/3645/3 "2020-02-24T17:42:45Z")

</div>

You’ll need to add some kind of loop, likely a `for` loop, the loops over all the data files you want to process. The body of the loop should open the file, runs the macros(?) `a1NTY` and `a2LEBX` like above, and end by saving the result either to a file or result array that you do something with later.

---

<div class="post-metadata">

### Author: ![Polly\_Hiro](https://discourse.paraview.org/user_avatar/discourse.paraview.org/polly_hiro/32/3035_2.png) [@Polly\_Hiro](https://discourse.paraview.org/u/Polly_Hiro)
#### Post date: [February 25, 2020, 3:22am UTC](https://discourse.paraview.org/t/loop-the-slice-filter-on-the-3d-dataset/3645/4 "2020-02-25T03:22:17Z")

</div>

Thanks for your reply.  
Do you have any links that I can refer as a tutorial? I am new in Python script in Paraview. The results I googled are mostly on Python itself but not on Paraview script. I tried on the following code but it failed.

\\\  
from paraview.simple import \*

#### disable automatic camera reset on ‘Show’

paraview.simple.\_DisableFirstRenderCameraReset()

import os  
Path = “D:\PhD\BU3DFE\_data\_vtk\AN04\_30”  
filelist = os.listdir(Path)  
for i in filelist:  
if i.endswith(".vtk"):  
with open(Path + i, ‘r’) as file:  
for line in file:

reader = line  
renderView1 = GetActiveViewOrCreate(‘RenderView’)  
readerDisplay = Show(reader, renderView1)  
readerDisplay.ColorArrayName = [None, ‘’]  
renderView1.ResetCamera()

a1NTX1 = a1NTX(Input=reader)  
a1NTX1Display = Show(a1NTX1, renderView1)  
a1NTX1Display.ColorArrayName = [None, ‘’]  
Hide(reader, renderView1)

a1NTY1 = a1NTY(Input=a1NTX1)  
a1NTY1Display = Show(a1NTY1, renderView1)  
a1NTY1Display.ColorArrayName = [None, ‘’]  
Hide(a1NTX1, renderView1)

SetActiveSource(reader)

a2LEBX1 = a2LEBX(Input=reader)  
a2LEBX1Display = Show(a2LEBX1, renderView1)  
a2LEBX1Display.ColorArrayName = [None, ‘’]  
Hide(reader, renderView1)

a2LEBY1 = a2LEBY(Input=a2LEBX1)  
a2LEBY1Display = Show(a2LEBY, renderView1)  
a2LEBY1Display.ColorArrayName = [None, ‘’]  
Hide(a2LEBX1, renderView1)

SetActiveSource(reader)

renderView1.CameraPosition = [3.6, -16.08, -1141.25]  
renderView1.CameraFocalPoint = [3.60, -16.08, -1519.90]  
renderView1.CameraParallelScale = 98.0

SaveData(‘D:/001.csv’, proxy=a1NTY1)  
SaveData(‘D:/002.csv’, proxy=a1LEBY1)  
endswith  
\\\

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [February 25, 2020, 2:43pm UTC](https://discourse.paraview.org/t/loop-the-slice-filter-on-the-3d-dataset/3645/5 "2020-02-25T14:43:22Z")

</div>

Hi. To format code use three back ticks (```) at the beginning and ending instead of backslashes. The code format is important to understanding what it does, especially in Python, so without it it is hard to give you advice about this piece of code. It is important to indent the body of the `if`, `with`, and `for` statements in Python, otherwise the code won’t work as expected. Also, if code doesn’t work, please report the error that it gives you or the incorrect behavior it produces.

If you have some programming background, a Python tutorial like [this one](https://www.w3schools.com/python/python_for_loops.asp) should give you the basics of what you need to know for effective ParaView scripting. The ParaView-specific parts are just Python classes and functions that you use - the rest is all Python.

---

<div class="post-metadata">

### Author: ![Gabriel\_Axtmann](https://discourse.paraview.org/user_avatar/discourse.paraview.org/gabriel_axtmann/32/1428_2.png) [@Gabriel\_Axtmann](https://discourse.paraview.org/u/Gabriel_Axtmann)
#### Post date: [February 28, 2020, 12:38pm UTC](https://discourse.paraview.org/t/loop-the-slice-filter-on-the-3d-dataset/3645/6 "2020-02-28T12:38:19Z")

</div>

Hi Polli, I am not sure what you really want to do but in principle you have to write a Python Macro as following and run it via pvpython. Here are some code snippets which maybe could help you.

So first, collect all your vtk data as you already did:  
`filelist = [f for f in os.listdir(path) if f.endswith('.vtk')]`

Next define a function what Paraview should do, in this example I am clippingVolumes let’s say in x-direction  
points=np.linspace(0,100,100)  
normal=np.array([1,0,0])

```
def slice(file,points,normal):   
    print('Read Elementlist ' + file)
                                           
    for i in range(0,len(points)):   
            # create a new 'Clip Closed Surface'
            clipClosedSurface1 = ClipClosedSurface(Input=file)
            clipClosedSurface1.ClippingPlane = 'Plane'
            clipClosedSurface1.ClippingPlane.Origin = [points[i],points[1],points[2]]
            clipClosedSurface1.ClippingPlane.Normal = [normal[i],normal[1],normal[2]]  
            clipClosedSurface1.InsideOut = 1 #insideOutClip....and so on

```

Last but not least, you can now loop over your filelist with a further for loop:

```
for files in filelist:
    slice(files,points,normal)

```

Run this script directly via GUI or pvpython in batch mode.
