Python Action Plugins for pcbnew on KiCad nightly for Windows not working out of the box (path issues)

Note this post is based on the wrong installation path for the 4.0.7 release. See the later post for clarification.
TL;DR: create a scripting directory in KiCads AppData, e.g. C:\Users\none\AppData\Roaming\kicad\scripting and put your scripts there…


I managed to write a small script to generate some traces in the PCB layout, and now wanted to “package” that into an “action plugin”. Unfortunately, it does not get loaded into the “Tools > External Plugins…” menu.

Application: kicad
Version: (5.0.0-rc2-dev-466-g7b3dc9c28), release build
Libraries:
    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 8 (build 9200), 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:
    USE_WX_GRAPHICS_CONTEXT=OFF
    USE_WX_OVERLAY=OFF
    KICAD_SCRIPTING=ON
    KICAD_SCRIPTING_MODULES=ON
    KICAD_SCRIPTING_WXPYTHON=ON
    KICAD_SCRIPTING_ACTION_MENU=ON
    BUILD_GITHUB_PLUGIN=ON
    KICAD_USE_OCE=ON
    KICAD_SPICE=ON

Sources:

According to kicadplugins.i, for Windows the paths should be

        <bundlepath>/
        <bundlepath>/plugins/
        [KICAD_PATH]/scripting/
        [KICAD_PATH]/scripting/plugins/
        <kicad_config_path>/scripting/
        <kicad_config_path>/scripting/plugins/

I don’t know what bundlepath or kicad_config_path, but KICAD_PATH is undefined on my system.
My install has several candidate directories, neither of which show an effect:

  • C:\Program Files (x86)\KiCad\bin\scripting\plugins
  • C:\Program Files (x86)\KiCad\share\kicad\scripting\plugins

There’s also

  • C:\Users\none\AppData\Local\kicad
  • C:\Users\none\AppData\Roaming\kicad

but these don’t seem to contain script-related stuff.

It does not matter whether it’s in scripting/ or plugins/.

BTW, the example script I’m using is

import pcbnew

class SimplePlugin(pcbnew.ActionPlugin):
    def defaults(self):
        self.name = "Plugin Name as shown in Pcbnew: Tools->External Plugins"
        self.category = "A descriptive category name"
        self.description = "A description of the plugin and what it does"

    def Run(self):
        # The entry function of the plugin that is executed on user action
        print("Hello World")

SimplePlugin().register() # Instantiate and register to Pcbnew

So let’s try to help KiCAD a bit

set KICAD_PATH="C:\Program Files (x86)\KiCad\bin"

is not permanent, but adding it via the System settings (Advanced System Settings > Environment Variables, there’s probably a faster way to set it via CMD) did it:

>set KICAD_PATH
KICAD_PATH=C:\Program Files (x86)\KiCad\bin 

and restarting KiCad finally resulted in the script being found.

But, since it’s in the program directory, it’s a pain to work with (any file change requires admin rights).
So let’s try to move the whole directory (CMD as administrator):

C:\Program Files (x86)\KiCad\bin>mklink /D "scripting\" "C:\Users\none\.kicad_plugins"
symbolic link created for scripting\ <<===>> C:\Users\none\.kicad_plugins

Note this directory is non-standard on Windows (I hope this does not cause any further confusion)!

So, while it does work with a bit of messing around, it does not work out of the box and so IMO it requires a bit of attention please.

Also what are the other two paths and how can one find out?
Should this be a bug report?

1 Like

It should work if your script is in “C:\Users\bob\AppData\Roaming\kicad\scripting” or equivalent, if that doesn’t work I think there is a bug.

1 Like

I have reported this: https://bugs.launchpad.net/kicad/+bug/1740776. Please add “this affects me” vote there.

1 Like

That works! So it’s just the documentation that could be improved… :slight_smile:

On second thought, it would be good if the scripting directory would be created by the installer.

1 Like

Ah, now I see. It’s the “config_path” which makes it work. I have to comment on my bug report.

An actual slight bug: “Refresh Plugins” only loads plugins, it does not detect if one was removed from disk.

While there apparently is no actual unloading of modules in Python (https://bugs.python.org/issue9072), the plugin could be removed from KICAD_PLUGINS if the module does not exist in the list (https://github.com/KiCad/kicad-source-mirror/blob/master/scripting/kicadplugins.i#L243).
Alternatively, the menu action might be called something like “load new plugins”, or it could get a mouse hint, or … :slight_smile:


It also looks as if LoadOneSubdirPlugin() does not reload() plugins like LoadOnePlugin() does :


My bad! I was in the old 4.0.7 installation directory (I thought it got overwritten), so it might have worked.


Yes and no, the example in C:\Program Files\KiCad\bin\scripting does not get loaded, while C:\Program Files\KiCad\share\kicad\scripting works (and C:\Users\none\AppData\Roaming\kicad\scripting). So if kicad_config_path is for appdata (as eelik wrote), bundlepath must be the other.

I think [KICAD_PATH] is the path of the KiCad binary, e.g. “C:\Program Files\KiCad\bin”, it does not refer to an environment variable. Note that several default program folders are searched regardless of where KiCad is installed, it can pick up files in the “wrong” folder.

Bundle path is a path provided by the installer, in Windows I think this is “C:\Program Files\KiCad\share\kicad”

kicad_config_path is where the “KiCad config” file is.

In some cases KiCad has hard coded paths specific to a platform, in other places KiCad uses a wx call to get a path (see gestfich.cpp)

I’m not sure about KICAD_PATH, I still have it set to the old install path as an environment variable (where it did have an effect), but will not do further “tests”. The directory in appdata is the better solution anyway.

Hmm, after grepping for “wxGetEnv” on KiCad source tree, it seems KICAD_PATH is used on Mac, but not Windows https://github.com/KiCad/kicad-source-mirror/blob/master/pcbnew/pcbnew.cpp#L204

I think you are right about documentation though. I also think @eelik’s suggestion to add a search path in user home directory would be good.

It would also be good if KiCad had a consistent way of handling paths and data modules! Each subsystem seems to have it’s own method depending on who wrote it. I have been researching the various methods for my kipi data installer. I have most parts figured out.

It’s part of the Python loader script, and as such, should be platform-independent:

You could grep again for KICAD_PATH. :slight_smile:

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