Get anchors from components

Hello! I tried to use Guido’s script to import anchors from components. But it doesn’t work properly. Can someone please share a script to import the original anchors from the components? Thanks a lot!

Not on my Mac now, but should be sufficiently easy to write yourself. Loop through layer.anchorsTraversingComponents() and append to layer.anchors what is not there yet.

1 Like

I’m still far from understanding the coding. But it’s something that I will learn for sure. @FlorianPircher Maybe you can help with it, too? :slight_smile: Thanks!

What do you mean exactly by importing anchors? From where to where? I imagine you have a composite made up of at least one component and want to paste the anchors of the selected component(s) to the composite layer. Is that correct?

In that case, try this:

for component in Layer.components:  # loop through all components in the current edit layer
	if component.selected:  # if the component is selected
		for anchor in component.component.layers[Layer.associatedMasterId].anchors:  # loop through all anchors in the component (in the same master as the current edit layer)
			Layer.anchors[anchor.name] = GSAnchor(name=anchor.name, pt=(anchor.position.x + component.position.x, anchor.position.y + component.position.y))  # in the current edit layer, create a new anchor with the same name and position of the component's anchor, adjusted for the position of the current component

This will copy the anchors of the selected component(s) in you currently open glyph to the current layer. I added some comments to clarify the code.

1 Like

Thank you @SCarewe I have glyphs with a few components, and I need to get the anchors from the original glyph. Will it work?

I don’t quite understand your use case. What exactly do you want to do? What do you mean by “getting” anchors?

1 Like

I’ll try to explain. I have group of glyphs. inside of them, I have components. So I want to select a few glyphs, and place the same Anchors I have on the original shape of the component, on the component themselves. Is it more clear?

For every glyph meaning for the frontmost layer in the glyph (so, in the currently selected master), or for every master in every selected glyph?

Other question, why do you want to do this in the first place? Using anchors on top of components is seldom a good idea.

1 Like

Yes, for the selected glyphs. In the front most layer. In the currently selected master.

Because… My components, are complex. So I need to build them, and then be able to change them with other components… So I need the anchors in place.

For all layers of all currently selected glyphs:

for selection in Font.selectedLayers:  # loop through all currently selected layers
	parent = selection.parent  # find the glyph belonging to the layer
	for layer in parent.layers:  # loop through all layers (masters) of the glyph
		for component in layer.components:  # loop through all components in the layer
			for anchor in component.component.layers[layer.associatedMasterId].anchors:  # loop through all anchors in the component (in the same master as the current edit layer)
				layer.anchors[anchor.name] = GSAnchor(name=anchor.name, pt=(anchor.position.x + component.position.x, anchor.position.y + component.position.y))  # in the current edit layer, create a new anchor with the same name and position of the component's anchor, adjusted for the position of the current component

For only the current master:

for layer in Font.selectedLayers:  # loop through all currently selected layers
	for component in layer.components:  # loop through all components in layer
		for anchor in component.component.layers[layer.associatedMasterId].anchors:  # loop through all anchors in the component (in the same master as the current edit layer)
			layer.anchors[anchor.name] = GSAnchor(name=anchor.name, pt=(anchor.position.x + component.position.x, anchor.position.y + component.position.y))  # in the current edit layer, create a new anchor with the same name and position of the component's anchor, adjusted for the position of the current component

All this (and much, much more) is very well documented in the Glyphs Python API.

Edit: Removed the line making it run only on selected components

2 Likes

Thank you! I tried the script, but nothing happened…

True. I forgot to remove a line. Can you figure out which one it is? :wink:

1 Like

I’m trying really hard :)))

You don’t want the script only to work on selected components, as you are batch-processing multiple layers and thus are not manually selecting specific components. Remove the condition if component.selected and the script will treat all components, not just the selected ones.

1 Like

That makes so much sense! And it works! True, I have to learn the basics. I have to. I wish there were video tutorials. It would be so much easier to learn. @SCarewe Thanks a lot once again for saving me here. I appreciate it. And thank you for pushing me to learn to script.

A suite of video tutorials is a great idea. I personally found the tutorials at Scripting Glyphs, part 1 | Glyphs to be very comprehensive, but maybe videos could be made too, one day™

1 Like

Yes. That will be amazing!

Why do you like to get the anchos in the composite glyph? For most parts Glyphs will do that automatically if needed.

1 Like

Hi @GeorgSeifert Yes. it does it automatically only the first time you place the components. But then, you copy this glyph and paste it as a component, and you want to add another component to this glyph. It’s impossible to place it in the right place without another anchor. I hope it makes sense to you. Thanks!