[SOLVED] How to run Python script in the GUI at startup and continue interactively?
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
[SOLVED] How to run Python script in the GUI at startup and continue interactively?
Hi,
I would like to execute FreeCAD Daily 0.19 and pass it a Python script. FreeCAD would invoke and after the GUI is fully running and ready to take keyboard and mouse input, before it does so, it executes my script just as if I were to type in the Python script into the Python interpreter window in the GUI. After it executes the script (which may fail with an exception, etc.), I would like FreeCAD to continue normally with interactive use (i.e., not exit the program).
I know about the command-line option of "-c" (Start up and Configuration) for running in console mode, but I sense there is no way to force that to run the GUI interactively after the script has finished execution.
Is the only way for me to do so is to resort to using the Macro at Startup mechanism and rig something up? That is less desirable in my use-cases because I do not want to have to install scripts in specific locations (the Mod directory if I understand correctly), but manage my scripts elsewhere.
One notable use-case (there are many others I can imagine): I often need to invoke FreeCAD on a non-existent file. If I pass that filename on the command-line, FreeCAD attempts to open it and then fails with a message that the file does not exist. I have to then copy/paste the full path of the file out of the report view, and do the Save As operation to create it from scratch. And, I'd prefer to write the script that does the existence check and document creation all within the Python interpreter running within the FreeCAD GUI, not in a wrapper Bash script that then invokes FreeCAD (of course, that is certainly doable, but only if the answer to this post is "no"), hence this post.
Thanks!
I would like to execute FreeCAD Daily 0.19 and pass it a Python script. FreeCAD would invoke and after the GUI is fully running and ready to take keyboard and mouse input, before it does so, it executes my script just as if I were to type in the Python script into the Python interpreter window in the GUI. After it executes the script (which may fail with an exception, etc.), I would like FreeCAD to continue normally with interactive use (i.e., not exit the program).
I know about the command-line option of "-c" (Start up and Configuration) for running in console mode, but I sense there is no way to force that to run the GUI interactively after the script has finished execution.
Is the only way for me to do so is to resort to using the Macro at Startup mechanism and rig something up? That is less desirable in my use-cases because I do not want to have to install scripts in specific locations (the Mod directory if I understand correctly), but manage my scripts elsewhere.
One notable use-case (there are many others I can imagine): I often need to invoke FreeCAD on a non-existent file. If I pass that filename on the command-line, FreeCAD attempts to open it and then fails with a message that the file does not exist. I have to then copy/paste the full path of the file out of the report view, and do the Save As operation to create it from scratch. And, I'd prefer to write the script that does the existence check and document creation all within the Python interpreter running within the FreeCAD GUI, not in a wrapper Bash script that then invokes FreeCAD (of course, that is certainly doable, but only if the answer to this post is "no"), hence this post.
Thanks!
Last edited by bgoodr on Fri Jan 24, 2020 5:16 pm, edited 1 time in total.
Re: How to run Python script in the GUI at startup and continue interactively?
Here is a Bash script that implements what I would like to implement inside a Python script running within the FreeCAD Python interpreter:
And because it is Bash, it assumes there is a Bash interpreter installed on the host, which might be problematic for Microsoft Windows users and other platforms, or assumes a specific version of Bash. This was tested on a Ubuntu 19.10 machine.
freecad-daily version used was:
Thanks!
- Download the attachment.
- Rename it to "freecadnew".
- Put it somewhere in your $PATH.
- Run chmod a+x path_to_where_you_copied_it_to to give it execute permissions.
- Set/export the FREECAD_NEW_DOCUMENT_TEMPLATE_PATH to a fully-qualified path to a template file which is typically one that is created with Part Design a single Body object in it. The Body name should have the same file prefix as the template file, so that the script can search and replace it within the XML file with the file prefix of the supplied path (see below).
- Invoke it like this: freecadnew /tmp/foo/bar/my_new_doc.FCStd
- FreeCAD will then load /tmp/foo/bar/my_new_doc.FCStd and you should see that the main Body will be equal to the file prefix of my_new_doc
And because it is Bash, it assumes there is a Bash interpreter installed on the host, which might be problematic for Microsoft Windows users and other platforms, or assumes a specific version of Bash. This was tested on a Ubuntu 19.10 machine.
freecad-daily version used was:
Code: Select all
$ dpkg --list freecad\*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-=====================-====================================-============-======================================================
rc freecad 1:0.17.13541-1ppa3~ubuntu18.04.1 amd64 Extensible Open Source CAx program (beta)
ii freecad-daily 0.19~pre2~202001191534~ubuntu19.10.1 all Extensible Open Source CAx program
ii freecad-daily-common 0.19~pre2~202001191534~ubuntu19.10.1 all Extensible Open Source CAx program - common files
un freecad-daily-doc <none> <none> (no description available)
un freecad-daily-python2 <none> <none> (no description available)
ii freecad-daily-python3 0.19~pre2~202001191534~ubuntu19.10.1 amd64 Extensible Open Source CAx program - Python 3 binaries
un freecad-daily-runtime <none> <none> (no description available)
un freecad-doc <none> <none> (no description available)
$
- Attachments
-
- freecadnew.txt
- (3.3 KiB) Downloaded 92 times
Re: How to run Python script in the GUI at startup and continue interactively?
With Macro at Startup, only the launcher really need to be in the Mod folder, but it can be hacked to run a script that virtually can anywhere you want.
Re: How to run Python script in the GUI at startup and continue interactively?
Thanks.
So, the answer is "no", so someone would have to implement a new '-g gui_script.py' option, in order for me to avoid hacking a launcher in the Mod directory. It is a hack because I still would have to rig a way to communicate which gui_script.py to the Startup Mod launcher for it to exec (e.g., passing it via env var), and that seems as hacky as my freecadnew Bash script.
Until someone thinks this suggestion is of wide utility, let's drop it for now.
Re: How to run Python script in the GUI at startup and continue interactively?
This has been discussed already and IIRC this was too much a security hole.
Re: How to run Python script in the GUI at startup and continue interactively?
That doesn't make any sense to me. If this was a security hole then the other option that allows you to run scripts from the console mode would also be considered a security risk.
Re: How to run Python script in the GUI at startup and continue interactively?
Maybe I mistook. According old thread, you can run the script just by passing it as argument to the FreeCAD binary.
Re: How to run Python script in the GUI at startup and continue interactively?
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
My macros on Gist.github here complete macros Wiki and forum.
Re: How to run Python script in the GUI at startup and continue interactively?
This is precisely what is detailed in Macro_at_Startup
Re: How to run Python script in the GUI at startup and continue interactively?
Thank you openBrain!! I missed that because the -h output makes no indication of this:openBrain wrote: ↑Tue Jan 21, 2020 4:40 pm Maybe I mistook. According old thread, you can run the script just by passing it as argument to the FreeCAD binary.
Code: Select all
FreeCAD
For a detailed description see https://www.freecadweb.org/wiki/Start_up_and_Configuration
Usage: FreeCAD [options] File1 File2 ...
Allowed options:
Generic options:
-v [ --version ] Prints version string
-h [ --help ] Prints help message
-c [ --console ] Starts in console mode
--response-file arg Can be specified with '@name', too
--dump-config Dumps configuration
--get-config arg Prints the value of the requested configuration key
Configuration:
-l [ --write-log ] Writes a log file to:
/home/brentg/.FreeCAD/FreeCAD.log
--log-file arg Unlike --write-log this allows logging to an
arbitrary file
-u [ --user-cfg ] arg User config file to load/save user settings
-s [ --system-cfg ] arg System config file to load/save system settings
-t [ --run-test ] arg Test case - or 0 for all
-M [ --module-path ] arg Additional module paths
-P [ --python-path ] arg Additional python paths
--single-instance Allow to run a single instance of the application
So this is a wiki and/or command-line -h output bug where it does not clarify that File1 File2 ... can be a python script or macro. Not a big deal, but seems like it would be an easy fix.Python scripts/FreeCAD macro files can be specified as command line arguments to either of the executables mentioned above.