# What is the best way to send a single and/or a stream of vtkPolyData over a network?

**URL:** https://discourse.vtk.org/t/what-is-the-best-way-to-send-a-single-and-or-a-stream-of-vtkpolydata-over-a-network/8928
**Category:** Web
**Created:** [July 11, 2022, 3:49pm UTC](https://discourse.vtk.org/t/what-is-the-best-way-to-send-a-single-and-or-a-stream-of-vtkpolydata-over-a-network/8928 "2022-07-11T15:49:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![joaomlap](https://discourse.vtk.org/user_avatar/discourse.vtk.org/joaomlap/32/3201_2.png) [@joaomlap](https://discourse.vtk.org/u/joaomlap)
#### Post date: [July 11, 2022, 3:49pm UTC](https://discourse.vtk.org/t/what-is-the-best-way-to-send-a-single-and-or-a-stream-of-vtkpolydata-over-a-network/8928/1 "2022-07-11T15:49:37Z")

</div>

Hi,

I was researching the best way to send a vtkPolyData object over a network and came across this GitHub issue:

> <https://github.com/Kitware/vtk-js/issues/928>
>
> Hello,
> would it be possible to create a JSON containing a Polydata on backend s…erver (running vtk through a Python script) and send it to vtk-js directly (using a json.dumps on server side) ?
> 
> When trying ot use it, I get this response : Object of type 'vtkPolyData' is not JSON serializable
> 
> Thanks a lot

Where @Sebastien_Jourdain states there is a way of converting it to a `vtp` format and the use XMLPolyDataReader to read it in vtk.js. I could not find an example of converting vtkPolyData to vtp without writing the result to a file using vtkXMLPolyDataWriter.

Is there an example I can see or a project where it’s being done, I would be very grateful to see how I could achieve this.

Thank you very much.

---

<div class="post-metadata">

### Author: ![finetjul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/finetjul/32/154_2.png) [@finetjul](https://discourse.vtk.org/u/finetjul)
#### Post date: [July 12, 2022, 6:41am UTC](https://discourse.vtk.org/t/what-is-the-best-way-to-send-a-single-and-or-a-stream-of-vtkpolydata-over-a-network/8928/2 "2022-07-12T06:41:33Z")

</div>

Have you considered wslink (low level) or trame (high level) to do vtk networking?

They rely on the [render\_window\_serializer](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Web/Python/vtkmodules/web/render_window_serializer.py) python module.

Alternatively you can consider [export\_scene\_macro](https://github.com/Kitware/vtk-js/blob/master/Utilities/ParaView/export-scene-macro.py) (recent [PR](https://github.com/Kitware/vtk-js/pull/2503)).

---

<div class="post-metadata">

### Author: ![joaomlap](https://discourse.vtk.org/user_avatar/discourse.vtk.org/joaomlap/32/3201_2.png) [@joaomlap](https://discourse.vtk.org/u/joaomlap)
#### Post date: [July 12, 2022, 7:56am UTC](https://discourse.vtk.org/t/what-is-the-best-way-to-send-a-single-and-or-a-stream-of-vtkpolydata-over-a-network/8928/3 "2022-07-12T07:56:02Z")

</div>

Hi @finetjul 🙂

Yes, I’ve used wslink to create a stream of images to the frontend.

I am exploring something slightly different as I don’t want to render on the backend (because the vtk objects are small-ish), I am more interested in rendering on the frontend, hence the serialisation of a vtkPolyData object.

My idea is to request the data from the frontend, do some processing, then create a cache on the backend (again the need for serialising).

I think I’ve achieved half of it even though I am not sure if this is the best way.

Backend (VTK Python wrapper):

```auto
writer = vtkXMLPolyDataWriter()
writer.SetInputData(data)
writer.WriteToOutputStringOn()
writer.Update()
s = writer.GetOutputString()

```

Frontend (vtk.js):

```auto
const reader = vtkXMLPolyDataReader.newInstance();
const textEncoder = new TextEncoder();
reader.parseAsArrayBuffer(textEncoder.encode(data));
const { mapper } = context.current; // context contains the actor/mapper, etc
mapper.setInputConnection(reader.getOutputPort());

```

At the moment I am not able to see anything yet (which might be related to my incorrect camera settings).

But I would appreciate if you could tell if this is a correct solution for my problem stated above. Different approaches are welcome.

Thank you for your help.

---

<div class="post-metadata">

### Author: ![finetjul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/finetjul/32/154_2.png) [@finetjul](https://discourse.vtk.org/u/finetjul)
#### Post date: [July 12, 2022, 9:35am UTC](https://discourse.vtk.org/t/what-is-the-best-way-to-send-a-single-and-or-a-stream-of-vtkpolydata-over-a-network/8928/4 "2022-07-12T09:35:20Z")

</div>

Then you should consider the [render\_window\_serializer](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Web/Python/vtkmodules/web/render_window_serializer.py) script along with [SynchronizableRenderWindow](https://github.com/Kitware/vtk-js/blob/master/Sources/Rendering/Misc/SynchronizableRenderWindow/index.js)

---

<div class="post-metadata">

### Author: ![joaomlap](https://discourse.vtk.org/user_avatar/discourse.vtk.org/joaomlap/32/3201_2.png) [@joaomlap](https://discourse.vtk.org/u/joaomlap)
#### Post date: [July 15, 2022, 3:38pm UTC](https://discourse.vtk.org/t/what-is-the-best-way-to-send-a-single-and-or-a-stream-of-vtkpolydata-over-a-network/8928/5 "2022-07-15T15:38:52Z")

</div>

Thanks Julien, sorry for the delayed reply.

Is there an example on how to use them together?  
Do I need to use wslink if I use them?

---

<div class="post-metadata">

### Author: ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)
#### Post date: [July 18, 2022, 7:44pm UTC](https://discourse.vtk.org/t/what-is-the-best-way-to-send-a-single-and-or-a-stream-of-vtkpolydata-over-a-network/8928/6 "2022-07-18T19:44:49Z")

</div>

You can find a [trame example](https://github.com/Kitware/trame/blob/master/examples/06_vtk/01_SimpleCone/LocalRendering.py) pushing the geometry that is visible in a vtkRenderWindow over the network without the need of rendering capability on the server side.

There is also a [second example](https://github.com/Kitware/trame/blob/master/examples/06_vtk/01_SimpleCone/ClientView.py) dealing with the conversion more in a manual fashion.

---

<div class="post-metadata">

### Author: ![joaomlap](https://discourse.vtk.org/user_avatar/discourse.vtk.org/joaomlap/32/3201_2.png) [@joaomlap](https://discourse.vtk.org/u/joaomlap)
#### Post date: [July 20, 2022, 8:12am UTC](https://discourse.vtk.org/t/what-is-the-best-way-to-send-a-single-and-or-a-stream-of-vtkpolydata-over-a-network/8928/7 "2022-07-20T08:12:43Z")

</div>

Thank you Sebastien.  
I will refer to those two examples to continue my work.
