Best way to use Python to launch Rhino and then run some more Python

Hi community folks,

The TL;DR is that I want to automate the use of Rhino and another tool using Python as the “master” program that is summoning the other apps to do specific tasks. What’s the best way to have Python call an instance of Rhino, to then have Rhino fire up and run a RhinoPython script?

I’m trying to use Python to oversee some automation that incorporates multiple software tools, with a work-flow something like this:

Main routine starts

  • Initializes file lists of things to be processed
  • pulls configuration data from some .py or txt files that are meant to be edited/editable
  • Calls Rhino, telling it to initialize a particular Python script, which upon full initialization:
    • loads a 3dm file, then initializes a loop where it:
    • imports another geometry file (.ply in this case)
    • does stuff with the geometry
    • saves out the results as a new .ply file
    • cleans up the current .ply data, ready to load a new one
  • Calls other tool (CloudCompare), does some stuff, and saves out a result as a .stl or .obj
  • Calls a new instance of Rhino, with a different Python script, which:
    • loads a reference mesh .obj
    • imports the output mesh from previous processing
    • checks the mesh for integrity, fixes what it can, and bails on the attempt if it can’t come up with a manifold mesh, logging it.
    • performs a mesh boolean operation between the meshes
    • If that succeeds, saves the resulting mesh as an .stl, otherwise error logs and moves on to re-setting the conditions for loading the next mesh.

When all the files initially queued have been processed (or error logged) the main routine quits, leaving that log for the user of what it could/couldn’t accomplish.

At the end of the whole process I want a batch of STLs that have come from these processing steps that can’t be all done efficiently or optimally in only one application, so I’m trying to “glue together” the tools from different apps to get the work done.

Any and all suggestions/code-snippets of recommendation would be very helpful!

And also, any suggestion as to what building such a tool should be worth to a client would be good information as well—I have no idea how to price coding work, as I’m a designer by background, and I’m learning my way here.

So much for TL;DRs :smile: Well, I think the best place to start would be Python’s subprocess module with which you could do a to run Rhino.exe and a RunPythonScript command call and go from there…

Sample script:

import subprocess
import os
rhinoPath = "C:\Program Files\Rhinoceros 5 (64-bit)\System\Rhino.exe" 
fileName = "D:\file.3dm" 
scriptName = "D:\"
scriptCall = "-_RunPythonScript {0}".format(scriptName)
callScript = '"{0}" /nosplash /runscript="{1}", "{2}"'.format(rhinoPath, scriptCall, fileName)
1 Like


Not sure if it helps you but a lot of the functionality of CloudCompare is available in Rhino Open Projects. The big ones being mesh-mesh registration and mesh-mesh deviation. If this is all you need you could run your whole program in Rhino.

Thanks @wattzie, that’s an interesting resource—but alas I need to do poisson reconstruction on point clouds, between Rhino operations on the point clouds first and then mesh operations after :wink:

@krsnadas that looks like constructing a command-line argument series that gets passed to Rhino on launch—telling Rhino to launch and then go straight to the called rhinoscript on launch, right?

I see a caveat to this is that there’s no direct communication between the master-script and the running instance of Rhino, so it’ll only know that the work is done with a returned [0] at the end of things, when the last thing the Rhino Python script does is quit the process—so that means that the master script can’t move on and would have to wait for that to finish before starting the procedure on the next file to process, or otherwise risk files being absent/colliding requests for intermediary files that haven’t finished processing yet (if it’s not waiting for the response before firing off the next step of things).

Then the master also has to wait until they’re all done before it can send any of them on to the next stage of processing (CloudCompare) without some clever coding to allow it to move on… At first blush I can think of a way to loop starting a round of Rhino & Cloud Compare simultaneously, so that result from Rhino for A gets passed into CC while Rhino starts on item B, but then they’re locked in time to each-other and aren’t asynchronous.

I don’t know diddly squat about threading, but I would like to figure out if these can be run separately to make use of multiple processors simultaneously, each instance of Rhino chugging along on a single file until they’re all done.

Hi krsnadas,

Is there a way to do the same thing from MATLAB? If so, what would be the command-sequence look like?