Open Maya as separate thread from Python

# Maya thread
class maya_thread(threading.Thread):
    def run(self):
        ''' Opens Maya as a seperate Thread '''
        maya_cmd = ('maya -command "runCommandPortInMaya"')
        #print 'Maya Running...'
        maya_process = subprocess.call(maya_cmd,shell=True)
        return maya_process

#------------------------------------------------------------------------------------------------
# Establish connection with maya
def connectWithMaya(port):
    ''' connects to Maya using socket '''
    maya_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    maya_socket.connect(("10.88.162.46", port))
    maya_socket.send(r'print"\nConnected to Python ...\n";')
    return maya_socket

#------------------------------ Connecting to Maya and sending Mel Commands -------------------
        # Maya Thread
        maya_process = maya_thread().start()
        # Maya Socket
        maya_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #---------------------------------------------------------------
        # Loop to wait till Maya is Invoked
        # Port specified in runCommandPortInMaya.mel file
        port = 5055
        while 1:
            try:
                maya_socket.connect((socket.gethostname(), port))
                break
            except:
                continue

# Loop to wait till Maya is up and fully running
        while 1:
            #maya can only send 4096 bytes through socket
            data = maya_socket.recv(4096)
            data = data.replace("\n","")
            #print data
            tokens = data.split(" : ")
            # if preferences are saved in maya, then Maya is fully loaded with all plugins
            if re.search("Preferences saved.",data):
                break
        #---------------------------------------------------------------
        #("Maya is Running...\n")

No comments: