Hi everyone
I wrote a PS script that allows me to convert wood textures to tiled wood textures based on user-defined guides.
Here´s a graphic that hopefully explains what it does.
What it does is actually not that important.
It works fine and now i want to optimize the code to speed it up.
Can anyone look over it and tell me if there are parts which should/could be optimzed?
Any possible errors i overlooked and should handle?
Thanks for you time and help.
Code: Select all
#target Photoshop
var savePath
var doc = app.activeDocument;
var guides = app.activeDocument.guides;
var gHArray = [];
var w = doc.width;
var h = doc.height;
app.preferences.rulerUnits = Units.PIXELS
app.preferences.typeUnits = TypeUnits.PIXELS
try
{
savePath = doc.path.fsName +"\\";
}
catch(e)
{
savePath = "Enter Path";
}
var fileName = doc.name.substr (0, doc.name.indexOf ("."))
if(fileName == ""){fileName = "Filename"}
var dlg = new Window("dialog{text:'GuideTiling',bounds:[100,100,450,500],\
etDivisionsV:EditText{bounds:[200,30,300,50] , text:'4' ,properties:{multiline:false,noecho:false,readonly:false}},\
statictext0:StaticText{bounds:[20,30,160,50] , text:'Planks Vertical ' ,properties:{scrolling:undefined,multiline:undefined}},\
statictext1:StaticText{bounds:[20,55,160,75] , text:'Offset (%) ' ,properties:{scrolling:undefined,multiline:undefined}},\
etOffsetX:EditText{bounds:[200,55,300,75] , text:'25' ,properties:{multiline:false,noecho:false,readonly:false}},\
statictext0:StaticText{bounds:[20,90,170,110] , text:'Random Plank length (%) ' ,properties:{scrolling:undefined,multiline:undefined}},\
etPlankLength:EditText{bounds:[200,90,300,110] , text:'0' ,properties:{multiline:false,noecho:false,readonly:false}},\
statictext1:StaticText{bounds:[20,115,160,135] , text:'Random Offset (%) ' ,properties:{scrolling:undefined,multiline:undefined}},\
etRandomOffset:EditText{bounds:[200,115,300,135] , text:'0' ,properties:{multiline:false,noecho:false,readonly:false}},\
btnStart:Button{bounds:[120,150,220,170] , text:'Make Tiles ' },\
btnReset:Button{bounds:[225,150,270,170] , text:'Reset ' },\
etSavePath:EditText{bounds:[20,245,330,265] , text:'D ' ,properties:{multiline:false,noecho:false,readonly:false}},\
etFileName:EditText{bounds:[20,270,260,290] , text:'fileName ' ,properties:{multiline:false,noecho:false,readonly:false}},\
ddExt:DropDownList{bounds:[280,270,330,290],properties:{items:['jpeg','png','tiff']}},\
statictext3:StaticText{bounds:[20,300,121,317] , text:'diffuse ' ,properties:{scrolling:false,multiline:false}},\
statictext4:StaticText{bounds:[20,320,121,337] , text:'bump ' ,properties:{scrolling:false,multiline:false}},\
statictext5:StaticText{bounds:[20,340,121,357] , text:'ref ' ,properties:{scrolling:false,multiline:false}},\
stInfo:StaticText{bounds:[20,190,330,220] , text:' ' ,properties:{scrolling:false,multiline:true}},\
btnSaveMaps:Button{bounds:[120,370,220,390] , text:'Save Maps ' }\
};")
dlg.ddExt.selection = dlg.ddExt.items[2]
dlg.etFileName.text = fileName
dlg.etSavePath.text = savePath
var errorInfo =""
dlg.btnStart.onClick = function() {
function checkForErrors(){
if(guides.length <2){errorInfo += "Define Guides first! "}
if(doc.artLayers.length <3){errorInfo += "Three Layers required!"}
dlg.stInfo.text = errorInfo
}
checkForErrors()
if(errorInfo == ""){
var divisions = parseInt(dlg.etDivisionsV.text);
var offsetXPercent = parseInt(dlg.etOffsetX.text);
var maxVariationLength = parseInt(dlg.etPlankLength.text);
var maxRandomOffset = parseInt(dlg.etRandomOffset.text);
function doSort(data_A, data_B) {
if ( data_A < data_B ) // data_A come before data_B
return -1;
if ( data_A > data_B ) // data_A come After data_B
return 1;
return 0; // data_A == data_B, no change.
}
takeSnapshot ()
function takeSnapshot () {
var desc = new ActionDescriptor();
var sref = new ActionReference();
sref.putClass(charIDToTypeID("SnpS"));
desc.putReference(charIDToTypeID("null"), sref);
var fref = new ActionReference();
fref.putProperty(charIDToTypeID("HstS"), charIDToTypeID("CrnH"));
desc.putReference(charIDToTypeID("From"), fref );
executeAction(charIDToTypeID("Mk "), desc, DialogModes.NO );
}
//-- GUIDES --//
gHArray = []
for( var g = 0; g < guides.length; g++ ){
if(guides[g].direction.toString() == 'Direction.HORIZONTAL'){
gHArray.push(parseInt(guides[g].coordinate.value))
}
}
gHArray.sort (doSort) //all horizontal guides sorted in array
//-- PLANKES --//
var cellLength = w / divisions;
for (var j = 0; j < gHArray.length-1; j++) { //line
// calc division points
var tilingCoords = [0]
for(var i = 1; i<divisions;i++){
tilingCoords.push (cellLength*i+cellLength*parseInt(Math.random()*maxVariationLength)/100)
}
tilingCoords.push(w)
for (var i = 0; i < divisions; i+=2) { //single planks
var yTop = gHArray[j]
var yBottom = gHArray[j+1]
var A= Array(tilingCoords, yTop);
var B = Array(tilingCoords[i+1],yTop);
var C = Array(tilingCoords[i+1], yBottom);
var D = Array(tilingCoords, yBottom);
var myRegion = Array(A, B, C, D, A);
for (var currentLayer = 1; currentLayer <=3;currentLayer++){ //layer
doc.selection.select(myRegion)
doc.activeLayer = doc.artLayers[(doc.artLayers.length - currentLayer)]
doc.selection.copy()
doc.paste()
doc.artLayers[(doc.artLayers.length - currentLayer)-1].rotate (180)
doc.artLayers[(doc.artLayers.length - currentLayer)-1].merge()
}
}
}
//-- OFFSET --//
for (var j = 0; j < gHArray.length-1; j++) {
var yTop = gHArray[j]
var yBottom = gHArray[j+1]
var A= Array(0, yTop);
var B = Array(w,yTop);
var C = Array(w, yBottom);
var D = Array(0, yBottom);
var E = Array(0, yTop);
var myRegion = Array(A, B, C, D, E);
randomOffset = cellLength*parseInt(Math.random()*maxRandomOffset)/100
offsetX = cellLength*j*(offsetXPercent/100)+randomOffset
for (var currentLayer = 1; currentLayer <=3;currentLayer++){ //layer
doc.selection.select(myRegion);
doc.activeLayer = doc.artLayers[(doc.artLayers.length - currentLayer)]
doc.artLayers[(doc.artLayers.length - currentLayer)].applyOffset(offsetX, 0, OffsetUndefinedAreas.WRAPAROUND);
}
}
doc.selection.deselect()
}
app.refresh()
}
dlg.btnReset.onClick = function() {
function revertToLastSnapshot() {
var docRef = app.activeDocument;
var hsObj = docRef.historyStates;
var hsLength = hsObj.length;
for (var i=hsLength - 1;i>-1;i--) {
if (hsObj.snapshot) {
doc.activeHistoryState = doc.historyStates.getByName('Snapshot ' + i);
break;
}
}
}
revertToLastSnapshot()
app.refresh()
}
dlg.btnSaveMaps.onClick = function() {
// saving the stuff, not important right now
}
dlg.show();