Newbie Help/Tutorial Required...Converting simple tool from Maya UI to PySide...

134 views
Skip to first unread message

rob lee Jones

unread,
Nov 3, 2014, 8:22:56 AM11/3/14
to python_in...@googlegroups.com
Hi there,
This is a question to the community at large for some fastrack help in converting a simple tutorial.exercise 'wall builder' script written in python using Maya's native UI functionality to one that deploys Pyside instead.
I'm aware that there is a growing repository of intro to PySide in Maya type tutorials...but they don't seem to go much beyond the very first 'hello world' button push. I'm happy to stand corrected on this observation by the way.

So here's a little exercise I would like to convert as a way of understanding how user input can be captured and used inside a python function.
If anyone can knock up a solution which I imagine would be real quick if you are already fluent with pyQT/PySide it would be most appreciated.
Basically the script below accepts two integers from the User (width, height of wall to be built) and when the 'create wall' button is pushed these values are then passed to a python function that uses them to generate the wall of the required dimensions.
I've started to look through the docs but was hoping to get a heads up from anyone that is willing to shed light on how to do this in PySide.

Here's the original python script with native Maya UI functionality to be converted to using PySide instead....


import maya.cmds as cmds
if cmds.window("brickGen", exists=True):
    cmds.deleteUI( "brickGen")

window = cmds.window("brickGen", title = "Brick Wall Generator") 

cmds.columnLayout()
cmds.intField("wallHeight", minValue=1, maxValue=30, value=1) 
cmds.intField("wallLength", minValue=1, maxValue=30, value=1) 
cmds.button(label="create wall", command="brickGen()")


def brickGen(): 
    # question ....how to do the next two lines or equivalent in PySide.....
    col = cmds.intField("wallHeight", q=True, value=True)
    row = cmds.intField("wallLength" , q=True, value=True)
    i=0
    j=0
    offset=-1.5
    brickHeight=1

    while i < col:
        h= brickHeight*i
        if (i%2==0):
            print 'even brick'
            while j < row:
                cmds.polyCube(w=1,h=1,d=3)
                cmds.move(3*j, z=True, r=True)
                cmds.move(h, y=True, r=True)
                j+=1
        else:
            print 'odd brick'
            while j < row:
                cmds.polyCube(w=1,h=1,d=3)
                cmds.move(3*j, z=True, r=True)
                cmds.move(h, y=True, r=True)
                cmds.move(offset,z=True,r=True)
                j+=1

        i+=1
        j=0
    
cmds.showWindow() 


Thanks in advance to anyone for any help with this request.
Regards
Rob.

Marcus Ottosson

unread,
Nov 3, 2014, 8:36:01 AM11/3/14
to python_in...@googlegroups.com
Hi Rob,

There's been some mentions of this in other threads, but it deserves some further spotlight.


It goes well beyond the basic examples and should provide you with a solid foothold on how to get started with PyQt/PySide.

Best,
Marcus

--
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/9ca1d797-894a-472f-97ac-4fa84cc24b56%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Nov 3, 2014, 1:44:00 PM11/3/14
to python_in...@googlegroups.com

Hey Rob

One of the important concepts to have down when getting into Qt is classes. If you understand those, then you should be good to go.
The approach you would take is to create a QDialog subclass. This will have some kind of layout attached, such as a QFormLayout.
Then you will add QLabel and QSpinBox instances for your width and height. And then a QPushButton. All of these should be member attributes of the QDialog.
Qt uses signal/slot communication. You would create a method on your subclass that would perform the wall building when called. It should look at the member QSpinBox instances to grab their current int values. Then you connect your push button clicked signal to your builder method. All of the widget setup and connection happens in the constructor of your class.
Then you will have this complete QDialog subclass that can be created and shown.

I could write out an example, but if you are only just getting started, it may be best to grab a book or watch a video (I have a PyQt training video at cmiVfx), as the example would most likely lead to a bunch of extra getting-started questions covered in training material


- justin

--

Robert Jones

unread,
Nov 4, 2014, 3:47:10 PM11/4/14
to python_in...@googlegroups.com

Thanks Marcus and Justin on that initial round of advice and support. Well I happen to have recently purchased both the Rapid GUI Programming with Python and QT book and the cmiVFX pyQT/Maya training. The book I've been dipping into/skimming and thinking yikes theres a lot to learn and the training I've yet to find a sustained period of time to work through end to end. So all tooled up and no excuses really I guess - it's just a case of really applying myself. That said….I would greatly appreciate it Justin if you could knock up an example solution to the specific task I had in mind (i.e. accept 2 integer inputs from the user and feed them into a python function via a button click) and then I'll fill in all the other gaps in my current knowledge and hope to start making more progress with this transition to QT-based Maya UI design…I just need to get beyond the early hurdles and get up and running…I'm fine with classes pretty much and general oops concepts, however my earlier forays into pyQT/pySide have been a series of false starts to date with numerous technical issues getting in the way. However, now feels like the right time to move on.


Thanks again for your advice and support so far.

Regards

Rob.

Justin Israel

unread,
Nov 4, 2014, 5:13:33 PM11/4/14
to python_in...@googlegroups.com

Here is an example of what it might look like

from PySide import QtCore, QtGui

class BrickWallGenDialog(QtGui.QDialog):

    def __init__(self, parent=None):
        super(BrickWallGenDialog, self).__init__(parent)

        self.setObjectName("brickGen")
        self.setWindowTitle("Brick Wall Generator")

        self._heightInput = QtGui.QSpinBox()
        self._heightInput.setRange(1, 30)

        self._lengthInput = QtGui.QSpinBox()
        self._lengthInput.setRange(1, 30)

        self._button = QtGui.QPushButton("Create Wall")

        layout = QtGui.QFormLayout(self)
        layout.addRow("Wall Height:", self._heightInput)
        layout.addRow("Wall Length:", self._lengthInput)
        layout.addRow(self._button)

        # Connections
        self._button.clicked.connect(self.brickGen)

    def brickGen(self):
        height = self._heightInput.value()
        length = self._lengthInput.value()
        print "Do brickGen with %d x %d" % (height, length)

win = BrickWallGenDialog()
win.resize(200, 100)
win.show()

Robert Jones

unread,
Nov 5, 2014, 12:09:36 AM11/5/14
to python_in...@googlegroups.com

Hi Justin,

I've just seen your latest reply and wanted to say many thanks as that as actually helped a lot and has made the road ahead much clearer.

In fact I now realise that I already understand everything else in the example solution but seeing it contextualised with this almost trivial little example has really helped to clarify the general approach.


Once again many thanks for your support.

Regards

Rob.

Reply all
Reply to author
Forward
0 new messages