-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDraw Centerline.py
70 lines (56 loc) · 1.79 KB
/
Draw Centerline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#MenuTitle: Draw Centreline
# -*- coding: utf-8 -*-
__doc__="""
Draw centre line
"""
import GlyphsApp
# Glyphs.clearLog()
Font = Glyphs.font
FontMaster = Font.selectedFontMaster
selectedLayers = Font.selectedLayers
def drawPath( myCoordinates ):
try:
myRect = GSPath()
for thisPoint in myCoordinates:
newNode = GSNode()
newNode.type = thisPoint[1]
newNode.connection = thisPoint[2]
newNode.position = ( thisPoint[0][0], thisPoint[0][1] )
myRect.nodes.append( newNode )
# myRect.closed = True
return myRect
except Exception as e:
return False
def process( thisLayer ):
myPaths = []
# selection = thisLayer.selection()
# if selection == None:
# selection = thisLayer.paths
# dump each path as a list
for path in thisLayer.paths:
# new path
thisPath = []
for node in path.nodes:
thisPath += [ [ (node.position.x, node.position.y), node.type, node.connection ] ]
# re-arrange list to put last item first (GA returns list this way, not sure why)
thisPath.insert(0, thisPath.pop())
# appen path to myPaths
myPaths += [thisPath]
for eachPath in myPaths:
counter = len(eachPath)
myNewCoordinates = []
for i, coordinate in enumerate(eachPath):
# only do this for half the number of points
if counter > len(eachPath)/2:
# average each pair point
myNewCoordinates += [ [ ( (coordinate[0][0]+eachPath[(-1-i)][0][0])/2, (coordinate[0][1]+eachPath[(-1-i)][0][1])/2 ), coordinate[1], coordinate[2] ] ]
counter -= 1
layerCentrePath = drawPath(myNewCoordinates)
thisLayer.paths.append( layerCentrePath )
Font.disableUpdateInterface()
for thisLayer in selectedLayers:
thisGlyph = thisLayer.parent
thisGlyph.beginUndo()
print "Drawing centreline for %s: %s." % ( thisGlyph.name, process( thisLayer ) )
thisGlyph.endUndo()
Font.enableUpdateInterface()