Skip to content

Instantly share code, notes, and snippets.

@mio3io
Created March 7, 2018 12:54
Show Gist options
  • Save mio3io/67174982aba988ef739721d3f32335db to your computer and use it in GitHub Desktop.
Save mio3io/67174982aba988ef739721d3f32335db to your computer and use it in GitHub Desktop.
フォトショ用スクリプト
/*
* DiffusionLayer for Photoshop
* フォトショ用レイヤーランダム配置
* 2012.10.06 hisato
*/
#target photoshop
function DiffusionLayer() {
this.userUnit = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
this.userDialogs = app.displayDialogs;
displayDialogs = DialogModes.NO;
// パネルの初期値
this.defaults = {
'posJitter' : false, // true or false
'posGridX' : 2, // 1-5
'posGridY' : 2, // 1-5
'posMargin' : 1, // 0-10 (表示は10倍)
'opaJitter' : 0, // 0-100
'scaleJitter' : 0, // 0-100
'rotateJitter' : 0, // 0-180
'snapshot' : false, // true or false
};
}
DiffusionLayer.prototype.execute = function() {
var docRef = app.activeDocument;
var tmpactiveLayer = docRef.activeLayer;
if (docRef.activeLayer.artLayers) {
var targetLayers = docRef.activeLayer.artLayers;
} else {
return this.__exit("レイヤーセットを選択してください");
}
function changeSliderValue() {
this.value = Math.round(this.value);
this.viewValue.text = this.value;
}
// 左 上 右 下
var winObj = new Window("dialog", "DiffusionLayer", [ 0, 0, 400, 450 ]);
// ★パネル 位置
winObj.posPnl = winObj.add("panel", [ 20, 20, 380, 160 ], "位置");
winObj.posPnl.jitter = winObj.posPnl.add("checkbox", [ 150, 20, 150 + 200, 20 + 20 ], "ランダムに位置を移動させる");
winObj.posPnl.jitter.value = this.defaults.posJitter;
// ジッター
/*
* winObj.posPnl.add("statictext", [ 10, 20, 100 + 80, 20 + 20 ], "ジッター");
* winObj.posPnl.jitter = winObj.posPnl.add("slider", [ 150, 15, 150 + 150,
* 15 + 20 ], 1, 0, 1); winObj.posPnl.jitter.viewValue =
* winObj.posPnl.add("statictext", [ 310, 20, 310 + 30, 20 + 20 ],
* winObj.posPnl.jitter.value); winObj.posPnl.jitter.onChange =
* changeSliderValue;
*/
winObj.posPnl.add("statictext", [ 10, 50, 10 + 80, 50 + 20 ], "グリッド分割");
// グリッド横
winObj.posPnl.add("statictext", [ 120, 50, 120 + 30, 50 + 20 ], "横");
winObj.posPnl.gridX = winObj.posPnl.add("slider", [ 150, 45, 150 + 150, 45 + 20 ], this.defaults.posGridX, 1, 5);
winObj.posPnl.gridX.viewValue = winObj.posPnl.add("statictext", [ 310, 50, 310 + 30, 50 + 20 ], winObj.posPnl.gridX.value);
winObj.posPnl.gridX.onChange = changeSliderValue;
// グリッド縦
winObj.posPnl.add("statictext", [ 120, 70, 120 + 30, 70 + 20 ], "縦");
winObj.posPnl.gridY = winObj.posPnl.add("slider", [ 150, 65, 150 + 150, 65 + 20 ], this.defaults.posGridY, 1, 5);
winObj.posPnl.gridY.viewValue = winObj.posPnl.add("statictext", [ 310, 70, 310 + 30, 70 + 20 ], winObj.posPnl.gridY.value);
winObj.posPnl.gridY.onChange = changeSliderValue;
// 領域割り込み
winObj.posPnl.add("statictext", [ 10, 100, 100 + 80, 100 + 20 ], "領域割り込み");
winObj.posPnl.margin = winObj.posPnl.add("slider", [ 150, 95, 150 + 150, 95 + 20 ], this.defaults.posMargin, 0, 10);
winObj.posPnl.margin.viewValue = winObj.posPnl.add("statictext", [ 310, 100, 310 + 30, 100 + 20 ], winObj.posPnl.margin.value * 10);
winObj.posPnl.add("statictext", [ 330, 100, 330 + 20, 100 + 20 ], "%");
winObj.posPnl.margin.onChange = function() {
this.value = Math.round(this.value);
this.viewValue.text = this.value * 10;
}
// ★パネル 不透明度
winObj.opaPnl = winObj.add("panel", [ 20, 170, 380, 230 ], "不透明度");
winObj.opaPnl.add("statictext", [ 10, 20, 10 + 80, 20 + 20 ], "ジッター");
winObj.opaPnl.jitter = winObj.opaPnl.add("slider", [ 150, 15, 150 + 150, 15 + 20 ], this.defaults.opaJitter, 0, 100);
winObj.opaPnl.jitter.viewValue = winObj.opaPnl.add("statictext", [ 310, 20, 310 + 30, 20 + 20 ], winObj.opaPnl.jitter.value);
winObj.opaPnl.jitter.onChange = changeSliderValue;
// ★パネル サイズ
winObj.scalePnl = winObj.add("panel", [ 20, 240, 380, 300 ], "サイズ");
winObj.scalePnl.add("statictext", [ 10, 20, 10 + 80, 20 + 20 ], "ジッター");
winObj.scalePnl.jitter = winObj.scalePnl.add("slider", [ 150, 15, 150 + 150, 15 + 20 ], this.defaults.scaleJitter, 0, 100);
winObj.scalePnl.jitter.viewValue = winObj.scalePnl.add("statictext", [ 310, 20, 310 + 30, 20 + 20 ], winObj.scalePnl.jitter.value);
winObj.scalePnl.jitter.onChange = changeSliderValue;
// ★パネル 回転
winObj.rotatePnl = winObj.add("panel", [ 20, 310, 380, 370 ], "回転");
winObj.rotatePnl.add("statictext", [ 10, 20, 10 + 80, 20 + 20 ], "ジッター");
winObj.rotatePnl.jitter = winObj.rotatePnl.add("slider", [ 150, 15, 150 + 150, 15 + 20 ], this.defaults.rotateJitter, 0, 180);
winObj.rotatePnl.jitter.viewValue = winObj.rotatePnl.add("statictext", [ 310, 20, 310 + 30, 20 + 20 ], winObj.rotatePnl.jitter.value);
winObj.rotatePnl.jitter.onChange = changeSliderValue;
winObj.snapshot = winObj.add("checkbox", [ 20, 380, 20 + 200, 380 + 20 ], "スナップショットを作成する");
winObj.snapshot.value = this.defaults.snapshot;
winObj.btnOk = winObj.add("button", [ 110, 410, 110 + 80, 440 ], "OK", {
name : "ok"
});
winObj.btnNo = winObj.add("button", [ 210, 410, 210 + 80, 440 ], "キャンセル", {
name : "cancel"
});
winObj.center();
if (winObj.show() == 2) {
return this.__exit();
}
var get = {
'pos' : {
'jitter' : Math.round(winObj.posPnl.jitter.value),
'gridX' : Math.round(winObj.posPnl.gridX.value),
'gridY' : Math.round(winObj.posPnl.gridY.value),
'margin' : Math.round(winObj.posPnl.margin.value)
},
'opa' : {
'jitter' : Math.round(winObj.opaPnl.jitter.value)
},
'scale' : {
'jitter' : Math.round(winObj.scalePnl.jitter.value)
},
'rotate' : {
'jitter' : Math.round(winObj.rotatePnl.jitter.value)
}
};
if (!get["pos"]["jitter"] && !get["opa"]["jitter"] && !get["scale"]["jitter"] && !get["rotate"]["jitter"]) {
return;
}
if (winObj.snapshot.value) {
var idMk = charIDToTypeID("Mk ");
var desc11 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
var ref5 = new ActionReference();
var idSnpS = charIDToTypeID("SnpS");
ref5.putClass(idSnpS);
desc11.putReference(idnull, ref5);
var idFrom = charIDToTypeID("From");
var ref6 = new ActionReference();
var idHstS = charIDToTypeID("HstS");
var idCrnH = charIDToTypeID("CrnH");
ref6.putProperty(idHstS, idCrnH);
desc11.putReference(idFrom, ref6);
executeAction(idMk, desc11, DialogModes.NO);
}
var stageL = 0;
var stageT = 0;
var stageR = parseInt(docRef.width);
var stageB = parseInt(docRef.height);
var stageWidth = stageR - stageL;
var stageHeight = stageB - stageT;
var celWidth = parseInt(stageWidth / get["pos"]["gridX"]);
var celHeight = parseInt(stageHeight / get["pos"]["gridY"]);
var gridMap = [];
if (get["pos"]["jitter"]) {
for ( var y = 0; y < get["pos"]["gridY"]; y++) {
for ( var x = 0; x < get["pos"]["gridX"]; x++) {
var l = (x * celWidth) + stageL;
var t = (y * celHeight) + stageT;
var r = (x + 1) * celWidth + stageL;
var b = (y + 1) * celHeight + stageT;
l -= Math.ceil(celWidth * get["pos"]["margin"] / 10);
r += Math.ceil(celWidth * get["pos"]["margin"] / 10);
t -= Math.ceil(celHeight * get["pos"]["margin"] / 10);
b += Math.ceil(celHeight * get["pos"]["margin"] / 10);
if (x == get["pos"]["gridX"] - 1 && stageWidth % get["pos"]["gridX"]) {
r += stageWidth % get["pos"]["gridX"];
}
if (y == get["pos"]["gridY"] - 1 && stageHeight % get["pos"]["gridY"]) {
b += stageHeight % get["pos"]["gridY"];
}
gridMap.push([ l, t, r, b ]);
}
}
}
var gridIndexs = []
for ( var i = 0; i < get["pos"]["gridX"] * get["pos"]["gridY"]; i++) {
gridIndexs.push(i);
}
gridIndexs = gridIndexs.shuffle();
var test = '';
var gridIndex = 0;
for ( var i = 0; i < targetLayers.length; i++) {
var currentLayer = docRef.activeLayer = targetLayers[i];
if (get["opa"]["jitter"]) {
var min = currentLayer.opacity - (currentLayer.opacity * get["opa"]["jitter"] / 100) || 1;
var max = currentLayer.opacity + (100 - currentLayer.opacity) * get["opa"]["jitter"] / 100;
var rand = Math.floor(Math.random() * (max + 1 - min) + min);
try {
currentLayer.opacity = rand;
} catch (e) {
}
}
if (get["scale"]["jitter"]) {
var min = 100 - get["scale"]["jitter"] || 1;
var max = 100 + get["scale"]["jitter"];
var rand = Math.floor(Math.random() * (max + 1 - min) + min);
try {
currentLayer.resize(rand, rand);
} catch (e) {
}
}
if (get["rotate"]["jitter"]) {
var rand = Math.floor(Math.random() * (get["rotate"]["jitter"] + 1 + get["rotate"]["jitter"]) - get["rotate"]["jitter"]);
try {
currentLayer.rotate(rand);
} catch (e) {
}
}
if (get["pos"]["jitter"]) {
var layL = parseInt(currentLayer.bounds[0]);
var layT = parseInt(currentLayer.bounds[1]);
var layR = parseInt(currentLayer.bounds[2]);
var layB = parseInt(currentLayer.bounds[3]);
try {
currentLayer.translate(-layL, -layT);
} catch (e) {
continue;
}
var grid = gridMap[gridIndexs[gridIndex]];
var selectR = grid[2] - (layR - layL);
var selectB = grid[3] - (layB - layT);
var randX = Math.floor(Math.random() * (selectR + 1 - grid[0]) + grid[0]);
var randY = Math.floor(Math.random() * (selectB + 1 - grid[1]) + grid[1]);
currentLayer.translate(randX, randY);
if (gridIndex++ >= gridIndexs.length - 1) {
gridIndex = 0;
}
}
}
// alert(test);
docRef.activeLayer = tmpactiveLayer;
this.__destructor();
};
DiffusionLayer.prototype.__destructor = function() {
displayDialogs = this.userDialogs;
preferences.rulerUnits = this.userUnit;
};
DiffusionLayer.prototype.__exit = function(msg) {
if (msg) {
alert(msg);
}
this.__destructor();
};
Array.prototype.shuffle = function() {
var len = this.length;
var ary = this.concat();
var res = [];
while (len)
res.push(ary.splice(Math.floor(Math.random() * len--), 1));
return res
};
var oDiffusionLayer = new DiffusionLayer();
oDiffusionLayer.execute();
delete oDiffusionLayer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment