Import FreeCAD in Python

If you’ve compiled FreeCAD, but don’t know how to import it as a package in Python then read on.

First of all, you need to locate where the file is.


Mine is in /home/mandeep/Desktop/build/lib.

To be able to do:

import FreeCAD

You’ll have to go to that directory and run python and if you now try to import, it will work. But most probably, you won’t agree to use it as it might feel impractical.

Another method is to append this path to sys.path. For this, you need to edit a file

For example,


The would probably be where your python lib directory is. It may probably look like: /usr/lib/python2.7. But I am using the Python within the virtualenv so the python lib is within the virtualenv e.g. venv/lib/python2.7. Here venv is my virtualenv.

Read more about this at

The idea to edit the file is that it gets initialized with Python. So you can now import your packages from anywhere.



I said “sed”

Now it’s the turn for macros be fetched from the wiki page at We’ll need to parse the links (and macro names) from there. So we need some pattern to be able to parse similar data (i.e. links). The common thing was that the links had ?title=Macro… So it was suggested by mentors that we can have a template with the links that can have a specific class.

So in the wiki source, the links are written as: [[Macro makeCube|Macro MakeCube]]. Here 1st argument (before the pipe) is the name of the page/URL. The second one is the link text that appears on the wiki. If we omit the second argument, then the first one is taken as the link text and URL.

So aim was to convert something like [[Macro makeCube|Macro MakeCube]] to {{MacroLink|Macro makeCube}}. Here MacroLink is the name of the template. Which is replaced by the tags we specify. Here it would be replaced by a span with class “MacroLink”.

Okay, so what’s the problem! There are more than 100 entries there and one would simply edit it one by one manually. So “sed” was there to rescue.

Now I had to look for some pattern to be able to replace text. So as we have to remove the content after the pipe (‘|’) and replace the [[]] with {{}} and add template tag and a pipe.

I am not good with regular expressions. On searching something similar, I found So I got the idea and used it like:

sed ‘s/|Macro.*\]\]/\}\}/i’ input.txt > wikinew.txt

It selects the text starting from “|Macro” till “]]” and replace it by “}}” as we need it at the end. Hence, the content after pipe would be removed too. Now we’ll have entries like [[Macro makeCube}}.

sed -i.bak ‘s/\[\[Macro/\{\{MacroLink|Macro/’ wikinew.txt

Now it was the turn for the prefix. The above command will select text like [[Macro and will replace it by {{MacroLink|Macro. As we needed to add a template “MacroLink” to it. That fulfilled the need. Although, 2-3 entries needed to be modified manually. The final page is here (not sure if it would exist). Code

The code is I’m done!

Fetching GitHub submodules

Yesterday, I took the first step towards fetching information from GitHub. Today added code for checking if the repository contains submodules and will return that instance. Then we may use it to access submodule information like name, submodule_git_url etc.


I used x.raw_data.get(typeto check if x a submodule or not. Actually raw_data contains many other attributes (like html_url, git_url, name, sha, type etc.) that provide additional info. about the instance itself. And for fetching the original repository link, I used: x.raw_data.get(submodule_git_url).

Here is the code for the particular file:

More about repository methods in PyGithub:

Rate Limit:

For requests using Basic Authentication or OAuth, you can make up to 5,000 requests per hour. For unauthenticated requests, the rate limit allows you to make up to 60 requests per hour. Unauthenticated requests are associated with your IP address, and not the user making requests.

GitHub API Returns

Hi everyone,

On 11 June, I tried experimenting with parsing the submodules data from

I started working on PyGithub today (12 June). Actually, it’s a third-party library wrapper written in Python for the GitHub API. The day went off struggling with PyGithub to work. I couldn’t understand much at the beginning. As the night elapsed, it started working for me. 😛

I started with some basic example like the fetching of the names of the files. You may see the repository here: Actually, this repository has also been created using the API itself (without opening in the browser). Finally got slept at around 7 AM.

Now next is to do get if the instance fetched is a submodule or not. It seems to be much easier to be done now.

GitHub API

As I fetched macro code using BeautifulSoup. Now the FreeCAD workbenches are hosted at one place i.e. I needed to fetch the file details from this repository from GitHub. It was already done in Python by parsing the HTML and scraping it. I tried a Python module called BeautifulSoup that makes it easy to play with the HTML within Python.

But these aren’t the permanent solutions. We’ll have to make frequent changes in our code if the source website (GitHub) changes. So I thought of using GitHub API. It’ll be more of a stable solution.

Here is the list of libraries you can use in any programming language: I am going to explore PyGithub.

Install PyGithub:

pip install pygithub

For more, read:

Stay tuned!


FreeCAD manages its macros its wiki: Let’s take an example of a macro “Macro Image Scaling” whose code is at I needed to fetch the code from wiki and further process that.

Lately, I was doing some experiments to parse macro code from the FreeCAD wiki. Firstly, I used PyQuery. But it uses lxml which is a shared library and it was having some issues while packaging for Windows. As lxml is a shared lib and the available Python 2.7 lxml packages were built using VC9 (Visual C++) which isn’t compatible with FreeCAD’s python which is built with VC12. FreeCAD ships its own Python on Windows.

Then another day I found BeautifulSoup via Daman. It’s pretty good. You may see the use case here:

But while running this as macro in FreeCAD, the output gets printed to the report view. But after executing this macro, the report view kept on stopping at a particular character of fetched macro output code. This seemed to be some problem with length of string redirected to the report view. The code works fine in Python console.


FreeCAD Plugins

There are basically two ways to add functionality to FreeCAD.
1. Workbenches
2. Macros


Workbenches can be thought of the set of tools grouped together to perform a specific task. Workbenches can be C++ or Python modules. The C++ ones written earlier are bundled with FreeCAD itself like Part, PartDesign, Mesh. More information regarding this can be found at

Workbenches that are being written in Python are managed at so that they can be separated out from FreeCAD and plugged in later on. Hence, it becomes easy to handle Python modules as they don’t need compilation. Python workbenches are Arch, openSCAD, ship etc.

Some are hybrid (combination of C++ and Python) like PathDraft etc.


Macros can be visualized as single-file python scripts. These are having .FCMacro file extension. There are two ways to record macros.

1. Interactive: Go to Menu > Macro > Macro recording > Enter any name > Click ‘Record‘ > Do whatever you want to do and then click Macro > Stop macro recording. Read more:

2. Python: Go to MenuMacro >  Macros > Create > Enter file name > Click ‘OK‘ > Write python code and save (Ctrl+S).

Macros are located at the FreeCAD wiki: Later, it was thought to port those macros to GitHub: As one doesn’t need to know programming to create Macros, hence a normal user finds it difficult to use git and GitHub and most macro authors don’t have a GitHub account so they didn’t move the macros there. And the wiki is very easy for people to put their macros.


Hence, the idea is to implement a plugin manager that would be managing both Workbenches and the Macros without giving much hassle to the users to install them, via a single interface.

Currently, the installation directory for ~/.FreeCAD/Mod (in GNU/Linux at least). To see the User Data directory, go to FreeCAD Python console and paste:

print FreeCAD.getUserAppDataDir()

This will print the directory where to store the plugins. Mod is for containing the workbenches and the Macro directory is for storing the user created macros.