Segment type - NSArray object has not attribute 'type'

I developed and tested a new script (https://twitter.com/luke_prowse/status/1102532938934022144) on 2.4.2 + 2.5.2 + 2.6.1. Foolishly only on my own machine :expressionless: On a friend’s machine this error pops up. Seems like I can read a segment type but he can’t?

AttributeError: ‘__NSArrayI’ object has no attribute ‘type’

The error says that segment is an NSArray, not an segment as you expect.

I think the problem is that if any other script before yours is importing RoboFab, the path.segment is overloaded to return a list of RSegments. Your college has no RoboFab, so the default implementation is used.

I highly recommend to use the path.nodes API.

for path in Layer.paths:
	index = -1
	for node in path.nodes:
		index++ # add the increment at the beginning that you dont need to worry about `continue` calls.
		if node.type == LINE:
			prefNode = path.nodes[index - 1]
			tp0 = prefNode.position
			tp1 = node.position
			dist = math.hypot(tp1.x - tp0.x, tp1.y - tp0.y)
			pathTotalLength += dist
			straightlinepts = self.PointToPointSteps(tp0, tp1, spacebetween)
			for sl in straightlinepts: allpointslist.append(sl)
		elif node.type == CURVE:
			...
1 Like

Aye aye aye. I didn’t even realise this was RoboFab. I’ve been repeatedly recycling some of my early glyph-scripting explorations unaware of this.

Will research the path.nodes api and update. Thanks Georg!!!

1 Like

Finally looked in to this and a bit confused:

I don’t think I’m doing any Robofab import and neither was my colleague. Why would the native Glyphs API be overrun by something extrinsic? The API states the ability to read read a path by segments. Question why it’s there at all.

From what I can tell in order to simulate a read-as-segments using path.nodes I would have to make a fairly labor heavy loop (compared to path.segments), searching back and forth in the list with conditions, complicated further by a first node in the list sometimes being a handle.

It seems a bit over complex at first sight. Particularly from a visual pov.

If the reality is path.segments is untrustworthy I’ll deal with it. Also just trying to understand as drawing/reading path structures is a fundament.

You get used to the more direct acces.
And the segments itself are quite heavy as they are generated every time you call path.segments.
And it can be any other plugin to import robofab. I know it is not ideal. I clean it up in the next version.

1 Like

Ok :slight_smile: Better to get used to it now than later. Thanks.