# Transform from bounding box via scripting?

How can I transform horizontally so that the left edge stays aligned? As in how does the applyTransform() work and is it possible to set a transform origin?

You need to set up the transformation: It is not always obvious how that works but google will help you.

Here is an example that scales 200% from X=200. It’s kind of like move the hole thing away from the origin, then scale (or rotate) and then move it back.

``````from Foundation import NSAffineTransform
Transform = NSAffineTransform.transform()

Transform.translateXBy_yBy_(200, 0)
Transform.scaleBy_(2)
Transform.translateXBy_yBy_(200, 0)
Layer.applyTransform(Transform.transformStruct())
``````
1 Like

Here is the method I use; instructions in the comments:

``````import math

def transform(shiftX=0.0, shiftY=0.0, rotate=0.0, skew=0.0, scale=1.0):
"""
Returns an NSAffineTransform object for transforming layers.
Apply an NSAffineTransform t object like this:
Layer.transform_checkForSelection_doComponents_(t,False,True)
Access its transformation matrix like this:
tMatrix = t.transformStruct() # returns the 6-float tuple
Apply the matrix tuple like this:
Layer.applyTransform(tMatrix)
Component.applyTransform(tMatrix)
Path.applyTransform(tMatrix)
Chain multiple NSAffineTransform objects t1, t2 like this:
t1.appendTransform_(t2)
"""
myTransform = NSAffineTransform.transform()
if rotate:
myTransform.rotateByDegrees_(rotate)
if scale != 1.0:
myTransform.scaleBy_(scale)
if not (shiftX == 0.0 and shiftY == 0.0):
myTransform.translateXBy_yBy_(shiftX,shiftY)
if skew:
skewStruct = NSAffineTransformStruct()
skewStruct.m11 = 1.0
skewStruct.m22 = 1.0
skewTransform = NSAffineTransform.transform()
skewTransform.setTransformStruct_(skewStruct)
myTransform.appendTransform_(skewTransform)
return myTransform
``````
2 Likes

There is a addition to NSAffineTransform available in Glyphs:

``````- (void)shearXBy:(CGFloat)xShear yBy:(CGFloat)yShear;
``````

that can be used in python like this:

``````myTransform.shearXBy_yBy_(skewX, skewY)
``````
2 Likes