Skip to content

Instantly share code, notes, and snippets.

@miica37
Last active May 8, 2022 01:49
Show Gist options
  • Save miica37/81af00bb36ec2c42d422b68ceeec294c to your computer and use it in GitHub Desktop.
Save miica37/81af00bb36ec2c42d422b68ceeec294c to your computer and use it in GitHub Desktop.
Maya Python Script to Capture Viewport
import os
import maya.cmds as cmds
from time import gmtime, strftime
def scene_resolution():
return [cmds.getAttr("defaultResolution.width")*3,cmds.getAttr("defaultResolution.height")*3]
def playback_selection_range():
aPlayBackSliderPython = mel.eval('$tmpVar=$gPlayBackSlider')
time_selection = cmds.timeControl(aPlayBackSliderPython, q=True, rng=True)[1:-1]
start = round(float(time_selection.split(":")[0]))
end = round(float(time_selection.split(":")[1]))
if start + 1 == end:
return None
else:
return [start, end]
def playblast_snapshot(path=None, range=None):
current_image_format = cmds.getAttr("defaultRenderGlobals.imageFormat")
cmds.setAttr("defaultRenderGlobals.imageFormat", 32) # *.png
if range is None:
range = playback_selection_range()
if range is None:
start = cmds.playbackOptions(q=True, min=True)
end = cmds.playbackOptions(q=True, max=True)
range = [start, end]
cmds.playblast(frame=int((range[0] + range[1]) / 2), cf=path, fmt="image", orn=0, os=1,
wh=scene_resolution(), p=100, v=False)
cmds.setAttr("defaultRenderGlobals.imageFormat", current_image_format)
# Normal Preview
#----------------------------
my_documents_path = os.path.expanduser('~')
user_path = os.path.dirname(my_documents_path)
desktop_path = os.path.normpath(os.path.join(user_path, 'Desktop'))
current_frame = int(cmds.currentTime(q=True))
filename = "snap_{}.png".format(strftime("%m%d_%H%M%S", gmtime()))
snap_output = os.path.join(desktop_path, filename)
playblast_snapshot(snap_output, [current_frame, current_frame])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment