Maya Python UI Button to Toggle Group Visibility
Create a Qt button in Maya that toggles a transform group's visibility using the Python API, with a minimal code example and verification steps.
28 May 2026, 07:20 UTC

Quick Answer
\nYou can add a button that toggles the visibility of a Maya group using the Python API; the example creates a dialog with a Toggle button that switches the myGroup.visibility attribute between 0 (hidden) and 1 (visible).
How it works
\nMaya's Python API exposes UI commands via maya.cmds and Qt classes via maya.ui. The callback reads the current visibility attribute, flips it, and updates the scene. All UI construction must occur on Maya's main UI thread to avoid crashes.
Worked configuration
\nimport maya.cmds as cmds
from maya import OpenMayaUI
from PySide2 import QtWidgets
import maya.utils
\ndef toggle_grp_visibility():
visible = cmds.getAttr('myGroup.visibility') == 0
cmds.setAttr('myGroup.visibility', 1 if visible else 0)\n\ndef create_tool():
def build():
win = QtWidgets.QDialog()
win.setWindowTitle('MyGroup Toggle')
btn = QtWidgets.QPushButton('Toggle MyGroup')
btn.clicked.connect(toggle_grp_visibility)
btn.clicked.connect(win.accept)
win.show()
maya.utils.executeInMainThread(build)\ncreate_tool()\n\nVerification steps
\n- \n
- Run the script in Maya's Script Editor; a new window should appear titled MyGroup Toggle. \n
- Click the button; the Outliner eye icon for
myGroupshould toggle. \n - Run the script again; no duplicate window should appear, confirming the function wraps UI creation. \n
- Check the Script Editor for errors; none indicate the UI thread was respected. \n
Limits and performance considerations
\nThe API runs on Maya's UI thread, so long-running Python loops will freeze the interface. For heavy work, use background threads and schedule UI updates with maya.utils.executeInMainThread. Rapid visibility toggling on large groups may impact performance; consider hardware-accelerated display settings for frequent changes.
Common mistakes
\n- \n
- Defining UI code outside a function, causing duplicate windows on repeated runs. \n
- Relying on global variables that persist across sessions, leading to unexpected state. \n
- Failing to clean up dialogs, which can cause memory leaks. \n
- Creating Qt widgets in a background thread; this can crash Maya because UI objects are not thread-safe. \n
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.