I want to connect an Arduino (eg: Uno) to Blender and animate blender objects based on arduino inputs.
1 Answers
The way I did it was by writing serial information to the usb port from the arduino and reading it from inside blender with pyserial.
I did run this in the Blender's game engine, which may or may not be what you want. The Blender side looks something like this (code is not very clean, I just hacked this together quickly for fun, but hopefully to gives you some idea of how you could implement it):
from bge import logic
import serial
from mathutils import *
from math import *
def startup():
ser = serial.Serial('/dev/ttyUSB0')
scene = logic.getCurrentScene()
object = scene.objects['Suzanne']
print('startup')
logic.globalDict['serial_bus'] = ser
logic.globalDict['main_object'] = object
logic.globalDict['light'] = scene.lights['Lamp']
def loop():
ser = logic.globalDict['serial_bus']
object = logic.globalDict['main_object']
light = logic.globalDict['light']
# this is where the data is actually read from the serial interface
data = ser.readline().decode()
pot, var, phot = data.partition(";")
print("pot:", pot, "phot:", phot)
object.localOrientation = Euler((-.31,0,radians(float(pot)))).to_matrix()
light.energy = 10 - float(phot) * (10.0/800.0)
The Arduino side is just Serial.printing numbers separated by semicolons.
How exactly you want to animate objects in blender depends on your usecase. If it's for a real time demo, you probably want to use the BGE (or use another game engine entirely, such as godot). If you want to use it as a secondary input device in Blender's viewport (e.g. a sort of DIY 3D mouse), you'll probably want to look at writing an addon.
- 157,169
- 58
- 601
- 1,133
-
@Saku I've moved our conversation to chat to avoid cluttering up this page too much. Let me know if you have any issues getting access, I think you should be good. – gandalf3 Jun 26 '18 at 02:34