Integration of Blender with external Python Application

Hello all,

I have been studying a Machine Learning course that uses Python 3.5
In that course I learnt how to process images including images captured by a webcam. So my code in Python tracks my eyes via the webcam and adds 1 to a counter every time I blink. Now I want to pass that information to a blend file running.
It has a low poly humanoid face as my Avatar. This Avatar has shape_keys that make him blink.
I have this signal from my python Machine Learning code say I as a person have blinked. How can we pass that signal to my Avatar and make him blink (i.e. manipulate the shape_keys). This completes an almost closed loop.

This may be a critical requirement for our team that is building a wheelchair. The patient can talk to the wheel chair only via blinks. The wheelchair’s on-board computer blinks back to say I understood what you want.

I am a complete Blender-Nancy. I depend on you to guide me to make my avatar in blender blink. Please help with a step-by-step set of instructions.

Thank you

Blender python even though its embeded inside blender it offers full support for cpython. If you have code that must run as standalone then you will need to use IPC (Inter Process Communication) , IPC is short for “the way applications talk to each other” and there are many forms. I have played with two a) sockets and b) memory mapped files. Others are Pipes , DBUS, json etc.

Sockets are what you use when you connect to internet on any network, it can connect anything at any distance. But they are not superfast so not good for super fast code. Expect 1 ms respond time locally and a bit more remotely.

Memory mapped files is what your OS uses to load DLLs , they are extremely efficient because its a low level fuctionality of the OS that resides inside the OS kernel but they are strictly local meaning code that resides in the same memory. They have the advantage of sharing memory show you do not need to copy data around like sockets and they are extremely fast.

If your code can run as a python library then its just a matter of making an addon and doing a import my_lib

I wont provide you with a step by step process because there are literally billions of ways for doing this , this very basic functionality. Application and code that talks to each other or even different languages its not exception as you may thing its actually the rule . Even blender is a multi language project.

Its up to you to modify your code to use these technologies as there is no golden rule. You will also find a ton of examples online. In the end Blender python is just plain python so do not expect anything really different.

sockets had worked for me… At least it can be a fast way for prototyping… If like me the memory mapped stuff scares you…
at the end you’ll find a quick client/server setup, use (localhost) as an IP adress so you can run it in the same computer…

Surprisingly enough memory mapping at C level is rather simple. Its just about creating a pointer, opening a file , calling a function to create the shared memory and passing the file and the pointer as arguments to it. If you understand C its a piece of cake.

Python makes it even simpler supporting memory mapped files out of the box.

Just take a look here