Skip to content

Instantly share code, notes, and snippets.

@sonictk
Created August 1, 2018 22:10
Show Gist options
  • Save sonictk/1766aef257d698859c2cf6ccd7a1464a to your computer and use it in GitHub Desktop.
Save sonictk/1766aef257d698859c2cf6ccd7a1464a to your computer and use it in GitHub Desktop.
def playblastViewport(outputDir,
filename='image',
fileFormat='iff'):
"""
This function playblasts the current active viewport to a given directory on
disk.
NOTE:
This function can only be run in a Maya interactive session.
Args:
outputDir (str): The path to the directory to write the images to.
filename (str): The base name of the image. The frame number will be
appended at the end of the filename.
fileFormat (str): The format that the image should be saved in. Valid
formats are: als, bmp, cin, gif, jpg, rla, sgi, tga, tif.
"iff" is the default, and is the fastest format to save into since
it doesn't need any internal conversion.
"""
if not os.path.isdir(outputDir):
os.makedirs(outputDir)
origTime = omAnim.MAnimControl.currentTime()
startTime = omAnim.MAnimControl.minTime()
endTime = omAnim.MAnimControl.maxTime()
omAnim.MAnimControl.setCurrentTime(startTime)
view = omUi.M3dView.active3dView()
if view.getRendererName() == omUi.M3dView.kViewport2Renderer:
vp2Enabled = True
else:
vp2Enabled = False
startFrame = int(startTime.asUnits(om.MTime.kFilm))
endFrame = int(endTime.asUnits(om.MTime.kFilm))
totalFrames = endFrame - startFrame
progressWindow = omUi.MProgressWindow()
progressWindow.reserve()
progressWindow.setInterruptable(False)
progressWindow.setProgressRange(0, totalFrames)
progressWindow.setProgress(0)
progressWindow.setTitle('Creating playblast')
progressWindow.startProgress()
for frameNum in xrange(startFrame, endFrame):
progressWindow.setProgressStatus('Writing frame {0} of {1}...'.format(
progressWindow.progress(), totalFrames)
)
view.scheduleRefresh()
image = om.MImage()
# NOTE: (yliangsiew) By default, MMimage format is BGRA byte and VP2 is float.
if vp2Enabled:
image.create(view.portWidth(), view.portHeight(), 4, om.MImage.kFloat)
view.readColorBuffer(image)
image.convertPixelFormat(om.MImage.kByte)
else:
view.readColorBuffer(image)
image.writeToFile(outputDir + '/{0}.{1}.{2}'.format(filename,
str(frameNum).zfill(3),
fileFormat),
fileFormat)
progressWindow.advanceProgress(1)
omAnim.MAnimControl.setCurrentTime(om.MTime(frameNum, om.MTime.kFilm))
omAnim.MAnimControl.setCurrentTime(origTime)
progressWindow.endProgress()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment