PDA

View Full Version : Python to Shell


nottoshabi
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?

zoharl
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.

nottoshabi
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.

zoharl
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"

import socket
import sys

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

cmd = sys.argv[1];

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

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

message = cmd
maya.send(message)
print "Message sent"

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

finally:
# Close Socket Connection
maya.close()


If it won't work try:

https://docs.google.com/open?id=0B4PDrM6_sr2UMTY4MzM4NTYtYjFiYi00ZDNkLWI0MTUtZGEzOGQ2NjJmZjhk

nottoshabi
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.

Thanks...

zoharl
11-15-2011, 06:34 AM
The execute stuff inside maya, for example run calc.exe:

import os
os.system('calc')

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:

http://rgruet.free.fr/PQR26/PQR2.6.html

CGTalk Moderation
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.