Oafkad
Oafkad

Reputation: 3

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

I'm trying to create a script in Python in Maya that will allow me to dynamically alter the image of a specific button and nothing else about that button. I'm crashing into some serious issues that I'll detail below:

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)  

Firstly this is the script. What the button should do is defined at the bottom and as best as I can tell that is all fine. It was already existing code that I was handed.

My problems are as follows:

  1. The image changes for all buttons on the bar. This is incredibly unhelpful and I'm not sure why this would be the case.
  2. The names of all the buttons change to whatever buttonName is. So in this case, all buttons are renamed to "Button 1" which is also incredibly frustrating for me.
  3. The script on the original button is cloned to all other buttons.

An addendum to 2 is I've tried renaming my buttonName variable on the off chance that buttonName is an intrinsic variable assigned to these button scripts.

In the past I was able to accomplish editing just the image of a button with the following MEL code:

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

I cannot figure out what is unique about this code compared to what I've done in Python but clearly there is something going on for me.

Any aid is welcome because at this point I'm completely at a loss. It looks like clicking any button on a shelf will cause it to iterate through all buttons on that shelf.

Thanks!

Upvotes: 0

Views: 1407

Answers (1)

Daniel Skovli
Daniel Skovli

Reputation: 495

I can't speak to your Toggler() class, because I'm quite confused by its purpose, but the below script snippet is fully functional in 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

I'm not using the width, height and parent flags you were using. Other than that, everything is more or less the same as your own code. Perhaps editing the parent isn't working as expected? Your mel equivalent command does not set this flag either.

Upvotes: 1

Related Questions