FreeCAD api fails in external application

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
drmacro
Veteran
Posts: 8870
Joined: Sun Mar 02, 2014 4:35 pm

FreeCAD api fails in external application

Post by drmacro »

This is a long one. So, I'll start with a summary.
Below refers to FreeCAD 0.20.

With this combination: Blender 2.93.3 Python console, lists all objects including PartDesign::Body objects (Debian 11, FreeCAD git source 0.20 compiled locally)

On Blender 3.1.2 Python console, that code lists all objects, but no PartDesign::Body objects (Majaro, FreeCAD AUR build and a built from git version.)

The Python code in question (in Blender) is as follows:

Code: Select all

import FreeCAD as F
fc_file = r"/home/mac/SharedData/Projects/MechMovements/Simple_assy_001.FCStd"
live_fc = F.open(fc_file)
for obj in F.ActiveDocument.Objects:
    print(obj.TypeId, 'Name: ', obj.Name, 'Label: ', obj.Label)
The following code in the FreeCAD Python console on either PC lists all objects, including Body objects:

Code: Select all

for obj in App.ActiveDocument.Objects:
	print(obj.TypeId, 'Name: ', obj.Name, 'Label: ', obj.Label)
Looking for ideas on how to trouble shoot this.

Python console in Blender on the Manjaro PC says the FreeCAD module is imported from /usr/lib/python3.10/site-packages/
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: FreeCAD api fails in external application

Post by onekk »

Errors?

The problem is that importing FreeCAD alone, you don't import the FreeCADGui module, and probably Part:Design is using the Gui part.

in some code it will test if the Gui is up and do things differently.

I know that you are "simply reading" a file, so checking for a Gui in this phase is strange, but...

Hope it helps

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
drmacro
Veteran
Posts: 8870
Joined: Sun Mar 02, 2014 4:35 pm

Re: FreeCAD api fails in external application

Post by drmacro »

onekk wrote: Fri Apr 29, 2022 7:46 am Errors?

The problem is that importing FreeCAD alone, you don't import the FreeCADGui module, and probably Part:Design is using the Gui part.

in some code it will test if the Gui is up and do things differently.

I know that you are "simply reading" a file, so checking for a Gui in this phase is strange, but...

Hope it helps

Carlo D.
Hmm...that doesn't seem to explain why the same code works with one version of Blender, but, not another, though both appear to be the same version of FreeCAD.

Or, maybe I don't understand what you said...I've only had 2 cups of coffee so far today... :mrgreen:
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: FreeCAD api fails in external application

Post by onekk »

drmacro wrote: Fri Apr 29, 2022 11:57 am Hmm...that doesn't seem to explain why the same code works with one version of Blender, but, not another, though both appear to be the same version of FreeCAD.

Or, maybe I don't understand what you said...I've only had 2 cups of coffee so far today... :mrgreen:
No probably you are right, I have concentrate on the code, and noted the import of FreeCAD, and I posted without thinking enough.

Without some errors, it is difficult to guess why these differences.

FreeCAD is the same version, but it is not compiled in the same manner.

Debian Building is different from AUR building, or it may depends, on the different Python versions, there are many things that could be go wrong.

I'm driving mad actually with some Qt quirks that have worked in the past and now are not running correctly, but even Qt is complex, and maybe some "settings" are different, see what happened with "Start WB" when in Arch Linux some policy where modified, after many research it was a "sandox settings" for the Web module that some distributions use and others apparently don't.

You could try to install a miniconda environment and install both Blender and FreeCAD on it and see if something change, I admit this is not a cheap try as a miniconda environment will consume around 4 or 5 Gb of disk space, but it is quite isolated from the OS but permit to inspect sources as they are present on disk.

As usual, probably I'm wrong :D

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: FreeCAD api fails in external application

Post by onekk »

drmacro wrote: Fri Apr 29, 2022 11:57 am Hmm...that doesn't seem to explain why the same code works with one version of Blender, but, not another, though both appear to be the same version of FreeCAD.

Or, maybe I don't understand what you said...I've only had 2 cups of coffee so far today... :mrgreen:
No probably you are right, I have concentrate on the code, and noted the import of FreeCAD, and I posted without thinking enough.

Without some errors, it is difficult to guess why these differences.

FreeCAD is the same version, but it is not compiled in the same manner.

Debian Building is different from AUR building, or it may depends, on the different Python versions, there are many things that could be go wrong.

I'm driving mad actually with some Qt quirks that have worked in the past and now are not running correctly, but even Qt is complex, and maybe some "settings" are different, see what happened with "Start WB" when in Arch Linux some policy where modified, after many research it was a "sandox settings" for the Web module that some distributions use and others apparently don't.

You could try to install a miniconda environment and install both Blender and FreeCAD on it and see if something change, I admit this is not a cheap try as a miniconda environment will consume around 4 or 5 Gb of disk space, but it is quite isolated from the OS but permit to inspect sources as they are present on disk.

As usual, probably I'm wrong :D

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: FreeCAD api fails in external application

Post by wmayer »

Related thread: https://forum.freecadweb.org/viewtopic.php?f=22&t=39778

AFAIR some years ago this had been discussed in a further thread but I am unable to find it.
Python console in Blender on the Manjaro PC says the FreeCAD module is imported from /usr/lib/python3.10/site-packages/
And the PartDesign module? I can imagine that it imported the PartDesign package but not the actual PartDesign module which is _PartDesign.so

From the Blender Python console you should be able to import PartDesign. When doing so what does the call of PartDesign tell you? It should give you the path to an __init__.py file and when loading it into a text editor it should contain this content:

Code: Select all

import _PartDesign
makeFilletArc = _PartDesign.makeFilletArc
You can also check what's the process ID of the Blender application and then run

Code: Select all

lsof -p <Blender_Process_ID> | grep PartDesign
This should at least list _PartDesign.so
drmacro
Veteran
Posts: 8870
Joined: Sun Mar 02, 2014 4:35 pm

Re: FreeCAD api fails in external application

Post by drmacro »

wmayer wrote: Fri Apr 29, 2022 2:41 pm ...
I have had a chance to try you suggestions, but, will in short order.

I'm curious though, how would this explain that nothing different is imported when running the same snippet on Debian and it works as expected?
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
drmacro
Veteran
Posts: 8870
Joined: Sun Mar 02, 2014 4:35 pm

Re: FreeCAD api fails in external application

Post by drmacro »

wmayer wrote: Fri Apr 29, 2022 2:41 pm ...
From the Blender Python console you should be able to import PartDesign. When doing so what does the call of PartDesign tell you? It should give you the path to an __init__.py file and when loading it into a text editor it should contain this content:

Code: Select all

import _PartDesign
makeFilletArc = _PartDesign.makeFilletArc
This produces no output at the console.
You can also check what's the process ID of the Blender application and then run

Code: Select all

lsof -p <Blender_Process_ID> | grep PartDesign
This should at least list _PartDesign.so
This produces:

Code: Select all

blender 34881  mac  mem       REG                8,2   1634200 24520772 /usr/lib/_PartDesign.so
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: FreeCAD api fails in external application

Post by wmayer »

OK, then it obviously loads the PartDesign module. Now what happens if you create a document and add a body feature?

Code: Select all

doc = F.newDocument()
obj = doc.addObject("PartDesign::Body", "Body")
for obj in F.ActiveDocument.Objects:
    print(obj.TypeId, 'Name: ', obj.Name, 'Label: ', obj.Label)
drmacro
Veteran
Posts: 8870
Joined: Sun Mar 02, 2014 4:35 pm

Re: FreeCAD api fails in external application

Post by drmacro »

wmayer wrote: Sat Apr 30, 2022 9:36 am OK, then it obviously loads the PartDesign module. Now what happens if you create a document and add a body feature?

Code: Select all

doc = F.newDocument()
obj = doc.addObject("PartDesign::Body", "Body")
for obj in F.ActiveDocument.Objects:
    print(obj.TypeId, 'Name: ', obj.Name, 'Label: ', obj.Label)

Code: Select all

obj = doc.addObject("PartDesign::Body", "Body")
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
ModuleNotFoundError: {'sclassname': 'N4Base11PyExceptionE', 'sErrMsg': "No module named 'PartDesign'", 'sfile': '', 'iline': 0, 'sfunction': '', 'swhat': "No module named 'PartDesign'", 'btranslatable': False, 'breported': False}
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
Post Reply