GSLayer.cutBetweenPoints() not working

Hello,

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):

It also makes cuts to the master layer which I am not manipulating at all and it shows the resultant paths without some of the path nodes.

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.

Ricard.

You probably need to make a copy of shapes, otherwise you’re cutting the original paths. Instead of this

newLayer.shapes = cLayer.shapes

try

newLayer.shapes = [shape.copy() for shape in cLayer.shapes]

Thank you so much! That was it! Now I just need to know why in some very steep segments it is not working properly.

Is it a bug or is it somehow manageable?

@alexs @GeorgSeifert

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:

There’s been discussions on the forum, the workaround was to add some small value to the coordinate, like x+=.00001

1 Like

Try this:

# 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))
1 Like

About the steep segments: could you send me a sample file?

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.

CutPathSample.glyphs (5.3 KB)

the A works for me. In the H, the overlap is messing it up a bit. So you might like to remove the overlap before you run the script.

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.


How does your script looks like @GeorgSeifert?

Again, thank you so much.

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.

Thank you so much for the help @alexs and @GeorgSeifert!

I usually use 10000 and -10000 as the end coordinates for the cutting line to avoid such surprises.

1 Like

Makes lots of sense, that’s what I’ll do with this script and similar ones.

Thanks again.