Created
August 19, 2020 13:58
-
-
Save gfxhacks/f3e750f416f94952d7c9894ed8f78a71 to your computer and use it in GitHub Desktop.
Autodesk Maya: Takes a screenshot of the active viewport using the playblast feature and saves it next to the scene file. For more info: https://gfxhacks.com/versioning-with-previews-in-maya/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Name: scene-preview.py | |
Author: gfxhacks.com | |
Desc: Captures and saves a scene preview based on the active viewport, at the current frame. Uses playblast feature. | |
''' | |
import maya.cmds as cmds | |
import os | |
import time | |
# set screenshot dimensions | |
width = 960 | |
height = 540 | |
# get active viewport panel | |
panel = cmds.getPanel(withFocus=True) | |
# throw error if active panel is not a viewport | |
if "modelPanel" not in cmds.getPanel(typeOf=panel): | |
cmds.confirmDialog(title='Error!', message='Please select a viewport panel first.', button=['Ok'], defaultButton='Ok', dismissString='No') | |
raise RuntimeError('Error: Please select a viewport panel, then try again.') | |
# get current frame number | |
curFrame = int(cmds.currentTime(query=True)) | |
# get name of current file | |
scName = cmds.file(query=True, sn=True, shn=True) | |
# get full path of current file | |
scPath = cmds.file(query=True, sn=True) | |
# set new path where previews will be saved to | |
path = scPath + "-prv/" | |
# get name of current camera | |
cam = cmds.modelEditor(panel, query=True, camera=True) | |
# get current timestamp | |
ts = int(time.time()) | |
# construct full path | |
fullPath = "{}{}-{}-f{}-{}.jpg".format(path, scName, cam, curFrame, ts) | |
# Create path if it doesn't exist | |
if not os.path.exists(path): | |
os.makedirs(path) | |
# run playblast for current viewport | |
cmds.playblast(fr=curFrame, v=False, fmt="image", c="jpg", orn=False, cf=fullPath, wh=[width,height], p=100) | |
# log path to console for reference | |
print('Snapshot saved as: ' + fullPath) | |
# show popup message in viewport | |
cmds.inViewMessage(amg='<span style="color:#82C99A;">Snapshot saved</span> for <hl>' + cam + '</hl> in <hl>' + scName + '<hl> at frame <hl>' + str(curFrame) + '</hl>', pos='topRight', fade=True, fst=3000) |
Cool glad it worked 😊 - we'll update the script and article, thx for pointing out the issue. Future updates will be on the repo, since it's easier to track: https://github.com/gfxhacks/maya-scene-preview
Hi gfxhacks, I got the same error message as curiocrucible said and I swapped the command line as mentioned but still got the error message, may I know if there is any solution? thank you :)
I am using Maya 2023 on Window
Hi Xavier, could you let us know the camera name that you are using?
If there is more than one |
separator in the name string you might want to try using [-1]
instead of [1]
# get name of current camera
cam = cmds.modelEditor(panel, query=True, camera=True).split("|")[-1]
Thank you and its fixed, cheers :D
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updating the cam name query worked! Thank you so much for taking the time and being so responsive, I really appreciate it!