Hi everyone, I am trying to make a real-time update, eg. a renaming between maya scene and my tool.
Currently I am able to effect changes from my tool and modify the naming in the scene. But in the event if I perform an undo in the maya scene itself, the undo changes are not effected back into the tool.
Here is my code:
class Example(QtGui.QWidget):
def __init__(self, parent=None):
super(Example, self).__init__()
self.initUI()
self.setup_connections()
def initUI(self):
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Rename')
self.list_widget = QtGui.QListWidget()
self.add_loc_btn = QtGui.QPushButton("Add new Locator")
vlayout = QtGui.QVBoxLayout()
vlayout.addWidget(self.list_widget)
vlayout.addWidget(self.add_loc_btn)
vlayout.addStretch(1)
self.setLayout(vlayout)
def setup_connections(self):
self.add_loc_btn.clicked.connect(self.add_locator)
self.list_widget.itemDoubleClicked.connect(self.list_item_dbl_click)
self.list_widget.itemChanged.connect(self.list_item_click)
def add_locator(self):
new_loc = cmds.spaceLocator()
loc_fp = cmds.ls(new_loc[0], long=True)
loc_item = QtGui.QListWidgetItem(str(new_loc[0]))
loc_item.setData(32, loc_fp[0])
loc_item.setFlags(
loc_item.flags() |
QtCore.Qt.ItemIsUserCheckable |
QtCore.Qt.ItemIsEditable
)
self.list_widget.addItem(loc_item)
def list_item_dbl_click(self):
current_item = self.list_widget.currentItem()
self.prev_name = current_item.text()
self.rename_counter = 1
def list_item_click(self, test):
success = False
current_item = self.list_widget.currentItem()
list_item_row = self.list_widget.currentRow()
new_name = current_item.text()
if self.validate_naming(new_name):
locator = current_item.data(32)
if cmds.objExists(locator) and self.rename_counter == 1:
cmds.select(locator)
cmds.rename(new_name)
# reset counter back to 0
self.rename_counter = 0
success = True
else:
# Set back to its initial naming before rename
current_item.setText(self.prev_name)
if success:
list_item = self.list_widget.item(list_item_row)
self.list_widget.setCurrentItem(list_item)
def validate_naming(self, name_input):
if re.search("^\s*[0-9]", name_input):
LOG.warning("Input naming cannot be started off with numbers!")
return False
return True
myWin = Example()
myWin.show()
Here are some steps to reproduce:
- Click on the âAdd new Locatorâ button
- It will creates a locator1 in the scene and populated in the list widget
- Double click on the locator1 in the list widget and input in a new name. Eg. âmyLocatorâ
- Go back to Maya and tried pressing âCtrl+Zâ (undo)
- While it does changes the name âmyLocatorâ back to âlocator1â in Maya context, I would also like it to be âupdatedâ in my Gui.
How should I go about doing this?
P.S: I was also trying to find a way to peform undoing in the Gui which will also in turns effects the changes in the Maya scene if possible.