Interfacing the Wiring hardware and Blender/Phyton

This tutorial introduces the basic interfacing between the Wiring hardware and and Blender/Phyton http://www.blender.org/. The brief for the tutorial will be to send data to the Wiring board, the Wiring board will read the data and turn ON or OFF the board LED accordingly. It assumes the Wiring software and Blender/Phyton are installed and the proper Wiring setup has been previously completed. For more Information on Wiring install check out the tutorials about Wiring installation and software setup.

Note: This tutorial have benn prove it using Blender 2.49 which can be downloaded from here and Phyton 2.5 wich can be downloaded from here.

Step 1

Copy and paste the following code on the Wiring editor: Verify your program is free of compiling errors by pressing the Compile/Verify button in the menu bar. Press the Upload button in the menu bar. In case of syntax errors the Wiring environment will print the error messages otherwise it will print the Upload was completed successfully, the uploading process triggers activity in the Rx/Tx LEDs on the Wiring hardware. The new program will start automatically after uploading. Use the Serial Monitor button to watch the data coming from the Wiring board, then close the Serial Monitor again.

/**
 * Simple Read/ Blender/Python
 * 
 * Read data from the serial port and turn ON the onboard LED if
 * the character received is an 'a'
 * For the Wiring boards v1 the on-board LED is on pin 48, 
 * on Wiring S the on-board LED is on pin 15.
 */

int data; // to read the char

void setup() {
  Serial.begin(9600);
  pinMode(WLED, OUTPUT);
}

void loop() {
  if(Serial.available()) {      // if data available
    data = Serial.read();       // read data
    if(data == 'H') {           // if value read is character 'a'
      digitalWrite(WLED, HIGH); // turn ON the onboard LED
    } else {
      digitalWrite(WLED, LOW);  // if not turn it OFF
    }
  }
  delay(100); // wait 100ms for next read
}

 

Paso 2

Download PySerial here. Uncompress the file and placed the folder "serial" with the other Python libraries for Blender.

Windows:

Placed the folder "serial" in the folder ".blender/scripts" which us inside the Blender folder.

Mac OS X:

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

To turn off the hidden files just do the same again but use "no" instead of "yes".

Placed the folder "serial" in the folder ".blender/scripts" which us inside the Blender folder.

 

Step 3

Next step is to setup things in Blender. Setup the interface to "Scripting" mode.

 

Step 4

Copy the following code in the editor and name it "script.py".

import serial
from time   import sleep
# you will need the name of your serial port
# check in wiring - tools - menu
serialport = serial.Serial('COM2', 9600)

sleep(4)    

while 1:
     serialport.write('H')
     sleep(1)
     serialport.write('L')
     sleep(1)

serialport.close()

Step 5

Attach 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:

Step 6

Run the game engine. Click in the '3D Panel' and just press 'P'.