 Bounds value problem

The layer has two nodes independently.
and I want to calculate the bounds size and position through the following script.
But it feedback the wrong values, which can’t calculate the values including the two independent nodes.

layer =  Glyphs.font.glyphs['space'].layers
print(layer.bounds.size.width, layer.bounds.size.height)
print(layer.bounds.origin.x, layer.bounds.origin.y)

bounds value.glyphs (6.1 KB)

The layer bounds do not include single nodes, since they are removed on export. If you want the bounds of all selected nodes, use the selectionBounds.

so, what is the ‘select all’ of layers function?

I am not sure what you mean. Do you want to select all nodes on the current layer with a script?

yes, thanks.

You can use this:

for path in Layer.paths:
path.selected = True

If you also want to select anchors:

for anchor in Layer.anchors:
anchor.selected = True

And to selecting all components:

for component in Layer.components:
component.selected = True

got it, thanks a lot:)

Adding the section one by one can be VERY slow. So this is much faster:

newSelection = []
for path in Layer.paths:
newSelection.extend(path.nodes)
newSelection.extend(Layer.anchors)
for component in Layer.components:
newSelection.append(component)
Layer.selection = newSelection

But what are you trying to do? What do you need the bounds for?

You could write a function that gets the bound from all nodes:

from Foundation import NSMinX, NSMinY, NSMaxX, NSMaxY

minX = 30000
minY = 30000
maxX = -30000
maxY = -30000

layerBounds = Layer.bounds
minX = min(minX, NSMinX(layerBounds))
minY = min(minY, NSMinY(layerBounds))
maxX = max(maxX, NSMaxX(layerBounds))
maxY = max(maxY, NSMaxY(layerBounds))

for path in Layer.paths:
pathBounds = path.fastBounds()
minX = min(minX, NSMinX(pathBounds))
minY = min(minY, NSMinY(pathBounds))
maxX = max(maxX, NSMaxX(pathBounds))
maxY = max(maxY, NSMaxY(pathBounds))
print(minX, minY, maxX, maxY)

The first is what I need.
Thanks:)

Depending what you are trying to do, I would try to not change the state of the layers when running the script.