Is there a Python package for modifying *.kicad_mod files

I have made a bunch of footprints over the years, and currently trying to clean them up and get them consistent. For example most dont have a %R, e.g.

(fp_text user %R (at 0 6.35 90) (layer F.Fab)
    (effects (font (size 1 1) (thickness 0.15)))
  )

I started writing a python script to set silkscreen line width sizes, and silkscreen text thickness, and ref designator text.

from pathlib import Path
import re

p = Path.cwd().parent.joinpath('mylib.pretty')
files =  p.glob('*.kicad_mod')

CONVENTIONS = {
        'silkscreen_line_width' : ['0.15', r'(?<=layer "F.SilkS"\) \(width )(\d.\d\d?)'],
        'silkscreen_text_width' : ['0.15', r'(?<=\(layer "F.SilkS"\)\n    \(effects \(font \(size 1 1\) \(thickness )(\d.\d\d?)'],
        'silkscreen_ref' : ['REF**', r'(?<=fp_text reference ")([a-zA-Z0-9*]+)(?=")'],
}
for filename in files:
    print('Opening ', filename)
    with open(filename, 'r') as f:
        content = f.read();
    edited = content
    for convention, [correct, pattern] in CONVENTIONS.items():
        edited = re.sub(pattern, correct, edited)
    with open(filename, 'w') as f:
        content = f.write(edited);

However when it comes to added attributes that arent already there it gets tricker.
I have searched for docs on the *.kicad_mod file format but did not find.

I did come across this: https://gitlab.com/kicad/libraries/kicad-footprint-generator which is about making new footprints, not editing existing footprints.

Is there a generic KiCAD lib for altering kicad_mod files? failing that looking at the format it seems like some sort of LISP format. Is there a generic LISP file parser/modifier in python? Similar to how you get them comes for JSON/XML?

Pcbnew has a python API. It can handle footprints, but I’m not sure how it happens when you have to read and write footprint files.

You can start by opening Pcbnew -> Tools -> Scripting Console.

1 Like

Quite a lot of the Footprints in KiCad’s default libraries are generated from Python scripts.

This:
https://html.duckduckgo.com/html?q=kicad+footprint+generator

gets you to results such as:

https://github.com/pointhi/kicad-footprint-generator
https://kicad.github.io/footprints/

Yes, there is. Look for “s-expression parser”, there are some choices.

Thank you! This is a new world for me from an old world… “car” and “cdr”. I wonder why they choose “s-expression” over a more modern format like XML or JSON.

Thanks, I actually had come across that (posted in original question, but the new gitlab repo). What I could see from it, was that its about generating new components, not parsing existing components into an object that can be manipulated, then rewritten.

Take a look at this one

There are some others in “pip”, too:



Wow real thunder is quite prolific. I have been seeing his name a lot with his FreeCAD work, what a champ. Thank for the link.

Perfect thank you! For anyone else interested, I recommend sexpdata https://sexpdata.readthedocs.io/en/latest/, from a quick review it seemed like it had the best documentation, and that is often an indicator to code quality.
Cheers!

1 Like

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