Channel Swizzle Script

Following on from my last blog post, I’ve finished most of my channel swizzle scripts. These generate a file with layers for each channel, then export as a new file with the layers combined into one layer as channels. I channel pack all my VFX textures so it will make my workflow a lot faster!

12

If you want to use this, feel free to save it as a .jsx file in C:\Program Files\Adobe\Adobe Photoshop CC 2019\Presets\Scripts, but I’d appreciate if you drop me a comment to let me know. Code below if you want to skip my rambling!

I had a couple of issues related to understanding the class hierarchy – for example, I thought that I could save select all as an object, and then perform copy on that object, but actually I needed to have a selection object, of which selectAll and copy are both methods – but I got there in the end!

I had a very odd “has no constructor” error when trying to make a new SolidColor instance, but the only way I solved it was by copying what looked like the same code from the scripting reference documents. I have no idea why mine didn’t work – an extra space or bad character perhaps? I haven’t found as many resources though google/stack overflow as I normally find for python and maya related topics.

EDIT: The no constructor error comes from forgetting to set Photoshop as your connected target application – just a silly mistake!

This one does currently save as a psd in the same location, with the same name, so will write over the source file. I’m not sure exactly what I want to do in terms of work file and export file right now, so I’ll change that once I’ve decided on a nice workflow.

Also, found a nice way to preserve the whitespace when copy pasting code onto this blog – the <pre></pre> HTML tag allows the text to be pasted exactly as is, which is super helpful and makes it much more readable.

function main()
{
	var currentDoc = app.activeDocument 

	//make duplicate
	fname = currentDoc.name.split(".")[0]
	newDoc = currentDoc.duplicate(fname)

	swizzle(newDoc)
	savePSD(currentDoc, newDoc)
}

function swizzle(doc)
{
	//make new layer to recive contents
	newLayer = doc.artLayers.add()
	newLayer.kind = LayerKind.NORMAL
	newLayer.name = "Channel Swizzle"
	
	//Fill layer with white
	doc.selection.selectAll()
	var myWhite = new SolidColor()
	myWhite.rgb.red = 255
	myWhite.rgb.green = 255
	myWhite.rgb.blue = 255
	doc.selection. fill(myWhite)
	
	var channels = ["Red", "Green", "Blue"]
	var allLayers = doc.artLayers
	var i 
	
	for (i = 0; i < channels.length; i++)
	{
		var myChannel = doc.channels.getByName(channels[i])
		var myLayer = allLayers.getByName(channels[i])
		doc.activeLayer = myLayer
		//select all
		doc.selection.selectAll()
		//copy without merging
		doc.selection.copy()
		//set active layer
		doc.activeLayer = newLayer
		//select channel
		doc.activeChannels = [myChannel]
		//paste
		doc.paste() //needs a try catch for if there's nothing on the layer
	}

	//put active channel back to all channels
	doc.activeChannels = doc.componentChannels
}

function savePSD(currentDoc, newDoc)
{
	//set png settings and file path
	var psdSettings = new PhotoshopSaveOptions()
	psdSettings.alphaChannels = false
	psdSettings.annotations = false
	psdSettings.embedColorProfile = true
	psdSettings.layers = false
	psdSettings.spotColors = false

	//set file path - careful not to echo anything in built here!
	filePath = new File(currentDoc.path + '/' + currentDoc.name.replace(/\.[^\.]+$/, '.psd'))

	//save new doc
	newDoc.saveAs(filePath, psdSettings, true, Extension.LOWERCASE)

	//close new doc and return focus to original
	newDoc.close(SaveOptions.DONOTSAVECHANGES)
	app.activeDocument = currentDoc

}

main()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s