This is the complimentary Photoshop script that is referenced in an article I wrote for BlenderArt, issue #29, called “Creating Blender icons for Adobe Flash”: Read it onlineDownload it (as PDF). Enjoy!

Code follows:

// enable double clicking from the Macintosh Finder or the Windows Explorer
#target photoshop

// debug level: 0-2 (0:disable, 1:break on error, 2:break at beginning)
// $.level = 0;
// debugger; // launch debugger on next line

// on localized builds we pull the $$$/Strings from a .dat file, see documentation for more details
//$.localize = true;

subSelectionsToLayers();

//var data = mapSelection(activeDocument,data);
// You can get the area of the selection with:
//alert(data.area);
// The "selection" is an array, so you can get it like.
//data.mapa[0] // for the first sub-path item
//data.mapa[0][0] // for the first path point in the first sub-path item, etc.

function subSelectionsToLayers()
{
    if (app.documents.length > 0)
    {
        if (app.activeDocument.activeLayer.isBackgroundLayer == false)
        {
            var ndoc = app.activeDocument;
            var lyr = ndoc.activeLayer;

            //make an array to hold points
            var points = [];

            var selBounds = Array(
                Array(0,0),
                Array(app.activeDocument.width, 0),
                Array(app.activeDocument.width, app.activeDocument.height),
                Array(0, app.activeDocument.height)
            );

            //ndoc.selection.select(selBounds);

            // make a selection
            //ndoc.selection.selectAll();

            //alert(ndoc.selection);

            // make the path
            //ndoc.selection.copy(); //makeWorkPath(1);
            ndoc.selection.makeWorkPath(1);

            var p = ndoc.pathItems["Work Path"];
            //alert ("p.subPathItems.length = " + p.subPathItems.length);

            var currentPathItem;
            var myPathsArray;
            var myPathItem;

            myPathsArray = extractSubPathInfo(p);

            //create a new path with each subPath
            for(var s=0;s<myPathsArray.length;s+=2)
            {
                //alert( myPathsArray[s].subPathItems[0].pathPoints[0].anchor[0] );
                //alert( ndoc.pathItems[0].subPathItems[s].pathPoints[0].anchor["x"] );
                //alert( ndoc.pathItems[0].subPathItems[s].pathPoints[0].anchor.x );

                alert(myPathsArray[s + 1]);

                // Keep layer active for next selection.
                ndoc.activeLayer = lyr;

                // Create a new pathItem in the document.
                myPathItem = ndoc.pathItems.add('myPath'+s, myPathsArray[s]);

                // Make selection from path.
                myPathItem.makeSelection();

                // Copy and paste the selection's content into a new layer.
                ndoc.selection.copy(true);
                var layerRef = ndoc.paste();

                // Reference the original path (myPathsArray[s+1])
                // and get the x and y values of it's top-left corner (myPathsArray[s+1][0] and myPathsArray[s+1][1]).
                layerRef.name = "trig" + myPathsArray[s+1][0] + "x" + myPathsArray[s+1][1];

                // Destroy the pathItem from the document.
                // It's not needed after this point, and this will reduce clutter.
                // If you want to save the paths, then comment out this line.
                myPathItem.remove();
            }
        }
        else
        {
            alert("Operation cannot be performed on background layer");
        }
    }
    else
    {
        alert("Create a document with an active selection before running this script!");
    }
}

function extractSubPathInfo(pathObj)
{
    // Create an array of pathItems. Each pathObj is added to a new pathItem, with only one subPathItem (itself).
    // Each element can be used as the second arugment in pathItems.add()
    // I.e., doc.pathItems.add("myPath1", pathArray[0]);
    var pathArray = new Array();
    var pl = pathObj.subPathItems.length;

    // Define the path.
    var limx;
    var limy;
    var pArray;
    var subPathArray;
    for(var s=0;s<pl;s++)
    {
        pArray = new Array();
        limx = 800;
        limy = 600;
        // Create new PathInfo item for this subPathItem
        for(var i=0;i<pathObj.subPathItems[s].pathPoints.length;i++)
        {
            //alert(pathObj.subPathItems[s].pathPoints[i].anchor);
            //end;

            if( limx > pathObj.subPathItems[s].pathPoints[i].anchor[0] )
            {
                limx = pathObj.subPathItems[s].pathPoints[i].anchor[0];
            }

            if( limy > pathObj.subPathItems[s].pathPoints[i].anchor[1] )
            {
                limy = pathObj.subPathItems[s].pathPoints[i].anchor[1];
            }

            pArray[i] = new PathPointInfo;
            pArray[i].kind = pathObj.subPathItems[s].pathPoints[i].kind;
            pArray[i].anchor = pathObj.subPathItems[s].pathPoints[i].anchor;
            pArray[i].leftDirection = pathObj.subPathItems[s].pathPoints[i].leftDirection;
            pArray[i].rightDirection = pathObj.subPathItems[s].pathPoints[i].rightDirection;
        }

        // Create a new SubPathInfo for this subPathItem.
        subPathArray = new Array();
        subPathArray[0] = new SubPathInfo();
        subPathArray[0].operation = ShapeOperation.SHAPEADD;
        subPathArray[0].closed = true;
        subPathArray[0].entireSubPath = pArray;
        //subPathArray[1] = new Array(limx, limy);

        //alert( "point: " + limx + "," + limy );

        pathArray.push(subPathArray);
        pathArray.push(new Array(limx, limy));
    }

    return pathArray;
 }

// Find get all path points for all regions in a path.
// Also, get area
function mapSelection( ndoc, out)
{
    // Get history.
    var hs = ndoc.historyStates;

    // Set units.
    preferences.rulerUnits = Units.PIXELS;

    // Get selection.
    ndoc.selection.makeWorkPath(1);

    // Get path for selection.
    var p = ndoc.pathItems["Work Path"];
    var a = new Array();
    var t = 0;
    var l = p.subPathItems.length;

    // Split path into array.
    // for the first sub-path item
    var n;
    for ( k = 0; k < l; k++)
    {
        n = p.subPathItems[k].pathPoints.length;

        a[k] = new Array (n);

        for ( i = 0; i < n; i++)
        {
            a[k][i] = new Array();
            a[k][i] = p.subPathItems[k].pathPoints[i].anchor;
        }
    }

    // Find all path points for this selection, and add them to the array.
    // Also, calculate area.
    var t = 0;
    var n;
    var s;
    var j;
    for ( k = 0; k < l; k++)
    {
        n = a[k].length;
        s = 0;

        for ( i = 0; i < n; i++)
        {
            j = (i+1)%n;
            s += ( a[k][i][0] * a[k][j][1] );
            s -= ( a[k][i][1] * a[k][j][0] );
        }

        s /= 2;
        t += s;
    }

    ndoc.activeHistoryState = hs[hs.length - 2];
    out = new Array();
    out.mapa = a;
    out.area = parseFloat( t / ndoc.resolution / ndoc.resolution * 2.54 * 2.54 ).toFixed(2); //convert to cm^2

    return out;
}

– 30 –

Advertisements