Importing FreeCAD in Dynamo Python

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
cnirbhay
Posts: 115
Joined: Wed Aug 17, 2016 4:24 pm

Importing FreeCAD in Dynamo Python

Post by cnirbhay »

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 2286 times
Thank you.

Regards,
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Importing FreeCAD in Dynamo Python

Post by wmayer »

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
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Importing FreeCAD in Dynamo Python

Post by DeepSOIC »

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
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Importing FreeCAD in Dynamo Python

Post by wmayer »

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: 115
Joined: Wed Aug 17, 2016 4:24 pm

Re: Importing FreeCAD in Dynamo Python

Post by cnirbhay »

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 2226 times
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Importing FreeCAD in Dynamo Python

Post by wmayer »

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

Re: Importing FreeCAD in Dynamo Python

Post by sgrogan »

What is the result of

Code: Select all

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

Re: Importing FreeCAD in Dynamo Python

Post by cnirbhay »

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 2209 times
User avatar
cnirbhay
Posts: 115
Joined: Wed Aug 17, 2016 4:24 pm

Re: Importing FreeCAD in Dynamo Python

Post by cnirbhay »

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
Veteran
Posts: 6499
Joined: Wed Oct 22, 2014 5:02 pm

Re: Importing FreeCAD in Dynamo Python

Post by sgrogan »

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.
"fight the good fight"
Post Reply