My goal is to define scale bars programmatically, that have a fixed length, but may be changed afterwards by the graphic designer.
I have come sofar as to define a closed path within the document:
def addLine(doc):
def point(x, y):
result = Dispatch("Photoshop.PathPointInfo")
result.Kind = 2 # for PsPointKind --> 2 (psCornerPoint)
result.LeftDirection = result.rightDirection = result.Anchor = (x, y)
return result
points = [
point(100, 100),
point(200, 100),
point(200, 110),
point(100, 110)
]
lineSubPathArray = Dispatch("Photoshop.SubPathInfo")
lineSubPathArray.Operation = 1 #for PsShapeOperation --> 1 (psShapeAdd
lineSubPathArray.Closed = True
lineSubPathArray.EntireSubPath = points
myPathItem = doc.PathItems.Add("bar-path", [lineSubPathArray])
From here, I can load the saved document back into photoshop (CS6) and then create a shape layer manually: Layer | New fill layer | solid color …
This results in a shape layer, similar to what I get by using the line tool, in which the line effectively is a rectangle whose height may be changed later.
First question: how to create the fill layer by using the API?
Second: I defined a rectangle 100pixels wide, but I get one 418 pixels wide. The doc has its doc.application.preferences.rulerUnits
set to psPixels
(1). Why is this?
Last: Isn’t it possible to define a line as true line defined by two end points and set its stroke width instead of it’s height?
2
Answers
There are two lessons I have learned form translating the solution by Ghoul Fool into Python:
Here is, for those who have come to this question later, what I have come to to get a shape layer with a bar that can be changed in height afterwards. The action that is called is nothing more than a recording of New Fill Layer from the layer-menu (CS6):
As for my second question: The unit of the path's cornerpoints are invariantly in Points, no matter what he rulers are set to.
This may be of use:
You can define a new colour with
new SolidColor()
and then fill your path by adding
myPathItem.fillPath(fillColor,ColorBlendMode.NORMAL,100,false,0,true,true);
As for rectangle being too large: What resolution and units do you have the psd file set to? The script below will switch to pixels, and set the resolution of your document to 72dpi
As for doing a stroke to replace the line. Well… you’ve got options.
You can do a brush stroke with a two point line:
However, I don’t think that’s what your after. You can turn your path into a shape and then have a stroke and fill colour assigned. However… you need a minimum of 3 points for that to work.
As a suggestion, instead of a path create a shape from start – which oddly will work with a minimum of two points. But I have no idea how to do that in code!