Skip to content

Instantly share code, notes, and snippets.

@minimalefforttech
Created November 18, 2024 09:05
Show Gist options
  • Save minimalefforttech/3cbdf57af549360aa4298cf54dbc33b2 to your computer and use it in GitHub Desktop.
Save minimalefforttech/3cbdf57af549360aa4298cf54dbc33b2 to your computer and use it in GitHub Desktop.
using Qt to pause a running pipeline for visual debugging
from PySide2 import QtWidgets, QtCore # or PySide6 for Maya 2025+
import maya.cmds as mc
import maya.mel as mm
def my_super_long_function():
print("Running a long function here")
# Do some initial prep
cube, _ = mc.polyCube()
sphere, _ = mc.polySphere()
cone, _ = mc.polyCone()
mc.move(-0.505272, -0.0342346, 0.315813, cube, r=1)
mc.move(-1.057024, 2.94418, -0.836557, sphere, r=1)
mc.move(4.337798, -1.232447, -1.953501, cone, r=1)
# Pause the execution
dialog = QtWidgets.QMessageBox(
QtWidgets.QMessageBox.Warning,
"Execution Paused",
"Click OK to continue")
# Make sure the dialog doesn't block interaction but stays on top.
dialog.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
dialog.setModal(False)
dialog.show()
print("Function is paused, have a look around")
QtWidgets.QApplication.processEvents()
loop = QtCore.QEventLoop()
dialog.finished.connect(loop.quit)
# The event loop waits until quit is called,
# In the meantime, Qt will continue to run the mainevent loop
loop.exec_()
print("Function is now continuing")
# finish it off
mc.select([cube, sphere, cone])
geometry, _ = mc.polyUnite()
mm.eval("DeleteHistory")
mc.rename(geometry, "geometry")
my_super_long_function()
from PySide2 import QtWidgets, QtCore
def my_super_long_function():
print("Running a long function here")
# Pause the execution
dialog = QtWidgets.QMessageBox(
QtWidgets.QMessageBox.Warning,
"Execution Paused",
"Click OK to continue")
# Make sure the dialog doesn't block interaction but stays on top.
dialog.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
dialog.setModal(False)
dialog.show()
print("Function is paused, have a look around")
loop = QtCore.QEventLoop()
dialog.finished.connect(loop.quit)
# The event loop waits until quit is called,
# In the meantime, Qt will continue to run the mainevent loop
loop.exec_()
print("Function is now continuing")
my_super_long_function()
@mottosso
Copy link

mottosso commented Dec 5, 2024

Genius. :)

Minor tweak/additions:

  • A separate pause function to put in a shared module
  • Automatic refresh of the UI since it appears one would otherwise have to interact with Maya to make it run the previous commands.
  • Pass title to UI to help user understand where in the code they are
from PySide2 import QtWidgets, QtCore  # or PySide6 for Maya 2025+
import maya.cmds as mc
import maya.mel as mm

def pause(title):
    dialog = QtWidgets.QMessageBox(
        QtWidgets.QMessageBox.Warning,
        title,
        title)

    dialog.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
    dialog.setModal(False)
    dialog.show()
    
    # Let Maya refresh, otherwise you'll need to
    # manually interact with it to get it to perform
    # the prior steps
    QtCore.QTimer.singleShot(10, cmds.refresh)

    QtWidgets.QApplication.processEvents()
    loop = QtCore.QEventLoop()
    dialog.finished.connect(loop.quit)
    loop.exec_()


def my_super_long_function():
    print("Running a long function here")
    # Do some initial prep
    cube, _ = mc.polyCube()
    sphere, _ = mc.polySphere()
    cone, _ = mc.polyCone()
    mc.move(-0.505272, -0.0342346, 0.315813, cube, r=1)
    mc.move(-1.057024, 2.94418, -0.836557, sphere, r=1)
    mc.move(4.337798, -1.232447, -1.953501, cone, r=1)

    pause("Before merging geometry")

    mc.select([cube, sphere, cone])
    geometry, _ = mc.polyUnite()
    mm.eval("DeleteHistory")

    pause("Before renaming")

    mc.rename(geometry, "geometry")

my_super_long_function()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment