如何在 Maya 中使用 Python 使 select 命令在按钮上起作用?

How to make select command work on button using Python in Maya?

我是 Python 编程新手,需要 Maya 方面的帮助。

所以我想创建一个 UI 和一个 select 在我的 Maya 场景中是一个名为 "big" 的对象的按钮,但我无法让它工作。如何将 select 命令添加到我的按钮 ball_btn

我尝试将 cmds.select("ball") 插入按钮,但没有成功。

谢谢!

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

Maya 文档已经为您提供了一个很好的示例,说明如何连接 button to a function

一旦你的按钮在被点击时触发了一个功能,你可以做一个简单的检查对象是否存在于场景中然后select它:

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 用于您自己的自定义函数。