[Pymel] Capture Command History

Following up on this thread: http://tech-artists.org/forum/showthread.php?3675-PyMel-3ds-Max-callbacks-like-functionality-in-Maya

I want to be able to trigger a script whenever the most recent line in the command history is, say, “polyEditUVs”. How can I get the command history from Maya without doing something like this?


import pymel.core as pm

fileLoc = "C:\\mayaCommandHistory.log"
pm.scriptEditorInfo(historyFilename=fileLoc, writeHistory=True)

fileHandle = open(fileLoc, 'r')
fileLines = fileHandle.readlines()
print fileLines[-1]


you can look at the undo stack, although it doesn’t quite give the info you are looking for:

cmds.undoInfo(q=True, printQueue=True)

// 0: selectionMaskResetAll //
// 1: selectionMaskResetAll //
// 2: CreatePolygonCube //
// 3: InteractiveSplitTool //
// 4: CompleteCurrentTool //

That doesn’t return the undo queue, just puts it to the script editor. It’ll be helpful to make sure I get the right command in case the user turned echoing off, though.

would it perhaps be possible to create a duplicate of the script (look in your install loc) into your documents/maya/scripts folder with an amendment at the end calling the command you want?

My current method of distribution precludes me from being able to do that.

listHistory should do the job. http://www.luma-pictures.com/tools/pymel/docs/1.0/generated/functions/pymel.core.general/pymel.core.general.listHistory.html#pymel.core.general.listHistory


selection = pm.ls(sl=True)
history= pm.listHistory(selection, lv=1)

Unless Im misreading the question. Level 1 will return the top most history node. O wait is this command history as in modeling history or script editor command history?

Script Editor Command History is what I’m looking for, although the method you’re suggesting might work for what I need.

Script Editor Command History is what I’m looking for, although the method you’re suggesting might work for what I need.

I’d take a look ate the cmdScrollFieldReporter or the cmdFileOutput calls. The first one gives you access to the ScriptReporter.