Launching Rhino 5 with a batch file and running python scripts

Hello,

I am interested in running Rhino 5 remotely using a batch file. Here’s what I need to do with one batch file:

  • Launch Rhino 5 and load a specific file
  • Run a python script (the command “RunPythonScript”)
  • Exit Rhino

The python script makes a new text file at the end which I need to pass to some modelling stuff in C++. I can work out how to run Rhino via a batch file, but not run the script. Here is my current file built from the guide here: http://wiki.mcneel.com/developer/commandline

“H:*Correct File Directory*\Rhino 5\Rhino_File.3dm”

This runs the file but nothing I’ve tried runs a python script, even through trying the “/runscript” command. Can you run python files in Rhino using the command line?

Thanks very much, if you need any specifics let me know.

In my opinion, trying to automate Rhino in this manner is difficult. For one, there are a limited number of characters you can pass to an application on the command line. And, the command line syntax can be very tricky, especially for file names and even trickier. File names with spaces in them will need nested double-quote characters.

An alternative approach to consider is to automate Rhino using ActiveX automation. Do to this, you would create a script (using either VBScript or JScript) which would launch Rhino. Once Rhino was running, you could have the script tell Rhino to run some commands.

Here is a fairly simple script that runs Rrhino, draws a few curves, saves the file to your desktop, and then closes.

Option Explicit

Sub TestRhinoAutomation

  ' Declare local variables
  Dim objRhino, objShell, strDesktop, strPath
  
  ' Create a Rhino 5 64-bit application object
  On Error Resume Next
  Set objRhino = CreateObject("Rhino5x64.Application")
  If Err.Number <> 0 Then
    On Error Resume Next
    ' Create a Rhino 5 32-bit application object
    Set objRhino = CreateObject("Rhino5.Application")
    If Err.Number <> 0 Then
      Call WScript.Echo("Failed to create Rhino object.")
      Exit Sub
    End If
  End If
  Call WScript.Echo("Rhino object created.")

  ' Run some commands
  Call objRhino.RunScript("_Circle 0 10", 0)
  Call objRhino.RunScript("_Line -5,-5,0 5,5,0", 0)
  Call objRhino.RunScript("_Line 5,-5,0 -5,5,0", 0)
  Call WScript.Echo("Geometry created.")
  
  ' Create a shell object
  Set objShell = WScript.CreateObject("WScript.Shell")
  Call WScript.Echo("Shell object created.")
  
  ' Get the desktop folder
  strDesktop = objShell.SpecialFolders("Desktop")
  strPath = Chr(34) & strDesktop & "\TestRhinoAutomation.3dm" & Chr(34)

  ' Save the file  
  Call objRhino.RunScript("_-Save " & strPath, 0)
  Call WScript.Echo("File saved.")
 
  ' Exit Rhino
  Call objRhino.RunScript("_Exit", 0)
  Call WScript.Echo("Done!")
   
End Sub

' Run the subroutine defined above
Call TestRhinoAutomation

To run the script, you’d use the CSCRIPT command-line command.

C:\> cscript TestRhinoAutomation.vbs

Also, from automation, you can get a hold of the RhinoScript object and call methods on it (if you need to get fancier). Here is an example of that.

Does this help?

you might try to run your python script by running it from a rhino script which is started using the /runscript command line option :wink:

Call Main()
Sub Main()
    Rhino.Command "_-RunPythonScript ScriptName.py", True
End Sub

c.

This seems like a good method, clement’s simpler suggestion below might be the one we prefer. Thanks!

Thanks - this seems like the simplest method. We will probably use this.

Hi, I need this procedure, but with c#

https://github.com/mcneel/rhino-developer-samples/tree/6/rhinocommon/cs/SampleCsAutomation

– Dale