P4Python
#Needs p4python installed
#P4Utility
import os
import sys
from P4 import P4
class P4Utils:
def __init__(self,user,port,client):
self.p4 = P4()
self.p4.user = user
self.p4.port = port
self.p4.client = client
def connectToP4(self):
try:
self.p4.connect()
except:
print "Error in Connecting to P4"
def do_sync(self,depot):
try:
output = self.p4.run("sync",depot)
return output
except:
output = "Files up-to-date"
return output
def do_changes(self,depot):
output = self.p4.run("changes",depot)
return output
Microsoft Excel + Python
# excelWin32ReadWrite
from win32com.client import Dispatch
class excelWin32ReadWrite:
def __init__(self,filename = None):
self.xlApp = Dispatch('Excel.Application')
if filename:
self.filename = filename
self.xlBook = self.xlApp.Workbooks.Open(filename)
else:
self.xlBook = self.xlApp.Workbooks.Add()
self.filename = ''
def save(self,newfilename = None):
if newfilename:
self.filename = newfilename
self.xlBook.SaveAs(newfilename)
else:
self.xlBook.Save()
def setCell(self,sheet,row,col,value):
sht = self.xlBook.Worksheets(sheet)
sht.Cells(row,col).Value = value
def getCell(self,sheet,row,col):
sht = self.xlBook.Worksheets(sheet)
return sht.Cells(row,col).Value
def getContiguosRange(self,sheet,row,col):
sht = self.xlBook.Worksheets(sheet)
bottom = row
while sht.Cells(bottom+1, col).Value not in [None,'']:
bottom = bottom+1
right = col
while sht.Cells(row, right+1).Value not in [None,'']:
right = right+1
return sht.Range(sht.Cells(row,col), sht.Cells(bottom,right)).Value
def renameSheet(self,sheet,newname):
self.xlBook.Worksheets(sheet).Name = newname
def deleteSheet(self,sheet):
self.xlBook.Worksheets(sheet).Delete
def returnAllSheetNames(self):
allSheets = self.xlBook.Sheets
sheetNames = []
for i in range(0,len(allSheets)):
sheetNames.append(self.xlBook.Sheets[i].Name)
return sheetNames
def close(self):
self.xlBook.Close(SaveChanges = 0)
del self.xlApp
Add user modules path to system path in Python
import os
import sys
#
pyModulesPath = os.path.join(os.getcwd(),"pyModules")
sys.path.append(pyModulesPath)
#
Parse XML Document in Python
from xml.etree import ElementTree
#------------------------------------------------------------------------------------------------
# function to parse XML in dictionary
def readConfigFile(filename):
'''Reads config XLS and returns dictionary of all variables'''
dictree = ElementTree.parse(filename).getroot()
global_params = {}
listCtrl_values = []
tmpList = []
relative_paths = ["relativepath”]
print "Reading Global Parameters..."
for entry in dictree:
relative_switch = 0
tmpList = []
key = re.sub('[\t\n\r\f\v]','',str(entry.tag))
value = re.sub('[\t\n\r\f\v]','',str(entry.text))
for item in relative_paths:
if key == item:
relative_switch = 1
if relative_switch == 1:
value = os.path.join(os.getcwd(),value)
global_params[key] = value
tmpList.append(key)
tmpList.append(value)
listCtrl_values.append(tmpList)
return global_params,listCtrl_values
Open Maya as separate thread from Python
# Maya thread
class maya_thread(threading.Thread):
def run(self):
''' Opens Maya as a seperate Thread '''
maya_cmd = ('maya -command "runCommandPortInMaya"')
#print 'Maya Running...'
maya_process = subprocess.call(maya_cmd,shell=True)
return maya_process
#------------------------------------------------------------------------------------------------
# Establish connection with maya
def connectWithMaya(port):
''' connects to Maya using socket '''
maya_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
maya_socket.connect(("10.88.162.46", port))
maya_socket.send(r'print"\nConnected to Python ...\n";')
return maya_socket
#------------------------------ Connecting to Maya and sending Mel Commands -------------------
# Maya Thread
maya_process = maya_thread().start()
# Maya Socket
maya_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#---------------------------------------------------------------
# Loop to wait till Maya is Invoked
# Port specified in runCommandPortInMaya.mel file
port = 5055
while 1:
try:
maya_socket.connect((socket.gethostname(), port))
break
except:
continue
# Loop to wait till Maya is up and fully running
while 1:
#maya can only send 4096 bytes through socket
data = maya_socket.recv(4096)
data = data.replace("\n","")
#print data
tokens = data.split(" : ")
# if preferences are saved in maya, then Maya is fully loaded with all plugins
if re.search("Preferences saved.",data):
break
#---------------------------------------------------------------
#("Maya is Running...\n")