Designing footprints with pcbnew Python scripting - how to save to library?

I’m looking to edit my footprint libraries programmatically, and I’ve found some functions in the pcbnew API that seem useful.

Here’s the workflow I’m looking at:

  1. Grab paths for all my custom KiCad libraries from my filesystem
  2. For every library, get all footprints in the library (footprints = pcbnew.FootprintEnumerate(library_path)
  3. For every footprint, do the edits I want (e.g. for f in footprints: f.SetField( whatever ))

I’m testing this step by step in the KiPython window, and although I’m making the changes I want in step 3, I’m not seeing them reflected when I open the Footprint Editor. I probably need to save them, but pcbnew.FootprintSave(library_path, module) throws the following error:

pcbnew.FootprintSave( 'librarypath.pretty' , 'footprint.kicad_mod')
Traceback (most recent call last):
  File "/usr/lib/python3.10/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/pcbnew.py", line 19641, in FootprintSave
    return plug.FootprintSave(libname,module)
  File "/usr/lib/python3/dist-packages/pcbnew.py", line 8120, in FootprintSave
    return _pcbnew.PCB_IO_FootprintSave(self, aLibraryPath, aFootprint, aProperties)
TypeError: in method 'PCB_IO_FootprintSave', argument 3 of type 'FOOTPRINT const *'
Additional information:
Wrong number or type of arguments for overloaded function 'PCB_IO_FootprintSave'.
  Possible C/C++ prototypes are:
    PCB_IO::FootprintSave(wxString const &,FOOTPRINT const *,STRING_UTF8_MAP const *)
    PCB_IO::FootprintSave(wxString const &,FOOTPRINT const *)

I’ve tried to look for documentation on what “module” means in this context, and the error message is not illuminating. I’ve also tried running the above command without .kicad_mod and that hasn’t worked either.

How can I make sure my footprint changes are saved to the library? Is FootprintSave the right function to be using? If so, what should be used for the module variable?


I hereby certify that I am not simply asking someone else to design a footprint for me.

This is an auto-generated message that is in place on the “footprints” section of the KiCad.info forum. If I remove it and ask for a footprint to be designed anyway, I understand that I will be subject to forum members telling me to go design my own footprint or referring me to a 3rd party footprint site.

Specify your kicad version because api varies quite a bit.

Generic advice I can give is that judging by the error kicad expects you to pass a string and a FOOTPRINT object to FootprintSave(), not 2 strings.

I’m using KiCad 8.0.7. I tried passing the FOOTPRINT object as the second argument and that worked, thank you!