Ok so I know its been asked a few times on here, but I could find
anyone that came to a conclusion. I was also trying to get external
device input, this time over an Arduino, which means that it simply
reads the value over the serial port. In my test example below its
only sending one value.
things work up to a point, but I can't get my TimerCallback message to
work- I keep getting an error when i try to load it. If someone could
take a look and give me an idea where I've gone wrong I would
appreciate it.
......heres the code
import maya.OpenMaya as om
import maya.OpenMayaMPx as omMPx
import serial
#Set name and id
nodeName = "ardServer"
nodeId = om.MTypeId(0x998112)
#set the serial port / baudrate
ser1 = serial.Serial('/dev/tty.usbserial-A600ahtY',9600)
class ardNode(omMPx.MPxNode):
# input and output variables
OUTPUT = om.MObject()
def __init__(self):
omMPx.MPxNode.__init__(self)
def nodeCreator():
return omMPx.asMPxPtr( ardNode() )
def nodeInit():
#create the attribute
numAttr = om.MFnNumericAttribute()
ardNode.OUTPUT =
numAttr.create("sensor0","s0",om.MFnNumericData.kInt,0)
numAttr.setStorable(1)
numAttr.setWritable(1)
ardNode.addAttribute(ardNode.OUTPUT)
#create the callback
nodeHdl = om.MObjectHandle(ardNode)
cb = om.MTimerMessage.addTimerCallback(0.05, requestSample,
nodeHdl)
#callback procedure
def requestSample(elapsed, last, node):
if node.isValid():
nodeFn = om.MFnDependencyNode(node.objectRef())
plug = om.MPlug(node, ardNode.OUTPUT)
#open the serial port and read it
try:
if ser1.isOpen()==0 :
ser1.open()
s0val = int(ser1.readline())
ser1.close()
plug.setInt(s0val)
except:
sys.stderr.write("not Connected")
raise
def initializePlugin(mobject):
mplugin = omMPx.MFnPlugin(mobject)
try:
mplugin.registerNode(nodeName, nodeId, nodeCreator, nodeInit)
except:
sys.stderr.write("Error loading")
raise
def uninitializePlugin(mobject):
mplugin = omMPx.MFnPlugin(mobject)
try:
mplugin.deregisterNode( nodeId )
except:
sys.stderr.write("Error removing")
raise
--
http://groups.google.com/group/python_inside_maya
To unsubscribe from this group, send email to
python_inside_maya+unsubscribegooglegroups.com or reply to this email with the
words "REMOVE ME" as the subject.