I am trying to programatically add a new track to a KiCad board using the Python API. My code fails when trying to assign the layer to the new track.

My code is modeled off this example...


...which builds a dictionary of available layers, and then uses that dictionary to look up the desired layer by name.

Unfortunately the field pcbnew.LAYER_ID_COUNT doe snot seem to exist in the latest version of KiCad (5.1.2). To confirm, I typed the following into the Python command prompt...

import pcbnew
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'module' object has no attribute 'LAYER_ID_COUNT'

How can I work around this issue?


1 Answer 1


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!":

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...


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.