View Full Version : Python to Shell

11 November 2011, 08: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 November 2011, 09: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 November 2011, 11: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 November 2011, 01: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 script from the cmd:

# Usage example:
# python "print \"hi\""
# python "polySphere"

import socket
import sys

if len(sys.argv) < 2:
print "Usage: commandPort <mel_cmd>"

cmd = sys.argv[1];

print "Sending command:\n\n", cmd

host = 'localhost'
#host = "" # ip of this machine
port = 6001

# basic socket test
#host = ""
#port = 80

maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Socket created"
maya.connect( (host,port) )
print "Connected"

message = cmd
print "Message sent"

raise Exception, 'Connection Failed To : %s:%s' % (host, port)

# Close Socket Connection

If it won't work try:

11 November 2011, 02: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 November 2011, 07:34 AM
The execute stuff inside maya, for example run calc.exe:

import os

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:

CGTalk Moderation
11 November 2011, 07: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.