===Controlling objects in Blender's Game Engine with Wiring===
**Introduction**
This tutorial will explain the very basics of sending data from your Wiring board to Blender's Game Engine.
It was written using an Intel Mac OS X, Version 10.5. and Blender 2.45. It should be pretty much the same
for other platforms but there may be differences with where you put python libraries, I am
sure this is well documented elsewhere. This should all work equally well with an Arduino board and possibly even Stamp, but I haven't tried them.
Before continuing you will need the following ready;
* A Wiring Board and Wiring software, up and running I hope.
* A potentiometer or another analogue sensor connected to Wiring.
* Blender installed.
* A little understanding of both Blender and Wiring.
\\
**Installing Pyserial**
Pyserial is a Python library that enables you to read serial data into Blender and probably
countless other applications. It is available to download from here; \\
http://pyserial.sourceforge.net/ \\
Once you have a folder called pyserial look inside and there should be a folder just called 'serial'. \\
You need put this folder in with the other Python libraries
Blender uses. For some reason these files are hidden away inside of the Blender
application. You can get to them like this.
Turn on hidden files by opening a terminal and running the following two lines.
username$ defaults write com.apple.Finder AppleShowAllFiles yes
username$ Killall Finder
Killall Finder sounds a bit drastic but it just relaunches finder
To turn off the hidden files just do the same again but use 'no' instead of yes
\\
**Getting the Wiring board ready**
For this example I suggest following the 'Reading a potentiometer' tutorial on the Wiring site here;
http://www.wiring.org.co/learning/examples/potentiometer.html
Here is the diagram from that page;
\\
{{http://www.wiring.org.co/learning/examples/images/potentiometer.gif?555*604}}
\\
**Programming the Wiring board**
Next program the board with the same code as the 'Reading a potentiometer' tutorial;
// Reading a potentiometer
// by BARRAGAN
int val;
void setup()
{
Serial.begin(9600); // sets the serial port to 9600
}
void loop()
{
val = analogRead(0); // read analog input pin 0
Serial.print(val, DEC); // prints the value read
Serial.print(" "); // prints a space betwen the numbers
delay(100); // wait 100ms for next reading
}
Now change the code to the following;
// Reading a potentiometer
// by BARRAGAN
// modified by Simon Blackmore to demonstrate using wiring with Blender
int val;
void setup()
{
Serial.begin(9600); // sets the serial port to 9600
}
void loop()
{
val = analogRead(0); // read analog input pin 0
Serial.print(val/4, BYTE); // Divides the value by 4 to give a value of 0 - 255 (1 BYTE)
delay(100); // wait 100ms for next reading
}
The important thing to change is;
Serial.print(val/4, BYTE); // Divides the value by 4 to give a value of 0 - 255 (1 BYTE)
\\
Also take out the line;
\\
Serial.print(" "); // prints a space betwen the numbers
\\
**Using Wiring with Blender**
Open Blender from a Terminal. This is important as it allow you see what Blender is outputting and debug any problems. Here is the line I use, but you may need to change the address of you application and the window size.
username$ /Applications/blender/Blender.app/Contents/MacOS/blender -p 240 0 1200 900
In Blender you need to open up a text window and put in the following python script.
# import game logic and serial modules
import GameLogic
import serial
# you will need the name of your serial port
# check in wiring - tools - menu
ser = serial.Serial('/dev/tty.usbserial-0000103D', 9600, timeout=None)
x = ser.read(size=1) #read one byte
ser.close() # close the port
# use ord to turn the value of the 8-bit string to an integer
y = ord(x)
print "y is", y
Now name the file in box at the bottom of the window something like 'serial.py'
To run the script you need attach it to an object. Select any object in the scene and click on its logic properties in the "Buttons Window".
Add an always sensor and link it to the python script. This should mean the python script will be executed when the game engine is running.
\\
It should look like this;
\\
{{http://www.simonblackmore.net/notes_media/wiring_blender/blender_screen1.jpg}}
\\
Now run the game engine.
\\
Click in the '3D Panel' and just press 'P'. The '3D Panel' should now change into the game view and numbers from wiring should appear in the terminal window.
\\
Here is the blender file to download;
\\
http://www.simonblackmore.net/notes_media/wiring_blender/serial_test.blend
\\
\\
The numbers from wiring can now be used to control anything that can be controlled by the Game Engine.
\\
And finally here is an example of controlling the directional force of an object with the potentiometer.
\\
http://www.simonblackmore.net/notes_media/wiring_blender/control_object1.blend
Simon Blackmore, January 2008