How to get footprint graphical elements using KiCAD pcbnew plugin Python API

See our previous post How to get list of all footprints using KiCAD pcbnew plugin Python API and How to get all selected footprints using KiCAD pcbnew plugin Python API for more info on how to obtain a footprint object.

Once you have a pcbnew.FOOTPRINT object, you can get its graphical elements (which are basically the lines, rectangles, circles etc in the footprint – but not any silkscreen labels) using

footprint.GraphicalElements()

Example:

for graphical_element in footprint.GraphicalItems():   
    print(graphical_element)

Output:

<pcbnew.FP_TEXT; proxy of <Swig Object of type 'FP_TEXT *' at 0x7fc47f2c41e0> >
<pcbnew.FP_SHAPE; proxy of <Swig Object of type 'FP_SHAPE *' at 0x7fc47f0f10b0> >
<pcbnew.FP_SHAPE; proxy of <Swig Object of type 'FP_SHAPE *' at 0x7fc47f2c69d0> >
<pcbnew.FP_SHAPE; proxy of <Swig Object of type 'FP_SHAPE *' at 0x7fc47f2c6d30> >
<pcbnew.FP_SHAPE; proxy of <Swig Object of type 'FP_SHAPE *' at 0x7fc47f0f10e0> >
<pcbnew.FP_SHAPE; proxy of <Swig Object of type 'FP_SHAPE *' at 0x7fc47f2c6cd0> >
<pcbnew.FP_SHAPE; proxy of <Swig Object of type 'FP_SHAPE *' at 0x7fc47f2c6ca0> >
<pcbnew.FP_SHAPE; proxy of <Swig Object of type 'FP_SHAPE *' at 0x7fc47f0f38d0> >
<pcbnew.FP_SHAPE; proxy of <Swig Object of type 'FP_SHAPE *' at 0x7fc47f2c68b0> >
<pcbnew.FP_SHAPE; proxy of <Swig Object of type 'FP_SHAPE *' at 0x7fc47f2c41e0> >
<pcbnew.FP_SHAPE; proxy of <Swig Object of type 'FP_SHAPE *' at 0x7fc47f0f10b0> >
<pcbnew.FP_SHAPE; proxy of <Swig Object of type 'FP_SHAPE *' at 0x7fc47f2c69d0> >
<pcbnew.FP_SHAPE; proxy of <Swig Object of type 'FP_SHAPE *' at 0x7fc47f2c6d30> >

You can filter them using

fp_texts: list[pcbnew.FP_TEXT] = [
    item for footprint in selected_footprint.GraphicalItems()
    if type(item).__name__ == 'FP_TEXT'
]
fp_shapes: list[pcbnew.FP_SHAPE] = [
    item for footprint in selected_footprint.GraphicalItems()
    if type(item).__name__ == 'FP_SHAPE'
]