PyQt Resources and UI

263 views
Skip to first unread message

Daz

unread,
Nov 9, 2012, 5:43:38 AM11/9/12
to python_in...@googlegroups.com
Heya

I'm fighting with pyqt for another week and uhh uhh uhh I rather be scripting some scripts than learning UI whh... 

Anyway I'm trying to wrap my head around QT I have Justin latest tutorial but its such a hard topic and he somehow skips all the basic and use all the fancy stuff that I get lost :( 

I'm trying to basically make ... a button that I click and that will create sphere... as basic as it gets...

I noticed Justin is using the cmd tool to convert UI. to PY code which is cool-ich even tho I don't know how to run it from maya hyh... 

Anyway a friend Kurian dropped me a few lines as to how to load it but since them I just cant find good place to learn... any hints as to resources? Where do I look for info, maya help python file or QT - where ? :O

Code: 

"from PyQt4 import QtCore, QtGui, uic
import maya.OpenMayaUI as mui
import sip

baseUI = "C:\Users\dmakowski\Desktop\PyQTtests\My\ui\ui3.ui"
baseUIClass, baseUIWidget = uic.loadUiType(baseUI)

class Ui_MainWindow(baseUIWidget, baseUIClass):
def __init__(self,parent=None):
super(baseUIWidget, self).__init__(parent)
self.setupUi(self)


def getMayaWindow():
ptr = mui.MQtUtil.mainWindow()
return sip.wrapinstance(long(ptr), QtCore.QObject)

def mayaMain():
maya_ui1_window = Ui_MainWindow(getMayaWindow())
maya_ui1_window.show()

mayaMain()

"

It sort off make sense... I'd love to find a proper help file to read what each line means but argh... I guess from now on I have to write a maya function and just link it to python button/text field(I dont even want to know how hard it will be to write a textfield wrrr... :(  ) 


In any case here is my basic stuff if any1 can hint me that would be great !


Thanks, bye...

cant attach files so I'm sticking text here...

ui3.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="labRenderSettings">
    <property name="geometry">
     <rect>
      <x>5</x>
      <y>5</y>
      <width>131</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>Render Settings</string>
    </property>
   </widget>
   <widget class="QPushButton" name="btallsettings">
    <property name="geometry">
     <rect>
      <x>2</x>
      <y>20</y>
      <width>80</width>
      <height>20</height>
     </rect>
    </property>
    <property name="text">
     <string>All settings</string>
    </property>
   </widget>
   <widget class="QLabel" name="labEditors">
    <property name="geometry">
     <rect>
      <x>5</x>
      <y>40</y>
      <width>131</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>Editors</string>
    </property>
   </widget>
   <widget class="QPushButton" name="BTmatedit">
    <property name="geometry">
     <rect>
      <x>2</x>
      <y>55</y>
      <width>80</width>
      <height>20</height>
     </rect>
    </property>
    <property name="text">
     <string>Material Editor</string>
    </property>
   </widget>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>


Panupat Chongstitwattana

unread,
Nov 9, 2012, 6:35:36 AM11/9/12
to python_in...@googlegroups.com
Hey Daz.

No worry. I'm a newbie myself. Believe me, when I just started, I bombard this group with tons of questions and everyone was very helpful :)

So first, the getMayaWindow() function is there to tell PyQt that the UI we're creating is a child of Maya's UI. Without it, your UI will still work. But it can, and will, go behind Maya's windows. Making it kinda annoying to find. Don't worry about the content of it much, just use it the way it is.

Next, what you do, is you sub class one of PyQt's UI class. For example

testUI = QtGui,QMainWindow()
testUI.show()

Similar idea, instead of typing out the base class yourself, you let uic do it for you (plus loads the ui in the process)

baseUI = "path/to/ui/file"
from_class, base_class = uic.loadUiType(baseUI)

Then you subclass your own class from those 2.

class myUI( base_class , from_class ):

    def __init__(self, parent=getMayaWindow()):
        super(base_class, self).__init__(parent)
        self.setupUI()

And that's where you tell your UI to become child of Maya's window. You can replaced the getMayaWindow() with None to see what happens.

Now to display your UI :

test = myUI()
test.show()

Next. You have a QPushButton named "btallsettings". We want it to fire off a command when clicked. So, add this to your __init__

self.btallsettings.clicked.connect(self.btallClicked)

and add a new def within the class

def btallClicked(self):
        # do whatever you want
        cmds.sphere()

 You can read more about each class in nokia's website. http://doc.qt.nokia.com/4.7/index.html I found it a ton easier to understand than PyQt's riverbank site.

I hope that helps clarify some basics! :)

Best regard,
Panupat.

Panupat Chongstitwattana

unread,
Nov 9, 2012, 7:14:38 AM11/9/12
to python_in...@googlegroups.com
Hey Daz.

self.setupUI() <---- should use a small letter i. Sorry.

self.seuoUi()

Daz

unread,
Nov 9, 2012, 8:04:23 AM11/9/12
to python_in...@googlegroups.com
Heya

Right that sort of explains a lot ! I have to run few tests and try it out more in depth.... 

In any case I'm bit lost with ur explanation... I'm getting Unexpected indent in my __init__ function for some reason, I thought I can just type there anything I want to have in __ini__ as my template or so :S

I also finally got an idea how I link buttons... or I think. I define the link of button name and option in __init__ and then I can create function for it later on...  gotta read manual instead of spamming u gurys here :)

anyway here is a bit of work attempt I did... 

from PyQt4 import QtCore, QtGui, uic
import maya.OpenMayaUI as mui
import sip

baseUI = "C:\Users\dmakowski\Desktop\PyQTtests\My\ui\ui3.ui"
baseUIClass, baseUIWidget = uic.loadUiType(baseUI)

class Ui_MainWindow(baseUIWidget, baseUIClass):
def __init__(self,parent=None):
super(baseUIWidget, self).__init__(parent)
self.setupUi(self)
self.btallsettings.clicked.connect(self.btallClicked)




def getMayaWindow():
ptr = mui.MQtUtil.mainWindow()
return sip.wrapinstance(long(ptr), QtCore.QObject)

def mayaMain():
maya_ui1_window = Ui_MainWindow(getMayaWindow())
maya_ui1_window.show()

def btallClicked(self):
# do whatever you want
cmds.sphere()

mayaMain()

Panupat Chongstitwattana

unread,
Nov 9, 2012, 10:24:43 AM11/9/12
to python_in...@googlegroups.com
Don't rely on e-mails indenting :)

Here, I uploaded the source to dropbox. Place both files together in your documents/maya/version/script directory

https://www.dropbox.com/s/8brcnttgasz4gbm/daz.py
https://www.dropbox.com/s/1k6ojz2ivthrh0f/ui3.ui

Inside Maya, execute this

import daz
reload(daz)

daz.launch

Best regard,
Panupat.



Panupat Chongstitwattana

unread,
Nov 9, 2012, 10:32:01 AM11/9/12
to python_in...@googlegroups.com
grr... forgot parentesis

import daz
reload(daz)

daz.launch()


Justin Israel

unread,
Nov 9, 2012, 11:54:21 AM11/9/12
to python_in...@googlegroups.com
Wanted to point this out as well. The way you are calling super() needs a slight adjustment.

#Original:
class Ui_MainWindow(baseUIWidget, baseUIClass):
    def __init__(self,parent=None):
        super(baseUIWidget, self).__init__(parent)

This is calling the super class of baseUIWidget, which means you aren't initializing the super of Ui_MainWindow, but rather the super class of baseUIWidget, which could cause issues. 
Example:
If baseUIWidget is a QDialog, then you are actually initializing from a QWidget

#Adjustment
class Ui_MainWindow(baseUIWidget, baseUIClass):
    def __init__(self,parent=None):
        super(Ui_MainWindow, self).__init__(parent)

You call super on your actual classname, and super will determine the proper superclass to return.



Daz

unread,
Nov 9, 2012, 11:58:28 AM11/9/12
to python_in...@googlegroups.com
Heya

Yay that runs like dream, I've edited beginning slightly to use my general folder structure and so on and so far I/you got the button working ! :) 

The steps start now that I want to hock up second button... for some reason I get error with this part of code... any hints why ? :/

# when click this button, run function "btallClicked"
self.btnAllLightSettings.clicked.connect( self.btnAllLightSettingsClicked )
self.btnCut.clicked.connect( self.btnCutClicked )

# show the UI
self.show()


def btnAllLightSettingsClicked( self ):
#anything you want. Let's say, create a sphere
settings_Lights.allSettings()

def btnCutClicked(self):
settings_Lights.sampleMax

I end up with 

# Error: unexpected indent
# File "<maya console>", line 48
# self.btnCut.clicked.connect( self.btnCutClicked )
# ^
# IndentationError: unexpected indent #

which is very strange. It appears just after I past the second self.btnCut.clicked command. Can I stack them together 1 after another or is there some more complex way of loading them up? :s

I thought I have to script a command for each button for each function to happen... 

Justin Israel

unread,
Nov 9, 2012, 12:10:24 PM11/9/12
to python_in...@googlegroups.com
What are you using for an editor? It seems that you are just having copy/paste errors and maybe a mixture of spaces vs tabs.
Your best bet is to not use the emails for code. Make use of either pastebin.com, or https://gist.github.com/, or any online code site where people can look at a large amount of code with proper indents.
It will help you avoid the indent problem.



Daz

unread,
Nov 9, 2012, 12:19:48 PM11/9/12
to python_in...@googlegroups.com
Uh true ! 

Here we go... just the part with self.btnCut button that I'm trying to add to list that dont want to work... :/


Justin Israel

unread,
Nov 9, 2012, 12:25:26 PM11/9/12
to python_in...@googlegroups.com
Your second button is indented too far:

# line 43
self.btnCut.clicked.connect( self.btnCutClicked )

Dedent it one.

Daz

unread,
Nov 9, 2012, 12:33:37 PM11/9/12
to python_in...@googlegroups.com
Bah it works... cant believe I mist the spaces :/ thanks ! Off to code 30 buttons now... :]

Justin Israel

unread,
Nov 9, 2012, 12:36:14 PM11/9/12
to python_in...@googlegroups.com
Awesome! May your button coding experience be dynamic, and rewarding!

On Nov 9, 2012, at 9:19 AM, Daz wrote:

Daz

unread,
Nov 9, 2012, 8:28:24 PM11/9/12
to python_in...@googlegroups.com
Heya

Just run in to weird issue... It works fine at work but not at home not, I did try to install python and other files the same as I did at work... Any idea why I get this error when starting script maybe?


Thanks, bye.

Justin Israel

unread,
Nov 9, 2012, 8:37:17 PM11/9/12
to python_in...@googlegroups.com
The last line of the traceback is a pretty good indication:
# IOError: [Errno 13] Permission denied: 'D:/Scripts/Daz_Tools/Beta' #

You have limited permissions on that location, which it is trying to read to parse your UI file.

Daz

unread,
Nov 9, 2012, 8:42:40 PM11/9/12
to python_in...@googlegroups.com
Yup I know that just dont know how that can happen if I'm admin on my PC hehe :)

Even running maya as admin dont help arrr...

But naming correctly path worked ehhh... note for future, dont code at 2.00 in the morning whhh

Thanks ! Sorry for wasting time :/

Daz

unread,
Nov 12, 2012, 4:37:17 AM11/12/12
to python_in...@googlegroups.com
Heya

Just a little hiccup further down the line... tried googling it but nothing pop up... except this topic so pretty funny hehe :)

Created resource container in QT for my logo widget... and I'm getting error when trying to start up the script... any idea how to add it to script? I try adding the patch to globals but that did not work :/, 



thanks, bye.

Panupat Chongstitwattana

unread,
Nov 12, 2012, 4:42:07 AM11/12/12
to python_in...@googlegroups.com
Would be helpful if you provide us with the error message you're getting.

From a quick look, are you getting an I/O Error: 2 or something similar? From this line

baseUI  = "C:\Users\dmakowski\Desktop\PyQTtests\My\ui\ui6.ui"

Text with backslashes usually get interpret into special characters. You might want to change that to double slashes

baseUI  = "C:\\Users\\dmakowski\\Desktop\\PyQTtests\\My\\ui\\ui6.ui"

Panupat.

Daz

unread,
Nov 12, 2012, 6:27:01 AM11/12/12
to python_in...@googlegroups.com
Heya

Its in pastebin at the very bottom. That whats I get nothing else. 

Copy here just in case :)
# Error: No module named ResourcesQTRecomFarmhouseTools_rc

# Traceback (most recent call last):

# File "<maya console>", line 20, in <module>

# File "C:\Program Files\Autodesk\Maya2013\Python\lib\site-packages\PyQt4\uic\__init__.py", line 203, in loadUiType

# exec(code_string.getvalue(), ui_globals)

# File "<string>", line 25, in <module>

# ImportError: No module named ResourcesQTRecomFarmhouseTools_rc #

Thanks !

Justin Israel

unread,
Nov 12, 2012, 10:30:24 AM11/12/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
It is expecting to find your resource python file in the path, named ResourcesQTRecomFarmhouseTools_rc.py
Either you named it something different when you converted it from the qrc file, or you forgot to convert it using pyrcc4

Also I would recommend using forward slashing for paths regardless of operating system. Python handles it just fine for windows, which makes it universal and you don't have to escape them. 

Daz

unread,
Nov 12, 2012, 11:17:03 AM11/12/12
to python_in...@googlegroups.com
Heya

ah no I did not convert it, did not know I have to... will google it and try it cracking :)

Thanks!
Reply all
Reply to author
Forward
0 new messages