# How to copy zbuffer after window interaction

**URL:** https://discourse.vtk.org/t/how-to-copy-zbuffer-after-window-interaction/9289
**Category:** Support
**Tags:** python
**Created:** [September 7, 2022, 8:43am UTC](https://discourse.vtk.org/t/how-to-copy-zbuffer-after-window-interaction/9289 "2022-09-07T08:43:08Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![llobera](https://discourse.vtk.org/user_avatar/discourse.vtk.org/llobera/32/5540_2.png) [@llobera](https://discourse.vtk.org/u/llobera)
#### Post date: [September 7, 2022, 8:43am UTC](https://discourse.vtk.org/t/how-to-copy-zbuffer-after-window-interaction/9289/1 "2022-09-07T08:43:08Z")

</div>

I am trying to figure out how to obtain the zbuffer for a view after I have interacted with it. I am just starting with VTK. Here is the code that I have so far,

> ```
> # Create a render window, and interactor
> renderWindow = vtkRenderWindow()
> renderWindow.SetSize(800, 600)
> 
> # Create renderer + add to renderwindow
> renderer = vtkRenderer()
> renderWindow.AddRenderer(renderer)
> 
> def key_press(obj,key):
> if obj.GetKeySym() == "u":
> # get depth buffer
> z_buffer_data = vtkFloatArray()
> rmax, cmax = renderWindow.GetActualSize()
> 
> # 0,0 starts at lower left
> renderWindow.GetZbufferData(0, 0, cmax-1, rmax-1, z_buffer_data)
> 
> # convert to numpy
> z_buffer_data_numpy = numpy_support.vtk_to_numpy(z_buffer_data)
> z_buffer_data_numpy = np.reshape(z_buffer_data_numpy, (-1, cmax))
> 
> return z_buffer_data_numpy
>         
> # Create interactor
> renderWindowInteractor = vtkRenderWindowInteractor()
> renderWindowInteractor.SetInteractorStyle(vtkInteractorStyleTerrain())
> renderWindowInteractor.SetRenderWindow(renderWindow)
> renderWindowInteractor.AddObserver("KeyPressEvent", key_press)
> 
> ```

How can I get ‘z\_buffer\_data\_numpy’ and proceed with other calculations?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [September 7, 2022, 3:22pm UTC](https://discourse.vtk.org/t/how-to-copy-zbuffer-after-window-interaction/9289/2 "2022-09-07T15:22:11Z")

</div>

Hi,

Well, off the top of my head, you can either set a gobal variable or subclass `vtk.vtkInteractorStyleTrackballCamera`: [How to get the key code in a vtk KeyPressEvent using python - Stack Overflow](https://stackoverflow.com/questions/32636503/how-to-get-the-key-code-in-a-vtk-keypressevent-using-python) for a better designed way to get in-event data.

regards,

Paulo

---

<div class="post-metadata">

### Author: ![llobera](https://discourse.vtk.org/user_avatar/discourse.vtk.org/llobera/32/5540_2.png) [@llobera](https://discourse.vtk.org/u/llobera)
#### Post date: [September 8, 2022, 11:38am UTC](https://discourse.vtk.org/t/how-to-copy-zbuffer-after-window-interaction/9289/3 "2022-09-08T11:38:55Z")

</div>

Thanks Paulo,

Unfortunately this example (and others I have found) only show how you can print the key being pressed. I want to be able to dump the zbuffer into a numpy array but I do not know how to pass the actual array (I am able to print it and that looks fine). I just would like for the values to be returned.

M.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [September 8, 2022, 1:08pm UTC](https://discourse.vtk.org/t/how-to-copy-zbuffer-after-window-interaction/9289/4 "2022-09-08T13:08:35Z")

</div>

No, look at the example again, you can design the subclass to add any additional members you like. You can use them to fetch any information you need after the event is processed.

---

<div class="post-metadata">

### Author: ![llobera](https://discourse.vtk.org/user_avatar/discourse.vtk.org/llobera/32/5540_2.png) [@llobera](https://discourse.vtk.org/u/llobera)
#### Post date: [September 8, 2022, 2:25pm UTC](https://discourse.vtk.org/t/how-to-copy-zbuffer-after-window-interaction/9289/5 "2022-09-08T14:25:40Z")

</div>

The only way that I managed to get the z-buffer was by defining the variable within the _keypressed_ function to be global (I think you had mentioned this as a possibility earlier). But could not find other clues otherwise.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [September 8, 2022, 4:13pm UTC](https://discourse.vtk.org/t/how-to-copy-zbuffer-after-window-interaction/9289/6 "2022-09-08T16:13:13Z")

</div>

Global variables are not regarded as a good practice from the design standpoint. Use it sparingly and document it plenty. Again, I guess you know how to code classes in Python, so, you I can tell you a global variable is not the only way.

---

<div class="post-metadata">

### Author: ![llobera](https://discourse.vtk.org/user_avatar/discourse.vtk.org/llobera/32/5540_2.png) [@llobera](https://discourse.vtk.org/u/llobera)
#### Post date: [September 8, 2022, 5:00pm UTC](https://discourse.vtk.org/t/how-to-copy-zbuffer-after-window-interaction/9289/7 "2022-09-08T17:00:26Z")

</div>

Yes. In fact I have never used global variables. But, right now, I cannot say that I see how to return the zbuffer without recourse to using it??? I altered the code you pointed towards but that is modifying the interactor style class which is then ‘associated’ with a _renderWindowInteractor_. So I am lost as to how to access this through the interactor style class. I just starting to get accustomed to VTK…

M.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [September 8, 2022, 9:58pm UTC](https://discourse.vtk.org/t/how-to-copy-zbuffer-after-window-interaction/9289/8 "2022-09-08T21:58:00Z")

</div>

Hi,

Sorry if I sound nitpicking, but we don’t say the custom class is _associated with_ but _extends_ (the current OOP jargon) `vtkInteractorStyleTrackballCamera`. To extend a class is to create a new class that has everything `vtkInteractorStyleTrackballCamera` has plus new features you want to add:

```python
class MyInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):

    def __init__ (self,parent=None):
        self.parent = iren
        self.AddObserver("KeyPressEvent",self.keyPressEvent)
        self.pressed_key = '\0' #<--- this is a new member variable.

    def keyPressEvent(self,obj,event):
        key = self.parent.GetKeySym()
        if key == 'l':
            print(key)
        #save the pressed key to the member variable so we can access it later.
        self.pressed_key = key
        return

(...)

iren = vtk.vtkRenderWindowInteractor()

my_interactor = MyInteractorStyle()

iren.SetInteractorStyle( my_interactor )

iren.SetRenderWindow(renWin)
renWin.Render()

iren.Initialize()
iren.Start()

#print the key that was pressed last.
print( "The last key pressed is ", my_interactor.key_pressed ) 

```

I hope this helps,

Paulo

---

<div class="post-metadata">

### Author: ![llobera](https://discourse.vtk.org/user_avatar/discourse.vtk.org/llobera/32/5540_2.png) [@llobera](https://discourse.vtk.org/u/llobera)
#### Post date: [September 9, 2022, 8:18am UTC](https://discourse.vtk.org/t/how-to-copy-zbuffer-after-window-interaction/9289/9 "2022-09-09T08:18:45Z")

</div>

Thank you Paulo. That worked great!!
