Re: Clearing Maya Memory of Scripts (python)

3,205 views
Skip to first unread message

jdob

unread,
Jun 14, 2012, 12:17:34 PM6/14/12
to python_in...@googlegroups.com
Hi Mat-

Tough to say what the problem is without more info.  There should be no need to "clear Mayas memory of things that happened in the script editor" but maybe I'm misunderstanding what you're trying to do.  Can you post a short example of your code?  Also, can you turn on "Show Stack Trace" (in the script editor's Edit menu) and make it fail again?  You should get some useful info if something is failing in python land.

Justin Israel

unread,
Jun 14, 2012, 2:42:38 PM6/14/12
to python_in...@googlegroups.com
Maya runs python as a persistant environment. If your script is doing things on a global level, then those would continue to live on. Also like you said, if you are setting up other global callbacks like scriptJobs or attaching to existing UI elements and not cleaning those up, they too would be a persistant issue.

But yea, we would need to see some more specific examples of what you are doing and how you are doing it. Ideally your data structures should be bound to instances of classes or die withe the scope of a function. If your script fails, there shouldn't be anything persistant. Are you using global variables in your script? 

On Thu, Jun 14, 2012 at 9:17 AM, jdob <dobs...@googlemail.com> wrote:
Hi Mat-

Tough to say what the problem is without more info.  There should be no need to "clear Mayas memory of things that happened in the script editor" but maybe I'm misunderstanding what you're trying to do.  Can you post a short example of your code?  Also, can you turn on "Show Stack Trace" (in the script editor's Edit menu) and make it fail again?  You should get some useful info if something is failing in python land.

Mathew Schwartz

unread,
Jun 15, 2012, 10:39:07 AM6/15/12
to python_in...@googlegroups.com
Okay I see,

I think what I am wondering then is if there is a way to clear everything that was done, such as a global variable.  Its not just one script thats doing it, but the best example I can give is when the tooltip is activated.  Typing "cmd" does not give any options, but if I run "import maya.cmds as cmds", the even when I create a new python tab, "cmd" brings up the cmds option.  I need to restart to get rid of the cmds option under tooltip.  So the question is, how can restart the python environment without restarting Maya?

Thanks

m

jdob

unread,
Jun 15, 2012, 12:16:34 PM6/15/12
to python_in...@googlegroups.com
I don't understand your example - what do you mean by "the cmds option under tooltip"?

The script editor's python tabs are all talking to the same python interpreter - there is only one per session.  So if you import a module in one tab, it will be there in all of the tabs.

-jon

Justin Israel

unread,
Jun 15, 2012, 1:20:27 PM6/15/12
to python_in...@googlegroups.com
I think the best suggestion, without seeing your code examples, it what I mentioned before. Don't do things in the global scope. Its one thing to try out tests in the script editor. But when you actually write scripts, encapsulate your code in class or function structures.

Beyond that...we need to see something you are actually doing.

Mathew Schwartz

unread,
Jun 15, 2012, 2:47:02 PM6/15/12
to python_in...@googlegroups.com
So if im testing a scripts then the only way to remove the variables is to restart?  For the tooltip, the attached images are the example.  In one tab i type "n" it gives me the list of options for "n", in a different tab "import numpy as np" then I run that, and now anytime i type "n" i get the "np" option.  What if I dont want to be importing that anymore.  what if during testing, I change a module, i thought there was a reload() command for reloading a module, what if I want to unload it.  Does this make more sense?

Thanks

m
1.jpg
2.jpg
3.jpg

Justin Israel

unread,
Jun 15, 2012, 2:50:59 PM6/15/12
to python_in...@googlegroups.com
This is exactly how it works, yes. 
If you import numpy as np in one tab it is now in memory in the single python interpreter instance of Maya. Any tab will reflect this.

If you are testing a module and make changes to it, then you can use the reload() command.
import myModule
# make changes
reload(myModule)

There isn't really a clean way to just flat out delete a module, as it can still have remaining references in other areas. You could just say: del myModule, which will kill that global reference. But anything else that imported or used it will still have access to it in memory.

Justin Israel

unread,
Jun 16, 2012, 12:23:30 PM6/16/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
Limited answer:  del cmds
General answer: restart maya

Otherwise you have to track everything you create, delete objects via the del command, and make sure nothing else is using them in their own code

There is no magic method to "reset" the environment because there is no basline to consider a reset point. The env can be modified even from mayas startup process with scripts. 

See how far the del command will get you. Then, restart

Mathew Schwartz

unread,
Jun 18, 2012, 12:51:06 PM6/18/12
to python_in...@googlegroups.com
thanks justin,

yea ive been trying to figure out what im trying to do in terms of python.  I guess i really want a "exit()" function in the maya environment and then be able to access "python" again.

it seems to just be annoying when using the tooltip, I was hoping to get it working to my liking to save time when remembering some of the maya flags, but maybe ill setup command completion in an external editor instead.

i have a more specific question.. but ill start a new thread

Thanks again guys.

damon shelton

unread,
Jun 18, 2012, 12:55:39 PM6/18/12
to python_in...@googlegroups.com
you can always wrap your code into a run fiunction during testing, that way all variables are declared within that function - kind of like wrapping mel code in the editor with curly braces to make it into a local space and not global.
Reply all
Reply to author
Forward
0 new messages