Level One Magic
"Any sufficiently advanced technology is indistinguishable from magic." - Arthur C. Clarke
exportTexturePieces.js
///////////////////////////////////////////////////////////////////////////////
// Function: height2Normals
// Usage: execute the xNormal plugin Height2Normals
// Input: none
// Return: none
///////////////////////////////////////////////////////////////////////////////
function height2Normals()
{
    var h2n_call = stringIDToTypeID("DAC3798E-E2D1-4353-BAFD-986D4766FADD")
    var desc3 = new ActionDescriptor()
    // Source Channel: R
    desc3.putEnumerated( charIDToTypeID("hSrc"), charIDToTypeID("THei"), charIDToTypeID("thrr") )
    desc3.putBoolean( charIDToTypeID("nSrc"), true ) // Normalize Source
    desc3.putBoolean( charIDToTypeID("bias"), false ) // Bias
    desc3.putBoolean( charIDToTypeID("phiA"), false )
    desc3.putBoolean( charIDToTypeID("atil"), false ) // Allow Tiling
    desc3.putDouble( charIDToTypeID("XMoo"), 0.600000 ) // Smoothing
    // Method: 4Samples
    desc3.putEnumerated( charIDToTypeID("XMet"), charIDToTypeID("XTMe"), charIDToTypeID("4sam") )
    // Swizzle R: X+
    desc3.putEnumerated( charIDToTypeID("kswX"), charIDToTypeID("TSwi"), charIDToTypeID("swXp") )
    // Swizzle G: Y+
    desc3.putEnumerated( charIDToTypeID("kswY"), charIDToTypeID("TSwi"), charIDToTypeID("swYp") )
    // Swizzle B: Z-
    desc3.putEnumerated( charIDToTypeID("kswZ"), charIDToTypeID("TSwi"), charIDToTypeID("swZp") )
    executeAction( h2n_call, desc3, DialogModes.NO )
}

///////////////////////////////////////////////////////////////////////////////
// Function: stripLayers
// Usage: remove all layers from a document except one
// Input: a document, the name of a layer to keep
// Return: none
///////////////////////////////////////////////////////////////////////////////
function stripLayers(docName, layerToKeep)
{
    while(docName.layerSets.length > 0)
    {
        docName.layerSets[0].remove()
    }
    var i = 0
    while(docName.artLayers.length > 1)
    {
        if(docName.artLayers[i].name == layerToKeep)
        {
            i++
        }
        docName.artLayers[i].remove()
    }
}

///////////////////////////////////////////////////////////////////////////////
// Function: flattenAndSave
// Usage: flatten all layers into a single layer and save the file
// Input: the name of a layer set to keep, the path to the save file
// Return: none
///////////////////////////////////////////////////////////////////////////////
function flattenAndSave(group, filePath)
{
    try
    {
        var newDoc = app.activeDocument.duplicate()
        var mergeLayer = newDoc.layerSets.getByName(group).merge()
        newDoc.bitsPerChannel = BitsPerChannelType.EIGHT
        stripLayers(newDoc, group)
        var saveFile = new File(filePath + ".png")
        savePNG(saveFile)
        newDoc.close(SaveOptions.DONOTSAVECHANGES)
    } 
    catch(e) 
    { }
}

///////////////////////////////////////////////////////////////////////////////
// Function: normalAndSave
// Usage: flatten all layers into a single layer, execute Height2Normals and save the file
// Input: the name of a layer set to keep, the path to the save file
// Return: none
///////////////////////////////////////////////////////////////////////////////
function normalAndSave(group, filePath)
{
    try
    {
        var newDoc = app.activeDocument.duplicate()
        var mergeLayer = newDoc.layerSets.getByName(group).merge()
        newDoc.bitsPerChannel = BitsPerChannelType.EIGHT
        stripLayers(newDoc, group)
        height2Normals()
        var saveFile = new File(filePath + ".png")
        savePNG(saveFile)
        newDoc.close(SaveOptions.DONOTSAVECHANGES)
    }
    catch(e)
    { }
}

///////////////////////////////////////////////////////////////////////////////
// Function: savePNG
// Usage: save the active document as a PNG
// Input: the file to save to
// Return: none
///////////////////////////////////////////////////////////////////////////////
function savePNG(saveFile)

    if(saveFile.exists) 
        saveFile.remove(); 
    pngSaveOptions = new PNGSaveOptions(); 
    activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE); 


///////////////////////////////////////////////////////////////////////////////
// Function: saveTGA
// Usage: save the active document as a TGA
// Input: the file to save to
// Return: none
///////////////////////////////////////////////////////////////////////////////
function saveTGA(saveFile)

    if(saveFile.exists) 
        saveFile.remove(); 
    tgaSaveOptions = new TGASaveOptions(); 
    activeDocument.saveAs(saveFile, tgaSaveOptions, true, Extension.LOWERCASE); 


///////////////////////////////////////////////////////////////////////////////
// Function: main
// Usage: the main function to call
// Input: none
// Return: none
///////////////////////////////////////////////////////////////////////////////
function main()
{
    var docName = app.activeDocument.name.replace(/\.[^\.]+$/, ''); 
    var docExt = decodeURI(app.activeDocument.name).replace(/^.*\./,''); 
    if(docExt.toLowerCase() != 'psd') 
        return; 
    var docPath = app.activeDocument.path; 
    docPath = docPath + "/" + docName
    flattenAndSave("Diffuse", docPath + "_col")
    flattenAndSave("Height", docPath + "_bmp")
    normalAndSave("Height", docPath + "_nml")
    flattenAndSave("Specular", docPath + "_spc")
    flattenAndSave("Glow", docPath + "_glo")
}

main()

Back to Main

© 2003-2013 Andrew Kelts