Getting error `object has no attribute LAYER_ID_COUNT' in KiCad PCBNEW trying to add track in Python script
The following code will create the layertable table used by the above example (and many other examples)...

layertable = {}

for i in range(1000):
    name = board.GetLayerName(i)
    if name != "BAD INDEX!":
        layertable[name]=i


It is a horrible hack, but it works. It makes the reasonable assumption that there will never be more than 1000 layers.

Once the above code as set up the layertable dictionary, you can assign a layer to a track like this...

track.SetLayer(layertable["F.Cu"])