Thank you so much guys, it took me a while, but i was able to put it together!
I also needed an extra state of the button to handle user error - in my script i need to check if anything selected before executing actual functions - so i added an extra signal to the provided example (Thanks @leocov ! )
I guess i also need to check if user selects geometry rather then lights or locators or whatever…
As for the disabling group boxes I just have an extra variable to set-up once i know i want to change the visibility and a function to run when i want to do that (Thanks @marcuso ! )
Here is what i have right now,
It might be ugly and not very efficient as i am still such an amateur in python, but it gets the job done!
Thanks again, i would love to hear additional critique or advise if you have any!
from maya import cmds
from Qt import QtWidgets, QtCore, QtGui
# Custom QPushButton with multiple states/signals
class StateButton(QtWidgets.QPushButton):
clickedStart = QtCore.Signal()
clickedStartError = QtCore.Signal()
clickedReset = QtCore.Signal()
def __init__(self, parent=None):
super(StateButton, self).__init__(parent=parent)
self._state = 0
#init
self._update_text()
self.clicked.connect(self._emit_state)
def _emit_state(self):
# Here i check if anything selected or not,
# if yes we can change the state and proceed with the script,
# if not - rise an error
if self._state == 0:
current_selection = cmds.ls(selection=True)
if not current_selection:
self.clickedStartError.emit()
self._state = 0
else:
self.clickedStart.emit()
self._state = 1
elif self._state == 1:
self.clickedReset.emit()
self._state = 0
self._update_text()
def _update_text(self):
if self._state == 0:
self.setText("Start")
elif self._state == 1:
self.setText("Reset")
class Gradient_Baker_UI(QtWidgets.QTabWidget):
def __init__(self):
super(Gradient_Baker_UI, self).__init__()
self.States_Button = StateButton()
# Defining text messages for the Log window
self.start_message = 'Select object(s) you want to work with and press Start'
self.start_successful_message = 'Further instructions'
self.nothing_selected_error_message = 'Nothing is selected, select an object or group'
# Initial text in Log window
self.log_text = self.start_message
# Tabs
self.tab1 = QtWidgets.QWidget()
self.tab2 = QtWidgets.QWidget()
self.tab3 = QtWidgets.QWidget()
self.addTab(self.tab1, 'Tab 1')
self.addTab(self.tab2, 'Tab 2')
self.addTab(self.tab3, 'Tab 3')
self.tab_01_UI()
self.tab_02_UI()
self.tab_03_UI()
self.setWindowTitle('Gradient Baker')
def tab_01_UI(self):
layout = QtWidgets.QVBoxLayout()
self.setTabText(0, "Baker")
self.tab1.setLayout(layout)
# Log window with overwrite mode turned on
self.log_window = QtWidgets.QPlainTextEdit('%s' % self.log_text)
self.log_window.setReadOnly(True)
self.log_window.setOverwriteMode(True)
self.log_window.setMaximumHeight(60)
# Start/Reset button and its signals assigned to functions
self.start_reset_button = self.States_Button
self.start_reset_button.clickedStart.connect(self.start_function)
self.start_reset_button.clickedStartError.connect(self.nothing_selected_error_function)
self.start_reset_button.clickedReset.connect(self.reset_function)
# Axis group box
x_button = QtWidgets.QPushButton('X')
x_button.setAutoExclusive(True)
x_button.setCheckable(True)
y_button = QtWidgets.QPushButton('Y')
y_button.setAutoExclusive(True)
y_button.setCheckable(True)
y_button.setChecked(True)
z_button = QtWidgets.QPushButton('Z')
z_button.setAutoExclusive(True)
z_button.setCheckable(True)
# Another button
self.reproject_faces_button = QtWidgets.QPushButton('Re-project for selected faces')
self.axis_group_box = QtWidgets.QGroupBox("Axis")
axis_group_box_layout = QtWidgets.QGridLayout()
axis_group_box_layout.addWidget(x_button, 0, 0)
axis_group_box_layout.addWidget(y_button, 0, 1)
axis_group_box_layout.addWidget(z_button, 0, 2)
axis_group_box_layout.addWidget(self.reproject_faces_button, 1, 0, 1, 0)
self.axis_group_box.setLayout(axis_group_box_layout)
layout.addWidget(self.log_window)
layout.addWidget(self.start_reset_button)
layout.addWidget(self.axis_group_box)
self.locked = 1
self.update_ui_visibility()
def tab_02_UI(self):
layout = QtWidgets.QVBoxLayout()
self.setTabText(1, "Presets")
self.tab2.setLayout(layout)
def tab_03_UI(self):
layout = QtWidgets.QVBoxLayout()
self.setTabText(2, "About")
self.tab3.setLayout(layout)
# Changes text in the Log
def text_change(self):
self.log_window.setPlainText('%s' % self.log_text)
# Error message style for the Log
def log_window_error_stylesheet(self):
self.log_window.setStyleSheet("""QPlainTextEdit {background-color: #ff6666; color: #333333;}""")
# Default message style for the Log
def log_window_default_stylesheet(self):
self.log_window.setStyleSheet("")
# Start function
def start_function(self):
self.log_text = self.start_successful_message
self.text_change()
self.log_window_default_stylesheet()
self.locked = 0
self.update_ui_visibility()
# Reset function
def reset_function(self):
self.log_text = self.start_message
self.text_change()
self.log_window_default_stylesheet()
self.locked = 1
self.update_ui_visibility()
# Error message function
def nothing_selected_error_function(self):
self.log_text = self.nothing_selected_error_message
self.text_change()
self.log_window_error_stylesheet()
raise RuntimeError(self.nothing_selected_error_message)
self.locked = 1
self.update_ui_visibility()
# Function to enable/disable group box
def update_ui_visibility(self):
if self.locked == 1:
self.axis_group_box.setDisabled(True)
elif self.locked == 0:
self.axis_group_box.setDisabled(False)
# Function to show UI in Maya
def show_UI():
ui = Gradient_Baker_UI()
ui.show()
return ui
from GradientBaker import Temp_UI
reload (Temp_UI)
ui = Temp_UI.show_UI()