Wii Nunchuk to Maya

April 11th, 2010

Simple proof of concept: A Wii Nunchuk controller connected to Maya via an Arduino, which sends the Nunchuk’s accelerometer data through the serial port. Maya uses PySerial to open the serial port, reads the data, and converts it to an attribute value.

You can see that the accelerometer has quite a lot of “ring” from sharp shocks.

I based this test on Tod Kurt’s BlinkMChuck code. The Nunchuk is connected to the Arduino with this connector from Todbot. Here’s an archive containing the test files: WiiChukPOC.zip

And here’s a link to the setup required to configure pySerial: http://zoomy.net/2009/07/26/basic-arduino-to-maya-communication/

Code follows.

Arduino code

/*
 * Based on BlinkMChuck (c) 2008 Tod E. Kurt, http://thingm.com/
*/

#include "Wire.h"
#include "BlinkM_funcs.h"
#include "nunchuck_funcs.h"

int blinkm_addr = 0x09; // the address of our BlinkM
int loop_cnt=0;
byte hue, bri;
#define BLINKM_ARDUINO_POWERED 1

void setup() {
    Serial.begin(19200);
    if( BLINKM_ARDUINO_POWERED ) {
        BlinkM_beginWithPower();
    } else {
        BlinkM_begin();
    }
    nunchuck_init(); // send the initilization handshake
}

void loop() {
    if( loop_cnt > 5 ) { // every 50 msecs get new data
        loop_cnt = 0;
        if( ! nunchuck_get_data() ) { 
            Serial.println("error getting nunchuck data");
        }
        hue  = nunchuck_accelx(); // ranges from approx 70 - 182
        bri  = nunchuck_accely(); // ranges from approx 65 - 173

        Serial.print( hue,DEC); Serial.print( ";" );
        Serial.println( bri,DEC);
    }
    loop_cnt++;
    delay(10);
}

Maya python code

### Wii Nunchuk to Maya proof of concept
### http://zoomy.net/2010/04/11/wii-nunchuk-to-maya/
###
sys.path.append( "C:\Program Files\Common Files\Python\Python25\Lib\site-packages\win32")
sys.path.append( "C:\Program Files\Common Files\Python\Python25\Lib\site-packages\win32\lib")
import time, sys, serial, win32file, win32con, re
import maya.cmds as cmds
import maya.mel as mel

try: ser
except: 1
else: ser.close()

# open serial connection - adjust settings for your input device
ser = serial.Serial(
  port='COM3',
  baudrate=19200,
  parity=serial.PARITY_NONE,
  stopbits=serial.STOPBITS_ONE,
  bytesize=serial.EIGHTBITS
)

sphere1 = polySphere(n="sphere1")[0]
cmds.setAttr(sphere1+".scaleY", 2)
flare=nonLinear(type='flare')[0]
cmds.setAttr(flare+".startFlareX",1.5)
cmds.setAttr(flare+".startFlareZ",1.5)
cmds.setAttr(flare+"Handle.visibility",0)

# progress bar, enabling "Esc"
gMainProgressBar = mel.eval('$tmp = $gMainProgressBar');
cmds.progressBar( gMainProgressBar,
        edit=True,
        beginProgress=True,
        isInterruptable=True,
        status='Reading serial data...' )
        
while 1:
  data = ser.readline()
  data = data.split(';')
  data[1] = data[1][:-2]
  print data

  cmds.setAttr(sphere1+".rotateZ", (float(data[0])*-1)-45)
  cmds.setAttr(sphere1+".rotateX", float(data[1])+90)
  refresh() # update the viewscreen

  if cmds.progressBar(gMainProgressBar, query=True, isCancelled=True ) :
    delete(whichObj)
    break

ser.close()
cmds.progressBar(gMainProgressBar, edit=True, endProgress=True)

###
« previously: Patrick Jean – Pixels | Home | next: Superbrothers »

7 Responses to “Wii Nunchuk to Maya”

  1. Emil Says:

    Hi,

    I’m working on a similar experiment with some other sensors. Have you experienced a massive slow down of the maya refresh rate after like 30 secs?

    Cheers
    Emil

  2. zoomy Says:

    No, but I’m not certain I’ve ever recorded more than 30 seconds. Sounds ram-related to me…

  3. Bob Says:

    is refresh() a python method? Cool code btw, thanks for sharing.

  4. Bob Says:

    nevermind. I figured it out but mine is slow. Should i adjust the baudrate?

  5. zoomy Says:

    Also try changing the timing in the loop — the “loop_cnt > 5″ and “delay 10″ lines, I believe those numbers are both 10s of milliseconds.

  6. firas3d Says:

    Peter,

    thats for sharing ! am trying to use the iPhone sensors as maya device( i found a way to read sensor data using (iseismometer.com) application.

    am not expert, but in your example u used the “progress bar” to exit from reading,

    Is there a way to use Maya “cmds.assignInputDevice” ? I think it will be better to use the “maya device” as a top layer that controls the reading.. and also faster, without the need to refresh the viewport.

    I’ll use your method now, but I appreciate if u guide me in defining Maya device ..

    cheers,
    Firas.

  7. zoomy Says:

    Firas,

    I’m sure there is, but I’ve never used it successfully — let me know if you get something working!

Leave a Reply