Get active footprint library path in Python

Edit: Changed category to Software. I’m hoping this is correct, “External Plugins” seemed wrong when I read the description.

Is there any way in Python to get the path to the active footprint library?

I’m a beginner both in Python and in Kicad/Pcbnew scripting. I’m trying to iterate over all modules on the board and listing the 3D Models associated with the footprint in the lib. Later I will update each module on the board which does not correspond with the footprint in the lib, much like the “Update Footprints from Library” function in Pcbnew.

I have written a small loop which successfully lists the 3D model, but when I use ‘FootprintLoad()’ it requires me to specify the path to the library. I would like to use the path of the active library in Pcbnew instead, but I really can’t figure out how to get this path.

pcb = pcbnew.GetBoard()
for mod in pcb.GetModules():
fpid = mod.GetFPID()
footprint = pcbnew.FootprintLoad(“C:/git/am-hardware/lib/KiCAD.pretty”, fpid.GetUniStringLibItemName())
lib_component = pcbnew.MODULE(footprint)
lib_model_list = lib_component.Models()
for lib_model in lib_model_list:
print('Lib 3D model filename: ’ + lib_model.m_Filename)

I don’t think there is a concept of “active library” in pcbnew board editor. The footprint picker when you place components just remembers last chosen component, that’s the closest thing to it probably. I don’t think that is exposed in API.

Or are you looking for a way to get library path of a MODULE object you are looking at? I don’t think there is a way to do that, it’s not in the data model. At most you can get library nickname from GetFPID() but then you have to manually parse fp lib table (both global and local) to find the path.

Thank you for your answer!

Yes, I’m basically trying to get the library path of a module object I’m looking at, in order to use FootprintLoad to load the same kind of module from the lib.

I tried using str(fpid.GetLibNickname() and that does indeed return “AM” which is the nickname of my lib specified in “Manage Footprint Libraries…”-dialogue. But in order to satisfy FootprintLoad I need to translate that to a path…

Do you have any idea of how I could parse fp_lib_table in order to find the path? Is there any function which can return this table so I can iterate over it? I’ve tried for hours to find a way to do this. There is a namespace ‘FP_LIB_TABLE_T’ available, but I have no idea of how to use it or if I can use it.

The “Update Footprints from Library” function in Pcbnew does a great job at finding the footprint modules in the lib, based on the modules on the board, so I’m hoping that it’s possible to do similar things in Python.

Kind regards,

Take a look at the work from Miles Mccoo. IIRC he did some work to list the libraries and/or footprints

Thanks for the advice! I have looked at that but as I understand it he made a patch to kicad which never got merged. I would love the pcbnew.GetLogicalLibs() function, but I don’t think that its available in standard release of pcbnew?

To my knowledge there is no API to get lib table. That’s why I was suggesting parsing fp-lib-table files manually, it’s a hack but can be done. They are just s-expression text files. Making variable expansion work will be a bit tricky but you may not even need that if you just want to make it work for your particular setup.

Thank you for the clarification! It’s pretty easy to make it work just for my particular setup, I was hoping for a fairly easy way to make it work with every setup. Need to give this some thought.

Do you know if the path to the fp_lib_table files can be found in some environment variable, so that I at least know where to find the files on all operative systems?

It’s in the settings directory which is platform dependent. Ultimately it comes from wxWidgets. I don’t know if there’s KiCad python API to find it.

1 Like

To get the config folder use GetKicadConfigPath()


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.