PEG Naming-[Maya Save Interruption]
Updated: Oct 5, 2018
So now we've settled our core logic of making sure that a file name is following our rules. But now we've got to make sure that this is checked each and every time that the user presses save. (Side note: all files are available in the github download zip here)
OPEN MAYA MODULE
This is achieved through a module called OpenMaya. This module allows for us to intercept the save event and insert our own function in front of it. Importing this module along with our file validation we just wrote, and our module for checking location, we can complete the core goal here.
First things first however. First we build the function that we want to run each time that save is called. (This is being built inside the LoadPreSave)
So within that, we first go out and check the file location against the list of file directories that the user defines as wanting the script PEG to control their file names. If it's not in that list of places, then we don't care about this name and we can let the save go through unimpeded.
If it is a location that the user wants PEG to control the name, then we must run the name through the validation we previously wrote, allowing it to return either a list of errors with the name, or a NoneType variable which means that it passed our tests.
If it did not pass the test, then we rename the file to be TEMP and then pop up some UI that will show to the user what they did wrong and give them a chance to fix their errors. For clarity's sake here is an image of that UI that we have yet to cover within Maya.
But before we get bogged down with UI talk and that rabbit hole, let's talk about how we make this function spring each time that the user saves.
This line right here is what does that. We imported the OpenMaya module and are referring to it here as om for short. We are adding a step to the normal save routine, and asking Maya to insert a task before save. (kBeforeSave) The task is to run this function (onSave) which was defined just above this. Now this line would normally have to be run each time a user opens Maya again, as it is a session based flag in the system. But there is a workaround for that of course.
MAKING IT RUN BACKGROUND
But what I did instead is include this file in an import in the userSetup file inside of the scripts folder. Maya automatically looks for scripts called siteCustomize and userSetup within the scripts folder each time it initializes, and runs them as part of it's setup. Therefore by adding a call to this script, which I titled LoadPreSave, it allows for the save function to be adjusted during Maya's startup and then the user never has to worry about it. Most users hopefully won't even know it's there.