import/reload from inside py script?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 02 February 2011   #1
import/reload from inside py script?

I've setup a simple UI w/a textScrollBox so I can keep a list of my scripts and reload any of them by clicking. But the 'import' and 'reload' statements don't like that idea - they need a module.
When I click on the name of a script in the list, this is what happens:
Can anyone help?
def reloadScript(): 
     	# Query textScrollList for selected script: 
     	selectedScript = cmds.textScrollList('scriptList', q=1, si=1)[0] # Items returned as a list 
     	print selectedScript #This works and prints the name of the hilighted script in the textScrollBox 
     	import selectedScript 
     	# ImportError: No module named selectedScript # 
     	reload (selectedScript) 
     		# TypeError: reload() argument must be module # 
Thanks.
 
Old 02 February 2011   #2
doing the equivalent of an eval() in MEL with a catenation would do it:
exec('import '+selectedScript)
although knowing the strange ways python works, I can't guarentee this is an advisable approach... but it's only Maya...
:nathaN
 
Old 02 February 2011   #3

 selectedScript = 'someScript'
 module = __import__(selectedScript)
 reload(module)
__________________
vimeo
blog
 
Old 02 February 2011   #4
Originally Posted by mlefevre:
selectedScript = 'someScript'
  module = __import__(selectedScript)
  reload(module)

Perfect - even reloads itself.
Thanks much.
 
Old 02 February 2011   #5
the best solution is to go the "unload" way instead of "reload". deleting modules from sys.modules makes python forget about a module that was loaded.

something like that, might contain typos:



#unload module, need a full qualified module name with a package as well
import sys
try:
   del(sys.modules['myPackage.myModule'])
except:
   # exception catching is there just in case such module was not loaded in first place,
   # in which case this part of code just does nothing
   pass


#importing myModule here; this will actually run the import everytime this script is rerun
from myPackage.myModule import MyClass
myInstance = MyClass()


the "unload" method works best because once you have a lot of scripts cross-referencing each other, you would not know in which order you should be reloading them. at work, we just got a routine that just unloads every imported module by package name, which we just run before executing test code - haven't failed us for years now. can't post any code though, but it should not be hard for you to figure out from here.
__________________
Victor
ngskintools.com - skinning plugin for Autodesk Maya
 
Old 02 February 2011   #6
@uiron:
Thanks for that advice. So far I only use scripts that have all the code in them - I haven't learned to use packages, modules, and classes.
Its my impression that all of the modules called by a UI exist within the scope of that UI, so wouldn't all the modules in a script be removed when a UI window is closed?
if cmds.window(winName, q=True, ex=True):
cmds.deleteUI(winName)

Coincidently, because I do re-use defs and UI panels, I've been thinking about going modular w/my scripts, I'm going to post about that separately.
Thanks again.
 
Old 02 February 2011   #7
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 03:21 AM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.