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):
- 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.
- 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.