如何在“曲线图编辑器”窗口中为新用户创建的按钮添加父项?

时间:2015-11-25 20:40:59

标签: python window maya mel

我想在Maya Graph编辑器中添加一些新按钮 - 特别是在Channel列表的顶部,窗口左侧有所有属性。但是,我宁愿不使用Graph Editor本身的Maya Startup脚本。有没有办法在每个新的Graph Editor窗口中使用单独的脚本“父”我想要的新按钮?

理想情况下,这可能是所有Python。

2 个答案:

答案 0 :(得分:2)

<强> 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)。

  

此命令采用一个字符串类型参数并返回一个字符串   指示参数是否是内置的“命令”,“梅尔   过程“,”脚本“或变量。如果是变量,则为类型   变量也给出了。如果参数是Mel程序或者   脚本文件,包含脚本或过程的文件的路径   包含在返回值中。

此组合将允许您跟踪图形编辑器的创建方式和位置。现在我们来做吧。

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

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

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

2:运行whatIs "tearOffPanel";(mel)

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

通过对此文件的一些调查,您可以推断出可以使用scriptedPanel命令创建一个全新的图形编辑器。

3:创建自己的图表面板

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

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:尝试了解图表编辑器的构建方式

此脚本将打印出Graph Editor的小部件层次结构(首先创建自定义Graph Editor):

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给出的默认名称。因此,我们无法使用完整路径添加小部件和父级,因为每次创建新的Graph Editor时此路径都会更改。

我们将依赖的项目是GE_ui_scriptedPanelOutlineEdFormformLayout包含其他formLayoutpaneLayout

|___|___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)])

答案 1 :(得分:1)

为此,您可能必须使用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的sonme实验:

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指针,然后遍历子项以找到你可能想要的布局。

希望有所帮助