Archive for the ‘Python’ Category

Arduino to Maya Mocap II

Saturday, August 1st, 2009

Another proof of concept: Maya processing and recording three simultaneous data streams from the Arduino at ~30 fps. There’s still only the one button on the Arduino, but I’m faking three sensors by reading and transmitting its state three times.

The keys for the spheres’ “simultaneous” state changes are never more than about 50 milliseconds apart. In every mocap test I’ve run so far, every key has wound up quantized by multiples of 10 milliseconds in the timeline, perhaps due to the pySerial sampling rate or some internal Maya clock… suspicious.

Code follows:

Fastidious Python Shrub

Friday, July 31st, 2009

This shrub code features pains-takingly accurate collision-detection using closestIntersection nodes, and a UI featuring a progress bar.

It is very slow. Perhaps a tad obsessive in its detection. These 500 cones took about 10 minutes.

Code follows:

Basic Arduino to Maya Mocap

Wednesday, July 29th, 2009

Once again, The Mystery Hand starring in Proof of Concept: MOCAP.

I’m transmitting from the Arduino 10 times a second, which is slow enough for the Python to behave in an orderly fashion.

The Arduino code and board setup is the same as before. Highlights of the new Python code include the ability to break the script out of an otherwise infinite loop, using the GUI’s snazzy built-in progress-bar.

Code follows:

Basic Arduino to Maya III

Tuesday, July 28th, 2009

Baby steps while I bonk my head against the Maya API:

Code follows:

Basic Arduino to Maya II

Monday, July 27th, 2009

Here’s Maya reading real-time input from an Arduino board, set up in the circuit from the Arduino Button tutorial, and assuming the environment setup from my first Arduino post:

I’ve tweaked the code for the video to make the readout easier to see.

Code follows:

Basic Arduino to Maya Communication

Sunday, July 26th, 2009

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


  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.


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:

Polite Python shrub

Friday, April 24th, 2009

A 100-pyramid Pythonized polite shrub — 500 took too long, and crashed Maya.

Gory details (including code) follow:

Semi-polite Python worm

Friday, April 17th, 2009

I was doing so well, placidly retracing my steps through my MEL code and converting it to Python, like a responsible citizen. But then I got impatient, so I skipped straight to the last version of my worm-building code, and Pythonized it all at once using my still-rudimentary Pymel knowledge.

It took a bit longer than I expected. I ran into some funny roadblocks, some because Pymel is still evolving, and some because my brain is. Anyway, it seems to be working, with some small improvements, and only one teensy drawback.

The Python is easier to read and write than the MEL, but it takes eight times longer to run: for 100 pyramids, over 10 runs, an average of 97 seconds versus 12 seconds for the MEL. Whether this is a dirty secret of Pymel or my own ineptitude will take more eptitude to discover.

I may do a side-by-side translation/comparison in the future; there’s a lot of MEL floating around, and some kind of Rosetta stone may be useful. For now, you get the Python.

Code follows:

Aligned faces pyramid worm in Python

Friday, April 3rd, 2009

A Python version of the Aligned faces pyramid worm MEL script, for Maya:

Code and commentary follows:

Random walk pyramids in Python

Thursday, April 2nd, 2009

Here’s a Python version of the first random pyramids code, for Maya:

I know I’m converting now-deprecated MEL code here, but I can use the practice.

Random walk keyframed blocks in Python

Thursday, April 2nd, 2009

I’ve converted my first cube-generating code from MEL to Python, using the PyMEL modules for Maya, with promising results.

Code and commentary follows: