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