Yea there are a few problems in your code, so I can see why it wasn't
working correctly.
Here is a modified version:
## No globals# camLs = []
class orientCameraUI(QDialog):
def __init__(self, parent=None):
super(orientCameraUI, self).__init__(parent)
# You have a class, so you should store state in the class
self.__camLs = []
self.setMinimumWidth(300)
self.setMaximumHeight(80)
self._initUI()
self._camMenu()
# self.createConnections()
def _initUI(self):
...
## Get rid of this. The QPushButton doesn't have aboutToShow()
# def createConnections(self):
# self.connect(self.setCameraBtn, SIGNAL('aboutToShow()'),
self._updateMenu)
def _camMenu(self):
menu = QMenu("menu", self.setCameraBtn)
self.setCameraBtn.setMenu(menu)
menu.aboutToShow.connect(self._updateMenu)
menu.triggered.connect(self._camSelected)
def _camSelected(self, action):
self.currentCamTxt.setText(action.text())
def _updateMenu(self):
## You were creating a brand new menu and clearing it
## each time, instead of clearing the one already set
## on the QPushButton. So nothing was really changing.
# menu = QMenu("menu", self.setCameraBtn)
menu = self.setCameraBtn.menu()
menu.clear()
cams = cmds.listRelatives(cmds.ls(cameras=True), parent=True)
if not cams:
return
allCams = [c for c in cams if not cmds.camera(c, q=True,
startupCamera=True)]
self.__camLs = allCams
if not allCams:
return
for item in camLs:
menu.addAction(item)
Some points about it:
- Get rid of the global list. You can just use an instance attribute if
you want to separately track the camera names
- You were duplicating code and only updating a new temporary QMenu that
you are actually leaking on every update. You never actually updated the
menu set on the button
- Just set up the empty menu once in your init. Then your aboutToShow()
can rebuild that menu every single time.
On Wed, Oct 8, 2014 at 3:29 PM, likage <[email protected]> wrote:
> It is not working for me initially.
> I think I am still screwing up somewhere... Is this correct for me to do
> it in this way?
>
> camLs = []
>
> class orientCameraUI(QDialog):
> def __init__(self, parent=None):
> super(orientCameraUI, self).__init__(parent)
> self.setMinimumWidth(300)
> self.setMaximumHeight(80)
> self.initUI()
> self.camMenu()
> self.createConnections()
>
> def initUI(self):
> ...
>
> def createConnections(self):
> self.connect(self.setCameraBtn, SIGNAL('aboutToShow()'), self.
> _updateMenu)
>
> def camMenu(self):
> allCams = [c for c in cmds.listRelatives(cmds.ls(cameras=1),parent
> =1) if not cmds.camera(c, q=True, startupCamera=True)]
>
> camLs.extend(allCams)
>
> menu = QMenu("menu", self.setCameraBtn)
>
> for item in camLs:
> menu.addAction(QAction(item, menu))
> self.setCameraBtn.setMenu(menu)
>
> menu.aboutToShow.connect(self._updateMenu)
> menu.triggered.connect(self._camSelected)
>
> def _camSelected(self, action):
> self.currentCamTxt.setText(action.text())
>
> def _updateMenu(self):
> menu = QMenu("menu", self.setCameraBtn)
> menu.clear()
>
> allCams = [c for c in cmds.listRelatives(cmds.ls(cameras=1),parent
> =1) if not cmds.camera(c, q=True, startupCamera=True)]
> if not allCams:
> return
>
> del camLs[:]
> camLs.extend(allCams)
>
> for item in camLs:
> menu.addAction(item)
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/4fead427-96fa-4891-919b-26564df30b23%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/4fead427-96fa-4891-919b-26564df30b23%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1a8BkOXxmscv7vk41w%2BqzSD2hQcLKnBxMe_%2BQn5ns63Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.