Tutorials on python scripting in pcbnew


So you want too select modules with the script and edit them with the UI Interface? Or am I on the wrong track.

Had a look in the source code (especially at block_module_editor.cpp). I’m not so familiar with C++, but I think they work with List, we don’t have access to from Python.
Why not use the script to modify the modules?


@pwuertz I like the idea of creating your own selection commands. I don’t like the popup listing the different filters. Like you, I also think the filters are not sufficient. I’ll poke around and see if I can find an appropriate python command(s)

@upanie I’ll see if I can find a command to rezoom programatically. I’ve already been looking for the command to redraw (simpler than zoom). Assuming I find it, I imagine zoom will not be far away.

Good ideas for future topics. Thank you.


@DougE Yes, I would like to select modules by script and then move/manipulate the block using the UI (i.e. I still want to use the UI for working on the layout).

Often I find myself in a situation where I’d like to move around a group of objects coherently, but the need to select every object single handedly whenever I loose the selection really bothers me. PcbNew does not have a “group” feature, so I figured defining groups via script and recall them would be the easiest approach (much like CTRL+Number lets you define/recall groups of units in mainstream strategy games).

The point is, nothing I do from the scripting interface seems to have a direct effect on the UI. If you change the position of an object for example you need to click around a bit until the UI actually picks up the change and draws the new position.

@mmccoo You mentioned a redraw or update command for the UI? Maybe a module.SetSelected() followed by ui.redraw/update() will do the trick?


I have added another post to my blog. This time it’s about how to add your own command on the top, left, or right toolbars.

kicad.mmccoo.com (I’m not posting a direct link for fear that I’ll get flagged again for spamming. If you feel this thread is spam, please give a comment on why)

To summarize the important bits:

import wx
import wx.aui # this is the key one

Enables you to use the wx inspection tool

import wx.lib.inspection

These exports also cause pcbnew to give you the correct wxApp and wxAuiControl pointers (otherwise, you’d get a parent class pointer missing the interesting APIs.)

I appreciate the topic ideas I’ve gotten on this thread:

  • zoom
  • redraw (though I guess that’s my own)
  • autoload python
  • special selections (this one could be hugely useful, I think. I’m thinking I could use this with sheet based schematics.)



Thanks again for taking the time to share your knowledge. :+1:

I really hope the spam flag was some kind of mistake. Please mods confirm this.


I checked, @davidsrsb took care of that and as far as I can see he didn’t agree with the @system flagging him for spamming.
The system is just a little bit more careful with new users as @mmccoo joined on 17.2.2017, so he fell into the raster as basic user it seems… don’t know when he’ll get member status, there are a couple of tiny hurdles to jump over for:


I oked @mmccoo posts and unblocked them. To get promoted @mmccoo needs to read some other threads and post in a few. Reading the forum rules gets you a badge too


Thank you @dBman, @Joan_Sparky and @davidsrsb for the support and info on how the system works. I’d assumed it was just generic infrastructure + overzealous flag police. Guess I shoulda rtm :slight_smile:


What type of language would you prefer for writing KiCad scripts?

Unfortunately not that easy… this info is pretty well hidden, gotta go to your Preferences and there to Badges and then hopefully there is a link somewhere… :smirk:


I have added a new post to the list of tutorials about python scripting in pcbnew.

In a nutshell, there are two new python APIs (you’ll need to use a nightly build or recompile yourself)



x = pcbnew.FromMM(10) y = pcbnew.FromMM(10) width = pcbnew.FromMM(20) height = pcbnew.FromMM(20) pcbnew.WindowZoom(x, y, width, height)

Folks that have expressed interest in such a capability: @upanie @pwuertz


@mmccoo Unfortunately the Refresh command doesn’t do the trick for me. There is probably another mechanism involved in the selection handling of objects in pcbnew.

I did check out the Zoom function just for fun. It only works for the classic view though, not with the OpenGL canvas enabled. I liked your post on adding such functionality yourself and found your git commit. It’s a nice entry point for someone without knowledge of the code! What I get from this is that PCB_EDIT_FRAME seems to handle two different canvas objects (GetCanvas, GetGalCanvas, IsGalCanvasActive). This is probably why there is kind of a break in functionality between the different views in pcbview.

Thanks for digging into this!


Hi @mmccoo @pwuertz
is there a way to know from a python script, if GalCanvas is active?


Thanks so much for this information!
It took me about 4 hours to work out how to add filled zones (like mask) and text.
Very rough demo here in case someone else needs it: https://github.com/mattvenn/kicad/blob/master/python_api/test.py#L74


What did you use to draw that UML diagram? I’ve been looking for something that would draw UML diagrams that look as neat as that but have been disappointed by everything. I would love to know.


@royalt Sorry for the really late reply. I used http://plantuml.com/ to generate the UML.

@pwuertz On refreshing the GAL canvas, I have submitted a patch so the pcbnew.Refresh(); will get GAL canvas to update. I hope to have it merged in the next days.

It’s been a while since I’ve posted to this thread. I’ve added a new blog post, this time about applying the placement from the schematic to the layout. This helps get an initial placement that isn’t random.


I also have a show youtube video demo:

One question for everyone: when posting about new blog posts, should I continue this thread or start a new one?

Personally, even though I go for long periods without working on this stuff, I like coming back to one place to see if anyone has asked a question. Better late than never? There aren’t a lot of people out there that are comfortable with the kicad code base.


Continue this old one.


I’ve added this thread to the FAQ. Please consider editing the original post from time to time if you feel that something could be added that would keep someone from having to read the entire thread.


Agree, continue



thank you for the feedback.

changing the original post… Duh. should have thought of that myself. In fact, i think have have done just that in the past.

Youth is walking into a room and remember why. :slight_smile:


Does the autoplacement script work with hiearchical schematics?