Scripting changes via Python, Rhino won't exit if I take a screenshot


#1

I’m having trouble with a Python script I wrote to modify some parameters in GH, update the geometry, and then exit. I’d like a screentshot of the updated geometry, and when I include that command the exit is ignored and Rhino stays open waiting for user input. Here’s my script:

import clr
clr.AddReferenceByName(“Grasshopper”)

import System
import Rhino
import Grasshopper
from Rhino import RhinoApp, RhinoDoc
from Rhino.Geometry import *
import rhinoscript as rs

radius = 5.5
height = 8.5

gh = RhinoApp.GetPlugInObject(“Grasshopper”)
gh.LoadEditor()
gh.CloseAllDocuments()
gh.ShowEditor()
gh.OpenDocument(“definedvolume.ghx”)

docServer = Grasshopper.GH_InstanceServer.DocumentServer
doc = docServer[0] # first opened document

def FindBatteryByNickname(docObjects, name):
if docObjects is None: return None
for obj in docObjects:
attr = obj.Attributes
if attr.PathName == name:
return obj
raise Exception(name + " was not found in document")

radiusSlider = FindBatteryByNickname(doc.Objects, “Radius”)
heightSlider = FindBatteryByNickname(doc.Objects, “Height”)

radiusSlider.SetSliderValue(radius)
heightSlider.SetSliderValue(height)

gh.RunSolver(False)

volumeObj = FindBatteryByNickname(doc.Objects, “VolumeOutput”)
stringOut = volumeObj.InstanceDescription.decode(‘unicode_escape’).encode(‘ascii’,‘ignore’)
vol = stringOut.split(’\n’)[1]
outputFile = open(‘volume.txt’, ‘w’)
outputFile.write(‘Volume\n’)
outputFile.write(vol)
outputFile.close()

path = “C:\Temp\”

rs.application.Command("_-ScreenCaptureToFile " + path + “cyl.jpg”)
gh.CloseAllDocuments()
rs.application.Command("_Exit")

I tried two different screenshot commands. Both generate a screenshot, but neither will exit. If I comment out the screenshot command it exits as expected. I’d appreciate any help figuring out how to get it to do both the screenshot and exit.

As a secondary question, it doesn’t appear that Rhino has a background execution. The screenshot also will grab whatever’s on the screen where the perspective viewport is, resulting in screenshots of folders or other applications if I run this and look at something else on that screen. Anyone have a solution to that?

Thanks,
Marcus


#2

Here’s the Grasshopper file used.


#3

There is a capturetobitmap or a viewporttoclipboard method more can be read on the gh forum if you search for screenshot of viewport. I cannot seem to paste the link from my tablet

EDIT: Here is the link: http://www.grasshopper3d.com/forum/topics/screenshot-of-viewport?commentId=2985220%3AComment%3A924258


#4

I found a workaround to what seems like a bug to me. I use the rhinosyntax exit instead of the RhinoScript command:

rs.application.Exit()