一半君的总结纸

听话只听一半君

#123 如何从undo queue里得到命令历史?

想得到执行命令的历史,但是undoInfo -q -undoName只能返回上一条命令的历史

查看undoInfo命令的帮助,有printQueue参数,但是用他查询的时候没有返回值,只是显示在script editor窗口里,此时可以使用如下方法

from contextlib import contextmanager
import re
import maya.OpenMaya as om

def catchOutput(msg, *args):
    f.write("Caught: %s\n" % msg)

@contextmanager
def output_redirector():
    catchOutput_id = om.MCommandMessage.addCommandOutputCallback(catchOutput)
    try:
        yield
    finally:
        om.MCommandMessage.removeCallback(catchOutput_id)

import io
f=io.StringIO()

def get_undo_queue():
   
    with output_redirector():
        mc.undoInfo(q=1,pq=1)
    
    undo_cmds=[]    
    for line in f.getvalue().split('\n'):
        if len(line.strip(' ')):
            id,code = re.search('(?<=Caught:\s)(\d*)\:\s(.*)',line).groups()
            print id,'-->',code
            undo_cmds.append( (id,code))
        
    return undo_cmds

上面假设每条历史只有1行,当然实际中不会如此,使用cmd_list=get_undo_queue()后

# 0: miCreateDefaultPresets # 
# 1: CreatePolygonCube # 
# 2: select -r pCube1  # 
# 3: move -r 0 0 5.710821  # 
# 4: move -r 3.047437 0 0  # 
0 --> miCreateDefaultPresets
1 --> CreatePolygonCube
2 --> select -r pCube1 
3 --> move -r 0 0 5.710821 
4 --> move -r 3.047437 0 0 

通过拿到之前所有历史命令,可以满足一些奇怪的需求,比如想隐藏/显示 最后一次操作的displayLayer,此时可以通过找到最后一次执行的layerEditorLayerButtonVisibilityChange,看他操作的是哪个layer,然后把隐藏layer的命令连到一个快捷键的press,把显示layer的命令连到同一个快捷键的release上,最后效果是按下此键,比如;就会隐藏某个层,松手就会显示这个层,也许建模的时候会用到,lz的用法是在hair sim跑完以后,调节曲线的时候用(调曲线选点的时候隐藏所有头发曲线,松开快捷键后显示所有头发,此时用移动工具调点)

参考:
Redirecting all kinds of stdout in Python
How to make Maya output all messages into console/terminal?
Extracting data from Output Window

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: