[PyMel + PySide] QlistWidget and Pymel Selection

191 views
Skip to first unread message

Padraig Ó Cuínn

unread,
Nov 30, 2015, 3:05:30 AM11/30/15
to Python Programming for Autodesk Maya
Hi Everyone,

I was wondering if you can help me out with the above topic. 

basically what I have is a simple UI with a line edit ontop of a ListWidget and under that a button that refreshes the list <<<< basically what i am hoping to have happen..

so far this:

#mayas selection vioa pymel
maya_list
= pm.selected()




#connection from button to refresh list
button
.clicked.connect(refresh)


#the refresh function adds  the maya_list
refresh
(self):
   
self.listWidget.clear()
   
self.listWidget.addItem(maya_list)


I havent even begun to work on the lineEdit part yet but it's supposed to search through all the listed items in the listWidget and display them as typed.

Any help would be awh-Mazing.

Thanks

Padraig

Geordie Martinez

unread,
Nov 30, 2015, 3:42:05 AM11/30/15
to python_inside_maya

you have to loop through and add an item at a time like this:

from PySide import QtGui, QtCore
#clear it out
def refresh(self):
    self.listWidget.clear()
    items = pm.selected()
    for item in items:
        newItem = QtGui.QListWidgetItem( item.nodeName())
        self.listWidget.addItem(newItem)

try that


--
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/d2e2cff9-3157-428c-bc11-05b17b7a0aa2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Justin Israel

unread,
Nov 30, 2015, 5:09:03 AM11/30/15
to python_inside_maya

And for your next step in dealing with your line edit, you may want to think about switching to a QListView + QStandardItemModel + Qsortfilterproxymodel. That would easily let you apply filter strings/patterns on your model. Otherwise you might have to loop over the items in your QListWidget and manually test the text value of each one, and set the hidden state. I don't remember if a QListWidget let's you put a Qsortfilterproxymodel in between it. Maybe it does.

Justin


Padraig Ó Cuínn

unread,
Nov 30, 2015, 6:19:13 PM11/30/15
to Python Programming for Autodesk Maya
Hi Geordie and Justin, I tried your function and for some reason I am nt getting any output at all on selecting objects and pushbutton. I'll give more details later on when I get time thank you.

Padraig

Geordie Martinez

unread,
Nov 30, 2015, 7:35:21 PM11/30/15
to python_inside_maya
when you connect the button it's probably just not calling the function. post your code and we'll take a gander.

On Mon, Nov 30, 2015 at 3:19 PM, Padraig Ó Cuínn <patchquin...@gmail.com> wrote:
Hi Geordie and Justin, I tried your function and for some reason I am nt getting any output at all on selecting objects and pushbutton. I'll give more details later on when I get time thank you.

Padraig

--
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.

Padraig Ó Cuínn

unread,
Nov 30, 2015, 8:20:09 PM11/30/15
to Python Programming for Autodesk Maya
Hey Geordie, I figured the issue out (I had the function after the call haha), It works perfectly only for single selection tho. I would imagine its just a matter of using newItems = QtGui.QListWidgetItems and self.listWidget.addItems(newItems).

also i discovered that pyside craps out at so many executions is a try and except the best way around this or is there a more elegant way to it.

forgive the stupid amount of import but Pycharm kept shooting import errors unless i gave it what it wanted even in mayapy console.

import PySide

from PySide import QtGui, QtCore
from maya import OpenMayaUI as omui
from shiboken import wrapInstance
import pymel.core as pm
from PysideCode.scroller.uiCode import scrollerUI
from PySide.QtGui import QWidget, QMainWindow

pointer = omui.MQtUtil.mainWindow()
mayaMainWindow = wrapInstance(long(pointer), QWidget)



class mainWindow(QMainWindow, scrollerUI.Ui_MainWindow ):
def __init__(self, parent=mayaMainWindow):
super(mainWindow, self).__init__(parent)
self.setupUi(self)

self.pushButton.clicked.connect(self.reList)

def reList(self):

self.listWidget.clear()
items = pm.selected()
for item in items:
            newItem = PySide.QtGui.QListWidgetItem(item.nodeName())
self.listWidget.addItem(newItem)



if __name__ == '__main__':

try:
mainWindow.close()
except:
pass


win = mainWindow()
win.show()

Padraig

Justin Israel

unread,
Nov 30, 2015, 8:22:52 PM11/30/15
to Python Programming for Autodesk Maya
Is that an indent error, when you are looping over the selection items and adding them? Its hard to tell from all the breaks in your code, but it seems like you only add the last item, once the for-loop finishes? Also can you describe what you mean about pyside "crapping out"?

--
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.

Padraig Ó Cuínn

unread,
Nov 30, 2015, 9:08:24 PM11/30/15
to Python Programming for Autodesk Maya
Hi Justin, No errors at all it just keeps the selection of the last chosen item bit and lists it after the loop.

By capping out I mean that after day 5 or more execute of the script I get a winEvent error and have to force close maya.

Padraig Ó Cuínn

unread,
Dec 3, 2015, 1:35:59 AM12/3/15
to Python Programming for Autodesk Maya
bah its kind of hard to impliment the qListView 

Justin Israel

unread,
Dec 3, 2015, 2:28:37 AM12/3/15
to Python Programming for Autodesk Maya

Its not too much different if you use QStandardItemModel. The difference is that you just add items to the model instead of the view. It still has that high level item approach.


On Thu, 3 Dec 2015 7:36 PM Padraig Ó Cuínn <patchquin...@gmail.com> wrote:
bah its kind of hard to impliment the qListView 

--
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.
Message has been deleted

Padraig Ó Cuínn

unread,
Dec 3, 2015, 2:19:39 PM12/3/15
to Python Programming for Autodesk Maya
Hi Justin,

Here is what I have came up with so far. The same problem has applied where it will only select the first object in the selection and add it tot he QListView. 
I'm not sure if I have came up with the best solution for the searchbox but it works just fine, If you have any comments on it that would be great,

import pymel.core as pm
from maya import OpenMayaUI as omui
import shiboken
from PysideCode.scroller.uiCode import scrollerUI
from PySide import QtGui, QtCore
from PySide.QtGui import QWidget

pointer = omui.MQtUtil.mainWindow()
mayaMainWindow = shiboken.wrapInstance(long(pointer), QWidget)

class mainWindow(PySide.QtGui.QMainWindow, scrollerUI.Ui_MainWindow):
    def __init__(self, parent=mayaMainWindow):
        super(mainWindow, self).__init__(parent)
        self.setupUi(self)

        #connections
        self.pushButton.clicked.connect(self.reList)
        self.listView.clicked.connect(self.selectionHandler)
        self.lineEdit.textChanged.connect(self.filterHandler)
        self.statusbar.showMessage('You selected it')

    def reList(self):
        for node in pm.selected():
            self.model.clear()
            item = QtGui.QStandardItem()
            item.setText(str(node))
            self.model.appendRow(item)

    def selectionHandler(self, index):
        index = self.proxy.mapToSource(index)
        item = self.model.itemFromIndex(index)

        pm.select(item.data())

    def filterHandler(self):
        regex = '.*{}.*'.format(self.lineEdit.text().replace(' ', '.*'))
        self.proxy.setFilterRegExp(regex)
        self.proxy.invalidateFilter()



if __name__ == "__main__":

    global win
    try:
        win.close()
        win.deleteLater()
    except:
        pass
    win = mainWindow()
    win.show()

Padraig

Justin Israel

unread,
Dec 3, 2015, 3:47:42 PM12/3/15
to Python Programming for Autodesk Maya
You are clearing your model on each loop:

for node in pm.selected():
            self.model.clear()

That should fix it.

--
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.

Padraig Ó Cuínn

unread,
Dec 3, 2015, 4:19:03 PM12/3/15
to python_in...@googlegroups.com

Ah yeah I figured that out afterwards haha. Same stupid mistake. Now that the search function and the new model is done. It's time to move onto the status bar. For more complex work with selections like what rob has in his book.

Padraig

Reply all
Reply to author
Forward
0 new messages