I am trying to interface Arduino to Blender BGE and vice versa.
Before jumping to BGE Python script, I am testing it with Blender Python.
This is what happens so far:
I have Arduino program already running in the background, the Potentiometer is sending message to Serial.
I am using PySerial Library.
Now, I thought that what I need to do is to read the data inside Serial:
import serial
import bpy
arduino = serial.Serial('COM3', 9600)
x = 0
# THIS SCRIPT WILL GENERATE MAXIMUM RECURSION DEPTH ERROR
#define action
def Action(Scene):
potValue = arduino.readline()
bpy.data.objects['Cube'].scale[0] = 0.1 * int(potValue)
bpy.data.objects['Cube'].scale[1] = 0.1 * int(potValue)
bpy.data.objects['Cube'].scale[2] = 0.1 * int(potValue)
bpy.data.scenes[0].update()
#register action
bpy.app.handlers.scene_update_pre.append(Action)
"""
while(x < 1000):
print(x)
# read arduino Series value
print( arduino.readline() )
x += 1
"""
However doing it like above, and I am getting “Maximum Recursion Depth Error”. Perhaps because I assigned the scene_update_pre.append() the wrong way?
And I cannot stop the script from running. This is confusing.
On the other hand for BGE, also I found issue. If I use while(True), the PySeries seems to read the Potentiometer correctly, however, BGE came to halt (while still reading Series value) and I need to restart Blender.
BGE Code:
from bge import logic
from bge import events
import bge
import math
import serial
#arduino = 0
# Get the cube controller
cont = logic.getCurrentController()
own = cont.owner
if not 'init' in own:
own['init'] = 1
print ("initializing ....")
#arduino = serial.Serial('COM3', 9600, timeout=1)
"""
# move cube using sine movement in Z
own.position.x += 0.1
own.position.z = own.position.z + math.sin(own.position.x)
"""
arduino = serial.Serial('COM3', 9600, timeout=1)
"""
if (arduino.isOpen()):
x = arduino.readline()
#own.position.z = x
print(x)
print("reading")
arduino = serial.Serial('COM3', 9600, timeout=1)
"""
arduino.write("it is working")
Any idea to fix this or maybe any thread pointing me in the right direction?
I thought using Serial direct to Arduino will be simple, but not the case.
I am also thinking OSC will be what I need. But I like to see if I could:
- Control Arduino using BGE
- Control Blender using Arduino (not so real time is okey)
- Control BGE using Arduino (real time)