Mobile app version of vmapp.org
Login or Join
Si6392903

: Error in Drawing a path using Photoshop scripting I am using the following script for drawing a set of paths onto a photoshop image. var docRef= app.activeDocument; app.displayDialogs

@Si6392903

Posted in: #Automation #PhotoshopScripting

I am using the following script for drawing a set of paths onto a photoshop image.

var docRef= app.activeDocument;
app.displayDialogs = DialogModes.NO;

var currentRulerUnits = app.preferences.rulerUnits;
var currentTypeUnits = app.preferences.typeUnits;
var currentDialogMode = app.preferences.displayDialogs;

app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;


var height= docRef.height;
var width = docRef.width;


var isHorizontal = false;
var backgroundlayer = docRef.artLayers.getByName("Background");

var layer1,layer2,layer3,layer4;
if(isHorizontal)
{
// Copy and paste the layers in such a way that they are in a horizontal orientation.
layer1 = backgroundlayer.duplicate();
layer1.resize(120,120,AnchorPosition.TOPCENTER)
layer1.name = "A"
layer1.translate(0,height*(-0.5));

layer2 = backgroundlayer.duplicate();
layer2.resize(120,120,AnchorPosition.BOTTOMCENTER)
layer2.name= "B"
layer2.translate(0,height*(0.5));

layer3 = backgroundlayer.duplicate();
layer3.resize(120,120,AnchorPosition.TOPCENTER);
layer3.name = "C";
layer3.translate(0,height*(-0.5));

layer4 = backgroundlayer.duplicate();
layer4.resize(120,120,AnchorPosition.BOTTOMCENTER);
layer4.name= "D";
layer4.translate(0,height*(0.5));
}
else
{
layer1 = backgroundlayer.duplicate();
layer1.resize(120,120,AnchorPosition.MIDDLELEFT);
layer1.name = "A";
layer1.translate(width*(-0.5),0);

layer2 = backgroundlayer.duplicate();
layer2.resize(120,120,AnchorPosition.MIDDLERIGHT);
layer2.name= "B";
layer2.translate(width*(0.5),0);

layer3 = backgroundlayer.duplicate();
layer3.resize(120,120,AnchorPosition.MIDDLELEFT);
layer3.name = "C";
layer3.translate(width*(-0.5),0);

layer4 = backgroundlayer.duplicate();
layer4.resize(120,120,AnchorPosition.MIDDLERIGHT);
layer4.name= "D";
layer4.translate(width*(0.5),0);
}

var topXspread = .18; // 25 % Percentage expressed as decimal
var topYhighspread = 0.2; // 20 %
var topYlowspread = 0.6; // 80%
var yminspread = 0.05; // 5 %
var smalltoffset = yminspread/2;
var overallYoffset = 100; // units are in pixels
var midpointYoffset = -75;

var point1 = GeneratePathPointInfo(Math.floor((width/2) * (1 - topXspread)),overallYoffset+Math.floor(height/2 *(1 + yminspread)));
var point2 = GeneratePathPointInfo(Math.floor((width/2)),overallYoffset+Math.floor(height*(topYhighspread)));
var point3 = GeneratePathPointInfo(Math.floor((width/2)*(1 + topXspread)),overallYoffset+Math.floor(height/2 *(1 + yminspread)));
var point4 = GeneratePathPointInfo(Math.floor((width/2)),overallYoffset+Math.floor(height* topYlowspread));
var point5 = GeneratePathPointInfo(Math.floor((width/2)),midpointYoffset+overallYoffset+Math.floor(height/2));


GenerateTrianglePathItem(point1,point2,point4,"bigtleft",docRef);
GenerateTrianglePathItem(point2,point3,point4,"bigtright",docRef);
GenerateLinePathItem(point2,point4,"vertical",docRef);
GenerateLinePathItem(point1,point5,"lineleft",docRef);
GenerateLinePathItem(point3,point5,"lineright",docRef);


var point1small = GeneratePathPointInfo(Math.floor(width/2 * (1 - topXspread)),overallYoffset+Math.floor((height/2 *(1 + yminspread))+ (height *smalltoffset)));
var point3small = GeneratePathPointInfo(Math.floor(width/2*(1 + topXspread)),overallYoffset+Math.floor((height/2 *(1 + yminspread))+ (height *smalltoffset)));
var point4small = GeneratePathPointInfo(Math.floor(width/2),overallYoffset+Math.floor((height* topYlowspread)+ (height *smalltoffset)));
var point6small = GeneratePathPointInfo(Math.floor(width/2),overallYoffset+Math.floor((height* topYlowspread)+ (height * 6 * smalltoffset)));
GenerateTrianglePathItem(point1small,point4small,point6small,"smalltleft",docRef);
GenerateTrianglePathItem(point3small,point4small,point6small,"smalltright",docRef);
GenerateLinePathItem(point4small,point6small,"verticalsmall",docRef);

function GenerateTrianglePathItem(point1,point2,point3,name,docRef){
var spi = new SubPathInfo();
spi.closed = true;
spi.operation = ShapeOperation.SHAPEXOR;
spi.entireSubPath = [point1,point2,point3];
var line = docRef.pathItems.add(name, [spi]);
};

function GenerateLinePathItem(point1,point2,name,docRef){
var spi = new SubPathInfo();
spi.closed = true;
spi.operation = ShapeOperation.SHAPEXOR;
spi.entireSubPath = [point1,point2];
var line = docRef.pathItems.add(name, [spi]);
};

function GeneratePathPointInfo(x,y){
var startPoint = new PathPointInfo();
startPoint.anchor = [x,y];
startPoint.leftDirection = [x,y];
startPoint.rightDirection = [x,y];
startPoint.kind = PointKind.CORNERPOINT;
return startPoint;
};


The co-ordinates for the points (Point1, 2, 3 ..) are being calculated correctly and are withing the bounds of the image. However, the final path drawn is being drawn completely outside the image bounds.

Can anyone help me with why this is happening so?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Si6392903

1 Comments

Sorted by latest first Latest Oldest Best

 

@Nimeshi706

I'm not used to scripting with Photoshop so I'm not sure if there is a way to get PathPointInfo to work in pixels—seems there should be—but I can't find it.

One solution is to just change the document resolution to 72PPI. You can save the initial resolution at the beginning of the script, change to 72PPI then change back to the initial resolution once you are finished. I just tested this and it works with no problem.

var docRef= app.activeDocument;
app.displayDialogs = DialogModes.NO;

// Starting resolution
var startResolution = docRef.resolution;

// Change resolution to 72ppi without changing pixel size
docRef.resizeImage(undefined, undefined, 72, ResampleMethod.NONE );


// REST OF YOUR CODE...


// Reset back to starting resolution
docRef.resizeImage(undefined, undefined, startResolution, ResampleMethod.NONE );

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme