As you are working on your Maya scene, you may encounter moments that are worth taking a snapshot of, for reference. Another use may be for versioning: take snapshots of your progress. As you save different versions of your scene, you can quickly look at the previews instead of opening the project.
Below is a short script that can be added as a shelf item. It takes a snapshot of the active viewport, using Maya's playblast feature, and saves it in a folder next to the current scene file - taking care of renaming it in the process.
This section should rapidly get you up to speed on what the script does.
For convenience, the image dimensions are placed at the top. Here you can specify the width and height of the playblast image.
Note: The size of the image is capped at the width and height set in the scene's Render Settings panel. The default is 960 x 540.
Panels and cameras
Since the playblast command renders the active panel, we have to check whether the active panel is actually a viewport. The technical name for a viewport in Maya is: modelPanel.
If we have the correct panel we can then retrieve the active camera.
Here are some useful links:
Working with Files and Paths
The file command is used to read the scene name and path. Using the shn flag we retrieve only the scene name, excluding the path.
We can then customize the folder path. In our case we added -prv/.
Our folder path is then passed to the makedirs command to create the actual folder - right next to the scene file.
The full path will instead be specified in the playblast command to define the image output.
Note: We used the .jpg extension. Make sure to change this if you specify a different output image format in the playblast command.
A playblast is simply a preview generated from the active viewport. The tool is accessible from Windows > Playblast in Maya. By default it takes the settings from the Render Settings > Common Tab. We can override those settings using custom flags/options.
For a list of all available flags see the link below.
Logging and Messages
To provide feedback or error messages there are a few options available. For the alert popup error message we used the confirmDialog command.
Popup Error Message
Below is the link to the command reference.
Additionally, the promptDialog offers a text field for user input.
Logging to the Script Editor Console
We can simply use the print() command to output a message in the Script Editor console.
Additionally we can have messages popup in the active viewport. Maya offers a command for that: inViewMessage.
Note: Remember to convert Numbers to Strings when concatenating text. You can use the str() command but it is usually best to use String Formatting.
Creating Shelf Items
First make sure you have the shelf enabled. You can turn it on here: Windows > UI Elements > Shelf.
With the shelf open, locate the settings cog icon and choose the shelf editor...
In the shelf editor you can create a new shelf or add a new item to an existing one. To add a new item simply choose the new item button and rename the shelf item.
Head over to the Command tab, select Python as the source language and paste in the script.
Note: The shelf items are automatically saved for the current scene when you make changes. You may want to ensure the shelves are saved to the Maya preferences by manually choosing the Save All Shelves button.
Customizing Shelf Items
You can customize the look by choosing an icon. You can upload or choose one from the built in factory icons.
Write a description of what the shelf item does in the Tooltip field.
For more info on creating or editing shelf items, refer to: