0

I am working on a game that uses an animation software called Spine to create the character animations. Spine allows you to create skeletal animations by attaching PNGs to bones & then moving the bones on a timeline by key-framing position, scale, rotation, etc. For the game graphics to look sharp, we need to make sure each of the PNGs are divisible by a certain number ratio.

So with that in mind, I'm trying to create a PS Script that will export each layer out as a PNG but check for the following:

  1. If the layer's dimensions are not divisible by 16, add on pixels evenly to the height & width to make the dimensions divisible by 16. Then export as a PNG.
  2. If the layer's dimensions are already divisible by 16, export out a PNG as normal.

Some kind guys helped me with a piece of code that adds on pixels to a canvas to make it divisible by 16 but I'd like to make the code more dynamic by it looking at layers & bypassing the script if the layer is already divisible by 16:

preferences.rulerUnits = Units.PIXELS; with(activeDocument)
    resizeCanvas(width + 16 - width % 16, height + 16 - height % 16)

Any help with this problem would be greatly appreciated, i'm a humble animator and this is way over my head! Kind Regards, Steve.

2 Answers2

1

The code you have will increase a dimension by 16 if it's already divisible by 16, which is pointless. I don't think the "kind guys" did you any favors here.

For each dimension with length n, change it to n + (-n & 15). That will leave n as is if it is already a multiple of 16.

If both dimensions are unchanged by this, then don't bother resizing the image.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
0

Mark has done the heavy lifting with the maths. Don't worry if you don't understand bitwise operators (&) at this stage. It works.

// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF 

// make a reference to the current Photoshop file
var srcDoc = app.activeDocument;
var theLayer = app.activeDocument.activeLayer;

// get the layer bounds
var lb = theLayer.bounds; 

// the layer bounds are Top, Left, Bottom, Right
// But we just want the layers' width & height
var w = lb[2]-lb[0]; // right - left
var h = lb[3]-lb[1]; // bottom - top

// select the layer's pixels
get_layer_as_selection();

//crop to the smallest area
crop_to_selection();

// add to the canvas in multiples of 16
srcDoc.resizeCanvas(w + (-w & 15), h + (-h & 15));


// Reset any dialog boxes
displayDialogs = DialogModes.ALL; // OFF


// function CROP TO SELECTION()
// ----------------------------------------------------------------
function crop_to_selection()
{
  executeAction( charIDToTypeID( "Crop" ), new ActionDescriptor(), DialogModes.NO );
}

function get_layer_as_selection()
{
    var idslct = charIDToTypeID( "slct" );
  var desc61 = new ActionDescriptor();
  var idnull = charIDToTypeID( "null" );
  var ref21 = new ActionReference();
  var idHstS = charIDToTypeID( "HstS" );
  var idOrdn = charIDToTypeID( "Ordn" );
  var idPrvs = charIDToTypeID( "Prvs" );
  ref21.putEnumerated( idHstS, idOrdn, idPrvs );
  desc61.putReference( idnull, ref21 );
  executeAction( idslct, desc61, DialogModes.NO );
}
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125