import FreeCAD disables output globally?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
0xCoto
Posts: 26
Joined: Wed Aug 10, 2022 3:56 pm

import FreeCAD disables output globally?

Post by 0xCoto »

It seems that importing FreeCAD in Python using:

Code: Select all

sys.path.append("/usr/lib/freecad/lib")
import FreeCAD

import irrelevant_module
irrelevant_module.output_something()
causes problems in some other irrelevant function of a module I'm calling.

Specifically, for some reason, if FreeCAD is imported (without having to do any operations, just importing the module alone), my (irrelevant) function does not print anything to the screen, even though it should. But if I comment the FreeCAD import, i.e.:

Code: Select all

sys.path.append("/usr/lib/freecad/lib")
#import FreeCAD

import irrelevant_module
irrelevant_module.output_something()
it appears that irrelevant_module.output_something() is able to output (print things to terminal) again.

I am certain that irrelevant_module does not use anything FreeCAD-related, and could surely not be using the 'FreeCAD' term anywhere, but I am suspecting that perhaps FreeCAD disables console outputs in some way? print's do work though, so I'm not quite sure.

Note: irrelevant_module.output_something() actually calls C++ code, so it doesn't use Python prints, but rather:

Code: Select all

cout << "..." << endl;
If anyone is familiar with what the FreeCAD module is doing, maybe you have some ideas as to what's happening under the hood? In case it makes things easier, I don't mind un-importing FreeCAD before running output_something(), as I won't bee needing FreeCAD afterwards in the code.

Thanks!
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: import FreeCAD disables output globally?

Post by onekk »

See maybe:

https://wiki.freecadweb.org/Embedding_FreeCAD

But note:
FreeCAD can be imported as a Python module in other programs or in a standalone Python console, together with all its modules and components. It's even possible to import the FreeCAD user interface as a python module but with some restrictions indicated in Caveats.

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/
0xCoto
Posts: 26
Joined: Wed Aug 10, 2022 3:56 pm

Re: import FreeCAD disables output globally?

Post by 0xCoto »

onekk wrote: Mon Dec 05, 2022 8:48 am See maybe:

https://wiki.freecadweb.org/Embedding_FreeCAD

But note:
FreeCAD can be imported as a Python module in other programs or in a standalone Python console, together with all its modules and components. It's even possible to import the FreeCAD user interface as a python module but with some restrictions indicated in Caveats.
Sorry, I'm not sure what to make of this. It's unclear what kind of change I'd have to implement in my code to resolve my issue.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: import FreeCAD disables output globally?

Post by onekk »

The main thing is that using FreeCAD as a Library importing it in an external Python interpreter is not a "cup of tea" so basically you have to be very careful.

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/
0xCoto
Posts: 26
Joined: Wed Aug 10, 2022 3:56 pm

Re: import FreeCAD disables output globally?

Post by 0xCoto »

onekk wrote: Mon Dec 05, 2022 9:06 pm The main thing is that using FreeCAD as a Library importing it in an external Python interpreter is not a "cup of tea" so basically you have to be very careful.
Sorry, this isn't particularly helpful. If anyone could chime in and suggest specifically what might have to be adjusted, I'd appreciate it.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: import FreeCAD disables output globally?

Post by onekk »

0xCoto wrote: Tue Dec 06, 2022 12:42 pm ....
Sorry, this isn't particularly helpful. If anyone could chime in and suggest specifically what might have to be adjusted, I'd appreciate it.
Quoted text is first paragraph of the Wiki Page.

There are many things that are lacking, first of all a proper code, and not fragments as it is not clear what FreeCAD is used for, maybe you are doing something with FreeCAD methods that cause the problem.

Then a full FreeCAD version info as advised in the IMPORTANT posr at top of forum page, and some info about your Python interpreter version as it is crucial that has same version as what FreeCAD is compiled with.

No one here have a crystall ball to guess these things.

But some of these things are explained in the linked Wiki page under Caveats section.

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: import FreeCAD disables output globally?

Post by wmayer »

I hope this is more helpful: https://github.com/FreeCAD/FreeCAD/blob ... y.cpp#L187
In other words this causes to redirect any output that is written to std::cout, std::cerr or std::clog to FreeCAD's stream buffers which call the suitable Base::Console() methods Log() or Error(). Important to note is that messages written to std::cout will be redirected to Base::Console().Log()

The console methods forward the messages to their observers and for the console output the class ConsoleObserverStd is responsible. Now when loading FreeCAD as Python module the ConsoleObserverStd is completely disabled so that all console output will be suppressed. In order to enable this observer again you have to call:

Code: Select all

FreeCAD.Console.SetStatus("Console", "Err", True)
FreeCAD.Console.SetStatus("Console", "Log", True)
FreeCAD.Console.SetStatus("Console", "Msg", True)
FreeCAD.Console.SetStatus("Console", "Wrn", True)
0xCoto
Posts: 26
Joined: Wed Aug 10, 2022 3:56 pm

Re: import FreeCAD disables output globally?

Post by 0xCoto »

wmayer wrote: Wed Dec 07, 2022 10:04 am I hope this is more helpful: https://github.com/FreeCAD/FreeCAD/blob ... y.cpp#L187
In other words this causes to redirect any output that is written to std::cout, std::cerr or std::clog to FreeCAD's stream buffers which call the suitable Base::Console() methods Log() or Error(). Important to note is that messages written to std::cout will be redirected to Base::Console().Log()

The console methods forward the messages to their observers and for the console output the class ConsoleObserverStd is responsible. Now when loading FreeCAD as Python module the ConsoleObserverStd is completely disabled so that all console output will be suppressed. In order to enable this observer again you have to call:

Code: Select all

FreeCAD.Console.SetStatus("Console", "Err", True)
FreeCAD.Console.SetStatus("Console", "Log", True)
FreeCAD.Console.SetStatus("Console", "Msg", True)
FreeCAD.Console.SetStatus("Console", "Wrn", True)
This gives:

Code: Select all

Traceback (most recent call last):
  File "/home/user/Desktop/api/s_api.py", line 848, in <module>
    step2stl_fc(step_file, clear=clear)
  File "/home/user/Desktop/api/s_api.py", line 154, in step2stl_fc
    FreeCAD.Console.SetStatus("Console", "Err", True)
Base.FreeCADError: Unknown Console Type
I'm running 0.19, not sure if that could be a problem?

Looks like FreeCAD.Console.SetStatus("Console", "Err", True) only works when being run from FreeCAD's console, hmm.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: import FreeCAD disables output globally?

Post by wmayer »

Looks like FreeCAD.Console.SetStatus("Console", "Err", True) only works when being run from FreeCAD's console, hmm.
Odd! I have explicitly loaded FreeCAD as Python module in order to see that it works like this:

Code: Select all

python3
import FreeCAD

Err("Something\n") # => No output
FreeCAD.Console.SetStatus("Console", "Err", True)
Err("Something\n") # => Output is there
I have tested it with the current developer version, not with 0.19. I will have a look...
0xCoto
Posts: 26
Joined: Wed Aug 10, 2022 3:56 pm

Re: import FreeCAD disables output globally?

Post by 0xCoto »

Also tried a minimum reproducible example, but I can't seem to reproduce your case:

Image

My suspicion is that "Console" might not be the exact "console name" or "identifier" it picks up when being run from the terminal vs FreeCAD itself? Because I get the same error when using e.g. "Test" instead of "Console" in FreeCAD.
Post Reply