'FreeCAD' is not a built-in module

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
markusd91
Posts: 4
Joined: Mon Oct 25, 2021 6:26 am

'FreeCAD' is not a built-in module

Post by markusd91 »

Hi,

I'm having problem getting started with scripting in FreeCAD from external shell. It does not seem to connect to the running FreeCAD application. Are there any guidlines of how to set this up properly?

import FreeCAD as App
App.ActiveDocument.addObject("Part::Box","Box")

Nothing happens..

When I type 'FreeCAD' I get: 'C:\\Users\\MSDOSE\\AppData\\Local\\FreeCAD 0.18\\bin\\FreeCAD.pyd'>

Is that correct?

I use FreeCAD 0.18 and pyhton 3.6.
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: 'FreeCAD' is not a built-in module

Post by wmayer »

Is that correct?
If you run the Python interpreter directly or import the FreeCAD module with an external Python-based application then it loads FreeCAD.pyd.

If you start FreeCAD.exe or FreeCADCmd.exe then there is the built-in module called FreeCAD.
markusd91
Posts: 4
Joined: Mon Oct 25, 2021 6:26 am

Re: 'FreeCAD' is not a built-in module

Post by markusd91 »

If I create new documents through the python script and then iterate all documents then these documents are in fact added. But not in the running Freecad.exe application. There is no connection between script and active document.

How do I make that connection?

Further, when trying to run any code outside the python.exe from the bin folder it fails as there is no module named FreeCAD. I have added it to sys.path to no avail.
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: 'FreeCAD' is not a built-in module

Post by wmayer »

Can you explain in more details what you want to achieve, please?
markusd91
Posts: 4
Joined: Mon Oct 25, 2021 6:26 am

Re: 'FreeCAD' is not a built-in module

Post by markusd91 »

Sure,

Today I use pyautocad with AutoCad C3D to automate/visualize stuff which is great. However, I'm trying to move to FreeCAD as I believe it has all the functionality I need, is free, and more compatible with python.

Just like today I want to work on a script in my external console (spyder) and as I run the script I want the document/model to update. I'd like to add buttons and modify workspaces etc. However, I need to see what's going on as I make the changes just like if I type something in the built in python console in FreeCAD.

Right now, I don't get any connection between my external script and FreeCAD as there is "No module named FreeCAD" when trying to import FreeCAD. Opening the pyhton.exe in the bin folder does not give me this error but it does nothing to my active FreeCAD application.

Let's say I want to create a box in an active FreeCAD document using an external script (in python.exe in bin folder):

>>> import FreeCAD
FreeCAD 0.18, Libs: 0.18R4 (GitTag)
>>> doc = FreeCAD.newDocument()
>>> doc
<Document object at 000001CA7BFACE00> #Document created but not in active FreeCAD application
>>> box = doc.addObject("Part::Box", "myBox")
>>> box
<Part::PartFeature>#Box created in that document
>>> doc.recompute()
1

I don't know where this file ends up and how I get that file in my running application.
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: 'FreeCAD' is not a built-in module

Post by wmayer »

Today I use pyautocad with AutoCad C3D to automate/visualize stuff which is great. However, I'm trying to move to FreeCAD as I believe it has all the functionality I need, is free, and more compatible with python.
I never used it so I don't know how these programs communicate.
I'm having problem getting started with scripting in FreeCAD from external shell. It does not seem to connect to the running FreeCAD application. Are there any guidlines of how to set this up properly?
So far I assume you run the normal FreeCAD application and now you start an external Python session where you want to load the FreeCAD.pyd in the hope to communicate with the FreeCAD application. Sorry, but this doesn't work because the FreeCAD.exe is a separate process and you cannot simply start a second process that has direct access to the internally running Python interpreter of FreeCAD.exe.

If we have two separate processes then one way is to communicate over sockets and this we have implemented basic support for. For more details have a look at: https://forum.freecadweb.org/viewtopic.php?f=22&t=59724

A different way is to not start the FreeCAD.exe but load the FreeCAD.pyd and FreeCADGui.pyd modules. With Jupyter Notebook you have the expected FreeCAD GUI running and from a browser window you can access the FreeCAD internals.
For more details have a look at https://forum.freecadweb.org/viewtopic. ... 54#p409754

The steps to set this up:
  • in the command line start jupyter notebook which will open a browser window
  • in the page you find a button called New. Click on it and choose Python3. This will open a notebook page in a second browser window
  • inside the cell enter this and press Shift+Return

    Code: Select all

    %gui qt5
    import sys
    sys.path.append("Enter here the path to FreeCADGui.pyd")
    from freecad import app
    import FreeCADGui as gui
    gui.showMainWindow()
    
After a few seconds you should see the FreeCAD GUI.

If you e.g. enter in the next cell:

Code: Select all

app.newDocument()
and press Shift+Return you will see that in the FreeCAD GUI a new document has been created.
markusd91
Posts: 4
Joined: Mon Oct 25, 2021 6:26 am

Re: 'FreeCAD' is not a built-in module

Post by markusd91 »

I will try this, thanks!
Post Reply