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

**URL:** <https://discourse.mcneel.com/t/scripting-changes-via-python-rhino-wont-exit-if-i-take-a-screenshot/8205>\
**Category:** Scripting\
**Created:** [May 7, 2014, 6:47pm UTC](https://discourse.mcneel.com/t/scripting-changes-via-python-rhino-wont-exit-if-i-take-a-screenshot/8205 "2014-05-07T18:47:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![endingpop](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@endingpop](https://discourse.mcneel.com/u/endingpop)\
**Post date:** [May 7, 2014, 6:47pm UTC](https://discourse.mcneel.com/t/scripting-changes-via-python-rhino-wont-exit-if-i-take-a-screenshot/8205/1 "2014-05-07T18:47:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![endingpop](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@endingpop](https://discourse.mcneel.com/u/endingpop)\
**Post date:** [May 7, 2014, 6:55pm UTC](https://discourse.mcneel.com/t/scripting-changes-via-python-rhino-wont-exit-if-i-take-a-screenshot/8205/2 "2014-05-07T18:55:33Z")

</div>

[Here’s the Grasshopper file used.](https://www.dropbox.com/s/bdbs029hlj54hrp/definedvolume.ghx)

---

<div class="post-metadata">

**Author:** ![dannyboyesiom](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dannyboyesiom/32/747690_2.png) [@dannyboyesiom](https://discourse.mcneel.com/u/dannyboyesiom)\
**Post date:** [May 7, 2014, 8:10pm UTC](https://discourse.mcneel.com/t/scripting-changes-via-python-rhino-wont-exit-if-i-take-a-screenshot/8205/3 "2014-05-07T20:10:26Z")

</div>

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](http://www.grasshopper3d.com/forum/topics/screenshot-of-viewport?commentId=2985220%3AComment%3A924258)

---

<div class="post-metadata">

**Author:** ![endingpop](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@endingpop](https://discourse.mcneel.com/u/endingpop)\
**Post date:** [May 9, 2014, 3:10pm UTC](https://discourse.mcneel.com/t/scripting-changes-via-python-rhino-wont-exit-if-i-take-a-screenshot/8205/4 "2014-05-09T15:10:45Z")

</div>

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()
```
