我如何在图表编辑器 window 中设置新的、用户创建的按钮的父级?

How do I parent new, user-created buttons inside the Graph Editor window?

我想向 Maya Graph 编辑器添加一些新按钮 -- 特别是在频道列表的顶部,所有属性都在 window 的左侧。但是,我不想为图表编辑器本身使用 Maya 启动脚本。有没有办法 "parent" 使用单独的脚本在每个新图表编辑器 window 中添加我想要的新按钮?

理想情况下,这可能是全部 Python。

为此,您可能不得不选择 PySide/PyQt。找到图形编辑器的指针,找到元素的布局方式。

这是一个关于更改 Maya 菜单样式表的示例:

from maya.OpenMayaUI import MQtUtil as omui
import sip
from PyQt4 import QtGui

def changeMayaMenuColors(fontStyle='italic', fontWeight='bold', fontColor='cyan'):
    # Get the widget
    widgetStr = mel.eval( 'string $tempString = $gMainCreateMenu' )
    ptr = omui.findControl( widgetStr )
    widget = sip.wrapinstance(long(ptr), QtGui.QWidget)
    widget.setStyleSheet('font-style:%s;'%fontStyle +'font-weight:%s;'%fontWeight + 'color:%s;'%fontColor)

这里是关于 channelBox 的实验:

from PySide import QtGui, QtCore
from shiboken import wrapInstance
from maya.OpenMayaUI import MQtUtil


channelbox = wrapInstance(long(MQtUtil.findControl('mainChannelBox')), QtGui.QWidget)
channelbox_children = channelbox.children()

first_widget = channelbox_children[0] # EDIT
first_widget.hide()
#first_widget.show()
#---------------------------------------------------------------------------------------------------------------
mySubWdget = first_widget.children()

new_button = QtGui.QPushButton()
new_layout = QtGui.QVBoxLayout()
first_widget.setLayout(new_layout)
new_layout.addWidget(new_button)

def print_hodor():
    print 'HODOR'

new_button.clicked.connect(print_hodor)

您可以对所有 Maya 小部件进行这种实验(找到指针,然后使用 wrapInstance 获取 QT 指针,然后遍历子项以找到您可能想要的布局)

希望对您有所帮助

TL;DR;

if cmds.window("GE_ui_window", exists=True): #If the window exists
    cmds.deleteUI("GE_ui_window") #Delete it
cmds.window("GE_ui_window", title="My custom Graph Editor") #Create your custom win

cmds.frameLayout("GE_ui_frameLayout", p="GE_ui_window", lv=False, bv=False ) 

if cmds.scriptedPanel("GE_ui_scriptedPanel", exists=True): #If the scriptel panel already exists
    cmds.deleteUI("GE_ui_scriptedPanel") #Delete it
cmds.scriptedPanel("GE_ui_scriptedPanel", unParent=True, type="graphEditor")
cmds.scriptedPanel( "GE_ui_scriptedPanel", e=True, parent="GE_ui_window|GE_ui_frameLayout") #parent the scripted panel to your frame layout

cmds.showWindow("GE_ui_window")

channelLayout = cmds.formLayout("GE_ui_scriptedPanelOutlineEdForm", query=True, ca=True)[0] #Get the channel box's layout
filterLayout = cmds.formLayout("GE_ui_scriptedPanelOutlineEdForm", query=True, ca=True)[1] #Get the filter's layout

myRowLayout=cmds.rowLayout(numberOfColumns=3, p="GE_ui_scriptedPanelOutlineEdForm") #Create a row layout 
cmds.button(label="Café", h=100, p=myRowLayout) #Add some buttons
cmds.button(label="Clope", p=myRowLayout)
cmds.button(label="Caca", p=myRowLayout)

#This will reorder the content of the left formLayout
cmds.formLayout("GE_ui_scriptedPanelOutlineEdForm", edit=True, af=[ \
            (channelLayout, "bottom", 0), \
            (channelLayout, "right", 0),  \
            (filterLayout, "top", 0),  \
            (myRowLayout, "left", 0),  \
            (myRowLayout, "right", 0)],  \
            ac=[(myRowLayout, "top", 0, filterLayout), \
            (channelLayout, "top", 0, myRowLayout)])

在编辑 Maya 的 UI 时,有两件非常有用的事情需要知道。

首先是脚本编辑器中的 History -> Echo all commands 复选框。这可以打印出很多垃圾,其中包含有用的信息。

第二件事是 whatIs 命令 (Doc)。

This command takes one string type argument and returns a string indicating whether the argument is a builtin "Command", a "Mel procedure", a "Script", or a variable. If it is a variable, the type of the variable is also given. If the argument is a Mel procedure or a script file, the path to the file containing the script or procedure is included in the return value.

此组合将使您能够追踪图表编辑器的创建方式和位置。现在开始吧。

1:打开图形编辑器Window -> Animation Editors -> Graph Editor

GraphEditor;
tearOffPanel "Graph Editor" "graphEditor" true;
// Result: graphEditor1Window // 

GraphEditor; 是一个 运行 时间命令,它在调用时执行 tearOffPanel "Graph Editor" "graphEditor" true;。这就是它出现在脚本编辑器中的原因。

2: 运行 whatIs "tearOffPanel"; (梅尔)

// Result: Mel procedure found in: C:/Program Files/Autodesk/Maya2014/scripts/startup/tearOffPanel.mel // 

稍微调查一下这个文件,您可以推断出您可以使用 scriptedPanel 命令创建一个全新的图形编辑器。

3:创建自己的图表面板

scriptedPanel doc 向您展示了如何创建脚本面板并将其包含在 window 中。 您现在可以使用此创建自定义图形编辑器:

if cmds.window("GE_ui_window", exists=True):
    cmds.deleteUI("GE_ui_window")
cmds.window("GE_ui_window", title="My custom Graph Editor")

cmds.frameLayout("GE_ui_frameLayout", p="GE_ui_window", lv=False, bv=False )

if cmds.scriptedPanel("GE_ui_scriptedPanel", exists=True):
    cmds.deleteUI("GE_ui_scriptedPanel")
cmds.scriptedPanel("GE_ui_scriptedPanel", unParent=True, type="graphEditor", label='Sample')
cmds.scriptedPanel( "GE_ui_scriptedPanel", e=True, parent="GE_ui_window|GE_ui_frameLayout")

cmds.showWindow("GE_ui_window")

4:尝试了解图表编辑器的构建方式

此脚本将打印出图表编辑器的小部件层次结构(首先创建您的自定义图表编辑器):

nbIteration = 0
def getChildren(uiItem, nbIteration):
    for childItem in cmds.layout(uiItem, query=True, childArray=True):
        try:
            print "|___"*nbIteration + childItem
            getChildren(uiItem + "|" + childItem, nbIteration+1)
        except:
            pass
getChildren("GE_ui_window|GE_ui_frameLayout|GE_ui_scriptedPanel", nbIteration)

此外,您可以查看 C:\Program Files\Autodesk\Maya2014\scripts\others\graphEditorPanel.mel@939: global proc addGraphEditor (string $whichPanel)

您现在可以意识到,很多小部件没有任何名称,只有 Maya 给出的默认名称。因此,我们无法添加小部件并使用完整路径作为它们的父级,因为每次创建新的图形编辑器时此路径都会更改。

我们将尝试依赖的项目是 GE_ui_scriptedPanelOutlineEdForm,它是一个 formLayout,其中包含另一个 formLayout 和一个 paneLayout

|___|___GE_ui_scriptedPanelOutlineEdForm 
|___|___|___paneLayout123 #layout containing the two channel boxes
|___|___|___|___GE_ui_scriptedPanelOutlineEd
|___|___|___|___GE_ui_scriptedPanelOutlineEdSlave
|___|___|___formLayout276 #Layout containing the "filter part"
|___|___|___|___textField63 #It's text
|___|___|___|___iconTextButton102

5:创建按钮并对 GE_ui_scriptedPanelOutlineEdForm

的内容重新排序
channelLayout = cmds.formLayout("GE_ui_scriptedPanelOutlineEdForm", query=True, ca=True)[0] #Get the channel box's layout
filterLayout = cmds.formLayout("GE_ui_scriptedPanelOutlineEdForm", query=True, ca=True)[1] #Get the filter's layout

myRowLayout=cmds.rowLayout(numberOfColumns=3, p="GE_ui_scriptedPanelOutlineEdForm") #Create a row layout 
cmds.button(label="Café", h=100, p=myRowLayout) #Add some buttons
cmds.button(label="Clope", p=myRowLayout)
cmds.button(label="Caca", p=myRowLayout)

#This will reorder the content of the left formLayout
cmds.formLayout("GE_ui_scriptedPanelOutlineEdForm", edit=True, af=[ \
            (channelLayout, "bottom", 0), \
            (channelLayout, "right", 0),  \
            (filterLayout, "top", 0),  \
            (myRowLayout, "left", 0),  \
            (myRowLayout, "right", 0)],  \
            ac=[(myRowLayout, "top", 0, filterLayout), \
            (channelLayout, "top", 0, myRowLayout)])