I am working on a script that should cut a glyph into several parts. To summarize it, it should do a cut of a 10% from the left and make a second one 60% from the left as well.
To do so I am copying the paths into a new sublayer and doing each cut to each layer. However, .cutBetweenPoints() does not seem to work in layers other than the master one and it is cutting the paths on each layer in a quite random way.
Here’s a simplified version of the script as it is right now when iterating through Font.selection and retrieving g:
cLayer = g.layers[Font.masters[0].id]
layerW = cLayer.bounds.size.width
cuts = [layerW * 0.1, layerW * 0.6]
# Iterate through each cut
for i, c in enumerate(cuts):
# Create a new layer for the resulting shape
newLayer = GSLayer()
newLayer.shapes = cLayer.shapes
newLayer.name = 'Left cut %s' % i
newLayer.associatedMasterId = Font.masters[0].id
# Appending the new layer
Font.glyphs[g.name].layers.append(newLayer)
# Applying the cut
cX = cLayer.LSB + c
newLayer.cutBetweenPoints(NSPoint(cutX, layerTop), NSPoint(cutX, layerBottom))
One of the resultant layers looks like this (which is not ok):
I don’t know whether this is bug or I am doing something wrong but the main goal of this script is to two slices from one glyph in different layers. One that goes from to 10% of its width from the left and the other one to 60%.
Thank you so much for any help that anyone will provide.
Also, in a next step I need to use del(layer.shapes[i]) but for some reason it does not work either. It worked at some point but suddenly it stoped working. Using .pop(i) on the array of shapes works, though.
As far as I know, that method works the same way as the knife tool, so your screenshot suggests that it only cut through one segment, not the whole path top to bottom. Instead of layerTop/Bottom try large values like 10000 and -10000.
By the way, this method is buggy in some complex paths or if you cut right through nodes:
# Iterate through each cut
for i, c in enumerate(cuts):
# Create a new layer for the resulting shape
newLayer = cLayer.copy()
newLayer.name = 'Left cut %s' % i
# Appending the new layer
cLayer.parent.layers.append(newLayer)
# Applying the cut
cutX = cLayer.LSB + c
print(NSPoint(cutX, layerTop), NSPoint(cutX, layerBottom))
newLayer.cutBetweenPoints(NSPoint(cutX, layerTop), NSPoint(cutX, layerBottom))
Thank you Georg, this seems to work as well just like the option suggested by @alexs.
Regarding the file, find attached a file with 3 characters. While the script works for O, it does not seem to work with the A or even H. I’ve tried incrementing a bit the cutX value as Alexs said but it didn’t work and resulted with the same output.
I am actually doing it before making any cut with cLayer.removeOverlap() but maybe I need to refresh the layer passed as an argument to the function that handles the cuts since, after removing the overlap I amb calling handleCuts(cLayer).
However, the first cut is done properly but the second one is not.
Ok, I’ve solved it. I was assigning the top NSPoint() coordinate right in the y value of the cap height so the first point was actually on top of a path and this was causing the problem. Something that @alexs was pointing at in one of the responses.
I didn’t notice this until I applied new local guides at the top and bottom values of the cuts.
I’ll keep testing it everything works fine in the rest of the characters.