Hi,
I am trying to create a socket connection to another app in a separate thread using python in Maya. i have tried the following based on the python.org socket examples and thread timer code from Nathan Horne (http://nathanhorne.com):
import sys
import threading
import maya.utils as utils
import maya.cmds as cmds
import string
import socket
import random
try:
from maya.utils import executeInMainThreadWithResult
except:
executeInMainThreadWithResult = None
global sockData
def createPoly():
print "create a sphere:"
# simple func to create a sphere to demonstrate successful control of the GUI
sphereX = random.randint(10, 50)
sphereZ = random.randint(10, 50)
sphereRadius = random.randint(0, 2)
newSphere = cmds.polySphere(axis=[sphereX,0,sphereZ], r=sphereRadius)
class Timer(threading.Thread):
def __init__(self, interval, function, args=[], kwargs={}, repeat=True):
self.interval = interval
self.function = function
self.repeat = repeat
self.args = args
self.kwargs = kwargs
self.event = threading.Event()
threading.Thread.__init__(self)
def run(self):
def \_mainLoop():
self.event.wait(self.interval)
if not self.event.isSet():
if executeInMainThreadWithResult:
executeInMainThreadWithResult(self.function, *self.args, **self.kwargs)
else:
self.function(*self.args, **self.kwargs)
if self.repeat:
while not self.event.isSet():
\_mainLoop()
else:
\_mainLoop()
self.stop()
def start(self):
self.event.clear()
threading.Thread.start(self)
def stop(self):
self.event.set()
threading.Thread.\_\_init\_\_(self)
def listenSocket():
sockData = conn.recv(1024)
if sockData:
print ("listening")
conn.send(sockData)
print(sockData)
createPoly()
# conn.close()
#create and start a timer
HOST = '' # Symbolic name meaning all available interfaces
PORT = 1024 # Arbitrary non-privileged port
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST, PORT))
sock.listen(1)
conn, addr = sock.accept()
print 'Connected by', addr
socketTimer = Timer(5, listenSocket, repeat=True)
socketTimer.start()
In the above code, I use the timer class to run a socket connection in another thread so that Maya can run other operations. The createPoly() function is simply to ensure that my external client can make things happen in Maya.
Unfortunately, the code works briefly(!) each time before hanging the Maya GUI. Every time I update (send) from the client it will create a new sphere and give me time to move it around a little. After this it simply hangs......
I want to use the socket to port an interface written in Flex for use as a Maya plugin GUI. I know about Tkinter and have implemented versions using both this and the native Maya UI controls but neither has the level of functionality or layout control I am looking for.
Can someone tell me where I am going wrong? I know I don't close conn or sock during the code (I type them at the console to stop the routine) but the hanging problems are definitely thread related.
To test this code, I use both a simple Flex socket page (I can pass this on if need be) but the python.org socket client example code (docs.python.org/library/socket.html) should also trigger the polySphere function to test.....
Any help or ideas anyone can come up with are very much appreciated 
Thanks,
Charlie