Skip to content

Instantly share code, notes, and snippets.

@Meatplowz
Last active June 1, 2024 04:08
Show Gist options
  • Save Meatplowz/154fb17487e9ce0c0e8b362262a2d8a4 to your computer and use it in GitHub Desktop.
Save Meatplowz/154fb17487e9ce0c0e8b362262a2d8a4 to your computer and use it in GitHub Desktop.
Maya Threaded Server example with Client
"""
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)
"""
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()
@Meatplowz
Copy link
Author

Run the "maya_threaded_server" code inside of Maya.
You can then run the "maya_external_client" outside of Maya and provide live input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment