How to make a layer visible via scripting

Hi,
How to make a layer visible via scripting ?
I didn’t find this info the API :confused:

You mean show a specific layer in edit view?

I mean how to toggle a layer.
Capture d’écran 2022-02-10 à 20.32.33

So you mean the ‘eye’ icon? There should be a visible property on the layer. It might be missing in the wrapper. So you have to access the objectiveC methods.

font = Glyphs.font

selectedGlyph = []
for selectedLayer in font.selectedLayers:
	selectedGlyph.append(selectedLayer.parent)

for glyph in selectedGlyph:
	for layer in glyph.layers:
		print(layer.visible)
		layer.visible = True

  File "<macro panel>", line 10
AttributeError: 'NSKVONotifying_GSLayer' object attribute 'visible' is read-only

It seem that in not possible to edit the state of visible property, or I’m doing something wrong ?

Also for me not entirely clear what you mean. Maybe you want to insert the GSLayer object into Glyphs.font.currentTab.layers (treat it like a Python list).

I would like to code a script to make visible all masterLayer with same width (for example to make visible Thin Extended/Regular Extended/Black Extended)
To see this for example:

for layer in glyph.layers:
    layer.setVisible_(True)
2 Likes
font = Glyphs.font
glyph = font.selectedLayers[0].parent
masterID = font.selectedFontMaster.id
referenceWidth = glyph.layers[masterID]
for layer in glyph.layers:
    if layer.width == referenceWidth:
        layer.setVisible_(True)
    else:
        layer.setVisible_(False)

This measures the current master of the current glyph and makes visible all layers with the same width in the current glyph.

1 Like

Thanks Rainer,
Mine is a bit more messy, but also detect if the current master is italic.
Here is my code :

#MenuTitle:Show all MasterLayers of the current Selected Master width in Edit View.
# -*- coding: utf-8 -*-
__doc__="""
Make MasterLayers visible of the width of Selected Master. Visible mean enable "eye" in layer panel.
"""

font = Glyphs.font

axeDic = {}
axeIndex = -1
for axe in font.axes:
	axeIndex += 1
	axeDic[axeIndex] = axe.name

for key, value in axeDic.items():
	if value == "Width":
		widthIndex = key
	if value == "Slanted":
		slantedIndex = key


showMasters = []
for master in font.masters:
	if master.axes[widthIndex] == font.selectedFontMaster.axes[widthIndex] and master.axes[slantedIndex] == font.selectedFontMaster.axes[slantedIndex]:
		showMasters.append(master)
		

for layer in font.glyphs[0].layers:
	if layer.isMasterLayer:
		if layer.master in showMasters:
			layer.setVisible_(True)
		else:
			layer.setVisible_(False)

1 Like

I will mix yours and mine, and add it to my repo

1 Like