ScanMayaFile.py

'''
This Python script will check for naming convention errors in
all mayaAscii files in given director path.
wxPython must be installed for GUI of the Script.
'''
# ScanMayaFile.py
import os
import wx
import re
from mayaFileScanBaseClass import mayaFileScanBaseClass

class mayaScanUI(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(900, 700))
        self.SetIcon(wx.Icon('M:/212p-asset_lib/mel/icons/mickey_small.bmp',\
        wx.BITMAP_TYPE_ICO))
        panel = wx.Panel(self, -1)

        font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
        font.SetPointSize(9)

        vbox = wx.BoxSizer(wx.VERTICAL)

        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        st1 = wx.StaticText(panel, -1, 'Directory :')
        st1.SetFont(font)
        hbox1.Add(st1, 0, wx.RIGHT, 8)
        self.tc1 = wx.TextCtrl(panel, -1,size=(100,22))
        hbox1.Add(self.tc1, 2)
        btn1 = wx.Button(panel, 101, 'Browse', size=(50, 23))
        hbox1.Add(btn1,0)
        vbox.Add(hbox1, 0, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 10)

        #vbox.Add((-1, 2))

        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        st2 = wx.StaticText(panel, -1, 'Log File  :')
        st2.SetFont(font)
        hbox2.Add(st2, 0, wx.RIGHT, 8)
        self.tc2 = wx.TextCtrl(panel, -1,size=(100,22))
        hbox2.Add(self.tc2, 2)
        btn2 = wx.Button(panel, 102, 'Browse', size=(50, 23))
        hbox2.Add(btn2,0)
        vbox.Add(hbox2, 0, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 10)

        vbox.Add((-1, 10))

        hbox4 = wx.BoxSizer(wx.HORIZONTAL)
        st3 = wx.StaticText(panel, -1, 'Error Log')
        st3.SetFont(font)
        hbox4.Add(st3, 0)
        vbox.Add(hbox4, 0, wx.LEFT | wx.TOP, 10)

        vbox.Add((-1, 10))

        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        self.tc_txt = wx.TextCtrl(panel, -1, style=wx.TE_MULTILINE)
        hbox3.Add(self.tc_txt, 1, wx.EXPAND)
        vbox.Add(hbox3, 1, wx.LEFT | wx.RIGHT | wx.EXPAND, 10)

        vbox.Add((-1, 25))

        vbox.Add((-1, 25))

        hbox5 = wx.BoxSizer(wx.HORIZONTAL)
        btn_scn = wx.Button(panel, 104, 'Scan', size=(70, 30))
        hbox5.Add(btn_scn, 0)
        btn_cl = wx.Button(panel, 103, 'Close', size=(70, 30))
        hbox5.Add(btn_cl, 0, wx.LEFT | wx.BOTTOM , 5)
        vbox.Add(hbox5, 0, wx.ALIGN_RIGHT | wx.RIGHT, 10)

        panel.SetSizer(vbox)
        self.Centre()
        self.Show(True)
        self.Bind(wx.EVT_BUTTON, self.OnClose, id=103)
        self.Bind(wx.EVT_BUTTON, self.opendir, id=101)
        self.Bind(wx.EVT_BUTTON, self.openfile,id=102)
        self.Bind(wx.EVT_BUTTON, self.scan_mayafile,id=104)

    def OnClose(self,event):
        self.Close()
    def opendir(self, event):
        dlg = wx.DirDialog(self, "Choose a directory:",\
        style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        if dlg.ShowModal() == wx.ID_OK:
            self.tc1.AppendText(dlg.GetPath())
        dlg.Destroy()
    def openfile(self, event):
        dlg = wx.FileDialog(self, "Save Log File", os.getcwd(), "", "*.txt", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            mypath = os.path.basename(path)
            self.tc2.AppendText(path)
        dlg.Destroy()
    def scan_mayafile(self,event):
        '''Scans All Maya Files'''
        basedir = self.tc1.GetValue()
        logfile = self.tc2.GetValue()
        epnames=['Prefix_']

        for root,dirs,files in os.walk(basedir):
            for f in files:
                if(f.endswith('.ma')):
                    st =('\n\n-------------------------------------------------------------\n'+\
                    'Scanning File : '+os.path.join(root,f)+'\n--------------------------------\
                    -----------------------------'+'\n\n')
                    self.tc_txt.AppendText(st)
                    file = mayaFileScanBaseClass(os.path.join(root,f))
                    nodenames = file.fileScan()
                    for n in nodenames:
                        st =('Incorrect Node Name : '+n+'\n')
                    ftnpaths = file.fileScan_ftn()
                    if len(ftnpaths) > 0:
                        for ftn in ftnpaths:
                            if os.path.exists(ftn):
                                sub = ftn.split('/')
                                substr = sub[len(sub)-1]
                                i = 0
                                for j in epnames:
                                    if substr.startswith(j):
                                        i = 1
                                        start = j
                                        break
                                if i==0:
                                    st =('File Name Wrong : '+substr+'\n')
                                    self.tc_txt.AppendText(st)
                                else:
                                    substr2 = substr.strip(start)
                                    if len(re.findall('\W',substr2[0:-4])) > 0:
                                        st =('Illegal Characters : '+substr+'\n')
                                        self.tc_txt.AppendText(st)
                            else:
                                st =('Missing File : '+ftn+'\n')
                                self.tc_txt.AppendText(st)
        #log.write('\n------------------------------------------------------------------\n')
        self.tc_txt.AppendText('\n--------------------End of Scanning\
        ----------------------------------------------\n')
        if logfile != '':
            log = open(logfile,'w')
            log.write(self.tc_txt.GetValue())
            log.close()

app = wx.App()
mayaScanUI(None, -1, 'Scan mayaFile for Shader/Texture Network')
app.MainLoop()

mayaFileScanBaseClass.py

# Definition of Class to scan given Maya Ascii File
import re
import os

class mayaFileScanBaseClass:
    def __init__(self,filename):
        self.filename = filename
    def fileScan(self):
        '''Scans File for 'createNode' and looks for shading nodes'''
        fid = open(self.filename)
        output = []
        epnames=['Prefix_']
        for line in fid:
            if line.startswith('createNode'):
                words = line.split(' ')
                nodeTypes = ['lambert','shadingEngine','materialInfo','bump2d',\
                'bump3d','blinn','file','place2dTexture','place3dTexture',\
                'anisotropic','rampShader','phong','ramp','blendColors',\
                'surfaceLuminance','clearCoat','layeredTexture','samplerInfo']
                for n in nodeTypes:
                    if words[1]==n:
                        names = line.split('-n ')
                        name = names[1].split('"')
                        namechk = name[1].replace(words[1],'')
                        if(not namechk.islower()):
                            output.append(name[1])
                        else:
                            i = 0
                            for j in epnames:
                                if(name[1].startswith(j)):
                                    i = 1
                                    break
                            if i==0:
                                output.append(name[1])
                            else:
                                if len(re.findall('\W',name[1])) > 0:
                                    output.append(name[1])
        fid.close()
        return output
    def fileScan_ftn(self):
        '''Scans File for ftn nodeType and does texture\
        name checking and check if texture file exists '''
        fid = open(self.filename)
        paths = []
        for line in fid:
            if re.compile('.ftn').search(line,1):
                path = line[32:-3]
                paths.append(path)
        return paths

First Python Script for Production

# rename all files and folders
# renameFiles.py
import os
from time import strftime,localtime

'''
Rename All File and Folders
'''
def renameAll(f,root):
    fpath = os.path.join(root,f)
    if(f.startswith('212p-999')):
        n = f.replace('212p-999','212p-998')
        npath = os.path.join(root,n)
        os.renames(fpath,npath)
        print 'Renamed : '+fpath
        l.write('\n'+'Renamed : '+fpath)
    else:
        print 'Skipped : '+fpath
        l.write('\n'+'Skipped : '+fpath)

'''
Search and Replace string inside Maya Files
'''
def findReplaceFile(fpath):
    print fpath
    l.write('\n'+'----------------------------------------------------------------------------------------------------------')
    l.write('\n'+fpath)
    print "Start Time : " + strftime("%H:%M:%S",localtime())
    l.write('\n'+"Start Time : " + strftime("%H:%M:%S",localtime()))
    filein = open(fpath,'r')
    inputstr = filein.read()
    filein.close()

    outputstr=inputstr.replace('212p-999','212p-998')
    outputstr=outputstr.replace('p212_999','p212_998')
    outputstr=outputstr.replace('212p-201_Suitcase.ma','212p-201_suitcase.ma')
    fileout=open(fpath,'w')
    fileout.write(outputstr)
    fileout.close()
    print ("End Time : " + strftime("%H:%M:%S",localtime())+'\n')
    l.write('\n'+"End Time : " + strftime("%H:%M:%S",localtime()))

'''
Program Starts Here
'''

basedir=raw_input('Enter Root Path : ')
logdir=raw_input('Enter Log Path : ')

logfile = os.path.join(logdir,'logfile1.txt')

l=open(logfile,'w')
l.write('Rename starts'+'\n'+'------------------------'+'\n'+strftime("%H:%M:%S",localtime())+'\n'+'\n')
for root,dirs,files in os.walk(basedir):
    for f in files:
        renameAll(f,root)

print '------------------------------------'
l.write('\n'+'---------------------------------------------------------------')
for root,dirs,files in os.walk(basedir,topdown=False):
    for d in dirs:
        renameAll(d,root)

print '\n'+'All Files & Folders Renamed'
l.write('\n'+'\n'+'Files & Folders Renamed')
l.write('\n'+'---------------------------------------------------------------'+'\n\n\n\n\n\n\n')
l.write('\n'+'Search and Replace inside Maya Files'+'\n-------------------------------------------\n')

filec = 0

for root,dirs,files in os.walk(basedir):
    for f in files:
        fpath=os.path.join(root, f)
        if(f.endswith('.ma')):
            findReplaceFile(fpath)
            filec+=1

print '\n Total Maya Files Restored : ', filec

l.write('\n'+'------------------'+'\n'+'Rename ends'+'\n'+strftime("%H:%M:%S",localtime())+'\n')
l.close()