Importing FreeCAD in Dynamo Python

Need help, or want to share a macro? Post here!
User avatar
cnirbhay
Posts: 116
Joined: Wed Aug 17, 2016 4:24 pm

Importing FreeCAD in Dynamo Python

Postby cnirbhay » Wed Sep 05, 2018 6:14 pm

Hi all.

Please guide me in the following issue where I'm unable to import FreeCAD in the Python console of Dynamo, whereas I was able to import any other module from the same path. Please see the code and screenshot below:

Code: Select all

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

import sys
sys.path.insert(0,r"E:\Softwares\FreeCAD_0.18.14014_x64_dev_win\FreeCAD_0.18.14014_x64_dev_win\bin")

import FreeCAD
Screenshot (372).png
Screenshot (372).png (112.64 KiB) Viewed 879 times
Thank you.

Regards,
wmayer
Site Admin
Posts: 16306
Joined: Thu Feb 19, 2009 10:32 am

Re: Importing FreeCAD in Dynamo Python

Postby wmayer » Wed Sep 05, 2018 8:50 pm

sys.path.insert(0,r"E:\Softwares\FreeCAD_0.18.14014_x64_dev_win\FreeCAD_0.18.14014_x64_dev_win\bin")
Either double the backslashes or replace them with slashes. Then try again.
User avatar
DeepSOIC
Posts: 7800
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Importing FreeCAD in Dynamo Python

Postby DeepSOIC » Wed Sep 05, 2018 9:55 pm

wmayer wrote:
Wed Sep 05, 2018 8:50 pm
sys.path.insert(0,r"E:\Softwares\FreeCAD_0.18.14014_x64_dev_win\FreeCAD_0.18.14014_x64_dev_win\bin")
Either double the backslashes or replace them with slashes. Then try again.
r in front of the string literal should be sufficient...
wmayer
Site Admin
Posts: 16306
Joined: Thu Feb 19, 2009 10:32 am

Re: Importing FreeCAD in Dynamo Python

Postby wmayer » Thu Sep 06, 2018 6:09 am

Thanks for the hint. Didn't know the "r" thing.
Then cnirbhay check the Python version used by Dynamo and that of your FreeCAD version. They must match with their major and minor version number as otherwise they are not binary compatible and thus cannot be mixed in one process.
User avatar
cnirbhay
Posts: 116
Joined: Wed Aug 17, 2016 4:24 pm

Re: Importing FreeCAD in Dynamo Python

Postby cnirbhay » Thu Sep 06, 2018 4:48 pm

wmayer wrote:
Thu Sep 06, 2018 6:09 am
Then cnirbhay check the Python version used by Dynamo and that of your FreeCAD version. They must match with their major and minor version number as otherwise they are not binary compatible and thus cannot be mixed in one process.
I checked their respective Python versions also and they matched too. Please see the screenshot below:
Dyn_FC.JPG
Dyn_FC.JPG (151.78 KiB) Viewed 819 times
wmayer
Site Admin
Posts: 16306
Joined: Thu Feb 19, 2009 10:32 am

Re: Importing FreeCAD in Dynamo Python

Postby wmayer » Thu Sep 06, 2018 5:09 pm

And the architecture? Are both either 32-bit or 64-bit applications?
User avatar
sgrogan
Posts: 5977
Joined: Wed Oct 22, 2014 5:02 pm

Re: Importing FreeCAD in Dynamo Python

Postby sgrogan » Thu Sep 06, 2018 5:15 pm

What is the result of

Code: Select all

import platform
print(platform.sys.version)
I suspect the compilers are different.
User avatar
cnirbhay
Posts: 116
Joined: Wed Aug 17, 2016 4:24 pm

Re: Importing FreeCAD in Dynamo Python

Postby cnirbhay » Thu Sep 06, 2018 5:23 pm

sgrogan wrote:
Thu Sep 06, 2018 5:15 pm
I suspect the compilers are different.
Well, in that case, I got a warning in Dynamo that no module named platform found. Both the applications are 64-bit.
Screenshot (373).png
Screenshot (373).png (151.15 KiB) Viewed 802 times
User avatar
cnirbhay
Posts: 116
Joined: Wed Aug 17, 2016 4:24 pm

Re: Importing FreeCAD in Dynamo Python

Postby cnirbhay » Fri Sep 07, 2018 4:25 pm

sgrogan wrote:
Thu Sep 06, 2018 5:15 pm
What is the result of

Code: Select all

import platform
print(platform.sys.version)
So, now what is the solution to it? Any hints or pointers would be great. :-)
User avatar
sgrogan
Posts: 5977
Joined: Wed Oct 22, 2014 5:02 pm

Re: Importing FreeCAD in Dynamo Python

Postby sgrogan » Fri Sep 07, 2018 4:58 pm

cnirbhay wrote:
Fri Sep 07, 2018 4:25 pm
So, now what is the solution to it? Any hints or pointers would be great.
I don't know another way to get the compiler used to build python.
I suspect Dynamo's Python 2.7 is built with VC9, this is what standard Python is built with.
FreeCAD's python is built with VC12, so the 2 python's would be incompatible as they would try load different CRT's.

Maybe you can get some info from Dynamo's Help About or the like?

EDIT: I just discovered that Dynamo uses Iron Python and doesn't work with CPython compiled extensions (like FreeCAD.pyd)
see here for ex. https://forum.dynamobim.com/t/numpy-for ... cript/5394 (i.e. numpy.pyd)

So you will only be able to use pure python modules (.py files) with Dynamo apparently.