HOWTO: Register a python plugin inside pcbnew Tools menu


suppose to be :
def Run( self ):
pcb = pcbnew.GetBoard()
for draw in pcb.m_Drawings:
if draw.GetClass() == ‘PTEXT’:
txt = re.sub( “$date$ [0-9]{4}-[0-9]{2}-[0-9]{2}”,
“$date$”, draw.GetText() )
if txt == “$date$”:
draw.SetText( “$date$ %s” )


which python do I need for it ?


That’s python I have in my computer,
bianchi@bianchi-HP-Pavilion-dv6-Notebook-PC:~/.kicad_plugins$ type py
py3clean pybuild pydoc pydoc3.5 pygettext3 python python2.7-config python3.5 python3.5m-config python3m-config
py3compile pyclean pydoc2.7 pygettext pygettext3.5 python2 python2-config python3.5-config python3-config python-config
py3versions pycompile pydoc3 pygettext2.7 pyhtmlizer python2.7 python3 python3.5m python3m pyversions
bianchi@bianchi-HP-Pavilion-dv6-Notebook-PC:~/.kicad_plugins$ type python2.7
python2.7 is /usr/bin/python2.7
bianchi@bianchi-HP-Pavilion-dv6-Notebook-PC:~/.kicad_plugins$ type python2
python2 is /usr/bin/python2
bianchi@bianchi-HP-Pavilion-dv6-Notebook-PC:~/.kicad_plugins$ type python3
python3 is /usr/bin/python3

Do I miss something else ?


I don’t know how to get code to format properly here but I will try

import pcbnew
import re
import datetime

class text_by_date(pcbnew.ActionPlugin):

    def defaults(self): = 'Add date on PCB'
        self.category = 'Modify PCB'
        self.description = 'Automaticaly add date on an existing PCB'

    def Run(self):
        pcb = pcbnew.GetBoard()
        for draw in pcb.Drawings():
            if draw.GetClass() == 'PTEXT':
                txt = re.sub("\$date\$ [0-9]{4}-[0-9]{2}-[0-9]{2}",
                             '$date$', draw.GetText())
                if txt == '$date$':
                    draw.SetText('$date$ %s' %


It looks ok in the preview window.

If it shows in the plugins menu now, you will probably still get some other errors because of how quickly the code base is changing. The script probably needs rewriting from the beginning.


Just substitute pcb.Drawings() with pcb.GetDrawings().
Like on this example:


:information_source: Undocumented forum tip : for best code formatting, use triple backtick : ```

e.g. typing

    if self.num_inputs>4:
        height = (self.num_inputs-1) * 100
        top = int(height/100/2) * 100


    if self.num_inputs>4:
        height = (self.num_inputs-1) * 100
        top = int(height/100/2) * 100

Now, where the heck is the “backtick” key… on my keyboard it’s the top left key above Tab


Many thanks jsreynaud, just tried the modified code and it works fine.

Thanks bobc, I will use your tip the next time I try and post something


I have made some indents as suggested and put the script on ~/.kicad_plugins

but I still can not refresh plugin ??..


Try to run your script in command line directly by running:

And share the result.


The script in the screenshot still refers to

for draw in pcb.m_Drawings:  


for draw in pcb.GetDrawings():  

which generates an error on my machine


Here’s the output


File “”, line 8 = “Add date on PCB”
IndentationError: expected an indented block


Here’s the output :


bianchi@bianchi-HP-Pavilion-dv6-Notebook-PC:~/.kicad_plugins$ python2
File “”, line 8 = “Add date on PCB”
IndentationError: expected an indented block


and from the other location :

bianchi@bianchi-HP-Pavilion-dv6-Notebook-PC:/usr/share/kicad/scripting/plugins$ python2
File “”, line 8 = “Add date on PCB”
IndentationError: expected an indented block

How to fix it ? I’m not using phyton, so I don’t understand it.


The script is improperly formatted python. Specifically the indentation is not correct (which is very meaningful in python).

The three lines after “def defaults…” must each be indented (the three lines that begin with “self.”. Also, make sure all indentions throughout the file are of the same type: either spaces (usually 4 spaces per each indention) or tabs.


Could the identation be like that ?


The menu can be displayed correctly now, thank you very much everyone :grinning:


When I click the menu I got exception :

How to fix it ?
Use the default code ?


You should add brackets after the GetDrawings: GetDrawings(). It’s a function call.


It’s working now, thanks everyone


Hey I’m also trying to get this to run. This is my version info:

Application: kicad
Version: (2017-11-28 revision f992d14)-master, release build
    wxWidgets 3.0.2
    libcurl/7.43.0 SecureTransport zlib/1.2.5
Platform: Mac OS X (Darwin 15.6.0 x86_64), 64 bit, Little endian, wxMac
Build Info:
    wxWidgets: 3.0.2 (UTF-8,STL containers,compatible with 2.8)
    Boost: 1.61.0
    Curl: 7.43.0
    Compiler: Clang 7.3.0 with C++ ABI 1002

Build settings:

I copied the plugins from MitjaNemec and jsreynaud into my ~/kicad_plugins directory like this:

I can run import replicatelayout in the scripting console but nothing happens (no error, no crash, no hang, just a new line in the console prompt). The external plugins menu remains empty as well. What am I doing wrong?


Do other action plugins work when put into ~/.kicad_plugins ?

Mine works when I put it into the installation folder (on Windows nightly)

C:\Program Files\KiCad\share\kicad\scripting\plugins\replicate_layout

Application: kicad
Version: (2017-11-28 revision f107545a8)-makepkg, release build
wxWidgets 3.0.3
libcurl/7.54.1 OpenSSL/1.0.2l zlib/1.2.11 libssh2/1.8.0 nghttp2/1.23.1 librtmp/2.3
Platform: Windows 7 (build 7601, Service Pack 1), 64-bit edition, 64 bit, Little endian, wxMSW
Build Info:
wxWidgets: 3.0.3 (wchar_t,wx containers,compatible with 2.8)
Boost: 1.60.0
Curl: 7.54.1
Compiler: GCC 7.1.0 with C++ ABI 1011

Build settings: