Batch -ViewCapturetoFile - Rhino + Python Novice

I’ve found a few other threads to touch on the topic of scripting w/ -ViewCaptureToFile, but none of them seem to specifically address the problem I’m having.

Problem: Script finishes running and successfully changes the active view and changes the display mode, however, it fails to actually output a file.

Any help/suggestions would be greatly appreciated. Thanks!

import rhinoscriptsyntax as rs
fileName = "C:\Users\norwoodw\Desktop\ViewCaptureTest\Test.png"
#fileLocation = "C:\Users\norwoodw\Desktop\ViewCaptureTest\"
#Name = "2017_0807-TestCapture"
#fileExtension = ".png"
#fileName = fileLocation + Name + fileExtension
rs.RestoreNamedView ("Perspective")
rs.Command("-SetDisplayMode " + "PW_Architectural")
rs.Command("-ViewCaptureToFile " +(fileName)+ "_Width 1600 _Height 900 _Scale 4 _TransparentBackgroud Yes     _Enter")

@Weston_Norwood,

it looks like you are missing a space in your last line between filename and _Width. Also take care at the end of the last line there are too many spaces before the _Enter.

btw. you might consider to put quotes around the filename so rhino’s commandline does not interpret the spaces as Enter like below:

chr(34) + filename + chr(34)

_
c.

1 Like

Thank you for the reply. I tried cleaning up the code and inserting the spaces as you suggested. chr(34) seems to be ‘double quote’ and chr(32) ‘space’. The command is still running all the way through, and when I type it manually the width, height, and scale are indeed all changed. It doesn’t seem to be passing the file path through, as there is no file output. Any other thoughts?
Thanks!
import Rhino
import sys

import rhinoscriptsyntax as rs
fileName = " 
C:\Users\norwoodw\Desktop\ViewCaptureTest\Test.png"
#fileLocation = "C:\Users\norwoodw\Desktop\ViewCaptureTest\"
#Name = "2017_0807-TestCapture"
#fileExtension = ".png"
#fileName = fileLocation + Name + fileExtension
rs.RestoreNamedView ("Perspective")
rs.Command("-SetDisplayMode " + "PW_Architectural")
rs.Command("-ViewCaptureToFile" +chr(32)+(fileName)+chr(32)+     "_Width 1600 _Height 900 _Scale 4 _TransparentBackgroud Yes")

Hi @Weston_Norwood,

this seems to work here, note the double slashes and how i created the file path:

import rhinoscriptsyntax as rs
fileLocation = "C:\\Users\\norwoodw\\Desktop\\"
Name = "2017_0807-TestCapture"
fileExtension = ".png"
fileName = chr(34) + fileLocation + Name + fileExtension + chr(34)
rs.RestoreNamedView ("Perspective")
rs.Command("-SetDisplayMode " + "PW_Architectural")
cmd = " _Width 1600 _Height 900 _Scale 4 _TransparentBackgroud _Yes _Enter"
rs.Command("-ViewCaptureToFile " + fileName + cmd)

I’ve used the desktop as destination.

_
c.

1 Like

I really appreciate your help. I also appreciate your notes about why you did certain things.
This little tool will likely save myself and co-workers time, but I’m primarily working on it as a means of learning python.
Thanks for helping me along the way!

import Rhino
import rhinoscriptsyntax as rs
import sys
import time
#import time
fileLocation = "C:\\Users\\norwoodw\\Desktop\\ViewCaptureTest\\"
Name = "2017_0807-TestCapture"
fileExtension = ".png"
fileName = chr(34) + fileLocation + Name + fileExtension + chr(34)
cmd = " _Width 1600 _Height 900 _Scale 4 _TransparentBackgroud _Yes _Enter"
views = rs.NamedViews()
if views:
for view in views:
    fileName = chr(34) + fileLocation + Name + view + fileExtension + chr(34)
    rs.RestoreNamedView (view)
    rs.Command("-SetDisplayMode " + "PW_Architectural")
    rs.Command("-ViewCaptureToFile " + fileName + cmd)
    print ("Successfully captured view "+(view)+"!")
    time.sleep(.5)
else:
    print ("There are no named views detected.")    
print("Successfully caputered all named views!")
1 Like

Hi @Weston_Norwood,

In many programming languages, the backslash character in a string denotes a string literal. Thus, if you want to use a backslash in a string, and not as a string literal, then you need to specify 2 backslashes. For example:

# BAD
path = "C:\Users\norwoodw\Desktop\ViewCaptureTest\"
# GOOD
path = "C:\\Users\\norwoodw\\Desktop\\ViewCaptureTest\\"

Also, when passing strings to the Rhino command line, if the string contains spaces, then the whole string should be surrounded by double-quote characters. This is because Rhino interprets spaces as an key character. For example:

# BAD
path = "C:\\Users\\norwoodw\\Desktop\\View Capture Test\\"
# GOOD
path = chr(34) + "C:\\Users\\norwoodw\\Desktop\\View Capture Test\\" + chr(34)

where the ASCII character 34 is a double-quote character.

Note, if you do this a lot, it can be useful to have a utility function that does this for you. For example:

def Quote(str):
    return chr(34) + str + chr(34)
    
path = Quote("C:\\Users\\norwoodw\\Desktop\\View Capture Test\\")

– Dale

2 Likes