How to send command to Maya, from python 3 script?


#1

I have a script that I had to write in Python 3.
now I need to send command to Maya from my script ( running maya ), something like open a file, anyone have any idea ?
is it possible through command port ? if so can I get an example ?


#2

just found solution
run this code in maya :

import maya.cmds as cmds
cmds.commandPort(name=":54321", sourceType="python")

then run this in your interpreter

import socket
HOST = '127.0.0.1'	
PORT = 54321	
ADDR=(HOST,PORT)
def SendCommand():
    client = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    client.connect(ADDR)
    command = 'cmds.polyCube()'	
    client.send(command)
    client.close()

if __name__=='__main__':
    SendCommand()