Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Python 2.7
# This file contains all the functions needed to receive commands in 3ds Max and send them from another application.
# In Max just run this file and the open_max_command_port() command
# In another application you only need to run the send_command_to_max() command
# This only works with maxscript commands!
# Format them like this: send_command_to_max("box()")
import socket
try: # Using a try here so it doesn't error out in my IDE
import MaxPlus
except ImportError:
pass
import threading
HOST = '127.0.0.1' # The local IP
COMMAND_PORT = 7500 # The port used by the server
def send_command_to_max(command):
try:
maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
maya.connect(HOST, COMMAND_PORT)
maya.sendto(command.encode(), COMMAND_PORT)
maya.close()
print("Sent Command: ", command)
return True
except Exception as e:
print(e)
return False
def listener():
max_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
max_socket.bind((HOST, COMMAND_PORT))
command = None
while command != "exit":
max_socket.listen(5)
connection_obj, sender = max_socket.accept()
data = connection_obj.recv(1024)
if data:
command = str(data.decode())
print("Command: ", data.decode())
if command == "exit":
break
try: # Try to evaluate the command.
MaxPlus.Core.EvalMAXScript("%s" % command)
except Exception as e:
print(e)
max_socket.close()
def open_max_command_port():
listen_thread = threading.Thread(target=listener)
listen_thread.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.