How to connect nodes with same position without losing one?

Hello, I am running into an issue with the GSLayer.connectPathsWithNode_andNode_() method. If the two nodes have the same position, one of them is deleted. I need to keep them both.

Is there another method of connecting nodes, which retains both nodes even if they share the same position? Thanks!

You need to code that yourself. ;(

I see, thanks. I’m dealing with separate segments, that I all want to connect.

Some ideas I came up with:

  • Create a “dummy node” somewhere (for example at (0, 0))
  • Connect the last node of the first segment to that node
  • Connect the first node of the next segment to that node
  • Remove the dummy node
  • Check whether the two nodes share the same position
  • Connect them (and lose one of them in the process)
  • Duplicate the resulting node programmatically
  • Use a pen to connect the two nodes. I haven’t tried using pens yet, is that feasible?

Do these approaches make sense? Is there any other, smarter way?

With segments, you mean GSPath objects?
In this case:
You start with:

  1. two open paths:
    1. check if the two connection nodes are in the right order. The first node needs to be the end node of its path and the second needs to be the start node. Either switch the paths or reverse one or both segments.
    2. copy all nodes from the second path to the first.
  2. one open path:
    1. close the path.

That makes sense. I didn’t realise it was that simple. Thanks!

In case anybody else is looking for this, it’s actually very easy:

for index in range(len(Layer.paths) - 1):
	for index, node in enumerate(Layer.paths[1].nodes):
		proxy_layer.paths[0].nodes.append(node)
	Layer.shapes.remove(Layer.paths[1])
Layer.paths[0].closed = True

This will iterate over all segments, which are separate paths in the layer, and copy the next path’s nodes to the first path, essentially just connecting them. It then removes the next path. In the end, the resulting single path is closed.