Python- Updating alpha from a group
I've tied the alpha updating to an action inside photoshop, and the results are pretty pleasing. Within the script there is a bit of juggling between active layers and active channels in order to get the action working properly.
I also like to leave the user off in a place that makes sense- in this case, the alpha group, since its the one being updated.
The basics of this script:
Checks for an 'alpha' group in the active PSD file. If present, copies it's merged contents to the alpha channel.
I also like to leave the user off in a place that makes sense- in this case, the alpha group, since its the one being updated.
The basics of this script:
Checks for an 'alpha' group in the active PSD file. If present, copies it's merged contents to the alpha channel.
##############################################################################
#
# Photoshop alpha updater
# (C) Pete Hanshaw, 2012
# http://peterhanshawart.blogspot.com.au/
#
##############################################################################
#
# Checks for an 'alpha' group in the active PSD file. If present, copies it's
# contents to the alpha channel.
#
# Requires the Win32 Extensions:
# http://python.net/crew/mhammond/win32/
#
##############################################################################
#Import required modules
from win32com.client.dynamic import Dispatch
from stat import *
import os
from sys import exit
import pythoncom
#Define the name of the Alpha group here
alpha_group = 'alpha'
#Begin the script
if (__name__ == '__main__'):
#COM dispatch for Photoshop
try:
psApp = Dispatch('Photoshop.Application')
except:
os.system("color 0c")
print "Photoshop dispatch failed"
os.system("PAUSE")
exit(0)
#Get the currently active document
try:
doc = psApp.activeDocument
#Gracefully end if a document is not open
except:
os.system("color 0c")
print "You need to have an active Photoshop Doc to use this script."
os.system("PAUSE")
exit(0)
#Get information about our current doc
activeChannels = doc.ActiveChannels
layerSets = doc.LayerSets
channelRef = doc.Channels
#Check if there are any layerSets in the current doc
if (len(layerSets) > 0):
#Assume there is no alpha
alphaExists = False
#Check for an alpha channel
for channel in channelRef:
if channel.name == "Alpha 1":
alphaExists = True
break
if alphaExists == False:
#add a blank alpha channel
doc.Channels.Add()
#save the current setup
saved_state = doc.activeHistoryState
#Prepare the alpha group for duplication
doc.activeChannels = activeChannels
doc.activeLayer = (doc.layerSets[alpha_group])
doc.activeLayer.Visible = True
doc.activeLayer.Merge()
doc.activeLayer.Copy()
#Now that we have a merged copy, revert the merging
doc.activeHistoryState = saved_state
#Now paste the merged alpha source into the alpha channel
doc.activeLayer = (doc.artLayers['Background'])
doc.activeChannels = [doc.Channels[5]]
doc.Paste()
#Restore control to the user
doc.selection.deselect
doc.activeChannels = activeChannels
doc.activeLayer = (doc.layerSets[alpha_group])
print "All done!"
else:
os.system("color 0c")
print "Doc has no groups."
os.system("PAUSE")
exit(0)
Comments
Post a Comment