Basic Arduino to Maya Communication

July 26th, 2009

This article describes a basic setup allowing an Arduino microcontroller board to communicate with Maya via a serial connection.

Assumptions

  1. You have an Arduino board.
  2. You have successfully uploaded sketches to it.
  3. You have Maya 8.5 or later, and can execute Python scripts from within Maya.
  4. You are running 32-bit Windows.

Disclaimer: I only barely understand why this works. If you know how these instructions could be simplified, better generalized, or otherwise improved, please comment.

Overview

Since Maya 8.5, Maya has come with an installation of Python — for Maya 2009, it’s 2.5.1, and you can run it right in the script editor. The separately-downloadable pySerial module allows Python scripts to communicate with serial devices such as the Arduino. However, pySerial requires certain files from the Python for Windows extensions, aka PyWin32. PyWin32 apparently requires that a full installation of Python be available, and pySerial requires that the Python install and the PyWin32 files be of the same version number. So, I installed Python 2.5 first, which wrote appropriate registry entries. Then I installed PyWin32 for Python 2.5, noted the paths, and installed pySerial.

After futzing with test scripts to ensure correct installation, I uploaded a sketch to Arduino which wrote “hello world” to the serial port, and ran a Python script in Maya which listened, and heard, oh yes.

Details and code follow:

Details and code

I installed the pySerial Python module by extracting the serial directory to Maya’s python directory, which for me was C:\Program Files\Autodesk\Maya2009\python. Apparently you can just add the path to the PYTHONPATH Windows environment variable, but when I tried that, it broke Maya’s script editor, claiming some MEL problem. A likely story.

When trying to “import serial” in the Python script editor, it claimed it was missing two files called win32file.pyd and win32con.pyd. These turned out to be available from PyWin32, which I installed, but then the Python script returned a version mismatch — I had Python 2.1 installed separately from Maya, and apparently PyWin32 checked that install’s version when installing.

So — I downloaded Python 2.5 from the Python download page and installed it to C:\Program Files\Common Files\Python, because I already had an installation of Python there. Then I re-installed PyWin32, pointing it to the Python 2.5 installation.

That was the setup — the rest is code.

Arduino code

I found this simple Arduino serial test script by Tod Kurt linked to by Matt Croydon (original file here), and uploaded it to my Arduino.

/* Serial Hello World
 * ------------------- 
 * Simple Hello world for serial ports.
 * Print out "Hello world!" and blink pin 13 LED every second.
 *
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt 
 * http://todbot.com/
 */

int ledPin = 13;   // select the pin for the LED
int i=0;           // simple counter to show we're doing something

void setup() {
  pinMode(ledPin,OUTPUT);   // declare the LED's pin as output
  Serial.begin(9600);        // connect to the serial port
}

void loop () {
  Serial.print(i++);
  Serial.println(" Hello world!");  // print out a hello
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
  delay(500);
}

Python code

So the Arduino was talking to the serial port — getting Maya to listen was harder. I patched together the code below from a bunch of sources, mostly through the tantalizing hints Dan Thompson left in his post Maya + Python + Arduino + Servo (Part 1). He’s working the other way around, driving servos from Maya, mwa haw haw.

To get the right serial port settings, I went to the Windows Device Manager and found the USB Serial Port, noted the port name, and found the other settings in Properties > Port Settings. The path.append stuff is apparently unusual, but it wouldn’t work for me without it. I ran this from the script editor (note that you’ll have to force-quit Maya to get it to stop):

### pySerial port listener
import sys
sys.path.append( "C:\Program Files\Common Files\Python\Python25\Lib\site-packages\win32")
import win32file
sys.path.append( "C:\Program Files\Common Files\Python\Python25\Lib\site-packages\win32\lib")
import win32con
import serial

# configure the serial connections
ser = serial.Serial(
  port='COM3',
  baudrate=9600,
  parity=serial.PARITY_NONE,
  stopbits=serial.STOPBITS_ONE,
  bytesize=serial.EIGHTBITS
)

while 1:
  print ser.readline()
###

The Result

« previously: Rhonda drawing tool | Home | next: Basic Arduino to Maya II »

Leave a Reply