Skip to content

Instantly share code, notes, and snippets.

@Lauloque
Created January 19, 2023 19:52
Show Gist options
  • Select an option

  • Save Lauloque/6b11a11cff34fed20644d213e64fc8e3 to your computer and use it in GitHub Desktop.

Select an option

Save Lauloque/6b11a11cff34fed20644d213e64fc8e3 to your computer and use it in GitHub Desktop.
This example script shows how to edit a shelf button. It cycles through the buttons of the current shown shelf, finds one by its name, and changes its icon label. I use it on a shelf button so that it edits itself on click.
import maya.cmds as cmds
import maya.mel as mel
import maya.api.OpenMaya as om
def buttonSwitch():
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 = 'ButtonName' # name, not icon label
for b in buttons:
label = cmds.shelfButton(b, q=True, l=True)
if label != targetButton:
continue
print('Found target button: `{}` -> {}'.format(targetButton, b))
currentLabel = cmds.shelfButton(b, q=True, imageOverlayLabel=True)
newLabel = "New Name"
print('Icon label: `{}` -> `{}`'.format(currentLabel, newLabel))
cmds.shelfButton(b, e=True, imageOverlayLabel=newLabel)
om.MGlobal.displayInfo("Changed icon name to "+newLabel)
buttonSwitch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment