module 'App' has no attribute 'ActiveDocument', but it works in the Python console ?

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
techGuy
Posts: 126
Joined: Sat Jun 18, 2022 12:48 am

module 'App' has no attribute 'ActiveDocument', but it works in the Python console ?

Post by techGuy »

I would like to get the name of the active document in my code.

This is my code:

Code: Select all

filename = App.ActiveDocument.Name
which returns an error: module 'App' has no attribute 'ActiveDocument'

Yet in the Python console of FreeCAD:

Code: Select all

>>> App.ActiveDocument.Name
'naca2412_BH'
What am I missing ?

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

Re: module 'App' has no attribute 'ActiveDocument', but it works in the Python console ?

Post by onekk »

Hello,

first if all you miss to read this:

viewtopic.php?t=2264

Second:

From where you issue the commands, from an external python interpreter?

This information is crucial as if you want to use "FreeCAD as a library" younshoukd read the wiki page describing sone caveats as it is not a "common use case", not exotic but not so common.

Regards

Carlo D.
Last edited by onekk on Sun Mar 19, 2023 7:53 pm, edited 1 time in total.
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/
techGuy
Posts: 126
Joined: Sat Jun 18, 2022 12:48 am

Re: module 'App' has no attribute 'ActiveDocument', but it works in the Python console ?

Post by techGuy »

onekk wrote: Sun Mar 19, 2023 5:33 pm From where you issue the commands, from an external python interpreter?
I ran the Python command in CfdOF workbench in FreeCAD
FreeCAD Session.png
FreeCAD Session.png (223.67 KiB) Viewed 420 times
My code is running as part of the CfdOF workbench code, in one of the workbench's modules.
This information is crucial as if you want to use "FreeCADvasca library" younshoukd read the wiki page describing sone caveats as it is not a "common use case", not exotic but not so common.
I am not using the "FreeCADvasca library".

My code is running in CfdTools.py in the CfdOF workbench. https://github.com/jaheyns/CfdOF
techGuy
Posts: 126
Joined: Sat Jun 18, 2022 12:48 am

Re: module 'App' has no attribute 'ActiveDocument', but it works in the Python console ?

Post by techGuy »

I figured it out. The correct syntax is:

Code: Select all

App = FreeCAD
filename = App.ActiveDocument.Name
Here is a good API reference https://freecad.github.io/SourceDoc/modules.html
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: module 'App' has no attribute 'ActiveDocument', but it works in the Python console ?

Post by onekk »

techGuy wrote: Sun Mar 19, 2023 6:24 pm ...
From a single line of code it is difficult to guess what you are importing and what not.

For this reason it is better to post a MWE (Minimal Working Example) even if the example is not "working" at all. :)

Usually App is a widespread way to refer to FreeCAD module (IMHO a bad way as it works in FreeCAD Puthin console but not in a stand-alone script.

Same for Gui to refer to FreeCADGui module.

I have amended my post thanks to note the mistyping.

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/
Post Reply