Replace 'Playblast' button with 'Tinyblast' button

Learned some Qt and didn't like how I handled the 'Tinyblast' button. Now instead of a big ole 'Tinyblast' button underneath the other 'Playblast' buttons, Tinyblast overrides the default 'Playblast' button.
This commit is contained in:
Jack 2024-09-27 23:04:14 -04:00
parent 796cff48c8
commit 98dbfd4603
1 changed files with 45 additions and 45 deletions

View File

@ -1,10 +1,16 @@
import maya.OpenMaya as om
import maya.OpenMayaMPx as ompx
import maya.OpenMayaUI as omui
import maya.cmds as cmds
import os
import subprocess
import sys
from shiboken6 import wrapInstance
from PySide6 import QtWidgets
playblast_job_id = None
def get_plugin_directory():
# Get the path of the currently loaded plugin
plugin_name = "tinyblast"
@ -55,6 +61,15 @@ def custom_playblast(*args, **kwargs):
except subprocess.CalledProcessError as e:
print(f"Error during FFmpeg conversion: {e}")
def get_playblast_options_window():
# Check if the Playblast Options window is open
windows = cmds.lsUI(windows=True)
for window in windows:
if cmds.window(window, query=True, title=True) == "Playblast Options": # Exact title match
window_ptr = omui.MQtUtil.findWindow(window)
return wrapInstance(int(window_ptr), QtWidgets.QWidget)
return None
class WindowWatcher:
""" A class to watch for a particular window in Maya """
@ -66,64 +81,46 @@ class WindowWatcher:
def check_for_window_open(self):
if not self.window_opened:
window = self.get_window_by_title(self.window_title)
window = get_playblast_options_window()
if window:
self.on_open_callback()
self.window_opened = True
else:
window = self.get_window_by_title(self.window_title)
window = get_playblast_options_window()
if not window:
self.window_opened = False
if self.on_close_callback:
self.on_close_callback()
def get_window_by_title(self, title):
# Check all open windows and return the one that matches the title
windows = cmds.lsUI(windows=True)
for window in windows:
if cmds.window(window, query=True, title=True) == title:
return window
return None
def add_custom_button_to_playblast():
# Get the Playblast Options window
window = get_playblast_options_window()
if window:
# Find the layout of the Playblast Options window
layout = cmds.columnLayout(adjustableColumn=True)
if layout:
# Add a custom button below existing UI
cmds.setParent(layout) # Set the parent to the top-level layout
cmds.columnLayout(adjustableColumn=True)
cmds.button(label="Tinyblast", command=custom_button_action)
else:
print("Couldn't find the layout for the Playblast Options window.")
else:
print("Playblast Options window not found.")
def custom_button_action(*args):
custom_playblast()
def get_playblast_options_window():
# Check if the Playblast Options window is open
windows = cmds.lsUI(windows=True)
for window in windows:
if cmds.window(window, query=True, title=True) == "Playblast Options": # Exact title match
return window
def find_button_by_label(window, label_text):
for widget in window.findChildren(QtWidgets.QPushButton):
if widget.text() == label_text:
return widget
return None
def setup_script_job(playblast_job_id):
def override_playblast_button():
playblast_window = get_playblast_options_window()
tinyblast_button = find_button_by_label(playblast_window, 'Playblast')
if tinyblast_button:
tinyblast_button.clicked.disconnect()
tinyblast_button.setText('Tinyblast')
tinyblast_button.clicked.connect(custom_playblast)
def setup_script_job():
global playblast_job_id
# Kill any previously running scriptJob
if playblast_job_id is not None and cmds.scriptJob(exists=playblast_job_id):
cmds.scriptJob(kill=playblast_job_id, force=True)
print(f"Killed previous scriptJob with ID: {playblast_job_id}")
print("Created scriptjob")
# Watch for the Playblast Options window by title
playblast_watcher = WindowWatcher(
window_title="Playblast Options", # Exact window title to look for
on_open_callback=add_custom_button_to_playblast
on_open_callback=override_playblast_button
)
# Set up a new scriptJob
@ -134,28 +131,31 @@ class Tinyblast(ompx.MPxCommand):
ompx.MPxCommand.__init__(self)
def doIt(selfself, args):
print("Executing custom playblast command.")
print("So I started blastin'.")
custom_playblast()
def tinyblastCmd():
def tinyblast_cmd():
return ompx.asMPxPtr(Tinyblast())
def initializePlugin(mobject):
global playblast_job_id
try:
mplugin = ompx.MFnPlugin(mobject, "Jack Christensen", "1.0.1", "Any")
mplugin.registerCommand("tinyblast", tinyblastCmd)
mplugin = ompx.MFnPlugin(mobject, "Jack Christensen", "1.1.0", "Any")
mplugin.registerCommand("tinyblast", tinyblast_cmd)
om.MGlobal.displayInfo("Tinyblast plugin loaded.")
playblast_job_id = None
setup_script_job(playblast_job_id)
setup_script_job()
except Exception as e:
om.MGlobal.displayError(f"Failed to initialize plugin: {str(e)}")
raise
def uninitializePlugin(mobject):
global playblast_job_id
try:
mplugin = ompx.MFnPlugin(mobject)
mplugin.deregisterCommand("tinyblast")
om.MGlobal.displayInfo("Tinyblast plugin unloaded.")
cmds.scriptJob(kill=playblast_job_id, force=True)
print("Killed script job")
except Exception as e:
om.MGlobal.displayError(f"Failed to uninitialize plugin: {str(e)}")
raise