XSI Viewport Capture with Python


Had to create a function to capture the viewport today and thought it’d be useful to post it here for others to find.

NOTE: You may need to do the procedure on this page to get this to work properly: http://www.xsi-blog.com/archives/24

See below:

# Python
 
import os
xsi = Application
log = xsi.LogMessage
 
def ET_ViewportCapture( inWidth, inHeight, inPath, inFileName):
 
	# Get current frame
	oPlayControl = xsi.ActiveProject.Properties.Find("PlayControl")
	Fc = oPlayControl.Parameters("Current").Value
 
	# Get Viewport Object
	oViewportCapture = xsi.Dictionary.GetObject("ViewportCapture")
 
	# Start End Frame set to current frame
	oViewportCapture.NestedObjects("Start Frame").Value = Fc
	oViewportCapture.NestedObjects("End Frame").Value = Fc
	oViewportCapture.NestedObjects("OpenGL Anti-Aliasing").Value = 4
 
	# Set image size
	oViewportCapture.NestedObjects("Width").Value = inWidth
	oViewportCapture.NestedObjects("Height").Value = inHeight
 
	# Set the output file name
	oSlash = XSIUtils.Slash
 
	oPath = str(xsi.ActiveProject2.Path) + str(inPath)
 
	#
	if os.path.isdir(oPath) == False:
		os.mkdir(oPath)
 
	# Set output file location
	oViewportCapture.NestedObjects("File Name").Value = inPath + str(inFileName) + ".pic"
	oViewportCapture.NestedObjects("Padding").Value = "(fn)(ext)"
 
	# Set viewport options
	oConstructionLevel = xsi.Dictionary.GetObject("Views.ViewB.UserCamera.camvis.constructionlevel")
	oGridAxis = xsi.Dictionary.GetObject("Views.ViewB.UserCamera.camvis.gridaxisvis")
 
	oConstructionLevel.Value = 0
	oGridAxis.Value = 0
 
	xsi.CaptureViewport(2, 0)
 
ET_ViewportCapture(75,75,"\\Render_Pictures\\","FileName")