如何使用Maya中的Python在按钮上进行选择命令工作



我是Python编程的新手,需要在Maya中提供帮助。

因此,我正在尝试使用一个按钮创建一个UI,该按钮在Maya场景中选择一个名为"big"的对象,但我无法使其工作。如何将SELECT命令添加到我的按钮ball_btn

我试图将cmds.select("ball")插入按钮,但没有运气。

谢谢!

ball_btn = mc.button(label = “”, w = 50, h = 30, bgc = [1.000,0.594,0.064])

玛雅文档已经为您提供了一个很好的示例

单击时按钮会触发功能后,您可以进行简单检查对象是否存在场景,然后选择它:

import maya.cmds as cmds

# Define a function that the button will call when clicked.
def select_obj(*args):
  if cmds.objExists("ball"):  # Check if there's an object in the scene called ball.
      cmds.select("ball")  # If it does exist, then select it.
  else:
      cmds.error("Unable to find ball in the scene!")  # Otherwise display an error that it's missing.

# Create simple interface.
win = cmds.window(width=150)
cmds.columnLayout(adjustableColumn=True)
cmds.button(label="Select", command=select_obj)  # Use command parameter to connect it to the earlier function.
cmds.showWindow(win)

您也可以使用lambda直接将按钮命令连接到cmds.select

import maya.cmds as cmds

# Create simple interface.
win = cmds.window(width=150)
cmds.columnLayout(adjustableColumn=True)
cmds.button(label="Select", command=lambda x: cmds.select("ball"))  # Use lambda to connect directly to the select method.
cmds.showWindow(win)

但是,您将对它处理错误的方式进行零自定义,或者您是否希望它做其他事情。通常,除非有充分的理由不这样做,否则请坚持触发功能的按钮。请记住,您也可以将lambda用于自己的自定义功能。

最新更新