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 »