Running Python Plugin with cmd?

Is it possible, to run a Python Plugin without opening KiCad7?

I wrote a Plugin to autofill all the zones. Now I want to run that plugin for a “.kicad_pcb” file without opening KiCad, similar to the pcb export with “kicad-cli.exe”.

Yes, it’s possible. To load the board use pcbnew.LoadBoard(path) instead of pcbnew.GetBoard(). Some API that depends on existing pcbnew window will not work but filling zones should be fine.

Ok great.
And how do i start the skript? Which command should i use?

Same way you start any python script or program. Either python3 filename.py or python3 -m modulename

On Windows, I call python from a .bat script like this:

@echo off

rmdir /S /Q bom
mkdir bom

pushd .
call "C:\Program Files\KiCad\7.0\bin\kicad-cmd.bat"
popd

SET NAME=ProjectName

kicad-cli sch export netlist -o "bom/netlist.xml" --format kicadxml %NAME%.kicad_sch
python "C:\Program Files\KiCad\7.0\bin\scripting\plugins\bom_csv_grouped_extra.py" "bom/netlist.xml" "bom/%NAME%_grouped.csv" "Manuf" "PN"
python "%USERPROFILE%\Documents\KiCad\7.0\3rdparty\plugins\org_openscopeproject_InteractiveHtmlBom\generate_interactive_bom.py" --extra-fields PN --extra-data-file %NAME%.kicad_pcb %NAME%.kicad_pcb
1 Like

Do you know whats wrong with my code?
If I type: “python3 ZoneFillerPlugin_action.py” in cmd nothing happens.

ZoneFillerPlugin_action.py

import pcbnew
import os

class ZoneFillerPlugin(pcbnew.ActionPlugin):
    def defaults(self):
        self.name = "Zone Filler"
        self.category = "Modify PCB"
        self.description = "Fill all zones on the PCB"

    def Run(self):
        pcb_path = r"C:\Users\....\layout.kicad_pcb"
    
        board = pcbnew.LoadBoard(pcb_path)
        filler = pcbnew.ZONE_FILLER(board)
        zones = board.Zones()
        filler.Fill(zones)

ZoneFillerPlugin().register()

For a start . . . that looks like an abbreviated path rather than the full path . . . and should that “r” be there ? sorry if I’m way off, know nothing about Python.

You can’t call an action plugin directly from the command line. You need a script that has an entry point (if __main__:)

Why are you assigning a regex string to pcb_path? FYI r"string" is a regex string, Ok, r"string" is a raw string. f"string" is a format string, and just “string” is a regular string.

r"string" is not regex string, it’s raw string. This syntax allows using unescaped backslashes in the path.

https://docs.python.org/2/reference/lexical_analysis.html#string-literals

1 Like

Ah, ok I got confused because I used those to create regex matchers.

DATED = re.compile(r'.*\.(aac|mp3|pdf)$')

Yeah they are commonly used for regexes because regexes often contain backslashes themselves, and it is nice to not have to escape them.

To be honest:
I´m really confused by all those responses. I have no idea about coding in python and just searched on google.

I want to be abel to start a plugin without opening Kicad.

The follwoing Plugin already worked in KiCad.

import pcbnew
import os

class ZoneFillerPlugin(pcbnew.ActionPlugin):
    def defaults(self):
        self.name = "Zone Filler"
        self.category = "Modify PCB"
        self.description = "Fill all zones on the PCB"

    def Run(self):
        board = pcbnew.GetBoard()
        filler = pcbnew.ZONE_FILLER(board)
        zones = board.Zones()
        filler.Fill(zones)

ZoneFillerPlugin().register()

If you have no idea about coding in Python, this is perhaps a hard place to start.

The plugin you found is an action plugin, it is designed to be run from within KiCad. You need to make further modifications to it to run outside of KiCad, but if you don’t know Python, your best bet is to see if someone else is willing to do that work for you (I do not have time at the moment to make a complete version and test it)

If your goal is just to fill zones from the command line, maybe someone has a standalone script already that does this.

2 Likes

Ok thanks for the explanation!

For me, using the CMD-line in the Python Console is bothersome so, for non-Kicad-specific App’s, I prefer to build the Python code as an executable Stand-Alone-App and Run it (call it) from a Kicad Plugin.

So, first step is to build the code so it runs by using the Simple Plugin code setup (search for the Documentation). And, place your .py in the Scripting>Plugin folder (add an Icon to it or it gets a default icon). If it does the stuff on the PCB you want, continue…

It should run. If successful, you can build an Executable App by various means - I’ve used several but, prefer PyInstaller (note the Uppercase letters - there’s a bad look-alike pyinstaller (same name but Not the same code).

Once successful making it a Stand-Alone App, you can run it from a plugin by following my post(s) here. If you scroll through the full post, you’ll see link to my Colorizer App that contains a Plugin Folder (it contains the needed code but, you may need to Tweak it per the discussion so it runs on your OP system…)

you can modify this plugin a bit and make it work from cmd.

Create zonefiller.py:

import pcbnew

if __name__ == "__main__":
    board_path = "/home/aws/demo.kicad_pcb"
    board = pcbnew.LoadBoard(board_path)
    filler = pcbnew.ZONE_FILLER(board)
    zones = board.Zones()
    filler.Fill(zones)
    pcbnew.SaveBoard(board_path, board)

and run it with python zonerfiller.py

1 Like

It sadly doesn´t work.
Is Python 3.11.4 (64-Bit) the right Version?
Is there anything else that has to be installed? (besides KiCad obviously)
Do i have to change anything in the “main”?

When reporting that something doesn’t work you should specify as much detail as you can so others can help you. How exactly doesn’t it work? Was there any error output? How did you run it? Did you edit the path to the board? If you are on windows are you using python bundled with kicad?

I did exactly as adamws told me and changed the path.
When I typed python zonefiller.py in the cmd nothing happend. The cmd had no output and the zones weren´t filled.

And what do you mean with: “python bundled with kicad”? The Python scripting console?
Sorry I´m completly new to KiCad an Python.