How to call "Show all glyphs that use this as a component" via script?

How to call “Show all glyphs that use this as a component” via script?

Probably there is a way shorter approach, but I once wrote a work around in this script (Nevermind the old methods for opening tabs and so :smiley: )

1 Like

Ha! thanks :sweat_smile: I was hoping there would be something in the API.

I am curious as well about that. And for an information if a glyph is reused at all. It got to be somewhere. Wanted to use it for a reporter, but my loop slowed down everything like hell :vhs:

This is all implemented in the select tool. it is not really useable to access it from a script. I could refactor it a bit to improve on it but this would go pretty low in the list.

1 Like

Sounds nice. No rush at all. Put it at the very bottom if at all. I was just curious, since there is so much stuff accessible already. And the script does what it shall do : )

Is something like glyph is reused accessible as a property already? I still need this pretty often in certain scripts and find that checking each glyph agains each other glyph in a font manually is incredibly slow.

Here a simple sketch as example (run with a font that has Cyrillic and/or Greek):

import time
thisFont = Glyphs.font
allGlyphsInFont = [g.name for g in thisFont.glyphs]
start_time = time.time()

def glyphIsReusedInAnotherScipt(glyphName, masterIDX):
	reusedGlyphs = []
	for thisGlyphName in allGlyphsInFont:
		thisGlyph = thisFont.glyphs[thisGlyphName]
		try:
			masterLayer = thisGlyph.layers[masterIDX]
			layerComponents = masterLayer.components
			if len(layerComponents) == 1:
				if layerComponents[0].componentName == glyphName:
					reusedGlyphs.append(thisGlyphName)
		except: pass
	if len(reusedGlyphs) > 0:
		reusedGlyphs.insert(0, glyphName)
		return reusedGlyphs
	return None
	
for gName in allGlyphsInFont:
	isReused = glyphIsReusedInAnotherScipt(gName, 0)
	if isReused:
		print isReused
		
print("--- %s seconds ---" % (time.time() - start_time))

Apparently there must be a method built in to Glyphs that runs lightning fast when clicking Show all glyphs that use this glyph as a component

Here is a slightly optimized version of the script:

thisFont = Glyphs.font

def glyphIsReusedInAnotherScipt(glyphName, masterID):
	reusedGlyphs = []
	for thisGlyph in thisFont.glyphs:
		try:
			masterLayer = thisGlyph.layers[masterID]
			layerComponents = masterLayer.componentNames
			if glyphName in layerComponents:
				reusedGlyphs.append(thisGlyphName)
		except: pass
	if len(reusedGlyphs) > 0:
		reusedGlyphs.insert(0, glyphName)
		return reusedGlyphs
	return None
	
def tunTest():
	for g in thisFont.glyphs:
		isReused = glyphIsReusedInAnotherScipt(g.name, thisFont.masters[0].id)

import cProfile
cProfile.run('tunTest()')

but this is indeed much faster:

thisFont = Glyphs.font
def tunTest():
	for g in thisFont.glyphs:
		isReused = thisFont.glyphsContainingComponentWithName_masterID_(g.name, thisFont.masters[0].id)
import cProfile
cProfile.run('tunTest()')

The native method does almost exactly the same thing but is still 10 times faster.

3 Likes

This looks very promising. Thank you so much, Georg! First test is going down from about 30 sec to 0.35 sec. After rewriting the function to my exact need, it executes in a blink of an eye now :slight_smile: – Implemented in Kernkraft.

2 Likes