Sunday 3 January 2021

how to add a fill layer in photoshop programatically

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?



from how to add a fill layer in photoshop programatically

No comments:

Post a Comment