I ran into a ‘module’ object has no attribute error when trying to execute a function in Maya through a custom menu. Looking for some help to sort this out.
env: Windows 7, Maya 2013
I set up a custom folder structure as below:
main_folder
script_folder
[INDENT]init.py[/INDENT]
[INDENT]function01.py[/INDENT]
userSetup.py
I then set up maya script environment variable to specify the path to the userSetup.py.
import maya.cmds as cmds
try:
from function01 import *
except:
print 'something is wrong'
It seems to me the code is executed just fine as I am able to see and interact with the custom menu created in Maya until when I execute the function01, Maya throws me a ‘module’ object has no attribute ‘function01’ error. Did I not import the function01 properly or something?
import maya.cmds as cmds
try:
from function01 import *
except:
print 'something is wrong'
It seems to me the code is executed just fine as I am able to see and interact with the custom menu created in Maya until when I execute the function01, Maya throws me a ‘module’ object has no attribute ‘function01’ error. Did I not import the function01 properly or something?
Why are you importing * from init. It makes it very difficult to see where things are coming from with * alone, but to further mask it in init ? Couldn’t you just do this:
Multiple * imports run the risk of redefining things accidentally - if function01.py and function02.py have identically named functions your version will only have the last defined one in vray_script_folder
You’re also making extra work for yourself. The only reason to import the parent package first is if it does some silent initialization that you need to have done before getting the children - otherwise it’s just an organizational construct. You could just do this in your handler:
def sampleFunction():
import vray_script_folder.function01 as func1 # to avoid the long names
# (but I like long names - they make it clear what's going on :) )
### menu creation stuff
cmds.menuItem(p=customMenu, l='function label', c=func1.function01) # just assign the callable, don't use the strings
Lastly, by try/catching the import error you don’t know if you’ve actually got the names at all. Let them fail so you know they are broken and fix them. “Fail early, fail often” is the mantra