Seeking advice for my bachelor's thesis.. Is it possible to have Blender periodically call my python code?

Hello, everyone!

I am currently writing my bachelor’s thesis in information technology, and I really need some help from someone familiar with Blender’s internals. Edit: I would like to apologise if this kind of content is not welcome here, but this seemed like the best possible place I could ask.

Edit2: Thank’s to Strike_Digital’s answer I think I now have a solution, or at least a very good starting point to solving my problem. Thanks, Blender community, very cool. Makes me want to contribute at some point:)

The Problem

I’ll try to give the shortest possible explanation without going into why I need this too much. (I will explain what I’m building in the next section)

I need code (Python or C++, not crucial) to run in parallel with Blender and have access to the scene data.
Thread safety would almost certainly be an issue, so a better alternative would maybe be if I was somehow able to get Blender to call my code periodically at a relatively stable interval (like a game engine OnUpdate function). Basically the only requirement for this code is to be able to access the scene data either at any time, or at short intervals. Well, and not be sandboxed I guess :).

I doubt this is possible without modifying Blender itself (if I’m wrong, please tell me!), but if possible I would like to avoid diving too deep into the source code as I am quite time constrained :sweat_smile: . If this is indeed impossible with blender’s current code, I’d be very thankful if someone pointed me at some internal update loop where I could insert a callback to my code if I were to modify blender. I’m not however sure how easy it would be to access scene data with C++ code, so perhaps a better solution would be for me to implement some functionality to register a python update callback with Blender. But I wouldn’t even know where to start…

Why I need this

Basically, what I’m implementing as part of my thesis is two plugins - a blender plugin (which might end up being a source code modification, not a plugin) and a VST plugin. They are supposed to work together, to provide integration between Blender and the DAW (Difgital Audio Workstation) for spatial audio sound design and mixing. So the VST plugin can be placed on each track in the DAW that corresponds to the sound of some 3D object in the scene, and then you pick an object from the Blender scene the position of which will define where the sound will be placed in space. The VST plugin will output Ambisonics - a popular spatial audio representation. Oh, and I want to do this in real time…
So in the end, what I need to be able to get from Blender is:

  • a list of objects in the current scene (this I don’t need to update constantly of course)
  • the current position of these objects - this I need to be able to update periodically. I could of course get the positional data for objects once for the whole animation duration, when the user presses some button in the DAW plugin, but that’s not as cool :))
  • in an ideal world I would also synchronize play/pause and time between Blender and the DAW, but I’m not sure how I would architect that.

Initially I thought this was going to be an easier task (famoust last words), but I kinda forgot to check if the capabilities of the Blender Python API match my requirements…

2 Likes

What you’re suggesting should be pretty easy to do. You could call a function every n seconds using a timer. Or you could call a function every time the scene is updates by using a handler.

You could probably synchronize play/pause as well, but that depends a bit more on which software you’re controlling it from (but for example, it’s fairly easy to get/set whether the timeline is playing or not)

In general, you’ll probably want a bit of knowledge about how the API works first though before trying to implement these things, as without that I can imagine it may turn into quite a painful process.

Hope that helps!

2 Likes

Thank you very much for the reply! I don’t have the time to look into it in depth right now, but I think this is exactly what I was looking for. I suppose the update frequency will be quite unpredictable, but I can get around that by updating some scene state buffer owned by the central process from which all the vst instances will get their data.
I guess I should have studied the python API more before concluding it wasn’t capable of what I needed…
And you are absolutely correct that I should learn the API more thoroughly before beginning to implement this. I’m familiar with it to some extent, and I have programmed a couple operators before, but somehow I missed the application handler part of it.
Thanks again!

1 Like