Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #parsing PSDs for use in Substance Designer network
- #finds masks, albedos, normals and exports out
- #importing PSD Tools
- from psd_tools import PSDImage
- #importing PSD imageops module fomr Pillow
- from PIL import Image
- import PIL.ImageOps
- #for logging
- import time
- import logging,sys
- logging.basicConfig(stream = sys.stderr, level = logging.WARNING)
- #for file directory navigation
- from os import path
- from os import listdir
- from os import mkdir
- #math
- import math
- class locations():
- """Storing locations in this class"""
- def __init__(self,*args):
- self.sourceImages = " "
- self.exportImages= " "
- def sourceDirectory(self):
- """Setting location for where PSDs are located"""
- sourceImages = ("c:\\Telltale\\ArtData\\Textures\\WalkingDead3\\PSDs\\")
- #sourceImages = ("c:\\test\\PSDs\\")
- allFiles = [f for f in listdir(sourceImages) if path.isfile(path.join(sourceImages,f))]
- return (sourceImages,allFiles)
- def exportDirectory(self):
- """Setting location for where TGAs are located"""
- exportImages = ("c:\\Telltale\\ArtData\\Textures\\WalkingDead3\\")
- #exportImages = ("c:\\test\\PSDs\\export\\")
- if not path.exists(exportImages):
- logging.info ("Can't find this path! Creating...")
- mkdir (exportImages)
- else:
- logging.info ("Export path found...")
- return (exportImages)
- class psdExport():
- def psdFiles(self):
- """Return list of only PSDs defined in locations.sourceDirectory"""
- psdfiles =[]
- psdList = locations()
- for fileExtensions in psdList.sourceDirectory():
- for files in fileExtensions:
- if files.endswith(".psd"):
- psdfiles.append(files)
- return (psdfiles)
- def psdProcess(self,logFileName):
- """Save out PSDs into seperate TGAs for SBS Designer to process"""
- listPSD=psdExport()
- directory = locations()
- log = open(logFileName, "a")
- PSDdir = directory.sourceDirectory()[0]
- TGAdir = directory.exportDirectory()
- PSDs = listPSD.psdFiles()
- fullPSDpath = [] ## I need to do this for PSD_tools
- for p in PSDs:
- p = PSDdir + p
- fullPSDpath.append(p)
- for item in fullPSDpath[:]:
- print ("Processing {0}").format(item)
- log.write(item + "\n")
- psd = PSDImage.load(item)
- exportName = path.basename(item)
- psdTime = time.clock()
- for group in psd.layers[:]:
- # if group.name == "DIF":
- # diffuse = group
- # logging.info ("Found Diffuse.. exporting")
- # try:
- # if diffuse.as_PIL().mode == "RGBA":
- # log.write("Alpha layer in the PSD:" + (exportName))
- # diffuse.as_PIL().thumbnail(size,Image.BICUBIC)
- # diffuse.as_PIL().save(TGAdir + exportName[:-4] + '.tga')
- # diffuseSuccess = ("Successfully saved out diffuse!")
- # logging.info (diffuseSuccess)
- # log.write(diffuseSuccess + "\n")
- # except IOError as e:
- # diffuseError = ("Couldn't save out diffuse, is it checked out in source control?")
- # logging.info (diffuseError)
- # log.write(diffuseError + "\n")
- # pass
- # if group.name == "NRM":
- # normal = group
- # logging.info ("Found Normal Map.. exporting")
- # size = (1024,1024)
- # try:
- # normal.as_PIL().thumbnail(size,Image.BICUBIC)
- # normal.as_PIL().save(TGAdir + exportName[:-4] + '_nm.tga')
- # normalSuccess = ("Successfully saved out normal map!")
- # logging.info (normalSuccess)
- # log.write(normalSuccess +"\n")
- # except IOError as e:
- # normalError = ("Couldn't save out normal map, is it checked out in source control?")
- # logging.info (normalError)
- # log.write(normalError +"\n")
- # pass
- if group.name == "Masks":
- for layer in group.layers[:]:
- if layer.name == "mask":
- masks = layer
- logging.info ("Found Mask.. exporting")
- size = (1024,1024)
- try:
- masks.as_PIL().thumbnail(size,Image.BICUBIC)
- masks.as_PIL().save(TGAdir + exportName[:-4] + '_mask.tga')
- maskSuccess = ("Successfully saved out mask!")
- logging.info (maskSuccess)
- log.write(maskSuccess +"\n")
- except IOError as e:
- maskError = ("Couldn't save out mask, is it checked out in source control?")
- logging.info (maskError)
- log.write(maskError +"\n")
- pass
- # if group.name == "DTL":
- # for layer in group.layers[:]:
- # if layer.name == "detail":
- # size = (1024,1024)
- # detail = layer
- # logging.info ("Found Detail Map.. exporting")
- # try:
- # detail.as_PIL().thumbnail(size,Image.BICUBIC)
- # detail.as_PIL().save(TGAdir + exportName[:-4] + '_detail.tga')
- # #Now opening and correcting the detail map
- # detail = Image.open(TGAdir + exportName[:-4] + '_detail.tga')
- # if detail.mode == 'RGBA':
- # detail = Image.merge('RGB', detail.split()[0:3])
- # detail = PIL.ImageOps.invert(detail)
- # detail.save(TGAdir + exportName[:-4]+ '_detail.tga')
- # detailSuccess = ("Successfully saved out detail map!")
- # logging.info (detailSuccess)
- # log.write(detailSuccess +"\n")
- # except IOError as e:
- # detailError = ("Couldn't save out detail map, is it checked out in source control?")
- # logging.info (detailError)
- # log.write(detailError +"\n")
- # pass
- psdFinalTime = (time.clock()-psdTime)
- log.write(("Time to export file: {0:.2f} seconds\n".format(psdFinalTime)))
- def startPSDexport():
- liststuff = psdExport()
- #just log things
- logDir = ("C:\\Telltale\\")
- logFileName = (str(time.strftime("%Y-%m-%d.%H.%M.%S")) + ".txt")
- logFileName = path.join (logDir, logFileName)
- log = open(logFileName,"a")
- fullTimer = time.clock()
- print ('Outputting logging to {0}').format(logFileName)
- #run the batch process
- liststuff.psdProcess(logFileName)
- totalTime = (time.clock()-fullTimer)
- log.write(("Time to export PSDs: {0:.2f} seconds\n".format(totalTime)))
- log.close()
- #run batch process
- startPSDexport()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement