Basic Arduino to Maya Mocap

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:

Maya Python code

### simple button record
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
import maya.cmds as cmds
import maya.mel as mel

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

recordStart = timerX()
totalTime = timerX(startTime=recordStart)

mySphere = sphere()[0]

# progress bar, enabling "Esc"
gMainProgressBar = mel.eval('$tmp = $gMainProgressBar');
cmds.progressBar( gMainProgressBar,
        edit=True,
        beginProgress=True,
        isInterruptable=True,
        status='Reading serial data...' )
        
# main loop
while 1:
  myInput = ser.read()
  print myInput
  myScale = int(myInput)+1
  mySphere.scale.set((myScale, myScale, myScale))
  keyTime = (timerX(startTime=recordStart))*24
  cmds.setKeyframe("nurbsSphere1", attribute="scaleX", v=myScale, t=keyTime)
  cmds.setKeyframe("nurbsSphere1", attribute="scaleY", v=myScale, t=keyTime)
  cmds.setKeyframe("nurbsSphere1", attribute="scaleZ", v=myScale, t=keyTime)
  refresh() # update the viewscreen

  if cmds.progressBar(gMainProgressBar, query=True, isCancelled=True):
    # note the time to set playback range
    endTime = timerX(startTime=recordStart)*24
    break

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

currentTime(1)
cmds.playbackOptions(l='once', max=endTime) # no looping

# turn on "Auto Frame" and show graph editor
mel.eval('animCurveEditor -edit -autoFit 1 graphEditor1GraphEd;')
mel.eval('setNamedPanelLayout( "Persp/Graph" )')
###

(By the way: how to keep “Auto Frame” on in the Graph Editor.)

« previously: Basic Arduino to Maya III | Home | next: Fenrir »

Leave a Reply