skip to Main Content

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

Answers


  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.

    Login or Signup to reply.
  2. With the canvas resize you omitted the anchor position (don’t worry it will still work without it) However, you might want to resize the canvas from the floor up (if you’re working on a character).

    The new width and height can be found from using the bitwise shift (&) that Mark explained. Don’t worry if you don’t understand it, but it will work. I’m an artist and I’ve been there.

    resizeCanvas(newwidth, newheight, AnchorPosition.BOTTOMCENTER)
    

    The defualt is AnchorPosition.MIDDLECENTER

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search