Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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/
'''
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)
@curiocrucible
Copy link

curiocrucible commented Sep 28, 2021

Amazing script! I was on Maya 2018 when I recently discovered it, and it worked flawlessly. I recently updated to Maya 2022 and the script stopped working. It generates the folder to dump the images to, but it doesn't save any image files there. Any chance you would be willing to update this to work with Maya 2022?

@gfxhacks
Copy link
Author

gfxhacks commented Sep 29, 2021

Hey there @curiocrucible :)
Just tested in 2022.1 and seems to be working. If you open the Script Editor and run the script, does it throw any error messages?

@curiocrucible
Copy link

curiocrucible commented Sep 29, 2021

Hi, thanks for the reply. I'm on 2022.2. If I run it from the Script Editor I get an error saying "Please select a viewport panel first." If I run it from the shelf button I made I don't get that error, and it generates a folder but doesn't save anything to the folder. I've tried activating different viewports, but the result is the same. It worked fine before I upgraded from 2018. Does the update to Python 3.7 perhaps change how the active viewport is fetched? Thanks again!

@gfxhacks
Copy link
Author

gfxhacks commented Sep 29, 2021

Can't seem to replicate on our end. If you are on Windows the issue might be with the | special character (currently on OSX here so can't check) - the cam name (since Maya 2019) seems to be returned as |persp|perspShape.

Try to swap the cam name query command with:

# get name of current camera
cam = cmds.modelEditor(panel, query=True, camera=True).split("|")[1]

@curiocrucible
Copy link

curiocrucible commented Sep 29, 2021

Updating the cam name query worked! Thank you so much for taking the time and being so responsive, I really appreciate it!

@gfxhacks
Copy link
Author

gfxhacks commented Sep 29, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment