swatchDisplayPort and PySide

Hello.
I’m creating simple script for my friend to have some favorite materials always on top.

Now it’s working well, for each material I use qPushButton with context menu on right click (left click assigning material to selection).

And my question to you guys is it is possible to instead of buttons use “swatchDisplayPort” or something like that?
I have no idea how to attach it to my QT UI. Even popup menu is on left click :confused:
http://download.autodesk.com/global/docs/maya2013/en_us/CommandsPython/swatchDisplayPort.html
http://download.autodesk.com/us/maya/2011help/CommandsPython/popupMenu.html

now I’m just attaching material qPushbutton to qGridLayout, but it will be quite nice to have swatches like in hypershade.

My code is pretty rought right now, now I’m adding some new functions like import/export libraries. And mostly rewriting all trying to optimize.
But with no programming background, I’ll appreciate all tips :slight_smile:

Best regards :wink:
Krzym

Nathan Horne’s blog post covers how to add maya widgets to Qt:

http://nathanhorne.com/embedding-a-maya-widget-into-a-pyqt-ui/

Here’s a simple example using the swatchDisplayPort:


from PySide import QtGui
import maya.OpenMayaUI as omui
import shiboken

# Create the port using cmds first. I couldn't get the displayPort to work unless I created a window first
tempwin = cmds.window()
cmds.columnLayout()
port = cmds.swatchDisplayPort(rs=100, wh=(100, 100), sn='lambert1')

# Get the QWidget of the port
ptr = omui.MQtUtil.findControl(port)
qport = shiboken.wrapInstance(long(ptr), QtGui.QWidget)

# Add it to a pyside UI
widget = QtGui.QWidget()
vbox = QtGui.QVBoxLayout()
widget.setLayout(vbox)
qport.setParent(widget)
vbox.addWidget(qport)

# We only needed the window to create the port
cmds.deleteUI(tempwin)

widget.show()

Thanks, this is what im looking for! I read few his articles but skipped this one some way :slight_smile:

I do not want to open new topic, because its related to your code.
Its working pretty well, I did the same in my script, and it is working for “first load”. I got array with shaders and the first load of this array to script is working perfect, but every next try is just creating new shader balls on top.
I tried cmds.deleteUI but its not working or crashing maya. I had problems to find anything on google, so I hope maybe you have some idea for that.

Basicly task is to on click/run function self.refresh() cleanup all shader balls from window.

Here I’m attaching my script so you can run it and see what I’m talking about:
main: favoriteMaterial.py - Pastebin.com
Qt utils: QTutils.py - Pastebin.com
shelf: shelf - Pastebin.com

Best regards,
Krzysiek

Your code uses ui files so I can’t run them.

However deleteUI works for me in my prior example:

widget = QtGui.QWidget()
delbutton = QtGui.QPushButton("Delete")
vbox = QtGui.QVBoxLayout()
vbox.addWidget(qport)
vbox.addWidget(delbutton)
widget.setLayout(vbox)

delbutton.clicked.connect(lambda: cmds.deleteUI(port))

Yep your example is working, in my script its working too. But I have no idea whats going on, because Maya is crashing every time.
I created function: clearSwatch which deleteUI from exactly selected Swatch - and it’s working well (well, not realy)
calling:


self.clearSwatch[0]
or even

test = 5
self.clearSwatch[5]

is working well <- tested in few places, runing from e.g.: newMaterial or deleteSlot
but trying to send material index into deleteSlot means, instant crash. I have no idea why, checked type of parameter I receive inside (its integer). So when I sent self.deleteSlot(5) <- crash. when i send the same, but inside I’ll hardcode self.clearSwatch(5) i’ll work.

next I created clearAllSwatches which should delete all existing swatches from array ports. And there the hell happens -> I have no idea whats going on, but using this function means instant maya crash.

Here is updated file:
favoriteMaterial.py

main.ui

Qtutils and shelf is on first post.

For testing you should look into self.newMaterial, its connected with the Plus [+] icon.

The most stupid thing is that I do not have any errors, I’ve check everything many times, and I getting closer to abandon the idea of using shaderSwatches.

Hope you will have time to help me, because I have no more ideas.

Btw. Thanks for your reply :wink: And sorry for my delayed message now, I was on a little vacation and later trying to figure out what i’m doing wrong with this script.

You’re deleting a UI element using a pop-up action that is parented to the widget being deleted. This seems to crash Maya. Wrap the deleteUI call in evalDeferred.