BlendZMQ (open source add-on): Streaming data into Blender 2.8x without Freezing the interface

I hope I can help some coders here with a demo add-on that allows for streaming data into (and out of) Blender ^-^
The other program doesn’t have to be executed on the same machine, or even be written in the same language!

Normally, executing heavy code code would make the rest of Blender slow / unresponsive. This add-on uses bpy.app.timers and ZeroMQ to transmit data between Blender and an outside program (possibly executed on a different machine). It keeps Blender completely responsive.

Demo YouTube video: https://www.youtube.com/watch?v=68zSpWZirtI
BlendZMQ demo]

All code (fully commented) and instructions can be found here: https://github.com/NumesSanguis/Blender-ZMQ-add-on
The most important bit of code (timer and ZeroMQ): https://github.com/NumesSanguis/Blender-ZMQ-add-on/blob/master/blendzmq_ops.py#L38-L136

2 Likes

I haven’t tried it yet so maybe this is stupid question, however can it also manipulate mesh data? For example to produce something like a differential line growth etc.

You should see this as a code example on how to make an add-on that accepts data from outside Blender (or sending data from Blender to another program). The code should be quite straightforward, so it should be easy to modify (if you’re familiar with Python). Anything that is possible with Python and the Blender API can be done.

Instead of having to do both the differential calculation and the mesh manipulation in Blender, you can do the calculation in another program (even non-Python), send the data to Blender, and then hook it up with code that generates the mesh. Maybe combining it with an add-on like Sorcar (Sorcar - Procedural modeling in Blender using Node Editor)?

So yes, technically it is possible, but not without coding yourself what needs to be done. This add-on itself is just a tech-demo.

Could this be used for live data-visualisation?
For example, there are websites that are streaming data in json via their API. If we can use that data in Blender, then we might get the ultimate visualisation tool.
Think about live stats about cryptocurrencies (trading), stocks, and loads of other stuff.
I am looking for this quite some time. There are some addons that do live visualisation, but that is with MIDI and Sound.

Gonna check. I ve got latences with my blender 2.8 artnet server. Sounds good to me.

Thank you. You actually gave me the exact answer I needed. Thanks for this, I am gonna dig through it soon. This could streamline the workflow at least a bit when you use a lot of different software.

1 Like

Could this be used for live data-visualisation?

This is 1 potential use-case I could imagine, yes ^-^

json via their API

ZeroMQ is not a HTTP protocol, therefore not directly. Your message can contain JSON however. This is just a string and I do this in my FACSvatar project (a project for streaming facial expressions into Blender, for which I’ll soon create a Blender add-on based on this code). You can see here the code I use to encode it as JSON.

For API querying you have 2 ways of dealing with this in Blender (that I see):

  1. You do the API query (can be slow due to latency in website response) in a separate thread and put the received data in aqueue. You have a timer function in the main Blender thread that checks at a fast frequency whether new data has been added to this queue, and if yes, visualize it.
  2. Do the API requesting in a script (doesn’t have to be Python) outside Blender (which is allowed to wait without affecting Blender) and when you get the data (do some processing on it), send it to Blender over ZeroMQ sockets. This is what this add-on shows how to do. From here you only have to connect the data to some Blender object / particles.

With method 1, you keep all your code in Blender. The issue with this approach is that you need to be familiar with threading and you need to learn quite a bit of the Blender internals to pull this off without freezing the interface and proper error handling. All code in the main Blender thread has the risk of slowing down the interface.

I developed method 2, because you can do all data processing code outside Blender with how you’re used to code Python. In Blender you have to deal with a game-like internal loop that constantly runs. Outside Blender your script just runs from begin to end (which also allows for asyncio if you feel fancy). You can kill this script without affecting Blender.
Another advantage is that your data processing is not tied to Blender and you can connect it with other programs. Useful if someone already wrote a script to process this JSON API data and you only have to add the ZeroMQ streaming component. This script doesn’t even have to run on the same PC as your Blender.

Thanks for the extensive reply. I will study it, and come back with more questions.
I think data visualisation in Blender will open many doors and I believe it’s really worth it for (addon) developer to dive in it.
Until now we have things like game developers, modellers, still renders, animations and that is for a few markets. Data visualisation is for a whole other market. Just an examples:

  • In a call center, employer wants to monitor their employees performance. Existing data visualisation (excel-like) is quite poor and doesn’t give a good impression of what is going on.

  • Trading. Like tradeview, but better.

  • Weather

  • Google Analytics, Youtube analytics,

  • Social Media: buffer, statsocial, instagram, blogger, medium, twitter,

Thanks for developing this. Definitely, a keystone addon.

1 Like

To give a working example of a use-case, I slightly modified this add-on for my other project, FACSvatar. This project is about animating avatar’s facial expressions in real-time, based on recorded video’s or real-time webcam. This data is processed and can even be used in combination with a trained Deep Neural Network.

A demo of streaming facial data into Blender (models made with MB-Lab 1.7.7):

Code: https://github.com/NumesSanguis/FACSvatar-Blender
(The readme hasn’t been updated yet)

1 Like