Python says module doesn't exist... I say it does!

51 views
Skip to first unread message

er...@embodee.com

unread,
May 13, 2019, 5:13:25 PM5/13/19
to Python Programming for Autodesk Maya
Hello all!

I am much newer to scripting in Python and my experience is almost exclusively limited to writing tools for use in a Maya production pipeline for which I am the exclusive author. So please excuse the rudimentary nature of the code! I am learning best practices as I go. :)

Here is my issue: I have a "toolsMain" script which I use to call functions from a "userInterfaceElements" script for UI element creation. From there, the commands on the buttons and widgets will call functions from a "modelingTools" script which I import as "MOD". When I run the scripts in Maya, the UI loads correctly, but when I press the "Merge" button on the UI window I get the following error: # Error: NameError: file <maya console> line 1: name 'MOD' is not defined #

But I know it does. I have quadruple checked my spelling and syntax multiple times, and I am beginning to think something is happening that I don't quite understand and haven't seen before. I have successfully imported scripts, passed arguments between scripts and functions in the past, but this seems new and different and I can't quite figure it out. Any help would be greatly appreciated!!! Thanks in advance!

The code for the 3 scripts is below in its simplest form for cut and paste purposes:

"toolsMain.py"
[python]
import maya.cmds as mc
import userInterfaceElements as ELEM
reload (ELEM)

def toolsMain ():
if (mc.window("toolsWindowMain", exists = True)):
mc.deleteUI("toolsWindowMain")
toolsWindow = mc.window(title="Tools Window", iconName= "toolsWindow", widthHeight=(400, 500))
scrollMain = mc.scrollLayout("ScrollLayoutMain_01", w=390, hst=16, vst=16)
tabsMain = mc.tabLayout("TabLayoutMain_01", imw=5, imh=5)

##TAB 1 - Main
tab_1 = ELEM.stdColumnLayout("ColumnLayout_01", "TabLayoutMain_01")

## Modeling Frame ##
ELEM.stdFrameLayout("ModelingFrame_01", "ColumnLayout_01", "Modeling")
ELEM.stdRowColumnLayout("ModelingRowColumn_01", "ModelingFrame_01")

#Modeling Buttons
ELEM.modelingButton("MergeButton_01", "Merge", "merge", "MOD.doMergeWindow()")

#Organize Tabs
mc.tabLayout(tabsMain, edit=True, tabLabel=((tab_1, "Main")))

mc.showWindow(toolsWindow)
[/python]

"userInterfaceElements.py"
[python]
import maya.cmds as mc
import modelingTools as MOD
reload (MOD)

def stdColumnLayout(name, daddy): #(name, parent)
mc.columnLayout(name, parent=daddy, rowSpacing=5, columnWidth=390)
return name

def stdFrameLayout(name, daddy, callIt): #(name, parent, label)
mc.frameLayout(name, parent=daddy, label=callIt, borderVisible=True, collapsable=True, width=390)

def stdRowColumnLayout(name, daddy): #(name, parent)
mc.rowColumnLayout(name, parent=daddy, numberOfColumns=3, rowSpacing=[10, 5])

def modelingButton(name, callIt, tip, runIt): #(name, label, annotation, command)
toolTip = "DEFAULT"
mc.button(name, parent="ModelingRowColumn_01", label=callIt, annotation=toolTip, command=runIt, enableBackground=True, backgroundColor=[0.68, 0.76, 0.66], width=125, height=30)
[/python]

"modelingTools.py"
[python]
def doMergeWindow ():
print "POPPING UP A MERGE WINDOW NOW..."
[/python]

Justin Israel

unread,
May 13, 2019, 7:38:40 PM5/13/19
to python_in...@googlegroups.com
I would recommend switching to passing python objects as your callbacks instead of strings that have to be evaluated:

This:
     command=MOD.doMergeWindow
Instead of this:
     command="MOD.doMergeWindow()"

If you pass a string, you are relying on the python scopes to be correct at the time the command string is evaluated. But if you already have MOD imported, it is much more reliable to just pass the reference to the function (dont call it; just pass the object). 

Also I would recommend removing the reload() from the top of your production scripts. It is also a source of headaches and should only be used for testing.

 

--
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/8eb43586-5a70-4380-b2f4-504d80e1c31d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

er...@embodee.com

unread,
May 13, 2019, 11:28:29 PM5/13/19
to Python Programming for Autodesk Maya
Thank you, Justin! I will play around with it using your suggestions.

Alon Zaslavsky

unread,
May 14, 2019, 2:44:44 PM5/14/19
to python_in...@googlegroups.com
Hi all,

I'm trying to fix our bonesTools. All the files are here:
/tip/rnd/people/marissa/puppet-2.x/modules/python/tip/maya/studio/tools/bonesTool

In Qt Designer the icons appear in the UI. But when the tool run in Maya the icons don't show. I've read online that I need to set them up in a qrc resource file. I've done this, but still no luck.

Anyone know what I'm missing?

Thanks!

Justin Israel

unread,
May 14, 2019, 3:54:07 PM5/14/19
to python_in...@googlegroups.com
There isn't enough info here to know what you are missing. Having the icons compiled into the qrc file is the first required step. What did you do after that to consume the qrc file in your tool? 


Thanks!

--
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.

Alon Zaslavsky

unread,
May 14, 2019, 5:56:11 PM5/14/19
to python_in...@googlegroups.com
I just set the qrc file as the resource file for my tool ui file. Saved. And re-ran the tool.
I also learned I could compile the qrc file into a py file, but don't know where to go from there.

Any advice would be appreciated.


For more options, visit https://groups.google.com/d/optout.


--
Alon Zaslavsky
Puppet TD

Justin Israel

unread,
May 14, 2019, 7:14:41 PM5/14/19
to python_in...@googlegroups.com
Regardless of whether you load a qrc or convert it to python and import that, the next step would be to actually use the resources. Otherwise you are still accessing the filesystem, as the resource file doesn't automatically re-route your file access:


The format is to use paths like ":/resource/path/to/file.png"



Marcus Ottosson

unread,
May 15, 2019, 11:21:11 AM5/15/19
to python_in...@googlegroups.com

Alternatively, don’t use or convert any qrc files, and use absolute paths to your icons.

I never got the appeal of qrc files; they are not mandatory. With them, you’ll need to recompile whenever you change your graphics, and if you keep your graphics in the same repository as your qrc files you’ve doubled the repository size. Absolute paths can be achieved relative your repository via e.g. os.path.join(os.path.dirname(__file__), "resources", "my_icon.png")


Alon Zaslavsky

unread,
May 20, 2019, 6:28:59 PM5/20/19
to python_in...@googlegroups.com
Hi Marcus,

I thought I fixed my icon visibility issue, but after logging out and back in they don't load anymore.
These are png files in the same directory as both the UI and PY file.

What solutions do you recommend I try?

All the files are here:
/tip/rnd/people/marissa/puppet-2.x/modules/python/tip/maya/studio/tools/bonesTool/

Hope you can help. Cheers and thanks!


For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages