Maya PySide2 and QSlider with connectControl to object

Maya PySide2 and QSlider with connectControl to object

5inch
Contributor Contributor
2,604 Views
3 Replies
Message 1 of 4

Maya PySide2 and QSlider with connectControl to object

5inch
Contributor
Contributor

Hello Together,

please can you support me with this...

I try to connect a QSlider with an easy QWindow with simple Attributes of an object. The slider should be connected with a QTextEdit to manually edit the values. everything should be updated in realtime. I found the Python command: "connectControl" which should do the Job, but I dont get evereything up and running.

 

Here is my code:

from PySide2 import QtCore, QtGui, QtWidgets
import maya.cmds as cmds


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)

        self.horizontalSlider = QtWidgets.QSlider(Dialog)
        self.horizontalSlider.setGeometry(QtCore.QRect(60, 60, 160, 22))
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.setObjectName("horizontalSlider")
        self.horizontalSlider.setTickInterval(1000)
        self.horizontalSlider.setSingleStep(0.1)
        self.horizontalSlider.setRange(0, 1000)
        

        self.textEdit = QtWidgets.QTextEdit(Dialog)
        self.textEdit.setGeometry(QtCore.QRect(230, 60, 41, 31))
        self.textEdit.setObjectName("textEdit")
        
        self.horizontalSlider.valueChanged.connect(self.print_it)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtWidgets.QApplication.translate("Dialog", "Dialog", None, -1))
        
        
    def print_it(self,x):
        try:
            print float(x)
            self.horizontalSlider.valueChanged[int].connect(self.textEdit.setText(str(x)))
            
        except Exception as e:
            print e

if __name__ == "__main__":
    if cmds.window("Dialog", q = True, ex = True):
        cmds.deleteUI("Dialog")

Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()

pyside2_qSlider_2.png

and this is the code I found from the Maya docs:

import maya.cmds as cmds

sphereNames = cmds.polySphere()
sphereName = sphereNames[0]
window = cmds.window()
cmds.columnLayout()
cmds.text( l='X Value:' )
cmds.floatField( 'xx' )
cmds.connectControl( 'xx', '%s.tx' % sphereName )
cmds.text( l='Visibility' )
cmds.checkBox( 'vis' )
cmds.connectControl( 'vis', '%s.visibility' % sphereName )
cmds.floatFieldGrp( 'rot', l='Rotation:', numberOfFields=3 )
# index 1 would be the text label
cmds.connectControl( 'rot', '%s.rx' % sphereName, index=2 )
cmds.connectControl( 'rot', '%s.ry' % sphereName, index=3 )
cmds.connectControl( 'rot', '%s.rz' % sphereName, index=4 )
cmds.showWindow( window )

and the result:

pyside2_qSlider_1.png

Many thanks for any support on this.

5inch

0 Likes
Accepted solutions (3)
2,605 Views
3 Replies
Replies (3)
Message 2 of 4

olarn
Advocate
Advocate
Accepted solution

I think that command is meant for maya ui such as one build by cmds, not raw pyside widgets.

 

Same effect could possibly be implemented using 

http://help.autodesk.com/cloudhelp/2018/ENU/Maya-SDK/cpp_ref/class_m_node_message.html#add4ee645b2ce...

wire the callback to your ui and you should be receiving updates.

 

Also this line here

self.horizontalSlider.valueChanged[int].connect(self.textEdit.setText(str(x)))

Shouldn't it be something like this?

self.horizontalSlider.valueChanged.connect(lambda x: self.textEdit.setText(str(x)))

 

0 Likes
Message 3 of 4

5inch
Contributor
Contributor
Accepted solution

Hello olarn,

sorry for the late reply, I was off for the last days.

 

Good points you are highlighting here, many thanks.

I want to build the UI now with Maya UI commands, may be better supported then pySide2.

 

Im looking for a good example for layouts for the UI. Any tip?

 

Best regards, 5inch

0 Likes
Message 4 of 4

olarn
Advocate
Advocate
Accepted solution

The laziest possible way is to build the whole ui in designer and load it by this command.

From maintainability standpoint I wouldn't recommend it but you be the judge. 

http://help.autodesk.com/cloudhelp/2018/ENU/Maya-Tech-Docs/CommandsPython/loadUI.html

0 Likes