[SOLVED] How to run Python script in the GUI at startup and continue interactively?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
bgoodr
Posts: 228
Joined: Sat Jun 14, 2014 9:40 pm

[SOLVED] How to run Python script in the GUI at startup and continue interactively?

Post by bgoodr »

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!
Last edited by bgoodr on Fri Jan 24, 2020 5:16 pm, edited 1 time in total.
User avatar
bgoodr
Posts: 228
Joined: Sat Jun 14, 2014 9:40 pm

Re: How to run Python script in the GUI at startup and continue interactively?

Post by bgoodr »

Here is a Bash script that implements what I would like to implement inside a Python script running within the FreeCAD Python interpreter:
  1. Download the attachment.
  2. Rename it to "freecadnew".
  3. Put it somewhere in your $PATH.
  4. Run chmod a+x path_to_where_you_copied_it_to to give it execute permissions.
  5. 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).
  6. Invoke it like this: freecadnew /tmp/foo/bar/my_new_doc.FCStd
  7. 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
Now, this gets me there for sure, but it is invasive as it makes assumptions about the internal schema of the XML files, which I suspect is subject to change.

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)
$ 
Thanks!
Attachments
freecadnew.txt
(3.3 KiB) Downloaded 92 times
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to run Python script in the GUI at startup and continue interactively?

Post by openBrain »

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. ;)
User avatar
bgoodr
Posts: 228
Joined: Sat Jun 14, 2014 9:40 pm

Re: How to run Python script in the GUI at startup and continue interactively?

Post by bgoodr »

openBrain wrote: Mon Jan 20, 2020 5:25 pm 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. ;)
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.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to run Python script in the GUI at startup and continue interactively?

Post by openBrain »

This has been discussed already and IIRC this was too much a security hole. ;)
User avatar
bgoodr
Posts: 228
Joined: Sat Jun 14, 2014 9:40 pm

Re: How to run Python script in the GUI at startup and continue interactively?

Post by bgoodr »

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.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to run Python script in the GUI at startup and continue interactively?

Post by openBrain »

Maybe I mistook. According old thread, you can run the script just by passing it as argument to the FreeCAD binary. ;)
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: How to run Python script in the GUI at startup and continue interactively?

Post by mario52 »

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.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to run Python script in the GUI at startup and continue interactively?

Post by openBrain »

mario52 wrote: Tue Jan 21, 2020 8:55 pm other [Macro] Bit of help for super users :)
This is precisely what is detailed in Macro_at_Startup ;)
User avatar
bgoodr
Posts: 228
Joined: Sat Jun 14, 2014 9:40 pm

Re: How to run Python script in the GUI at startup and continue interactively?

Post by bgoodr »

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. ;)
Thank you openBrain!! I missed that because the -h output makes no indication of this:

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

But deep in the guts of https://www.freecadweb.org/wiki/Start_u ... _Interface do I finally find the answer:
Python scripts/FreeCAD macro files can be specified as command line arguments to either of the executables mentioned above.
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.
Post Reply