Python - Live Housekeeping in Maya.

Sometimes, there's a need to make sure things aren't saved with the scene. Things like helper geometry or specific 3rd Party plugin dependency nodes, or even maya locator or lights. And sometimes you may just want to have a bit of a clear out.

Expressions for example. I wonder if you've also come across the 80mb+ file that had nothing but simple geometry yet filled to the brim with legacy debris in the DAG from 3rd party tools that had no right to be in your pipeline. In this case, a tool one of the team used generated expressions to provide an interactive UX for splitting polys. For one reason or another it failed to cleanup after itself. Added to the fact that an artist's or animator's workflow can often propogate these tpyes of nodes by means of 'dirty' imports, it can lead to rubbish in the scenes than at the very least increases the storage footprint and load / save times, at the worst it could compromise the scene's export integrity. Expressions are one example of this 80mb 'infection'.

So we needed a way to clean the scene, but not rely on the users to action this clean up. We decided that running the cleanup script when the user saves is probably a good time to do this.

So rather than a scriptJob (iirc this doesn't catch save events), I'm using the API and OpenMaya.MSceneMessage. The kBeforeSave enumerator is used to register a callback to a function that does the cleaning up, onSave in this case.

import maya.cmds as mc
import maya.OpenMaya as om

def onSave(*args):

callbackID = om.MSceneMessage.addCallback(om.MSceneMessage.kBeforeSave,onSave)

This will execture everytime the user saves. It's unobtrusive and simple.

To remove it, use the following..
def callBackOff():

This worked well to clean assets whilst the underlying issues were dealt with, but I've often thought this could be used for other live housekeeping duties. We nearly always validate for game assets exports, rarely for maya saves.

And if you had an evil streak, you could have a lot of fun with this.

No comments:

Post a comment