0 and 1 are not indexes they are commands. PSP more or less follows the SVG standard 0 = MoveTo (used to move to a starting point without drawing), 1 = LineTo (move from current position to new position). 2 = CurveTo (Cubic), 3= Close Path.
If you're seeing a 2 it's because it was accidentally drawn with the curve tool and dragged out ever so slightly. The first two (x,y) points are the control handles for the curve. On the positive side, in both cases the last coordinate tuple is the ending point. So grabbing the -1 value should always get the end point.
Code: Select all
# Pseudo-code I haven't tested this but it works in my head.
StartPoint = PathData[0][-1]
EndPoint = PathData[1][-1] #assumes two points exist, but doesn't care what kind of movement is used. Could use just two unconnected nodes so long as they were part of the same path object.
Rect = (StartPoint, EndPoint[0]-StartPoint[0], EndPoint[1] - StartPoint[1])
You could also just use the rectangle pre-set tool and use the Rect data directly rather than working it out. It should be just as fast to drag out a rectangle as it is to select two points for a line.
The easiest option for getting all objects is to just manually select them all in the layers palette (select the bottom one, SHIFT + click on the top one to select them all) then use ReturnVectorObjectProperties and the ['ListOfObjects'] property will have all of the objects you selected.
GetNextObject is supposed to get the first object of a vector layer if no object is selected, and then go through the top level objects until the layer no longer has any children to go through. It does not go into vector object groups though. So avoid using those. And it won't auto-jump into the next vector layer. If a vector object is already selected it'll just grab the next one in line. So, if you want to guarantee it'll start at the first object and then move up the line after that, just select the top level layer first. There are several methods, I like to select the bottom most layer and then select the layer I was just on, but I can also do it by checking the layer type and whether a vectorobject is already selected.
Here are two options I can think of right now. Not much but simple examples. Naturally there are probably far better solutions.
method 1
Code: Select all
currentLayer = App.Do(Environment, 'ReturnLayerProperties')
if currentLayer['LayerType'] != App.Constants.Layertype.Vector:
return #not a vector layer, cannot proceed
App.Do(Environment, 'SelectLayer', {'Path':(-9999,-9999,[0],False)}) #Select bottom layer
App.Do(Environment, 'Selectlayer', {'Path':currentLayer['Path']}) # re-select layer
rects = []
while App.Do(Environment, 'GetNextObject')['HasObject']:
obj = App.Do(Environment, 'ReturnVectorObjectProperties')['ListOfObjects'][0] #object data
if obj['ObjectType'] == 'Path':
pathData = obj['ObjectData']['Path']
if len(pathData) > 1:
Startpoint = pathData[0][-1]
Endpoint = pathData[1][-1]
rects.append( (StartPoint, EndPoint[0]-StartPoint[0], EndPoint[1] - StartPoint[1])) #add rectangle to list
method 2
Code: Select all
currentLayer = App.Do(Environment, 'ReturnLayerProperties')
if currentLayer['LayerType'] != App.Constants.Layertype.Vector:
return #not a vector layer, cannot proceed
currentObject = App.Do(Environment, 'ReturnVectorProperties')['ListOfObjects'][0]
if currentObject['ObjectType'] != 'Object': # We have an object selected, move out one level. If it is an object, we have a layer, proceed as normal
App.Do(Environment, 'SelectLayer', {'Path': (-1, 0, [], False)})
rects = []
while App.Do(Environment, 'GetNextObject')['HasObject']:
obj = App.Do(Environment, 'ReturnVectorObjectProperties')['ListOfObjects'][0] #object data
if obj['ObjectType'] == 'Path':
pathData = obj['ObjectData']['Path']
if len(pathData) > 1:
Startpoint = pathData[0][-1]
Endpoint = pathData[1][-1]
rects.append( (StartPoint, EndPoint[0]-StartPoint[0], EndPoint[1] - StartPoint[1])) #add rectangle to list