Call RhinoCommand in Python Script

It is sometimes very important to call rhino commands in python script to realize batch rendering/saving/viewcapturing etc. Here is a code demonstrating how to realize it. This code has several functions like render&save file/ view capture to file etc.

For instance, if you want to batch save file:

import rhinoscriptsyntax as rs
for i in range(10):
FileName = "C:UsersXXXDesktopFile%s.3dm" % i
rs.Command("_-SaveAs " + chr(34) + FileName + chr(34), 0)

Here is a code demonstrating several functions like render&save file/ view capture to file etc.(written by Lauren Vasey for batch rendering or animating)

import rhinoscriptsyntax as rs
import System
import scriptcontext as sc
import os
# library written by Lauren Vasey for batch rendering or animating, copywrite ICD 2015
def RenderSingleFileViewName(filepath, filename, viewNames):
executeStatement = "_-Open " + chr(34) + filepath + chr(34)
# open file
rs.Command( executeStatement)
fileFolderName = filepath[:-4]
for view in viewNames:
renderName = fileFolderName + "_" + view + ".png"
#restore view and render
rs.CurrentView(view)
rs.Command ("_-Render")
rs.Command ("_-SaveRenderWindowAs " + chr(34) + renderName + chr(34), 0)
rs.Command ("_-CloseRenderWindow", 0)
return
def MainviewCaptureToFile(Folder = None, FileLabel = None, saveImage= False, extImg = ".png", count = None, wi = None, h = None):
allfiles = getAll3dmFilesinFolder(allfiles, extImg)
if not count == None:
nextfile = count
else:
nextfile = getExtensionNumber(allfiles)
newfileStr = str(nextfile)
lenNumber = len(newfileStr)
print"next file", nextfile
newfileStr = str(nextfile)
numVals = 4
numZeros = numVals - lenNumber
fileName = Folder + "\" + FileLabel
for l in range(numZeros):
fileName = fileName + "0"
fileNameImg = fileName + str(nextfile) + extImg
print fileNameImg
rs.Command ( "_-ViewCaptureToFile " + chr(34) + fileNameImg + chr(34) + " _-Enter", 0)
return