Getting QT to work in Maya (2014)

Hey guys,

im following the digital tutors tutorial to crack QT designer but i have hit a block.

im trying to run the script using this:

import sys
Dir = 'c:/dtQT'
if Dir not in sys.path:
    sys.path.append(Dir)
try: reload(masterScript)
except: import masterScript
masterScript.main()

but i am getting this error: # Error: SyntaxError: file line 6: invalid syntax

My py script is currently this:

#
#USER Interface creation for Maya
#

from PyQt4 import QtGui, QtCore, uic
from pymel.core import *
import pymel.core as pm
from pymel import *
import maya.cmds as cmds
from functools import partial

#Path to the designer UI file
ui_filename = 'c:/dtQT/test_ui.ui'
form_class, base_class = uic.loadUiType(ui_filename)

#Interface class
class masterScript_ui(base_class, form_class):
        def__init__(self):
	           super(base_class, self).__init__()
	           self.setupUi(self)
		       self.setObjectName('test_ui')
		       self.setDockNestingEnabled(True)
		       self.connectInterface()
			 
		def connectInterface(self):																				
		      QtCore.QObject.connect(self.SelectionVertex, QtCore.SIGNAL("clicked()"),self.SelectionVertexWin)
			  QtCore.QObject.connect(self.SelectionEdge, QtCore.SIGNAL("clicked()"),self.SelectionEdgeWin)
			  #QtCore.QObject.connect(self.SelectionBorder, QtCore.SIGNAL("clicked()"),self.SelectionBorderWin)
			  QtCore.QObject.connect(self.SelectionPolygon, QtCore.SIGNAL("clicked()"),self.SelectionPolygonWin)
			  QtCore.QObject.connect(self.SelectionElement, QtCore.SIGNAL("clicked()"),self.SelectionElementWin)
			  
		def SelectionVertexWin(self):
		        mel.eval('global proc SelectionVertex(){SelectVertexMask;}')
				mel.SelectionVertex()
		
        def SelectionEdgeWin(self):
		        mel.eval('global proc SelectionEdge(){SelectEdgeMask;}')
				mel.SelectionEdge()		
				
		#def SelectionBorderWin(self):
		        mel.eval('global proc SelectionBorder(){SelectEdgeMask;}')
				mel.SelectionBorder()		
				
		def SelectionPolygonWin(self):
		        mel.eval('global proc SelectionPolygon(){SelectFacetMask;}')
				mel.SelectionPolygon()		
				
		def SelectionElementWin(self):
		        mel.eval('global proc SelectionElement(){SelectToggleMode;}')
				mel.SelectionElement()		
				
				
				
#main
def main():
        global ui
		ui=test_ui()
		ui.show()
		
if__name__== "__main__":
   main()

My QT UI is called “test_ui.ui”

Really hope someone here can spot the problem because I am stumped!

Yeah this might be because of an error in the module being imported. I have had this before.

Double check all the indentation of your Python file.

I have a feeling it might be because of the fact that you have commented out this line

#def SelectionBorderWin(self):

and that commented line falls outside the code’s indentation. Try removing that line or indenting it to fit the block’s indentation and give it a run.

just tried that and no change. same syntax :confused:

I see your call to

ui=test_ui()

Where have you defined the method test_ui() ?

That should probably be masterScript_ui()

ah yes your correct. However, im still getting the syntax?

Still not cracked it. Gonna be going over this tomorrow all day i think

Ah hate it when that happens. Actually can you share your ui code (and any other code needed to run this) in http://pastebin.com/ or something? I will try debugging it in my end.

Hey man i am very grateful for your help, means alot man.

This = the code which should activate my py file which in turn opens my ui

import sys
Dir = 'c:/dtQT'
if Dir not in sys.path:
    sys.path.append(Dir)
try: reload(masterScript)
except: import masterScript
masterScript.main()

This equals the code which is being grabbed by maya

#
#USER Interface creation for Maya
#

from PyQt4 import QtGui, QtCore, uic
from pymel.core import *
import pymel.core as pm
from pymel import *
import maya.cmds as cmds
from functools import partial

#Path to the designer UI file
ui_filename = 'c:/dtQT/test_ui.ui'
form_class, base_class = uic.loadUiType(ui_filename)

#Interface class
class masterScript_ui (base_class, form_class):
        def__init__(self):
	           super(base_class, self).__init__()
	           self.setupUi(self)
		       self.setObjectName('masterScript_ui')
		       self.setDockNestingEnabled(True)
		       self.connectInterface()
			 
		def connectInterface(self):																				
		      QtCore.QObject.connect(self.SelectionVertex, QtCore.SIGNAL("clicked()"),self.SelectionVertexWin)
			  QtCore.QObject.connect(self.SelectionEdge, QtCore.SIGNAL("clicked()"),self.SelectionEdgeWin)
			  #QtCore.QObject.connect(self.SelectionBorder, QtCore.SIGNAL("clicked()"),self.SelectionBorderWin)
			  QtCore.QObject.connect(self.SelectionPolygon, QtCore.SIGNAL("clicked()"),self.SelectionPolygonWin)
			  QtCore.QObject.connect(self.SelectionElement, QtCore.SIGNAL("clicked()"),self.SelectionElementWin)
			  
		def SelectionVertexWin(self):
		        mel.eval('global proc SelectionVertex(){SelectVertexMask;}')
				mel.SelectionVertex()
		
                def SelectionEdgeWin(self):
		        mel.eval('global proc SelectionEdge(){SelectEdgeMask;}')
				mel.SelectionEdge()		
				
		#def SelectionBorderWin(self):
		        mel.eval('global proc SelectionBorder(){SelectEdgeMask;}')
				mel.SelectionBorder()		
				
		def SelectionPolygonWin(self):
		        mel.eval('global proc SelectionPolygon(){SelectFacetMask;}')
				mel.SelectionPolygon()		
				
		def SelectionElementWin(self):
		        mel.eval('global proc SelectionElement(){SelectToggleMode;}')
				mel.SelectionElement()		
				
				
				
#main
def main():
        global ui
		ui=masterScript_ui()
		ui.show()
		
if__name__== "__main__":
       main()

The ui code i have messaged you about :slight_smile:

Everything under the init is indented too far.

Hey So there were a bunch of indentation problems in the code that I fixed (here: http://pastebin.com/pTK8V4ZQ ). But the UI file you sent seems to be a C++ file (probably compiled from your .ui file). uic.loadUiType() needs a .ui file to run (which would be in XML format) and not the C++ file (that was generated FROM your .ui file). Can you send me that?

And I think sharing stuff via pastebin is better because tech-artists’ vbulletin has a bug of misformatting characters in code blocks.

Thanks guys, i am now using Notepad++ to have a clearer view of indentation :).

@Kartikg3 - i sent you a dropbox file of my ui :slight_smile:

To make things easier for myself i renamed everything the same as the tutorial so:

ui: masterScript_ui.ui
Python: masterScript.py

The results i am getting are:

Error: ImportError: file c:/dtQT\masterScript.py line 2: No module named PyQt4

Which means Maya is picking up my python script but it doesnt understand PyQT4? maybe i am using a more up to date version on qt?

Update:

So Maya 2014 ships with pyside which is why i am guessing i didnt ever need to import pyQt4?

After deleting this line i now get this error:

Error: NameError: file c:/dtQT\masterScript.py line 10: name ‘uic’ is not defined

which is refering to line 10 in my python code:

9:ui_filename = ‘C:\dtQT\3dsmaya_ui.ui’
10:form_class, base_class = uic.loadUiType(ui_filename) <-----

Yeah Maya 2014 ships with PySide. So you don’t need to build PyQt4 for it. Most of your code can stay the same, but few things need to change for it to work with PySide.

Check this piece of code out:
http://jason-parks.com/Downloads/PySide/loadUiType.py
taken from:
http://www.jason-parks.com/artoftech/?p=579

Also check out Rob’s QtShim: https://github.com/rgalanakis/practicalmayapython/blob/master/src/chapter5/qtshim.py
That will help you write code that works on both PyQt and PySide.

All the best.

Im back…god i have been busy with work. I would love to tell you guys what im working on :).

So i got a few hours spare today and thought i would return to the script! I looked at that piece of code but tbh, really not sure what to do with it? i pout it in the beggining of my code and i still get this:

Error: NameError: file c:/dtQT\masterScript.py line 10: name ‘uic’ is not defined

Is that becausue the path to the ui designer file is located in here?

with open(uiFile, 'r') as f:
			o = StringIO()
			frame = {}

Hi ben, I think I wrote that tutorial you’re following lol. I’ll see if i can figure out what’s going wrong. I’ll post if i figure something out.

-Tim

Would really appreciate it man :slight_smile:

Seems they changed alot from 2012 to 2014, here is an updated pyqt:

I’ll get a chance to try to run it after work tonight.

i think i have been looking down the wrong path all along. I was talking to my tech artist at work and i can just open my qt ui in maya with this:

import maya.cmds as cmds
path = """C:\\Users\\Ben\\Desktop\\Dropbox\\Scripts\	est_ui.ui"""

dialog1 = cmds.loadUI(f=path)
cmds.showWindow(dialog1)

def vertex(*args):
   print "vertex";
   
def edge(*args):
   print "edge";
   
def border(*args):
   print "border";
   
def polygon(*args):
   print "polygon";
   
def element(*args):
   print "element";

Now I know the buttons work I just need to put the code instead of the print?

what is the format I need to use for a easy command like this?

def vertex(*args): cmds.SelectVertexMask;

Sorry, been really busy at work! I did get it working just fine in 2015, but there shouldn’t be any difference between the two. Wanna pm me your email so I could send you a ui file and py file that works and we can go from there?