I’m new to Glyphs, and i like it! My Question: Is it possible to rotate a glyph (or path) via python?
… and: how can i copy one glyph or path to another (empty) glyph? I’m trying something like
currPath.initWithPathDict([pathDict-from-first-glyph]) - without any feedback/result
There is no build in functionality for that.
To rotate, you would use a NSAffineTransform (see the apple docs )
Then iterate all nodes and use the transformPoint: method from NSAffineTransform.
Transform = NSAffineTransform.transform()
Transform.translateXBy_yBy_(100, 100)
Transform.rotateByDegrees_(30)
Transform.translateXBy_yBy_(-100, -100)
for Path in Layer.paths:
for Node in Path.nodes:
Node.position = Transform.transformPoint_(NSMakePoint(Node.x, Node.y))
To copy one path, you need to add it to the Layer:
Layer = Doc.selectedLayers()[0]
Other_Glyph = Font.glyphs['B']
Other_Layer = Other_Glyph.layers[Layer.layerId]
Other_Path = Other_Layer.paths[0].copy()
Layer.paths.append(Other_Path)
For rotating, you can also use simple, plain vanilla matrix calculations. Go to the Wackelpudding.py in my Github repository and look for a function called rotate(). It takes point coordinates, an angle and rotation center coordinates, and it will return the rotated coordinates. You can iterate through all nodes of a glyph and rotate the glyph like this.
Thank you, Georg; that’s great - both solutions worked straightaway (after adjusting some values like transform, of course).
I tried “copy()” - on the layer, where it didn’t work … is there a complete list of methods somewhere?
Maybe it’s my stupor - but i wasn’t able to find that out by myself …
thanks mekkablue; didn’t see your post before - your collection of scripts is a great source of inspiration and help!
One more thing: after copying it seems that the copied glyph is somehow connected to it’s source, assume i copy “1” over to “2” - when i change the “1” after that, the “2” is also changed! Do i have to “decouple” them somehow?
To copy something, have to first get hold of is, make a copy and then add it somewhere else.
So you have to do:
NewPath = OldPath.copy()
NewLayer.paths.append(NewPath)
To copy the hole layer is a bit more complicated.
And did you see the scripting Documentation?