Skip to content

Instantly share code, notes, and snippets.

@atvKumar
Created July 27, 2015 11:47
Show Gist options
  • Save atvKumar/7949ac37451607fdcf07 to your computer and use it in GitHub Desktop.
Save atvKumar/7949ac37451607fdcf07 to your computer and use it in GitHub Desktop.
Auto-updating GUI
#-------------------------------------------------------------------------------
# Boiler Plate Code for Windows
# By : Kumaran
import pymel.core as pm
class UI(pm.uitypes.Window):
_TITLE = "mkWindow"
_LAYOUT = "mkLayout"
_DOCKCONTROL = "mkDockCtrl"
_SCRIPTJOB = "mkWindowScriptJob"
dockControl = None
currSel = None
def __new__(cls, name):
if pm.window(cls._TITLE, exists=True):
pm.deleteUI(cls._TITLE)
if(pm.dockControl(cls._DOCKCONTROL, ex=1)):
pm.deleteUI(cls._DOCKCONTROL, control=1)
self = pm.window(cls._TITLE, title=cls._TITLE)
return pm.uitypes.Window.__new__(cls, self)
def __init__(self, name):
form = pm.formLayout(self._LAYOUT)
with form:
lbl_sel = pm.text(p=form, l="Selected:")
form.attachForm(lbl_sel, 'left', 0)
self.currSel = pm.textField(p=form, w=150, ip=1)
form.attachControl(self.currSel, 'left', 5, lbl_sel)
self.show()
self._setup_scriptJob(name)
def _setup_scriptJob(self, name):
if pm.optionVar.has_key(self._SCRIPTJOB):
jobNum = pm.optionVar[self._SCRIPTJOB]
if pm.scriptJob(ex=jobNum):
pm.scriptJob(kill=jobNum, force=True)
jobNum = pm.scriptJob(event=["SelectionChanged", "%s.ui_refresh()" % name])
pm.optionVar[self._SCRIPTJOB] = jobNum
jobNum = pm.scriptJob(uid=[self._TITLE,
"pm.scriptJob(kill=pm.optionVar[\"%s\"], force=True)" % self._SCRIPTJOB])
def ui_makeDockable(self):
''' make the window dockable '''
# skip if already dockable
if(self.dockControl):
pm.mel.eval('warning "%s"' % "Window already dockable!")
return
# create dockControl
self.dockControl = pm.dockControl(self._DOCKCONTROL, l=self, content=self,
area='left', allowedArea=['left', 'right'])
def ui_updateTextBox(self, msg):
print "Recieved:", msg
self.currSel.setText(msg)
def ui_refresh(self):
try:
sel = pm.ls(sl=1)[0]
self.ui_updateTextBox(sel)
except IndexError:
self.ui_updateTextBox("Nothing Selected!")
#
#-------------------------------------------------------------------------------
win = UI("win")
#win.ui_makeDockable()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment