- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
pathOfFiles = "S:/My/Custom/Folder/" ignoreList = ["sharedUserSetup.py", "__init__.py", "importPython.py"] files = cmds.getFileList( folder=pathOfFiles, filespec='*.py' )
def importPy(): if len(files) == 0: cmds.warning("No files found") else: for f in files: if f in ignoreList: continue module = (os.path.splitext(f)[0]) exec( 'import ' + module)
I'm trying to "source" my python scripts when I first start a Maya session.
In this code I'm scanning my custom folder path and bringing out all of the python files and then attempting to import them. This code works perfectly fine if I execute it through Maya's command line window, however when I try and access this function through a UserSetup.py in a maya.utils.executeDeferred function it only works for that one instance. If I then try and run one of my scripts, for example, myScript.makeObject() through the command line I will get the error:
# Error: NameError: file <maya console> line 1: name 'myScript' is not defined #
However if I do:
import importPython myScript.makeObject()
I will be able to continuously call myScript.makeObject() and the script will execute as expected.
Could somebody explain why the python script isn't able to be called continuously using this start up method? I imagine what I'm doing is I'm importing the functions of the folder scripts in to the importPy() function rather than my Maya session? So how do I stop doing that?
Likewise if there is a better way to deal with this I'd love to hear it.
Thanks!
Solved! Go to Solution.