# Correct way to update a vtkImageData and refresh the render window (renderWindow.Render() not working)

**URL:** https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359
**Category:** Support
**Created:** [August 11, 2021, 5:20pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359 "2021-08-11T17:20:06Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![bva99](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bva99/32/3745_2.png) [@bva99](https://discourse.vtk.org/u/bva99)
#### Post date: [August 11, 2021, 5:20pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/1 "2021-08-11T17:20:06Z")

</div>

I have created a class that encapsulates the creation of:

- a `vtkImageData` referred to here as “`mesh`”,
- a lookup table,
- a poly data set (from `vtkImageDataGeometryFilter`),
- a mapper (using `vtkPolyDataMapper`), that associates the lookup table to it, with appropriate ranges,
- an actor,
- a renderer,
- a render window, referred to here as “`renderWindow`”,
- and a render window interactor, referred to as “`renderWindowInteractor`”,

The constructer finishes with a call to `renderWindowInteractor.Initialize()`.

All of these objects are instance attributes of the class. I’ve skipped a few method calls to try to keep the question succint.

When instantiated, the user must input the initial scalar values of the cells using a numpy array referred to as “`values`”. An instance attribute called (`self.`) `values` is generated. A shallow copy of this numpy array is then taken using `numpy_to_vtk` (from `vtk.utils.numpy_support`) yielding a corresponding vtk array referred to as “`values_vtk`”, which is also an instance attribute of the class. The scalars of the cell data of the vtkImageData object (`self.`) `mesh` is set to this vtk array.

The real issue I am having is when updating the `vtkImageData` of the object when new cell data scalar `values` are passed.

I created a method as `def update(self, values)` that:

1. `self.values = values`
2. Re-assignes `self.values_vtk` to be equal to the vtk equivalent value of this new `self.values` array.
3. `self.mesh.GetCellData().SetScalars(self.values_vtk)` to update `mesh`.
4. `self.renderWindow.Render()` to refresh the window.

Firstly, I would like to know if steps 1 to 3 are okay, performance-wise, in order to update the cell scalar values of an ImageData. I had initially thought that just by changing `self.values_vtk` (i.e. only step 2 without step 3), the ImageData would automatically change (maybe after a call to `.Modified()`), but that was not the case in my tests.

Lastly, and most importantly, **step 4 is not working** - the window does not refresh with the changed ImageData cell scalar values. Or rather, it does so only a couple of times and then stops. When using PyCharm, if I begin the process in debug mode and set a breakpoint anywhere in the code after calling `.update()`, then the window consistently updates the visualization as desired, when the execution reaches the breakpoint. I tried using `time.pause(0.001)`, but this did not work. I also tried calling `self.renderWindowInteractor.ReInitialize()` after step 5 but this just crashes the program.

* * *

**Edit** : the written class can be found attached below.

[vtkVisualizationPackage.py](https://discourse.vtk.org/uploads/short-url/c9hKW11UIjF9JAUkQ2VjltBKPB.py) (11.9 KB)

* * *

**Edit 2** : fixed something in the attached code related to the `spacing` input.

I am starting to believe the issue has something to do with Windows. In the following MWE, the first call, of quickly changing renders works fine. But the second one stops. I am in Windows 10.

```auto
import numpy as np
from vtkVisualizationPackage import VTKImageData2DTopology
import time

def main(n_iter=100, sleep_time=0.01):
    nels = [20, 10]
    design_domain = [1., 0.5]
    types_of_els = 3
    n_tot_els = nels[0]*nels[1]
    values = np.zeros(n_tot_els, dtype=np.int64)
    values[1::3] = 1
    values[2::3] = 2

    imageObject = VTKImageData2DTopology(nels=nels, number_of_vals=types_of_els,
                                         values=values, domain_size=design_domain)
    imageObject.zoom_camera(1.66)

    for i in range(n_iter):
        if (i % 3) == 0:
            values = np.ones(n_tot_els, dtype=np.int64)*2
            values[1::3] = 0
            values[2::3] = 1
        elif (i % 3) == 1:
            values = np.ones(n_tot_els, dtype=np.int64)
            values[1::3] = 2
            values[2::3] = 0
        else:
            values = np.zeros(n_tot_els, dtype=np.int64)
            values[1::3] = 1
            values[2::3] = 2

        imageObject.update(values)
        time.sleep(sleep_time)

if __name__ == " __main__":
    main(sleep_time=0.01)
    print(f"Finished first animation")
    main(n_iter=30, sleep_time=0.3)
    print(f"Finished second animation")

```

* * *

**Edit 3** : It seems that a quick and dirty fix would be to make the window interactor `.Start()` or after updating and then somehow trigger a `Q` click event automatically. Which is not ideal… Or maybe a method that makes the window interactive for a milisecond, after calling the `.Render()` method.

* * *

**Edit 4** : Edit 3 has been accomplished using `renderWindowInteractor.ProcessEvents()`. But I hope that there is a better way to accomplish this.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 11, 2021, 7:01pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/2 "2021-08-11T19:01:40Z")

</div>

My best guess is that the timestamp of the data array is not being updated. You can update the timestamp manually before calling Render():

```python
self.values_vtk.Modified()

```

In general, the VTK rendering pipeline won’t re-upload a VTK data array to the GPU unless the timestamp of the array has changed. If you modify a VTK data set yourself, then it is your responsibility to ensure that the timestamps of all modified arrays are updated.

---

<div class="post-metadata">

### Author: ![bva99](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bva99/32/3745_2.png) [@bva99](https://discourse.vtk.org/u/bva99)
#### Post date: [August 11, 2021, 7:13pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/3 "2021-08-11T19:13:09Z")

</div>

Thank you for the suggestion! Though, I tried it out and it did not work.  
I also attempted

```auto
self.mesh.Modified()

```

but that also does not refresh the window.

Maybe I need to manually create a timestamp? Or is one automatically created at first render?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 11, 2021, 7:18pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/4 "2021-08-11T19:18:31Z")

</div>

Every object derived from vtkObjectBase has a timestamp, so timestamps do not have to be created, they only have to be updated.

It’s often the data arrays that need a Modified() call, rather than the data set, but of course it doesn’t hurt to do both.

~~Can you show the code you use to get the mesh into the renderer? What mapper are you using?~~ Ah, I see that it goes through `vtkImageDataGeometryFilter` and then `vtkPolyDataMapper`.

---

<div class="post-metadata">

### Author: ![bva99](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bva99/32/3745_2.png) [@bva99](https://discourse.vtk.org/u/bva99)
#### Post date: [August 11, 2021, 7:27pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/5 "2021-08-11T19:27:39Z")

</div>

I have added the python implementation of the class to the question.

I am using a `vtkPolyDataMapper`. It is only called once in the constructer. I’ve tried adding `.Modified()` and `.Update()` to it in my `.update(values)` method, but it doesn’t work.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 11, 2021, 7:36pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/6 "2021-08-11T19:36:28Z")

</div>

I’m not sure if `vtkImageDataGeometryFilter` is designed to work with cell data, unfortunately it’s a filter that I’m not familiar with. In general a “vtkImageData” is used to store the data values for a regular grid of points (e.g. via `GetPointData().SetScalars()`), and then other filters and mappers will interpolate to get the values beween the points. This is e.g. how contouring and volume rendering works with images in VTK.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 11, 2021, 7:44pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/7 "2021-08-11T19:44:25Z")

</div>

I looked at the `vtkImageDataGeometryFilter` and yes, it uses both the cell data and the point data of the input. So my next guess is that this filter itself isn’t modifying the timestamps for the arrays that it produces (which would be a bug, of course).

You can try debugging by getting the polydata that it produces and checking that the timestamps are modified when they should be.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 11, 2021, 7:51pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/8 "2021-08-11T19:51:12Z")

</div>

Before that, though, you can try changing the pipeline time to see if it helps:

```python
self.mesh.DataHasBeenGenerated()

```

---

<div class="post-metadata">

### Author: ![bva99](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bva99/32/3745_2.png) [@bva99](https://discourse.vtk.org/u/bva99)
#### Post date: [August 11, 2021, 7:56pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/9 "2021-08-11T19:56:32Z")

</div>

Thank you for the suggestions David!

I tried adding both of the following before `self.renderWindow.Render()` in the `.update(values)` method:

```auto
self.mesh.DataHasBeenGenerated()
self.meshPolyData.Modified()

```

But that did not work.  
I’ll try adding a “`print(timestamps)`” to check what you’ve said.

It’s difficult to debug this in debug mode in PyCharm, because when the execution is paused in a breakpoint, the window gets updated as is expected.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 11, 2021, 8:08pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/10 "2021-08-11T20:08:20Z")

</div>

Rather than using breakpoints, you can add an observer for the `EndEvent` of the `vtkImageDataGeometryFilter`. I can’t remember the exact syntax, but it’s something like this:

```auto
def callback(caller, event):
    print(caller.GetOutput().GetPointData().GetMTime())

filter.AddObserver('EndEvent', callback)

```

---

<div class="post-metadata">

### Author: ![bva99](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bva99/32/3745_2.png) [@bva99](https://discourse.vtk.org/u/bva99)
#### Post date: [August 11, 2021, 8:15pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/11 "2021-08-11T20:15:43Z")

</div>

Thank you David. I’ll try out what you’ve written.

In the meantime, I’ve added a Minimum Working Example to the question. I am starting to think the issue has something to do with Windows ignoring update calls since it “thinks” the window is unresponsive.

For quick updates the `.update()` method seems to be working. Only after the visualization window has been opened for a while, it stops refreshing and, after clicking on it, I get a “not responding” from Windows.

---

<div class="post-metadata">

### Author: ![bva99](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bva99/32/3745_2.png) [@bva99](https://discourse.vtk.org/u/bva99)
#### Post date: [August 11, 2021, 8:24pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/12 "2021-08-11T20:24:41Z")

</div>

I’ve tested the observer to track modifications to both the PointData and the CellData of the filter.

Increasing integer numbers are being printed.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 11, 2021, 8:39pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/13 "2021-08-11T20:39:35Z")

</div>

You mentioned that you are running this in PyCharm… maybe PyCharm is running Python within the PyCharm process instead of spawning a new process? This could interfere with the handling of GUI events by vtkWin32RenderWindow and vtkWin32RenderWindowInteractor. Maybe other PyCharm users (I’m not one myself) have relevant experience to share…

---

<div class="post-metadata">

### Author: ![bva99](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bva99/32/3745_2.png) [@bva99](https://discourse.vtk.org/u/bva99)
#### Post date: [August 11, 2021, 8:43pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/14 "2021-08-11T20:43:25Z")

</div>

Thank you for the suggestion!

Though I tested running the MWE directly from windows powershell and it also has the same effect. The initial seconds or so work, then the window becomes unresponsive and stops updating.

---

<div class="post-metadata">

### Author: ![bva99](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bva99/32/3745_2.png) [@bva99](https://discourse.vtk.org/u/bva99)
#### Post date: [August 11, 2021, 9:25pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/15 "2021-08-11T21:25:28Z")

</div>

I’ve found a Quick & Dirty fix.

After calling “`self.renderWindow.Render()`”, do

```auto
self.renderWindowInteractor.ProcessEvents()

```

at the end of the `.update(values)’ method.

Since no events are actually programmed, the window becomes interactive for a few miliseconds and it never freezes, consistently showing the updated visuals.

I’m not marking this as a solution, though. I need a shower after implementing this.

* * *

Edit: marked this as the solution, for now.

---

<div class="post-metadata">

### Author: ![toddy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@toddy](https://discourse.vtk.org/u/toddy)
#### Post date: [August 11, 2021, 11:40pm UTC](https://discourse.vtk.org/t/correct-way-to-update-a-vtkimagedata-and-refresh-the-render-window-renderwindow-render-not-working/6359/16 "2021-08-11T23:40:50Z")

</div>

You are most likely blocking the event loop in the GUI by calling `sleep()`. What happens in the second call to `main()` if the sleep time is again set to 0.01?

Try making the `update()` method asynchronous.
