Hello! I’m new to learning how to use QTDesigner to make UI’s for Maya Python tools. Does anyone know the correct way to structure things/import tools such that Maya can recognize where the UI file is when it’s called from my python script? Up to this point I’ve only loaded in single file python scripts to make buttons. Any simple examples would be much appreciated! Thanks!
I’m assuming that means you want to directly load the .ui files that are saved out from Designer?
The trick is using that __file__
variable that holds the path to the current file so you can search relative to that.
Full disclosure: I do this in an uncommon way, but here’s how I do it in my own projects.
To make it work, you’ll need
Mottosso’s Qt.py for QtCompat.loadUi
Lines 26, 28, 47-59 from traversalDialog.py
And that getUiFile
function from utils.py which looks at the name of the file, and grabs the corresponding .ui file from the ui
folder.
Thanks! This makes sense, but I do have a follow-up question. When I try to do something like this I get the error "NameError: name ‘file’ is not defined # ". I understand this is because I’m trying to run it in the script editor, but I should be running it from a module… but my question is how do I correctly “run it from a module”?
My current set-up is that I have my python script and another python script that imports the module. They are in the same folder. I ensure the path to that folder is in the python path. I load my second script into the script editor and run it, but it says module not found…
Are you missing the __init__.py
file?
I made this folder structure and dropped it on my desktop
TopLevelFolderAddedToPythonPath
FolderForYourModule
__init__.py
myAwesomeDialog.py
__init__.py
is completely empty
myAwesomeDialog.py
contains a single do-nothing class class MyAwesomeDialog: pass
Then I ran this code in the Maya script editor.
import sys
folder = r'C:\Users\Tyler\Desktop\TopLevelFolderAddedToPythonPath'
if folder not in sys.path:
sys.path.append(folder)
from FolderForYourModule.myAwesomeDialog import MyAwesomeDialog
Ah, I see that works! Thank you so much!