Recently, Timothy and I were talking about some interesting about message in Maya.
For daily tasks in Digimax, Timothy has to handle a mass of Maya scene files in batch way such that he can make it on schedule. But there are so many tasks and Maya scene files! He has spent some time preparing several scripts (Mel or Python, but most of them are Python) for that task. It seems work well but sometimes, he needs to get output messages from Maya for debugging and other scripts. For example, one Python script when invoked inside Maya would output a list of filtered and managed nodes in one Maya scene and after that, we can apply some other programs/scripts on nodes by this message.
How could we redirect all Maya messages into console or terminal while Maya is invoked in GUI mode?
My first reaction was “What a weird and kinda stupid question?” ‘cause it seems to be non sense to have a GUI mode Maya redirect its message to console. Because you have already opened a Maya GUI, all you have to do can be done within that GUI though. But it turns to to make sense when you are going to make use of scripts to automatically handle lots of Maya scenes (ex, 100 animation shots, each has more than one Maya scene files!). If this was the case, why did we need the GUI? How about invoking Maya as standalone mode and in that way, we all output messages were naturally redirected to console!
“Some scripts/nodes need GUI’s redrawing to make it re-calculate values, such that we need Maya in GUI mode.”, replied by Timothy. Well, IMHO, there should be some way to do that Dependency node’s evaluation in standalone mode but we didn’t know that yet. Actually, there is a likely hot topic ”forcing a custom node’s attribute to compute every dep graph update” in Python Inside Maya group but let’s just skip it and figure out some hacking way to redirect all messages to console/terminal.
- Maya is invoked from a terminal/console as a GUI mode. All Maya messages have to be correctly redirected to the console.
- There are 3 types of messages: 1) msgs by Maya itself, 2) msgs from Mel’s print and 3) msgs from Python’s print.
- We can make use of sys.stdout, the default standard output (terminal) to redirect Python’s print.
- For Mel’s print and Maya’s command messages, we need to use CommandMessage.CommandOutputCallback in Maya API. Just register a CommandOutput callback and everytime there is a message, this callback would be invoked.
For Python’s print:
import sys sys.stdout = sys.__stdout__ dir(sys.stdout) print "ooxx"
For Mel’s print and Maya command output:
def callback(nativeMsg, messageType, data): print nativeMsg, messageType, data id = mo.MCommandMessage.addCommandOutputCallback(callback, None)
Testing of MEL:
print "ooxx from mel.print";
Testing of Python:
print "ooxx from python"
Testing of creating a sphere by GUI
Output in terminal/console:
ooxx print "ooxx from mel.print"; 0 None ooxx from mel.print 1 None print "ooxx from python" sphere() 0 None ooxx from python name 'sphere' is not defined # Traceback (most recent call last): # File "", line 2, in ? # NameError: name 'sphere' is not defined 4 None polySphere -r 1 -sx 20 -sy 20 -ax 0 1 0 -cuv 2 -ch 1; 1 None pSphere1 polySphere1 5 None ; 1 None saveShelf Custom "/home/drake/maya/8.5-x64/prefs/shelves/shelf_Custom"; 0 None 1 5 None saveShelf Toon "/home/drake/maya/8.5-x64/prefs/shelves/shelf_Toon"; 0 None 1 5 None saveShelf DGTOOL "/home/drake/maya/8.5-x64/prefs/shelves/shelf_DGTOOL"; 0 None 1 5 None saveShelf FL "/home/drake/maya/8.5-x64/prefs/shelves/shelf_FL"; 0 None 1 5 None saveShelf LightingTools "/home/drake/maya/8.5-x64/prefs/shelves/shelf_LightingTools"; 0 None 1 5 None saveShelf RL "/home/drake/maya/8.5-x64/prefs/shelves/shelf_RL"; 0 None 1 5 None saveShelf ShaderConverter "/home/drake/maya/8.5-x64/prefs/shelves/shelf_ShaderConverter"; 0 None 1 5 None saveShelf StereoTools "/home/drake/maya/8.5-x64/prefs/shelves/shelf_StereoTools"; 0 None 1 5 None Saving preferences to : /home/drake/maya/8.5-x64/prefs/userPrefs.mel 2 None Saving window positions to : /home/drake/maya/8.5-x64/prefs/windowPrefs.mel 2 None Saving hotkeys to : /home/drake/maya/8.5-x64/prefs/userHotkeys.mel 2 None Saving named commands to : /home/drake/maya/8.5-x64/prefs/userNamedCommands.mel 2 None Saving plug-in preferences to: /home/drake/maya/8.5-x64/prefs/pluginPrefs.mel 2 None Preferences saved. See Script Editor for details. 2 None