My last goal was to have this external process inform Maya and the tool that resides in Maya, of any updates that may need to be made. If you've written any Python and user interfaces with Python you are likely familiar with GIL (global interpreter lock). In a simplified explanation you could being running a process that will lock up your tool and any parent of the tool, in this case Maya. By using threads you can run your processes in the background and not lock up your tool or Maya. Well, it's less likely to happen. Now that I can communicate with Maya from an external source, I should be able to update any tools within as needed.
Many thanks to the TDAnon guys, including @DhruvGovil for pointing me in the right direction.
Here is an example of me sending external messages to Maya. The scripts I use can be found below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
External Maya Client | |
This can be run outside of Maya | |
Use in idle or an IDE that can accept input. | |
""" | |
import socket | |
import logging | |
HOST = 'localhost' | |
PORT = 12345 | |
def message_maya(text): | |
""" | |
Send a message to Maya, server must be running the same PORT | |
:param text: string | |
:return: | |
""" | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
try: | |
logging.info(" Connecting Socket to Maya...") | |
sock.connect((HOST, PORT)) | |
logging.info(" Sending Message to Maya: {}".format(text)) | |
sock.send(text) | |
except Exception, connect_error: | |
logging.error("FAILED to send message to Maya: {}".format(connect_error)) | |
try: | |
logging.info(" Closing Socket") | |
sock.close() | |
except Exception, close_error: | |
logging.error("FAILED to close socket: {}".format(close_error)) | |
if __name__ == "__main__": | |
logging.basicConfig(level=logging.DEBUG) | |
while True: | |
try: | |
message = input("Input Message:") | |
except Exception, input_error: | |
msg = "Error with Input, must use quotes!: {}", input_error | |
logging.error(msg) | |
message = None | |
if message: | |
message = message.strip() | |
message_maya(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Maya Threaded Server | |
Run this in Maya | |
""" | |
import logging | |
import socket | |
import threading | |
import maya.cmds as cmds | |
import maya.utils as maya_utils | |
HOST = "localhost" | |
PORT = 12345 | |
CONNECTIONS = 4 | |
def function_to_process(data): | |
""" | |
Maya function | |
:param data: incoming data to process | |
:return: | |
""" | |
logging.info("Maya Server, Process Function: {}".format(data)) | |
cmds.headsUpMessage("Processing incoming data: {}".format(data), time=3.0) | |
def process_update(data): | |
""" | |
Process incoming data, run this in the Maya main thread | |
:param data: | |
:return: | |
""" | |
try: | |
maya_utils.executeInMainThreadWithResult(function_to_process, data) | |
except Exception, e: | |
cmds.error("Maya Server, Exception processing Function: {}".format(e)) | |
def maya_server(host=HOST, port=PORT, connections=CONNECTIONS): | |
""" | |
Maya server | |
:param host: Host IP or localhost | |
:param port: Integer | |
:param connections: Integer Number of connections to handle | |
:return: | |
""" | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
try: | |
sock.bind((host, port)) | |
except Exception, socket_error: | |
msg = "Maya Server, Failed to open port: {}".format(socket_error) | |
logging.error(msg) | |
cmds.error(msg) | |
return | |
sock.listen(connections) | |
logging.info("Starting Maya Server: {}".format(port)) | |
while True: | |
client, address = sock.accept() | |
data = client.recv(1024) | |
if data: | |
if data == "#Shutdown#": | |
break | |
else: | |
logging.info("Maya Server, Data Received: {}".format(data)) | |
process_update(data) | |
try: | |
client.close() | |
except Exception, client_error: | |
logging.info("Maya Server, Error Closing Client Socket: {}".format(client_error)) | |
logging.info("Maya Server, Shutting Down.") | |
try: | |
sock.close() | |
except Exception, close_error: | |
logging.info("Maya Server, Error Closing Socket: {}".format(close_error)) | |
if __name__ == "__main__": | |
logging.basicConfig(level=logging.DEBUG) | |
threading.Thread(target=maya_server).start() |