Script to scale layers, works on a single, doesn't work on a selection

Hi, would love a bit of advice with my first ever script, which is supposed to rescale all the graphics for selected layers and change the dimensions.

    Font.disableUpdateInterface()

    newWidth = 600

    for thisLayer in selectedLayers:
    	thisMaster = thisLayer.master
    	layer = Glyphs.font.selectedLayers[0]

    	scaleFactor = newWidth/layer.width

    	newLSB = thisLayer.LSB * scaleFactor
    	newRSB = thisLayer.RSB * scaleFactor

    	# shift
    	shift = (thisLayer.LSB - thisLayer.RSB ) * -0.5
    	shiftMatrix = transform(shiftX=shift).transformStruct()
    	thisLayer.applyTransform( shiftMatrix )

    	# scale
    	scaleMatrix = transform(scale=scaleFactor).transformStruct()
    	thisLayer.applyTransform( scaleMatrix )

    	# change metrics
    	thisLayer.LSB = newLSB
    	thisLayer.RSB = newRSB
    	thisLayer.width = newWidth

    Font.enableUpdateInterface()

It works great if you run it once, on a single glyph. But if you run it on two or more, it doesn’t do the second and subsequent ones correctly. I can’t see what it’s carrying over from the first to the next…? Thanks if you can help!

The script never sets the selectedLayers do it keeps the layer from a previous run of a script.

But the script does some things that don’t seem to make sense. You don’t need to calculate a shiftX if you later set the LSB and setting the RSB and directly after it set the width is not making a difference.

Thanks, Georg, I see the problem now. :sweat_smile:

Scripting is a really good feature!

These lines are suspicious. Either you should move them out of the loop or it should be

scaleFactor = newWidth / thisLayer.width

Yes, in my naive enthusiasm I had copied and posted code from two different places without reading carefully enough — always a fruitful source of errors… :slight_smile: It all works now.

Thanks!

1 Like