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

2024-04-19 11:39:30 发布

您现在位置: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中所做的工作相比,我不知道这段代码有什么独特之处,但很明显,这对我来说是有意义的

任何援助都是受欢迎的,因为现在我完全不知所措。看起来,单击工具架上的任何按钮都会使其遍历该工具架上的所有按钮

谢谢


Tags: 图像脚本ifmodebuttontool按钮ctx
1条回答
网友
1楼 · 发布于 2024-04-19 11:39:30

我无法与您的Toggler()类交谈,因为我对它的用途感到非常困惑,但下面的脚本片段在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等效命令也没有设置此标志

相关问题 更多 >