#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# # ~bmConvertDDS(Maya).py~ # # AUTHOR: Brian Mumm # # UPDATED: July 2009 # # DESCRIPTION: 1. Opens all psd's in folder # 2. Exports each layer to DDS # 3. Changes the maya files to use the new dds files # # #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# import win32com.client import os # Will not work unless below is done: # Make sure to have the PYTHONPATH point to Maya/python/lib/site-packages # Also MAYA_LOCATION points to the Maya install folder import maya.standalone import maya.cmds as cmds from bmMail import * # Change to match your root folder psdRoot = r'C:\\IRAD\\SVN\\Art\\3D\\Characters\\Villain' # Map of layer group name strings and the suffix to use when exporting exportTypes = ('dxt1', 'dxt3', 'dxt5' ) def SaveDDS(objApp, pName, pType): id3 = objApp.CharIDToTypeID( "save" ) desc2 = win32com.client.Dispatch( "Photoshop.ActionDescriptor" ) id4 = objApp.CharIDToTypeID( "As " ) desc3 = win32com.client.Dispatch( "Photoshop.ActionDescriptor" ) id5 = objApp.CharIDToTypeID( "barF" ) desc3.PutBoolean( id5, True ) id6 = objApp.CharIDToTypeID( "fdev" ) desc3.PutDouble( id6, 3.000000 ) id7 = objApp.CharIDToTypeID( "fbia" ) desc3.PutDouble( id7, 0.000000 ) id8 = objApp.CharIDToTypeID( "urad" ) desc3.PutDouble( id8, 5.000000 ) id9 = objApp.CharIDToTypeID( "uamo" ) desc3.PutDouble( id9, 0.500000 ) id10 = objApp.CharIDToTypeID( "uthr" ) desc3.PutDouble( id10, 0.000000 ) id11 = objApp.CharIDToTypeID( "xstf" ) desc3.PutDouble( id11, 1.000000 ) id12 = objApp.CharIDToTypeID( "xthf" ) desc3.PutDouble( id12, 1.000000 ) id13 = objApp.CharIDToTypeID( "qual" ) desc3.PutInteger( id13, 71 ) id14 = objApp.CharIDToTypeID( "erdi" ) desc3.PutBoolean( id14, False ) id15 = objApp.CharIDToTypeID( "erdw" ) desc3.PutInteger( id15, 1 ) id16 = objApp.CharIDToTypeID( "usfa" ) desc3.PutBoolean( id16, False ) #Make sure its the right compression if the group is not in PS send a email. id17 = objApp.CharIDToTypeID( "txfm" ) if pType == "dxt1": desc3.PutInteger( id17, 0 ) elif pType == "dxt3": desc3.PutInteger( id17, 2) elif pType == "dxt5": desc3.PutInteger( id17, 3) else: mail('brianmumm@gmail.com','DDS type failure!','The following file ' + pName + ' the compression type got messed up. Go see ' + os.getenv('COMPUTERNAME')) id18 = objApp.CharIDToTypeID( "weig" ) desc3.PutInteger( id18, 0 ) id19 = objApp.CharIDToTypeID( "tmty" ) desc3.PutInteger( id19, 0 ) id20 = objApp.CharIDToTypeID( "mmty" ) desc3.PutInteger( id20, 32 ) id21 = objApp.CharIDToTypeID( "smip" ) desc3.PutInteger( id21, 0 ) id22 = objApp.CharIDToTypeID( "bina" ) desc3.PutBoolean( id22, False ) id23 = objApp.CharIDToTypeID( "prem" ) desc3.PutBoolean( id23, False ) id24 = objApp.CharIDToTypeID( "film" ) desc3.PutBoolean( id24, False ) id25 = objApp.CharIDToTypeID( "alpb" ) desc3.PutBoolean( id25, False ) id26 = objApp.CharIDToTypeID( "bord" ) desc3.PutBoolean( id26, False ) id27 = objApp.CharIDToTypeID( "brdr" ) desc3.PutDouble( id27, 0.000000 ) id28 = objApp.CharIDToTypeID( "brdg" ) desc3.PutDouble( id28, 0.000000 ) id29 = objApp.CharIDToTypeID( "brdb" ) desc3.PutDouble( id29, 0.000000 ) id30 = objApp.CharIDToTypeID( "mmft" ) desc3.PutInteger( id30, 2 ) id31 = objApp.CharIDToTypeID( "fdcl" ) desc3.PutBoolean( id31, False ) id32 = objApp.CharIDToTypeID( "fdaf" ) desc3.PutBoolean( id32, False ) id33 = objApp.CharIDToTypeID( "f2rl" ) desc3.PutDouble( id33, 0.500000 ) id34 = objApp.CharIDToTypeID( "f2gl" ) desc3.PutDouble( id34, 0.500000 ) id35 = objApp.CharIDToTypeID( "f2bl" ) desc3.PutDouble( id35, 0.500000 ) id36 = objApp.CharIDToTypeID( "f2al" ) desc3.PutDouble( id36, 0.500000 ) id37 = objApp.CharIDToTypeID( "fddl" ) desc3.PutInteger( id37, 0 ) id38 = objApp.CharIDToTypeID( "fafm" ) desc3.PutDouble( id38, 0.150000 ) id39 = objApp.CharIDToTypeID( "bafh" ) desc3.PutDouble( id39, 0.500000 ) id40 = objApp.CharIDToTypeID( "dthc" ) desc3.PutBoolean( id40, False ) id41 = objApp.CharIDToTypeID( "dth0" ) desc3.PutBoolean( id41, False ) id42 = objApp.CharIDToTypeID( "smth" ) desc3.PutInteger( id42, 0 ) id43 = objApp.CharIDToTypeID( "filg" ) desc3.PutDouble( id43, 2.200000 ) id44 = objApp.CharIDToTypeID( "fieg" ) desc3.PutBoolean( id44, False ) id45 = objApp.CharIDToTypeID( "filw" ) desc3.PutDouble( id45, 10.000000 ) id46 = objApp.CharIDToTypeID( "over" ) desc3.PutBoolean( id46, False ) id47 = objApp.CharIDToTypeID( "fblr" ) desc3.PutDouble( id47, 1.000000 ) id48 = objApp.CharIDToTypeID( "nmcv" ) desc3.PutBoolean( id48, False ) id49 = objApp.CharIDToTypeID( "ncnv" ) desc3.PutInteger( id49, 1009 ) id50 = objApp.CharIDToTypeID( "nflt" ) desc3.PutInteger( id50, 1040 ) id51 = objApp.CharIDToTypeID( "nmal" ) desc3.PutInteger( id51, 1034 ) id52 = objApp.CharIDToTypeID( "nmbr" ) desc3.PutBoolean( id52, False ) id53 = objApp.CharIDToTypeID( "nmix" ) desc3.PutBoolean( id53, False ) id54 = objApp.CharIDToTypeID( "nmiy" ) desc3.PutBoolean( id54, False ) id55 = objApp.CharIDToTypeID( "nmiz" ) desc3.PutBoolean( id55, False ) id56 = objApp.CharIDToTypeID( "nmah" ) desc3.PutBoolean( id56, False ) id57 = objApp.CharIDToTypeID( "nswp" ) desc3.PutBoolean( id57, False ) id58 = objApp.CharIDToTypeID( "nmsc" ) desc3.PutDouble( id58, 2.200000 ) id59 = objApp.CharIDToTypeID( "nmnz" ) desc3.PutInteger( id59, 0 ) id60 = objApp.CharIDToTypeID( "usbi" ) desc3.PutBoolean( id60, False ) id61 = objApp.CharIDToTypeID( "lien" ) desc3.PutBoolean( id61, False ) id62 = objApp.CharIDToTypeID( "shdi" ) desc3.PutBoolean( id62, False ) id63 = objApp.CharIDToTypeID( "shfi" ) desc3.PutBoolean( id63, False ) id64 = objApp.CharIDToTypeID( "shmm" ) desc3.PutBoolean( id64, True ) id65 = objApp.CharIDToTypeID( "shan" ) desc3.PutBoolean( id65, True ) id66 = objApp.CharIDToTypeID( "clrc" ) desc3.PutInteger( id66, 0 ) id67 = objApp.CharIDToTypeID( "vdx1" ) desc3.PutBoolean( id67, True ) id68 = objApp.CharIDToTypeID( "vdx2" ) desc3.PutBoolean( id68, True ) id69 = objApp.CharIDToTypeID( "vdx3" ) desc3.PutBoolean( id69, True ) id70 = objApp.CharIDToTypeID( "vdx5" ) desc3.PutBoolean( id70, True ) id71 = objApp.CharIDToTypeID( "v444" ) desc3.PutBoolean( id71, True ) id72 = objApp.CharIDToTypeID( "v555" ) desc3.PutBoolean( id72, True ) id73 = objApp.CharIDToTypeID( "v565" ) desc3.PutBoolean( id73, True ) id74 = objApp.CharIDToTypeID( "v888" ) desc3.PutBoolean( id74, True ) id75 = objApp.CharIDToTypeID( "alph" ) desc3.PutBoolean( id75, False ) id76 = objApp.CharIDToTypeID( "usra" ) desc3.PutBoolean( id76, False ) id77 = objApp.CharIDToTypeID( "usfs" ) desc3.PutInteger( id77, 0 ) id78 = objApp.CharIDToTypeID( "prev" ) desc3.PutBoolean( id78, False ) id79 = objApp.CharIDToTypeID( "rdep" ) desc3.PutInteger( id79, 3000 ) id80 = objApp.CharIDToTypeID( "lomm" ) desc3.PutBoolean( id80, False ) id81 = objApp.CharIDToTypeID( "scar" ) desc3.PutDouble( id81, 1.000000 ) id82 = objApp.CharIDToTypeID( "scag" ) desc3.PutDouble( id82, 1.000000 ) id83 = objApp.CharIDToTypeID( "scab" ) desc3.PutDouble( id83, 1.000000 ) id84 = objApp.CharIDToTypeID( "scaa" ) desc3.PutDouble( id84, 1.000000 ) id85 = objApp.CharIDToTypeID( "biar" ) desc3.PutDouble( id85, 0.000000 ) id86 = objApp.CharIDToTypeID( "biag" ) desc3.PutDouble( id86, 0.000000 ) id87 = objApp.CharIDToTypeID( "biab" ) desc3.PutDouble( id87, 0.000000 ) id88 = objApp.CharIDToTypeID( "biaa" ) desc3.PutDouble( id88, 0.000000 ) id89 = objApp.CharIDToTypeID( "siar" ) desc3.PutDouble( id89, 1.000000 ) id90 = objApp.CharIDToTypeID( "siag" ) desc3.PutDouble( id90, 1.000000 ) id91 = objApp.CharIDToTypeID( "siab" ) desc3.PutDouble( id91, 1.000000 ) id92 = objApp.CharIDToTypeID( "siaa" ) desc3.PutDouble( id92, 1.000000 ) id93 = objApp.CharIDToTypeID( "biir" ) desc3.PutDouble( id93, 0.000000 ) id94 = objApp.CharIDToTypeID( "biig" ) desc3.PutDouble( id94, 0.000000 ) id95 = objApp.CharIDToTypeID( "biib" ) desc3.PutDouble( id95, 0.000000 ) id96 = objApp.CharIDToTypeID( "biia" ) desc3.PutDouble( id96, 0.000000 ) id97 = objApp.CharIDToTypeID( "outw" ) desc3.PutBoolean( id97, False ) id98 = objApp.CharIDToTypeID( "clcL" ) desc3.PutBoolean( id98, True ) id99 = objApp.StringIDToTypeID( "NVIDIA D3D/DDS" ) desc2.PutObject( id4, id99, desc3 ) id100 = objApp.CharIDToTypeID( "In " ) desc2.PutPath( id100, pName ) # Flatten the image before exporting id101 = objApp.CharIDToTypeID( "FltI" ) objApp.ExecuteAction( id101 ) # Export to DDS objApp.ExecuteAction( id3, desc2, 3 ) ### MAIN ### if (__name__ == '__main__'): # COM dispatch for Photoshop psApp = win32com.client.Dispatch('Photoshop.Application') # Initialize Maya python maya.standalone.initialize( name='python' ) # Get all PSDs under root dir psdFiles = [] for root, dir, files in os.walk(psdRoot): for thisFile in files: if (thisFile.lower().endswith('.psd')): fullFilename = os.path.join(root, thisFile) psdFiles.append(fullFilename) # Loop through PSDs we found. for psdFile in psdFiles: doc = psApp.Open(psdFile) layerSets = doc.LayerSets if (len(layerSets) > 0): # First hide all root-level layers for layer in doc.Layers: layer.Visible = False # ... and layerSets for layerSet in layerSets: layerSet.Visible = False # Loop through each LayerSet (aka Group) for layerSet in layerSets: lsName = layerSet.Name.lower() if (lsName in exportTypes): layerSet.Visible = True # make visible again # Make our export filename ddsFile = os.path.splitext(psdFile)[0] + '.dds' # If PNG exists but older than PSD, delete it. if (os.path.exists(ddsFile)): psdTime = os.stat(psdFile)[8] ddsTime = os.stat(ddsFile)[8] if (psdTime > ddsTime): os.remove(ddsFile) # Export .DDS for this layer Group if (not os.path.exists(ddsFile)): doc = psApp.Open(psdFile) SaveDDS(psApp,ddsFile,lsName) print 'exporting:', ddsFile # Now set all the Maya file to use the .dds just exported mayaFile = ddsFile.split('_')[0] + '.mb' if not os.path.exists(mayaFile): mail('brianmumm@gmail.com','Maya file failure!','The following file ' +mayaFile + ' does not exsist make sure the artist: ' + os.getenv('COMPUTERNAME') + ' named the texture and maya file the same.') print "Error: %s doesnot exist." % mayaFile raise SystemExit # Open the file cmds.file(mayaFile, force=True, open=True ) # Get all file nodes in the scene fileNodes = cmds.ls(type="file", long=True) # Check and find the old psd and replace it with the new dds that was exported for fileNode in fileNodes: # Get the texture file name fileNode = fileNode + '.fileTextureName' texFile= cmds.getAttr(fileNode) # Remove the path only need the file name texFile = os.path.split(texFile)[1] psdFile = os.path.split(psdFile)[1] # Check see if its the correct file if it is then replace if(texFile == psdFile): cmds.setAttr( fileNode, ddsFile,type='string' ) # Save the file cmds.file(save=True, force=True) else: print 'skipping newer file:', psdFile # Make LayerSet invisible again layerSet.Visible = False else: mail('brianmumm@gmail.com','DDS group failure!','The following file ' + psdFile + ' didn\'t have a correctly named PS group. Group name: ' + lsName + ' Go see ' + os.getenv('COMPUTERNAME')) else: mail('brianmumm@gmail.com','DDS group failure!','The following file ' + psdFile + ' didn\'t have a group. Go see ' + os.getenv('COMPUTERNAME')) # Close PSD without saving doc.Close(2)