Set Layer as Master Layer

Hi! How can I make a secondary layer to be the master layer via a script? Is it based on the order on the ID or what?

Thanks!

The layerId and the associatedMasterId have to be the same.

And could you please give me some extra info on how should I proceed? Because if I just change the id of the layer for the associatedMasterId Glyphs crashes I assume this happens because two layers has the same id or something like that.

This is code that runes when you make a layer the master in the Layer panel:

if not Layer.isMasterLayer:
	glyph = Layer.parent
	oldMaster = glyph.layers[Layer.associatedMasterId]
	if oldMaster:
		layers = glyph.pyobjc_instanceMethods.layers().copy()
		del(layers[oldMaster.layerId])
		oldMaster.name = "oldMaster"
		oldMaster.layerId = NSString.UUID()
		layers[oldMaster.layerId] = oldMaster
		del(layers[Layer.layerId])
		Layer.layerId = Layer.associatedMasterId
		layers[Layer.layerId] = Layer
		glyph.layers = layers