We are successfully launching Rhino 7 with command line parameters to run a script. For testing, the script just waits a few seconds and then exits with the sys.exit() command. Except that it always displays a popup window so that a user has to press a button. Our goal is to run the system unattended. Also, we are hoping to capture the “exit number” such as sys.exit(33) but have not been able to get that far yet.
Here is the simple script that Rhino is running:
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
import os
import sys
import time
time.sleep(3)
rs.DocumentModified(False)
sys.exit(3)
Both the script above and the Python script that runs Rhino are attached below. The output from the Python script running Rhino looks like this…
C:\Users\henry\Documents>python RunRhinoFromPython.py
The executable path exists
The script path exists
“C:\Program Files\Rhino 7\System\Rhino.exe” /nosplash /runscript=“-RunPythonScript ““C:\Users\henry\Documents\rhino_sysexit.py””" /runscript="-Exit”
Starting the application…
Process has a PID = 8528
b’’
b’’
which (I think) is good except that it requires a user to dismiss this popup window.
Thank you for the information, Nathan. That removed the dialog when exiting the application. That part is a success. Unlike the sys.exit() function, Rhino.RhinoApp.Exit(True) does not accept any exit code.
I tried to write the stderr and stdout directly using:
sys.stderr.write(‘Using the write command’)
sys.stdout.write(‘Something from stdout’)
in the script but they appear to be eaten by Rhino and do not show up to the calling program. I tried a different type of method to open Rhino and still did not get anything back from stderr or stdout.
Here is the program that opens Rhino using (what I think are) standard Python procedures: RunRhinoFromPython.py (2.5 KB)
There still does not appear to be an exit code when Rhino closes. Well, technically there is an exit code of zero that I cannot change.
This is the script that Rhino runs
import ctypes
import time
import sys
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
# Pause for dramatic effect
time.sleep(3)
# We don't want to save the document
#rs.DocumentModified(False)
sc.doc.Modified = False
# Write to stderr and stdout
sys.stderr.write('Using the write command')
sys.stdout.write('Something from stdout')
hWnd = Rhino.RhinoApp.MainWindowHandle()
ctypes.windll.user32.PostMessageW(hWnd, 0x0010, 0, 0)
# Exit the application without and popup dialogs
Rhino.RhinoApp.Exit(True)
# This will not work
#sys.exit(3)
I couldn’t find understandable documentation on the PostMessageW parameters so I tried (hWnd, 0x0010, 0, 1) and (hWnd, 0x0010, 1, 0) but still the exit code was zero.
It looks like we will have to just write exit/error messages to a text file instead of using exit codes.
Not on a system with Python installed? What kind of barbarians do you work for? Even my abacus came with Python installed… sure, it is v2.7 but still not too bad.
Yes, the methods above will all start Python remotely but the original problem was to return an error code the way most(?) applications do. Since Python is running inside Rhino there doesn’t seem to be a way to exit from Python with an error code. It isn’t the end of the world.