Maya Python UI 在属性编辑器中显示

0 投票
1 回答
1084 浏览
提问于 2025-04-18 14:59

这是我第一次来这里,我一直在为Python编程而苦恼,特别是想弄明白如何根据动作或鼠标事件来更新东西。

最近,每当我尝试测试我的脚本时,我常常会看到一些按钮和布局面板出现在属性编辑器里,而它们本来应该在我创建的窗口里。怎么才能让这种情况不再发生呢?

我觉得我不能在这里贴出代码,因为代码大约有1000行,但我该如何找到一种方法来防止这种情况发生呢?是不是因为我用了太多setParent('..')这个函数?

1 个回答

1

如果你的按钮等控件显示的位置不对,可能是因为你在某个函数执行后,重新设置了它们的父级。

如果你想确保你的控件放在正确的位置,你需要记住你创建的任何窗口、布局或面板的名字,并在开始创建控件之前,明确地把它们设置为父级。否则,父级就会变成“最后创建的那个”。你可以通过下面的方式来验证这一点:

# make a button out of context
import maya.cmds as cmds
xxx = cmds.button('boo')

# ask the parent of what we just made....
print cmds.control(xxx, q=True, p=True)

## Result: u'MayaWindow|MainAttributeEditorLayout|formLayout2|AEmenuBarLayout|AErootLayout|AEselectAndCloseButtonLayout' # 

如果你创建了一个顶层容器(比如窗口或面板),父级就会被切换:

w = cmds.window()
c = cmds.columnLayout() 
b = cmds.button("bar")

# ask b's parent....
print cmds.control(b, q=True, p=True)
## Result: window3|columnLayout49  #

你也可以明确地切换父级:

def make_a_layout(window_name):
    w = cmds.window(window_name)
    c = cmds.columnLayout()
    return c

layout_a = make_a_layout('window_a')
# any future widgets go into this layout...
print cmds.button("layout 1 a") 
#     window_a|columnLayout55|layout_1_a

layout_b = make_a_layout('window_b')
# now this is the active layout
print cmds.button("layout 2 a ")  
#     window_b|columnLayout56|layout_2_a

# explicitly set the parent to the first layout
# now new widgets will be there
cmds.setParent(layout_a)
print cmds.button("layout 1 b")
#  window_a|columnLayout56|layout_1_b

如你所见,每次创建新布局时,当前的父级都会被设置。你可以通过 setParent ('..') 来上升一个层级,或者通过 setParent('your_layout_here') 明确设置为任何布局。

撰写回答