import maya.cmds scope

455 views
Skip to first unread message

ben cowell-thomas

unread,
Sep 18, 2012, 4:59:22 AM9/18/12
to python_in...@googlegroups.com
Hello,

A basic question that has me stumped. I'm using userSetup.py and I'm confused about scope. I've probably overcomplicated a simple task .. but I'm trying to understand how userSetup.py works so I've put part of my setup into a module. 

 userSetup.py contains the following:

import maya.cmds as cmds
print "--- Executing C:\Users\ben.c\Documents\maya\scripts\userSetup.py ---"
import eclipseConnection
eclipseConnection.startUpFunc()

eclipseConnection.py contains:

import maya.utils as utils

def startUpFunc():
print "--- Executing C:\Users\ben.c\Documents\maya\scripts\eclipseConnection.py ---"
if cmds.commandPort(':7720', q=True) !=1:
cmds.commandPort(n=':7720', eo = False, nr = True)

Firstly I don't ever see the result of the print commands on startup. Is this normal ?

Secondly and more importantly I find that when troubleshooting, if I manually execute eclipseConnection.startUpFunc() after startup, Maya reports: 

# Error: NameError: global name 'cmds' is not defined #

Now I would expect cmds to work fine within eclipseConnection as if I type 'cmds' manually I see:

# Result: <module 'maya.cmds' from 'C:\Program Files\Autodesk\Maya2011\Python\lib\site-packages\maya\cmds\__init__.py'> #


My questions are .. why don't I see the result of the print commands on startup (even though I know userSetup.py is running as cmds exists), and secondly why can't my module access maya.cmds even though I believe it be defined globablly ?

thank you
ben 

Justin Israel

unread,
Sep 18, 2012, 11:27:29 AM9/18/12
to python_in...@googlegroups.com
For the print statement, I believe the script editor is not yet connected to stdout at the time you call print in userSetup.py
If, for instance, you check the Console app on OSX you will see those print statements when you start Maya. If you were to print deferred until the next idle moment, then you would see them in the Script Editor:

import maya.utils
maya.utils.executeDeferred('print "FOO"')

You are correct in your second question that it is a matter of scope. Python modules each have their own scope. You must explicitly import other modules to bring them into scope. They will only import once per app, as subsequent imports will just lookup the name.
userSetup.py is not imported into Maya. It is executed into Maya's global app namespace, which is the same namespace as the script editor. If you include import statements in userSetup.py, they will be available from the Script Editor. But your other scripts always need to import them. This is more of a general python matter as opposed to being Maya-specific.
I add the import statements to my userSetup for convenience when developing, so that I don't have to constantly import them to the script editor each time.



Mr.sp jangid

unread,
Sep 18, 2012, 1:18:55 PM9/18/12
to python_in...@googlegroups.com
please unsubscribe me by this group

thnx

Justin Israel

unread,
Sep 18, 2012, 1:34:15 PM9/18/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
Go to your account and manage your group submissions:

ben cowell-thomas

unread,
Sep 20, 2012, 1:09:59 AM9/20/12
to python_in...@googlegroups.com
Brilliant, thank you Justin .. that's helped explain two very confusing issues for me.

So in general would it be considered good practice to include import maya.cmds as cmds etc at the top of every module or is this wasteful ?

Thanks
ben

Justin Israel

unread,
Sep 20, 2012, 1:41:22 AM9/20/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
It wouldn't be wasteful. It would be required :)
Each module needs to import the modules it intends to use. 
Even if you import 50 scripts that import maya.cmds, it will only load it once. The rest of the time it will be looked up in the existing dict of modules. 
So ya.... Import away! :)
Reply all
Reply to author
Forward
0 new messages