PySide beginner question

60 views
Skip to first unread message

Aren Voorhees

unread,
Jun 3, 2015, 6:15:47 PM6/3/15
to python_in...@googlegroups.com
Hey all, I'm trying to get started learning PySide.  In my below example, I know I should technically be using classes and probably don't have things constructed exactly right, but hey, baby steps.  

Anyway,  I imagine this is pretty simple.  I have a SpinBox that can contain integers, and a button that should preform a function when played.  Only I need to put the value of the SpinBox into the function as an argument which I'm having a hard time figuring out.  It seems like QtGui.QSpinBox().value() gets the value of the SpinBox but I don't know how to send that back to the function as an argument.  The button in question is several lines from the bottom.  I've scoured the internet for help, but maybe I don't even know what I'm actually looking for.   Any help would be hugely appriciated! 

from PySide import QtGui
import maya.OpenMayaUI as mui
import shiboken
import pymel.core as pm
import maya.cmds as cmds




def get_maya_window():
    pointer
= mui.MQtUtil.mainWindow()
   
return shiboken.wrapInstance(long(pointer), QtGui.QWidget)


###---Functions---###a


def move_keys(spinBoxVal):


    curSel
= pm.selected()


    pm
.keyframe(curSel, relative=True, tc=(spinBoxVal))        


####---Window code---###


windowName
= "myWin"


#check for window
if cmds.window("myWin", exists = True):
    cmds
.deleteUI("myWin", wnd=True)


#window
parent
= get_maya_window()
window
= QtGui.QMainWindow(parent)
window
.setObjectName(windowName)
window
.setWindowTitle("Anim Tools")


#font
font
= QtGui.QFont()
font
.setPointSize(12)
font
.setBold(True)


#main widget
mainWidget
= QtGui.QWidget()
window
.setCentralWidget(mainWidget)


#Translate layout
translateLayout
= QtGui.QHBoxLayout()


#Vertical layout
verticalLayout
= QtGui.QVBoxLayout(mainWidget)
parent
= get_maya_window()
verticalLayout
.addLayout(translateLayout)


#Label
label
= QtGui.QLabel("Shift Value:")
translateLayout
.addWidget(label)


#SpinBox
spinBox
= QtGui.QSpinBox()
spinBox
.setRange(0,10000)
spinBox
.setValue(50)
translateLayout
.addWidget(spinBox)


#button
button
= QtGui.QPushButton("Shift")
verticalLayout
.addWidget(button)
button
.setMinimumSize(100,40)
button
.setFont(font)
button
.setMaximumSize(400,40)
button
.setStyleSheet("background-color:rgb(59,248,130); color: black")
button
.clicked.connect(move_keys)#                                        <---------This is where I don't know what to do!


#show window
window
.show()


Joe Weidenbach

unread,
Jun 3, 2015, 6:36:50 PM6/3/15
to python_in...@googlegroups.com
This would be a bit more straightforward with a class, it's true.  However, I think it's great you've gotten this far.  You're almost there, and we can work around not being in a class.

First, we need to bring in partial to make this work:

from functools import partial

partial takes as arguments a function and some values, and returns a new function that integrates those arguments.  The one issue with this: partial will get evaluated when the interface is created, not when the button is clicked.  We could avoid this issue with a class, but oh well.  The workaround is that we can pass the spinBox to move_keys() instead of it's value, and then we can evaluate the value in move_keys():

def move_keys(spinBox):
    spinBoxVal = spinBox.value()

    curSel = pm.selected()


    pm.keyframe(curSel, relative=True, tc=(spinBoxVal))   

Now that we've got that issue fixed, we just have to change the connect() line:

button.clicked.connect(partial(move_keys, spinBox))

Put that all together, and you get this: http://pythonfiddle.com/simple-partial-callback-in-pyside

That should do it for you!

Hope that helps

--
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/88c51f8c-e2fa-4142-9a18-fd47f321522f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joe Weidenbach

unread,
Jun 3, 2015, 6:59:14 PM6/3/15
to python_in...@googlegroups.com
Whoops, pythonfiddle was being stubborn.  It's there now :)

Aren Voorhees

unread,
Jun 5, 2015, 3:28:23 PM6/5/15
to python_in...@googlegroups.com
Hey, that worked!  Thanks so much for the help and explanation!  I'm realizing that I just need to suck it up and jump strait into classes so I'm not spending time learning the wrong (or less good, at least) way of doing things.  Thanks again!
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