如何在运行时使用 Python 在 Maya 中编辑特定的工具架按钮?

How do I edit specific shelf buttons at runtime in Maya with Python?

我正在尝试在 Maya 中的 Python 中创建一个脚本,该脚本允许我动态更改特定按钮的图像 而关于该按钮的其他内容 .我遇到了一些严重的问题,我将在下面详细说明:

import maya.cmds as cmds
import maya.mel as mel

cmds.refresh(cv=1, f=1)

gShelfTopLevel = mel.eval("global string $gShelfTopLevel; $temp = $gShelfTopLevel;")
currentShelf = cmds.tabLayout(gShelfTopLevel,q=1,st=1)
buttons = cmds.shelfLayout(currentShelf,q=1,ca=1)
buttonName = "Button 1"

for button in buttons:  
    if cmds.shelfButton(button, q=True, l=True) == buttonName:
        cmds.shelfButton(button, h=35, w=35, e=1, i="icons/axis_Object.png", p=currentShelf )
        #If this was working I'd have an if statement here for a second image.
        break

Toggler()

class Toggler():

    if ctx == 'moveSuperContext':

        tool = 'Move'
        mode = cmds.manipMoveContext(tool, q=1, m=1)

        if mode != 2:
            cmds.manipMoveContext(tool, e=1, m=2)
        else:
            cmds.manipMoveContext(tool, e=1, m=0)

    if ctx == 'RotateSuperContext':

        tool = 'Rotate'
        mode = cmds.manipRotateContext(tool, q=1, m=1)

        if mode != 0:
            cmds.manipRotateContext(tool, e=1, m=0)
        else:
            cmds.manipRotateContext(tool, e=1, m=1)  

    if ctx == 'scaleSuperContext':

        tool = 'Scale'
        mode = cmds.manipScaleContext(tool, q=1, m=1)

        if mode != 0:
            cmds.manipScaleContext(tool, e=1, m=0)
        else:
            cmds.manipScaleContext(tool, e=1, m=2)  

首先这是脚本。按钮应该做什么在底部定义,尽我所能告诉它一切都很好。我收到的代码已经存在。

我的问题如下:

  1. 栏上所有按钮的图像都会发生变化。这是非常无益的,我不确定为什么会这样。
  2. 所有按钮的名称都更改为任何 buttonName。所以在这种情况下,所有按钮都重命名为 "Button 1",这对我来说也非常令人沮丧。
  3. 原始按钮上的脚本被克隆到所有其他按钮。

2 的附录是我尝试重命名我的 buttonName 变量,因为 buttonName 是分配给这些按钮脚本的内部变量。

过去我只能使用以下 MEL 代码编辑按钮的图像:

shelfButton -edit -image "icons/axis_World.png" $button;

与我在 Python 中所做的相比,我无法弄清楚这段代码有什么独特之处,但显然我正在做一些事情。

欢迎任何帮助,因为此时我完全不知所措。看起来点击架子上的任何按钮都会导致它循环访问该架子上的所有按钮。

谢谢!

我无法与您的 Toggler() class 交谈,因为我对其用途感到很困惑,但下面的脚本片段在 Maya 2018.4 中完全可用:

import maya.cmds as cmds
import maya.mel as mel

gShelfTopLevel = mel.eval("global string $gShelfTopLevel; $temp = $gShelfTopLevel;")
currentShelf = cmds.tabLayout(gShelfTopLevel, q=True, st=True)
buttons = cmds.shelfLayout(currentShelf, q=True, ca=True)
targetButton = 'Button 1' # Button 'name' not 'icon label'
toggleIcons = ['showManip.png', 'globalManip.png']

for b in buttons:
    label = cmds.shelfButton(b, q=True, l=True)

    if label != targetButton:
        continue

    print('Found target button: `{}` -> {}'.format(targetButton, b))

    currentIcon = cmds.shelfButton(b, q=True, i=True)
    newIcon = toggleIcons[0] # default
    if currentIcon in toggleIcons:
        try:
            idx = toggleIcons.index(currentIcon) + 1
            newIcon = toggleIcons[idx] if idx < len(toggleIcons) else toggleIcons[0]
        except Exception as e:
            print('Failed to iterate through list of icons, using default: {}'.format(e))

    print('Current image is {} -> swapping to {}'.format(currentIcon, newIcon))
    cmds.shelfButton(b, e=True, i=newIcon)
    break

我没有使用您使用的 widthheightparent 标志。除此之外,一切都或多或少与您自己的代码相同。也许编辑 parent 没有按预期工作?您的 mel 等效命令也没有设置此标志。