Desperately need help with opening/closing corners, node indices

Hello. I am (simply said) trying the following:

I have a square (so four straight line segments). For each straight line segment, I want to open the corners of this segment and then close them again. This order is important (not open a corner, then close it, then go to the next one – both corners need to be open at the same time and then closed afterwards).

My approach is the following: for every node of the segment, open the current node and the next node, then close them both again. I am sadly failing at the last node of the path. Here is my code:

select_tool = NSClassFromString("GSToolSelect").alloc().init()

def convert_node_index(nodes, index):
	new_index = index % len(nodes)
	return nodes[new_index]


for path in Layer.paths:
	nodecount = len(path.nodes)
	index = 0
	while index < nodecount:  # make sure only the original number of nodes is used
		# open the current node
		Layer.openCornerAtNode_offset_(convert_node_index(path.nodes, index), 10)
		# open the next node (whose index is one higher now)
		Layer.openCornerAtNode_offset_(convert_node_index(path.nodes, index + 2), 10)
		# close the first corner
		select_tool._makeCorner_firstNodeIndex_endNodeIndex_(path, convert_node_index(path.nodes, index).index, convert_node_index(path.nodes, index + 1).index)
		# close the second corner
		select_tool._makeCorner_firstNodeIndex_endNodeIndex_(path, convert_node_index(path.nodes, index + 1).index, convert_node_index(path.nodes, index + 2).index)
		# finally, increase the index by one to go to the next node 
		index += 1

But, for reasons incomprehensible to me, the following happens:

… when what should actually happen is that the path looks exactly the same as before.

There is something very basic wrong in my logic. I have spent far too many hours trying to debug this. Please put me out of my misery.

Why do you like to do that?

I want to transform each straight segment separately, without changing the others, so the easiest way for me seemed to be to open the corners, then transform the segment, then close the corners again, etc.

select_tool = NSClassFromString("GSToolSelect").alloc().init()

def convert_node_index(nodes, index):
	new_index = index % len(nodes)
	return nodes[new_index]


for path in Layer.paths:
	nodecount = len(path.nodes)
	
	for index in range(nodecount - 1, 0, -1):
		Layer.openCornerAtNode_offset_(convert_node_index(path.nodes, index - 1), 100)
		Layer.openCornerAtNode_offset_(convert_node_index(path.nodes, index + 1), 100)

		select_tool._makeCorner_firstNodeIndex_endNodeIndex_(path, (index + 1) % len(path.nodes), (index + 2) % len(path.nodes))
		select_tool._makeCorner_firstNodeIndex_endNodeIndex_(path, (index - 1) % len(path.nodes), index % len(path.nodes))

Black magic. Thanks a lot.

Doing stuff like this backwards is often a good idea. But it might not be needed her.

Thanks a lot for the invaluable help, if you’re interested you can have a look at the (very unfinished!) project here:

A lot of cases still don’t work, for example multiple-path layers, but oh well. Todo.

2 Likes

Most likely you can keep the italic extremes.

Yes, indeed, in fact I need to. I still would like to find a method to remove italic extremes in favour of H/V extremes if the resulting curve isn’t too different. But first I need to find out why the filter fails when multiple paths are present.