KiCad 9.0 Python API

Moreover, Jon Evans commented this on a related issue:

I would imagine that would be up to the plugin itself to set a different icon than default if desired.

See qt - How to set application's taskbar icon in Windows 7 - Stack Overflow and python - wxpython icon for task bar - Stack Overflow

The icon is working fine:
image

But he also said something about appIDs. I can set it to something arbitrary so that it won’t group with a different python application. However, if I understand it correctly I can also set it to KiCad’s app id to group it with the KiCad application. If this is true, is there a way to get the appID? Either hardcoded or through the API?

Yes it should be. But, you should also be able to paste s-expression data using the new API eventually, there’s a command for it, just not implemented on the Python side yet or tested.

This should be possible through the mechanism KiCad uses to put things on the clipboard when you copy them, although there are some not-yet-implemented APIs in this area.

There is not currently a way to save an individual footprint to a file through KiCad’s board editor. That will definitely get added as part of adding API support to the footprint editor.

KiCad doesn’t set an explicit AppID in Windows. You might be able to call GetApplicationUserModelId function (appmodel.h) - Win32 apps | Microsoft Learn if you know KiCad’s PID, or we could add an API to call this internally (I’m not on WIndows most of the time and can’t easily test if this works)

Thanks again! I’ll continue down this road then. Right now the saving is done through normal python file interactions.

I think there is no way of getting KiCad’s PID from python, or I’m missing something.

If you add something like this to the API in a nightly build, I’ll gladly test it!

I’ve experimented and set a mostly fixed app id into kicad nightlies.

The problem is the windows behaviors may be quirky here.

For example, multiple instances of the same kicad version will now be grouped. It isn’t the worst result though.

Even though it’s possible, it’s not appropriate to feed unique identifiers into appmodeluserid, because it breaks several windows explorer functions which assume these consistently reappear. i.e. the pin to taskbar function on a running app creates the docked icon tied to an appmodeluserid, so you’ll just end up never being able to have the taskbar pin work properly if the launches app decides to create a new unique id

1 Like

Okay, that makes sense! Thanks for checking it out.

I don’t know if it is feasible, but would it be possible to add an API function that returns the current automatically created AppID of the used KiCad instance?

Version 0.1.2 has been released on PyPI corresponding with KiCad 9.0.0-rc2

After Mark’s change there is now an explicit assigned AppID, not an automatically-created one. You can test if manually assigning your plugin to use the same ID gives the desired behavior – if so, we can add this to the API at some point. You can see how it is generated here – it is not going to vary per-process or anything like that

3 Likes

I am not sure why, but the “round_tracks” example broke for me again the same way as for @albin . Other examples like the “hello.py” and “create_zone.py” example work. I had it working before but fetched the newest changes and rebuilt everything (and maybe did a few other things) and now it doesn’t work anymore. Any idea how to fix this?

@lucas I get a similar error on the round_tracks example.

Tested with KiCad Version: 9.0.0, release build, Windows 11. Python 3.11.3, kicad-python Version: 0.2.0 installed using pip. round_tracks example taken from the 0.2.0-tag commit.

I get this error when I click “Run”:

$ python round_tracks_action.py
Traceback (most recent call last):
  File "C:\git\other-repos\kicad-python\examples\round_tracks\round_tracks_action.py", line 131, in run
    for item in self.board.get_selection():
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\denne\AppData\Roaming\Python\Python311\site-packages\kipy\board.py", line 526, in get_selection
    for item in self._kicad.send(
                ^^^^^^^^^^^^^^^^^
  File "C:\Users\denne\AppData\Roaming\Python\Python311\site-packages\kipy\client.py", line 90, in send
    raise ApiError(f"KiCad returned error: {reply.status.error_message}",
kipy.errors.ApiError: KiCad returned error: no handler available for request of type kiapi.common.commands.GetSelection

I placed the example in the plugin directory instead, and it loaded just fine when I opened the PCB Editor. But when I open the plugin and click “Run” it hangs just as when I ran it from my external Python interpreter.

image

1 Like

@craftyjon Any idea of when you devs will enable the IPC API by default?

I’m 90% done with a new branch of Board2Pdf that uses IPC API (mostly cleaning left), but I don’t want to release it in PCM until the API is enabled by default, because I’m afraid that it will cause confusion and support issues.

I guess you mean the server setting? Not sure yet, maybe 9.0.1 but I haven’t looked at it yet. I wasn’t going to just enable it by default but instead add some kind of prompt to enable.

I guess I do, but just to be overly clear:

Ah, I see. Thanks for the info! Well, I guess that there’s no real rush. When I’m done with the new branch I’ll probably ask in this forum if anyone like to give it a try. And when it’s been at least somewhat tested and the server setting is enabled more intuitively I’ll release it in the PCM.

Now that V9 has been released, how does a beginner get started with the python API. Has the documentation been updated and is there a getting started guide? Or maybe a few tutorials? Thanks.

This is a good starting point:

From the page:
Automatically-generated documentation for kicad-python is available online at kicad-python documentation

There are some examples of scripts and plugins in the examples directory of the kicad-python repository that may be a good starting point for developing your own tools.

I went ahead and added something for tomorrow’s builds. You’ll need to add the optional entry "runtime": "ipc" to your version block in the plugin metadata.

Yeah, this is exactly what I see… I also tested this with the 9.0.0 release build on Windows 11. It isn’t really high priority for me at the moment to get the round track example working, but something seems to be off.

While this question was not directed to me, it was said that it will be finalized with version 10. Our plan for our addon was to release a secondary IPC version that will be merged into the main branch once the API is stable

Seems to work! I uninstalled 9.0.0 and deleted local settings, installed todays testing build, installed my plugin from PCM using a zip file, and got hit with this question:


After answering yes, the API was indeed enabled!

The plugin was installed in the correct place, but the PCB Editor fails to load it so I’ll have to debug that. When using swig based plugins it was easy to get the error messages from the failed loading in the properties, but it doesn’t seem to be possible with the ipc based plugins? Any chance this could be added?

image

There is no specific way that plugins can return “error messages” to KiCad. You can enable the console trace output though if your plugin can print errors to stderr.

Yea, that’s pretty much my understanding as well. But unless you start using the IPC API, notice which features are missing for your use case and add feature requests, then it will probably never have all the needed features. And it will never be stable unless people start using it and finds its quirks and bugs.

Better to start early and help with the bug hunt! :slight_smile:

2 Likes

Agreed, I still want to port over our coil generator soon. Haven’t had the time though

1 Like