View Full Version : Python to Shell
11-14-2011, 07:59 PM
Can python read and receive commands from a shell? I want to be able to read and write commands and information from a shell directlly into maya. Since we do all our saving and openning of files throw a shell. I'm sick and tired of hunting throw the shell to open and save files. I want to do it throw a UI directly in maya. And since we use specific commands to navigate throw the shell, can I send those commands from a UI in maya? Can that be done directlly throw mayas python?
11-14-2011, 08:25 PM
mayapy.exe is a python interpreter like any other, which through it you should be able to do anything. You can use the commandPort interface to send commands into maya (somewhat broken in the magnificent versions after 2010), and nothing stopping you to access the shell from a script inside maya.
11-14-2011, 10:00 PM
Ohh sweet. Can you give me an example of some code? I'm having a little bit of trouble finding some documentation, on how exactly to lay out the code.
11-15-2011, 12:29 AM
You didn't say which direction (maya->cmd or cmd->maya) or what exactly are you looking for.
I assume that you asked how to send a command to maya. First run commandPort in the mel command inside maya to open the listening port. Second try to run my commandPort.py script from the cmd:
# Usage example:
# python commandPort.py "print \"hi\""
# python commandPort.py "polySphere"
if len(sys.argv) < 2:
print "Usage: commandPort <mel_cmd>"
cmd = sys.argv;
print "Sending command:\n\n", cmd
host = 'localhost'
#host = "192.168.1.13" # ip of this machine
port = 6001
# basic socket test
#host = "www.google.com"
#port = 80
maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Socket created"
maya.connect( (host,port) )
message = cmd
print "Message sent"
raise Exception, 'Connection Failed To : %s:%s' % (host, port)
# Close Socket Connection
If it won't work try:
11-15-2011, 01:11 AM
I will give that a try. Im looking to do both. Send and receive info, thanks for the example. If you also know of any readable scripts that would do what I'm looking for, please point me in that direction.
11-15-2011, 06:34 AM
The execute stuff inside maya, for example run calc.exe:
And this is the whole bible for both directions, and I'm not sure what else you need.
If you are simply looking to work with files, then python has commands for creating directories, files, copying, deleting, editing, etc. Check the sections on file objects, os, posix in the following cheat sheet:
11-15-2011, 06:34 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2015, Jelsoft Enterprises Ltd.