Skip to content

Instantly share code, notes, and snippets.

@sacrifs
Last active November 12, 2024 01:27
Show Gist options
  • Save sacrifs/09d61287237934454cbbe1f794e8bf45 to your computer and use it in GitHub Desktop.
Save sacrifs/09d61287237934454cbbe1f794e8bf45 to your computer and use it in GitHub Desktop.
列数を入力すると、自動的にスプライトシート状に並べるPhotoshop用Script。連番画像ファイルをPhotoshop付属の「ファイルをレイヤーとして読み込み」を使って読み込んだ後使用する。
/**
* 簡易スプライトシート生成.jsx
* @author sacrifs
*/
var _doc = activeDocument,
_layers = [],
main = function(){
preferences.rulerUnits = Units.PIXELS;
var dialog = createDialog();
dialog.btnOk.onClick = function(){
var layers = _doc.layers;
getLayer(layers);
var colNum = parseInt(dialog.txtCol.text, 10);
var w = parseInt(dialog.txtWidth.text, 10);
var h = parseInt(dialog.txtHeight.text, 10);
var num = _layers.length;
var docW = w * colNum;
var docH = h * Math.ceil(num / colNum);
_doc.resizeCanvas(docW, docH, AnchorPosition.TOPLEFT);
for(var i = 0; i < num; i++){
var layer = _layers[i];
var x = (i % colNum) * w;
var y = (i / colNum | 0) * h;
layer.translate(x, y);
}
dialog.close();
alert("処理が完了しました。");
}
dialog.show();
},
getLayer = function(layers){
var num = layers.length;
for(var i = 0; i < num; i++){
var layer = layers[i];
if(layer.typename == "LayerSet"){
getLayer(layer.layers);
}
_layers.push(layer);
}
},
createDialog = function(){
var dialog = new Window("dialog", "簡易スプライトシート生成", [200, 100, 500, 300]);
var docW = parseInt(_doc.width, 10);
var docH = parseInt(_doc.height, 10);
dialog.btnOk = dialog.add("button", [160, 140, 230, 140 + 25], "OK", {name:"ok"});
dialog.btnCancel = dialog.add("button", [70, 140, 140, 140 + 25], "Cancel", {name:"cancel"});
dialog.stxtCol = dialog.add("statictext", [20, 12, 100, 10 + 25], "列数 : ");
dialog.txtCol = dialog.add("edittext", [80, 10, 280, 10 + 25], "10");
dialog.stxtWidth = dialog.add("statictext", [20, 52, 100, 50 + 25], "Width : ");
dialog.txtWidth = dialog.add("edittext", [80, 50, 280, 50 + 25], docW);
dialog.stxtHeight = dialog.add("statictext", [20, 92, 100, 90 + 25], "Height : ");
dialog.txtHeight = dialog.add("edittext", [80, 90, 280, 90 + 25], docH);
return dialog;
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment