如何使用 PySide 在 Maya 中将新按钮作为通道盒的父级?

How can I parent a new button to the channel box in Maya using PySide?

我对 PySide 有非常基本的了解。我希望为现有的 Channel Box 添加一个按钮。但是,除了获得主要的 Maya window 之外,我不确定从哪里开始。 (但我什至不确定这是正确的):

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

channelBox = wrapInstance(long(MQtUtil.findControl('mainChannelBox')), QtGui.QWidget)

搞乱 Maya 的 UI 可能是一项艰巨的任务,有两种方法可以做到这一点。 首先是使用 maya.cmds 将小部件添加到 Maya 的 UI。第二个是像您一样在 Qt class.

中包装一个 Maya 小部件

这是一个类似的问题: 我只用 maya.cmds 代码回答,还有一个使用 PySide 的答案可能会让您感兴趣。

这是一个解决方案:

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("MayaWindow|MainChannelsLayersLayout", nbIteration)

如果您 运行 此代码,这将为您提供 Channel Box / Layer Editor

中包含的小部件的名称
ChannelButtonForm <-- This is the form containing the 3 buttons on the top-right 
|___cbManipsButton
|___cbSpeedButton
|___cbHyperbolicButton
ChannelsLayersPaneLayout <-- This is the layout containing the channel box and the layer editor. A paneLayout has a splitter to resize his childrens.
|___ChannelBoxForm 
|___|___menuBarLayout1 
|___|___|___frameLayout1
|___|___|___|___mainChannelBox
|___LayerEditorForm
|___|___DisplayLayerUITabLayout
|___|___|___DisplayLayerTab
|___|___|___|___formLayout3

根据您希望按钮对齐的位置,您必须选择一个布局作为按钮的父布局。

在这种情况下,我将在频道 Box/Layer 编辑器的左上角放置一个按钮,与 3 个复选框按钮处于同一级别。

import maya.cmds as cmds

cmds.button("DrHaze_NewButton", l="HELLO", p="MayaWindow|MainChannelsLayersLayout|ChannelButtonForm")

由于您没有告诉我们您希望将按钮放置在何处,如果您想要更合适的内容,则必须编辑您的问题。