How to access the View Coordinate Value of a selected node?

If a Node (or a group of nodes) is selected, how can I access its View Coordinate Values?

Where exactly do you need that? There are several places (mostly repeater plugins and draw callbacks) where you can just use the position of the node directly.

In those other cases, use something like this:

activePos = currTab.activePosition() # origin of the layer in view coordinates. 
scale = currTab.scale
pos = node.position
posInViewCoordinates = NSPoint(activePos.x + (pos.x * scale), activePos.y + (pos.y * scale))

I was thinking that the View Coordinate Value of the selected node might be what I need to figure out how to set the origin for zooming. Relating back to this post.

Then my snipet should get you the result you need.

Trying this:

font = Glyphs.font
currTab = font.currentTab

activePos = currTab.activePosition() # origin of the layer in view coordinates. 
scale = currTab.scale
pos = node.position
posInViewCoordinates = NSPoint(activePos.x + (pos.x * scale), activePos.y + (pos.y * scale))

and get this error:

AttributeError: 'NSKVONotifying_GSEditViewController' object has no attribute 'activePosition'

Does viewPort.origin.x/y access that same value as .activePosition(), meaning the top-left corner at the ascender height?

I got one part of the conversion to work. I had to modify your snippet to a bit (changed + to - in the posInViewCoordinates).

From the selected node, the x-coordinate is converting to the correct view coordinates (I think? it at least has the correct difference from the origin node) but for some reason, I can’t figure out why the y-coordinate isn’t converting.

Any idea what is going wrong?

In the example, the scale is set to 1 to help simplify and maintain a 1:1 relationship in the view coordinates (vc) and glyph coordinates (gc). The origin in vc is -250, -250 and in gc it is 0, 700 so when a node is selected that is 100 units right of the origin, the x-coord is at -350.

scale_issue

# get current view port
editView = Font.currentTab
viewPort = editView.viewPort.copy()
# selected node
selectedLayer = Font.selectedLayers[0]
selection = selectedLayer.selectionPath().bounds()
pos_x = selection.origin.x
pos_y = selection.origin.y
# origin center to viewport
activePos_x = (0 - viewPort.size.width/2)
activePos_y = (0 - viewPort.size.height/2)
#scale of current tab
scale = editView.scale

posInViewCoordinates = NSPoint(activePos_x - (pos_x * scale), activePos_y - (pos_y * scale))

diff_x = activePos_x - posInViewCoordinates.x
diff_y = activePos_y - posInViewCoordinates.y
# set origin
viewPort.origin.x = activePos_x
viewPort.origin.y = activePos_y
# reposition view port of Edit View
editView.viewPort = viewPort

Got it!
Will post here once I clean up the code some.

You don’t need that .copy() (I’m surprised that it even works).
and you really need to take currTab.activePosition() into account. Try your code on the second layer in the edit view.