Sunday, February 15, 2009

Useful System Commands (Part One)

Below I've compiled a sort of list/summary of some of the most common and useful "system" commands that I use quite frequently. This section deals mainly with directory/file manipulation and involves using 3 very powerful modules: os, sys, and glob.
import glob, os, sys

# List Files in a Directory
path = 'C:\\Users\\User\\Documents\\maya\\scripts'
files = os.listdir(path) # Returns Only fileNames
files = glob.glob(path) # Returns Full Path to fileNames

# Filter Files in a Directory
pythonScripts = glob.glob(path + '\\*.py')

# Split File Path by Path/Filename in One Go
path = 'C:\\Users\\User\\Documents\\maya\\scripts\\testScript.py'
filePath, fileName = os.path.split(path)

# Split Filename by Name/Extension in One Go
fileBaseName, fileExtension = os.path.splitext(fileName)

# Get Main User Documents Directory
os.path.expanduser('~') # Returns C:/Users/User/Documents
os.path.expanduser('~/maya/scripts') # Returns C:/Users/User/Documents/maya/scripts

# Check if File/Directory Exists, If Not Create it
if not os.path.isdir(path):
os.mkdir(path)
if not os.path.isfile(path):
print 'File Does Not Exist : %s' % path

# Rename Directory/File
os.rename(oldFilename, newFilename)

# Execute/Launch A File in it's Native Program
os.startfile(path)

# Check Python Enviornment Paths
pythonPaths = sys.path
sys.path.append( os.path.expanduser('~/maya/scripts/') )
Documentation:
- sys module
- os module
- glob module

Sunday, February 8, 2009

Communicating With Maya -- socket & telnetlib

So recently I've been experimenting with ways to communicate with Maya over a network or across different instances of Maya and figured I'd post what I came across while researching this topic.
Python really makes this task quite effortless and is very easy to setup. There are 2 modules that come packaged with Python that allow information to be sent across open sockets/command Ports. The two modules are socket and telnetlib, but as of yet i haven't found a significant difference between the two as far as communicating with Maya (if anyone does know please enlighten me).

To start off, you must open up a commandPort within Maya so that Maya can retrieve any data sent externally...

cmds.commandPort(n=':6001')

SOCKET:
import socket

# Define Host:Port to Connect To
# Host = IP Address of Computer
# Port = Command Port Opened in Maya
host = 'localhost' # Refer's to "This Computer"
port = 6001

try:
# Connect to Maya Command Port
maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
maya.connect( (host,port) )

# Send Command Through Socket --> Can Only Send MEL Commands
message = 'python("cmds.sphere()")'
maya.send(message)

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

finally:
# Close Socket Connection
maya.close()

TELNETLIB:
from telnetlib import Telnet

# Define Host:Port to Connect To
# Host = IP Address of Computer
# Port = Command Port Opened in Maya
host = 'localhost'
port = 6001

try:
# Connect to Maya Command Port
maya = Telnet()
maya.open( host, '%s' % port )

# Send Command Through Socket --> Can Only Send MEL Commands
message = 'python("cmds.sphere()")'
maya.write(message)

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

finally:
# Close Socket Connection
maya.close()

So as you can see, there is very little difference between the two, but both offer powerful tools to expand the use of Maya.

These examples can be expanded upon to do many things including:
- Integrating Maya with External IDE's
- Cross-Network Chat Programs

These are just a few situations that can arise from these modules and leave a whole lot at a TD's disposal.

socket documentation
telnetlib documentation
The Significance of 'localhost'