How can i make Qpushbutton keep highlight until i deselect object

109 views
Skip to first unread message

nguyen vuducthuy

unread,
Apr 11, 2017, 11:57:25 AM4/11/17
to Python Programming for Autodesk Maya
Hi there,

I try to create an character picker that when i pressed button i select an object in the scene and it will keep highlight until i deselect that object. This really help me know what i'm selecting.
Thank for your help!

Justin Israel

unread,
Apr 11, 2017, 7:17:32 PM4/11/17
to python_in...@googlegroups.com
Sound like you want to make the button checkable:

Then you need to wire up a Maya callback so that the deselection can update the checked state on the button.

Justin
 

--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/cc0ae6f1-a251-4fb3-b063-fba31a54f8b3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

nguyen vuducthuy

unread,
Apr 12, 2017, 6:13:23 AM4/12/17
to Python Programming for Autodesk Maya
Well, i got it.

Here is a code i'm working, it still have some bug but it's work
ThankS!

import os
import json

from PySide.QtGui import QMainWindow
from PySide import QtGui, QtCore
from maya import cmds

from . import Bob
from ..qt_utils import get_anchor


class Picker(QMainWindow):

    def __init__(self, parent, data):
        super(Picker, self).__init__(parent)
        self.ui = Bob.Ui_MainWindow()
        self.data = data
        self.ui.setupUi(self)
        self.rubberband = QtGui.QRubberBand(
            QtGui.QRubberBand.Rectangle, self)
        self.setMouseTracking(True)
        # SIGNALS
        self.SCRIPT_JOB_NUMBER = cmds.scriptJob(event=["SelectionChanged", self.mayaSelectionChange])
        for k, v in data["anim_table"].iteritems():
            if hasattr(self.ui, k):
                getattr(self.ui, k).clicked.connect(lambda t=v: self.select(t))
        self.ui.select_all.clicked.connect(
            lambda t=data["anim_table"].values(): self.select(t))

    def select(self, target):
        shift, ctrl = 1, 4
        mod = int(cmds.getModifiers())
        flags = {"replace": mod == 0,
                 "toggle": mod == shift,
                 "deselect": mod == ctrl,
                 "add": mod == ctrl + shift}
        cmds.select(target, **flags)

    def mousePressEvent(self, event):
        self.origin = event.pos()
        self.rubberband.setGeometry(
            QtCore.QRect(self.origin, QtCore.QSize()))
        self.rubberband.show()
        QtGui.QWidget.mousePressEvent(self, event)

    def mouseMoveEvent(self, event):
        if self.rubberband.isVisible():
            self.rubberband.setGeometry(
                QtCore.QRect(self.origin, event.pos()).normalized())
        QtGui.QWidget.mouseMoveEvent(self, event)

    def mouseReleaseEvent(self, event):
        if self.rubberband.isVisible():
            self.rubberband.hide()
            selected = []
            rect = self.rubberband.geometry()
            for child in self.findChildren(QtGui.QPushButton):
                if rect.intersects(child.geometry()):
                    selected.append(child)
            if selected:
                # for child in selected:
                #     print ''.join("%s\n" % i for i in dir(child))
                #     print child.isChecked()
                [child.click() for child in selected]
                self.select([self.data["anim_table"][child.objectName()] for child in selected])
            else:
                print ' Nothing\n'
        QtGui.QWidget.mouseReleaseEvent(self, event)

    def mayaSelectionChange(self):
        for child in self.findChildren(QtGui.QPushButton):
            if child.isChecked() == True:
                selection = cmds.ls(selection = True)
                if len(selection)>0:
                    if self.data["anim_table"][child.objectName()] in [x.encode('UTF8') for x in selection]:
                        child.setChecked(True)
                    else:
                        child.setChecked(False)
                else:
                    child.setChecked(False)

    def closeEvent( self, event ):
        # Clean up the script job stuff prior to closing the dialog.
        cmds.scriptJob( kill=self.SCRIPT_JOB_NUMBER, force=True )
        super( Picker, self ).closeEvent( event )

def show():
    path = os.path.join(os.path.dirname(__file__), "..", "data", "Bob.json")
    with open(path) as fp:
        d = json.load(fp)
    if d.get("filetype") == "picker_data" and d.get("version") >= 0.1:
        Picker(parent=get_anchor(), data=d).show()



On Wednesday, April 12, 2017 at 6:17:32 AM UTC+7, Justin Israel wrote:
On Wed, Apr 12, 2017 at 3:57 AM nguyen vuducthuy <nguyenv...@gmail.com> wrote:
Hi there,

I try to create an character picker that when i pressed button i select an object in the scene and it will keep highlight until i deselect that object. This really help me know what i'm selecting.
Thank for your help!

Sound like you want to make the button checkable:

Then you need to wire up a Maya callback so that the deselection can update the checked state on the button.

Justin
 

--
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 python_inside_maya+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages